xref: /llvm-project/clang/test/Analysis/array-punned-region.c (revision df5801806d03c22099c85942134ca3004776016b)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -analyzer-config eagerly-assume=false -triple x86_64-pc-linux-gnu %s
2 
3 // RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core,debug.ExprInspection -verify -analyzer-config eagerly-assume=false -triple i386-pc-linux-gnu  %s
4 
5 int clang_analyzer_eval(int);
6 
7 typedef struct {
8   int a : 1;
9   int b[2];
10 } BITFIELD_CAST;
11 
array_struct_bitfield_1()12 void array_struct_bitfield_1() {
13   BITFIELD_CAST ff = {0};
14   BITFIELD_CAST *pff = &ff;
15   clang_analyzer_eval(*((int *)pff + 1) == 0); // expected-warning{{TRUE}}
16   ff.b[0] = 3;
17   clang_analyzer_eval(*((int *)pff + 1) == 3); // expected-warning{{TRUE}}
18 }
19 
array_struct_bitfield_2()20 int array_struct_bitfield_2() {
21   BITFIELD_CAST ff = {0};
22   BITFIELD_CAST *pff = &ff;
23   int a = *((int *)pff + 2); // expected-warning{{Assigned value is garbage or undefined [core.uninitialized.Assign]}}
24   return a;
25 }
26 
27 typedef struct {
28   unsigned int a : 1;
29   unsigned int x : 31;
30   unsigned int c : 1;
31   int b[2];
32 } mystruct;
33 
array_struct_bitfield_3()34 void array_struct_bitfield_3() {
35   mystruct ff;
36   mystruct *pff = &ff;
37   ff.b[0] = 3;
38   clang_analyzer_eval(*((int *)pff + 2) == 3); // expected-warning{{TRUE}}
39 }
40