clang-tools  14.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::parseFileExtensions(RawStringHeaderFileExtensions,
28  HeaderFileExtensions,
30  this->configurationDiag("Invalid header file extension: '%0'")
31  << RawStringHeaderFileExtensions;
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  // Anonymous 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
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::google::readability::GlobalNamesInHeadersCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: GlobalNamesInHeadersCheck.cpp:48
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::parseFileExtensions
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
Definition: FileExtensionsUtils.cpp:35
clang::tidy::utils::isSpellingLocInHeaderFile
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
Definition: FileExtensionsUtils.cpp:29
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:100
clang::tidy::utils::defaultFileExtensionDelimiters
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
Definition: FileExtensionsUtils.h:48
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:72
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
GlobalNamesInHeadersCheck.h
clang::tidy::ClangTidyCheck::configurationDiag
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Definition: ClangTidyCheck.cpp:36
clang::tidy::google::readability::GlobalNamesInHeadersCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: GlobalNamesInHeadersCheck.cpp:35
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
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: ClangTidyCheck.cpp:120
clang::tidy::google::readability::GlobalNamesInHeadersCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: GlobalNamesInHeadersCheck.cpp:40
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: FileExtensionsUtils.h:38