| 1 | //===- CallGraph.cpp - AST-based Call graph -------------------------------===// |
| 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 | // This file defines the AST-based CallGraph. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/Analysis/CallGraph.h" |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/AST/DeclBase.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/AST/ExprObjC.h" |
| 19 | #include "clang/AST/Stmt.h" |
| 20 | #include "clang/AST/StmtVisitor.h" |
| 21 | #include "clang/Basic/IdentifierTable.h" |
| 22 | #include "clang/Basic/LLVM.h" |
| 23 | #include "llvm/ADT/PostOrderIterator.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/Support/Casting.h" |
| 26 | #include "llvm/Support/Compiler.h" |
| 27 | #include "llvm/Support/DOTGraphTraits.h" |
| 28 | #include "llvm/Support/GraphWriter.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include <cassert> |
| 31 | #include <memory> |
| 32 | #include <string> |
| 33 | |
| 34 | using namespace clang; |
| 35 | |
| 36 | #define DEBUG_TYPE "CallGraph" |
| 37 | |
| 38 | STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges" ); |
| 39 | STATISTIC(NumBlockCallEdges, "Number of block call edges" ); |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | /// A helper class, which walks the AST and locates all the call sites in the |
| 44 | /// given function body. |
| 45 | class CGBuilder : public StmtVisitor<CGBuilder> { |
| 46 | CallGraph *G; |
| 47 | CallGraphNode *CallerNode; |
| 48 | |
| 49 | public: |
| 50 | CGBuilder(CallGraph *g, CallGraphNode *N) : G(g), CallerNode(N) {} |
| 51 | |
| 52 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
| 53 | |
| 54 | Decl *getDeclFromCall(CallExpr *CE) { |
| 55 | if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) |
| 56 | return CalleeDecl; |
| 57 | |
| 58 | // Simple detection of a call through a block. |
| 59 | Expr *CEE = CE->getCallee()->IgnoreParenImpCasts(); |
| 60 | if (BlockExpr *Block = dyn_cast<BlockExpr>(Val: CEE)) { |
| 61 | NumBlockCallEdges++; |
| 62 | return Block->getBlockDecl(); |
| 63 | } |
| 64 | |
| 65 | return nullptr; |
| 66 | } |
| 67 | |
| 68 | void addCalledDecl(Decl *D, Expr *CallExpr) { |
| 69 | if (G->includeCalleeInGraph(D)) { |
| 70 | CallGraphNode *CalleeNode = G->getOrInsertNode(D); |
| 71 | CallerNode->addCallee(Call: {CalleeNode, CallExpr}); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void VisitCallExpr(CallExpr *CE) { |
| 76 | if (Decl *D = getDeclFromCall(CE)) |
| 77 | addCalledDecl(D, CE); |
| 78 | VisitChildren(CE); |
| 79 | } |
| 80 | |
| 81 | void VisitLambdaExpr(LambdaExpr *LE) { |
| 82 | if (FunctionTemplateDecl *FTD = LE->getDependentCallOperator()) |
| 83 | for (FunctionDecl *FD : FTD->specializations()) |
| 84 | G->VisitFunctionDecl(FD); |
| 85 | else if (CXXMethodDecl *MD = LE->getCallOperator()) |
| 86 | G->VisitFunctionDecl(MD); |
| 87 | } |
| 88 | |
| 89 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 90 | if (FunctionDecl *FD = E->getOperatorNew()) |
| 91 | addCalledDecl(FD, E); |
| 92 | VisitChildren(E); |
| 93 | } |
| 94 | |
| 95 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 96 | CXXConstructorDecl *Ctor = E->getConstructor(); |
| 97 | if (FunctionDecl *Def = Ctor->getDefinition()) |
| 98 | addCalledDecl(Def, E); |
| 99 | VisitChildren(E); |
| 100 | } |
| 101 | |
| 102 | // Include the evaluation of the default argument. |
| 103 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 104 | Visit(E->getExpr()); |
| 105 | } |
| 106 | |
| 107 | // Include the evaluation of the default initializers in a class. |
| 108 | void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) { |
| 109 | Visit(E->getExpr()); |
| 110 | } |
| 111 | |
| 112 | // Adds may-call edges for the ObjC message sends. |
| 113 | void VisitObjCMessageExpr(ObjCMessageExpr *ME) { |
| 114 | if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) { |
| 115 | Selector Sel = ME->getSelector(); |
| 116 | |
| 117 | // Find the callee definition within the same translation unit. |
| 118 | Decl *D = nullptr; |
| 119 | if (ME->isInstanceMessage()) |
| 120 | D = IDecl->lookupPrivateMethod(Sel); |
| 121 | else |
| 122 | D = IDecl->lookupPrivateClassMethod(Sel); |
| 123 | if (D) { |
| 124 | addCalledDecl(D, ME); |
| 125 | NumObjCCallEdges++; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void VisitChildren(Stmt *S) { |
| 131 | for (Stmt *SubStmt : S->children()) |
| 132 | if (SubStmt) |
| 133 | this->Visit(SubStmt); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | } // namespace |
| 138 | |
| 139 | void CallGraph::addNodesForBlocks(DeclContext *D) { |
| 140 | if (BlockDecl *BD = dyn_cast<BlockDecl>(Val: D)) |
| 141 | addNodeForDecl(BD, true); |
| 142 | |
| 143 | for (auto *I : D->decls()) |
| 144 | if (auto *DC = dyn_cast<DeclContext>(Val: I)) |
| 145 | addNodesForBlocks(D: DC); |
| 146 | } |
| 147 | |
| 148 | CallGraph::CallGraph() { |
| 149 | ShouldWalkTypesOfTypeLocs = false; |
| 150 | ShouldVisitTemplateInstantiations = true; |
| 151 | ShouldVisitImplicitCode = true; |
| 152 | Root = getOrInsertNode(nullptr); |
| 153 | } |
| 154 | |
| 155 | CallGraph::~CallGraph() = default; |
| 156 | |
| 157 | bool CallGraph::includeInGraph(const Decl *D) { |
| 158 | assert(D); |
| 159 | if (!D->hasBody()) |
| 160 | return false; |
| 161 | |
| 162 | return includeCalleeInGraph(D); |
| 163 | } |
| 164 | |
| 165 | bool CallGraph::includeCalleeInGraph(const Decl *D) { |
| 166 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: D)) { |
| 167 | // We skip function template definitions, as their semantics is |
| 168 | // only determined when they are instantiated. |
| 169 | if (FD->isDependentContext()) |
| 170 | return false; |
| 171 | |
| 172 | IdentifierInfo *II = FD->getIdentifier(); |
| 173 | if (II && II->getName().starts_with(Prefix: "__inline" )) |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) { |
| 181 | assert(D); |
| 182 | |
| 183 | // Allocate a new node, mark it as root, and process its calls. |
| 184 | CallGraphNode *Node = getOrInsertNode(D); |
| 185 | |
| 186 | // Process all the calls by this function as well. |
| 187 | CGBuilder builder(this, Node); |
| 188 | if (Stmt *Body = D->getBody()) |
| 189 | builder.Visit(Body); |
| 190 | |
| 191 | // Include C++ constructor member initializers. |
| 192 | if (auto constructor = dyn_cast<CXXConstructorDecl>(Val: D)) { |
| 193 | for (CXXCtorInitializer *init : constructor->inits()) { |
| 194 | builder.Visit(init->getInit()); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | CallGraphNode *CallGraph::getNode(const Decl *F) const { |
| 200 | FunctionMapTy::const_iterator I = FunctionMap.find(Val: F); |
| 201 | if (I == FunctionMap.end()) return nullptr; |
| 202 | return I->second.get(); |
| 203 | } |
| 204 | |
| 205 | CallGraphNode *CallGraph::getOrInsertNode(Decl *F) { |
| 206 | if (F && !isa<ObjCMethodDecl>(Val: F)) |
| 207 | F = F->getCanonicalDecl(); |
| 208 | |
| 209 | std::unique_ptr<CallGraphNode> &Node = FunctionMap[F]; |
| 210 | if (Node) |
| 211 | return Node.get(); |
| 212 | |
| 213 | Node = std::make_unique<CallGraphNode>(args&: F); |
| 214 | // Make Root node a parent of all functions to make sure all are reachable. |
| 215 | if (F) |
| 216 | Root->addCallee(Call: {Node.get(), /*Call=*/nullptr}); |
| 217 | return Node.get(); |
| 218 | } |
| 219 | |
| 220 | void CallGraph::print(raw_ostream &OS) const { |
| 221 | OS << " --- Call graph Dump --- \n" ; |
| 222 | |
| 223 | // We are going to print the graph in reverse post order, partially, to make |
| 224 | // sure the output is deterministic. |
| 225 | llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(this); |
| 226 | for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator |
| 227 | I = RPOT.begin(), E = RPOT.end(); I != E; ++I) { |
| 228 | const CallGraphNode *N = *I; |
| 229 | |
| 230 | OS << " Function: " ; |
| 231 | if (N == Root) |
| 232 | OS << "< root >" ; |
| 233 | else |
| 234 | N->print(os&: OS); |
| 235 | |
| 236 | OS << " calls: " ; |
| 237 | for (CallGraphNode::const_iterator CI = N->begin(), |
| 238 | CE = N->end(); CI != CE; ++CI) { |
| 239 | assert(CI->Callee != Root && "No one can call the root node." ); |
| 240 | CI->Callee->print(os&: OS); |
| 241 | OS << " " ; |
| 242 | } |
| 243 | OS << '\n'; |
| 244 | } |
| 245 | OS.flush(); |
| 246 | } |
| 247 | |
| 248 | LLVM_DUMP_METHOD void CallGraph::dump() const { |
| 249 | print(OS&: llvm::errs()); |
| 250 | } |
| 251 | |
| 252 | void CallGraph::viewGraph() const { |
| 253 | llvm::ViewGraph(G: this, Name: "CallGraph" ); |
| 254 | } |
| 255 | |
| 256 | void CallGraphNode::print(raw_ostream &os) const { |
| 257 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Val: FD)) |
| 258 | return ND->printQualifiedName(OS&: os); |
| 259 | os << "< >" ; |
| 260 | } |
| 261 | |
| 262 | LLVM_DUMP_METHOD void CallGraphNode::dump() const { |
| 263 | print(os&: llvm::errs()); |
| 264 | } |
| 265 | |
| 266 | namespace llvm { |
| 267 | |
| 268 | template <> |
| 269 | struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits { |
| 270 | DOTGraphTraits (bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} |
| 271 | |
| 272 | static std::string getNodeLabel(const CallGraphNode *Node, |
| 273 | const CallGraph *CG) { |
| 274 | if (CG->getRoot() == Node) { |
| 275 | return "< root >" ; |
| 276 | } |
| 277 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Val: Node->getDecl())) |
| 278 | return ND->getNameAsString(); |
| 279 | else |
| 280 | return "< >" ; |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | } // namespace llvm |
| 285 | |