clang-tools 6.0.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
10 #include "GlobalNamesInHeadersCheck.h"
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
35 void GlobalNamesInHeadersCheck::storeOptions(
36 ClangTidyOptions::OptionMap &Opts) {
37 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
38 }
39
40 void GlobalNamesInHeadersCheck::registerMatchers(
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->getLocStart().isMacroID())
52 return;
53
54 // Ignore if it comes from the "main" file ...
55 if (Result.SourceManager->isInMainFile(
56 Result.SourceManager->getExpansionLoc(D->getLocStart()))) {
57 // unless that file is a header.
58 if (!utils::isSpellingLocInHeaderFile(
59 D->getLocStart(), *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->getLocStart(),
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::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
clang::tidy::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: ClangTidy.cpp:449
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:42
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:34
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: HeaderFileExtensionsUtils.h:41
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidy.h:183
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
GlobalNamesInHeadersCheck.h
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:99
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
Definition: AndroidTidyModule.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:104
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::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidy.cpp:416