[clang] Handle __declspec() attributes in using
[llvm-project.git] / clang / test / Analysis / global-region-invalidation.c
blobfaca3baf11caf392138d859b651c692c6572221e
1 // RUN: %clang_analyze_cc1 -triple x86_64-apple-darwin10 -disable-free -verify %s \
2 // RUN: -analyzer-checker=core,deadcode,alpha.security.taint,debug.TaintTest,debug.ExprInspection
4 void clang_analyzer_eval(int);
6 // Note, we do need to include headers here, since the analyzer checks if the function declaration is located in a system header.
7 #include "Inputs/system-header-simulator.h"
9 // Test that system header does not invalidate the internal global.
10 int size_rdar9373039 = 1;
11 int rdar9373039(void) {
12 int x;
13 int j = 0;
15 for (int i = 0 ; i < size_rdar9373039 ; ++i)
16 x = 1;
18 // strlen doesn't invalidate the value of 'size_rdar9373039'.
19 int extra = (2 + strlen ("Clang") + ((4 - ((unsigned int) (2 + strlen ("Clang")) % 4)) % 4)) + (2 + strlen ("1.0") + ((4 - ((unsigned int) (2 + strlen ("1.0")) % 4)) % 4));
21 for (int i = 0 ; i < size_rdar9373039 ; ++i)
22 j += x; // no-warning
24 return j;
27 // Test stdin does not get invalidated by a system call nor by an internal call.
28 void foo(void);
29 int stdinTest(void) {
30 int i = 0;
31 fscanf(stdin, "%d", &i);
32 foo();
33 int m = i; // expected-warning + {{tainted}}
34 fscanf(stdin, "%d", &i);
35 int j = i; // expected-warning + {{tainted}}
36 return m + j; // expected-warning + {{tainted}}
39 // Test that const integer does not get invalidated.
40 const int x = 0;
41 int constIntGlob(void) {
42 const int *m = &x;
43 foo();
44 return 3 / *m; // expected-warning {{Division by zero}}
47 extern const int y;
48 int constIntGlobExtern(void) {
49 if (y == 0) {
50 foo();
51 return 5 / y; // expected-warning {{Division by zero}}
53 return 0;
56 static void * const ptr = 0;
57 void constPtrGlob(void) {
58 clang_analyzer_eval(ptr == 0); // expected-warning{{TRUE}}
59 foo();
60 clang_analyzer_eval(ptr == 0); // expected-warning{{TRUE}}
63 static const int x2 = x;
64 void constIntGlob2(void) {
65 clang_analyzer_eval(x2 == 0); // expected-warning{{TRUE}}
66 foo();
67 clang_analyzer_eval(x2 == 0); // expected-warning{{TRUE}}
70 void testAnalyzerEvalIsPure(void) {
71 extern int someGlobal;
72 if (someGlobal == 0) {
73 clang_analyzer_eval(someGlobal == 0); // expected-warning{{TRUE}}
74 clang_analyzer_eval(someGlobal == 0); // expected-warning{{TRUE}}
78 // Test that static variables with initializers do not get reinitialized on
79 // recursive calls.
80 void Function2(void);
81 int *getPtr(void);
82 void Function1(void) {
83 static unsigned flag;
84 static int *p = 0;
85 if (!flag) {
86 flag = 1;
87 p = getPtr();
89 int m = *p; // no-warning: p is never null.
90 m++;
91 Function2();
93 void Function2(void) {
94 Function1();
97 void SetToNonZero(void) {
98 static int g = 5;
99 clang_analyzer_eval(g == 5); // expected-warning{{TRUE}}