clang-tools  10.0.0git
GlobalNamesInHeadersCheck.cpp
Go to the documentation of this file.
1 //===--- GlobalNamesInHeadersCheck.cpp - clang-tidy -----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace google {
20 namespace readability {
21 
22 GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef Name,
23  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  Finder->addMatcher(decl(anyOf(usingDecl(), usingDirectiveDecl()),
42  hasDeclContext(translationUnitDecl()))
43  .bind("using_decl"),
44  this);
45 }
46 
47 void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
48  const auto *D = Result.Nodes.getNodeAs<Decl>("using_decl");
49  // If it comes from a macro, we'll assume it is fine.
50  if (D->getBeginLoc().isMacroID())
51  return;
52 
53  // Ignore if it comes from the "main" file ...
54  if (Result.SourceManager->isInMainFile(
55  Result.SourceManager->getExpansionLoc(D->getBeginLoc()))) {
56  // unless that file is a header.
58  D->getBeginLoc(), *Result.SourceManager, HeaderFileExtensions))
59  return;
60  }
61 
62  if (const auto *UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
63  if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
64  // Anonymous namespaces inject a using directive into the AST to import
65  // the names into the containing namespace.
66  // We should not have them in headers, but there is another warning for
67  // that.
68  return;
69  }
70  }
71 
72  diag(D->getBeginLoc(),
73  "using declarations in the global namespace in headers are prohibited");
74 }
75 
76 } // namespace readability
77 } // namespace google
78 } // namespace tidy
79 } // namespace clang
const FunctionDecl * Decl
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.
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.
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.
static constexpr llvm::StringLiteral Name
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.
===– 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.