| 1 | //===--- UnnamedNamespaceInHeaderCheck.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 "UnnamedNamespaceInHeaderCheck.h" |
| 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 13 | |
| 14 | using namespace clang::ast_matchers; |
| 15 | |
| 16 | namespace clang::tidy::google::build { |
| 17 | |
| 18 | UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck( |
| 19 | StringRef Name, ClangTidyContext *Context) |
| 20 | : ClangTidyCheck(Name, Context), |
| 21 | RawStringHeaderFileExtensions(Options.getLocalOrGlobal( |
| 22 | "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) { |
| 23 | if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, |
| 24 | HeaderFileExtensions, |
| 25 | utils::defaultFileExtensionDelimiters())) { |
| 26 | this->configurationDiag("Invalid header file extension: '%0'") |
| 27 | << RawStringHeaderFileExtensions; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void UnnamedNamespaceInHeaderCheck::storeOptions( |
| 32 | ClangTidyOptions::OptionMap &Opts) { |
| 33 | Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions); |
| 34 | } |
| 35 | |
| 36 | void UnnamedNamespaceInHeaderCheck::registerMatchers( |
| 37 | ast_matchers::MatchFinder *Finder) { |
| 38 | Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"), |
| 39 | this); |
| 40 | } |
| 41 | |
| 42 | void UnnamedNamespaceInHeaderCheck::check( |
| 43 | const MatchFinder::MatchResult &Result) { |
| 44 | const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace"); |
| 45 | SourceLocation Loc = N->getBeginLoc(); |
| 46 | if (!Loc.isValid()) |
| 47 | return; |
| 48 | |
| 49 | if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager, |
| 50 | HeaderFileExtensions)) |
| 51 | diag(Loc, "do not use unnamed namespaces in header files"); |
| 52 | } |
| 53 | |
| 54 | } // namespace clang::tidy::google::build |