xref: /llvm-project/clang/test/Analysis/ctu-main.c (revision e4d242768aefabc0091dd01fabecaffbc2b6984b)
1 // RUN: rm -rf %t && mkdir %t
2 // RUN: mkdir -p %t/ctudir2
3 // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu \
4 // RUN:   -emit-pch -o %t/ctudir2/ctu-other.c.ast %S/Inputs/ctu-other.c
5 // RUN: cp %S/Inputs/ctu-other.c.externalDefMap.ast-dump.txt %t/ctudir2/externalDefMap.txt
6 
7 // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -std=c89 -analyze \
8 // RUN:   -analyzer-checker=core,debug.ExprInspection \
9 // RUN:   -analyzer-config eagerly-assume=false \
10 // RUN:   -analyzer-config experimental-enable-naive-ctu-analysis=true \
11 // RUN:   -analyzer-config ctu-dir=%t/ctudir2 \
12 // RUN:   -analyzer-config ctu-phase1-inlining=none \
13 // RUN:   -verify=newctu %s
14 
15 // Simulate the behavior of the previous CTU implementation by inlining all
16 // functions during the first phase. This way, the second phase is a noop.
17 // RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -std=c89 -analyze \
18 // RUN:   -analyzer-checker=core,debug.ExprInspection \
19 // RUN:   -analyzer-config eagerly-assume=false \
20 // RUN:   -analyzer-config experimental-enable-naive-ctu-analysis=true \
21 // RUN:   -analyzer-config ctu-dir=%t/ctudir2 \
22 // RUN:   -analyzer-config ctu-phase1-inlining=all \
23 // RUN:   -verify=oldctu %s
24 
25 void clang_analyzer_eval(int);
26 
27 // A function that's definition is unknown both for single-tu (stu) and ctu
28 // mode.
29 int unknown(int);
test_unknown()30 void test_unknown() {
31   int res = unknown(6);
32   clang_analyzer_eval(res == 6); // newctu-warning{{UNKNOWN}}
33                                  // oldctu-warning@-1{{UNKNOWN}}
34 }
35 
36 // Test typedef and global variable in function.
37 typedef struct {
38   int a;
39   int b;
40 } FooBar;
41 extern FooBar fb;
42 int f(int);
testGlobalVariable()43 void testGlobalVariable() {
44   clang_analyzer_eval(f(5) == 1);         // newctu-warning{{TRUE}} ctu
45                                           // newctu-warning@-1{{UNKNOWN}} stu
46                                           // oldctu-warning@-2{{TRUE}}
47 }
48 
49 // Test enums.
50 int enumCheck(void);
51 enum A { x,
52          y,
53          z };
testEnum(void)54 void testEnum(void) {
55   clang_analyzer_eval(x == 0);            // newctu-warning{{TRUE}}
56                                           // oldctu-warning@-1{{TRUE}}
57   clang_analyzer_eval(enumCheck() == 42); // newctu-warning{{TRUE}} ctu
58                                           // newctu-warning@-1{{UNKNOWN}} stu
59                                           // oldctu-warning@-2{{TRUE}}
60 }
61 
62 // Test that asm import does not fail.
63 int inlineAsm(void);
testInlineAsm(void)64 int testInlineAsm(void) {
65   return inlineAsm();
66 }
67 
68 // Test reporting error in a macro.
69 struct S;
70 int g(struct S *);
testMacro(void)71 void testMacro(void) {
72   g(0); // newctu-warning@Inputs/ctu-other.c:29 {{Access to field 'a' results in a dereference of a null pointer (loaded from variable 'ctx')}}
73         // oldctu-warning@Inputs/ctu-other.c:29 {{Access to field 'a' results in a dereference of a null pointer (loaded from variable 'ctx')}}
74 }
75 
76 // The external function prototype is incomplete.
77 // warning:implicit functions are prohibited by c99
testImplicit(void)78 void testImplicit(void) {
79   int res = identImplicit(6);   // external implicit functions are not inlined
80   clang_analyzer_eval(res == 6); // newctu-warning{{TRUE}} ctu
81                                  // newctu-warning@-1{{UNKNOWN}} stu
82                                  // oldctu-warning@-2{{TRUE}}
83   // Call something with uninitialized from the same function in which the implicit was called.
84   // This is necessary to reproduce a special bug in NoStoreFuncVisitor.
85   int uninitialized;
86   h(uninitialized); // newctu-warning{{1st function call argument is an uninitialized value}}
87                     // oldctu-warning@-1{{1st function call argument is an uninitialized value}}
88 }
89 
90 // Tests the import of functions that have a struct parameter
91 // defined in its prototype.
92 struct DataType {
93   int a;
94   int b;
95 };
96 int structInProto(struct DataType *d);
testStructDefInArgument(void)97 void testStructDefInArgument(void) {
98   struct DataType d;
99   d.a = 1;
100   d.b = 0;
101   // Not imported, thus remains unknown both in stu and ctu.
102   clang_analyzer_eval(structInProto(&d) == 0); // newctu-warning{{UNKNOWN}}
103                                                // oldctu-warning@-1{{UNKNOWN}}
104 }
105 
106 int switchWithoutCases(int);
testSwitchStmtCrash(int x)107 void testSwitchStmtCrash(int x) {
108   switchWithoutCases(x);
109 }
110