annotate clang-tools-extra/clang-tidy/readability/StaticDefinitionInAnonymousNamespaceCheck.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- StaticDefinitionInAnonymousNamespaceCheck.cpp - clang-tidy--------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "StaticDefinitionInAnonymousNamespaceCheck.h"
anatofuz
parents:
diff changeset
10 #include "clang/AST/ASTContext.h"
anatofuz
parents:
diff changeset
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
anatofuz
parents:
diff changeset
12 #include "clang/Lex/Lexer.h"
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
15
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
16 namespace clang::tidy::readability {
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
17
150
anatofuz
parents:
diff changeset
18 void StaticDefinitionInAnonymousNamespaceCheck::registerMatchers(
anatofuz
parents:
diff changeset
19 MatchFinder *Finder) {
anatofuz
parents:
diff changeset
20 Finder->addMatcher(
anatofuz
parents:
diff changeset
21 namedDecl(anyOf(functionDecl(isDefinition(), isStaticStorageClass()),
anatofuz
parents:
diff changeset
22 varDecl(isDefinition(), isStaticStorageClass())),
236
c4bab56944e8 LLVM 16
kono
parents: 150
diff changeset
23 isInAnonymousNamespace())
150
anatofuz
parents:
diff changeset
24 .bind("static-def"),
anatofuz
parents:
diff changeset
25 this);
anatofuz
parents:
diff changeset
26 }
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 void StaticDefinitionInAnonymousNamespaceCheck::check(
anatofuz
parents:
diff changeset
29 const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
30 const auto *Def = Result.Nodes.getNodeAs<NamedDecl>("static-def");
anatofuz
parents:
diff changeset
31 // Skips all static definitions defined in Macro.
anatofuz
parents:
diff changeset
32 if (Def->getLocation().isMacroID())
anatofuz
parents:
diff changeset
33 return;
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 // Skips all static definitions in function scope.
anatofuz
parents:
diff changeset
36 const DeclContext *DC = Def->getDeclContext();
anatofuz
parents:
diff changeset
37 if (DC->getDeclKind() != Decl::Namespace)
anatofuz
parents:
diff changeset
38 return;
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 auto Diag =
anatofuz
parents:
diff changeset
41 diag(Def->getLocation(), "%0 is a static definition in "
anatofuz
parents:
diff changeset
42 "anonymous namespace; static is redundant here")
anatofuz
parents:
diff changeset
43 << Def;
anatofuz
parents:
diff changeset
44 Token Tok;
anatofuz
parents:
diff changeset
45 SourceLocation Loc = Def->getSourceRange().getBegin();
anatofuz
parents:
diff changeset
46 while (Loc < Def->getSourceRange().getEnd() &&
anatofuz
parents:
diff changeset
47 !Lexer::getRawToken(Loc, Tok, *Result.SourceManager, getLangOpts(),
anatofuz
parents:
diff changeset
48 true)) {
anatofuz
parents:
diff changeset
49 SourceRange TokenRange(Tok.getLocation(), Tok.getEndLoc());
anatofuz
parents:
diff changeset
50 StringRef SourceText =
anatofuz
parents:
diff changeset
51 Lexer::getSourceText(CharSourceRange::getTokenRange(TokenRange),
anatofuz
parents:
diff changeset
52 *Result.SourceManager, getLangOpts());
anatofuz
parents:
diff changeset
53 if (SourceText == "static") {
anatofuz
parents:
diff changeset
54 Diag << FixItHint::CreateRemoval(TokenRange);
anatofuz
parents:
diff changeset
55 break;
anatofuz
parents:
diff changeset
56 }
anatofuz
parents:
diff changeset
57 Loc = Tok.getEndLoc();
anatofuz
parents:
diff changeset
58 }
anatofuz
parents:
diff changeset
59 }
anatofuz
parents:
diff changeset
60
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
61 } // namespace clang::tidy::readability