| 1 | //===--- UseAnonymousNamespaceCheck.cpp - clang-tidy ----------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "UseAnonymousNamespaceCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | |
| 13 | using namespace clang::ast_matchers; |
| 14 | |
| 15 | namespace clang::tidy::misc { |
| 16 | namespace { |
| 17 | AST_POLYMORPHIC_MATCHER_P(isInHeaderFile, |
| 18 | AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, |
| 19 | VarDecl), |
| 20 | utils::FileExtensionsSet, HeaderFileExtensions) { |
| 21 | return utils::isExpansionLocInHeaderFile( |
| 22 | Node.getBeginLoc(), Finder->getASTContext().getSourceManager(), |
| 23 | HeaderFileExtensions); |
| 24 | } |
| 25 | |
| 26 | AST_MATCHER(FunctionDecl, isMemberFunction) { |
| 27 | return llvm::isa<CXXMethodDecl>(&Node); |
| 28 | } |
| 29 | AST_MATCHER(VarDecl, isStaticDataMember) { return Node.isStaticDataMember(); } |
| 30 | } // namespace |
| 31 | |
| 32 | UseAnonymousNamespaceCheck::UseAnonymousNamespaceCheck( |
| 33 | StringRef Name, ClangTidyContext *Context) |
| 34 | : ClangTidyCheck(Name, Context), |
| 35 | RawStringHeaderFileExtensions(Options.getLocalOrGlobal( |
| 36 | "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { |
| 37 | if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, |
| 38 | HeaderFileExtensions, |
| 39 | utils::defaultFileExtensionDelimiters())) { |
| 40 | this->configurationDiag("Invalid header file extension: '%0'") |
| 41 | << RawStringHeaderFileExtensions; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void UseAnonymousNamespaceCheck::storeOptions( |
| 46 | ClangTidyOptions::OptionMap &Opts) { |
| 47 | Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions); |
| 48 | } |
| 49 | |
| 50 | void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) { |
| 51 | Finder->addMatcher( |
| 52 | functionDecl(isStaticStorageClass(), |
| 53 | unless(anyOf(isInHeaderFile(HeaderFileExtensions), |
| 54 | isInAnonymousNamespace(), isMemberFunction()))) |
| 55 | .bind("x"), |
| 56 | this); |
| 57 | Finder->addMatcher( |
| 58 | varDecl(isStaticStorageClass(), |
| 59 | unless(anyOf(isInHeaderFile(HeaderFileExtensions), |
| 60 | isInAnonymousNamespace(), isStaticLocal(), |
| 61 | isStaticDataMember(), hasType(isConstQualified())))) |
| 62 | .bind("x"), |
| 63 | this); |
| 64 | } |
| 65 | |
| 66 | void UseAnonymousNamespaceCheck::check(const MatchFinder::MatchResult &Result) { |
| 67 | if (const auto *MatchedDecl = Result.Nodes.getNodeAs<NamedDecl>("x")) { |
| 68 | StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function"; |
| 69 | diag(MatchedDecl->getLocation(), |
| 70 | "%0 %1 declared 'static', move to anonymous namespace instead") |
| 71 | << Type << MatchedDecl; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | } // namespace clang::tidy::misc |