xref: /aosp_15_r20/external/clang/test/CodeGenCXX/debug-info-anon-union-vars.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited -triple x86_64-linux-gnu %s -o - | FileCheck %s
2*67e74705SXin Li 
3*67e74705SXin Li // Make sure that we emit a global variable for each of the members of the
4*67e74705SXin Li // anonymous union.
5*67e74705SXin Li 
6*67e74705SXin Li static union {
7*67e74705SXin Li   int c;
8*67e74705SXin Li   int d;
9*67e74705SXin Li   union {
10*67e74705SXin Li     int a;
11*67e74705SXin Li   };
12*67e74705SXin Li   struct {
13*67e74705SXin Li     int b;
14*67e74705SXin Li   };
15*67e74705SXin Li };
16*67e74705SXin Li 
test_it()17*67e74705SXin Li int test_it() {
18*67e74705SXin Li   c = 1;
19*67e74705SXin Li   d = 2;
20*67e74705SXin Li   a = 4;
21*67e74705SXin Li   return (c == 1);
22*67e74705SXin Li }
23*67e74705SXin Li 
foo()24*67e74705SXin Li void foo() {
25*67e74705SXin Li   union {
26*67e74705SXin Li     int i;
27*67e74705SXin Li     char c;
28*67e74705SXin Li   };
29*67e74705SXin Li   i = 8;
30*67e74705SXin Li }
31*67e74705SXin Li 
32*67e74705SXin Li // A funky reinterpret cast idiom that we used to crash on.
33*67e74705SXin Li template <class T>
buildBytes(const T v)34*67e74705SXin Li unsigned char *buildBytes(const T v) {
35*67e74705SXin Li   static union {
36*67e74705SXin Li     unsigned char result[sizeof(T)];
37*67e74705SXin Li     T value;
38*67e74705SXin Li   };
39*67e74705SXin Li   value = v;
40*67e74705SXin Li   return result;
41*67e74705SXin Li }
42*67e74705SXin Li 
instantiate(int x)43*67e74705SXin Li void instantiate(int x) {
44*67e74705SXin Li   buildBytes(x);
45*67e74705SXin Li }
46*67e74705SXin Li 
47*67e74705SXin Li // CHECK: !DIGlobalVariable(name: "c",{{.*}} file: [[FILE:.*]], line: 6,{{.*}} isLocal: true, isDefinition: true
48*67e74705SXin Li // CHECK: [[FILE]] = !DIFile(filename: "{{.*}}debug-info-anon-union-vars.cpp",
49*67e74705SXin Li // CHECK: !DIGlobalVariable(name: "d",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true
50*67e74705SXin Li // CHECK: !DIGlobalVariable(name: "a",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true
51*67e74705SXin Li // CHECK: !DIGlobalVariable(name: "b",{{.*}} file: [[FILE]], line: 6,{{.*}} isLocal: true, isDefinition: true
52*67e74705SXin Li // CHECK: !DIGlobalVariable(name: "result", {{.*}} isLocal: false, isDefinition: true
53*67e74705SXin Li // CHECK: !DIGlobalVariable(name: "value", {{.*}} isLocal: false, isDefinition: true
54*67e74705SXin Li // CHECK: !DILocalVariable(name: "i", {{.*}}, flags: DIFlagArtificial
55*67e74705SXin Li // CHECK: !DILocalVariable(name: "c", {{.*}}, flags: DIFlagArtificial
56*67e74705SXin Li // CHECK: !DILocalVariable(
57*67e74705SXin Li // CHECK-NOT: name:
58*67e74705SXin Li // CHECK: type: ![[UNION:[0-9]+]]
59*67e74705SXin Li // CHECK: ![[UNION]] = distinct !DICompositeType(tag: DW_TAG_union_type,
60*67e74705SXin Li // CHECK-NOT: name:
61*67e74705SXin Li // CHECK: elements
62*67e74705SXin Li // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "i", scope: ![[UNION]],
63*67e74705SXin Li // CHECK: !DIDerivedType(tag: DW_TAG_member, name: "c", scope: ![[UNION]],
64