[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clang-tidy / llvmlibc / CalleeNamespaceCheck.cpp
blob98ae857b589fd644ba01027a218974eb0d11af46
1 //===-- CalleeNamespaceCheck.cpp ------------------------------------------===//
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 "CalleeNamespaceCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "llvm/ADT/StringSet.h"
15 using namespace clang::ast_matchers;
17 namespace clang::tidy::llvm_libc {
19 // Gets the outermost namespace of a DeclContext, right under the Translation
20 // Unit.
21 const DeclContext *getOutermostNamespace(const DeclContext *Decl) {
22 const DeclContext *Parent = Decl->getParent();
23 if (Parent->isTranslationUnit())
24 return Decl;
25 return getOutermostNamespace(Parent);
28 void CalleeNamespaceCheck::registerMatchers(MatchFinder *Finder) {
29 Finder->addMatcher(
30 declRefExpr(to(functionDecl().bind("func"))).bind("use-site"), this);
33 // A list of functions that are exempted from this check. The __errno_location
34 // function is for setting errno, which is allowed in libc, and the other
35 // functions are specifically allowed to be external so that they can be
36 // intercepted.
37 static const llvm::StringSet<> IgnoredFunctions = {
38 "__errno_location", "malloc", "calloc", "realloc", "free", "aligned_alloc"};
40 void CalleeNamespaceCheck::check(const MatchFinder::MatchResult &Result) {
41 const auto *UsageSiteExpr = Result.Nodes.getNodeAs<DeclRefExpr>("use-site");
42 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func");
44 // Ignore compiler builtin functions.
45 if (FuncDecl->getBuiltinID() != 0)
46 return;
48 // If the outermost namespace of the function is __llvm_libc, we're good.
49 const auto *NS = dyn_cast<NamespaceDecl>(getOutermostNamespace(FuncDecl));
50 if (NS && NS->getName() == "__llvm_libc")
51 return;
53 const DeclarationName &Name = FuncDecl->getDeclName();
54 if (Name.isIdentifier() &&
55 IgnoredFunctions.contains(Name.getAsIdentifierInfo()->getName()))
56 return;
58 diag(UsageSiteExpr->getBeginLoc(), "%0 must resolve to a function declared "
59 "within the '__llvm_libc' namespace")
60 << FuncDecl;
62 diag(FuncDecl->getLocation(), "resolves to this declaration",
63 clang::DiagnosticIDs::Note);
66 } // namespace clang::tidy::llvm_libc