1 // RUN: rm -rf %t
2 // RUN: mkdir -p %t
3 // RUN: cp "%s" "%t/ctu-on-demand-parsing.c"
4 // RUN: cp "%S/Inputs/ctu-other.c" "%t/ctu-other.c"
5 //
6 // Path substitutions on Windows platform could contain backslashes. These are escaped in the json file.
7 // compile_commands.json is only needed for extdef_mapping, not for the analysis itself.
8 // RUN: echo '[{"directory":"%t","command":"gcc -std=c89 -Wno-visibility ctu-other.c","file":"ctu-other.c"}]' | sed -e 's/\\/\\\\/g' > %t/compile_commands.json
9 //
10 // RUN: echo '"%t/ctu-other.c": ["gcc", "-std=c89", "-Wno-visibility", "ctu-other.c"]' | sed -e 's/\\/\\\\/g' > %t/invocations.yaml
11 //
12 // RUN: cd "%t" && %clang_extdef_map "%t/ctu-other.c" > externalDefMap.txt
13 //
14 // RUN: cd "%t" && %clang_cc1 -fsyntax-only -std=c89 -analyze \
15 // RUN:   -analyzer-checker=core,debug.ExprInspection \
16 // RUN:   -analyzer-config experimental-enable-naive-ctu-analysis=true \
17 // RUN:   -analyzer-config ctu-dir=. \
18 // RUN:   -analyzer-config ctu-invocation-list=invocations.yaml \
19 // RUN:   -verify ctu-on-demand-parsing.c
20 //
21 // FIXME: Path handling should work on all platforms.
22 // REQUIRES: system-linux
23 
24 void clang_analyzer_eval(int);
25 
26 // Test typedef and global variable in function.
27 typedef struct {
28   int a;
29   int b;
30 } FooBar;
31 extern FooBar fb;
32 int f(int);
testGlobalVariable()33 void testGlobalVariable() {
34   clang_analyzer_eval(f(5) == 1); // expected-warning{{TRUE}}
35 }
36 
37 // Test enums.
38 int enumCheck(void);
39 enum A { x,
40          y,
41          z };
testEnum()42 void testEnum() {
43   clang_analyzer_eval(x == 0);            // expected-warning{{TRUE}}
44   clang_analyzer_eval(enumCheck() == 42); // expected-warning{{TRUE}}
45 }
46 
47 // Test that asm import does not fail.
48 int inlineAsm();
testInlineAsm()49 int testInlineAsm() { return inlineAsm(); }
50 
51 // Test reporting error in a macro.
52 struct S;
53 int g(struct S *);
testMacro(void)54 void testMacro(void) {
55   g(0);
56   // expected-warning@ctu-other.c:29 {{Access to field 'a' results in a dereference of a null pointer (loaded from variable 'ctx')}}
57 }
58 
59 // The external function prototype is incomplete.
60 // warning:implicit functions are prohibited by c99
testImplicit()61 void testImplicit() {
62   int res = identImplicit(6);    // external implicit functions are not inlined
63   clang_analyzer_eval(res == 6); // expected-warning{{TRUE}}
64   // Call something with uninitialized from the same function in which the
65   // implicit was called. This is necessary to reproduce a special bug in
66   // NoStoreFuncVisitor.
67   int uninitialized;
68   h(uninitialized); // expected-warning{{1st function call argument is an uninitialized value}}
69 }
70 
71 // Tests the import of functions that have a struct parameter
72 // defined in its prototype.
73 struct DataType {
74   int a;
75   int b;
76 };
77 int structInProto(struct DataType *d);
testStructDefInArgument()78 void testStructDefInArgument() {
79   struct DataType d;
80   d.a = 1;
81   d.b = 0;
82   clang_analyzer_eval(structInProto(&d) == 0); // expected-warning{{TRUE}} expected-warning{{FALSE}}
83 }
84