xref: /llvm-project/clang/test/Preprocessor/pragma_microsoft.c (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_cc1 -triple i686-unknown-windows-msvc %s -fsyntax-only -verify -fms-extensions -Wunknown-pragmas
2 // RUN: not %clang_cc1 -triple i686-unknown-windows-msvc %s -fms-extensions -E | FileCheck %s
3 
4 #define FOO 1
5 #define BAR "2"
6 
7 #pragma comment(linker,"foo=" FOO) // expected-error {{pragma comment requires parenthesized identifier and optional string}}
8 // CHECK: #pragma comment(linker,"foo=" 1)
9 #pragma comment(linker," bar=" BAR)
10 // CHECK: #pragma comment(linker," bar=" "2")
11 
12 #pragma comment( user, "Compiled on " __DATE__ " at " __TIME__ )
13 // CHECK: {{#pragma comment\( user, \"Compiled on \".*\" at \".*\" \)}}
14 
15 #pragma comment(foo)    // expected-error {{unknown kind of pragma comment}}
16 // CHECK: #pragma comment(foo)
17 #pragma comment(compiler,)     // expected-error {{expected string literal in pragma comment}}
18 // CHECK: #pragma comment(compiler,)
19 #define foo compiler
20 #pragma comment(foo)   // macro expand kind.
21 // CHECK: #pragma comment(compiler)
22 #pragma comment(foo) x // expected-error {{pragma comment requires}}
23 // CHECK: #pragma comment(compiler) x
24 
25 #pragma comment(user, "foo\abar\nbaz\tsome	thing")
26 // CHECK: #pragma comment(user, "foo\abar\nbaz\tsome	thing")
27 
28 #pragma detect_mismatch("test", "1")
29 // CHECK: #pragma detect_mismatch("test", "1")
30 #pragma detect_mismatch()  // expected-error {{expected string literal in pragma detect_mismatch}}
31 // CHECK: #pragma detect_mismatch()
32 #pragma detect_mismatch("test") // expected-error {{pragma detect_mismatch is malformed; it requires two comma-separated string literals}}
33 // CHECK: #pragma detect_mismatch("test")
34 #pragma detect_mismatch("test", 1) // expected-error {{expected string literal in pragma detect_mismatch}}
35 // CHECK: #pragma detect_mismatch("test", 1)
36 #pragma detect_mismatch("test", BAR)
37 // CHECK: #pragma detect_mismatch("test", "2")
38 
39 // __pragma
40 
41 __pragma(comment(linker," bar=" BAR))
42 // CHECK: #pragma comment(linker," bar=" "2")
43 
44 #define MACRO_WITH__PRAGMA { \
45   __pragma(warning(push)); \
46   __pragma(warning(disable: 10000)); \
47   1 + (2 > 3) ? 4 : 5; \
48   __pragma(warning(pop)); \
49 }
50 
51 #define PRAGMA_IN_ARGS(p) p
52 
f(void)53 void f(void)
54 {
55   __pragma() // expected-warning{{unknown pragma ignored}}
56 // CHECK: #pragma
57 
58   // If we ever actually *support* __pragma(warning(disable: x)),
59   // this warning should go away.
60   MACRO_WITH__PRAGMA // expected-warning {{lower precedence}} \
61                      // expected-note 2 {{place parentheses}}
62 // CHECK: #pragma warning(push)
63 // CHECK: #pragma warning(disable: 10000)
64 // CHECK: ; 1 + (2 > 3) ? 4 : 5;
65 // CHECK: #pragma warning(pop)
66 
67   // Check that macro arguments can contain __pragma.
68   PRAGMA_IN_ARGS(MACRO_WITH__PRAGMA) // expected-warning {{lower precedence}} \
69                                      // expected-note 2 {{place parentheses}} \
70                                      // expected-warning {{expression result unused}}
71 // CHECK: #pragma warning(push)
72 // CHECK: #pragma warning(disable: 10000)
73 // CHECK: ; 1 + (2 > 3) ? 4 : 5;
74 // CHECK: #pragma warning(pop)
75 }
76 
77 // This should include macro_arg_directive even though the include
78 // is looking for test.h  This allows us to assign to "n"
79 #pragma include_alias("test.h", "macro_arg_directive.h" )
80 #include "test.h"
test(void)81 void test( void ) {
82   n = 12;
83 }
84 
85 #pragma include_alias(<bar.h>, "bar.h") // expected-warning {{angle-bracketed include <bar.h> cannot be aliased to double-quoted include "bar.h"}}
86 #pragma include_alias("foo.h", <bar.h>) // expected-warning {{double-quoted include "foo.h" cannot be aliased to angle-bracketed include <bar.h>}}
87 #pragma include_alias("test.h") // expected-warning {{pragma include_alias expected ','}}
88 
89 // Make sure that the names match exactly for a replacement, including path information.  If
90 // this were to fail, we would get a file not found error
91 #pragma include_alias(".\pp-record.h", "does_not_exist.h")
92 #include "pp-record.h"
93 
94 #pragma include_alias(12) // expected-warning {{pragma include_alias expected include filename}}
95 
96 // It's expected that we can map "bar" and <bar> separately
97 #define test
98 // We can't actually look up stdio.h because we're using cc1 without header paths, but this will ensure
99 // that we get the right bar.h, because the "bar.h" will undef test for us, where <bar.h> won't
100 #pragma include_alias(<bar.h>, <stdio.h>)
101 #pragma include_alias("bar.h", "pr2086.h")  // This should #undef test
102 
103 #include "bar.h"
104 #if defined(test)
105 // This should not warn because test should not be defined
106 #pragma include_alias("test.h")
107 #endif
108 
109 // Test to make sure there are no use-after-free problems
110 #define B "pp-record.h"
111 #pragma include_alias("quux.h", B)
g(int k)112 void g(int k) {}
113 #include "quux.h"
114 
115 // Make sure that empty includes don't work
116 #pragma include_alias("", "foo.h")  // expected-error {{empty filename}}
117 #pragma include_alias(<foo.h>, <>)  // expected-error {{empty filename}}
118 
119 // Test that we ignore pragma warning.
120 #pragma warning(push)
121 // CHECK: #pragma warning(push)
122 #pragma warning(push, 1)
123 // CHECK: #pragma warning(push, 1)
124 #pragma warning(disable : 4705)
125 // CHECK: #pragma warning(disable: 4705)
126 #pragma warning(disable : 123 456 789 ; error : 321)
127 // CHECK: #pragma warning(disable: 123 456 789)
128 // CHECK: #pragma warning(error: 321)
129 #pragma warning(once : 321)
130 // CHECK: #pragma warning(once: 321)
131 #pragma warning(suppress : 321)
132 // CHECK: #pragma warning(suppress: 321)
133 #pragma warning(default : 321)
134 // CHECK: #pragma warning(default: 321)
135 #pragma warning(pop)
136 // CHECK: #pragma warning(pop)
137 #pragma warning(1: 123)
138 // CHECK: #pragma warning(1: 123)
139 #pragma warning(2: 234 567)
140 // CHECK: #pragma warning(2: 234 567)
141 #pragma warning(3: 123; 4: 678)
142 // CHECK: #pragma warning(3: 123)
143 // CHECK: #pragma warning(4: 678)
144 #pragma warning(5: 123) // expected-warning {{expected 'push', 'pop', 'default', 'disable', 'error', 'once', 'suppress', 1, 2, 3, or 4}}
145 
146 #pragma warning(push, 0)
147 // CHECK: #pragma warning(push, 0)
148 // FIXME: We could probably support pushing warning level 0.
149 #pragma warning(pop)
150 // CHECK: #pragma warning(pop)
151 
152 #pragma warning  // expected-warning {{expected '('}}
153 #pragma warning(   // expected-warning {{expected 'push', 'pop', 'default', 'disable', 'error', 'once', 'suppress', 1, 2, 3, or 4}}
154 #pragma warning()   // expected-warning {{expected 'push', 'pop', 'default', 'disable', 'error', 'once', 'suppress', 1, 2, 3, or 4}}
155 #pragma warning(push 4)  // expected-warning {{expected ')'}}
156 // CHECK: #pragma warning(push)
157 #pragma warning(push  // expected-warning {{expected ')'}}
158 // CHECK: #pragma warning(push)
159 #pragma warning(push, 5)  // expected-warning {{requires a level between 0 and 4}}
160 #pragma warning(pop, 1)  // expected-warning {{expected ')'}}
161 // CHECK: #pragma warning(pop)
162 #pragma warning(push, 1) asdf // expected-warning {{extra tokens at end of #pragma warning directive}}
163 // CHECK: #pragma warning(push, 1)
164 #pragma warning(disable 4705) // expected-warning {{expected ':'}}
165 #pragma warning(disable : 0) // expected-warning {{expected a warning number}}
166 #pragma warning(default 321) // expected-warning {{expected ':'}}
167 #pragma warning(asdf : 321) // expected-warning {{expected 'push', 'pop'}}
168 #pragma warning(push, -1) // expected-warning {{requires a level between 0 and 4}}
169 
170 // Test that runtime_checks is parsed but ignored.
171 #pragma runtime_checks("sc", restore) // no-warning
172 
173 // Test pragma intrinsic
174 #pragma intrinsic(memset) // no-warning
175 #pragma intrinsic(memcpy, strlen, strlen) // no-warning
176 #pragma intrinsic() // no-warning
177 #pragma intrinsic(asdf) // expected-warning {{'asdf' is not a recognized builtin; consider including <intrin.h>}}
178 #pragma intrinsic(main) // expected-warning {{'main' is not a recognized builtin; consider including <intrin.h>}}
179 #pragma intrinsic( // expected-warning {{missing ')' after}}
180 #pragma intrinsic(int) // expected-warning {{missing ')' after}}
181 #pragma intrinsic(strcmp) asdf // expected-warning {{extra tokens at end}}
182 
183 #define __INTRIN_H  // there should be no notes after defining __INTRIN_H
184 #pragma intrinsic(asdf) // expected-warning-re {{'asdf' is not a recognized builtin{{$}}}}
185 #pragma intrinsic(memset) // no-warning
186 #undef __INTRIN_H
187 #pragma intrinsic(asdf) // expected-warning {{'asdf' is not a recognized builtin; consider including <intrin.h>}}
188 
189 #pragma clang diagnostic push
190 #pragma clang diagnostic ignored "-Wignored-pragma-intrinsic"
191 #pragma intrinsic(asdf) // no-warning
192 #pragma clang diagnostic pop
193 #pragma intrinsic(asdf) // expected-warning {{'asdf' is not a recognized builtin; consider including <intrin.h>}}
194 
195 #pragma clang diagnostic push
196 #pragma clang diagnostic ignored "-Wignored-pragmas"
197 #pragma intrinsic(asdf) // no-warning
198 #pragma clang diagnostic pop
199 #pragma intrinsic(asdf) // expected-warning {{'asdf' is not a recognized builtin; consider including <intrin.h>}}
200 
201 // Test pragma function
202 #pragma function(memset)                 // no-warning
203 #pragma function(memcpy, strlen, strlen) // no-warning
204 #pragma function()                       // no-warning
205 #pragma function(asdf)                   // expected-warning {{'asdf' is not a recognized builtin; consider including <intrin.h>}}
206 #pragma function(main)                   // expected-warning {{'main' is not a recognized builtin; consider including <intrin.h>}}
207 #pragma function(                        // expected-warning {{missing ')' after}}
208 #pragma function(int)                    // expected-warning {{missing ')' after}}
209 #pragma function(strcmp) asdf            // expected-warning {{extra tokens at end}}
210 
211 #define __INTRIN_H       // there should be no notes after defining __INTRIN_H
212 #pragma function(asdf)   // expected-warning-re {{'asdf' is not a recognized builtin{{$}}}}
213 #pragma function(memset) // no-warning
214 #undef __INTRIN_H
215 #pragma function(asdf) // expected-warning {{'asdf' is not a recognized builtin; consider including <intrin.h>}}
216 
217 // MSVC accepts this, but we decide to reject it based on the MS docs saying the pragma must appear at the global level
pragma_function_foo()218 void pragma_function_foo() {
219 #pragma function(memset) // expected-error {{'#pragma function' can only appear at file scope}}
220 }
221 
222 #pragma optimize          // expected-warning{{missing '(' after '#pragma optimize'}}
223 #pragma optimize(         // expected-warning{{expected string literal in '#pragma optimize'}}
224 #pragma optimize(a        // expected-warning{{expected string literal in '#pragma optimize'}}
225 #pragma optimize("g"      // expected-warning{{expected ',' in '#pragma optimize'}}
226 #pragma optimize("g",     // expected-warning{{missing argument to '#pragma optimize'; expected 'on' or 'off'}}
227 #pragma optimize("g",xyz  // expected-warning{{unexpected argument 'xyz' to '#pragma optimize'; expected 'on' or 'off'}}
228 #pragma optimize("g",on)  // expected-warning{{unexpected argument 'g' to '#pragma optimize'; expected ""}}
229 #pragma optimize("",on)  // no-warning
230 #pragma optimize("", on) asdf // expected-warning{{extra tokens at end of '#pragma optimize'}}
231 
pragma_optimize_foo()232 void pragma_optimize_foo() {
233 #pragma optimize("", on) // expected-error {{'#pragma optimize' can only appear at file scope}}
234 }
235 
236 #pragma managed            // no-warning
237 #pragma unmanaged          // no-warning
238 #pragma managed(push, on)  // no-warning
239 #pragma managed(pop)       // no-warning
240 #pragma managed2           // expected-warning{{unknown pragma ignored}}
241 
242 #pragma execution_character_set                 // expected-warning {{expected '('}}
243 #pragma execution_character_set(                // expected-warning {{expected 'push' or 'pop'}}
244 #pragma execution_character_set()               // expected-warning {{expected 'push' or 'pop'}}
245 #pragma execution_character_set(asdf            // expected-warning {{expected 'push' or 'pop'}}
246 #pragma execution_character_set(asdf)           // expected-warning {{expected 'push' or 'pop'}}
247 #pragma execution_character_set(push            // expected-warning {{expected ')'}}
248 #pragma execution_character_set(pop,)           // expected-warning {{expected ')'}}
249 #pragma execution_character_set(pop,"asdf")     // expected-warning {{expected ')'}}
250 #pragma execution_character_set(push,           // expected-error {{expected string literal}}
251 #pragma execution_character_set(push,)          // expected-error {{expected string literal}}
252 #pragma execution_character_set(push,asdf)      // expected-error {{expected string literal}}
253 #pragma execution_character_set(push, "asdf")   // expected-warning {{only 'UTF-8' is supported}}
254 
255 #pragma execution_character_set(push)
256 #pragma execution_character_set(push, "utf-8")
257 #pragma execution_character_set(push, "UTF-8")
258 #pragma execution_character_set(pop)
259