1 // RUN: %clang_analyze_cc1 -triple=x86_64-unknown-linux-gnu -analyzer-checker=core,apiModeling,debug.ExprInspection -analyzer-config eagerly-assume=false -verify %s
3 void clang_analyzer_dump(int);
4 void clang_analyzer_dumpL(long);
5 void clang_analyzer_warnIfReached();
7 void testInspect(int x
) {
8 if ((x
< 10) || (x
> 100)) {
15 clang_analyzer_dump(i
); // expected-warning-re {{(reg_${{[0-9]+}}<int x>) + 1 }}
16 clang_analyzer_dumpL(l
); // expected-warning-re {{(reg_${{[0-9]+}}<int x>) - 9U }} instead of + 4294967287U
17 clang_analyzer_dumpL(l
+ 0L); // expected-warning-re {{(reg_${{[0-9]+}}<int x>) - 9 }} instead of + 4294967287
20 clang_analyzer_warnIfReached(); // no-warning
23 clang_analyzer_warnIfReached(); // no-warning
26 clang_analyzer_warnIfReached(); // no-warning
28 if ((l
+ 0L) > 1000) {
29 clang_analyzer_warnIfReached(); // no-warning
34 clang_analyzer_dumpL(l
); // expected-warning-re {{(reg_${{[0-9]+}}<int x>) + 9U }} instead of - 4294967287U
38 clang_analyzer_dumpL(l
); // expected-warning-re {{(reg_${{[0-9]+}}<int x>) - 11U }} instead of + 4294967285U
42 clang_analyzer_dumpL(l
); // expected-warning-re {{(reg_${{[0-9]+}}<int x>) - 9U }} instead of + 4294967287U
45 const int intMin
= 1 << (sizeof(int) * 8 - 1); // INT_MIN, negation value is not representable
46 const long longMin
= 1L << (sizeof(long) * 8 - 1); // LONG_MIN, negation value is not representable
48 void testMin(int i
, long l
) {
49 clang_analyzer_dump(i
+ (-1)); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) - 1 }} instead of + -1
50 clang_analyzer_dump(i
- (-1)); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) + 1 }} instead of - -1
51 clang_analyzer_dumpL(l
+ (-1)); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) - 1 }} instead of + -1
52 clang_analyzer_dumpL(l
- (-1)); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) + 1 }} instead of - -1
54 // Do not normalize representation if negation would not be representable
55 clang_analyzer_dump(i
+ intMin
); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) + -2147483648 }} no change
56 clang_analyzer_dump(i
- intMin
); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) - -2147483648 }} no change
57 // Produced value has higher bit with (long) so negation if representable
58 clang_analyzer_dumpL(l
+ intMin
); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) - 2147483648 }} instead of + -2147483648
59 clang_analyzer_dumpL(l
- intMin
); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) + 2147483648 }} instead of - -2147483648
62 void changingToUnsinged(unsigned u
, int i
) {
63 unsigned c
= u
+ (unsigned)i
;
64 unsigned d
= u
- (unsigned)i
;
66 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) - 1U }}
67 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) + 1U }}
71 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) - 2147483648U }}
72 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) + 2147483648U }}
77 void extendingToSigned(long l
, int i
) {
81 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) - 1 }}
82 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) + 1 }}
86 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) - 2147483648 }}
87 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<long l>) + 2147483648 }}
92 void extendingToUnigned(unsigned long ul
, int i
) {
93 unsigned long c
= ul
+ (unsigned long)i
;
94 unsigned long d
= ul
- (unsigned long)i
;
96 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned long ul>) - 1U }}
97 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned long ul>) + 1U }}
101 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned long ul>) - 2147483648U }}
102 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned long ul>) + 2147483648U }}
107 void truncatingToSigned(int i
, long l
) {
111 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) - 1 }}
112 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) + 1 }}
115 if (l
== (long)intMin
) { // negation outside of range, no-changes
116 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) + -2147483648 }}
117 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) + -2147483648 }}
120 if (l
== ((long)intMin
- 1L)) { // outside or range, no changes
121 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) + 2147483647 }}
122 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<int i>) - 2147483647 }}
125 if (l
== longMin
) { // outside of range, no changes
126 clang_analyzer_dump(c
+ 0); // expected-warning-re {{reg_${{[0-9]+}}<int i> }}
127 clang_analyzer_dump(d
+ 0); // expected-warning-re {{reg_${{[0-9]+}}<int i> }}
132 void truncatingToUnsigned(unsigned u
, long l
) {
133 unsigned c
= u
+ (unsigned)l
;
134 unsigned d
= u
- (unsigned)l
;
136 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) - 1U }}
137 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) + 1U }}
140 if (l
== (long)intMin
) {
141 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) - 2147483648U }}
142 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) + 2147483648U }}
145 if (l
== ((long)intMin
- 1L)) { // outside or range, no changes
146 clang_analyzer_dump(c
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) + 2147483647U }}
147 clang_analyzer_dump(d
+ 0); // expected-warning-re {{(reg_${{[0-9]+}}<unsigned int u>) - 2147483647U }}
150 if (l
== longMin
) { // outside of range, no changes
151 clang_analyzer_dump(c
+ 0); // expected-warning-re {{reg_${{[0-9]+}}<unsigned int u> }}
152 clang_analyzer_dump(d
+ 0); // expected-warning-re {{reg_${{[0-9]+}}<unsigned int u> }}
158 typedef long ssize_t
;
159 ssize_t
write(int, const void *, unsigned long);
161 int crashTest(int x
, int fd
) {
162 unsigned wres
= write(fd
, "a", 1);
168 return x
+ t1
; // no crash