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