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 
GlobalNamesInHeadersCheck(StringRef Name,ClangTidyContext * Context)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,
29                                   utils::defaultFileExtensionDelimiters())) {
30     this->configurationDiag("Invalid header file extension: '%0'")
31         << RawStringHeaderFileExtensions;
32   }
33 }
34 
storeOptions(ClangTidyOptions::OptionMap & Opts)35 void GlobalNamesInHeadersCheck::storeOptions(
36     ClangTidyOptions::OptionMap &Opts) {
37   Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
38 }
39 
registerMatchers(ast_matchers::MatchFinder * Finder)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 
check(const MatchFinder::MatchResult & Result)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.
58     if (!utils::isSpellingLocInHeaderFile(
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
81