clang-tools 22.0.0git
ImplementationInNamespaceCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "NamespaceConstants.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::llvm_libc {
16
18 Finder->addMatcher(
19 translationUnitDecl(
20 forEach(decl(isExpansionInMainFile(), unless(linkageSpecDecl()),
21 // anonymous namespaces generate usingDirective
22 unless(usingDirectiveDecl(isImplicit())))
23 .bind("child_of_translation_unit"))),
24 this);
25}
26
28 const MatchFinder::MatchResult &Result) {
29 const auto *MatchedDecl =
30 Result.Nodes.getNodeAs<Decl>("child_of_translation_unit");
31 const auto *NS = dyn_cast<NamespaceDecl>(MatchedDecl);
32
33 // LLVM libc declarations should be inside of a non-anonymous namespace.
34 if (NS == nullptr || NS->isAnonymousNamespace()) {
35 diag(MatchedDecl->getLocation(),
36 "declaration must be enclosed within the '%0' namespace")
38 return;
39 }
40
41 // Enforce that the namespace is the result of macro expansion
42 if (Result.SourceManager->isMacroBodyExpansion(NS->getLocation()) == false) {
43 diag(NS->getLocation(), "the outermost namespace should be the '%0' macro")
45 return;
46 }
47
48 // We want the macro to have [[gnu::visibility("hidden")]] as a prefix, but
49 // visibility is just an attribute in the AST construct, so we check that
50 // instead.
51 if (NS->getVisibility() != Visibility::HiddenVisibility) {
52 diag(NS->getLocation(), "the '%0' macro should start with '%1'")
54 return;
55 }
56
57 // Lastly, make sure the namespace name actually has the __llvm_libc prefix
58 if (NS->getName().starts_with(RequiredNamespaceRefStart) == false) {
59 diag(NS->getLocation(), "the '%0' macro expansion should start with '%1'")
61 return;
62 }
63}
64
65} // namespace clang::tidy::llvm_libc
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
static const llvm::StringRef RequiredNamespaceDeclStart
static const llvm::StringRef RequiredNamespaceDeclMacroName
static const llvm::StringRef RequiredNamespaceRefStart