blob: e7fbe4d9870ef32c2dd04c20953a46a9b2ce761c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -x c %s
// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -x c++ -analyzer-config c++-inlining=constructors %s
// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core,debug.ExprInspection -DINLINE -verify -x c %s
// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.core,debug.ExprInspection -DINLINE -verify -x c++ -analyzer-config c++-inlining=constructors %s
void clang_analyzer_eval(int);
struct S {
int field;
#if __cplusplus
const struct S *getThis() const { return this; }
#endif
};
#ifdef INLINE
struct S getS() {
struct S s = { 42 };
return s;
}
#else
struct S getS();
#endif
void testAssignment() {
struct S s = getS();
if (s.field != 42) return;
clang_analyzer_eval(s.field == 42); // expected-warning{{TRUE}}
s.field = 0;
clang_analyzer_eval(s.field == 0); // expected-warning{{TRUE}}
#if __cplusplus
clang_analyzer_eval(s.getThis() == &s); // expected-warning{{TRUE}}
#endif
}
void testImmediateUse() {
int x = getS().field;
if (x != 42) return;
clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
#if __cplusplus
clang_analyzer_eval((void *)getS().getThis() == (void *)&x); // expected-warning{{FALSE}}
#endif
}
int getConstrainedField(struct S s) {
if (s.field != 42) return 42;
return s.field;
}
int getAssignedField(struct S s) {
s.field = 42;
return s.field;
}
void testArgument() {
clang_analyzer_eval(getConstrainedField(getS()) == 42); // expected-warning{{TRUE}}
clang_analyzer_eval(getAssignedField(getS()) == 42); // expected-warning{{TRUE}}
}
//--------------------
// C++-only tests
//--------------------
#if __cplusplus
void testReferenceAssignment() {
const S &s = getS();
if (s.field != 42) return;
clang_analyzer_eval(s.field == 42); // expected-warning{{TRUE}}
clang_analyzer_eval(s.getThis() == &s); // expected-warning{{TRUE}}
}
int getConstrainedFieldRef(const S &s) {
if (s.field != 42) return 42;
return s.field;
}
bool checkThis(const S &s) {
return s.getThis() == &s;
}
void testReferenceArgument() {
clang_analyzer_eval(getConstrainedFieldRef(getS()) == 42); // expected-warning{{TRUE}}
clang_analyzer_eval(checkThis(getS())); // expected-warning{{TRUE}}
}
#endif
|