| 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 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "CalleeNamespaceCheck.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 |
|
| 13 |
#include "llvm/ADT/StringSet.h" |
| 14 |
|
| 15 |
using namespace clang::ast_matchers; |
| 16 |
|
| 17 |
namespace clang::tidy::llvm_libc { |
| 18 |
|
| 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); |
| 26 |
} |
| 27 |
|
| 28 |
void CalleeNamespaceCheck::registerMatchers(MatchFinder *Finder) { |
| 29 |
Finder->addMatcher( |
| 30 |
declRefExpr(to(functionDecl().bind("func"))).bind("use-site"), this); |
| 31 |
} |
| 32 |
|
| 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"}; |
| 39 |
|
| 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"); |
| 43 |
|
| 44 |
// Ignore compiler builtin functions. |
| 45 |
if (FuncDecl->getBuiltinID() != 0) |
| 46 |
return; |
| 47 |
|
| 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; |
| 52 |
|
| 53 |
const DeclarationName &Name = FuncDecl->getDeclName(); |
| 54 |
if (Name.isIdentifier() && |
| 55 |
IgnoredFunctions.contains(Name.getAsIdentifierInfo()->getName())) |
| 56 |
return; |
| 57 |
|
| 58 |
diag(UsageSiteExpr->getBeginLoc(), "%0 must resolve to a function declared " |
| 59 |
"within the '__llvm_libc' namespace") |
| 60 |
<< FuncDecl; |
| 61 |
|
| 62 |
diag(FuncDecl->getLocation(), "resolves to this declaration", |
| 63 |
clang::DiagnosticIDs::Note); |
| 64 |
} |
| 65 |
|
| 66 |
} // namespace clang::tidy::llvm_libc |