carlosgalvezp updated this revision to Diff 479383. carlosgalvezp added a comment.
Address comments Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D139113/new/ https://reviews.llvm.org/D139113 Files: clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/use-anonymous-namespace.h clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp +++ clang-tools-extra/test/clang-tidy/checkers/misc/use-anonymous-namespace.cpp @@ -1,4 +1,5 @@ -// RUN: %check_clang_tidy %s misc-use-anonymous-namespace %t +// RUN: %check_clang_tidy %s misc-use-anonymous-namespace %t -- -header-filter=.* -- -I%S/Inputs +#include "use-anonymous-namespace.h" static void f1(); // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: function 'f1' declared 'static', move to anonymous namespace instead [misc-use-anonymous-namespace] @@ -57,3 +58,7 @@ { static int x; } + +// OK +static const int v8{123}; +static constexpr int v9{123}; Index: clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/use-anonymous-namespace.h =================================================================== --- /dev/null +++ clang-tools-extra/test/clang-tidy/checkers/misc/Inputs/use-anonymous-namespace.h @@ -0,0 +1,3 @@ +// Should not warn here, do not require anonymous namespaces in headers +static int gv{123}; +static void gf(){} Index: clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h =================================================================== --- clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h +++ clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.h @@ -10,6 +10,7 @@ #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEANONYMOUSNAMESPACECHECK_H #include "../ClangTidyCheck.h" +#include "../utils/FileExtensionsUtils.h" namespace clang { namespace tidy { @@ -23,8 +24,7 @@ /// http://clang.llvm.org/extra/clang-tidy/checks/modernize/use-anonymous-namespace.html class UseAnonymousNamespaceCheck : public ClangTidyCheck { public: - UseAnonymousNamespaceCheck(StringRef Name, ClangTidyContext *Context) - : ClangTidyCheck(Name, Context) {} + UseAnonymousNamespaceCheck(StringRef Name, ClangTidyContext *Context); bool isLanguageVersionSupported(const LangOptions &LangOpts) const override { return LangOpts.CPlusPlus; } @@ -32,6 +32,7 @@ void check(const ast_matchers::MatchFinder::MatchResult &Result) override; private: + utils::FileExtensionsSet HeaderFileExtensions; template <typename T> void processMatch(const T *MatchedDecl); }; Index: clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp +++ clang-tools-extra/clang-tidy/misc/UseAnonymousNamespaceCheck.cpp @@ -37,16 +37,32 @@ return false; } +UseAnonymousNamespaceCheck::UseAnonymousNamespaceCheck( + StringRef Name, ClangTidyContext *Context) + : ClangTidyCheck(Name, Context) { + if (!utils::parseFileExtensions(utils::defaultHeaderFileExtensions(), + HeaderFileExtensions, + utils::defaultFileExtensionDelimiters())) { + this->configurationDiag("Invalid header file extension: '%0'") + << utils::defaultHeaderFileExtensions(); + } +} + template <typename T> void UseAnonymousNamespaceCheck::processMatch(const T *MatchedDecl) { + // Enforce anonymous namespaces only in source files, not headers + SourceLocation Loc = MatchedDecl->getLocation(); + SourceManager &SM = MatchedDecl->getASTContext().getSourceManager(); + if (utils::isSpellingLocInHeaderFile(Loc, SM, HeaderFileExtensions)) + return; + StringRef Type = llvm::isa<VarDecl>(MatchedDecl) ? "variable" : "function"; if (isInAnonymousNamespace(MatchedDecl)) - diag(MatchedDecl->getLocation(), "%0 %1 declared 'static' in " - "anonymous namespace, remove 'static'") + diag(Loc, "%0 %1 declared 'static' in " + "anonymous namespace, remove 'static'") << Type << MatchedDecl; else - diag(MatchedDecl->getLocation(), - "%0 %1 declared 'static', move to anonymous namespace instead") + diag(Loc, "%0 %1 declared 'static', move to anonymous namespace instead") << Type << MatchedDecl; } @@ -54,7 +70,8 @@ Finder->addMatcher( functionDecl(isStatic(), unless(isMemberFunction())).bind("func"), this); Finder->addMatcher( - varDecl(isStatic(), unless(anyOf(isStaticLocal(), isStaticDataMember()))) + varDecl(isStatic(), unless(anyOf(isStaticLocal(), isStaticDataMember(), + hasType(isConstQualified())))) .bind("var"), this); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits