| 1 | //===- CallGraphExtractor.cpp - Call Graph Summary Extractor --------------===// |
| 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 "clang/AST/ASTContext.h" |
| 10 | #include "clang/AST/Decl.h" |
| 11 | #include "clang/AST/DeclCXX.h" |
| 12 | #include "clang/AST/DeclObjC.h" |
| 13 | #include "clang/Analysis/AnalysisDeclContext.h" |
| 14 | #include "clang/Analysis/CallGraph.h" |
| 15 | #include "clang/Basic/SourceManager.h" |
| 16 | #include "clang/ScalableStaticAnalysis/Analyses/CallGraph/CallGraphSummary.h" |
| 17 | #include "clang/ScalableStaticAnalysis/Core/TUSummary/ExtractorRegistry.h" |
| 18 | #include "clang/ScalableStaticAnalysis/Core/TUSummary/TUSummaryBuilder.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include <memory> |
| 21 | |
| 22 | using namespace clang; |
| 23 | using namespace ssaf; |
| 24 | |
| 25 | namespace { |
| 26 | class final : public TUSummaryExtractor { |
| 27 | public: |
| 28 | using TUSummaryExtractor::TUSummaryExtractor; |
| 29 | |
| 30 | private: |
| 31 | void HandleTranslationUnit(ASTContext &Ctx) override; |
| 32 | |
| 33 | void handleCallGraphNode(const ASTContext &Ctx, const CallGraphNode *N); |
| 34 | }; |
| 35 | } // namespace |
| 36 | |
| 37 | void CallGraphExtractor::HandleTranslationUnit(ASTContext &Ctx) { |
| 38 | // FIXME: Depending on the IncludeLocalEntities option, the extractor should |
| 39 | // include or exclude calls to function-local defined: |
| 40 | // - lambda functions |
| 41 | // - methods of local classes |
| 42 | // Currently, the extractor always includes these callees, even if |
| 43 | // IncludeLocalEntities is false. |
| 44 | CallGraph CG; |
| 45 | CG.addToCallGraph( |
| 46 | D: const_cast<TranslationUnitDecl *>(Ctx.getTranslationUnitDecl())); |
| 47 | |
| 48 | for (const auto &N : llvm::make_second_range(c&: CG)) { |
| 49 | if (N && N->getDecl() && N->getDefinition()) |
| 50 | handleCallGraphNode(Ctx, N: N.get()); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void CallGraphExtractor::handleCallGraphNode(const ASTContext &Ctx, |
| 55 | const CallGraphNode *N) { |
| 56 | const FunctionDecl *Definition = N->getDefinition(); |
| 57 | |
| 58 | // FIXME: `clang::CallGraph` does not create entries for primary templates. |
| 59 | assert(!Definition->isTemplated()); |
| 60 | |
| 61 | auto CallerId = addEntity(D: Definition); |
| 62 | if (!CallerId) |
| 63 | return; |
| 64 | |
| 65 | auto FnSummary = std::make_unique<CallGraphSummary>(); |
| 66 | |
| 67 | PresumedLoc Loc = |
| 68 | Ctx.getSourceManager().getPresumedLoc(Loc: Definition->getLocation()); |
| 69 | FnSummary->Definition.File = Loc.getFilename(); |
| 70 | FnSummary->Definition.Line = Loc.getLine(); |
| 71 | FnSummary->Definition.Column = Loc.getColumn(); |
| 72 | FnSummary->PrettyName = AnalysisDeclContext::getFunctionName(D: Definition); |
| 73 | |
| 74 | for (const auto &Record : N->callees()) { |
| 75 | const Decl *CalleeDecl = Record.Callee->getDecl(); |
| 76 | |
| 77 | // FIXME: `clang::CallGraph` does not consider indirect calls, thus this is |
| 78 | // never null. |
| 79 | assert(CalleeDecl); |
| 80 | |
| 81 | // `clang::CallGraph` resolves ObjCMessageExprs (including property |
| 82 | // dot-syntax) to their ObjCMethodDecls and adds them as callees — see |
| 83 | // `CGBuilder::VisitObjCMessageExpr` in clang/lib/Analysis/CallGraph.cpp. |
| 84 | // ObjC dispatch is dynamic, so recording these as direct callees would be |
| 85 | // misleading; skip them until we model ObjC properly. |
| 86 | if (isa<ObjCMethodDecl>(Val: CalleeDecl)) |
| 87 | continue; |
| 88 | |
| 89 | // FIXME: `clang::CallGraph` does not create entries for primary templates. |
| 90 | assert(!CalleeDecl->isTemplated()); |
| 91 | |
| 92 | auto CalleeId = addEntity(D: cast<NamedDecl>(Val: CalleeDecl)); |
| 93 | if (!CalleeId) |
| 94 | continue; |
| 95 | |
| 96 | if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: CalleeDecl); |
| 97 | MD && MD->isVirtual()) { |
| 98 | FnSummary->VirtualCallees.insert(x: *CalleeId); |
| 99 | continue; |
| 100 | } |
| 101 | FnSummary->DirectCallees.insert(x: *CalleeId); |
| 102 | } |
| 103 | |
| 104 | SummaryBuilder.addSummary(Entity: *CallerId, Data: std::move(FnSummary)); |
| 105 | } |
| 106 | |
| 107 | static TUSummaryExtractorRegistry::Add<CallGraphExtractor> |
| 108 | (CallGraphSummary::Name, |
| 109 | "Extracts static call-graph information" ); |
| 110 | |
| 111 | namespace clang::ssaf { |
| 112 | // NOLINTNEXTLINE(misc-use-internal-linkage) |
| 113 | volatile int = 0; |
| 114 | } // namespace clang::ssaf |
| 115 | |