1 // RUN: %clang_cc1 -emit-llvm %s -o - -DDEFINE_GUID -triple=i386-pc-linux -fms-extensions | FileCheck %s --check-prefix=CHECK-DEFINE-GUID
2 // RUN: %clang_cc1 -emit-llvm %s -o - -DDEFINE_GUID -DBRACKET_ATTRIB -triple=i386-pc-linux -fms-extensions | FileCheck %s --check-prefix=CHECK-DEFINE-GUID
3 // RUN: %clang_cc1 -emit-llvm %s -o - -triple=i386-pc-linux -fms-extensions | FileCheck %s
4 // RUN: %clang_cc1 -emit-llvm %s -o - -triple=x86_64-pc-linux -fms-extensions | FileCheck %s --check-prefix=CHECK-64
5 // RUN: %clang_cc1 -emit-llvm %s -o - -DDEFINE_GUID -DWRONG_GUID -triple=i386-pc-linux -fms-extensions | FileCheck %s --check-prefix=CHECK-DEFINE-WRONG-GUID
6 
7 #ifdef DEFINE_GUID
8 struct _GUID {
9 #ifdef WRONG_GUID
10     unsigned int SomethingWentWrong;
11 #else
12     unsigned long  Data1;
13     unsigned short Data2;
14     unsigned short Data3;
15     unsigned char  Data4[8];
16 #endif
17 };
18 #endif
19 typedef struct _GUID GUID;
20 
21 #ifdef BRACKET_ATTRIB
22 [uuid(12345678-1234-1234-1234-1234567890aB)] struct S1 { } s1;
23 [uuid(87654321-4321-4321-4321-ba0987654321)] struct S2 { };
24 [uuid({12345678-1234-1234-1234-1234567890ac})] struct Curly;
25 [uuid({12345678-1234-1234-1234-1234567890ac})] struct Curly;
26 #else
27 struct __declspec(uuid("12345678-1234-1234-1234-1234567890aB")) S1 { } s1;
28 struct __declspec(uuid("87654321-4321-4321-4321-ba0987654321")) S2 { };
29 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890ac}")) Curly;
30 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890ac}")) Curly;
31 #endif
32 
33 #ifdef DEFINE_GUID
34 // Make sure we can properly generate code when the UUID has curly braces on it.
35 GUID thing = __uuidof(Curly);
36 // CHECK-DEFINE-GUID: @thing = global %struct._GUID zeroinitializer, align 4
37 // CHECK-DEFINE-WRONG-GUID: @thing = global %struct._GUID zeroinitializer, align 4
38 
39 // This gets initialized in a static initializer.
40 // CHECK-DEFINE-GUID: @g = global %struct._GUID zeroinitializer, align 4
41 // CHECK-DEFINE-WRONG-GUID: @g = global %struct._GUID zeroinitializer, align 4
42 GUID g = __uuidof(S1);
43 #endif
44 
45 // First global use of __uuidof(S1) forces the creation of the global.
46 // CHECK: @_GUID_12345678_1234_1234_1234_1234567890ab = linkonce_odr constant { i32, i16, i16, [8 x i8] } { i32 305419896, i16 4660, i16 4660, [8 x i8] c"\124\124Vx\90\AB" }, comdat
47 // CHECK: @gr = constant %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to %struct._GUID*), align 4
48 // CHECK-64: @gr = constant %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to %struct._GUID*), align 8
49 const GUID& gr = __uuidof(S1);
50 
51 // CHECK: @gp = global %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to %struct._GUID*), align 4
52 const GUID* gp = &__uuidof(S1);
53 
54 // CHECK: @cp = global %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ac to %struct._GUID*), align 4
55 const GUID* cp = &__uuidof(Curly);
56 
57 // Special case: _uuidof(0)
58 // CHECK: @zeroiid = constant %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_00000000_0000_0000_0000_000000000000 to %struct._GUID*), align 4
59 const GUID& zeroiid = __uuidof(0);
60 
61 // __uuidof(S2) hasn't been used globally yet, so it's emitted when it's used
62 // in a function and is emitted at the end of the globals section.
63 // CHECK: @_GUID_87654321_4321_4321_4321_ba0987654321 = linkonce_odr constant { i32, i16, i16, [8 x i8] } { i32 -2023406815, i16 17185, i16 17185, [8 x i8] c"C!\BA\09\87eC!" }, comdat
64 
65 // The static initializer for thing.
66 // CHECK-DEFINE-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 bitcast (%struct._GUID* @thing to i8*), i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ac to i8*), i32 16, i1 false)
67 // CHECK-DEFINE-WRONG-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 bitcast (%struct._GUID* @thing to i8*), i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ac to i8*), i32 4, i1 false)
68 
69 // The static initializer for g.
70 // CHECK-DEFINE-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 bitcast (%struct._GUID* @g to i8*), i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 16, i1 false)
71 // CHECK-DEFINE-WRONG-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 bitcast (%struct._GUID* @g to i8*), i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 4, i1 false)
72 
73 #ifdef DEFINE_GUID
fun()74 void fun() {
75   // CHECK-DEFINE-GUID: %s1_1 = alloca %struct._GUID, align 4
76   // CHECK-DEFINE-WRONG-GUID: %s1_1 = alloca %struct._GUID, align 4
77   // CHECK-DEFINE-GUID: %s1_2 = alloca %struct._GUID, align 4
78   // CHECK-DEFINE-WRONG-GUID: %s1_2 = alloca %struct._GUID, align 4
79   // CHECK-DEFINE-GUID: %s1_3 = alloca %struct._GUID, align 4
80   // CHECK-DEFINE-WRONG-GUID: %s1_3 = alloca %struct._GUID, align 4
81 
82   // CHECK-DEFINE-GUID: [[U1:%.+]] = bitcast %struct._GUID* %s1_1 to i8*
83   // CHECK-DEFINE-WRONG-GUID: [[U1:%.+]] = bitcast %struct._GUID* %s1_1 to i8*
84   // CHECK-DEFINE-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[U1]], i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 16, i1 false)
85   // CHECK-DEFINE-WRONG-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[U1]], i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 4, i1 false)
86   GUID s1_1 = __uuidof(S1);
87 
88   // CHECK-DEFINE-GUID: [[U2:%.+]] = bitcast %struct._GUID* %s1_2 to i8*
89   // CHECK-DEFINE-WRONG-GUID: [[U2:%.+]] = bitcast %struct._GUID* %s1_2 to i8*
90   // CHECK-DEFINE-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[U2]], i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 16, i1 false)
91   // CHECK-DEFINE-WRONG-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[U2]], i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 4, i1 false)
92   GUID s1_2 = __uuidof(S1);
93 
94   // CHECK-DEFINE-GUID: [[U3:%.+]] = bitcast %struct._GUID* %s1_3 to i8*
95   // CHECK-DEFINE-WRONG-GUID: [[U3:%.+]] = bitcast %struct._GUID* %s1_3 to i8*
96   // CHECK-DEFINE-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[U3]], i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 16, i1 false)
97   // CHECK-DEFINE-WRONG-GUID: call void @llvm.memcpy.p0i8.p0i8.i32(i8* align 4 [[U3]], i8* align 4 bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_12345678_1234_1234_1234_1234567890ab to i8*), i32 4, i1 false)
98   GUID s1_3 = __uuidof(s1);
99 }
100 #endif
101 
gun()102 void gun() {
103 #ifdef DEFINE_GUID
104   // CHECK-DEFINE-GUID: %s2_1 = alloca %struct._GUID, align 4
105   // CHECK-DEFINE-WRONG-GUID: %s2_1 = alloca %struct._GUID, align 4
106   // CHECK-DEFINE-GUID: %s2_2 = alloca %struct._GUID, align 4
107   // CHECK-DEFINE-WRONG-GUID: %s2_2 = alloca %struct._GUID, align 4
108   GUID s2_1 = __uuidof(S2);
109   GUID s2_2 = __uuidof(S2);
110 #endif
111   // CHECK: %r = alloca %struct._GUID*, align 4
112   // CHECK: %p = alloca %struct._GUID*, align 4
113   // CHECK: %zeroiid = alloca %struct._GUID*, align 4
114 
115   // CHECK: store %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_87654321_4321_4321_4321_ba0987654321 to %struct._GUID*), %struct._GUID** %r, align 4
116   const GUID& r = __uuidof(S2);
117   // CHECK: store %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_87654321_4321_4321_4321_ba0987654321 to %struct._GUID*), %struct._GUID** %p, align 4
118   const GUID* p = &__uuidof(S2);
119 
120   // Special case _uuidof(0), local scope version.
121   // CHECK: store %struct._GUID* bitcast ({ i32, i16, i16, [8 x i8] }* @_GUID_00000000_0000_0000_0000_000000000000 to %struct._GUID*), %struct._GUID** %zeroiid, align 4
122   const GUID& zeroiid = __uuidof(0);
123 }
124