clang-tools 10.0.0

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
9 #include "GlobalNamesInHeadersCheck.h"
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
34 void GlobalNamesInHeadersCheck::storeOptions(
35 ClangTidyOptions::OptionMap &Opts) {
36 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
37 }
38
39 void GlobalNamesInHeadersCheck::registerMatchers(
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.
57 if (!utils::isSpellingLocInHeaderFile(
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
clang::ast_matchers
Definition: AbseilMatcher.h:14
Decl
const FunctionDecl * Decl
Definition: AvoidBindCheck.cpp:93
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:41
clang::tidy::utils::isSpellingLocInHeaderFile
bool isSpellingLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether spelling location of Loc is in header file.
Definition: HeaderFileExtensionsUtils.cpp:33
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: HeaderFileExtensionsUtils.h:40
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
clang::tidy::google::readability::GlobalNamesInHeadersCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: GlobalNamesInHeadersCheck.cpp:39
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:58
GlobalNamesInHeadersCheck.h
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
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:47
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
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:34
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21