1 // RUN: %clang_analyze_cc1 -analyzer-checker=debug.DumpCallGraph %s -fblocks -std=c++14 2>&1 | FileCheck %s
2 
get5()3 int get5() {
4   return 5;
5 }
6 
add(int val1,int val2)7 int add(int val1, int val2) {
8   return val1 + val2;
9 }
10 
test_add()11 int test_add() {
12   return add(10, get5());
13 }
14 
mmm(int y)15 static void mmm(int y) {
16   if (y != 0)
17       y++;
18   y = y/y;
19 }
20 
foo(int x,int y)21 static int foo(int x, int y) {
22     mmm(y);
23     if (x != 0)
24       x++;
25     return 5/x;
26 }
27 
aaa()28 void aaa() {
29   foo(1,2);
30 }
31 
bbb(int y)32 void bbb(int y) {
33   int x = (y > 2);
34   ^ {
35       foo(x, y);
36   }();
37 }
38 void ccc();
ddd()39 void ddd() { ccc(); }
ccc()40 void ccc() {}
41 
42 void eee();
eee()43 void eee() {}
fff()44 void fff() { eee(); }
45 
46 // This test case tests that forward declaration for the top-level function
47 // does not affect call graph construction.
do_nothing()48 void do_nothing() {}
49 void test_single_call();
test_single_call()50 void test_single_call() {
51   do_nothing();
52 }
53 
54 namespace SomeNS {
55 template<typename T>
templ(T t)56 void templ(T t) {
57   ccc();
58 }
59 
60 template<>
templ(double t)61 void templ<double>(double t) {
62   eee();
63 }
64 
templUser()65 void templUser() {
66   templ(5);
67   templ(5.5);
68 }
69 }
70 
71 namespace Lambdas {
Callee()72   void Callee(){}
73 
f1()74   void f1() {
75     [](int i) {
76       Callee();
77     }(1);
78     [](auto i) {
79       Callee();
80     }(1);
81   }
82 }
83 
84 namespace CallDecl {
85   void SomeDecl();
86   void SomeOtherDecl();
SomeDef()87   void SomeDef() {}
88 
Caller()89   void Caller() {
90     SomeDecl();
91     SomeOtherDecl();
92   }
93 
SomeOtherDecl()94   void SomeOtherDecl() {
95     SomeDef();
96   }
97 }
98 
99 // 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::SomeDecl CallDecl::SomeOtherDecl $}}
101 // CHECK-NEXT: {{Function: CallDecl::Caller calls: CallDecl::SomeDecl CallDecl::SomeOtherDecl $}}
102 // CHECK-NEXT: {{Function: CallDecl::SomeOtherDecl calls: CallDecl::SomeDef $}}
103 // CHECK-NEXT: {{Function: CallDecl::SomeDecl calls: $}}
104 // CHECK-NEXT: {{Function: CallDecl::SomeDef calls: $}}
105 // CHECK-NEXT: {{Function: Lambdas::f1 calls: Lambdas::f1\(\)::\(anonymous class\)::operator\(\) Lambdas::f1\(\)::\(anonymous class\)::operator\(\) $}}
106 // CHECK-NEXT: {{Function: Lambdas::f1\(\)::\(anonymous class\)::operator\(\) calls: Lambdas::Callee $}}
107 // CHECK-NEXT: {{Function: Lambdas::f1\(\)::\(anonymous class\)::operator\(\) calls: Lambdas::Callee $}}
108 // CHECK-NEXT: {{Function: Lambdas::Callee calls: $}}
109 // CHECK-NEXT: {{Function: SomeNS::templUser calls: SomeNS::templ SomeNS::templ $}}
110 // CHECK-NEXT: {{Function: SomeNS::templ calls: eee $}}
111 // CHECK-NEXT: {{Function: SomeNS::templ calls: ccc $}}
112 // CHECK-NEXT: {{Function: test_single_call calls: do_nothing $}}
113 // CHECK-NEXT: {{Function: do_nothing calls: $}}
114 // CHECK-NEXT: {{Function: fff calls: eee $}}
115 // CHECK-NEXT: {{Function: eee calls: $}}
116 // CHECK-NEXT: {{Function: ddd calls: ccc $}}
117 // CHECK-NEXT: {{Function: ccc calls: $}}
118 // CHECK-NEXT: {{Function: bbb calls: < > $}}
119 // CHECK-NEXT: {{Function: < > calls: foo $}}
120 // CHECK-NEXT: {{Function: aaa calls: foo $}}
121 // CHECK-NEXT: {{Function: foo calls: mmm $}}
122 // CHECK-NEXT: {{Function: mmm calls: $}}
123 // CHECK-NEXT: {{Function: test_add calls: add get5 $}}
124 // CHECK-NEXT: {{Function: add calls: $}}
125 // CHECK-NEXT: {{Function: get5 calls: $}}
126