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"
19#include "llvm/ADT/STLExtras.h"
20#include <memory>
21
22using namespace clang;
23using namespace ssaf;
24
25namespace {
26class CallGraphExtractor final : public TUSummaryExtractor {
27public:
29
30private:
31 void HandleTranslationUnit(ASTContext &Ctx) override;
32
33 void handleCallGraphNode(const ASTContext &Ctx, const CallGraphNode *N);
34};
35} // namespace
36
37void CallGraphExtractor::HandleTranslationUnit(ASTContext &Ctx) {
38 CallGraph CG;
40 const_cast<TranslationUnitDecl *>(Ctx.getTranslationUnitDecl()));
41
42 for (const auto &N : llvm::make_second_range(CG)) {
43 if (N && N->getDecl() && N->getDefinition())
44 handleCallGraphNode(Ctx, N.get());
45 }
46}
47
48void CallGraphExtractor::handleCallGraphNode(const ASTContext &Ctx,
49 const CallGraphNode *N) {
50 const FunctionDecl *Definition = N->getDefinition();
51
52 // FIXME: `clang::CallGraph` does not create entries for primary templates.
53 assert(!Definition->isTemplated());
54
55 auto CallerId = addEntity(Definition);
56 if (!CallerId)
57 return;
58
59 auto FnSummary = std::make_unique<CallGraphSummary>();
60
61 PresumedLoc Loc =
62 Ctx.getSourceManager().getPresumedLoc(Definition->getLocation());
63 FnSummary->Definition.File = Loc.getFilename();
64 FnSummary->Definition.Line = Loc.getLine();
65 FnSummary->Definition.Column = Loc.getColumn();
66 FnSummary->PrettyName = AnalysisDeclContext::getFunctionName(Definition);
67
68 for (const auto &Record : N->callees()) {
69 const Decl *CalleeDecl = Record.Callee->getDecl();
70
71 // FIXME: `clang::CallGraph` does not consider indirect calls, thus this is
72 // never null.
73 assert(CalleeDecl);
74
75 // FIXME: `clang::CallGraph` does not consider ObjCMessageExprs as calls.
76 // Consequently, they don't appear as a Callee.
77 assert(!isa<ObjCMethodDecl>(CalleeDecl));
78
79 // FIXME: `clang::CallGraph` does not create entries for primary templates.
80 assert(!CalleeDecl->isTemplated());
81
82 auto CalleeId = addEntity(cast<NamedDecl>(CalleeDecl));
83 if (!CalleeId)
84 continue;
85
86 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(CalleeDecl);
87 MD && MD->isVirtual()) {
88 FnSummary->VirtualCallees.insert(*CalleeId);
89 continue;
90 }
91 FnSummary->DirectCallees.insert(*CalleeId);
92 }
93
94 SummaryBuilder.addSummary(*CallerId, std::move(FnSummary));
95}
96
97static TUSummaryExtractorRegistry::Add<CallGraphExtractor>
99 "Extracts static call-graph information");
100
101// This anchor is used to force the linker to link in the generated object file
102// and thus register the CallGraphExtractor.
103// 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(CallGraphSummary::Name, "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:227
SourceManager & getSourceManager()
Definition ASTContext.h:866
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.
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
U cast(CodeGen::Address addr)
Definition Address.h:327
static constexpr llvm::StringLiteral Name