1 //===-- CalleeNamespaceCheck.cpp ------------------------------------------===//
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 "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
21 const DeclContext
*getOutermostNamespace(const DeclContext
*Decl
) {
22 const DeclContext
*Parent
= Decl
->getParent();
23 if (Parent
->isTranslationUnit())
25 return getOutermostNamespace(Parent
);
28 void CalleeNamespaceCheck::registerMatchers(MatchFinder
*Finder
) {
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
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)
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")
53 const DeclarationName
&Name
= FuncDecl
->getDeclName();
54 if (Name
.isIdentifier() &&
55 IgnoredFunctions
.contains(Name
.getAsIdentifierInfo()->getName()))
58 diag(UsageSiteExpr
->getBeginLoc(), "%0 must resolve to a function declared "
59 "within the '__llvm_libc' namespace")
62 diag(FuncDecl
->getLocation(), "resolves to this declaration",
63 clang::DiagnosticIDs::Note
);
66 } // namespace clang::tidy::llvm_libc