1 // RUN: %clang_analyze_cc1 -Wno-unused-value -std=c++14 -verify %s -triple x86_64-pc-linux-gnu \
2 // RUN: -analyzer-checker=core,debug.ExprInspection,alpha.core.PointerArithm
4 // RUN: %clang_analyze_cc1 -Wno-unused-value -std=c++14 -verify %s -triple x86_64-pc-linux-gnu \
5 // RUN: -analyzer-config support-symbolic-integer-casts=true \
6 // RUN: -analyzer-checker=core,debug.ExprInspection,alpha.core.PointerArithm
8 template <typename T
> void clang_analyzer_dump(T
);
17 while (in
!= p
) // Loop must be entered.
27 return 5/littleX
.zero
; // no-warning
32 class Derived
: public Base
{};
34 void checkPolymorphicUse() {
38 ++p
; // expected-warning{{Pointer arithmetic on a pointer to base class is dangerous}}
41 void checkBitCasts() {
47 void checkBasicarithmetic(int i
) {
53 ++p
; // expected-warning{{Pointer arithmetic on non-array variables relies on memory layout, which is dangerous}}
54 p
= p
+ 2; // expected-warning{{}}
55 p
= 2 + p
; // expected-warning{{}}
56 p
+= 2; // expected-warning{{}}
57 a
+= p
[2]; // expected-warning{{}}
63 void checkArithOnSymbolic(int*p
) {
75 void arrayInStruct() {
87 p
[1] = 1; // expected-warning{{}}
90 void InitState(int* state
) {
91 state
[1] = 1; // expected-warning{{}}
94 int* getArray(int size
) {
100 void checkConditionalArray() {
101 int* maybeArray
= getArray(0);
102 InitState(maybeArray
);
105 void checkMultiDimansionalArray() {
110 unsigned ptrSubtractionNoCrash(char *Begin
, char *End
) {
111 auto N
= End
- Begin
;
118 bool ptrAsIntegerSubtractionNoCrash(__UINTPTR_TYPE__ x
, char *p
) {
119 __UINTPTR_TYPE__ y
= (__UINTPTR_TYPE__
)p
- 1;
124 bool integerAsPtrSubtractionNoCrash(char *p
, __UINTPTR_TYPE__ m
) {
125 auto n
= p
- reinterpret_cast<char*>((__UINTPTR_TYPE__
)1);
129 namespace Bug_55934
{
136 unsigned bits2
: 2; // <-- header
139 int parse(parse_t
*p
) {
140 unsigned copy
= p
->bits2
;
141 clang_analyzer_dump(copy
);
142 // expected-warning@-1 {{reg_$1<unsigned int Element{SymRegion{reg_$0<parse_t * p>},0 S64b,struct Bug_55934::parse_t}.bits2>}}
143 header
*bits
= (header
*)©
;
144 clang_analyzer_dump(bits
->b
);
145 // expected-warning@-1 {{derived_$2{reg_$1<unsigned int Element{SymRegion{reg_$0<parse_t * p>},0 S64b,struct Bug_55934::parse_t}.bits2>,Element{copy,0 S64b,struct Bug_55934::header}.b}}}
146 return bits
->b
; // no-warning
148 } // namespace Bug_55934
150 void LValueToRValueBitCast_dumps(void *p
, char (*array
)[8]) {
151 clang_analyzer_dump(p
);
152 clang_analyzer_dump(array
);
153 // expected-warning@-2 {{&SymRegion{reg_$0<void * p>}}}
154 // expected-warning@-2 {{&SymRegion{reg_$1<char (*)[8] array>}}}
155 clang_analyzer_dump((unsigned long)p
);
156 clang_analyzer_dump(__builtin_bit_cast(unsigned long, p
));
157 // expected-warning@-2 {{&SymRegion{reg_$0<void * p>} [as 64 bit integer]}}
158 // expected-warning@-2 {{&SymRegion{reg_$0<void * p>} [as 64 bit integer]}}
159 clang_analyzer_dump((unsigned long)array
);
160 clang_analyzer_dump(__builtin_bit_cast(unsigned long, array
));
161 // expected-warning@-2 {{&SymRegion{reg_$1<char (*)[8] array>} [as 64 bit integer]}}
162 // expected-warning@-2 {{&SymRegion{reg_$1<char (*)[8] array>} [as 64 bit integer]}}
165 unsigned long ptr_arithmetic(void *p
) {
166 return __builtin_bit_cast(unsigned long, p
) + 1; // no-crash