blob: d090c6edeaa3a28160d72c0769b934671b23afeb [file] [log] [blame]
Daniel Dunbar921b9682008-10-04 19:21:03 +00001//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the language specific #pragma handlers.
11//
12//===----------------------------------------------------------------------===//
13
Hans Wennborg899ded92014-10-16 20:52:46 +000014#include "clang/AST/ASTContext.h"
Nico Weber66220292016-03-02 17:28:48 +000015#include "clang/Basic/PragmaKinds.h"
David Majnemerad2986e2014-08-14 06:35:08 +000016#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/ParseDiagnostic.h"
19#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000020#include "clang/Parse/RAIIObjectsForParser.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000021#include "clang/Sema/LoopHint.h"
22#include "clang/Sema/Scope.h"
23#include "llvm/ADT/StringSwitch.h"
24using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000025
Reid Kleckner5b086462014-02-20 22:52:09 +000026namespace {
27
28struct PragmaAlignHandler : public PragmaHandler {
29 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Craig Topper2b07f022014-03-12 05:09:18 +000030 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
31 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000032};
33
34struct PragmaGCCVisibilityHandler : public PragmaHandler {
35 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000036 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
37 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000038};
39
40struct PragmaOptionsHandler : public PragmaHandler {
41 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000042 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
43 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000044};
45
46struct PragmaPackHandler : public PragmaHandler {
47 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000048 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
49 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000050};
51
Javed Absar2a67c9e2017-06-05 10:11:57 +000052struct PragmaClangSectionHandler : public PragmaHandler {
53 explicit PragmaClangSectionHandler(Sema &S)
54 : PragmaHandler("section"), Actions(S) {}
55 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
56 Token &FirstToken) override;
57private:
58 Sema &Actions;
59};
60
Reid Kleckner5b086462014-02-20 22:52:09 +000061struct PragmaMSStructHandler : public PragmaHandler {
62 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000063 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
64 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000065};
66
67struct PragmaUnusedHandler : public PragmaHandler {
68 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000069 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
70 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000071};
72
73struct PragmaWeakHandler : public PragmaHandler {
74 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000075 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
76 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000077};
78
79struct PragmaRedefineExtnameHandler : public PragmaHandler {
80 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000081 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
82 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000083};
84
85struct PragmaOpenCLExtensionHandler : public PragmaHandler {
86 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000087 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
88 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000089};
90
91
92struct PragmaFPContractHandler : public PragmaHandler {
93 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000094 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
95 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000096};
97
Steven Wub96a3a42018-01-05 22:45:03 +000098// Pragma STDC implementations.
99
100/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
101struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
102 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
103
104 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
105 Token &Tok) override {
106 tok::OnOffSwitch OOS;
107 if (PP.LexOnOffSwitch(OOS))
108 return;
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000109 if (OOS == tok::OOS_ON) {
Steven Wub96a3a42018-01-05 22:45:03 +0000110 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000111 }
112
113 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
114 1);
115 Toks[0].startToken();
116 Toks[0].setKind(tok::annot_pragma_fenv_access);
117 Toks[0].setLocation(Tok.getLocation());
118 Toks[0].setAnnotationEndLoc(Tok.getLocation());
119 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
120 static_cast<uintptr_t>(OOS)));
121 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Steven Wub96a3a42018-01-05 22:45:03 +0000122 }
123};
124
125/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
126struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
127 PragmaSTDC_CX_LIMITED_RANGEHandler() : PragmaHandler("CX_LIMITED_RANGE") {}
128
129 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
130 Token &Tok) override {
131 tok::OnOffSwitch OOS;
132 PP.LexOnOffSwitch(OOS);
133 }
134};
135
136/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
137struct PragmaSTDC_UnknownHandler : public PragmaHandler {
138 PragmaSTDC_UnknownHandler() = default;
139
140 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
141 Token &UnknownTok) override {
142 // C99 6.10.6p2, unknown forms are not allowed.
143 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
144 }
145};
146
Adam Nemet60d32642017-04-04 21:18:36 +0000147struct PragmaFPHandler : public PragmaHandler {
148 PragmaFPHandler() : PragmaHandler("fp") {}
149 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
150 Token &FirstToken) override;
151};
152
Reid Kleckner5b086462014-02-20 22:52:09 +0000153struct PragmaNoOpenMPHandler : public PragmaHandler {
154 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +0000155 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
156 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000157};
158
159struct PragmaOpenMPHandler : public PragmaHandler {
160 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +0000161 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
162 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000163};
164
165/// PragmaCommentHandler - "\#pragma comment ...".
166struct PragmaCommentHandler : public PragmaHandler {
167 PragmaCommentHandler(Sema &Actions)
168 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000169 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
170 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000171private:
172 Sema &Actions;
173};
174
175struct PragmaDetectMismatchHandler : public PragmaHandler {
176 PragmaDetectMismatchHandler(Sema &Actions)
177 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000178 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
179 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000180private:
181 Sema &Actions;
182};
183
184struct PragmaMSPointersToMembers : public PragmaHandler {
185 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000186 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
187 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000188};
189
190struct PragmaMSVtorDisp : public PragmaHandler {
191 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000192 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
193 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000194};
195
Warren Huntc3b18962014-04-08 22:30:47 +0000196struct PragmaMSPragma : public PragmaHandler {
197 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000198 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
199 Token &FirstToken) override;
200};
201
Dario Domizioli13a0a382014-05-23 12:13:25 +0000202/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
203struct PragmaOptimizeHandler : public PragmaHandler {
204 PragmaOptimizeHandler(Sema &S)
205 : PragmaHandler("optimize"), Actions(S) {}
206 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
207 Token &FirstToken) override;
208private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000209 Sema &Actions;
210};
211
212struct PragmaLoopHintHandler : public PragmaHandler {
213 PragmaLoopHintHandler() : PragmaHandler("loop") {}
214 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
215 Token &FirstToken) override;
216};
217
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000218struct PragmaUnrollHintHandler : public PragmaHandler {
219 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
220 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
221 Token &FirstToken) override;
222};
223
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000224struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
225 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
226};
227
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000228struct PragmaMSIntrinsicHandler : public PragmaHandler {
229 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
230 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
231 Token &FirstToken) override;
232};
233
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000234struct PragmaMSOptimizeHandler : public PragmaHandler {
235 PragmaMSOptimizeHandler() : PragmaHandler("optimize") {}
236 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
237 Token &FirstToken) override;
238};
239
Justin Lebar67a78a62016-10-08 22:15:58 +0000240struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
241 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
242 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
243 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
244 Token &FirstToken) override;
245
246private:
247 Sema &Actions;
248};
249
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000250/// PragmaAttributeHandler - "\#pragma clang attribute ...".
251struct PragmaAttributeHandler : public PragmaHandler {
252 PragmaAttributeHandler(AttributeFactory &AttrFactory)
253 : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {}
254 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
255 Token &FirstToken) override;
256
257 /// A pool of attributes that were parsed in \#pragma clang attribute.
258 ParsedAttributes AttributesForPragmaAttribute;
259};
260
Eli Bendersky06a40422014-06-06 20:31:48 +0000261} // end namespace
262
263void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000264 AlignHandler.reset(new PragmaAlignHandler());
265 PP.AddPragmaHandler(AlignHandler.get());
266
267 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
268 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
269
270 OptionsHandler.reset(new PragmaOptionsHandler());
271 PP.AddPragmaHandler(OptionsHandler.get());
272
273 PackHandler.reset(new PragmaPackHandler());
274 PP.AddPragmaHandler(PackHandler.get());
275
276 MSStructHandler.reset(new PragmaMSStructHandler());
277 PP.AddPragmaHandler(MSStructHandler.get());
278
279 UnusedHandler.reset(new PragmaUnusedHandler());
280 PP.AddPragmaHandler(UnusedHandler.get());
281
282 WeakHandler.reset(new PragmaWeakHandler());
283 PP.AddPragmaHandler(WeakHandler.get());
284
285 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
286 PP.AddPragmaHandler(RedefineExtnameHandler.get());
287
288 FPContractHandler.reset(new PragmaFPContractHandler());
289 PP.AddPragmaHandler("STDC", FPContractHandler.get());
290
Steven Wub96a3a42018-01-05 22:45:03 +0000291 STDCFENVHandler.reset(new PragmaSTDC_FENV_ACCESSHandler());
292 PP.AddPragmaHandler("STDC", STDCFENVHandler.get());
293
294 STDCCXLIMITHandler.reset(new PragmaSTDC_CX_LIMITED_RANGEHandler());
295 PP.AddPragmaHandler("STDC", STDCCXLIMITHandler.get());
296
297 STDCUnknownHandler.reset(new PragmaSTDC_UnknownHandler());
298 PP.AddPragmaHandler("STDC", STDCUnknownHandler.get());
299
Javed Absar2a67c9e2017-06-05 10:11:57 +0000300 PCSectionHandler.reset(new PragmaClangSectionHandler(Actions));
301 PP.AddPragmaHandler("clang", PCSectionHandler.get());
302
Reid Kleckner5b086462014-02-20 22:52:09 +0000303 if (getLangOpts().OpenCL) {
304 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
305 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
306
307 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
308 }
309 if (getLangOpts().OpenMP)
310 OpenMPHandler.reset(new PragmaOpenMPHandler());
311 else
312 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
313 PP.AddPragmaHandler(OpenMPHandler.get());
314
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000315 if (getLangOpts().MicrosoftExt ||
316 getTargetInfo().getTriple().isOSBinFormatELF()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000317 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
318 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000319 }
320
321 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000322 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
323 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
324 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
325 PP.AddPragmaHandler(MSPointersToMembers.get());
326 MSVtorDisp.reset(new PragmaMSVtorDisp());
327 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000328 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000329 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000330 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
331 PP.AddPragmaHandler(MSDataSeg.get());
332 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
333 PP.AddPragmaHandler(MSBSSSeg.get());
334 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
335 PP.AddPragmaHandler(MSConstSeg.get());
336 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
337 PP.AddPragmaHandler(MSCodeSeg.get());
338 MSSection.reset(new PragmaMSPragma("section"));
339 PP.AddPragmaHandler(MSSection.get());
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000340 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
341 PP.AddPragmaHandler(MSRuntimeChecks.get());
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000342 MSIntrinsic.reset(new PragmaMSIntrinsicHandler());
343 PP.AddPragmaHandler(MSIntrinsic.get());
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000344 MSOptimize.reset(new PragmaMSOptimizeHandler());
345 PP.AddPragmaHandler(MSOptimize.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000346 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000347
Justin Lebar67a78a62016-10-08 22:15:58 +0000348 if (getLangOpts().CUDA) {
349 CUDAForceHostDeviceHandler.reset(
350 new PragmaForceCUDAHostDeviceHandler(Actions));
351 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
352 }
353
Eli Bendersky06a40422014-06-06 20:31:48 +0000354 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
355 PP.AddPragmaHandler("clang", OptimizeHandler.get());
356
357 LoopHintHandler.reset(new PragmaLoopHintHandler());
358 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000359
360 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
361 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000362
363 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
364 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Adam Nemet60d32642017-04-04 21:18:36 +0000365
David Greenc8e39242018-08-01 14:36:12 +0000366 UnrollAndJamHintHandler.reset(new PragmaUnrollHintHandler("unroll_and_jam"));
367 PP.AddPragmaHandler(UnrollAndJamHintHandler.get());
368
369 NoUnrollAndJamHintHandler.reset(
370 new PragmaUnrollHintHandler("nounroll_and_jam"));
371 PP.AddPragmaHandler(NoUnrollAndJamHintHandler.get());
372
Adam Nemet60d32642017-04-04 21:18:36 +0000373 FPHandler.reset(new PragmaFPHandler());
374 PP.AddPragmaHandler("clang", FPHandler.get());
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000375
376 AttributePragmaHandler.reset(new PragmaAttributeHandler(AttrFactory));
377 PP.AddPragmaHandler("clang", AttributePragmaHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000378}
379
380void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000381 // Remove the pragma handlers we installed.
382 PP.RemovePragmaHandler(AlignHandler.get());
383 AlignHandler.reset();
384 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
385 GCCVisibilityHandler.reset();
386 PP.RemovePragmaHandler(OptionsHandler.get());
387 OptionsHandler.reset();
388 PP.RemovePragmaHandler(PackHandler.get());
389 PackHandler.reset();
390 PP.RemovePragmaHandler(MSStructHandler.get());
391 MSStructHandler.reset();
392 PP.RemovePragmaHandler(UnusedHandler.get());
393 UnusedHandler.reset();
394 PP.RemovePragmaHandler(WeakHandler.get());
395 WeakHandler.reset();
396 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
397 RedefineExtnameHandler.reset();
398
399 if (getLangOpts().OpenCL) {
400 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
401 OpenCLExtensionHandler.reset();
402 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
403 }
404 PP.RemovePragmaHandler(OpenMPHandler.get());
405 OpenMPHandler.reset();
406
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000407 if (getLangOpts().MicrosoftExt ||
408 getTargetInfo().getTriple().isOSBinFormatELF()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000409 PP.RemovePragmaHandler(MSCommentHandler.get());
410 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000411 }
412
Javed Absar2a67c9e2017-06-05 10:11:57 +0000413 PP.RemovePragmaHandler("clang", PCSectionHandler.get());
414 PCSectionHandler.reset();
415
Yunzhong Gao99efc032015-03-23 20:41:42 +0000416 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000417 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
418 MSDetectMismatchHandler.reset();
419 PP.RemovePragmaHandler(MSPointersToMembers.get());
420 MSPointersToMembers.reset();
421 PP.RemovePragmaHandler(MSVtorDisp.get());
422 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000423 PP.RemovePragmaHandler(MSInitSeg.get());
424 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000425 PP.RemovePragmaHandler(MSDataSeg.get());
426 MSDataSeg.reset();
427 PP.RemovePragmaHandler(MSBSSSeg.get());
428 MSBSSSeg.reset();
429 PP.RemovePragmaHandler(MSConstSeg.get());
430 MSConstSeg.reset();
431 PP.RemovePragmaHandler(MSCodeSeg.get());
432 MSCodeSeg.reset();
433 PP.RemovePragmaHandler(MSSection.get());
434 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000435 PP.RemovePragmaHandler(MSRuntimeChecks.get());
436 MSRuntimeChecks.reset();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000437 PP.RemovePragmaHandler(MSIntrinsic.get());
438 MSIntrinsic.reset();
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000439 PP.RemovePragmaHandler(MSOptimize.get());
440 MSOptimize.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000441 }
442
Justin Lebar67a78a62016-10-08 22:15:58 +0000443 if (getLangOpts().CUDA) {
444 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
445 CUDAForceHostDeviceHandler.reset();
446 }
447
Reid Kleckner5b086462014-02-20 22:52:09 +0000448 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
449 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000450
Steven Wub96a3a42018-01-05 22:45:03 +0000451 PP.RemovePragmaHandler("STDC", STDCFENVHandler.get());
452 STDCFENVHandler.reset();
453
454 PP.RemovePragmaHandler("STDC", STDCCXLIMITHandler.get());
455 STDCCXLIMITHandler.reset();
456
457 PP.RemovePragmaHandler("STDC", STDCUnknownHandler.get());
458 STDCUnknownHandler.reset();
459
Eli Bendersky06a40422014-06-06 20:31:48 +0000460 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
461 OptimizeHandler.reset();
462
463 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
464 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000465
466 PP.RemovePragmaHandler(UnrollHintHandler.get());
467 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000468
469 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
470 NoUnrollHintHandler.reset();
Adam Nemet60d32642017-04-04 21:18:36 +0000471
David Greenc8e39242018-08-01 14:36:12 +0000472 PP.RemovePragmaHandler(UnrollAndJamHintHandler.get());
473 UnrollAndJamHintHandler.reset();
474
475 PP.RemovePragmaHandler(NoUnrollAndJamHintHandler.get());
476 NoUnrollAndJamHintHandler.reset();
477
Adam Nemet60d32642017-04-04 21:18:36 +0000478 PP.RemovePragmaHandler("clang", FPHandler.get());
479 FPHandler.reset();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000480
481 PP.RemovePragmaHandler("clang", AttributePragmaHandler.get());
482 AttributePragmaHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000483}
484
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000485/// Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000486///
487/// Each annot_pragma_unused is followed by the argument token so e.g.
488/// "#pragma unused(x,y)" becomes:
489/// annot_pragma_unused 'x' annot_pragma_unused 'y'
490void Parser::HandlePragmaUnused() {
491 assert(Tok.is(tok::annot_pragma_unused));
Richard Smithaf3b3252017-05-18 19:21:48 +0000492 SourceLocation UnusedLoc = ConsumeAnnotationToken();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000493 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
494 ConsumeToken(); // The argument token.
495}
Eli Friedman570024a2010-08-05 06:57:20 +0000496
Rafael Espindola273fd772012-01-26 02:02:57 +0000497void Parser::HandlePragmaVisibility() {
498 assert(Tok.is(tok::annot_pragma_vis));
499 const IdentifierInfo *VisType =
500 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
Richard Smithaf3b3252017-05-18 19:21:48 +0000501 SourceLocation VisLoc = ConsumeAnnotationToken();
Rafael Espindola273fd772012-01-26 02:02:57 +0000502 Actions.ActOnPragmaVisibility(VisType, VisLoc);
503}
504
Benjamin Kramere003ca22015-10-28 13:54:16 +0000505namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000506struct PragmaPackInfo {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000507 Sema::PragmaMsStackAction Action;
508 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000509 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000510};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000511} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000512
513void Parser::HandlePragmaPack() {
514 assert(Tok.is(tok::annot_pragma_pack));
515 PragmaPackInfo *Info =
516 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
Alex Lorenz45b40142017-07-28 14:41:21 +0000517 SourceLocation PragmaLoc = Tok.getLocation();
Eli Friedman68be1642012-10-04 02:36:51 +0000518 ExprResult Alignment;
519 if (Info->Alignment.is(tok::numeric_constant)) {
520 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
Alex Lorenz45b40142017-07-28 14:41:21 +0000521 if (Alignment.isInvalid()) {
522 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000523 return;
Alex Lorenz45b40142017-07-28 14:41:21 +0000524 }
Eli Friedman68be1642012-10-04 02:36:51 +0000525 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000526 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
527 Alignment.get());
Alex Lorenz45b40142017-07-28 14:41:21 +0000528 // Consume the token after processing the pragma to enable pragma-specific
529 // #include warnings.
530 ConsumeAnnotationToken();
Eli Friedmanec52f922012-02-23 23:47:16 +0000531}
532
Eli Friedman68be1642012-10-04 02:36:51 +0000533void Parser::HandlePragmaMSStruct() {
534 assert(Tok.is(tok::annot_pragma_msstruct));
Nico Weber779355f2016-03-02 23:22:00 +0000535 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
536 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Eli Friedman68be1642012-10-04 02:36:51 +0000537 Actions.ActOnPragmaMSStruct(Kind);
Richard Smithaf3b3252017-05-18 19:21:48 +0000538 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000539}
540
541void Parser::HandlePragmaAlign() {
542 assert(Tok.is(tok::annot_pragma_align));
543 Sema::PragmaOptionsAlignKind Kind =
544 static_cast<Sema::PragmaOptionsAlignKind>(
545 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Alex Lorenz692821a2018-02-08 21:20:43 +0000546 Actions.ActOnPragmaOptionsAlign(Kind, Tok.getLocation());
547 // Consume the token after processing the pragma to enable pragma-specific
548 // #include warnings.
549 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000550}
551
Richard Smithba3a4f92016-01-12 21:59:26 +0000552void Parser::HandlePragmaDump() {
553 assert(Tok.is(tok::annot_pragma_dump));
554 IdentifierInfo *II =
555 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
556 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
Richard Smithaf3b3252017-05-18 19:21:48 +0000557 ConsumeAnnotationToken();
Richard Smithba3a4f92016-01-12 21:59:26 +0000558}
559
Eli Friedman68be1642012-10-04 02:36:51 +0000560void Parser::HandlePragmaWeak() {
561 assert(Tok.is(tok::annot_pragma_weak));
Richard Smithaf3b3252017-05-18 19:21:48 +0000562 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000563 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
564 Tok.getLocation());
565 ConsumeToken(); // The weak name.
566}
567
568void Parser::HandlePragmaWeakAlias() {
569 assert(Tok.is(tok::annot_pragma_weakalias));
Richard Smithaf3b3252017-05-18 19:21:48 +0000570 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000571 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
572 SourceLocation WeakNameLoc = Tok.getLocation();
573 ConsumeToken();
574 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
575 SourceLocation AliasNameLoc = Tok.getLocation();
576 ConsumeToken();
577 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
578 WeakNameLoc, AliasNameLoc);
579
580}
581
582void Parser::HandlePragmaRedefineExtname() {
583 assert(Tok.is(tok::annot_pragma_redefine_extname));
Richard Smithaf3b3252017-05-18 19:21:48 +0000584 SourceLocation RedefLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000585 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
586 SourceLocation RedefNameLoc = Tok.getLocation();
587 ConsumeToken();
588 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
589 SourceLocation AliasNameLoc = Tok.getLocation();
590 ConsumeToken();
591 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
592 RedefNameLoc, AliasNameLoc);
593}
594
595void Parser::HandlePragmaFPContract() {
596 assert(Tok.is(tok::annot_pragma_fp_contract));
597 tok::OnOffSwitch OOS =
598 static_cast<tok::OnOffSwitch>(
599 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Adam Nemet60d32642017-04-04 21:18:36 +0000600
601 LangOptions::FPContractModeKind FPC;
602 switch (OOS) {
603 case tok::OOS_ON:
604 FPC = LangOptions::FPC_On;
605 break;
606 case tok::OOS_OFF:
607 FPC = LangOptions::FPC_Off;
608 break;
609 case tok::OOS_DEFAULT:
610 FPC = getLangOpts().getDefaultFPContractMode();
611 break;
612 }
613
614 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +0000615 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000616}
617
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000618void Parser::HandlePragmaFEnvAccess() {
619 assert(Tok.is(tok::annot_pragma_fenv_access));
620 tok::OnOffSwitch OOS =
621 static_cast<tok::OnOffSwitch>(
622 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
623
624 LangOptions::FEnvAccessModeKind FPC;
625 switch (OOS) {
626 case tok::OOS_ON:
627 FPC = LangOptions::FEA_On;
628 break;
629 case tok::OOS_OFF:
630 FPC = LangOptions::FEA_Off;
631 break;
632 case tok::OOS_DEFAULT: // FIXME: Add this cli option when it makes sense.
633 FPC = LangOptions::FEA_Off;
634 break;
635 }
636
637 Actions.ActOnPragmaFEnvAccess(FPC);
638 ConsumeAnnotationToken();
639}
640
641
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000642StmtResult Parser::HandlePragmaCaptured()
643{
644 assert(Tok.is(tok::annot_pragma_captured));
Richard Smithaf3b3252017-05-18 19:21:48 +0000645 ConsumeAnnotationToken();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000646
647 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000648 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000649 return StmtError();
650 }
651
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000652 SourceLocation Loc = Tok.getLocation();
653
Momchil Velikov57c681f2017-08-10 15:43:06 +0000654 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope |
655 Scope::CompoundStmtScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000656 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
657 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000658
659 StmtResult R = ParseCompoundStatement();
660 CapturedRegionScope.Exit();
661
662 if (R.isInvalid()) {
663 Actions.ActOnCapturedRegionError();
664 return StmtError();
665 }
666
667 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000668}
669
Eli Friedman68be1642012-10-04 02:36:51 +0000670namespace {
Yaxun Liu5b746652016-12-18 05:18:55 +0000671 enum OpenCLExtState : char {
672 Disable, Enable, Begin, End
673 };
674 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
Eli Friedman68be1642012-10-04 02:36:51 +0000675}
676
677void Parser::HandlePragmaOpenCLExtension() {
678 assert(Tok.is(tok::annot_pragma_opencl_extension));
Yaxun Liu5b746652016-12-18 05:18:55 +0000679 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
680 auto State = Data->second;
681 auto Ident = Data->first;
Eli Friedman68be1642012-10-04 02:36:51 +0000682 SourceLocation NameLoc = Tok.getLocation();
Richard Smithaf3b3252017-05-18 19:21:48 +0000683 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000684
Yaxun Liu5b746652016-12-18 05:18:55 +0000685 auto &Opt = Actions.getOpenCLOptions();
686 auto Name = Ident->getName();
Eli Friedman68be1642012-10-04 02:36:51 +0000687 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
688 // overriding all previously issued extension directives, but only if the
689 // behavior is set to disable."
Yaxun Liu5b746652016-12-18 05:18:55 +0000690 if (Name == "all") {
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000691 if (State == Disable) {
Yaxun Liu5b746652016-12-18 05:18:55 +0000692 Opt.disableAll();
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000693 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
694 } else {
Yaxun Liu5b746652016-12-18 05:18:55 +0000695 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000696 }
Yaxun Liu5b746652016-12-18 05:18:55 +0000697 } else if (State == Begin) {
698 if (!Opt.isKnown(Name) ||
699 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
700 Opt.support(Name);
701 }
702 Actions.setCurrentOpenCLExtension(Name);
703 } else if (State == End) {
704 if (Name != Actions.getCurrentOpenCLExtension())
705 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
706 Actions.setCurrentOpenCLExtension("");
707 } else if (!Opt.isKnown(Name))
708 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
709 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
710 Opt.enable(Name, State == Enable);
711 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
712 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
713 else
714 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
Eli Friedman68be1642012-10-04 02:36:51 +0000715}
716
David Majnemer4bb09802014-02-10 19:50:15 +0000717void Parser::HandlePragmaMSPointersToMembers() {
718 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000719 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
720 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000721 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Richard Smithaf3b3252017-05-18 19:21:48 +0000722 SourceLocation PragmaLoc = ConsumeAnnotationToken();
David Majnemer4bb09802014-02-10 19:50:15 +0000723 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
724}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000725
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000726void Parser::HandlePragmaMSVtorDisp() {
727 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
728 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
Denis Zobnin2290dac2016-04-29 11:27:00 +0000729 Sema::PragmaMsStackAction Action =
730 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000731 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
Richard Smithaf3b3252017-05-18 19:21:48 +0000732 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Denis Zobnin2290dac2016-04-29 11:27:00 +0000733 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000734}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000735
Warren Huntc3b18962014-04-08 22:30:47 +0000736void Parser::HandlePragmaMSPragma() {
737 assert(Tok.is(tok::annot_pragma_ms_pragma));
738 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000739 auto TheTokens =
740 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
741 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Richard Smithaf3b3252017-05-18 19:21:48 +0000742 SourceLocation PragmaLocation = ConsumeAnnotationToken();
Warren Huntc3b18962014-04-08 22:30:47 +0000743 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000744 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000745 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000746
Warren Huntc3b18962014-04-08 22:30:47 +0000747 // Figure out which #pragma we're dealing with. The switch has no default
748 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000749 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000750 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
751 .Case("data_seg", &Parser::HandlePragmaMSSegment)
752 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
753 .Case("const_seg", &Parser::HandlePragmaMSSegment)
754 .Case("code_seg", &Parser::HandlePragmaMSSegment)
755 .Case("section", &Parser::HandlePragmaMSSection)
756 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000757
758 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
759 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
760 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000761 while (Tok.isNot(tok::eof))
762 PP.Lex(Tok);
763 PP.Lex(Tok);
764 }
765}
766
Reid Kleckner722b1df2014-07-18 00:13:16 +0000767bool Parser::HandlePragmaMSSection(StringRef PragmaName,
768 SourceLocation PragmaLocation) {
769 if (Tok.isNot(tok::l_paren)) {
770 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
771 return false;
772 }
Warren Huntc3b18962014-04-08 22:30:47 +0000773 PP.Lex(Tok); // (
774 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000775 if (Tok.isNot(tok::string_literal)) {
776 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
777 << PragmaName;
778 return false;
779 }
780 ExprResult StringResult = ParseStringLiteralExpression();
781 if (StringResult.isInvalid())
782 return false; // Already diagnosed.
783 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
784 if (SegmentName->getCharByteWidth() != 1) {
785 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
786 << PragmaName;
787 return false;
788 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000789 int SectionFlags = ASTContext::PSF_Read;
790 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000791 while (Tok.is(tok::comma)) {
792 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000793 // Ignore "long" and "short".
794 // They are undocumented, but widely used, section attributes which appear
795 // to do nothing.
796 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
797 PP.Lex(Tok); // long/short
798 continue;
799 }
800
Reid Kleckner722b1df2014-07-18 00:13:16 +0000801 if (!Tok.isAnyIdentifier()) {
802 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
803 << PragmaName;
804 return false;
805 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000806 ASTContext::PragmaSectionFlag Flag =
807 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000808 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000809 .Case("read", ASTContext::PSF_Read)
810 .Case("write", ASTContext::PSF_Write)
811 .Case("execute", ASTContext::PSF_Execute)
812 .Case("shared", ASTContext::PSF_Invalid)
813 .Case("nopage", ASTContext::PSF_Invalid)
814 .Case("nocache", ASTContext::PSF_Invalid)
815 .Case("discard", ASTContext::PSF_Invalid)
816 .Case("remove", ASTContext::PSF_Invalid)
817 .Default(ASTContext::PSF_None);
818 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
819 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000820 ? diag::warn_pragma_invalid_specific_action
821 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000822 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000823 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000824 }
825 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000826 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000827 PP.Lex(Tok); // Identifier
828 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000829 // If no section attributes are specified, the section will be marked as
830 // read/write.
831 if (SectionFlagsAreDefault)
832 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000833 if (Tok.isNot(tok::r_paren)) {
834 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
835 return false;
836 }
Warren Huntc3b18962014-04-08 22:30:47 +0000837 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000838 if (Tok.isNot(tok::eof)) {
839 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
840 << PragmaName;
841 return false;
842 }
Warren Huntc3b18962014-04-08 22:30:47 +0000843 PP.Lex(Tok); // eof
844 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000845 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000846}
847
Reid Kleckner722b1df2014-07-18 00:13:16 +0000848bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
849 SourceLocation PragmaLocation) {
850 if (Tok.isNot(tok::l_paren)) {
851 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
852 return false;
853 }
Warren Huntc3b18962014-04-08 22:30:47 +0000854 PP.Lex(Tok); // (
855 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000856 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000857 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000858 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000859 if (PushPop == "push")
860 Action = Sema::PSK_Push;
861 else if (PushPop == "pop")
862 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000863 else {
864 PP.Diag(PragmaLocation,
865 diag::warn_pragma_expected_section_push_pop_or_name)
866 << PragmaName;
867 return false;
868 }
Warren Huntc3b18962014-04-08 22:30:47 +0000869 if (Action != Sema::PSK_Reset) {
870 PP.Lex(Tok); // push | pop
871 if (Tok.is(tok::comma)) {
872 PP.Lex(Tok); // ,
873 // If we've got a comma, we either need a label or a string.
874 if (Tok.isAnyIdentifier()) {
875 SlotLabel = Tok.getIdentifierInfo()->getName();
876 PP.Lex(Tok); // identifier
877 if (Tok.is(tok::comma))
878 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000879 else if (Tok.isNot(tok::r_paren)) {
880 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
881 << PragmaName;
882 return false;
883 }
Warren Huntc3b18962014-04-08 22:30:47 +0000884 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000885 } else if (Tok.isNot(tok::r_paren)) {
886 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
887 return false;
888 }
Warren Huntc3b18962014-04-08 22:30:47 +0000889 }
890 }
891 // Grab the string literal for our section name.
892 StringLiteral *SegmentName = nullptr;
893 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000894 if (Tok.isNot(tok::string_literal)) {
895 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000896 diag::warn_pragma_expected_section_name :
897 diag::warn_pragma_expected_section_label_or_name :
898 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000899 PP.Diag(PragmaLocation, DiagID) << PragmaName;
900 return false;
901 }
902 ExprResult StringResult = ParseStringLiteralExpression();
903 if (StringResult.isInvalid())
904 return false; // Already diagnosed.
905 SegmentName = cast<StringLiteral>(StringResult.get());
906 if (SegmentName->getCharByteWidth() != 1) {
907 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
908 << PragmaName;
909 return false;
910 }
Warren Huntc3b18962014-04-08 22:30:47 +0000911 // Setting section "" has no effect
912 if (SegmentName->getLength())
913 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
914 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000915 if (Tok.isNot(tok::r_paren)) {
916 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
917 return false;
918 }
Warren Huntc3b18962014-04-08 22:30:47 +0000919 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000920 if (Tok.isNot(tok::eof)) {
921 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
922 << PragmaName;
923 return false;
924 }
Warren Huntc3b18962014-04-08 22:30:47 +0000925 PP.Lex(Tok); // eof
926 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
927 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000928 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000929}
930
Reid Kleckner1a711b12014-07-22 00:53:05 +0000931// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000932bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
933 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000934 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
935 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
936 return false;
937 }
938
Reid Kleckner1a711b12014-07-22 00:53:05 +0000939 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
940 PragmaName))
941 return false;
942
943 // Parse either the known section names or the string section name.
944 StringLiteral *SegmentName = nullptr;
945 if (Tok.isAnyIdentifier()) {
946 auto *II = Tok.getIdentifierInfo();
947 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
948 .Case("compiler", "\".CRT$XCC\"")
949 .Case("lib", "\".CRT$XCL\"")
950 .Case("user", "\".CRT$XCU\"")
951 .Default("");
952
953 if (!Section.empty()) {
954 // Pretend the user wrote the appropriate string literal here.
955 Token Toks[1];
956 Toks[0].startToken();
957 Toks[0].setKind(tok::string_literal);
958 Toks[0].setLocation(Tok.getLocation());
959 Toks[0].setLiteralData(Section.data());
960 Toks[0].setLength(Section.size());
961 SegmentName =
962 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
963 PP.Lex(Tok);
964 }
965 } else if (Tok.is(tok::string_literal)) {
966 ExprResult StringResult = ParseStringLiteralExpression();
967 if (StringResult.isInvalid())
968 return false;
969 SegmentName = cast<StringLiteral>(StringResult.get());
970 if (SegmentName->getCharByteWidth() != 1) {
971 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
972 << PragmaName;
973 return false;
974 }
975 // FIXME: Add support for the '[, func-name]' part of the pragma.
976 }
977
978 if (!SegmentName) {
979 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
980 return false;
981 }
982
983 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
984 PragmaName) ||
985 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
986 PragmaName))
987 return false;
988
989 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
990 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000991}
992
Benjamin Kramere003ca22015-10-28 13:54:16 +0000993namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000994struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000995 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000996 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000997 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000998};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000999} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +00001000
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001001static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
1002 std::string PragmaString;
1003 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1004 PragmaString = "clang loop ";
1005 PragmaString += Option.getIdentifierInfo()->getName();
David Greenc8e39242018-08-01 14:36:12 +00001006 } else if (PragmaName.getIdentifierInfo()->getName() == "unroll_and_jam") {
1007 PragmaString = "unroll_and_jam";
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001008 } else {
1009 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1010 "Unexpected pragma name");
1011 PragmaString = "unroll";
1012 }
1013 return PragmaString;
1014}
1015
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001016bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001017 assert(Tok.is(tok::annot_pragma_loop_hint));
1018 PragmaLoopHintInfo *Info =
1019 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
1020
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001021 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
1022 Hint.PragmaNameLoc = IdentifierLoc::create(
1023 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +00001024
Aaron Ballmanef940aa2014-07-31 21:24:32 +00001025 // It is possible that the loop hint has no option identifier, such as
1026 // #pragma unroll(4).
1027 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
1028 ? Info->Option.getIdentifierInfo()
1029 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001030 Hint.OptionLoc = IdentifierLoc::create(
1031 Actions.Context, Info->Option.getLocation(), OptionInfo);
1032
David Blaikie2eabcc92016-02-09 18:52:09 +00001033 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001034
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001035 // Return a valid hint if pragma unroll or nounroll were specified
1036 // without an argument.
1037 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
1038 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Greenc8e39242018-08-01 14:36:12 +00001039 bool PragmaUnrollAndJam = PragmaNameInfo->getName() == "unroll_and_jam";
1040 bool PragmaNoUnrollAndJam = PragmaNameInfo->getName() == "nounroll_and_jam";
1041 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll || PragmaUnrollAndJam ||
1042 PragmaNoUnrollAndJam)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001043 ConsumeAnnotationToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001044 Hint.Range = Info->PragmaName.getLocation();
1045 return true;
1046 }
1047
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001048 // The constant expression is always followed by an eof token, which increases
1049 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +00001050 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001051 "PragmaLoopHintInfo::Toks must contain at least one token.");
1052
1053 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +00001054 bool OptionUnroll = false;
David Greenc8e39242018-08-01 14:36:12 +00001055 bool OptionUnrollAndJam = false;
Adam Nemet2de463e2016-06-14 12:04:26 +00001056 bool OptionDistribute = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001057 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +00001058 if (OptionInfo) { // Pragma Unroll does not specify an option.
1059 OptionUnroll = OptionInfo->isStr("unroll");
David Greenc8e39242018-08-01 14:36:12 +00001060 OptionUnrollAndJam = OptionInfo->isStr("unroll_and_jam");
Adam Nemet2de463e2016-06-14 12:04:26 +00001061 OptionDistribute = OptionInfo->isStr("distribute");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001062 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
1063 .Case("vectorize", true)
1064 .Case("interleave", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00001065 .Default(false) ||
David Greenc8e39242018-08-01 14:36:12 +00001066 OptionUnroll || OptionUnrollAndJam || OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001067 }
1068
David Greenc8e39242018-08-01 14:36:12 +00001069 bool AssumeSafetyArg =
1070 !OptionUnroll && !OptionUnrollAndJam && !OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001071 // Verify loop hint has an argument.
1072 if (Toks[0].is(tok::eof)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001073 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001074 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
David Greenc8e39242018-08-01 14:36:12 +00001075 << /*StateArgument=*/StateOption
1076 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
Adam Nemet2de463e2016-06-14 12:04:26 +00001077 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001078 return false;
1079 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001080
1081 // Validate the argument.
1082 if (StateOption) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001083 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001084 SourceLocation StateLoc = Toks[0].getLocation();
1085 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Adam Nemet50de4e82016-04-19 22:17:45 +00001086
David Greenc8e39242018-08-01 14:36:12 +00001087 bool Valid =
1088 StateInfo && llvm::StringSwitch<bool>(StateInfo->getName())
1089 .Cases("enable", "disable", true)
1090 .Case("full", OptionUnroll || OptionUnrollAndJam)
1091 .Case("assume_safety", AssumeSafetyArg)
1092 .Default(false);
Adam Nemet50de4e82016-04-19 22:17:45 +00001093 if (!Valid) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001094 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
David Greenc8e39242018-08-01 14:36:12 +00001095 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
Adam Nemet2de463e2016-06-14 12:04:26 +00001096 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001097 return false;
1098 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001099 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001100 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1101 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001102 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
1103 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001104 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +00001105 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001106 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001107
1108 ExprResult R = ParseConstantExpression();
1109
1110 // Tokens following an error in an ill-formed constant expression will
1111 // remain in the token stream and must be removed.
1112 if (Tok.isNot(tok::eof)) {
1113 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1114 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1115 while (Tok.isNot(tok::eof))
1116 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001117 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001118
1119 ConsumeToken(); // Consume the constant expression eof terminator.
1120
1121 if (R.isInvalid() ||
1122 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
1123 return false;
1124
1125 // Argument is a constant expression with an integer type.
1126 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001127 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001128
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001129 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +00001130 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001131 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +00001132}
1133
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001134namespace {
1135struct PragmaAttributeInfo {
Erik Pilkington7d180942018-10-29 17:38:42 +00001136 enum ActionType { Push, Pop, Attribute };
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001137 ParsedAttributes &Attributes;
1138 ActionType Action;
1139 ArrayRef<Token> Tokens;
1140
1141 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1142};
1143
1144#include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1145
1146} // end anonymous namespace
1147
1148static StringRef getIdentifier(const Token &Tok) {
1149 if (Tok.is(tok::identifier))
1150 return Tok.getIdentifierInfo()->getName();
1151 const char *S = tok::getKeywordSpelling(Tok.getKind());
1152 if (!S)
1153 return "";
1154 return S;
1155}
1156
1157static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1158 using namespace attr;
1159 switch (Rule) {
1160#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1161 case Value: \
1162 return IsAbstract;
1163#include "clang/Basic/AttrSubMatchRulesList.inc"
1164 }
1165 llvm_unreachable("Invalid attribute subject match rule");
1166 return false;
1167}
1168
1169static void diagnoseExpectedAttributeSubjectSubRule(
1170 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1171 SourceLocation SubRuleLoc) {
1172 auto Diagnostic =
1173 PRef.Diag(SubRuleLoc,
1174 diag::err_pragma_attribute_expected_subject_sub_identifier)
1175 << PrimaryRuleName;
1176 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1177 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1178 else
1179 Diagnostic << /*SubRulesSupported=*/0;
1180}
1181
1182static void diagnoseUnknownAttributeSubjectSubRule(
1183 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1184 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1185
1186 auto Diagnostic =
1187 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1188 << SubRuleName << PrimaryRuleName;
1189 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1190 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1191 else
1192 Diagnostic << /*SubRulesSupported=*/0;
1193}
1194
1195bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1196 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1197 SourceLocation &LastMatchRuleEndLoc) {
1198 bool IsAny = false;
1199 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1200 if (getIdentifier(Tok) == "any") {
1201 AnyLoc = ConsumeToken();
1202 IsAny = true;
1203 if (AnyParens.expectAndConsume())
1204 return true;
1205 }
1206
1207 do {
1208 // Parse the subject matcher rule.
1209 StringRef Name = getIdentifier(Tok);
1210 if (Name.empty()) {
1211 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1212 return true;
1213 }
1214 std::pair<Optional<attr::SubjectMatchRule>,
1215 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1216 Rule = isAttributeSubjectMatchRule(Name);
1217 if (!Rule.first) {
1218 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1219 return true;
1220 }
1221 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1222 SourceLocation RuleLoc = ConsumeToken();
1223
1224 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1225 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1226 if (Parens.expectAndConsume())
1227 return true;
1228 } else if (Parens.consumeOpen()) {
1229 if (!SubjectMatchRules
1230 .insert(
1231 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1232 .second)
1233 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1234 << Name
1235 << FixItHint::CreateRemoval(SourceRange(
1236 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1237 LastMatchRuleEndLoc = RuleLoc;
1238 continue;
1239 }
1240
1241 // Parse the sub-rules.
1242 StringRef SubRuleName = getIdentifier(Tok);
1243 if (SubRuleName.empty()) {
1244 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1245 Tok.getLocation());
1246 return true;
1247 }
1248 attr::SubjectMatchRule SubRule;
1249 if (SubRuleName == "unless") {
1250 SourceLocation SubRuleLoc = ConsumeToken();
1251 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1252 if (Parens.expectAndConsume())
1253 return true;
1254 SubRuleName = getIdentifier(Tok);
1255 if (SubRuleName.empty()) {
1256 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1257 SubRuleLoc);
1258 return true;
1259 }
1260 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1261 if (!SubRuleOrNone) {
1262 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1263 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1264 SubRuleUnlessName, SubRuleLoc);
1265 return true;
1266 }
1267 SubRule = *SubRuleOrNone;
1268 ConsumeToken();
1269 if (Parens.consumeClose())
1270 return true;
1271 } else {
1272 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1273 if (!SubRuleOrNone) {
1274 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1275 SubRuleName, Tok.getLocation());
1276 return true;
1277 }
1278 SubRule = *SubRuleOrNone;
1279 ConsumeToken();
1280 }
1281 SourceLocation RuleEndLoc = Tok.getLocation();
1282 LastMatchRuleEndLoc = RuleEndLoc;
1283 if (Parens.consumeClose())
1284 return true;
1285 if (!SubjectMatchRules
1286 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1287 .second) {
1288 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1289 << attr::getSubjectMatchRuleSpelling(SubRule)
1290 << FixItHint::CreateRemoval(SourceRange(
1291 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1292 continue;
1293 }
1294 } while (IsAny && TryConsumeToken(tok::comma));
1295
1296 if (IsAny)
1297 if (AnyParens.consumeClose())
1298 return true;
1299
1300 return false;
1301}
1302
1303namespace {
1304
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001305/// Describes the stage at which attribute subject rule parsing was interrupted.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001306enum class MissingAttributeSubjectRulesRecoveryPoint {
1307 Comma,
1308 ApplyTo,
1309 Equals,
1310 Any,
1311 None,
1312};
1313
1314MissingAttributeSubjectRulesRecoveryPoint
1315getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1316 if (const auto *II = Tok.getIdentifierInfo()) {
1317 if (II->isStr("apply_to"))
1318 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1319 if (II->isStr("any"))
1320 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1321 }
1322 if (Tok.is(tok::equal))
1323 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1324 return MissingAttributeSubjectRulesRecoveryPoint::None;
1325}
1326
1327/// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1328/// suggests the possible attribute subject rules in a fix-it together with
1329/// any other missing tokens.
1330DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
Erich Keanee891aa92018-07-13 15:07:47 +00001331 unsigned DiagID, ParsedAttr &Attribute,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001332 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1333 SourceLocation Loc = PRef.getEndOfPreviousToken();
1334 if (Loc.isInvalid())
1335 Loc = PRef.getCurToken().getLocation();
1336 auto Diagnostic = PRef.Diag(Loc, DiagID);
1337 std::string FixIt;
1338 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1339 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1340 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1341 FixIt = ", ";
1342 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1343 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1344 FixIt += "apply_to";
1345 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1346 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1347 FixIt += " = ";
1348 SourceRange FixItRange(Loc);
1349 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1350 // Gather the subject match rules that are supported by the attribute.
1351 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1352 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1353 if (SubjectMatchRuleSet.empty()) {
1354 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1355 // placeholders will be supported by the fix-its.
1356 return Diagnostic;
1357 }
1358 FixIt += "any(";
1359 bool NeedsComma = false;
1360 for (const auto &I : SubjectMatchRuleSet) {
1361 // Ensure that the missing rule is reported in the fix-it only when it's
1362 // supported in the current language mode.
1363 if (!I.second)
1364 continue;
1365 if (NeedsComma)
1366 FixIt += ", ";
1367 else
1368 NeedsComma = true;
1369 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1370 }
1371 FixIt += ")";
1372 // Check if we need to remove the range
1373 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1374 FixItRange.setEnd(PRef.getCurToken().getLocation());
1375 }
1376 if (FixItRange.getBegin() == FixItRange.getEnd())
1377 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1378 else
1379 Diagnostic << FixItHint::CreateReplacement(
1380 CharSourceRange::getCharRange(FixItRange), FixIt);
1381 return Diagnostic;
1382}
1383
1384} // end anonymous namespace
1385
1386void Parser::HandlePragmaAttribute() {
1387 assert(Tok.is(tok::annot_pragma_attribute) &&
1388 "Expected #pragma attribute annotation token");
1389 SourceLocation PragmaLoc = Tok.getLocation();
1390 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1391 if (Info->Action == PragmaAttributeInfo::Pop) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001392 ConsumeAnnotationToken();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001393 Actions.ActOnPragmaAttributePop(PragmaLoc);
1394 return;
1395 }
1396 // Parse the actual attribute with its arguments.
Erik Pilkington7d180942018-10-29 17:38:42 +00001397 assert((Info->Action == PragmaAttributeInfo::Push ||
1398 Info->Action == PragmaAttributeInfo::Attribute) &&
Erik Pilkingtonb287a012018-10-29 03:24:16 +00001399 "Unexpected #pragma attribute command");
Erik Pilkington7d180942018-10-29 17:38:42 +00001400
1401 if (Info->Action == PragmaAttributeInfo::Push && Info->Tokens.empty()) {
1402 ConsumeAnnotationToken();
1403 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc);
1404 return;
1405 }
1406
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001407 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001408 ConsumeAnnotationToken();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001409
1410 ParsedAttributes &Attrs = Info->Attributes;
1411 Attrs.clearListOnly();
1412
1413 auto SkipToEnd = [this]() {
1414 SkipUntil(tok::eof, StopBeforeMatch);
1415 ConsumeToken();
1416 };
1417
1418 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1419 // Parse the CXX11 style attribute.
1420 ParseCXX11AttributeSpecifier(Attrs);
1421 } else if (Tok.is(tok::kw___attribute)) {
1422 ConsumeToken();
1423 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1424 "attribute"))
1425 return SkipToEnd();
1426 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1427 return SkipToEnd();
1428
1429 if (Tok.isNot(tok::identifier)) {
1430 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1431 SkipToEnd();
1432 return;
1433 }
1434 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1435 SourceLocation AttrNameLoc = ConsumeToken();
1436
1437 if (Tok.isNot(tok::l_paren))
1438 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +00001439 ParsedAttr::AS_GNU);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001440 else
1441 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1442 /*ScopeName=*/nullptr,
Erich Keanee891aa92018-07-13 15:07:47 +00001443 /*ScopeLoc=*/SourceLocation(), ParsedAttr::AS_GNU,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001444 /*Declarator=*/nullptr);
1445
1446 if (ExpectAndConsume(tok::r_paren))
1447 return SkipToEnd();
1448 if (ExpectAndConsume(tok::r_paren))
1449 return SkipToEnd();
1450 } else if (Tok.is(tok::kw___declspec)) {
1451 ParseMicrosoftDeclSpecs(Attrs);
1452 } else {
1453 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1454 if (Tok.getIdentifierInfo()) {
1455 // If we suspect that this is an attribute suggest the use of
1456 // '__attribute__'.
Erich Keanee891aa92018-07-13 15:07:47 +00001457 if (ParsedAttr::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1458 ParsedAttr::AS_GNU) !=
1459 ParsedAttr::UnknownAttribute) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001460 SourceLocation InsertStartLoc = Tok.getLocation();
1461 ConsumeToken();
1462 if (Tok.is(tok::l_paren)) {
1463 ConsumeAnyToken();
1464 SkipUntil(tok::r_paren, StopBeforeMatch);
1465 if (Tok.isNot(tok::r_paren))
1466 return SkipToEnd();
1467 }
1468 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1469 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1470 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1471 }
1472 }
1473 SkipToEnd();
1474 return;
1475 }
1476
Erich Keanec480f302018-07-12 21:09:05 +00001477 if (Attrs.empty() || Attrs.begin()->isInvalid()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001478 SkipToEnd();
1479 return;
1480 }
1481
1482 // Ensure that we don't have more than one attribute.
Erich Keanec480f302018-07-12 21:09:05 +00001483 if (Attrs.size() > 1) {
1484 SourceLocation Loc = Attrs[1].getLoc();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001485 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1486 SkipToEnd();
1487 return;
1488 }
1489
Erich Keanee891aa92018-07-13 15:07:47 +00001490 ParsedAttr &Attribute = *Attrs.begin();
Erich Keanec480f302018-07-12 21:09:05 +00001491 if (!Attribute.isSupportedByPragmaAttribute()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001492 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
Erich Keanec480f302018-07-12 21:09:05 +00001493 << Attribute.getName();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001494 SkipToEnd();
1495 return;
1496 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001497
1498 // Parse the subject-list.
1499 if (!TryConsumeToken(tok::comma)) {
1500 createExpectedAttributeSubjectRulesTokenDiagnostic(
1501 diag::err_expected, Attribute,
1502 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1503 << tok::comma;
1504 SkipToEnd();
1505 return;
1506 }
1507
1508 if (Tok.isNot(tok::identifier)) {
1509 createExpectedAttributeSubjectRulesTokenDiagnostic(
1510 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1511 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1512 SkipToEnd();
1513 return;
1514 }
1515 const IdentifierInfo *II = Tok.getIdentifierInfo();
1516 if (!II->isStr("apply_to")) {
1517 createExpectedAttributeSubjectRulesTokenDiagnostic(
1518 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1519 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1520 SkipToEnd();
1521 return;
1522 }
1523 ConsumeToken();
1524
1525 if (!TryConsumeToken(tok::equal)) {
1526 createExpectedAttributeSubjectRulesTokenDiagnostic(
1527 diag::err_expected, Attribute,
1528 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1529 << tok::equal;
1530 SkipToEnd();
1531 return;
1532 }
1533
1534 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1535 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1536 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1537 LastMatchRuleEndLoc)) {
1538 SkipToEnd();
1539 return;
1540 }
1541
1542 // Tokens following an ill-formed attribute will remain in the token stream
1543 // and must be removed.
1544 if (Tok.isNot(tok::eof)) {
1545 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1546 SkipToEnd();
1547 return;
1548 }
1549
1550 // Consume the eof terminator token.
1551 ConsumeToken();
1552
Erik Pilkington7d180942018-10-29 17:38:42 +00001553 // Handle a mixed push/attribute by desurging to a push, then an attribute.
1554 if (Info->Action == PragmaAttributeInfo::Push)
1555 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc);
1556
1557 Actions.ActOnPragmaAttributeAttribute(Attribute, PragmaLoc,
1558 std::move(SubjectMatchRules));
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001559}
1560
Eli Bendersky06a40422014-06-06 20:31:48 +00001561// #pragma GCC visibility comes in two variants:
1562// 'push' '(' [visibility] ')'
1563// 'pop'
Fangrui Song6907ce22018-07-30 19:24:48 +00001564void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001565 PragmaIntroducerKind Introducer,
1566 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +00001567 SourceLocation VisLoc = VisTok.getLocation();
1568
1569 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001570 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001571
1572 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1573
Eli Friedman570024a2010-08-05 06:57:20 +00001574 const IdentifierInfo *VisType;
1575 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +00001576 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +00001577 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001578 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001579 if (Tok.isNot(tok::l_paren)) {
1580 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1581 << "visibility";
1582 return;
1583 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001584 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001585 VisType = Tok.getIdentifierInfo();
1586 if (!VisType) {
1587 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1588 << "visibility";
1589 return;
1590 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001591 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001592 if (Tok.isNot(tok::r_paren)) {
1593 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1594 << "visibility";
1595 return;
1596 }
1597 } else {
1598 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1599 << "visibility";
1600 return;
1601 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001602 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001603 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001604 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +00001605 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1606 << "visibility";
1607 return;
1608 }
1609
David Blaikie2eabcc92016-02-09 18:52:09 +00001610 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +00001611 Toks[0].startToken();
1612 Toks[0].setKind(tok::annot_pragma_vis);
1613 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001614 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +00001615 Toks[0].setAnnotationValue(
1616 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001617 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +00001618}
1619
Daniel Dunbar921b9682008-10-04 19:21:03 +00001620// #pragma pack(...) comes in the following delicious flavors:
1621// pack '(' [integer] ')'
1622// pack '(' 'show' ')'
1623// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Fangrui Song6907ce22018-07-30 19:24:48 +00001624void PragmaPackHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001625 PragmaIntroducerKind Introducer,
1626 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +00001627 SourceLocation PackLoc = PackTok.getLocation();
1628
1629 Token Tok;
1630 PP.Lex(Tok);
1631 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001632 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001633 return;
1634 }
1635
Denis Zobnin10c4f452016-04-29 18:17:40 +00001636 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1637 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001638 Token Alignment;
1639 Alignment.startToken();
Mike Stump11289f42009-09-09 15:08:12 +00001640 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001641 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001642 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001643
1644 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +00001645
1646 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1647 // the push/pop stack.
1648 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
Denis Zobnin10c4f452016-04-29 18:17:40 +00001649 Action =
1650 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001651 } else if (Tok.is(tok::identifier)) {
1652 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +00001653 if (II->isStr("show")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001654 Action = Sema::PSK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001655 PP.Lex(Tok);
1656 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001657 if (II->isStr("push")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001658 Action = Sema::PSK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +00001659 } else if (II->isStr("pop")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001660 Action = Sema::PSK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001661 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001662 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001663 return;
Mike Stump11289f42009-09-09 15:08:12 +00001664 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001665 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001666
Daniel Dunbar921b9682008-10-04 19:21:03 +00001667 if (Tok.is(tok::comma)) {
1668 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001669
Daniel Dunbar921b9682008-10-04 19:21:03 +00001670 if (Tok.is(tok::numeric_constant)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001671 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001672 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001673
1674 PP.Lex(Tok);
1675 } else if (Tok.is(tok::identifier)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001676 SlotLabel = Tok.getIdentifierInfo()->getName();
Daniel Dunbar921b9682008-10-04 19:21:03 +00001677 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001678
Daniel Dunbar921b9682008-10-04 19:21:03 +00001679 if (Tok.is(tok::comma)) {
1680 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001681
Daniel Dunbar921b9682008-10-04 19:21:03 +00001682 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001683 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001684 return;
1685 }
Mike Stump11289f42009-09-09 15:08:12 +00001686
Denis Zobnin10c4f452016-04-29 18:17:40 +00001687 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001688 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001689
1690 PP.Lex(Tok);
1691 }
1692 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001693 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001694 return;
1695 }
1696 }
1697 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001698 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001699 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1700 // the push/pop stack.
1701 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
Denis Zobnin10c4f452016-04-29 18:17:40 +00001702 Action = Sema::PSK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001703 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001704
1705 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001706 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001707 return;
1708 }
1709
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001710 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001711 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001712 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001713 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1714 return;
1715 }
1716
David Blaikie2eabcc92016-02-09 18:52:09 +00001717 PragmaPackInfo *Info =
1718 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Denis Zobnin10c4f452016-04-29 18:17:40 +00001719 Info->Action = Action;
1720 Info->SlotLabel = SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001721 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001722
David Blaikie2eabcc92016-02-09 18:52:09 +00001723 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1724 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001725 Toks[0].startToken();
1726 Toks[0].setKind(tok::annot_pragma_pack);
1727 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001728 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001729 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001730 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001731}
1732
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001733// #pragma ms_struct on
1734// #pragma ms_struct off
Fangrui Song6907ce22018-07-30 19:24:48 +00001735void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001736 PragmaIntroducerKind Introducer,
1737 Token &MSStructTok) {
Nico Weber779355f2016-03-02 23:22:00 +00001738 PragmaMSStructKind Kind = PMSST_OFF;
1739
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001740 Token Tok;
1741 PP.Lex(Tok);
1742 if (Tok.isNot(tok::identifier)) {
1743 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1744 return;
1745 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001746 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001747 const IdentifierInfo *II = Tok.getIdentifierInfo();
1748 if (II->isStr("on")) {
Nico Weber779355f2016-03-02 23:22:00 +00001749 Kind = PMSST_ON;
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001750 PP.Lex(Tok);
1751 }
1752 else if (II->isStr("off") || II->isStr("reset"))
1753 PP.Lex(Tok);
1754 else {
1755 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1756 return;
1757 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001758
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001759 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001760 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1761 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001762 return;
1763 }
Eli Friedman68be1642012-10-04 02:36:51 +00001764
David Blaikie2eabcc92016-02-09 18:52:09 +00001765 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1766 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001767 Toks[0].startToken();
1768 Toks[0].setKind(tok::annot_pragma_msstruct);
1769 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001770 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001771 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1772 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001773 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001774}
1775
Javed Absar2a67c9e2017-06-05 10:11:57 +00001776// #pragma clang section bss="abc" data="" rodata="def" text=""
1777void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
1778 PragmaIntroducerKind Introducer, Token &FirstToken) {
1779
1780 Token Tok;
1781 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1782
1783 PP.Lex(Tok); // eat 'section'
1784 while (Tok.isNot(tok::eod)) {
1785 if (Tok.isNot(tok::identifier)) {
1786 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1787 return;
1788 }
1789
1790 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1791 if (SecType->isStr("bss"))
1792 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1793 else if (SecType->isStr("data"))
1794 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1795 else if (SecType->isStr("rodata"))
1796 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1797 else if (SecType->isStr("text"))
1798 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1799 else {
1800 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1801 return;
1802 }
1803
1804 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1805 if (Tok.isNot(tok::equal)) {
1806 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1807 return;
1808 }
1809
1810 std::string SecName;
1811 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1812 return;
1813
1814 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1815 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1816 Sema::PragmaClangSectionAction::PCSA_Clear),
1817 SecKind, SecName);
1818 }
1819}
1820
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001821// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1822// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001823static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001824 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001825 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001826
1827 if (IsOptions) {
1828 PP.Lex(Tok);
1829 if (Tok.isNot(tok::identifier) ||
1830 !Tok.getIdentifierInfo()->isStr("align")) {
1831 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1832 return;
1833 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001834 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001835
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001836 PP.Lex(Tok);
1837 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001838 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1839 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001840 return;
1841 }
1842
1843 PP.Lex(Tok);
1844 if (Tok.isNot(tok::identifier)) {
1845 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001846 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001847 return;
1848 }
1849
John McCallfaf5fb42010-08-26 23:41:50 +00001850 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001851 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001852 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001853 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001854 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001855 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001856 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001857 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001858 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001859 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001860 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001861 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001862 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001863 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001864 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001865 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1866 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001867 return;
1868 }
1869
David Majnemera8f2f1d2015-03-19 00:10:23 +00001870 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001871 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001872 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001873 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001874 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001875 return;
1876 }
1877
David Blaikie2eabcc92016-02-09 18:52:09 +00001878 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1879 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001880 Toks[0].startToken();
1881 Toks[0].setKind(tok::annot_pragma_align);
1882 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001883 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001884 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1885 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001886 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001887}
1888
Fangrui Song6907ce22018-07-30 19:24:48 +00001889void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001890 PragmaIntroducerKind Introducer,
1891 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001892 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001893}
1894
Fangrui Song6907ce22018-07-30 19:24:48 +00001895void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001896 PragmaIntroducerKind Introducer,
1897 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001898 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001899}
1900
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001901// #pragma unused(identifier)
Fangrui Song6907ce22018-07-30 19:24:48 +00001902void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001903 PragmaIntroducerKind Introducer,
1904 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001905 // FIXME: Should we be expanding macros here? My guess is no.
1906 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001907
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001908 // Lex the left '('.
1909 Token Tok;
1910 PP.Lex(Tok);
1911 if (Tok.isNot(tok::l_paren)) {
1912 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1913 return;
1914 }
Mike Stump11289f42009-09-09 15:08:12 +00001915
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001916 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001917 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001918 SourceLocation RParenLoc;
1919 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001920
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001921 while (true) {
1922 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001923
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001924 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001925 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001926 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001927 LexID = false;
1928 continue;
1929 }
1930
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001931 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001932 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1933 return;
1934 }
Mike Stump11289f42009-09-09 15:08:12 +00001935
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001936 // We are execting a ')' or a ','.
1937 if (Tok.is(tok::comma)) {
1938 LexID = true;
1939 continue;
1940 }
Mike Stump11289f42009-09-09 15:08:12 +00001941
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001942 if (Tok.is(tok::r_paren)) {
1943 RParenLoc = Tok.getLocation();
1944 break;
1945 }
Mike Stump11289f42009-09-09 15:08:12 +00001946
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001947 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001948 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001949 return;
1950 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001951
1952 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001953 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001954 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1955 "unused";
1956 return;
1957 }
1958
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001959 // Verify that we have a location for the right parenthesis.
1960 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001961 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001962
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001963 // For each identifier token, insert into the token stream a
1964 // annot_pragma_unused token followed by the identifier token.
1965 // This allows us to cache a "#pragma unused" that occurs inside an inline
1966 // C++ member function.
1967
David Blaikie2eabcc92016-02-09 18:52:09 +00001968 MutableArrayRef<Token> Toks(
1969 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1970 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001971 for (unsigned i=0; i != Identifiers.size(); i++) {
1972 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1973 pragmaUnusedTok.startToken();
1974 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1975 pragmaUnusedTok.setLocation(UnusedLoc);
1976 idTok = Identifiers[i];
1977 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001978 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001979}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001980
1981// #pragma weak identifier
1982// #pragma weak identifier '=' identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00001983void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001984 PragmaIntroducerKind Introducer,
1985 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001986 SourceLocation WeakLoc = WeakTok.getLocation();
1987
1988 Token Tok;
1989 PP.Lex(Tok);
1990 if (Tok.isNot(tok::identifier)) {
1991 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1992 return;
1993 }
1994
Eli Friedman68be1642012-10-04 02:36:51 +00001995 Token WeakName = Tok;
1996 bool HasAlias = false;
1997 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001998
1999 PP.Lex(Tok);
2000 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00002001 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002002 PP.Lex(Tok);
2003 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00002004 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002005 << "weak";
2006 return;
2007 }
Eli Friedman68be1642012-10-04 02:36:51 +00002008 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002009 PP.Lex(Tok);
2010 }
2011
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002012 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002013 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
2014 return;
2015 }
2016
Eli Friedman68be1642012-10-04 02:36:51 +00002017 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00002018 MutableArrayRef<Token> Toks(
2019 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002020 Token &pragmaUnusedTok = Toks[0];
2021 pragmaUnusedTok.startToken();
2022 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
2023 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002024 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002025 Toks[1] = WeakName;
2026 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002027 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002028 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00002029 MutableArrayRef<Token> Toks(
2030 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00002031 Token &pragmaUnusedTok = Toks[0];
2032 pragmaUnusedTok.startToken();
2033 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
2034 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002035 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00002036 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002037 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002038 }
2039}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002040
David Chisnall0867d9c2012-02-18 16:12:34 +00002041// #pragma redefine_extname identifier identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00002042void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
David Chisnall0867d9c2012-02-18 16:12:34 +00002043 PragmaIntroducerKind Introducer,
2044 Token &RedefToken) {
2045 SourceLocation RedefLoc = RedefToken.getLocation();
2046
2047 Token Tok;
2048 PP.Lex(Tok);
2049 if (Tok.isNot(tok::identifier)) {
2050 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2051 "redefine_extname";
2052 return;
2053 }
2054
Eli Friedman68be1642012-10-04 02:36:51 +00002055 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002056 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00002057
David Chisnall0867d9c2012-02-18 16:12:34 +00002058 if (Tok.isNot(tok::identifier)) {
2059 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2060 << "redefine_extname";
2061 return;
2062 }
Eli Friedman68be1642012-10-04 02:36:51 +00002063
2064 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002065 PP.Lex(Tok);
2066
2067 if (Tok.isNot(tok::eod)) {
2068 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2069 "redefine_extname";
2070 return;
2071 }
2072
David Blaikie2eabcc92016-02-09 18:52:09 +00002073 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
2074 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002075 Token &pragmaRedefTok = Toks[0];
2076 pragmaRedefTok.startToken();
2077 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
2078 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002079 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002080 Toks[1] = RedefName;
2081 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002082 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
David Chisnall0867d9c2012-02-18 16:12:34 +00002083}
2084
2085
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002086void
Fangrui Song6907ce22018-07-30 19:24:48 +00002087PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002088 PragmaIntroducerKind Introducer,
2089 Token &Tok) {
2090 tok::OnOffSwitch OOS;
2091 if (PP.LexOnOffSwitch(OOS))
2092 return;
2093
David Blaikie2eabcc92016-02-09 18:52:09 +00002094 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2095 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002096 Toks[0].startToken();
2097 Toks[0].setKind(tok::annot_pragma_fp_contract);
2098 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002099 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002100 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
2101 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00002102 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002103}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002104
Fangrui Song6907ce22018-07-30 19:24:48 +00002105void
2106PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002107 PragmaIntroducerKind Introducer,
2108 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00002109 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002110 if (Tok.isNot(tok::identifier)) {
2111 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2112 "OPENCL";
2113 return;
2114 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002115 IdentifierInfo *Ext = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002116 SourceLocation NameLoc = Tok.getLocation();
2117
2118 PP.Lex(Tok);
2119 if (Tok.isNot(tok::colon)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002120 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002121 return;
2122 }
2123
2124 PP.Lex(Tok);
2125 if (Tok.isNot(tok::identifier)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002126 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002127 return;
2128 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002129 IdentifierInfo *Pred = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002130
Yaxun Liu5b746652016-12-18 05:18:55 +00002131 OpenCLExtState State;
2132 if (Pred->isStr("enable")) {
2133 State = Enable;
2134 } else if (Pred->isStr("disable")) {
2135 State = Disable;
2136 } else if (Pred->isStr("begin"))
2137 State = Begin;
2138 else if (Pred->isStr("end"))
2139 State = End;
2140 else {
2141 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
2142 << Ext->isStr("all");
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002143 return;
2144 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002145 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002146
Eli Friedman68be1642012-10-04 02:36:51 +00002147 PP.Lex(Tok);
2148 if (Tok.isNot(tok::eod)) {
2149 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2150 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002151 return;
2152 }
Eli Friedman68be1642012-10-04 02:36:51 +00002153
Yaxun Liu5b746652016-12-18 05:18:55 +00002154 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2155 Info->first = Ext;
2156 Info->second = State;
David Blaikie2eabcc92016-02-09 18:52:09 +00002157 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2158 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002159 Toks[0].startToken();
2160 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2161 Toks[0].setLocation(NameLoc);
Yaxun Liu5b746652016-12-18 05:18:55 +00002162 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Majnemera8f2f1d2015-03-19 00:10:23 +00002163 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00002164 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002165
2166 if (PP.getPPCallbacks())
Fangrui Song6907ce22018-07-30 19:24:48 +00002167 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
Yaxun Liu5b746652016-12-18 05:18:55 +00002168 StateLoc, State);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002169}
2170
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002171/// Handle '#pragma omp ...' when OpenMP is disabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002172///
2173void
2174PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2175 PragmaIntroducerKind Introducer,
2176 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00002177 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2178 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002179 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00002180 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2181 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00002182 }
2183 PP.DiscardUntilEndOfDirective();
2184}
2185
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002186/// Handle '#pragma omp ...' when OpenMP is enabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002187///
2188void
2189PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2190 PragmaIntroducerKind Introducer,
2191 Token &FirstTok) {
2192 SmallVector<Token, 16> Pragma;
2193 Token Tok;
2194 Tok.startToken();
2195 Tok.setKind(tok::annot_pragma_openmp);
2196 Tok.setLocation(FirstTok.getLocation());
2197
Alexey Bataev96dae812018-02-16 18:36:44 +00002198 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002199 Pragma.push_back(Tok);
2200 PP.Lex(Tok);
Alexey Bataev96dae812018-02-16 18:36:44 +00002201 if (Tok.is(tok::annot_pragma_openmp)) {
2202 PP.Diag(Tok, diag::err_omp_unexpected_directive) << 0;
2203 unsigned InnerPragmaCnt = 1;
2204 while (InnerPragmaCnt != 0) {
2205 PP.Lex(Tok);
2206 if (Tok.is(tok::annot_pragma_openmp))
2207 ++InnerPragmaCnt;
2208 else if (Tok.is(tok::annot_pragma_openmp_end))
2209 --InnerPragmaCnt;
2210 }
2211 PP.Lex(Tok);
2212 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002213 }
2214 SourceLocation EodLoc = Tok.getLocation();
2215 Tok.startToken();
2216 Tok.setKind(tok::annot_pragma_openmp_end);
2217 Tok.setLocation(EodLoc);
2218 Pragma.push_back(Tok);
2219
David Blaikie2eabcc92016-02-09 18:52:09 +00002220 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2221 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2222 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
2223 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00002224}
Reid Kleckner002562a2013-05-06 21:02:12 +00002225
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002226/// Handle '#pragma pointers_to_members'
David Majnemer4bb09802014-02-10 19:50:15 +00002227// The grammar for this pragma is as follows:
2228//
2229// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2230//
2231// #pragma pointers_to_members '(' 'best_case' ')'
2232// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2233// #pragma pointers_to_members '(' inheritance-model ')'
2234void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
2235 PragmaIntroducerKind Introducer,
2236 Token &Tok) {
2237 SourceLocation PointersToMembersLoc = Tok.getLocation();
2238 PP.Lex(Tok);
2239 if (Tok.isNot(tok::l_paren)) {
2240 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2241 << "pointers_to_members";
2242 return;
2243 }
2244 PP.Lex(Tok);
2245 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2246 if (!Arg) {
2247 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2248 << "pointers_to_members";
2249 return;
2250 }
2251 PP.Lex(Tok);
2252
David Majnemer86c318f2014-02-11 21:05:00 +00002253 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00002254 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002255 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00002256 } else {
2257 if (Arg->isStr("full_generality")) {
2258 if (Tok.is(tok::comma)) {
2259 PP.Lex(Tok);
2260
2261 Arg = Tok.getIdentifierInfo();
2262 if (!Arg) {
2263 PP.Diag(Tok.getLocation(),
2264 diag::err_pragma_pointers_to_members_unknown_kind)
2265 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2266 return;
2267 }
2268 PP.Lex(Tok);
2269 } else if (Tok.is(tok::r_paren)) {
2270 // #pragma pointers_to_members(full_generality) implicitly specifies
2271 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00002272 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00002273 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002274 } else {
2275 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2276 << "full_generality";
2277 return;
2278 }
2279 }
2280
2281 if (Arg) {
2282 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002283 RepresentationMethod =
2284 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002285 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002286 RepresentationMethod =
2287 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002288 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002289 RepresentationMethod =
2290 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002291 } else {
2292 PP.Diag(Tok.getLocation(),
2293 diag::err_pragma_pointers_to_members_unknown_kind)
2294 << Arg << /*HasPointerDeclaration*/ 1;
2295 return;
2296 }
2297 }
2298 }
2299
2300 if (Tok.isNot(tok::r_paren)) {
2301 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2302 << (Arg ? Arg->getName() : "full_generality");
2303 return;
2304 }
2305
David Majnemera8f2f1d2015-03-19 00:10:23 +00002306 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00002307 PP.Lex(Tok);
2308 if (Tok.isNot(tok::eod)) {
2309 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2310 << "pointers_to_members";
2311 return;
2312 }
2313
2314 Token AnnotTok;
2315 AnnotTok.startToken();
2316 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2317 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002318 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00002319 AnnotTok.setAnnotationValue(
2320 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
2321 PP.EnterToken(AnnotTok);
2322}
2323
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002324/// Handle '#pragma vtordisp'
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002325// The grammar for this pragma is as follows:
2326//
2327// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2328//
2329// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2330// #pragma vtordisp '(' 'pop' ')'
2331// #pragma vtordisp '(' ')'
2332void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
2333 PragmaIntroducerKind Introducer,
2334 Token &Tok) {
2335 SourceLocation VtorDispLoc = Tok.getLocation();
2336 PP.Lex(Tok);
2337 if (Tok.isNot(tok::l_paren)) {
2338 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2339 return;
2340 }
2341 PP.Lex(Tok);
2342
Denis Zobnin2290dac2016-04-29 11:27:00 +00002343 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002344 const IdentifierInfo *II = Tok.getIdentifierInfo();
2345 if (II) {
2346 if (II->isStr("push")) {
2347 // #pragma vtordisp(push, mode)
2348 PP.Lex(Tok);
2349 if (Tok.isNot(tok::comma)) {
2350 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2351 return;
2352 }
2353 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002354 Action = Sema::PSK_Push_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002355 // not push, could be on/off
2356 } else if (II->isStr("pop")) {
2357 // #pragma vtordisp(pop)
2358 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002359 Action = Sema::PSK_Pop;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002360 }
2361 // not push or pop, could be on/off
2362 } else {
2363 if (Tok.is(tok::r_paren)) {
2364 // #pragma vtordisp()
Denis Zobnin2290dac2016-04-29 11:27:00 +00002365 Action = Sema::PSK_Reset;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002366 }
2367 }
2368
2369
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002370 uint64_t Value = 0;
Denis Zobnin2290dac2016-04-29 11:27:00 +00002371 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002372 const IdentifierInfo *II = Tok.getIdentifierInfo();
2373 if (II && II->isStr("off")) {
2374 PP.Lex(Tok);
2375 Value = 0;
2376 } else if (II && II->isStr("on")) {
2377 PP.Lex(Tok);
2378 Value = 1;
2379 } else if (Tok.is(tok::numeric_constant) &&
2380 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2381 if (Value > 2) {
2382 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2383 << 0 << 2 << "vtordisp";
2384 return;
2385 }
2386 } else {
2387 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2388 << "vtordisp";
2389 return;
2390 }
2391 }
2392
2393 // Finish the pragma: ')' $
2394 if (Tok.isNot(tok::r_paren)) {
2395 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2396 return;
2397 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00002398 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002399 PP.Lex(Tok);
2400 if (Tok.isNot(tok::eod)) {
2401 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2402 << "vtordisp";
2403 return;
2404 }
2405
2406 // Enter the annotation.
2407 Token AnnotTok;
2408 AnnotTok.startToken();
2409 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2410 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002411 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002412 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
Denis Zobnin2290dac2016-04-29 11:27:00 +00002413 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002414 PP.EnterToken(AnnotTok);
2415}
2416
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002417/// Handle all MS pragmas. Simply forwards the tokens after inserting
Warren Huntc3b18962014-04-08 22:30:47 +00002418/// an annotation token.
2419void PragmaMSPragma::HandlePragma(Preprocessor &PP,
2420 PragmaIntroducerKind Introducer,
2421 Token &Tok) {
2422 Token EoF, AnnotTok;
2423 EoF.startToken();
2424 EoF.setKind(tok::eof);
2425 AnnotTok.startToken();
2426 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2427 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002428 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00002429 SmallVector<Token, 8> TokenVector;
2430 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00002431 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00002432 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002433 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2434 }
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002435 // Add a sentinel EoF token to the end of the list.
Warren Huntc3b18962014-04-08 22:30:47 +00002436 TokenVector.push_back(EoF);
2437 // We must allocate this array with new because EnterTokenStream is going to
2438 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00002439 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2440 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00002441 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00002442 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2443 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00002444 AnnotTok.setAnnotationValue(Value);
2445 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00002446}
2447
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002448/// Handle the Microsoft \#pragma detect_mismatch extension.
Aaron Ballman5d041be2013-06-04 02:07:14 +00002449///
2450/// The syntax is:
2451/// \code
2452/// #pragma detect_mismatch("name", "value")
2453/// \endcode
2454/// Where 'name' and 'value' are quoted strings. The values are embedded in
2455/// the object file and passed along to the linker. If the linker detects a
2456/// mismatch in the object file's values for the given name, a LNK2038 error
2457/// is emitted. See MSDN for more details.
2458void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
2459 PragmaIntroducerKind Introducer,
2460 Token &Tok) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002461 SourceLocation DetectMismatchLoc = Tok.getLocation();
Aaron Ballman5d041be2013-06-04 02:07:14 +00002462 PP.Lex(Tok);
2463 if (Tok.isNot(tok::l_paren)) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002464 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002465 return;
2466 }
2467
2468 // Read the name to embed, which must be a string literal.
2469 std::string NameString;
2470 if (!PP.LexStringLiteral(Tok, NameString,
2471 "pragma detect_mismatch",
2472 /*MacroExpansion=*/true))
2473 return;
2474
2475 // Read the comma followed by a second string literal.
2476 std::string ValueString;
2477 if (Tok.isNot(tok::comma)) {
2478 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2479 return;
2480 }
2481
2482 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
2483 /*MacroExpansion=*/true))
2484 return;
2485
2486 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002487 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002488 return;
2489 }
2490 PP.Lex(Tok); // Eat the r_paren.
2491
2492 if (Tok.isNot(tok::eod)) {
2493 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2494 return;
2495 }
2496
Reid Kleckner71966c92014-02-20 23:37:45 +00002497 // If the pragma is lexically sound, notify any interested PPCallbacks.
2498 if (PP.getPPCallbacks())
Nico Webercbbaeb12016-03-02 19:28:54 +00002499 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
Reid Kleckner71966c92014-02-20 23:37:45 +00002500 ValueString);
2501
Nico Webercbbaeb12016-03-02 19:28:54 +00002502 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
Aaron Ballman5d041be2013-06-04 02:07:14 +00002503}
2504
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002505/// Handle the microsoft \#pragma comment extension.
Reid Kleckner002562a2013-05-06 21:02:12 +00002506///
2507/// The syntax is:
2508/// \code
2509/// #pragma comment(linker, "foo")
2510/// \endcode
2511/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2512/// "foo" is a string, which is fully macro expanded, and permits string
2513/// concatenation, embedded escape characters etc. See MSDN for more details.
2514void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
2515 PragmaIntroducerKind Introducer,
2516 Token &Tok) {
2517 SourceLocation CommentLoc = Tok.getLocation();
2518 PP.Lex(Tok);
2519 if (Tok.isNot(tok::l_paren)) {
2520 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2521 return;
2522 }
2523
2524 // Read the identifier.
2525 PP.Lex(Tok);
2526 if (Tok.isNot(tok::identifier)) {
2527 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2528 return;
2529 }
2530
2531 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002532 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00002533 PragmaMSCommentKind Kind =
2534 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2535 .Case("linker", PCK_Linker)
2536 .Case("lib", PCK_Lib)
2537 .Case("compiler", PCK_Compiler)
2538 .Case("exestr", PCK_ExeStr)
2539 .Case("user", PCK_User)
2540 .Default(PCK_Unknown);
2541 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00002542 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2543 return;
2544 }
2545
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +00002546 if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
2547 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2548 << II->getName();
2549 return;
2550 }
2551
Yunzhong Gao99efc032015-03-23 20:41:42 +00002552 // On PS4, issue a warning about any pragma comments other than
2553 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00002554 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00002555 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2556 << II->getName();
2557 return;
2558 }
2559
Reid Kleckner002562a2013-05-06 21:02:12 +00002560 // Read the optional string if present.
2561 PP.Lex(Tok);
2562 std::string ArgumentString;
2563 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2564 "pragma comment",
2565 /*MacroExpansion=*/true))
2566 return;
2567
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002568 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00002569 // FIXME: If the kind is "compiler" warn if the string is present (it is
2570 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002571 // The MSDN docs say that "lib" and "linker" require a string and have a short
2572 // whitelist of linker options they support, but in practice MSVC doesn't
2573 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00002574
2575 if (Tok.isNot(tok::r_paren)) {
2576 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2577 return;
2578 }
2579 PP.Lex(Tok); // eat the r_paren.
2580
2581 if (Tok.isNot(tok::eod)) {
2582 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2583 return;
2584 }
2585
Reid Kleckner71966c92014-02-20 23:37:45 +00002586 // If the pragma is lexically sound, notify any interested PPCallbacks.
2587 if (PP.getPPCallbacks())
2588 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2589
Nico Weber66220292016-03-02 17:28:48 +00002590 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00002591}
Dario Domizioli13a0a382014-05-23 12:13:25 +00002592
2593// #pragma clang optimize off
2594// #pragma clang optimize on
Fangrui Song6907ce22018-07-30 19:24:48 +00002595void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
Dario Domizioli13a0a382014-05-23 12:13:25 +00002596 PragmaIntroducerKind Introducer,
2597 Token &FirstToken) {
2598 Token Tok;
2599 PP.Lex(Tok);
2600 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002601 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00002602 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00002603 return;
2604 }
2605 if (Tok.isNot(tok::identifier)) {
2606 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2607 << PP.getSpelling(Tok);
2608 return;
2609 }
2610 const IdentifierInfo *II = Tok.getIdentifierInfo();
2611 // The only accepted values are 'on' or 'off'.
2612 bool IsOn = false;
2613 if (II->isStr("on")) {
2614 IsOn = true;
2615 } else if (!II->isStr("off")) {
2616 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2617 << PP.getSpelling(Tok);
2618 return;
2619 }
2620 PP.Lex(Tok);
Fangrui Song6907ce22018-07-30 19:24:48 +00002621
Dario Domizioli13a0a382014-05-23 12:13:25 +00002622 if (Tok.isNot(tok::eod)) {
2623 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2624 << PP.getSpelling(Tok);
2625 return;
2626 }
Eli Bendersky06a40422014-06-06 20:31:48 +00002627
2628 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2629}
2630
Adam Nemet60d32642017-04-04 21:18:36 +00002631namespace {
2632/// Used as the annotation value for tok::annot_pragma_fp.
2633struct TokFPAnnotValue {
2634 enum FlagKinds { Contract };
2635 enum FlagValues { On, Off, Fast };
2636
2637 FlagKinds FlagKind;
2638 FlagValues FlagValue;
2639};
2640} // end anonymous namespace
2641
2642void PragmaFPHandler::HandlePragma(Preprocessor &PP,
2643 PragmaIntroducerKind Introducer,
2644 Token &Tok) {
2645 // fp
2646 Token PragmaName = Tok;
2647 SmallVector<Token, 1> TokenList;
2648
2649 PP.Lex(Tok);
2650 if (Tok.isNot(tok::identifier)) {
2651 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2652 << /*MissingOption=*/true << "";
2653 return;
2654 }
2655
2656 while (Tok.is(tok::identifier)) {
2657 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2658
2659 auto FlagKind =
2660 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2661 OptionInfo->getName())
2662 .Case("contract", TokFPAnnotValue::Contract)
2663 .Default(None);
2664 if (!FlagKind) {
2665 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2666 << /*MissingOption=*/false << OptionInfo;
2667 return;
2668 }
2669 PP.Lex(Tok);
2670
2671 // Read '('
2672 if (Tok.isNot(tok::l_paren)) {
2673 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2674 return;
2675 }
2676 PP.Lex(Tok);
2677
2678 if (Tok.isNot(tok::identifier)) {
2679 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2680 << PP.getSpelling(Tok) << OptionInfo->getName();
2681 return;
2682 }
2683 const IdentifierInfo *II = Tok.getIdentifierInfo();
2684
2685 auto FlagValue =
2686 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2687 II->getName())
2688 .Case("on", TokFPAnnotValue::On)
2689 .Case("off", TokFPAnnotValue::Off)
2690 .Case("fast", TokFPAnnotValue::Fast)
2691 .Default(llvm::None);
2692
2693 if (!FlagValue) {
2694 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2695 << PP.getSpelling(Tok) << OptionInfo->getName();
2696 return;
2697 }
2698 PP.Lex(Tok);
2699
2700 // Read ')'
2701 if (Tok.isNot(tok::r_paren)) {
2702 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2703 return;
2704 }
2705 PP.Lex(Tok);
2706
2707 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2708 TokFPAnnotValue{*FlagKind, *FlagValue};
2709 // Generate the loop hint token.
2710 Token FPTok;
2711 FPTok.startToken();
2712 FPTok.setKind(tok::annot_pragma_fp);
2713 FPTok.setLocation(PragmaName.getLocation());
2714 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2715 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2716 TokenList.push_back(FPTok);
2717 }
2718
2719 if (Tok.isNot(tok::eod)) {
2720 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2721 << "clang fp";
2722 return;
2723 }
2724
2725 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2726 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2727
2728 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2729 /*DisableMacroExpansion=*/false);
2730}
2731
2732void Parser::HandlePragmaFP() {
2733 assert(Tok.is(tok::annot_pragma_fp));
2734 auto *AnnotValue =
2735 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2736
2737 LangOptions::FPContractModeKind FPC;
2738 switch (AnnotValue->FlagValue) {
2739 case TokFPAnnotValue::On:
2740 FPC = LangOptions::FPC_On;
2741 break;
2742 case TokFPAnnotValue::Fast:
2743 FPC = LangOptions::FPC_Fast;
2744 break;
2745 case TokFPAnnotValue::Off:
2746 FPC = LangOptions::FPC_Off;
2747 break;
2748 }
2749
2750 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +00002751 ConsumeAnnotationToken();
Adam Nemet60d32642017-04-04 21:18:36 +00002752}
2753
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002754/// Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002755static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2756 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002757 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002758 SmallVector<Token, 1> ValueList;
2759 int OpenParens = ValueInParens ? 1 : 0;
2760 // Read constant expression.
2761 while (Tok.isNot(tok::eod)) {
2762 if (Tok.is(tok::l_paren))
2763 OpenParens++;
2764 else if (Tok.is(tok::r_paren)) {
2765 OpenParens--;
2766 if (OpenParens == 0 && ValueInParens)
2767 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002768 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002769
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002770 ValueList.push_back(Tok);
2771 PP.Lex(Tok);
2772 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002773
2774 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002775 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002776 if (Tok.isNot(tok::r_paren)) {
2777 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2778 return true;
2779 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002780 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002781 }
2782
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002783 Token EOFTok;
2784 EOFTok.startToken();
2785 EOFTok.setKind(tok::eof);
2786 EOFTok.setLocation(Tok.getLocation());
2787 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2788
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00002789 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002790
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002791 Info.PragmaName = PragmaName;
2792 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002793 return false;
2794}
2795
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002796/// Handle the \#pragma clang loop directive.
Eli Bendersky06a40422014-06-06 20:31:48 +00002797/// #pragma clang 'loop' loop-hints
2798///
2799/// loop-hints:
2800/// loop-hint loop-hints[opt]
2801///
2802/// loop-hint:
2803/// 'vectorize' '(' loop-hint-keyword ')'
2804/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00002805/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002806/// 'vectorize_width' '(' loop-hint-value ')'
2807/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00002808/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002809///
2810/// loop-hint-keyword:
2811/// 'enable'
2812/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00002813/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00002814///
Mark Heffernan450c2382014-07-23 17:31:31 +00002815/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00002816/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00002817/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00002818/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00002819///
Eli Bendersky06a40422014-06-06 20:31:48 +00002820/// loop-hint-value:
2821/// constant-expression
2822///
2823/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2824/// try vectorizing the instructions of the loop it precedes. Specifying
2825/// interleave(enable) or interleave_count(_value_) instructs llvm to try
2826/// interleaving multiple iterations of the loop it precedes. The width of the
2827/// vector instructions is specified by vectorize_width() and the number of
2828/// interleaved loop iterations is specified by interleave_count(). Specifying a
2829/// value of 1 effectively disables vectorization/interleaving, even if it is
2830/// possible and profitable, and 0 is invalid. The loop vectorizer currently
2831/// only works on inner loops.
2832///
Eli Bendersky86483b32014-06-11 17:56:26 +00002833/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00002834/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2835/// completely if the trip count is known at compile time and unroll partially
2836/// if the trip count is not known. Specifying unroll(full) is similar to
2837/// unroll(enable) but will unroll the loop only if the trip count is known at
2838/// compile time. Specifying unroll(disable) disables unrolling for the
2839/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2840/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00002841void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2842 PragmaIntroducerKind Introducer,
2843 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002844 // Incoming token is "loop" from "#pragma clang loop".
2845 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00002846 SmallVector<Token, 1> TokenList;
2847
2848 // Lex the optimization option and verify it is an identifier.
2849 PP.Lex(Tok);
2850 if (Tok.isNot(tok::identifier)) {
2851 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2852 << /*MissingOption=*/true << "";
2853 return;
2854 }
2855
2856 while (Tok.is(tok::identifier)) {
2857 Token Option = Tok;
2858 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2859
Eli Bendersky86483b32014-06-11 17:56:26 +00002860 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002861 .Case("vectorize", true)
2862 .Case("interleave", true)
2863 .Case("unroll", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00002864 .Case("distribute", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002865 .Case("vectorize_width", true)
2866 .Case("interleave_count", true)
2867 .Case("unroll_count", true)
2868 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002869 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002870 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2871 << /*MissingOption=*/false << OptionInfo;
2872 return;
2873 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002874 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002875
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002876 // Read '('
2877 if (Tok.isNot(tok::l_paren)) {
2878 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002879 return;
2880 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002881 PP.Lex(Tok);
2882
2883 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2884 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2885 *Info))
2886 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002887
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002888 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002889 Token LoopHintTok;
2890 LoopHintTok.startToken();
2891 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002892 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002893 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002894 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2895 TokenList.push_back(LoopHintTok);
2896 }
2897
2898 if (Tok.isNot(tok::eod)) {
2899 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2900 << "clang loop";
2901 return;
2902 }
2903
David Blaikie2eabcc92016-02-09 18:52:09 +00002904 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2905 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002906
David Blaikie2eabcc92016-02-09 18:52:09 +00002907 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2908 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002909}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002910
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002911/// Handle the loop unroll optimization pragmas.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002912/// #pragma unroll
2913/// #pragma unroll unroll-hint-value
2914/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002915/// #pragma nounroll
David Greenc8e39242018-08-01 14:36:12 +00002916/// #pragma unroll_and_jam
2917/// #pragma unroll_and_jam unroll-hint-value
2918/// #pragma unroll_and_jam '(' unroll-hint-value ')'
2919/// #pragma nounroll_and_jam
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002920///
2921/// unroll-hint-value:
2922/// constant-expression
2923///
Mark Heffernanc888e412014-07-24 18:09:38 +00002924/// Loop unrolling hints can be specified with '#pragma unroll' or
2925/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2926/// contained in parentheses. With no argument the directive instructs llvm to
2927/// try to unroll the loop completely. A positive integer argument can be
2928/// specified to indicate the number of times the loop should be unrolled. To
2929/// maximize compatibility with other compilers the unroll count argument can be
2930/// specified with or without parentheses. Specifying, '#pragma nounroll'
2931/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002932void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2933 PragmaIntroducerKind Introducer,
2934 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002935 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2936 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002937 Token PragmaName = Tok;
2938 PP.Lex(Tok);
2939 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2940 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002941 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002942 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002943 Info->Option.startToken();
David Greenc8e39242018-08-01 14:36:12 +00002944 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll" ||
2945 PragmaName.getIdentifierInfo()->getName() == "nounroll_and_jam") {
Mark Heffernanc888e412014-07-24 18:09:38 +00002946 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
David Greenc8e39242018-08-01 14:36:12 +00002947 << PragmaName.getIdentifierInfo()->getName();
Mark Heffernanc888e412014-07-24 18:09:38 +00002948 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002949 } else {
2950 // Unroll pragma with an argument: "#pragma unroll N" or
2951 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002952 // Read '(' if it exists.
2953 bool ValueInParens = Tok.is(tok::l_paren);
2954 if (ValueInParens)
2955 PP.Lex(Tok);
2956
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002957 Token Option;
2958 Option.startToken();
2959 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002960 return;
2961
2962 // In CUDA, the argument to '#pragma unroll' should not be contained in
2963 // parentheses.
2964 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002965 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002966 diag::warn_pragma_unroll_cuda_value_in_parens);
2967
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002968 if (Tok.isNot(tok::eod)) {
2969 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2970 << "unroll";
2971 return;
2972 }
2973 }
2974
2975 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002976 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002977 TokenArray[0].startToken();
2978 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2979 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002980 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002981 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002982 PP.EnterTokenStream(std::move(TokenArray), 1,
2983 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002984}
Reid Kleckner3f1ec622016-09-07 16:38:32 +00002985
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002986/// Handle the Microsoft \#pragma intrinsic extension.
Reid Kleckner3f1ec622016-09-07 16:38:32 +00002987///
2988/// The syntax is:
2989/// \code
2990/// #pragma intrinsic(memset)
2991/// #pragma intrinsic(strlen, memcpy)
2992/// \endcode
2993///
2994/// Pragma intrisic tells the compiler to use a builtin version of the
2995/// function. Clang does it anyway, so the pragma doesn't really do anything.
2996/// Anyway, we emit a warning if the function specified in \#pragma intrinsic
2997/// isn't an intrinsic in clang and suggest to include intrin.h.
2998void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
2999 PragmaIntroducerKind Introducer,
3000 Token &Tok) {
3001 PP.Lex(Tok);
3002
3003 if (Tok.isNot(tok::l_paren)) {
3004 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
3005 << "intrinsic";
3006 return;
3007 }
3008 PP.Lex(Tok);
3009
3010 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
3011
3012 while (Tok.is(tok::identifier)) {
3013 IdentifierInfo *II = Tok.getIdentifierInfo();
3014 if (!II->getBuiltinID())
3015 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
3016 << II << SuggestIntrinH;
3017
3018 PP.Lex(Tok);
3019 if (Tok.isNot(tok::comma))
3020 break;
3021 PP.Lex(Tok);
3022 }
3023
3024 if (Tok.isNot(tok::r_paren)) {
3025 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
3026 << "intrinsic";
3027 return;
3028 }
3029 PP.Lex(Tok);
3030
3031 if (Tok.isNot(tok::eod))
3032 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3033 << "intrinsic";
3034}
Hans Wennborg1bbe00e2018-03-20 08:53:11 +00003035
3036// #pragma optimize("gsty", on|off)
3037void PragmaMSOptimizeHandler::HandlePragma(Preprocessor &PP,
3038 PragmaIntroducerKind Introducer,
3039 Token &Tok) {
3040 SourceLocation StartLoc = Tok.getLocation();
3041 PP.Lex(Tok);
3042
3043 if (Tok.isNot(tok::l_paren)) {
3044 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "optimize";
3045 return;
3046 }
3047 PP.Lex(Tok);
3048
3049 if (Tok.isNot(tok::string_literal)) {
3050 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_string) << "optimize";
3051 return;
3052 }
3053 // We could syntax check the string but it's probably not worth the effort.
3054 PP.Lex(Tok);
3055
3056 if (Tok.isNot(tok::comma)) {
3057 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_comma) << "optimize";
3058 return;
3059 }
3060 PP.Lex(Tok);
3061
3062 if (Tok.is(tok::eod) || Tok.is(tok::r_paren)) {
3063 PP.Diag(Tok.getLocation(), diag::warn_pragma_missing_argument)
3064 << "optimize" << /*Expected=*/true << "'on' or 'off'";
3065 return;
3066 }
3067 IdentifierInfo *II = Tok.getIdentifierInfo();
3068 if (!II || (!II->isStr("on") && !II->isStr("off"))) {
3069 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
3070 << PP.getSpelling(Tok) << "optimize" << /*Expected=*/true
3071 << "'on' or 'off'";
3072 return;
3073 }
3074 PP.Lex(Tok);
3075
3076 if (Tok.isNot(tok::r_paren)) {
3077 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "optimize";
3078 return;
3079 }
3080 PP.Lex(Tok);
3081
3082 if (Tok.isNot(tok::eod)) {
3083 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3084 << "optimize";
3085 return;
3086 }
3087 PP.Diag(StartLoc, diag::warn_pragma_optimize);
3088}
3089
Justin Lebar67a78a62016-10-08 22:15:58 +00003090void PragmaForceCUDAHostDeviceHandler::HandlePragma(
3091 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
3092 Token FirstTok = Tok;
3093
3094 PP.Lex(Tok);
3095 IdentifierInfo *Info = Tok.getIdentifierInfo();
3096 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
3097 PP.Diag(FirstTok.getLocation(),
3098 diag::warn_pragma_force_cuda_host_device_bad_arg);
3099 return;
3100 }
3101
3102 if (Info->isStr("begin"))
3103 Actions.PushForceCUDAHostDevice();
3104 else if (!Actions.PopForceCUDAHostDevice())
3105 PP.Diag(FirstTok.getLocation(),
3106 diag::err_pragma_cannot_end_force_cuda_host_device);
3107
3108 PP.Lex(Tok);
3109 if (!Tok.is(tok::eod))
3110 PP.Diag(FirstTok.getLocation(),
3111 diag::warn_pragma_force_cuda_host_device_bad_arg);
3112}
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003113
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003114/// Handle the #pragma clang attribute directive.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003115///
3116/// The syntax is:
3117/// \code
3118/// #pragma clang attribute push(attribute, subject-set)
Erik Pilkington7d180942018-10-29 17:38:42 +00003119/// #pragma clang attribute push
3120/// #pragma clang attribute (attribute, subject-set)
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003121/// #pragma clang attribute pop
3122/// \endcode
3123///
3124/// The subject-set clause defines the set of declarations which receive the
3125/// attribute. Its exact syntax is described in the LanguageExtensions document
3126/// in Clang's documentation.
3127///
3128/// This directive instructs the compiler to begin/finish applying the specified
3129/// attribute to the set of attribute-specific declarations in the active range
3130/// of the pragma.
3131void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
3132 PragmaIntroducerKind Introducer,
3133 Token &FirstToken) {
3134 Token Tok;
3135 PP.Lex(Tok);
3136 auto *Info = new (PP.getPreprocessorAllocator())
3137 PragmaAttributeInfo(AttributesForPragmaAttribute);
3138
Erik Pilkington7d180942018-10-29 17:38:42 +00003139 if (!Tok.isOneOf(tok::identifier, tok::l_paren)) {
3140 PP.Diag(Tok.getLocation(),
3141 diag::err_pragma_attribute_expected_push_pop_paren);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003142 return;
3143 }
Erik Pilkington7d180942018-10-29 17:38:42 +00003144
3145 // Determine what action this pragma clang attribute represents.
3146 if (Tok.is(tok::l_paren))
3147 Info->Action = PragmaAttributeInfo::Attribute;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003148 else {
Erik Pilkington7d180942018-10-29 17:38:42 +00003149 const IdentifierInfo *II = Tok.getIdentifierInfo();
3150 if (II->isStr("push"))
3151 Info->Action = PragmaAttributeInfo::Push;
3152 else if (II->isStr("pop"))
3153 Info->Action = PragmaAttributeInfo::Pop;
3154 else {
3155 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
3156 << PP.getSpelling(Tok);
3157 return;
3158 }
3159
3160 PP.Lex(Tok);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003161 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003162
3163 // Parse the actual attribute.
Erik Pilkington7d180942018-10-29 17:38:42 +00003164 if ((Info->Action == PragmaAttributeInfo::Push && Tok.isNot(tok::eod)) ||
3165 Info->Action == PragmaAttributeInfo::Attribute) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003166 if (Tok.isNot(tok::l_paren)) {
3167 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
3168 return;
3169 }
3170 PP.Lex(Tok);
3171
3172 // Lex the attribute tokens.
3173 SmallVector<Token, 16> AttributeTokens;
3174 int OpenParens = 1;
3175 while (Tok.isNot(tok::eod)) {
3176 if (Tok.is(tok::l_paren))
3177 OpenParens++;
3178 else if (Tok.is(tok::r_paren)) {
3179 OpenParens--;
3180 if (OpenParens == 0)
3181 break;
3182 }
3183
3184 AttributeTokens.push_back(Tok);
3185 PP.Lex(Tok);
3186 }
3187
3188 if (AttributeTokens.empty()) {
3189 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
3190 return;
3191 }
3192 if (Tok.isNot(tok::r_paren)) {
3193 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
3194 return;
3195 }
3196 SourceLocation EndLoc = Tok.getLocation();
3197 PP.Lex(Tok);
3198
3199 // Terminate the attribute for parsing.
3200 Token EOFTok;
3201 EOFTok.startToken();
3202 EOFTok.setKind(tok::eof);
3203 EOFTok.setLocation(EndLoc);
3204 AttributeTokens.push_back(EOFTok);
3205
3206 Info->Tokens =
3207 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
3208 }
3209
3210 if (Tok.isNot(tok::eod))
3211 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3212 << "clang attribute";
3213
3214 // Generate the annotated pragma token.
3215 auto TokenArray = llvm::make_unique<Token[]>(1);
3216 TokenArray[0].startToken();
3217 TokenArray[0].setKind(tok::annot_pragma_attribute);
3218 TokenArray[0].setLocation(FirstToken.getLocation());
3219 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
3220 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
3221 PP.EnterTokenStream(std::move(TokenArray), 1,
3222 /*DisableMacroExpansion=*/false);
3223}