clang-tools  3.9.0
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,
25  : ClangTidyCheck(Name, Context),
26  RawStringHeaderFileExtensions(
27  Options.getLocalOrGlobal("HeaderFileExtensions", "h")) {
28  if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
29  HeaderFileExtensions,
30  ',')) {
31  llvm::errs() << "Invalid header file extension: "
32  << RawStringHeaderFileExtensions << "\n";
33  }
34 }
35 
38  Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
39 }
40 
41 void
43  Finder->addMatcher(
44  decl(anyOf(usingDecl(), usingDirectiveDecl()),
45  hasDeclContext(translationUnitDecl())).bind("using_decl"),
46  this);
47 }
48 
49 void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
50  const auto *D = Result.Nodes.getNodeAs<Decl>("using_decl");
51  // If it comes from a macro, we'll assume it is fine.
52  if (D->getLocStart().isMacroID())
53  return;
54 
55  // Ignore if it comes from the "main" file ...
56  if (Result.SourceManager->isInMainFile(
57  Result.SourceManager->getExpansionLoc(D->getLocStart()))) {
58  // unless that file is a header.
60  D->getLocStart(), *Result.SourceManager, HeaderFileExtensions))
61  return;
62  }
63 
64  if (const auto* UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
65  if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
66  // Anynoumous namespaces inject a using directive into the AST to import
67  // the names into the containing namespace.
68  // We should not have them in headers, but there is another warning for
69  // that.
70  return;
71  }
72  }
73 
74  diag(D->getLocStart(),
75  "using declarations in the global namespace in headers are prohibited");
76 }
77 
78 } // namespace readability
79 } // namespace google
80 } // namespace tidy
81 } // namespace clang
const std::string Name
Definition: USRFinder.cpp:140
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 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: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
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
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's name.
Definition: ClangTidy.cpp:352
const NamedDecl * Result
Definition: USRFinder.cpp:137