blob: 5f7c1cd49e154ee76ff3373cf4a13a5cbe262edf [file] [log] [blame]
Samuel Benzaquen59c8aa92015-02-11 21:21:05 +00001//===--- 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
16using namespace clang::ast_matchers;
17
18namespace clang {
19namespace tidy {
Alexander Kornienkoed824e02015-03-05 13:46:14 +000020namespace google {
Samuel Benzaquen59c8aa92015-02-11 21:21:05 +000021namespace readability {
22
23void
24GlobalNamesInHeadersCheck::registerMatchers(ast_matchers::MatchFinder *Finder) {
25 Finder->addMatcher(
26 decl(anyOf(usingDecl(), usingDirectiveDecl()),
27 hasDeclContext(translationUnitDecl())).bind("using_decl"),
28 this);
29}
30
31void 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
Samuel Benzaquen3199b9a2015-03-24 15:21:45 +000048 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
Samuel Benzaquen59c8aa92015-02-11 21:21:05 +000058 diag(D->getLocStart(),
59 "using declarations in the global namespace in headers are prohibited");
60}
61
62} // namespace readability
Alexander Kornienkoed824e02015-03-05 13:46:14 +000063} // namespace google
Samuel Benzaquen59c8aa92015-02-11 21:21:05 +000064} // namespace tidy
65} // namespace clang