Debian Codegraph @

clang/test/Sema/dllexport.c

raw
1 // RUN: %clang_cc1 -triple i686-win32 -fsyntax-only -fms-extensions -verify -std=c99 %s
2 // RUN: %clang_cc1 -triple x86_64-win32 -fsyntax-only -fms-extensions -verify -std=c11 %s
3 // RUN: %clang_cc1 -triple i686-mingw32 -fsyntax-only -fms-extensions -verify -std=c11 %s
4 // RUN: %clang_cc1 -triple x86_64-mingw32 -fsyntax-only -fms-extensions -verify -std=c99 %s
5 // RUN: %clang_cc1 -triple i686-windows-itanium -fsyntax-only -fms-extensions -verify -std=c99 %s
6 // RUN: %clang_cc1 -triple x86_64-windows-itanium -fsyntax-only -fms-extensions -verify -std=c11 %s
7 // RUN: %clang_cc1 -triple x86_64-sie-ps5 -fsyntax-only -fms-extensions -verify -std=c99 %s
8 // RUN: %clang_cc1 -triple x86_64-sie-ps5 -fsyntax-only -fms-extensions -verify -std=c11 %s
9
10 // Invalid usage.
11 __declspec(dllexport) typedef int typedef1;
12 // expected-warning@-1{{'dllexport' attribute only applies to functions, variables, classes, and Objective-C interfaces}}
13 typedef __declspec(dllexport) int typedef2;
14 // expected-warning@-1{{'dllexport' attribute only applies to}}
15 typedef int __declspec(dllexport) typedef3;
16 // expected-warning@-1{{'dllexport' attribute only applies to}}
17 typedef __declspec(dllexport) void (*FunTy)(void);
18 // expected-warning@-1{{'dllexport' attribute only applies to}}
19 enum __declspec(dllexport) Enum { EnumVal };
20 // expected-warning@-1{{'dllexport' attribute only applies to}}
21 struct __declspec(dllexport) Record {};
22 // expected-warning@-1{{'dllexport' attribute only applies to}}
23
24
25
26 //===----------------------------------------------------------------------===//
27 // Globals
28 //===----------------------------------------------------------------------===//
29
30 // Export declaration.
31 __declspec(dllexport) extern int ExternGlobalDecl;
32
33 // dllexport implies a definition.
34 __declspec(dllexport) int GlobalDef;
35
36 // Export definition.
37 __declspec(dllexport) int GlobalInit1 = 1;
38 int __declspec(dllexport) GlobalInit2 = 1;
39
40 // Declare, then export definition.
41 __declspec(dllexport) extern int GlobalDeclInit;
42 int GlobalDeclInit = 1;
43
44 // Redeclarations
45 __declspec(dllexport) extern int GlobalRedecl1;
46 __declspec(dllexport) int GlobalRedecl1;
47
48 __declspec(dllexport) extern int GlobalRedecl2;
49 int GlobalRedecl2;
50
51 extern int GlobalRedecl3; // expected-note{{previous declaration is here}}
52 int useGlobalRedecl3(void) { return GlobalRedecl3; }
53 __declspec(dllexport) extern int GlobalRedecl3; // expected-error{{redeclaration of 'GlobalRedecl3' cannot add 'dllexport' attribute}}
54
55 extern int GlobalRedecl4; // expected-note{{previous declaration is here}}
56 __declspec(dllexport) extern int GlobalRedecl4; // expected-warning{{redeclaration of 'GlobalRedecl4' should not add 'dllexport' attribute}}
57
58
59 // External linkage is required.
60 __declspec(dllexport) static int StaticGlobal; // expected-error{{'StaticGlobal' must have external linkage when declared 'dllexport'}}
61
62 // Thread local variables are invalid.
63 __declspec(dllexport) __thread int ThreadLocalGlobal; // expected-error{{'ThreadLocalGlobal' cannot be thread local when declared 'dllexport'}}
64
65 // Export in local scope.
66 void functionScope(void) {
67 __declspec(dllexport) int LocalVarDecl; // expected-error{{'LocalVarDecl' must have external linkage when declared 'dllexport'}}
68 __declspec(dllexport) int LocalVarDef = 1; // expected-error{{'LocalVarDef' must have external linkage when declared 'dllexport'}}
69 __declspec(dllexport) extern int ExternLocalVarDecl;
70 __declspec(dllexport) static int StaticLocalVar; // expected-error{{'StaticLocalVar' must have external linkage when declared 'dllexport'}}
71 }
72
73
74
75 //===----------------------------------------------------------------------===//
76 // Functions
77 //===----------------------------------------------------------------------===//
78
79 // Export function declaration. Check different placements.
80 __attribute__((dllexport)) void decl1A(void); // Basic check with __attribute__
81 __declspec(dllexport) void decl1B(void);
82
83 void __attribute__((dllexport)) decl2A(void);
84 void __declspec(dllexport) decl2B(void);
85
86 // Export function definition.
87 __declspec(dllexport) void def(void) {}
88
89 // Export inline function.
90 __declspec(dllexport) inline void inlineFunc1(void) {}
91 extern void inlineFunc1(void);
92
93 inline void __attribute__((dllexport)) inlineFunc2(void) {}
94 extern void inlineFunc2(void);
95
96 // Redeclarations
97 __declspec(dllexport) void redecl1(void);
98 __declspec(dllexport) void redecl1(void);
99
100 __declspec(dllexport) void redecl2(void);
101 void redecl2(void);
102
103 __declspec(dllexport) void redecl3(void);
104 void redecl3(void) {}
105
106 void redecl4(void); // expected-note{{previous declaration is here}}
107 void useRedecl4(void) { redecl4(); }
108 __declspec(dllexport) void redecl4(void); // expected-error{{redeclaration of 'redecl4' cannot add 'dllexport' attribute}}
109
110 void redecl5(void); // expected-note{{previous declaration is here}}
111 void useRedecl5(void) { redecl5(); }
112 __declspec(dllexport) inline void redecl5(void) {} // expected-error{{redeclaration of 'redecl5' cannot add 'dllexport' attribute}}
113
114 // Allow with a warning if the decl hasn't been used yet.
115 void redecl6(void); // expected-note{{previous declaration is here}}
116 __declspec(dllexport) void redecl6(void); // expected-warning{{redeclaration of 'redecl6' should not add 'dllexport' attribute}}
117
118
119 // External linkage is required.
120 __declspec(dllexport) static int staticFunc(void); // expected-error{{'staticFunc' must have external linkage when declared 'dllexport'}}
121
122 // Static locals don't count as having external linkage.
123 void staticLocalFunc(void) {
124 __declspec(dllexport) static int staticLocal; // expected-error{{'staticLocal' must have external linkage when declared 'dllexport'}}
125 }
126
127
128
129 //===----------------------------------------------------------------------===//
130 // Precedence
131 //===----------------------------------------------------------------------===//
132
133 // dllexport takes precedence over dllimport if both are specified.
134 __attribute__((dllimport, dllexport)) extern int PrecedenceExternGlobal1A; // expected-warning{{'dllimport' attribute ignored}}
135 __declspec(dllimport) __declspec(dllexport) extern int PrecedenceExternGlobal1B; // expected-warning{{'dllimport' attribute ignored}}
136
137 __attribute__((dllexport, dllimport)) extern int PrecedenceExternGlobal2A; // expected-warning{{'dllimport' attribute ignored}}
138 __declspec(dllexport) __declspec(dllimport) extern int PrecedenceExternGlobal2B; // expected-warning{{'dllimport' attribute ignored}}
139
140 __attribute__((dllimport, dllexport)) int PrecedenceGlobal1A; // expected-warning{{'dllimport' attribute ignored}}
141 __declspec(dllimport) __declspec(dllexport) int PrecedenceGlobal1B; // expected-warning{{'dllimport' attribute ignored}}
142
143 __attribute__((dllexport, dllimport)) int PrecedenceGlobal2A; // expected-warning{{'dllimport' attribute ignored}}
144 __declspec(dllexport) __declspec(dllimport) int PrecedenceGlobal2B; // expected-warning{{'dllimport' attribute ignored}}
145
146 __declspec(dllexport) extern int PrecedenceExternGlobalRedecl1;
147 __declspec(dllimport) extern int PrecedenceExternGlobalRedecl1; // expected-warning{{'dllimport' attribute ignored}}
148
149 __declspec(dllimport) extern int PrecedenceExternGlobalRedecl2; // expected-warning{{'dllimport' attribute ignored}}
150 __declspec(dllexport) extern int PrecedenceExternGlobalRedecl2;
151
152 __declspec(dllexport) extern int PrecedenceGlobalRedecl1;
153 __declspec(dllimport) int PrecedenceGlobalRedecl1; // expected-warning{{'dllimport' attribute ignored}}
154
155 __declspec(dllimport) extern int PrecedenceGlobalRedecl2; // expected-warning{{'dllimport' attribute ignored}}
156 __declspec(dllexport) int PrecedenceGlobalRedecl2;
157
158 void __attribute__((dllimport, dllexport)) precedence1A(void) {} // expected-warning{{'dllimport' attribute ignored}}
159 void __declspec(dllimport) __declspec(dllexport) precedence1B(void) {} // expected-warning{{'dllimport' attribute ignored}}
160
161 void __attribute__((dllexport, dllimport)) precedence2A(void) {} // expected-warning{{'dllimport' attribute ignored}}
162 void __declspec(dllexport) __declspec(dllimport) precedence2B(void) {} // expected-warning{{'dllimport' attribute ignored}}
163
164 void __declspec(dllimport) precedenceRedecl1(void); // expected-warning{{'dllimport' attribute ignored}}
165 void __declspec(dllexport) precedenceRedecl1(void) {}
166
167 void __declspec(dllexport) precedenceRedecl2(void);
168 void __declspec(dllimport) precedenceRedecl2(void) {} // expected-warning{{'dllimport' attribute ignored}}