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 using namespace clang::ast_matchers; 14 15 namespace clang { 16 namespace tidy { 17 namespace llvm_libc { 18 19 // Gets the outermost namespace of a DeclContext, right under the Translation 20 // Unit. getOutermostNamespace(const DeclContext * Decl)21const DeclContext *getOutermostNamespace(const DeclContext *Decl) { 22 const DeclContext *Parent = Decl->getParent(); 23 if (Parent && Parent->isTranslationUnit()) 24 return Decl; 25 return getOutermostNamespace(Parent); 26 } 27 registerMatchers(MatchFinder * Finder)28void CalleeNamespaceCheck::registerMatchers(MatchFinder *Finder) { 29 Finder->addMatcher( 30 declRefExpr(to(functionDecl().bind("func"))).bind("use-site"), this); 31 } 32 check(const MatchFinder::MatchResult & Result)33void CalleeNamespaceCheck::check(const MatchFinder::MatchResult &Result) { 34 const auto *UsageSiteExpr = Result.Nodes.getNodeAs<DeclRefExpr>("use-site"); 35 const auto *FuncDecl = Result.Nodes.getNodeAs<FunctionDecl>("func"); 36 37 // Ignore compiler builtin functions. 38 if (FuncDecl->getBuiltinID() != 0) 39 return; 40 41 // If the outermost namespace of the function is __llvm_libc, we're good. 42 const auto *NS = dyn_cast<NamespaceDecl>(getOutermostNamespace(FuncDecl)); 43 if (NS && NS->getName() == "__llvm_libc") 44 return; 45 46 diag(UsageSiteExpr->getBeginLoc(), "%0 must resolve to a function declared " 47 "within the '__llvm_libc' namespace") 48 << FuncDecl; 49 50 diag(FuncDecl->getLocation(), "resolves to this declaration", 51 clang::DiagnosticIDs::Note); 52 } 53 54 } // namespace llvm_libc 55 } // namespace tidy 56 } // namespace clang 57