1 //===--- ImplementationInNamespaceCheck.cpp - clang-tidy ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "ImplementationInNamespaceCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
15 namespace clang::tidy::llvm_libc
{
17 const static StringRef RequiredNamespace
= "__llvm_libc";
18 void ImplementationInNamespaceCheck::registerMatchers(MatchFinder
*Finder
) {
20 decl(hasParent(translationUnitDecl()), unless(linkageSpecDecl()))
21 .bind("child_of_translation_unit"),
25 void ImplementationInNamespaceCheck::check(
26 const MatchFinder::MatchResult
&Result
) {
27 const auto *MatchedDecl
=
28 Result
.Nodes
.getNodeAs
<Decl
>("child_of_translation_unit");
29 if (!Result
.SourceManager
->isInMainFile(MatchedDecl
->getLocation()))
32 if (const auto *NS
= dyn_cast
<NamespaceDecl
>(MatchedDecl
)) {
33 if (NS
->getName() != RequiredNamespace
) {
34 diag(NS
->getLocation(), "'%0' needs to be the outermost namespace")
39 diag(MatchedDecl
->getLocation(),
40 "declaration must be declared within the '%0' namespace")
44 } // namespace clang::tidy::llvm_libc