clang-tools  8.0.0svn
GlobalNamesInHeadersCheck.cpp
Go to the documentation of this file.
1 //===--- GlobalNamesInHeadersCheck.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 #include "clang/Lex/Lexer.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace google {
21 namespace readability {
22 
23 GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef Name,
24  ClangTidyContext *Context)
25  : ClangTidyCheck(Name, Context),
26  RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
27  "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
28  if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
29  HeaderFileExtensions, ',')) {
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  Finder->addMatcher(decl(anyOf(usingDecl(), usingDirectiveDecl()),
43  hasDeclContext(translationUnitDecl()))
44  .bind("using_decl"),
45  this);
46 }
47 
48 void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
49  const auto *D = Result.Nodes.getNodeAs<Decl>("using_decl");
50  // If it comes from a macro, we'll assume it is fine.
51  if (D->getBeginLoc().isMacroID())
52  return;
53 
54  // Ignore if it comes from the "main" file ...
55  if (Result.SourceManager->isInMainFile(
56  Result.SourceManager->getExpansionLoc(D->getBeginLoc()))) {
57  // unless that file is a header.
59  D->getBeginLoc(), *Result.SourceManager, HeaderFileExtensions))
60  return;
61  }
62 
63  if (const auto *UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
64  if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
65  // Anynoumous namespaces inject a using directive into the AST to import
66  // the names into the containing namespace.
67  // We should not have them in headers, but there is another warning for
68  // that.
69  return;
70  }
71  }
72 
73  diag(D->getBeginLoc(),
74  "using declarations in the global namespace in headers are prohibited");
75 }
76 
77 } // namespace readability
78 } // namespace google
79 } // namespace tidy
80 } // namespace clang
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:473
Optional< Expected< tooling::AtomicChanges > > Result
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
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.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
const Decl * D
Definition: XRefs.cpp:80
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:438