Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/llvmlibc/ImplementationInNamespaceCheck.cpp

raw
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 //===----------------------------------------------------------------------===//
8
9 #include "ImplementationInNamespaceCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12
13 using namespace clang::ast_matchers;
14
15 namespace clang::tidy::llvm_libc {
16
17 const static StringRef RequiredNamespace = "__llvm_libc";
18 void ImplementationInNamespaceCheck::registerMatchers(MatchFinder *Finder) {
19 Finder->addMatcher(
20 decl(hasParent(translationUnitDecl()), unless(linkageSpecDecl()))
21 .bind("child_of_translation_unit"),
22 this);
23 }
24
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()))
30 return;
31
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")
35 << RequiredNamespace;
36 }
37 return;
38 }
39 diag(MatchedDecl->getLocation(),
40 "declaration must be declared within the '%0' namespace")
41 << RequiredNamespace;
42 }
43
44 } // namespace clang::tidy::llvm_libc