xref: /aosp_15_r20/external/clang/test/Analysis/unions-region.m (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li// RUN: %clang_cc1 -analyze -analyzer-checker=core -analyzer-store=region -analyzer-constraints=range %s -verify
2*67e74705SXin Li// expected-no-diagnostics
3*67e74705SXin Li
4*67e74705SXin Li//===-- unions-region.m ---------------------------------------------------===//
5*67e74705SXin Li//
6*67e74705SXin Li// This file tests the analyzer's reasoning about unions.
7*67e74705SXin Li//
8*67e74705SXin Li//===----------------------------------------------------------------------===//
9*67e74705SXin Li
10*67e74705SXin Li// [testA] When using RegionStore, this test case previously had a
11*67e74705SXin Li// false positive of a 'pass-by-value argument is uninitialized'
12*67e74705SXin Li// warning at the call to 'testA_aux' and 'testA_aux_2'.
13*67e74705SXin Liunion u_testA {
14*67e74705SXin Li  unsigned i;
15*67e74705SXin Li  float f;
16*67e74705SXin Li};
17*67e74705SXin Li
18*67e74705SXin Lifloat testA(float f) {
19*67e74705SXin Li  int testA_aux(unsigned x);
20*67e74705SXin Li  int testA_aux_2(union u_testA z);
21*67e74705SXin Li
22*67e74705SXin Li  union u_testA swap;
23*67e74705SXin Li  swap.f = f;
24*67e74705SXin Li
25*67e74705SXin Li  if (testA_aux(swap.i))  // no-warning
26*67e74705SXin Li    swap.i = ((swap.i & 0xffff0000) >> 16) | ((swap.i & 0x0000fffff) << 16);
27*67e74705SXin Li
28*67e74705SXin Li  testA_aux_2(swap); // no-warning
29*67e74705SXin Li
30*67e74705SXin Li  return swap.f;
31*67e74705SXin Li}
32*67e74705SXin Li
33*67e74705SXin Li// [testB] When using RegionStore, this test case previously had a
34*67e74705SXin Li// false positive of a 'pass-by-value argument is uninitialized'
35*67e74705SXin Li// warning at the call to 'testB_aux'.
36*67e74705SXin Livoid testB(int i) {
37*67e74705SXin Li  void testB_aux(short z);
38*67e74705SXin Li  union { short x[2]; unsigned y; } val;
39*67e74705SXin Li  val.y = 10;
40*67e74705SXin Li  testB_aux(val.x[1]); // no-warning
41*67e74705SXin Li}
42*67e74705SXin Li
43