[clang-tools-extra] [clang] [clang-tidy] Added new check to detect redundant inline keyword (PR #73069)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 22 07:53:29 PST 2023


=?utf-8?q?Félix-Antoine?= Constantin
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/73069 at github.com>


================
@@ -0,0 +1,99 @@
+//===--- RedundantInlineSpecifierCheck.cpp -
+// clang-tidy----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RedundantInlineSpecifierCheck.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/Decl.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/DeclTemplate.h"
+#include "clang/AST/ExprCXX.h"
+#include "clang/ASTMatchers/ASTMatchers.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
+
+#include "../utils/LexerUtils.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::readability {
+
+static std::optional<SourceLocation>
+getInlineTokenLocation(SourceRange RangeLocation, const SourceManager &Sources,
+                       const LangOptions &LangOpts) {
+  SourceLocation CurrentLocation = RangeLocation.getBegin();
+  Token CurrentToken;
+  while (!Lexer::getRawToken(CurrentLocation, CurrentToken, Sources, LangOpts,
+                             true) &&
+         CurrentLocation < RangeLocation.getEnd() &&
+         CurrentToken.isNot(tok::eof)) {
+    if (CurrentToken.is(tok::raw_identifier)) {
+      if (CurrentToken.getRawIdentifier() == "inline") {
+        return CurrentToken.getLocation();
+      }
+    }
+    CurrentLocation = CurrentToken.getEndLoc();
+  }
+  return std::nullopt;
+}
+
+void RedundantInlineSpecifierCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+      functionDecl(unless(isExpansionInSystemHeader()), unless(isImplicit()),
+                   unless(hasAncestor(lambdaExpr())), isInline(),
+                   anyOf(isConstexpr(), isDeleted(),
+                         allOf(isDefinition(), hasAncestor(recordDecl()),
+                               unless(hasAncestor(cxxConstructorDecl())))))
+          .bind("fun_decl"),
+      this);
+
+  Finder->addMatcher(
+      varDecl(isInline(), unless(isImplicit()),
+              anyOf(allOf(isConstexpr(), unless(isStaticStorageClass())),
+                    hasAncestor(recordDecl())))
+          .bind("var_decl"),
+      this);
+
+  Finder->addMatcher(
+      functionTemplateDecl(has(functionDecl(isInline()))).bind("templ_decl"),
+      this);
+}
+
+template <typename T>
+void RedundantInlineSpecifierCheck::handleMatchedDecl(
+    const T *MatchedDecl, const SourceManager &Sources,
+    const MatchFinder::MatchResult &Result, const char *Message) {
+  if (auto Loc = getInlineTokenLocation(MatchedDecl->getSourceRange(), Sources,
----------------
EugeneZelenko wrote:

Please do not use `auto` if type is not explicitly stated in same statement or iterator.

https://github.com/llvm/llvm-project/pull/73069


More information about the cfe-commits mailing list