clang 23.0.0git
CallGraphExtractor.cpp
Go to the documentation of this file.
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
10#include "clang/AST/Decl.h"
11#include "clang/AST/DeclCXX.h"
12#include "clang/AST/DeclObjC.h"
20#include "llvm/ADT/STLExtras.h"
21#include <memory>
22
23using namespace clang;
24using namespace ssaf;
25
26namespace {
27class CallGraphExtractor final : public TUSummaryExtractor {
28public:
30
31private:
32 void HandleTranslationUnit(ASTContext &Ctx) override;
33
34 void handleCallGraphNode(const ASTContext &Ctx, const CallGraphNode *N);
35};
36} // namespace
37
38void CallGraphExtractor::HandleTranslationUnit(ASTContext &Ctx) {
39 CallGraph CG;
41 const_cast<TranslationUnitDecl *>(Ctx.getTranslationUnitDecl()));
42
43 for (const auto &N : llvm::make_second_range(CG)) {
44 if (N && N->getDecl() && N->getDefinition())
45 handleCallGraphNode(Ctx, N.get());
46 }
47}
48
49void CallGraphExtractor::handleCallGraphNode(const ASTContext &Ctx,
50 const CallGraphNode *N) {
51 const FunctionDecl *Definition = N->getDefinition();
52
53 // FIXME: `clang::CallGraph` does not create entries for primary templates.
54 assert(!Definition->isTemplated());
55
56 auto CallerName = getEntityName(Definition);
57 if (!CallerName)
58 return;
59
60 auto FnSummary = std::make_unique<CallGraphSummary>();
61
62 PresumedLoc Loc =
63 Ctx.getSourceManager().getPresumedLoc(Definition->getLocation());
64 FnSummary->Definition.File = Loc.getFilename();
65 FnSummary->Definition.Line = Loc.getLine();
66 FnSummary->Definition.Column = Loc.getColumn();
67 FnSummary->PrettyName = AnalysisDeclContext::getFunctionName(Definition);
68
69 for (const auto &Record : N->callees()) {
70 const Decl *CalleeDecl = Record.Callee->getDecl();
71
72 // FIXME: `clang::CallGraph` does not consider indirect calls, thus this is
73 // never null.
74 assert(CalleeDecl);
75
76 // FIXME: `clang::CallGraph` does not consider ObjCMessageExprs as calls.
77 // Consequently, they don't appear as a Callee.
78 assert(!isa<ObjCMethodDecl>(CalleeDecl));
79
80 // FIXME: `clang::CallGraph` does not create entries for primary templates.
81 assert(!CalleeDecl->isTemplated());
82
83 auto CalleeName = getEntityName(CalleeDecl);
84 if (!CalleeName)
85 continue;
86
87 EntityId CalleeId = SummaryBuilder.addEntity(*CalleeName);
88 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(CalleeDecl);
89 MD && MD->isVirtual()) {
90 FnSummary->VirtualCallees.insert(CalleeId);
91 continue;
92 }
93 FnSummary->DirectCallees.insert(CalleeId);
94 }
95
96 EntityId CallerId = SummaryBuilder.addEntity(*CallerName);
97 SummaryBuilder.addSummary(CallerId, std::move(FnSummary));
98}
99
100static TUSummaryExtractorRegistry::Add<CallGraphExtractor>
101 RegisterExtractor("CallGraph", "Extracts static call-graph information");
102
103// This anchor is used to force the linker to link in the generated object file
104// and thus register the CallGraphExtractor.
105// NOLINTNEXTLINE(misc-use-internal-linkage)
Defines the clang::ASTContext interface.
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
static TUSummaryExtractorRegistry::Add< CallGraphExtractor > RegisterExtractor("CallGraph", "Extracts static call-graph information")
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
llvm::MachO::Record Record
Definition MachO.h:31
volatile int CallGraphExtractorAnchorSource
Defines the SourceManager interface.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:226
SourceManager & getSourceManager()
Definition ASTContext.h:859
TranslationUnitDecl * getTranslationUnitDecl() const
static std::string getFunctionName(const Decl *D)
FunctionDecl * getDefinition() const
Definition CallGraph.h:196
llvm::iterator_range< iterator > callees()
Iterator access to callees/children of the node.
Definition CallGraph.h:182
void addToCallGraph(Decl *D)
Populate the call graph with the functions in the given declaration.
Definition CallGraph.h:64
bool isTemplated() const
Determine whether this declaration is a templated entity (whether it is.
Definition DeclBase.cpp:308
unsigned getColumn() const
Return the presumed column number of this location.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLine() const
Return the presumed line number of this location.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
TUSummaryExtractor(TUSummaryBuilder &Builder)
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
std::optional< EntityName > getEntityName(const Decl *D)
Maps a declaration to an EntityName.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330