clang 24.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 // 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;
46 const_cast<TranslationUnitDecl *>(Ctx.getTranslationUnitDecl()));
47
48 for (const auto &N : llvm::make_second_range(CG)) {
49 if (N && N->getDecl() && N->getDefinition())
50 handleCallGraphNode(Ctx, N.get());
51 }
52}
53
54void 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(Definition);
62 if (!CallerId)
63 return;
64
65 auto FnSummary = std::make_unique<CallGraphSummary>();
66
67 PresumedLoc Loc =
68 Ctx.getSourceManager().getPresumedLoc(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(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>(CalleeDecl))
87 continue;
88
89 // FIXME: `clang::CallGraph` does not create entries for primary templates.
90 assert(!CalleeDecl->isTemplated());
91
92 auto CalleeId = addEntity(cast<NamedDecl>(CalleeDecl));
93 if (!CalleeId)
94 continue;
95
96 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(CalleeDecl);
97 MD && MD->isVirtual()) {
98 FnSummary->VirtualCallees.insert(*CalleeId);
99 continue;
100 }
101 FnSummary->DirectCallees.insert(*CalleeId);
102 }
103
104 SummaryBuilder.addSummary(*CallerId, std::move(FnSummary));
105}
106
107static TUSummaryExtractorRegistry::Add<CallGraphExtractor>
109 "Extracts static call-graph information");
110
111namespace clang::ssaf {
112// NOLINTNEXTLINE(misc-use-internal-linkage)
114} // namespace clang::ssaf
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
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:223
SourceManager & getSourceManager()
Definition ASTContext.h:869
TranslationUnitDecl * getTranslationUnitDecl() const
static std::string getFunctionName(const Decl *D)
FunctionDecl * getDefinition() const
Definition CallGraph.h:207
llvm::iterator_range< iterator > callees()
Iterator access to callees/children of the node.
Definition CallGraph.h:193
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.
volatile int CallGraphExtractorAnchorSource
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