[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / clang-tidy / llvmlibc / ImplementationInNamespaceCheck.cpp
blobae9819ed97502e846f7fd579ecf0236801d66f49
1 //===--- ImplementationInNamespaceCheck.cpp - clang-tidy ------------------===//
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 "ImplementationInNamespaceCheck.h"
10 #include "NamespaceConstants.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers;
16 namespace clang::tidy::llvm_libc {
18 void ImplementationInNamespaceCheck::registerMatchers(MatchFinder *Finder) {
19 Finder->addMatcher(
20 translationUnitDecl(
21 forEach(decl(isExpansionInMainFile(), unless(linkageSpecDecl()),
22 // anonymous namespaces generate usingDirective
23 unless(usingDirectiveDecl(isImplicit())))
24 .bind("child_of_translation_unit"))),
25 this);
28 void ImplementationInNamespaceCheck::check(
29 const MatchFinder::MatchResult &Result) {
30 const auto *MatchedDecl =
31 Result.Nodes.getNodeAs<Decl>("child_of_translation_unit");
32 const auto *NS = dyn_cast<NamespaceDecl>(MatchedDecl);
33 if (NS == nullptr || NS->isAnonymousNamespace()) {
34 diag(MatchedDecl->getLocation(),
35 "declaration must be enclosed within the '%0' namespace")
36 << RequiredNamespaceMacroName;
37 return;
39 if (Result.SourceManager->isMacroBodyExpansion(NS->getLocation()) == false) {
40 diag(NS->getLocation(), "the outermost namespace should be the '%0' macro")
41 << RequiredNamespaceMacroName;
42 return;
44 if (NS->getName().starts_with(RequiredNamespaceStart) == false) {
45 diag(NS->getLocation(), "the '%0' macro should start with '%1'")
46 << RequiredNamespaceMacroName << RequiredNamespaceStart;
47 return;
51 } // namespace clang::tidy::llvm_libc