17330f729Sjoerg //===- CallGraph.cpp - AST-based Call graph -------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines the AST-based CallGraph.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg
137330f729Sjoerg #include "clang/Analysis/CallGraph.h"
147330f729Sjoerg #include "clang/AST/Decl.h"
157330f729Sjoerg #include "clang/AST/DeclBase.h"
167330f729Sjoerg #include "clang/AST/DeclObjC.h"
177330f729Sjoerg #include "clang/AST/Expr.h"
187330f729Sjoerg #include "clang/AST/ExprObjC.h"
197330f729Sjoerg #include "clang/AST/Stmt.h"
207330f729Sjoerg #include "clang/AST/StmtVisitor.h"
217330f729Sjoerg #include "clang/Basic/IdentifierTable.h"
227330f729Sjoerg #include "clang/Basic/LLVM.h"
237330f729Sjoerg #include "llvm/ADT/PostOrderIterator.h"
247330f729Sjoerg #include "llvm/ADT/STLExtras.h"
257330f729Sjoerg #include "llvm/ADT/Statistic.h"
267330f729Sjoerg #include "llvm/Support/Casting.h"
277330f729Sjoerg #include "llvm/Support/Compiler.h"
287330f729Sjoerg #include "llvm/Support/DOTGraphTraits.h"
297330f729Sjoerg #include "llvm/Support/GraphWriter.h"
307330f729Sjoerg #include "llvm/Support/raw_ostream.h"
317330f729Sjoerg #include <cassert>
327330f729Sjoerg #include <memory>
337330f729Sjoerg #include <string>
347330f729Sjoerg
357330f729Sjoerg using namespace clang;
367330f729Sjoerg
377330f729Sjoerg #define DEBUG_TYPE "CallGraph"
387330f729Sjoerg
397330f729Sjoerg STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
407330f729Sjoerg STATISTIC(NumBlockCallEdges, "Number of block call edges");
417330f729Sjoerg
427330f729Sjoerg namespace {
437330f729Sjoerg
447330f729Sjoerg /// A helper class, which walks the AST and locates all the call sites in the
457330f729Sjoerg /// given function body.
467330f729Sjoerg class CGBuilder : public StmtVisitor<CGBuilder> {
477330f729Sjoerg CallGraph *G;
487330f729Sjoerg CallGraphNode *CallerNode;
497330f729Sjoerg
507330f729Sjoerg public:
CGBuilder(CallGraph * g,CallGraphNode * N)517330f729Sjoerg CGBuilder(CallGraph *g, CallGraphNode *N) : G(g), CallerNode(N) {}
527330f729Sjoerg
VisitStmt(Stmt * S)537330f729Sjoerg void VisitStmt(Stmt *S) { VisitChildren(S); }
547330f729Sjoerg
getDeclFromCall(CallExpr * CE)557330f729Sjoerg Decl *getDeclFromCall(CallExpr *CE) {
567330f729Sjoerg if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
577330f729Sjoerg return CalleeDecl;
587330f729Sjoerg
597330f729Sjoerg // Simple detection of a call through a block.
607330f729Sjoerg Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
617330f729Sjoerg if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
627330f729Sjoerg NumBlockCallEdges++;
637330f729Sjoerg return Block->getBlockDecl();
647330f729Sjoerg }
657330f729Sjoerg
667330f729Sjoerg return nullptr;
677330f729Sjoerg }
687330f729Sjoerg
addCalledDecl(Decl * D,Expr * CallExpr)69*e038c9c4Sjoerg void addCalledDecl(Decl *D, Expr *CallExpr) {
70*e038c9c4Sjoerg if (G->includeCalleeInGraph(D)) {
717330f729Sjoerg CallGraphNode *CalleeNode = G->getOrInsertNode(D);
72*e038c9c4Sjoerg CallerNode->addCallee({CalleeNode, CallExpr});
737330f729Sjoerg }
747330f729Sjoerg }
757330f729Sjoerg
VisitCallExpr(CallExpr * CE)767330f729Sjoerg void VisitCallExpr(CallExpr *CE) {
777330f729Sjoerg if (Decl *D = getDeclFromCall(CE))
78*e038c9c4Sjoerg addCalledDecl(D, CE);
797330f729Sjoerg VisitChildren(CE);
807330f729Sjoerg }
817330f729Sjoerg
VisitLambdaExpr(LambdaExpr * LE)827330f729Sjoerg void VisitLambdaExpr(LambdaExpr *LE) {
837330f729Sjoerg if (FunctionTemplateDecl *FTD = LE->getDependentCallOperator())
847330f729Sjoerg for (FunctionDecl *FD : FTD->specializations())
857330f729Sjoerg G->VisitFunctionDecl(FD);
867330f729Sjoerg else if (CXXMethodDecl *MD = LE->getCallOperator())
877330f729Sjoerg G->VisitFunctionDecl(MD);
887330f729Sjoerg }
897330f729Sjoerg
VisitCXXNewExpr(CXXNewExpr * E)907330f729Sjoerg void VisitCXXNewExpr(CXXNewExpr *E) {
917330f729Sjoerg if (FunctionDecl *FD = E->getOperatorNew())
92*e038c9c4Sjoerg addCalledDecl(FD, E);
937330f729Sjoerg VisitChildren(E);
947330f729Sjoerg }
957330f729Sjoerg
VisitCXXConstructExpr(CXXConstructExpr * E)967330f729Sjoerg void VisitCXXConstructExpr(CXXConstructExpr *E) {
977330f729Sjoerg CXXConstructorDecl *Ctor = E->getConstructor();
987330f729Sjoerg if (FunctionDecl *Def = Ctor->getDefinition())
99*e038c9c4Sjoerg addCalledDecl(Def, E);
1007330f729Sjoerg VisitChildren(E);
1017330f729Sjoerg }
1027330f729Sjoerg
1037330f729Sjoerg // Include the evaluation of the default argument.
VisitCXXDefaultArgExpr(CXXDefaultArgExpr * E)1047330f729Sjoerg void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
1057330f729Sjoerg Visit(E->getExpr());
1067330f729Sjoerg }
1077330f729Sjoerg
1087330f729Sjoerg // Include the evaluation of the default initializers in a class.
VisitCXXDefaultInitExpr(CXXDefaultInitExpr * E)1097330f729Sjoerg void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
1107330f729Sjoerg Visit(E->getExpr());
1117330f729Sjoerg }
1127330f729Sjoerg
1137330f729Sjoerg // Adds may-call edges for the ObjC message sends.
VisitObjCMessageExpr(ObjCMessageExpr * ME)1147330f729Sjoerg void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
1157330f729Sjoerg if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
1167330f729Sjoerg Selector Sel = ME->getSelector();
1177330f729Sjoerg
1187330f729Sjoerg // Find the callee definition within the same translation unit.
1197330f729Sjoerg Decl *D = nullptr;
1207330f729Sjoerg if (ME->isInstanceMessage())
1217330f729Sjoerg D = IDecl->lookupPrivateMethod(Sel);
1227330f729Sjoerg else
1237330f729Sjoerg D = IDecl->lookupPrivateClassMethod(Sel);
1247330f729Sjoerg if (D) {
125*e038c9c4Sjoerg addCalledDecl(D, ME);
1267330f729Sjoerg NumObjCCallEdges++;
1277330f729Sjoerg }
1287330f729Sjoerg }
1297330f729Sjoerg }
1307330f729Sjoerg
VisitChildren(Stmt * S)1317330f729Sjoerg void VisitChildren(Stmt *S) {
1327330f729Sjoerg for (Stmt *SubStmt : S->children())
1337330f729Sjoerg if (SubStmt)
1347330f729Sjoerg this->Visit(SubStmt);
1357330f729Sjoerg }
1367330f729Sjoerg };
1377330f729Sjoerg
1387330f729Sjoerg } // namespace
1397330f729Sjoerg
addNodesForBlocks(DeclContext * D)1407330f729Sjoerg void CallGraph::addNodesForBlocks(DeclContext *D) {
1417330f729Sjoerg if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
1427330f729Sjoerg addNodeForDecl(BD, true);
1437330f729Sjoerg
1447330f729Sjoerg for (auto *I : D->decls())
1457330f729Sjoerg if (auto *DC = dyn_cast<DeclContext>(I))
1467330f729Sjoerg addNodesForBlocks(DC);
1477330f729Sjoerg }
1487330f729Sjoerg
CallGraph()1497330f729Sjoerg CallGraph::CallGraph() {
1507330f729Sjoerg Root = getOrInsertNode(nullptr);
1517330f729Sjoerg }
1527330f729Sjoerg
1537330f729Sjoerg CallGraph::~CallGraph() = default;
1547330f729Sjoerg
includeInGraph(const Decl * D)1557330f729Sjoerg bool CallGraph::includeInGraph(const Decl *D) {
1567330f729Sjoerg assert(D);
1577330f729Sjoerg if (!D->hasBody())
1587330f729Sjoerg return false;
1597330f729Sjoerg
160*e038c9c4Sjoerg return includeCalleeInGraph(D);
161*e038c9c4Sjoerg }
162*e038c9c4Sjoerg
includeCalleeInGraph(const Decl * D)163*e038c9c4Sjoerg bool CallGraph::includeCalleeInGraph(const Decl *D) {
1647330f729Sjoerg if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1657330f729Sjoerg // We skip function template definitions, as their semantics is
1667330f729Sjoerg // only determined when they are instantiated.
1677330f729Sjoerg if (FD->isDependentContext())
1687330f729Sjoerg return false;
1697330f729Sjoerg
1707330f729Sjoerg IdentifierInfo *II = FD->getIdentifier();
1717330f729Sjoerg if (II && II->getName().startswith("__inline"))
1727330f729Sjoerg return false;
1737330f729Sjoerg }
1747330f729Sjoerg
1757330f729Sjoerg return true;
1767330f729Sjoerg }
1777330f729Sjoerg
addNodeForDecl(Decl * D,bool IsGlobal)1787330f729Sjoerg void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
1797330f729Sjoerg assert(D);
1807330f729Sjoerg
1817330f729Sjoerg // Allocate a new node, mark it as root, and process its calls.
1827330f729Sjoerg CallGraphNode *Node = getOrInsertNode(D);
1837330f729Sjoerg
1847330f729Sjoerg // Process all the calls by this function as well.
1857330f729Sjoerg CGBuilder builder(this, Node);
1867330f729Sjoerg if (Stmt *Body = D->getBody())
1877330f729Sjoerg builder.Visit(Body);
1887330f729Sjoerg
1897330f729Sjoerg // Include C++ constructor member initializers.
1907330f729Sjoerg if (auto constructor = dyn_cast<CXXConstructorDecl>(D)) {
1917330f729Sjoerg for (CXXCtorInitializer *init : constructor->inits()) {
1927330f729Sjoerg builder.Visit(init->getInit());
1937330f729Sjoerg }
1947330f729Sjoerg }
1957330f729Sjoerg }
1967330f729Sjoerg
getNode(const Decl * F) const1977330f729Sjoerg CallGraphNode *CallGraph::getNode(const Decl *F) const {
1987330f729Sjoerg FunctionMapTy::const_iterator I = FunctionMap.find(F);
1997330f729Sjoerg if (I == FunctionMap.end()) return nullptr;
2007330f729Sjoerg return I->second.get();
2017330f729Sjoerg }
2027330f729Sjoerg
getOrInsertNode(Decl * F)2037330f729Sjoerg CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
2047330f729Sjoerg if (F && !isa<ObjCMethodDecl>(F))
2057330f729Sjoerg F = F->getCanonicalDecl();
2067330f729Sjoerg
2077330f729Sjoerg std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
2087330f729Sjoerg if (Node)
2097330f729Sjoerg return Node.get();
2107330f729Sjoerg
2117330f729Sjoerg Node = std::make_unique<CallGraphNode>(F);
2127330f729Sjoerg // Make Root node a parent of all functions to make sure all are reachable.
2137330f729Sjoerg if (F)
214*e038c9c4Sjoerg Root->addCallee({Node.get(), /*Call=*/nullptr});
2157330f729Sjoerg return Node.get();
2167330f729Sjoerg }
2177330f729Sjoerg
print(raw_ostream & OS) const2187330f729Sjoerg void CallGraph::print(raw_ostream &OS) const {
2197330f729Sjoerg OS << " --- Call graph Dump --- \n";
2207330f729Sjoerg
2217330f729Sjoerg // We are going to print the graph in reverse post order, partially, to make
2227330f729Sjoerg // sure the output is deterministic.
2237330f729Sjoerg llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(this);
2247330f729Sjoerg for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator
2257330f729Sjoerg I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
2267330f729Sjoerg const CallGraphNode *N = *I;
2277330f729Sjoerg
2287330f729Sjoerg OS << " Function: ";
2297330f729Sjoerg if (N == Root)
2307330f729Sjoerg OS << "< root >";
2317330f729Sjoerg else
2327330f729Sjoerg N->print(OS);
2337330f729Sjoerg
2347330f729Sjoerg OS << " calls: ";
2357330f729Sjoerg for (CallGraphNode::const_iterator CI = N->begin(),
2367330f729Sjoerg CE = N->end(); CI != CE; ++CI) {
237*e038c9c4Sjoerg assert(CI->Callee != Root && "No one can call the root node.");
238*e038c9c4Sjoerg CI->Callee->print(OS);
2397330f729Sjoerg OS << " ";
2407330f729Sjoerg }
2417330f729Sjoerg OS << '\n';
2427330f729Sjoerg }
2437330f729Sjoerg OS.flush();
2447330f729Sjoerg }
2457330f729Sjoerg
dump() const2467330f729Sjoerg LLVM_DUMP_METHOD void CallGraph::dump() const {
2477330f729Sjoerg print(llvm::errs());
2487330f729Sjoerg }
2497330f729Sjoerg
viewGraph() const2507330f729Sjoerg void CallGraph::viewGraph() const {
2517330f729Sjoerg llvm::ViewGraph(this, "CallGraph");
2527330f729Sjoerg }
2537330f729Sjoerg
print(raw_ostream & os) const2547330f729Sjoerg void CallGraphNode::print(raw_ostream &os) const {
2557330f729Sjoerg if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
2567330f729Sjoerg return ND->printQualifiedName(os);
2577330f729Sjoerg os << "< >";
2587330f729Sjoerg }
2597330f729Sjoerg
dump() const2607330f729Sjoerg LLVM_DUMP_METHOD void CallGraphNode::dump() const {
2617330f729Sjoerg print(llvm::errs());
2627330f729Sjoerg }
2637330f729Sjoerg
2647330f729Sjoerg namespace llvm {
2657330f729Sjoerg
2667330f729Sjoerg template <>
2677330f729Sjoerg struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits2687330f729Sjoerg DOTGraphTraits (bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
2697330f729Sjoerg
getNodeLabelllvm::DOTGraphTraits2707330f729Sjoerg static std::string getNodeLabel(const CallGraphNode *Node,
2717330f729Sjoerg const CallGraph *CG) {
2727330f729Sjoerg if (CG->getRoot() == Node) {
2737330f729Sjoerg return "< root >";
2747330f729Sjoerg }
2757330f729Sjoerg if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
2767330f729Sjoerg return ND->getNameAsString();
2777330f729Sjoerg else
2787330f729Sjoerg return "< >";
2797330f729Sjoerg }
2807330f729Sjoerg };
2817330f729Sjoerg
2827330f729Sjoerg } // namespace llvm
283