clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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 void
24 GlobalNamesInHeadersCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
25 Finder->addMatcher(
26 decl(anyOf(usingDecl(), usingDirectiveDecl()),
27 hasDeclContext(translationUnitDecl())).bind("using_decl"),
28 this);
29 }
30
31 void GlobalNamesInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
32 const auto *D = Result.Nodes.getNodeAs<Decl>("using_decl");
33 // If it comes from a macro, we'll assume it is fine.
34 if (D->getLocStart().isMacroID())
35 return;
36
37 // Ignore if it comes from the "main" file ...
38 if (Result.SourceManager->isInMainFile(
39 Result.SourceManager->getExpansionLoc(D->getLocStart()))) {
40 // unless that file is a header.
41 StringRef Filename = Result.SourceManager->getFilename(
42 Result.SourceManager->getSpellingLoc(D->getLocStart()));
43
44 if (!Filename.endswith(".h"))
45 return;
46 }
47
48 if (const auto* UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) {
49 if (UsingDirective->getNominatedNamespace()->isAnonymousNamespace()) {
50 // Anynoumous namespaces inject a using directive into the AST to import
51 // the names into the containing namespace.
52 // We should not have them in headers, but there is another warning for
53 // that.
54 return;
55 }
56 }
57
58 diag(D->getLocStart(),
59 "using declarations in the global namespace in headers are prohibited");
60 }
61
62 } // namespace readability
63 } // namespace google
64 } // namespace tidy
65 } // namespace clang
Finder
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:188
Filename
std::string Filename
Filename as a string.
Definition: IncludeOrderCheck.cpp:36
GlobalNamesInHeadersCheck.h
Result
const NamedDecl * Result
Definition: USRFinder.cpp:121