Skip to content

Commit ffcc076

Browse files
author
Erich Keane
committed
[[Clang CallGraph]] CallGraph should still record calls to decls.
Discovered by a downstream user, we found that the CallGraph ignores callees unless they are defined. This seems foolish, and prevents combining the report with other reports to create unified reports. Additionally, declarations contain information that is likely useful to consumers of the CallGraph. This patch implements this by splitting the includeInGraph function into two versions, the current one plus one that is for callees only. The only difference currently is that includeInGraph checks for a body, then calls includeCalleeInGraph. Differential Revision: https://reviews.llvm.org/D76435
1 parent 34659de commit ffcc076

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

clang/include/clang/Analysis/CallGraph.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ class CallGraph : public RecursiveASTVisitor<CallGraph> {
6666
/// Determine if a declaration should be included in the graph.
6767
static bool includeInGraph(const Decl *D);
6868

69+
/// Determine if a declaration should be included in the graph for the
70+
/// purposes of being a callee. This is similar to includeInGraph except
71+
/// it permits declarations, not just definitions.
72+
static bool includeCalleeInGraph(const Decl *D);
73+
6974
/// Lookup the node for the given declaration.
7075
CallGraphNode *getNode(const Decl *) const;
7176

clang/lib/Analysis/CallGraph.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class CGBuilder : public StmtVisitor<CGBuilder> {
6767
}
6868

6969
void addCalledDecl(Decl *D, Expr *CallExpr) {
70-
if (G->includeInGraph(D)) {
70+
if (G->includeCalleeInGraph(D)) {
7171
CallGraphNode *CalleeNode = G->getOrInsertNode(D);
7272
CallerNode->addCallee({CalleeNode, CallExpr});
7373
}
@@ -157,6 +157,10 @@ bool CallGraph::includeInGraph(const Decl *D) {
157157
if (!D->hasBody())
158158
return false;
159159

160+
return includeCalleeInGraph(D);
161+
}
162+
163+
bool CallGraph::includeCalleeInGraph(const Decl *D) {
160164
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
161165
// We skip function template definitions, as their semantics is
162166
// only determined when they are instantiated.

clang/test/Analysis/debug-CallGraph.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ namespace CallDecl {
9797
}
9898

9999
// CHECK:--- Call graph Dump ---
100-
// CHECK-NEXT: {{Function: < root > calls: get5 add test_add mmm foo aaa < > bbb ddd ccc eee fff do_nothing test_single_call SomeNS::templ SomeNS::templ SomeNS::templUser Lambdas::Callee Lambdas::f1 Lambdas::f1\(\)::\(anonymous class\)::operator\(\) Lambdas::f1\(\)::\(anonymous class\)::operator\(\) CallDecl::SomeDef CallDecl::Caller CallDecl::SomeOtherDecl $}}
101-
// CHECK-NEXT: {{Function: CallDecl::Caller calls: CallDecl::SomeOtherDecl $}}
100+
// CHECK-NEXT: {{Function: < root > calls: get5 add test_add mmm foo aaa < > bbb ddd ccc eee fff do_nothing test_single_call SomeNS::templ SomeNS::templ SomeNS::templUser Lambdas::Callee Lambdas::f1 Lambdas::f1\(\)::\(anonymous class\)::operator\(\) Lambdas::f1\(\)::\(anonymous class\)::operator\(\) CallDecl::SomeDef CallDecl::Caller CallDecl::SomeDecl CallDecl::SomeOtherDecl $}}
101+
// CHECK-NEXT: {{Function: CallDecl::Caller calls: CallDecl::SomeDecl CallDecl::SomeOtherDecl $}}
102102
// CHECK-NEXT: {{Function: CallDecl::SomeOtherDecl calls: CallDecl::SomeDef $}}
103+
// CHECK-NEXT: {{Function: CallDecl::SomeDecl calls: $}}
103104
// CHECK-NEXT: {{Function: CallDecl::SomeDef calls: $}}
104105
// CHECK-NEXT: {{Function: Lambdas::f1 calls: Lambdas::f1\(\)::\(anonymous class\)::operator\(\) Lambdas::f1\(\)::\(anonymous class\)::operator\(\) $}}
105106
// CHECK-NEXT: {{Function: Lambdas::f1\(\)::\(anonymous class\)::operator\(\) calls: Lambdas::Callee $}}

0 commit comments

Comments
 (0)