Haojian Wu | c253f8b | 2016-04-05 11:42:08 +0000 | [diff] [blame] | 1 | //===--- StaticDefinitionInAnonymousNamespaceCheck.cpp - clang-tidy--------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Haojian Wu | c253f8b | 2016-04-05 11:42:08 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "StaticDefinitionInAnonymousNamespaceCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/Lex/Lexer.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
Carlos Galvez | 7d2ea6c | 2023-01-14 17:40:54 +0000 | [diff] [blame] | 16 | namespace clang::tidy::readability { |
Haojian Wu | c253f8b | 2016-04-05 11:42:08 +0000 | [diff] [blame] | 17 | |
Haojian Wu | c253f8b | 2016-04-05 11:42:08 +0000 | [diff] [blame] | 18 | void StaticDefinitionInAnonymousNamespaceCheck::registerMatchers( |
| 19 | MatchFinder *Finder) { |
Haojian Wu | c242c8c | 2016-09-27 07:58:52 +0000 | [diff] [blame] | 20 | Finder->addMatcher( |
| 21 | namedDecl(anyOf(functionDecl(isDefinition(), isStaticStorageClass()), |
| 22 | varDecl(isDefinition(), isStaticStorageClass())), |
Evgeny Shulgin | 836950c | 2022-01-26 17:55:14 -0700 | [diff] [blame] | 23 | isInAnonymousNamespace()) |
Haojian Wu | c242c8c | 2016-09-27 07:58:52 +0000 | [diff] [blame] | 24 | .bind("static-def"), |
| 25 | this); |
Haojian Wu | c253f8b | 2016-04-05 11:42:08 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | void StaticDefinitionInAnonymousNamespaceCheck::check( |
| 29 | const MatchFinder::MatchResult &Result) { |
| 30 | const auto *Def = Result.Nodes.getNodeAs<NamedDecl>("static-def"); |
| 31 | // Skips all static definitions defined in Macro. |
| 32 | if (Def->getLocation().isMacroID()) |
| 33 | return; |
| 34 | |
| 35 | // Skips all static definitions in function scope. |
| 36 | const DeclContext *DC = Def->getDeclContext(); |
| 37 | if (DC->getDeclKind() != Decl::Namespace) |
| 38 | return; |
| 39 | |
| 40 | auto Diag = |
| 41 | diag(Def->getLocation(), "%0 is a static definition in " |
| 42 | "anonymous namespace; static is redundant here") |
| 43 | << Def; |
| 44 | Token Tok; |
| 45 | SourceLocation Loc = Def->getSourceRange().getBegin(); |
| 46 | while (Loc < Def->getSourceRange().getEnd() && |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 47 | !Lexer::getRawToken(Loc, Tok, *Result.SourceManager, getLangOpts(), |
| 48 | true)) { |
Haojian Wu | c253f8b | 2016-04-05 11:42:08 +0000 | [diff] [blame] | 49 | SourceRange TokenRange(Tok.getLocation(), Tok.getEndLoc()); |
Gabor Horvath | afad84c | 2016-09-24 02:13:45 +0000 | [diff] [blame] | 50 | StringRef SourceText = |
| 51 | Lexer::getSourceText(CharSourceRange::getTokenRange(TokenRange), |
| 52 | *Result.SourceManager, getLangOpts()); |
Haojian Wu | c253f8b | 2016-04-05 11:42:08 +0000 | [diff] [blame] | 53 | if (SourceText == "static") { |
| 54 | Diag << FixItHint::CreateRemoval(TokenRange); |
| 55 | break; |
| 56 | } |
| 57 | Loc = Tok.getEndLoc(); |
| 58 | } |
| 59 | } |
| 60 | |
Carlos Galvez | 7d2ea6c | 2023-01-14 17:40:54 +0000 | [diff] [blame] | 61 | } // namespace clang::tidy::readability |