clang-tools  7.0.0svn
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(Options.getLocalOrGlobal(
26  "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
27  if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
28  HeaderFileExtensions, ',')) {
29  llvm::errs() << "Invalid header file extension: "
30  << RawStringHeaderFileExtensions << "\n";
31  }
32 }
33 
36  Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
37 }
38 
40  ast_matchers::MatchFinder *Finder) {
41  // Only register the matchers for C++; the functionality currently does not
42  // provide any benefit to other languages, despite being benign.
43  if (getLangOpts().CPlusPlus)
44  Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
45  this);
46 }
47 
49  const MatchFinder::MatchResult &Result) {
50  const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
51  SourceLocation Loc = N->getLocStart();
52  if (!Loc.isValid())
53  return;
54 
55  if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
56  HeaderFileExtensions))
57  diag(Loc, "do not use unnamed namespaces in header files");
58 }
59 
60 } // namespace build
61 } // namespace google
62 } // namespace tidy
63 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
llvm::StringRef Name
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
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:460
LangOptions getLangOpts() const
Returns the language options from the context.
Definition: ClangTidy.h:187
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
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:127
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
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&#39;s name.
Definition: ClangTidy.cpp:427