Samuel Benzaquen | 59c8aa9 | 2015-02-11 21:21:05 +0000 | [diff] [blame] | 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 { |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 20 | namespace google { |
Samuel Benzaquen | 59c8aa9 | 2015-02-11 21:21:05 +0000 | [diff] [blame] | 21 | namespace readability { |
| 22 | |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 23 | GlobalNamesInHeadersCheck::GlobalNamesInHeadersCheck(StringRef Name, |
| 24 | ClangTidyContext *Context) |
| 25 | : ClangTidyCheck(Name, Context), |
| 26 | RawStringHeaderFileExtensions( |
| 27 | Options.getLocalOrGlobal("HeaderFileExtensions", "h")) { |
| 28 | if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions, |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 29 | HeaderFileExtensions, ',')) { |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 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 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 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); |
Samuel Benzaquen | 59c8aa9 | 2015-02-11 21:21:05 +0000 | [diff] [blame] | 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. |
Haojian Wu | c2d7577 | 2016-02-05 11:23:59 +0000 | [diff] [blame] | 58 | if (!utils::isSpellingLocInHeaderFile( |
| 59 | D->getLocStart(), *Result.SourceManager, HeaderFileExtensions)) |
Samuel Benzaquen | 59c8aa9 | 2015-02-11 21:21:05 +0000 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 63 | if (const auto *UsingDirective = dyn_cast<UsingDirectiveDecl>(D)) { |
Samuel Benzaquen | 3199b9a | 2015-03-24 15:21:45 +0000 | [diff] [blame] | 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 | |
Samuel Benzaquen | 59c8aa9 | 2015-02-11 21:21:05 +0000 | [diff] [blame] | 73 | diag(D->getLocStart(), |
| 74 | "using declarations in the global namespace in headers are prohibited"); |
| 75 | } |
| 76 | |
| 77 | } // namespace readability |
Alexander Kornienko | ed824e0 | 2015-03-05 13:46:14 +0000 | [diff] [blame] | 78 | } // namespace google |
Samuel Benzaquen | 59c8aa9 | 2015-02-11 21:21:05 +0000 | [diff] [blame] | 79 | } // namespace tidy |
| 80 | } // namespace clang |