clang-tools  3.9.0
UnnamedNamespaceInHeaderCheck.cpp
Go to the documentation of this file.
1 //===--- UnnamedNamespaceInHeaderCheck.cpp - clang-tidy ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/ASTMatchers/ASTMatchers.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace google {
20 namespace build {
21 
22 UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck(
23  StringRef Name, ClangTidyContext *Context)
24  : ClangTidyCheck(Name, Context),
25  RawStringHeaderFileExtensions(
26  Options.getLocalOrGlobal("HeaderFileExtensions", "h,hh,hpp,hxx")) {
27  if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
28  HeaderFileExtensions,
29  ',')) {
30  llvm::errs() << "Invalid header file extension: "
31  << RawStringHeaderFileExtensions << "\n";
32  }
33 }
34 
37  Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
38 }
39 
41  ast_matchers::MatchFinder *Finder) {
42  // Only register the matchers for C++; the functionality currently does not
43  // provide any benefit to other languages, despite being benign.
44  if (getLangOpts().CPlusPlus)
45  Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
46  this);
47 }
48 
49 void
50 UnnamedNamespaceInHeaderCheck::check(const MatchFinder::MatchResult &Result) {
51  const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
52  SourceLocation Loc = N->getLocStart();
53  if (!Loc.isValid())
54  return;
55 
56  if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
57  HeaderFileExtensions))
58  diag(Loc, "do not use unnamed namespaces in header files");
59 }
60 
61 } // namespace build
62 } // namespace google
63 } // namespace tidy
64 } // namespace clang
SourceLocation Loc
'#' location in the include directive
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
const std::string Name
Definition: USRFinder.cpp:140
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:170
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:210
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Base class for all clang-tidy checks.
Definition: ClangTidy.h:110
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Definition: ClangTidy.cpp:385
std::map< std::string, std::string > OptionMap
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidy.cpp:352
const NamedDecl * Result
Definition: USRFinder.cpp:137