clang-tools 17.0.0git
ImplementationInNamespaceCheck.cpp
Go to the documentation of this file.
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::llvm_libc {
16
17const static StringRef RequiredNamespace = "__llvm_libc";
19 Finder->addMatcher(
20 decl(hasParent(translationUnitDecl()), unless(linkageSpecDecl()))
21 .bind("child_of_translation_unit"),
22 this);
23}
24
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")
36 }
37 return;
38 }
39 diag(MatchedDecl->getLocation(),
40 "declaration must be declared within the '%0' namespace")
42}
43
44} // namespace clang::tidy::llvm_libc
const FunctionDecl * Decl
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
static const StringRef RequiredNamespace