[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / google / UnnamedNamespaceInHeaderCheck.cpp
blob59766a1a3fd485ac6af7d9dde618ab66236b1320
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 //===----------------------------------------------------------------------===//
9 #include "UnnamedNamespaceInHeaderCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
14 using namespace clang::ast_matchers;
16 namespace clang::tidy::google::build {
18 UnnamedNamespaceInHeaderCheck::UnnamedNamespaceInHeaderCheck(
19 StringRef Name, ClangTidyContext *Context)
20 : ClangTidyCheck(Name, Context) {
21 std::optional<StringRef> HeaderFileExtensionsOption =
22 Options.get("HeaderFileExtensions");
23 RawStringHeaderFileExtensions =
24 HeaderFileExtensionsOption.value_or(utils::defaultHeaderFileExtensions());
25 if (HeaderFileExtensionsOption) {
26 if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
27 HeaderFileExtensions,
28 utils::defaultFileExtensionDelimiters())) {
29 this->configurationDiag("Invalid header file extension: '%0'")
30 << RawStringHeaderFileExtensions;
32 } else
33 HeaderFileExtensions = Context->getHeaderFileExtensions();
36 void UnnamedNamespaceInHeaderCheck::storeOptions(
37 ClangTidyOptions::OptionMap &Opts) {
38 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
41 void UnnamedNamespaceInHeaderCheck::registerMatchers(
42 ast_matchers::MatchFinder *Finder) {
43 Finder->addMatcher(namespaceDecl(isAnonymous()).bind("anonymousNamespace"),
44 this);
47 void UnnamedNamespaceInHeaderCheck::check(
48 const MatchFinder::MatchResult &Result) {
49 const auto *N = Result.Nodes.getNodeAs<NamespaceDecl>("anonymousNamespace");
50 SourceLocation Loc = N->getBeginLoc();
51 if (!Loc.isValid())
52 return;
54 if (utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
55 HeaderFileExtensions))
56 diag(Loc, "do not use unnamed namespaces in header files");
59 } // namespace clang::tidy::google::build