11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
15 using namespace clang::ast_matchers;
22 void UnnamedNamespaceInHeaderCheck::registerMatchers(
23 ast_matchers::MatchFinder *
Finder) {
26 if (getLangOpts().CPlusPlus)
27 Finder->addMatcher(namespaceDecl(isAnonymous()).bind(
"anonymousNamespace"),
32 UnnamedNamespaceInHeaderCheck::check(
const MatchFinder::MatchResult &
Result) {
33 SourceManager *
SM = Result.SourceManager;
34 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>(
"anonymousNamespace");
35 SourceLocation
Loc = N->getLocStart();
41 StringRef FileName = SM->getPresumedLoc(Loc).getFilename();
42 if (FileName.endswith(
".h") || FileName.endswith(
".hh") ||
43 FileName.endswith(
".hpp") || FileName.endswith(
".hxx"))
44 diag(Loc,
"do not use unnamed namespaces in header files");
SourceLocation Loc
'#' location in the include directive
std::unique_ptr< ast_matchers::MatchFinder > Finder