return Node.getStorageClass() == SC_Static;
}
+AST_POLYMORPHIC_MATCHER_P(isInHeaderFile,
+ AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
+ VarDecl),
+ utils::FileExtensionsSet, HeaderFileExtensions) {
+ return utils::isExpansionLocInHeaderFile(
+ Node.getBeginLoc(), Finder->getASTContext().getSourceManager(),
+ HeaderFileExtensions);
+}
+
AST_MATCHER(FunctionDecl, isMemberFunction) {
return llvm::isa<CXXMethodDecl>(&Node);
}
}
} // namespace
+UseAnonymousNamespaceCheck::UseAnonymousNamespaceCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
+ "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
+ if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
+ HeaderFileExtensions,
+ utils::defaultFileExtensionDelimiters())) {
+ this->configurationDiag("Invalid header file extension: '%0'")
+ << RawStringHeaderFileExtensions;
+ }
+}
+
+void UseAnonymousNamespaceCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
+}
+
void UseAnonymousNamespaceCheck::registerMatchers(MatchFinder *Finder) {
Finder->addMatcher(
functionDecl(isStatic(),
- unless(anyOf(isInAnonymousNamespace(), isMemberFunction())))
+ unless(anyOf(isInHeaderFile(HeaderFileExtensions),
+ isInAnonymousNamespace(), isMemberFunction())))
.bind("x"),
this);
Finder->addMatcher(
- varDecl(isStatic(), unless(anyOf(isInAnonymousNamespace(),
- isStaticLocal(), isStaticDataMember())))
+ varDecl(isStatic(),
+ unless(anyOf(isInHeaderFile(HeaderFileExtensions),
+ isInAnonymousNamespace(), isStaticLocal(),
+ isStaticDataMember(), hasType(isConstQualified()))))
.bind("x"),
this);
}
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_USEANONYMOUSNAMESPACECHECK_H
#include "../ClangTidyCheck.h"
+#include "../utils/FileExtensionsUtils.h"
namespace clang {
namespace tidy {
/// Warns when using 'static' functions or variables at global scope, and
/// suggests moving them to an anonymous namespace.
///
+/// The check supports these options:
+/// - `HeaderFileExtensions`: a semicolon-separated list of filename
+/// extensions of header files (The filename extension should not contain
+/// "." prefix). ";h;hh;hpp;hxx" by default.
+///
+/// For extension-less header files, using an empty string or leaving an
+/// empty string between ";" if there are other filename extensions.
+///
/// For the user-facing documentation see:
/// 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;
}
+ void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+
+private:
+ const StringRef RawStringHeaderFileExtensions;
+ utils::FileExtensionsSet HeaderFileExtensions;
};
} // namespace misc
keep C compatibility [1]. ``static`` is an overloaded term with different meanings in
different contexts, so it can create confusion.
+The following uses of ``static`` will *not* be diagnosed:
+
+* Functions or variables in header files, since anonymous namespaces in headers
+ is considered an antipattern. Allowed header file extensions can be configured
+ via the `HeaderFileExtensions` option (see below).
+* ``const`` or ``constexpr`` variables, since they already have implicit internal
+ linkage in C++.
+
Examples:
.. code-block:: c++
int x;
} // namespace
+Options
+-------
+
+.. option:: HeaderFileExtensions
+
+ A semicolon-separated list of filename extensions of header files (the filename
+ extensions should not include "." prefix). Default is ";h;hh;hpp;hxx".
+ For extension-less header files, using an empty string or leaving an
+ empty string between ";" if there are other filename extensions.
+
[1] `Undeprecating static <https://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1012>`_
--- /dev/null
+// Should not warn here, do not require anonymous namespaces in headers
+static int gv{123};
+static void gf(){}
-// 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]
{
static int x;
}
+
+// OK
+static const int v8{123};
+static constexpr int v9{123};