blob: c8de6b35f9ef1d6f3855e87d6a23af3a4da3f104 [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
52struct PragmaMSStructHandler : public PragmaHandler {
53 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000054 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
55 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000056};
57
58struct PragmaUnusedHandler : public PragmaHandler {
59 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000060 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
61 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000062};
63
64struct PragmaWeakHandler : public PragmaHandler {
65 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000066 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
67 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000068};
69
70struct PragmaRedefineExtnameHandler : public PragmaHandler {
71 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000072 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
73 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000074};
75
76struct PragmaOpenCLExtensionHandler : public PragmaHandler {
77 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000078 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
79 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000080};
81
82
83struct PragmaFPContractHandler : public PragmaHandler {
84 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000085 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
86 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000087};
88
Adam Nemet60d32642017-04-04 21:18:36 +000089struct PragmaFPHandler : public PragmaHandler {
90 PragmaFPHandler() : PragmaHandler("fp") {}
91 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
92 Token &FirstToken) override;
93};
94
Reid Kleckner5b086462014-02-20 22:52:09 +000095struct PragmaNoOpenMPHandler : public PragmaHandler {
96 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000097 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
98 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000099};
100
101struct PragmaOpenMPHandler : public PragmaHandler {
102 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +0000103 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
104 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000105};
106
107/// PragmaCommentHandler - "\#pragma comment ...".
108struct PragmaCommentHandler : public PragmaHandler {
109 PragmaCommentHandler(Sema &Actions)
110 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000111 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
112 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000113private:
114 Sema &Actions;
115};
116
117struct PragmaDetectMismatchHandler : public PragmaHandler {
118 PragmaDetectMismatchHandler(Sema &Actions)
119 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000120 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
121 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000122private:
123 Sema &Actions;
124};
125
126struct PragmaMSPointersToMembers : public PragmaHandler {
127 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000128 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
129 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000130};
131
132struct PragmaMSVtorDisp : public PragmaHandler {
133 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000134 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
135 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000136};
137
Warren Huntc3b18962014-04-08 22:30:47 +0000138struct PragmaMSPragma : public PragmaHandler {
139 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000140 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
141 Token &FirstToken) override;
142};
143
Dario Domizioli13a0a382014-05-23 12:13:25 +0000144/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
145struct PragmaOptimizeHandler : public PragmaHandler {
146 PragmaOptimizeHandler(Sema &S)
147 : PragmaHandler("optimize"), Actions(S) {}
148 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
149 Token &FirstToken) override;
150private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000151 Sema &Actions;
152};
153
154struct PragmaLoopHintHandler : public PragmaHandler {
155 PragmaLoopHintHandler() : PragmaHandler("loop") {}
156 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
157 Token &FirstToken) override;
158};
159
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000160struct PragmaUnrollHintHandler : public PragmaHandler {
161 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
162 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
163 Token &FirstToken) override;
164};
165
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000166struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
167 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
168};
169
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000170struct PragmaMSIntrinsicHandler : public PragmaHandler {
171 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
172 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
173 Token &FirstToken) override;
174};
175
Justin Lebar67a78a62016-10-08 22:15:58 +0000176struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
177 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
178 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
179 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
180 Token &FirstToken) override;
181
182private:
183 Sema &Actions;
184};
185
Eli Bendersky06a40422014-06-06 20:31:48 +0000186} // end namespace
187
188void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000189 AlignHandler.reset(new PragmaAlignHandler());
190 PP.AddPragmaHandler(AlignHandler.get());
191
192 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
193 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
194
195 OptionsHandler.reset(new PragmaOptionsHandler());
196 PP.AddPragmaHandler(OptionsHandler.get());
197
198 PackHandler.reset(new PragmaPackHandler());
199 PP.AddPragmaHandler(PackHandler.get());
200
201 MSStructHandler.reset(new PragmaMSStructHandler());
202 PP.AddPragmaHandler(MSStructHandler.get());
203
204 UnusedHandler.reset(new PragmaUnusedHandler());
205 PP.AddPragmaHandler(UnusedHandler.get());
206
207 WeakHandler.reset(new PragmaWeakHandler());
208 PP.AddPragmaHandler(WeakHandler.get());
209
210 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
211 PP.AddPragmaHandler(RedefineExtnameHandler.get());
212
213 FPContractHandler.reset(new PragmaFPContractHandler());
214 PP.AddPragmaHandler("STDC", FPContractHandler.get());
215
216 if (getLangOpts().OpenCL) {
217 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
218 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
219
220 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
221 }
222 if (getLangOpts().OpenMP)
223 OpenMPHandler.reset(new PragmaOpenMPHandler());
224 else
225 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
226 PP.AddPragmaHandler(OpenMPHandler.get());
227
Yunzhong Gao99efc032015-03-23 20:41:42 +0000228 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000229 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
230 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000231 }
232
233 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000234 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
235 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
236 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
237 PP.AddPragmaHandler(MSPointersToMembers.get());
238 MSVtorDisp.reset(new PragmaMSVtorDisp());
239 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000240 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000241 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000242 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
243 PP.AddPragmaHandler(MSDataSeg.get());
244 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
245 PP.AddPragmaHandler(MSBSSSeg.get());
246 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
247 PP.AddPragmaHandler(MSConstSeg.get());
248 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
249 PP.AddPragmaHandler(MSCodeSeg.get());
250 MSSection.reset(new PragmaMSPragma("section"));
251 PP.AddPragmaHandler(MSSection.get());
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000252 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
253 PP.AddPragmaHandler(MSRuntimeChecks.get());
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000254 MSIntrinsic.reset(new PragmaMSIntrinsicHandler());
255 PP.AddPragmaHandler(MSIntrinsic.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000256 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000257
Justin Lebar67a78a62016-10-08 22:15:58 +0000258 if (getLangOpts().CUDA) {
259 CUDAForceHostDeviceHandler.reset(
260 new PragmaForceCUDAHostDeviceHandler(Actions));
261 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
262 }
263
Eli Bendersky06a40422014-06-06 20:31:48 +0000264 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
265 PP.AddPragmaHandler("clang", OptimizeHandler.get());
266
267 LoopHintHandler.reset(new PragmaLoopHintHandler());
268 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000269
270 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
271 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000272
273 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
274 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Adam Nemet60d32642017-04-04 21:18:36 +0000275
276 FPHandler.reset(new PragmaFPHandler());
277 PP.AddPragmaHandler("clang", FPHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000278}
279
280void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000281 // Remove the pragma handlers we installed.
282 PP.RemovePragmaHandler(AlignHandler.get());
283 AlignHandler.reset();
284 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
285 GCCVisibilityHandler.reset();
286 PP.RemovePragmaHandler(OptionsHandler.get());
287 OptionsHandler.reset();
288 PP.RemovePragmaHandler(PackHandler.get());
289 PackHandler.reset();
290 PP.RemovePragmaHandler(MSStructHandler.get());
291 MSStructHandler.reset();
292 PP.RemovePragmaHandler(UnusedHandler.get());
293 UnusedHandler.reset();
294 PP.RemovePragmaHandler(WeakHandler.get());
295 WeakHandler.reset();
296 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
297 RedefineExtnameHandler.reset();
298
299 if (getLangOpts().OpenCL) {
300 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
301 OpenCLExtensionHandler.reset();
302 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
303 }
304 PP.RemovePragmaHandler(OpenMPHandler.get());
305 OpenMPHandler.reset();
306
Yunzhong Gao99efc032015-03-23 20:41:42 +0000307 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000308 PP.RemovePragmaHandler(MSCommentHandler.get());
309 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000310 }
311
312 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000313 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
314 MSDetectMismatchHandler.reset();
315 PP.RemovePragmaHandler(MSPointersToMembers.get());
316 MSPointersToMembers.reset();
317 PP.RemovePragmaHandler(MSVtorDisp.get());
318 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000319 PP.RemovePragmaHandler(MSInitSeg.get());
320 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000321 PP.RemovePragmaHandler(MSDataSeg.get());
322 MSDataSeg.reset();
323 PP.RemovePragmaHandler(MSBSSSeg.get());
324 MSBSSSeg.reset();
325 PP.RemovePragmaHandler(MSConstSeg.get());
326 MSConstSeg.reset();
327 PP.RemovePragmaHandler(MSCodeSeg.get());
328 MSCodeSeg.reset();
329 PP.RemovePragmaHandler(MSSection.get());
330 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000331 PP.RemovePragmaHandler(MSRuntimeChecks.get());
332 MSRuntimeChecks.reset();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000333 PP.RemovePragmaHandler(MSIntrinsic.get());
334 MSIntrinsic.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000335 }
336
Justin Lebar67a78a62016-10-08 22:15:58 +0000337 if (getLangOpts().CUDA) {
338 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
339 CUDAForceHostDeviceHandler.reset();
340 }
341
Reid Kleckner5b086462014-02-20 22:52:09 +0000342 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
343 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000344
345 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
346 OptimizeHandler.reset();
347
348 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
349 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000350
351 PP.RemovePragmaHandler(UnrollHintHandler.get());
352 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000353
354 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
355 NoUnrollHintHandler.reset();
Adam Nemet60d32642017-04-04 21:18:36 +0000356
357 PP.RemovePragmaHandler("clang", FPHandler.get());
358 FPHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000359}
360
361/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000362///
363/// Each annot_pragma_unused is followed by the argument token so e.g.
364/// "#pragma unused(x,y)" becomes:
365/// annot_pragma_unused 'x' annot_pragma_unused 'y'
366void Parser::HandlePragmaUnused() {
367 assert(Tok.is(tok::annot_pragma_unused));
368 SourceLocation UnusedLoc = ConsumeToken();
369 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
370 ConsumeToken(); // The argument token.
371}
Eli Friedman570024a2010-08-05 06:57:20 +0000372
Rafael Espindola273fd772012-01-26 02:02:57 +0000373void Parser::HandlePragmaVisibility() {
374 assert(Tok.is(tok::annot_pragma_vis));
375 const IdentifierInfo *VisType =
376 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
377 SourceLocation VisLoc = ConsumeToken();
378 Actions.ActOnPragmaVisibility(VisType, VisLoc);
379}
380
Benjamin Kramere003ca22015-10-28 13:54:16 +0000381namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000382struct PragmaPackInfo {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000383 Sema::PragmaMsStackAction Action;
384 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000385 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000386};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000387} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000388
389void Parser::HandlePragmaPack() {
390 assert(Tok.is(tok::annot_pragma_pack));
391 PragmaPackInfo *Info =
392 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
393 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000394 ExprResult Alignment;
395 if (Info->Alignment.is(tok::numeric_constant)) {
396 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
397 if (Alignment.isInvalid())
398 return;
399 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000400 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
401 Alignment.get());
Eli Friedmanec52f922012-02-23 23:47:16 +0000402}
403
Eli Friedman68be1642012-10-04 02:36:51 +0000404void Parser::HandlePragmaMSStruct() {
405 assert(Tok.is(tok::annot_pragma_msstruct));
Nico Weber779355f2016-03-02 23:22:00 +0000406 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
407 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Eli Friedman68be1642012-10-04 02:36:51 +0000408 Actions.ActOnPragmaMSStruct(Kind);
409 ConsumeToken(); // The annotation token.
410}
411
412void Parser::HandlePragmaAlign() {
413 assert(Tok.is(tok::annot_pragma_align));
414 Sema::PragmaOptionsAlignKind Kind =
415 static_cast<Sema::PragmaOptionsAlignKind>(
416 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
417 SourceLocation PragmaLoc = ConsumeToken();
418 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
419}
420
Richard Smithba3a4f92016-01-12 21:59:26 +0000421void Parser::HandlePragmaDump() {
422 assert(Tok.is(tok::annot_pragma_dump));
423 IdentifierInfo *II =
424 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
425 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
426 ConsumeToken();
427}
428
Eli Friedman68be1642012-10-04 02:36:51 +0000429void Parser::HandlePragmaWeak() {
430 assert(Tok.is(tok::annot_pragma_weak));
431 SourceLocation PragmaLoc = ConsumeToken();
432 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
433 Tok.getLocation());
434 ConsumeToken(); // The weak name.
435}
436
437void Parser::HandlePragmaWeakAlias() {
438 assert(Tok.is(tok::annot_pragma_weakalias));
439 SourceLocation PragmaLoc = ConsumeToken();
440 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
441 SourceLocation WeakNameLoc = Tok.getLocation();
442 ConsumeToken();
443 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
444 SourceLocation AliasNameLoc = Tok.getLocation();
445 ConsumeToken();
446 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
447 WeakNameLoc, AliasNameLoc);
448
449}
450
451void Parser::HandlePragmaRedefineExtname() {
452 assert(Tok.is(tok::annot_pragma_redefine_extname));
453 SourceLocation RedefLoc = ConsumeToken();
454 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
455 SourceLocation RedefNameLoc = Tok.getLocation();
456 ConsumeToken();
457 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
458 SourceLocation AliasNameLoc = Tok.getLocation();
459 ConsumeToken();
460 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
461 RedefNameLoc, AliasNameLoc);
462}
463
464void Parser::HandlePragmaFPContract() {
465 assert(Tok.is(tok::annot_pragma_fp_contract));
466 tok::OnOffSwitch OOS =
467 static_cast<tok::OnOffSwitch>(
468 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Adam Nemet60d32642017-04-04 21:18:36 +0000469
470 LangOptions::FPContractModeKind FPC;
471 switch (OOS) {
472 case tok::OOS_ON:
473 FPC = LangOptions::FPC_On;
474 break;
475 case tok::OOS_OFF:
476 FPC = LangOptions::FPC_Off;
477 break;
478 case tok::OOS_DEFAULT:
479 FPC = getLangOpts().getDefaultFPContractMode();
480 break;
481 }
482
483 Actions.ActOnPragmaFPContract(FPC);
Eli Friedman68be1642012-10-04 02:36:51 +0000484 ConsumeToken(); // The annotation token.
485}
486
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000487StmtResult Parser::HandlePragmaCaptured()
488{
489 assert(Tok.is(tok::annot_pragma_captured));
490 ConsumeToken();
491
492 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000493 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000494 return StmtError();
495 }
496
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000497 SourceLocation Loc = Tok.getLocation();
498
499 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000500 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
501 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000502
503 StmtResult R = ParseCompoundStatement();
504 CapturedRegionScope.Exit();
505
506 if (R.isInvalid()) {
507 Actions.ActOnCapturedRegionError();
508 return StmtError();
509 }
510
511 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000512}
513
Eli Friedman68be1642012-10-04 02:36:51 +0000514namespace {
Yaxun Liu5b746652016-12-18 05:18:55 +0000515 enum OpenCLExtState : char {
516 Disable, Enable, Begin, End
517 };
518 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
Eli Friedman68be1642012-10-04 02:36:51 +0000519}
520
521void Parser::HandlePragmaOpenCLExtension() {
522 assert(Tok.is(tok::annot_pragma_opencl_extension));
Yaxun Liu5b746652016-12-18 05:18:55 +0000523 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
524 auto State = Data->second;
525 auto Ident = Data->first;
Eli Friedman68be1642012-10-04 02:36:51 +0000526 SourceLocation NameLoc = Tok.getLocation();
527 ConsumeToken(); // The annotation token.
528
Yaxun Liu5b746652016-12-18 05:18:55 +0000529 auto &Opt = Actions.getOpenCLOptions();
530 auto Name = Ident->getName();
Eli Friedman68be1642012-10-04 02:36:51 +0000531 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
532 // overriding all previously issued extension directives, but only if the
533 // behavior is set to disable."
Yaxun Liu5b746652016-12-18 05:18:55 +0000534 if (Name == "all") {
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000535 if (State == Disable) {
Yaxun Liu5b746652016-12-18 05:18:55 +0000536 Opt.disableAll();
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000537 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
538 } else {
Yaxun Liu5b746652016-12-18 05:18:55 +0000539 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000540 }
Yaxun Liu5b746652016-12-18 05:18:55 +0000541 } else if (State == Begin) {
542 if (!Opt.isKnown(Name) ||
543 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
544 Opt.support(Name);
545 }
546 Actions.setCurrentOpenCLExtension(Name);
547 } else if (State == End) {
548 if (Name != Actions.getCurrentOpenCLExtension())
549 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
550 Actions.setCurrentOpenCLExtension("");
551 } else if (!Opt.isKnown(Name))
552 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
553 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
554 Opt.enable(Name, State == Enable);
555 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
556 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
557 else
558 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
Eli Friedman68be1642012-10-04 02:36:51 +0000559}
560
David Majnemer4bb09802014-02-10 19:50:15 +0000561void Parser::HandlePragmaMSPointersToMembers() {
562 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000563 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
564 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000565 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
566 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
567 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
568}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000569
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000570void Parser::HandlePragmaMSVtorDisp() {
571 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
572 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
Denis Zobnin2290dac2016-04-29 11:27:00 +0000573 Sema::PragmaMsStackAction Action =
574 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000575 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
576 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
Denis Zobnin2290dac2016-04-29 11:27:00 +0000577 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000578}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000579
Warren Huntc3b18962014-04-08 22:30:47 +0000580void Parser::HandlePragmaMSPragma() {
581 assert(Tok.is(tok::annot_pragma_ms_pragma));
582 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000583 auto TheTokens =
584 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
585 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Warren Huntc3b18962014-04-08 22:30:47 +0000586 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
587 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000588 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000589 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000590
Warren Huntc3b18962014-04-08 22:30:47 +0000591 // Figure out which #pragma we're dealing with. The switch has no default
592 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000593 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000594 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
595 .Case("data_seg", &Parser::HandlePragmaMSSegment)
596 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
597 .Case("const_seg", &Parser::HandlePragmaMSSegment)
598 .Case("code_seg", &Parser::HandlePragmaMSSegment)
599 .Case("section", &Parser::HandlePragmaMSSection)
600 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000601
602 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
603 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
604 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000605 while (Tok.isNot(tok::eof))
606 PP.Lex(Tok);
607 PP.Lex(Tok);
608 }
609}
610
Reid Kleckner722b1df2014-07-18 00:13:16 +0000611bool Parser::HandlePragmaMSSection(StringRef PragmaName,
612 SourceLocation PragmaLocation) {
613 if (Tok.isNot(tok::l_paren)) {
614 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
615 return false;
616 }
Warren Huntc3b18962014-04-08 22:30:47 +0000617 PP.Lex(Tok); // (
618 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000619 if (Tok.isNot(tok::string_literal)) {
620 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
621 << PragmaName;
622 return false;
623 }
624 ExprResult StringResult = ParseStringLiteralExpression();
625 if (StringResult.isInvalid())
626 return false; // Already diagnosed.
627 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
628 if (SegmentName->getCharByteWidth() != 1) {
629 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
630 << PragmaName;
631 return false;
632 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000633 int SectionFlags = ASTContext::PSF_Read;
634 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000635 while (Tok.is(tok::comma)) {
636 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000637 // Ignore "long" and "short".
638 // They are undocumented, but widely used, section attributes which appear
639 // to do nothing.
640 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
641 PP.Lex(Tok); // long/short
642 continue;
643 }
644
Reid Kleckner722b1df2014-07-18 00:13:16 +0000645 if (!Tok.isAnyIdentifier()) {
646 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
647 << PragmaName;
648 return false;
649 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000650 ASTContext::PragmaSectionFlag Flag =
651 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000652 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000653 .Case("read", ASTContext::PSF_Read)
654 .Case("write", ASTContext::PSF_Write)
655 .Case("execute", ASTContext::PSF_Execute)
656 .Case("shared", ASTContext::PSF_Invalid)
657 .Case("nopage", ASTContext::PSF_Invalid)
658 .Case("nocache", ASTContext::PSF_Invalid)
659 .Case("discard", ASTContext::PSF_Invalid)
660 .Case("remove", ASTContext::PSF_Invalid)
661 .Default(ASTContext::PSF_None);
662 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
663 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000664 ? diag::warn_pragma_invalid_specific_action
665 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000666 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000667 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000668 }
669 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000670 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000671 PP.Lex(Tok); // Identifier
672 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000673 // If no section attributes are specified, the section will be marked as
674 // read/write.
675 if (SectionFlagsAreDefault)
676 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000677 if (Tok.isNot(tok::r_paren)) {
678 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
679 return false;
680 }
Warren Huntc3b18962014-04-08 22:30:47 +0000681 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000682 if (Tok.isNot(tok::eof)) {
683 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
684 << PragmaName;
685 return false;
686 }
Warren Huntc3b18962014-04-08 22:30:47 +0000687 PP.Lex(Tok); // eof
688 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000689 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000690}
691
Reid Kleckner722b1df2014-07-18 00:13:16 +0000692bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
693 SourceLocation PragmaLocation) {
694 if (Tok.isNot(tok::l_paren)) {
695 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
696 return false;
697 }
Warren Huntc3b18962014-04-08 22:30:47 +0000698 PP.Lex(Tok); // (
699 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000700 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000701 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000702 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000703 if (PushPop == "push")
704 Action = Sema::PSK_Push;
705 else if (PushPop == "pop")
706 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000707 else {
708 PP.Diag(PragmaLocation,
709 diag::warn_pragma_expected_section_push_pop_or_name)
710 << PragmaName;
711 return false;
712 }
Warren Huntc3b18962014-04-08 22:30:47 +0000713 if (Action != Sema::PSK_Reset) {
714 PP.Lex(Tok); // push | pop
715 if (Tok.is(tok::comma)) {
716 PP.Lex(Tok); // ,
717 // If we've got a comma, we either need a label or a string.
718 if (Tok.isAnyIdentifier()) {
719 SlotLabel = Tok.getIdentifierInfo()->getName();
720 PP.Lex(Tok); // identifier
721 if (Tok.is(tok::comma))
722 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000723 else if (Tok.isNot(tok::r_paren)) {
724 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
725 << PragmaName;
726 return false;
727 }
Warren Huntc3b18962014-04-08 22:30:47 +0000728 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000729 } else if (Tok.isNot(tok::r_paren)) {
730 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
731 return false;
732 }
Warren Huntc3b18962014-04-08 22:30:47 +0000733 }
734 }
735 // Grab the string literal for our section name.
736 StringLiteral *SegmentName = nullptr;
737 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000738 if (Tok.isNot(tok::string_literal)) {
739 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000740 diag::warn_pragma_expected_section_name :
741 diag::warn_pragma_expected_section_label_or_name :
742 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000743 PP.Diag(PragmaLocation, DiagID) << PragmaName;
744 return false;
745 }
746 ExprResult StringResult = ParseStringLiteralExpression();
747 if (StringResult.isInvalid())
748 return false; // Already diagnosed.
749 SegmentName = cast<StringLiteral>(StringResult.get());
750 if (SegmentName->getCharByteWidth() != 1) {
751 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
752 << PragmaName;
753 return false;
754 }
Warren Huntc3b18962014-04-08 22:30:47 +0000755 // Setting section "" has no effect
756 if (SegmentName->getLength())
757 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
758 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000759 if (Tok.isNot(tok::r_paren)) {
760 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
761 return false;
762 }
Warren Huntc3b18962014-04-08 22:30:47 +0000763 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000764 if (Tok.isNot(tok::eof)) {
765 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
766 << PragmaName;
767 return false;
768 }
Warren Huntc3b18962014-04-08 22:30:47 +0000769 PP.Lex(Tok); // eof
770 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
771 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000772 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000773}
774
Reid Kleckner1a711b12014-07-22 00:53:05 +0000775// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000776bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
777 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000778 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
779 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
780 return false;
781 }
782
Reid Kleckner1a711b12014-07-22 00:53:05 +0000783 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
784 PragmaName))
785 return false;
786
787 // Parse either the known section names or the string section name.
788 StringLiteral *SegmentName = nullptr;
789 if (Tok.isAnyIdentifier()) {
790 auto *II = Tok.getIdentifierInfo();
791 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
792 .Case("compiler", "\".CRT$XCC\"")
793 .Case("lib", "\".CRT$XCL\"")
794 .Case("user", "\".CRT$XCU\"")
795 .Default("");
796
797 if (!Section.empty()) {
798 // Pretend the user wrote the appropriate string literal here.
799 Token Toks[1];
800 Toks[0].startToken();
801 Toks[0].setKind(tok::string_literal);
802 Toks[0].setLocation(Tok.getLocation());
803 Toks[0].setLiteralData(Section.data());
804 Toks[0].setLength(Section.size());
805 SegmentName =
806 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
807 PP.Lex(Tok);
808 }
809 } else if (Tok.is(tok::string_literal)) {
810 ExprResult StringResult = ParseStringLiteralExpression();
811 if (StringResult.isInvalid())
812 return false;
813 SegmentName = cast<StringLiteral>(StringResult.get());
814 if (SegmentName->getCharByteWidth() != 1) {
815 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
816 << PragmaName;
817 return false;
818 }
819 // FIXME: Add support for the '[, func-name]' part of the pragma.
820 }
821
822 if (!SegmentName) {
823 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
824 return false;
825 }
826
827 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
828 PragmaName) ||
829 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
830 PragmaName))
831 return false;
832
833 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
834 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000835}
836
Benjamin Kramere003ca22015-10-28 13:54:16 +0000837namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000838struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000839 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000840 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000841 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000842};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000843} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +0000844
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000845static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
846 std::string PragmaString;
847 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
848 PragmaString = "clang loop ";
849 PragmaString += Option.getIdentifierInfo()->getName();
850 } else {
851 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
852 "Unexpected pragma name");
853 PragmaString = "unroll";
854 }
855 return PragmaString;
856}
857
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000858bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000859 assert(Tok.is(tok::annot_pragma_loop_hint));
860 PragmaLoopHintInfo *Info =
861 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
862
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000863 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
864 Hint.PragmaNameLoc = IdentifierLoc::create(
865 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000866
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000867 // It is possible that the loop hint has no option identifier, such as
868 // #pragma unroll(4).
869 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
870 ? Info->Option.getIdentifierInfo()
871 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000872 Hint.OptionLoc = IdentifierLoc::create(
873 Actions.Context, Info->Option.getLocation(), OptionInfo);
874
David Blaikie2eabcc92016-02-09 18:52:09 +0000875 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000876
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000877 // Return a valid hint if pragma unroll or nounroll were specified
878 // without an argument.
879 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
880 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Blaikie2eabcc92016-02-09 18:52:09 +0000881 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000882 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000883 Hint.Range = Info->PragmaName.getLocation();
884 return true;
885 }
886
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000887 // The constant expression is always followed by an eof token, which increases
888 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +0000889 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000890 "PragmaLoopHintInfo::Toks must contain at least one token.");
891
892 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000893 bool OptionUnroll = false;
Adam Nemet2de463e2016-06-14 12:04:26 +0000894 bool OptionDistribute = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000895 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000896 if (OptionInfo) { // Pragma Unroll does not specify an option.
897 OptionUnroll = OptionInfo->isStr("unroll");
Adam Nemet2de463e2016-06-14 12:04:26 +0000898 OptionDistribute = OptionInfo->isStr("distribute");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000899 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
900 .Case("vectorize", true)
901 .Case("interleave", true)
Adam Nemet2de463e2016-06-14 12:04:26 +0000902 .Default(false) ||
903 OptionUnroll || OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000904 }
905
Adam Nemet2de463e2016-06-14 12:04:26 +0000906 bool AssumeSafetyArg = !OptionUnroll && !OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000907 // Verify loop hint has an argument.
908 if (Toks[0].is(tok::eof)) {
909 ConsumeToken(); // The annotation token.
910 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Adam Nemet2de463e2016-06-14 12:04:26 +0000911 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll
912 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000913 return false;
914 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000915
916 // Validate the argument.
917 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000918 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000919 SourceLocation StateLoc = Toks[0].getLocation();
920 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Adam Nemet50de4e82016-04-19 22:17:45 +0000921
922 bool Valid = StateInfo &&
923 llvm::StringSwitch<bool>(StateInfo->getName())
924 .Cases("enable", "disable", true)
925 .Case("full", OptionUnroll)
Adam Nemet2de463e2016-06-14 12:04:26 +0000926 .Case("assume_safety", AssumeSafetyArg)
Adam Nemet50de4e82016-04-19 22:17:45 +0000927 .Default(false);
928 if (!Valid) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000929 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
Adam Nemet2de463e2016-06-14 12:04:26 +0000930 << /*FullKeyword=*/OptionUnroll
931 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000932 return false;
933 }
David Blaikie2eabcc92016-02-09 18:52:09 +0000934 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000935 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
936 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000937 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
938 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000939 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000940 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000941 ConsumeToken(); // The annotation token.
942
943 ExprResult R = ParseConstantExpression();
944
945 // Tokens following an error in an ill-formed constant expression will
946 // remain in the token stream and must be removed.
947 if (Tok.isNot(tok::eof)) {
948 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
949 << PragmaLoopHintString(Info->PragmaName, Info->Option);
950 while (Tok.isNot(tok::eof))
951 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000952 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000953
954 ConsumeToken(); // Consume the constant expression eof terminator.
955
956 if (R.isInvalid() ||
957 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
958 return false;
959
960 // Argument is a constant expression with an integer type.
961 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000962 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000963
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000964 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +0000965 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000966 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000967}
968
969// #pragma GCC visibility comes in two variants:
970// 'push' '(' [visibility] ')'
971// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000972void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
973 PragmaIntroducerKind Introducer,
974 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000975 SourceLocation VisLoc = VisTok.getLocation();
976
977 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000978 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000979
980 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
981
Eli Friedman570024a2010-08-05 06:57:20 +0000982 const IdentifierInfo *VisType;
983 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000984 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000985 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000986 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000987 if (Tok.isNot(tok::l_paren)) {
988 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
989 << "visibility";
990 return;
991 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000992 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000993 VisType = Tok.getIdentifierInfo();
994 if (!VisType) {
995 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
996 << "visibility";
997 return;
998 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000999 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001000 if (Tok.isNot(tok::r_paren)) {
1001 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1002 << "visibility";
1003 return;
1004 }
1005 } else {
1006 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1007 << "visibility";
1008 return;
1009 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001010 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001011 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001012 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +00001013 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1014 << "visibility";
1015 return;
1016 }
1017
David Blaikie2eabcc92016-02-09 18:52:09 +00001018 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +00001019 Toks[0].startToken();
1020 Toks[0].setKind(tok::annot_pragma_vis);
1021 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001022 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +00001023 Toks[0].setAnnotationValue(
1024 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001025 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +00001026}
1027
Daniel Dunbar921b9682008-10-04 19:21:03 +00001028// #pragma pack(...) comes in the following delicious flavors:
1029// pack '(' [integer] ')'
1030// pack '(' 'show' ')'
1031// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +00001032void PragmaPackHandler::HandlePragma(Preprocessor &PP,
1033 PragmaIntroducerKind Introducer,
1034 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +00001035 SourceLocation PackLoc = PackTok.getLocation();
1036
1037 Token Tok;
1038 PP.Lex(Tok);
1039 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001040 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001041 return;
1042 }
1043
Denis Zobnin10c4f452016-04-29 18:17:40 +00001044 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1045 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001046 Token Alignment;
1047 Alignment.startToken();
Mike Stump11289f42009-09-09 15:08:12 +00001048 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001049 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001050 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001051
1052 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +00001053
1054 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1055 // the push/pop stack.
1056 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
Denis Zobnin10c4f452016-04-29 18:17:40 +00001057 Action =
1058 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001059 } else if (Tok.is(tok::identifier)) {
1060 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +00001061 if (II->isStr("show")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001062 Action = Sema::PSK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001063 PP.Lex(Tok);
1064 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001065 if (II->isStr("push")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001066 Action = Sema::PSK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +00001067 } else if (II->isStr("pop")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001068 Action = Sema::PSK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001069 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001070 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001071 return;
Mike Stump11289f42009-09-09 15:08:12 +00001072 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001073 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001074
Daniel Dunbar921b9682008-10-04 19:21:03 +00001075 if (Tok.is(tok::comma)) {
1076 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001077
Daniel Dunbar921b9682008-10-04 19:21:03 +00001078 if (Tok.is(tok::numeric_constant)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001079 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001080 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001081
1082 PP.Lex(Tok);
1083 } else if (Tok.is(tok::identifier)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001084 SlotLabel = Tok.getIdentifierInfo()->getName();
Daniel Dunbar921b9682008-10-04 19:21:03 +00001085 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001086
Daniel Dunbar921b9682008-10-04 19:21:03 +00001087 if (Tok.is(tok::comma)) {
1088 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001089
Daniel Dunbar921b9682008-10-04 19:21:03 +00001090 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001091 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001092 return;
1093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
Denis Zobnin10c4f452016-04-29 18:17:40 +00001095 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001096 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001097
1098 PP.Lex(Tok);
1099 }
1100 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001101 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001102 return;
1103 }
1104 }
1105 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001106 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001107 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1108 // the push/pop stack.
1109 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
Denis Zobnin10c4f452016-04-29 18:17:40 +00001110 Action = Sema::PSK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001111 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001112
1113 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001114 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001115 return;
1116 }
1117
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001118 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001119 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001120 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001121 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1122 return;
1123 }
1124
David Blaikie2eabcc92016-02-09 18:52:09 +00001125 PragmaPackInfo *Info =
1126 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Denis Zobnin10c4f452016-04-29 18:17:40 +00001127 Info->Action = Action;
1128 Info->SlotLabel = SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001129 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001130
David Blaikie2eabcc92016-02-09 18:52:09 +00001131 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1132 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001133 Toks[0].startToken();
1134 Toks[0].setKind(tok::annot_pragma_pack);
1135 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001136 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001137 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001138 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001139}
1140
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001141// #pragma ms_struct on
1142// #pragma ms_struct off
1143void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1144 PragmaIntroducerKind Introducer,
1145 Token &MSStructTok) {
Nico Weber779355f2016-03-02 23:22:00 +00001146 PragmaMSStructKind Kind = PMSST_OFF;
1147
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001148 Token Tok;
1149 PP.Lex(Tok);
1150 if (Tok.isNot(tok::identifier)) {
1151 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1152 return;
1153 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001154 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001155 const IdentifierInfo *II = Tok.getIdentifierInfo();
1156 if (II->isStr("on")) {
Nico Weber779355f2016-03-02 23:22:00 +00001157 Kind = PMSST_ON;
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001158 PP.Lex(Tok);
1159 }
1160 else if (II->isStr("off") || II->isStr("reset"))
1161 PP.Lex(Tok);
1162 else {
1163 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1164 return;
1165 }
1166
1167 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001168 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1169 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001170 return;
1171 }
Eli Friedman68be1642012-10-04 02:36:51 +00001172
David Blaikie2eabcc92016-02-09 18:52:09 +00001173 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1174 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001175 Toks[0].startToken();
1176 Toks[0].setKind(tok::annot_pragma_msstruct);
1177 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001178 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001179 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1180 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001181 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001182}
1183
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001184// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1185// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001186static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001187 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001188 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001189
1190 if (IsOptions) {
1191 PP.Lex(Tok);
1192 if (Tok.isNot(tok::identifier) ||
1193 !Tok.getIdentifierInfo()->isStr("align")) {
1194 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1195 return;
1196 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001197 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001198
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001199 PP.Lex(Tok);
1200 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001201 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1202 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001203 return;
1204 }
1205
1206 PP.Lex(Tok);
1207 if (Tok.isNot(tok::identifier)) {
1208 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001209 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001210 return;
1211 }
1212
John McCallfaf5fb42010-08-26 23:41:50 +00001213 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001214 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001215 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001216 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001217 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001218 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001219 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001220 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001221 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001222 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001223 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001224 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001225 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001226 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001227 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001228 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1229 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001230 return;
1231 }
1232
David Majnemera8f2f1d2015-03-19 00:10:23 +00001233 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001234 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001235 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001236 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001237 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001238 return;
1239 }
1240
David Blaikie2eabcc92016-02-09 18:52:09 +00001241 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1242 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001243 Toks[0].startToken();
1244 Toks[0].setKind(tok::annot_pragma_align);
1245 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001246 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001247 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1248 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001249 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001250}
1251
Douglas Gregorc7d65762010-09-09 22:45:38 +00001252void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1253 PragmaIntroducerKind Introducer,
1254 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001255 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001256}
1257
Douglas Gregorc7d65762010-09-09 22:45:38 +00001258void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1259 PragmaIntroducerKind Introducer,
1260 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001261 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001262}
1263
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001264// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001265void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1266 PragmaIntroducerKind Introducer,
1267 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001268 // FIXME: Should we be expanding macros here? My guess is no.
1269 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001270
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001271 // Lex the left '('.
1272 Token Tok;
1273 PP.Lex(Tok);
1274 if (Tok.isNot(tok::l_paren)) {
1275 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1276 return;
1277 }
Mike Stump11289f42009-09-09 15:08:12 +00001278
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001279 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001280 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001281 SourceLocation RParenLoc;
1282 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001283
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001284 while (true) {
1285 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001286
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001287 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001288 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001289 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001290 LexID = false;
1291 continue;
1292 }
1293
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001294 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001295 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1296 return;
1297 }
Mike Stump11289f42009-09-09 15:08:12 +00001298
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001299 // We are execting a ')' or a ','.
1300 if (Tok.is(tok::comma)) {
1301 LexID = true;
1302 continue;
1303 }
Mike Stump11289f42009-09-09 15:08:12 +00001304
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001305 if (Tok.is(tok::r_paren)) {
1306 RParenLoc = Tok.getLocation();
1307 break;
1308 }
Mike Stump11289f42009-09-09 15:08:12 +00001309
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001310 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001311 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001312 return;
1313 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001314
1315 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001316 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001317 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1318 "unused";
1319 return;
1320 }
1321
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001322 // Verify that we have a location for the right parenthesis.
1323 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001324 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001325
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001326 // For each identifier token, insert into the token stream a
1327 // annot_pragma_unused token followed by the identifier token.
1328 // This allows us to cache a "#pragma unused" that occurs inside an inline
1329 // C++ member function.
1330
David Blaikie2eabcc92016-02-09 18:52:09 +00001331 MutableArrayRef<Token> Toks(
1332 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1333 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001334 for (unsigned i=0; i != Identifiers.size(); i++) {
1335 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1336 pragmaUnusedTok.startToken();
1337 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1338 pragmaUnusedTok.setLocation(UnusedLoc);
1339 idTok = Identifiers[i];
1340 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001341 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001342}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001343
1344// #pragma weak identifier
1345// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001346void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1347 PragmaIntroducerKind Introducer,
1348 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001349 SourceLocation WeakLoc = WeakTok.getLocation();
1350
1351 Token Tok;
1352 PP.Lex(Tok);
1353 if (Tok.isNot(tok::identifier)) {
1354 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1355 return;
1356 }
1357
Eli Friedman68be1642012-10-04 02:36:51 +00001358 Token WeakName = Tok;
1359 bool HasAlias = false;
1360 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001361
1362 PP.Lex(Tok);
1363 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001364 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001365 PP.Lex(Tok);
1366 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001367 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001368 << "weak";
1369 return;
1370 }
Eli Friedman68be1642012-10-04 02:36:51 +00001371 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001372 PP.Lex(Tok);
1373 }
1374
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001375 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001376 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1377 return;
1378 }
1379
Eli Friedman68be1642012-10-04 02:36:51 +00001380 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00001381 MutableArrayRef<Token> Toks(
1382 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001383 Token &pragmaUnusedTok = Toks[0];
1384 pragmaUnusedTok.startToken();
1385 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1386 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001387 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001388 Toks[1] = WeakName;
1389 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001390 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001391 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00001392 MutableArrayRef<Token> Toks(
1393 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00001394 Token &pragmaUnusedTok = Toks[0];
1395 pragmaUnusedTok.startToken();
1396 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1397 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001398 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001399 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001400 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001401 }
1402}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001403
David Chisnall0867d9c2012-02-18 16:12:34 +00001404// #pragma redefine_extname identifier identifier
1405void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1406 PragmaIntroducerKind Introducer,
1407 Token &RedefToken) {
1408 SourceLocation RedefLoc = RedefToken.getLocation();
1409
1410 Token Tok;
1411 PP.Lex(Tok);
1412 if (Tok.isNot(tok::identifier)) {
1413 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1414 "redefine_extname";
1415 return;
1416 }
1417
Eli Friedman68be1642012-10-04 02:36:51 +00001418 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001419 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001420
David Chisnall0867d9c2012-02-18 16:12:34 +00001421 if (Tok.isNot(tok::identifier)) {
1422 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1423 << "redefine_extname";
1424 return;
1425 }
Eli Friedman68be1642012-10-04 02:36:51 +00001426
1427 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001428 PP.Lex(Tok);
1429
1430 if (Tok.isNot(tok::eod)) {
1431 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1432 "redefine_extname";
1433 return;
1434 }
1435
David Blaikie2eabcc92016-02-09 18:52:09 +00001436 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1437 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001438 Token &pragmaRedefTok = Toks[0];
1439 pragmaRedefTok.startToken();
1440 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1441 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001442 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001443 Toks[1] = RedefName;
1444 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001445 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
David Chisnall0867d9c2012-02-18 16:12:34 +00001446}
1447
1448
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001449void
1450PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1451 PragmaIntroducerKind Introducer,
1452 Token &Tok) {
1453 tok::OnOffSwitch OOS;
1454 if (PP.LexOnOffSwitch(OOS))
1455 return;
1456
David Blaikie2eabcc92016-02-09 18:52:09 +00001457 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1458 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001459 Toks[0].startToken();
1460 Toks[0].setKind(tok::annot_pragma_fp_contract);
1461 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001462 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001463 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1464 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001465 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001466}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001467
1468void
1469PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1470 PragmaIntroducerKind Introducer,
1471 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001472 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001473 if (Tok.isNot(tok::identifier)) {
1474 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1475 "OPENCL";
1476 return;
1477 }
Yaxun Liu5b746652016-12-18 05:18:55 +00001478 IdentifierInfo *Ext = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001479 SourceLocation NameLoc = Tok.getLocation();
1480
1481 PP.Lex(Tok);
1482 if (Tok.isNot(tok::colon)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00001483 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001484 return;
1485 }
1486
1487 PP.Lex(Tok);
1488 if (Tok.isNot(tok::identifier)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00001489 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001490 return;
1491 }
Yaxun Liu5b746652016-12-18 05:18:55 +00001492 IdentifierInfo *Pred = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001493
Yaxun Liu5b746652016-12-18 05:18:55 +00001494 OpenCLExtState State;
1495 if (Pred->isStr("enable")) {
1496 State = Enable;
1497 } else if (Pred->isStr("disable")) {
1498 State = Disable;
1499 } else if (Pred->isStr("begin"))
1500 State = Begin;
1501 else if (Pred->isStr("end"))
1502 State = End;
1503 else {
1504 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
1505 << Ext->isStr("all");
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001506 return;
1507 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001508 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001509
Eli Friedman68be1642012-10-04 02:36:51 +00001510 PP.Lex(Tok);
1511 if (Tok.isNot(tok::eod)) {
1512 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1513 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001514 return;
1515 }
Eli Friedman68be1642012-10-04 02:36:51 +00001516
Yaxun Liu5b746652016-12-18 05:18:55 +00001517 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
1518 Info->first = Ext;
1519 Info->second = State;
David Blaikie2eabcc92016-02-09 18:52:09 +00001520 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1521 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001522 Toks[0].startToken();
1523 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1524 Toks[0].setLocation(NameLoc);
Yaxun Liu5b746652016-12-18 05:18:55 +00001525 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Majnemera8f2f1d2015-03-19 00:10:23 +00001526 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00001527 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001528
1529 if (PP.getPPCallbacks())
Yaxun Liu5b746652016-12-18 05:18:55 +00001530 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
1531 StateLoc, State);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001532}
1533
Alexey Bataeva769e072013-03-22 06:34:35 +00001534/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1535///
1536void
1537PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1538 PragmaIntroducerKind Introducer,
1539 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001540 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1541 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001542 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001543 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1544 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001545 }
1546 PP.DiscardUntilEndOfDirective();
1547}
1548
1549/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1550///
1551void
1552PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1553 PragmaIntroducerKind Introducer,
1554 Token &FirstTok) {
1555 SmallVector<Token, 16> Pragma;
1556 Token Tok;
1557 Tok.startToken();
1558 Tok.setKind(tok::annot_pragma_openmp);
1559 Tok.setLocation(FirstTok.getLocation());
1560
1561 while (Tok.isNot(tok::eod)) {
1562 Pragma.push_back(Tok);
1563 PP.Lex(Tok);
1564 }
1565 SourceLocation EodLoc = Tok.getLocation();
1566 Tok.startToken();
1567 Tok.setKind(tok::annot_pragma_openmp_end);
1568 Tok.setLocation(EodLoc);
1569 Pragma.push_back(Tok);
1570
David Blaikie2eabcc92016-02-09 18:52:09 +00001571 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
1572 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
1573 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
1574 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00001575}
Reid Kleckner002562a2013-05-06 21:02:12 +00001576
David Majnemer4bb09802014-02-10 19:50:15 +00001577/// \brief Handle '#pragma pointers_to_members'
1578// The grammar for this pragma is as follows:
1579//
1580// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1581//
1582// #pragma pointers_to_members '(' 'best_case' ')'
1583// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1584// #pragma pointers_to_members '(' inheritance-model ')'
1585void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1586 PragmaIntroducerKind Introducer,
1587 Token &Tok) {
1588 SourceLocation PointersToMembersLoc = Tok.getLocation();
1589 PP.Lex(Tok);
1590 if (Tok.isNot(tok::l_paren)) {
1591 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1592 << "pointers_to_members";
1593 return;
1594 }
1595 PP.Lex(Tok);
1596 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1597 if (!Arg) {
1598 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1599 << "pointers_to_members";
1600 return;
1601 }
1602 PP.Lex(Tok);
1603
David Majnemer86c318f2014-02-11 21:05:00 +00001604 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001605 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001606 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001607 } else {
1608 if (Arg->isStr("full_generality")) {
1609 if (Tok.is(tok::comma)) {
1610 PP.Lex(Tok);
1611
1612 Arg = Tok.getIdentifierInfo();
1613 if (!Arg) {
1614 PP.Diag(Tok.getLocation(),
1615 diag::err_pragma_pointers_to_members_unknown_kind)
1616 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1617 return;
1618 }
1619 PP.Lex(Tok);
1620 } else if (Tok.is(tok::r_paren)) {
1621 // #pragma pointers_to_members(full_generality) implicitly specifies
1622 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001623 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001624 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001625 } else {
1626 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1627 << "full_generality";
1628 return;
1629 }
1630 }
1631
1632 if (Arg) {
1633 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001634 RepresentationMethod =
1635 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001636 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001637 RepresentationMethod =
1638 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001639 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001640 RepresentationMethod =
1641 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001642 } else {
1643 PP.Diag(Tok.getLocation(),
1644 diag::err_pragma_pointers_to_members_unknown_kind)
1645 << Arg << /*HasPointerDeclaration*/ 1;
1646 return;
1647 }
1648 }
1649 }
1650
1651 if (Tok.isNot(tok::r_paren)) {
1652 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1653 << (Arg ? Arg->getName() : "full_generality");
1654 return;
1655 }
1656
David Majnemera8f2f1d2015-03-19 00:10:23 +00001657 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001658 PP.Lex(Tok);
1659 if (Tok.isNot(tok::eod)) {
1660 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1661 << "pointers_to_members";
1662 return;
1663 }
1664
1665 Token AnnotTok;
1666 AnnotTok.startToken();
1667 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1668 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001669 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001670 AnnotTok.setAnnotationValue(
1671 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1672 PP.EnterToken(AnnotTok);
1673}
1674
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001675/// \brief Handle '#pragma vtordisp'
1676// The grammar for this pragma is as follows:
1677//
1678// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1679//
1680// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1681// #pragma vtordisp '(' 'pop' ')'
1682// #pragma vtordisp '(' ')'
1683void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1684 PragmaIntroducerKind Introducer,
1685 Token &Tok) {
1686 SourceLocation VtorDispLoc = Tok.getLocation();
1687 PP.Lex(Tok);
1688 if (Tok.isNot(tok::l_paren)) {
1689 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1690 return;
1691 }
1692 PP.Lex(Tok);
1693
Denis Zobnin2290dac2016-04-29 11:27:00 +00001694 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001695 const IdentifierInfo *II = Tok.getIdentifierInfo();
1696 if (II) {
1697 if (II->isStr("push")) {
1698 // #pragma vtordisp(push, mode)
1699 PP.Lex(Tok);
1700 if (Tok.isNot(tok::comma)) {
1701 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1702 return;
1703 }
1704 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001705 Action = Sema::PSK_Push_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001706 // not push, could be on/off
1707 } else if (II->isStr("pop")) {
1708 // #pragma vtordisp(pop)
1709 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001710 Action = Sema::PSK_Pop;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001711 }
1712 // not push or pop, could be on/off
1713 } else {
1714 if (Tok.is(tok::r_paren)) {
1715 // #pragma vtordisp()
Denis Zobnin2290dac2016-04-29 11:27:00 +00001716 Action = Sema::PSK_Reset;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001717 }
1718 }
1719
1720
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001721 uint64_t Value = 0;
Denis Zobnin2290dac2016-04-29 11:27:00 +00001722 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001723 const IdentifierInfo *II = Tok.getIdentifierInfo();
1724 if (II && II->isStr("off")) {
1725 PP.Lex(Tok);
1726 Value = 0;
1727 } else if (II && II->isStr("on")) {
1728 PP.Lex(Tok);
1729 Value = 1;
1730 } else if (Tok.is(tok::numeric_constant) &&
1731 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1732 if (Value > 2) {
1733 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1734 << 0 << 2 << "vtordisp";
1735 return;
1736 }
1737 } else {
1738 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1739 << "vtordisp";
1740 return;
1741 }
1742 }
1743
1744 // Finish the pragma: ')' $
1745 if (Tok.isNot(tok::r_paren)) {
1746 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1747 return;
1748 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001749 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001750 PP.Lex(Tok);
1751 if (Tok.isNot(tok::eod)) {
1752 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1753 << "vtordisp";
1754 return;
1755 }
1756
1757 // Enter the annotation.
1758 Token AnnotTok;
1759 AnnotTok.startToken();
1760 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1761 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001762 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001763 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
Denis Zobnin2290dac2016-04-29 11:27:00 +00001764 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001765 PP.EnterToken(AnnotTok);
1766}
1767
Warren Huntc3b18962014-04-08 22:30:47 +00001768/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1769/// an annotation token.
1770void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1771 PragmaIntroducerKind Introducer,
1772 Token &Tok) {
1773 Token EoF, AnnotTok;
1774 EoF.startToken();
1775 EoF.setKind(tok::eof);
1776 AnnotTok.startToken();
1777 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1778 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001779 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001780 SmallVector<Token, 8> TokenVector;
1781 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001782 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001783 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001784 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1785 }
Warren Huntc3b18962014-04-08 22:30:47 +00001786 // Add a sentinal EoF token to the end of the list.
1787 TokenVector.push_back(EoF);
1788 // We must allocate this array with new because EnterTokenStream is going to
1789 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00001790 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
1791 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00001792 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00001793 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
1794 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00001795 AnnotTok.setAnnotationValue(Value);
1796 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001797}
1798
Aaron Ballman5d041be2013-06-04 02:07:14 +00001799/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1800///
1801/// The syntax is:
1802/// \code
1803/// #pragma detect_mismatch("name", "value")
1804/// \endcode
1805/// Where 'name' and 'value' are quoted strings. The values are embedded in
1806/// the object file and passed along to the linker. If the linker detects a
1807/// mismatch in the object file's values for the given name, a LNK2038 error
1808/// is emitted. See MSDN for more details.
1809void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1810 PragmaIntroducerKind Introducer,
1811 Token &Tok) {
Nico Webercbbaeb12016-03-02 19:28:54 +00001812 SourceLocation DetectMismatchLoc = Tok.getLocation();
Aaron Ballman5d041be2013-06-04 02:07:14 +00001813 PP.Lex(Tok);
1814 if (Tok.isNot(tok::l_paren)) {
Nico Webercbbaeb12016-03-02 19:28:54 +00001815 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001816 return;
1817 }
1818
1819 // Read the name to embed, which must be a string literal.
1820 std::string NameString;
1821 if (!PP.LexStringLiteral(Tok, NameString,
1822 "pragma detect_mismatch",
1823 /*MacroExpansion=*/true))
1824 return;
1825
1826 // Read the comma followed by a second string literal.
1827 std::string ValueString;
1828 if (Tok.isNot(tok::comma)) {
1829 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1830 return;
1831 }
1832
1833 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1834 /*MacroExpansion=*/true))
1835 return;
1836
1837 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001838 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001839 return;
1840 }
1841 PP.Lex(Tok); // Eat the r_paren.
1842
1843 if (Tok.isNot(tok::eod)) {
1844 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1845 return;
1846 }
1847
Reid Kleckner71966c92014-02-20 23:37:45 +00001848 // If the pragma is lexically sound, notify any interested PPCallbacks.
1849 if (PP.getPPCallbacks())
Nico Webercbbaeb12016-03-02 19:28:54 +00001850 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
Reid Kleckner71966c92014-02-20 23:37:45 +00001851 ValueString);
1852
Nico Webercbbaeb12016-03-02 19:28:54 +00001853 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
Aaron Ballman5d041be2013-06-04 02:07:14 +00001854}
1855
Reid Kleckner002562a2013-05-06 21:02:12 +00001856/// \brief Handle the microsoft \#pragma comment extension.
1857///
1858/// The syntax is:
1859/// \code
1860/// #pragma comment(linker, "foo")
1861/// \endcode
1862/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1863/// "foo" is a string, which is fully macro expanded, and permits string
1864/// concatenation, embedded escape characters etc. See MSDN for more details.
1865void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1866 PragmaIntroducerKind Introducer,
1867 Token &Tok) {
1868 SourceLocation CommentLoc = Tok.getLocation();
1869 PP.Lex(Tok);
1870 if (Tok.isNot(tok::l_paren)) {
1871 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1872 return;
1873 }
1874
1875 // Read the identifier.
1876 PP.Lex(Tok);
1877 if (Tok.isNot(tok::identifier)) {
1878 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1879 return;
1880 }
1881
1882 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001883 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00001884 PragmaMSCommentKind Kind =
1885 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
1886 .Case("linker", PCK_Linker)
1887 .Case("lib", PCK_Lib)
1888 .Case("compiler", PCK_Compiler)
1889 .Case("exestr", PCK_ExeStr)
1890 .Case("user", PCK_User)
1891 .Default(PCK_Unknown);
1892 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001893 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1894 return;
1895 }
1896
Yunzhong Gao99efc032015-03-23 20:41:42 +00001897 // On PS4, issue a warning about any pragma comments other than
1898 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00001899 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00001900 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1901 << II->getName();
1902 return;
1903 }
1904
Reid Kleckner002562a2013-05-06 21:02:12 +00001905 // Read the optional string if present.
1906 PP.Lex(Tok);
1907 std::string ArgumentString;
1908 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1909 "pragma comment",
1910 /*MacroExpansion=*/true))
1911 return;
1912
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001913 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001914 // FIXME: If the kind is "compiler" warn if the string is present (it is
1915 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001916 // The MSDN docs say that "lib" and "linker" require a string and have a short
1917 // whitelist of linker options they support, but in practice MSVC doesn't
1918 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001919
1920 if (Tok.isNot(tok::r_paren)) {
1921 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1922 return;
1923 }
1924 PP.Lex(Tok); // eat the r_paren.
1925
1926 if (Tok.isNot(tok::eod)) {
1927 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1928 return;
1929 }
1930
Reid Kleckner71966c92014-02-20 23:37:45 +00001931 // If the pragma is lexically sound, notify any interested PPCallbacks.
1932 if (PP.getPPCallbacks())
1933 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1934
Nico Weber66220292016-03-02 17:28:48 +00001935 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001936}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001937
1938// #pragma clang optimize off
1939// #pragma clang optimize on
1940void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1941 PragmaIntroducerKind Introducer,
1942 Token &FirstToken) {
1943 Token Tok;
1944 PP.Lex(Tok);
1945 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001946 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001947 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001948 return;
1949 }
1950 if (Tok.isNot(tok::identifier)) {
1951 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1952 << PP.getSpelling(Tok);
1953 return;
1954 }
1955 const IdentifierInfo *II = Tok.getIdentifierInfo();
1956 // The only accepted values are 'on' or 'off'.
1957 bool IsOn = false;
1958 if (II->isStr("on")) {
1959 IsOn = true;
1960 } else if (!II->isStr("off")) {
1961 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1962 << PP.getSpelling(Tok);
1963 return;
1964 }
1965 PP.Lex(Tok);
1966
1967 if (Tok.isNot(tok::eod)) {
1968 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1969 << PP.getSpelling(Tok);
1970 return;
1971 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001972
1973 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1974}
1975
Adam Nemet60d32642017-04-04 21:18:36 +00001976namespace {
1977/// Used as the annotation value for tok::annot_pragma_fp.
1978struct TokFPAnnotValue {
1979 enum FlagKinds { Contract };
1980 enum FlagValues { On, Off, Fast };
1981
1982 FlagKinds FlagKind;
1983 FlagValues FlagValue;
1984};
1985} // end anonymous namespace
1986
1987void PragmaFPHandler::HandlePragma(Preprocessor &PP,
1988 PragmaIntroducerKind Introducer,
1989 Token &Tok) {
1990 // fp
1991 Token PragmaName = Tok;
1992 SmallVector<Token, 1> TokenList;
1993
1994 PP.Lex(Tok);
1995 if (Tok.isNot(tok::identifier)) {
1996 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
1997 << /*MissingOption=*/true << "";
1998 return;
1999 }
2000
2001 while (Tok.is(tok::identifier)) {
2002 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2003
2004 auto FlagKind =
2005 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2006 OptionInfo->getName())
2007 .Case("contract", TokFPAnnotValue::Contract)
2008 .Default(None);
2009 if (!FlagKind) {
2010 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2011 << /*MissingOption=*/false << OptionInfo;
2012 return;
2013 }
2014 PP.Lex(Tok);
2015
2016 // Read '('
2017 if (Tok.isNot(tok::l_paren)) {
2018 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2019 return;
2020 }
2021 PP.Lex(Tok);
2022
2023 if (Tok.isNot(tok::identifier)) {
2024 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2025 << PP.getSpelling(Tok) << OptionInfo->getName();
2026 return;
2027 }
2028 const IdentifierInfo *II = Tok.getIdentifierInfo();
2029
2030 auto FlagValue =
2031 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2032 II->getName())
2033 .Case("on", TokFPAnnotValue::On)
2034 .Case("off", TokFPAnnotValue::Off)
2035 .Case("fast", TokFPAnnotValue::Fast)
2036 .Default(llvm::None);
2037
2038 if (!FlagValue) {
2039 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2040 << PP.getSpelling(Tok) << OptionInfo->getName();
2041 return;
2042 }
2043 PP.Lex(Tok);
2044
2045 // Read ')'
2046 if (Tok.isNot(tok::r_paren)) {
2047 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2048 return;
2049 }
2050 PP.Lex(Tok);
2051
2052 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2053 TokFPAnnotValue{*FlagKind, *FlagValue};
2054 // Generate the loop hint token.
2055 Token FPTok;
2056 FPTok.startToken();
2057 FPTok.setKind(tok::annot_pragma_fp);
2058 FPTok.setLocation(PragmaName.getLocation());
2059 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2060 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2061 TokenList.push_back(FPTok);
2062 }
2063
2064 if (Tok.isNot(tok::eod)) {
2065 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2066 << "clang fp";
2067 return;
2068 }
2069
2070 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2071 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2072
2073 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2074 /*DisableMacroExpansion=*/false);
2075}
2076
2077void Parser::HandlePragmaFP() {
2078 assert(Tok.is(tok::annot_pragma_fp));
2079 auto *AnnotValue =
2080 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2081
2082 LangOptions::FPContractModeKind FPC;
2083 switch (AnnotValue->FlagValue) {
2084 case TokFPAnnotValue::On:
2085 FPC = LangOptions::FPC_On;
2086 break;
2087 case TokFPAnnotValue::Fast:
2088 FPC = LangOptions::FPC_Fast;
2089 break;
2090 case TokFPAnnotValue::Off:
2091 FPC = LangOptions::FPC_Off;
2092 break;
2093 }
2094
2095 Actions.ActOnPragmaFPContract(FPC);
2096 ConsumeToken(); // The annotation token.
2097}
2098
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002099/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002100static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2101 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002102 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002103 SmallVector<Token, 1> ValueList;
2104 int OpenParens = ValueInParens ? 1 : 0;
2105 // Read constant expression.
2106 while (Tok.isNot(tok::eod)) {
2107 if (Tok.is(tok::l_paren))
2108 OpenParens++;
2109 else if (Tok.is(tok::r_paren)) {
2110 OpenParens--;
2111 if (OpenParens == 0 && ValueInParens)
2112 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002113 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002114
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002115 ValueList.push_back(Tok);
2116 PP.Lex(Tok);
2117 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002118
2119 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002120 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002121 if (Tok.isNot(tok::r_paren)) {
2122 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2123 return true;
2124 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002125 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002126 }
2127
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002128 Token EOFTok;
2129 EOFTok.startToken();
2130 EOFTok.setKind(tok::eof);
2131 EOFTok.setLocation(Tok.getLocation());
2132 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2133
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00002134 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002135
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002136 Info.PragmaName = PragmaName;
2137 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002138 return false;
2139}
2140
Eli Bendersky06a40422014-06-06 20:31:48 +00002141/// \brief Handle the \#pragma clang loop directive.
2142/// #pragma clang 'loop' loop-hints
2143///
2144/// loop-hints:
2145/// loop-hint loop-hints[opt]
2146///
2147/// loop-hint:
2148/// 'vectorize' '(' loop-hint-keyword ')'
2149/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00002150/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002151/// 'vectorize_width' '(' loop-hint-value ')'
2152/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00002153/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002154///
2155/// loop-hint-keyword:
2156/// 'enable'
2157/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00002158/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00002159///
Mark Heffernan450c2382014-07-23 17:31:31 +00002160/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00002161/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00002162/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00002163/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00002164///
Eli Bendersky06a40422014-06-06 20:31:48 +00002165/// loop-hint-value:
2166/// constant-expression
2167///
2168/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2169/// try vectorizing the instructions of the loop it precedes. Specifying
2170/// interleave(enable) or interleave_count(_value_) instructs llvm to try
2171/// interleaving multiple iterations of the loop it precedes. The width of the
2172/// vector instructions is specified by vectorize_width() and the number of
2173/// interleaved loop iterations is specified by interleave_count(). Specifying a
2174/// value of 1 effectively disables vectorization/interleaving, even if it is
2175/// possible and profitable, and 0 is invalid. The loop vectorizer currently
2176/// only works on inner loops.
2177///
Eli Bendersky86483b32014-06-11 17:56:26 +00002178/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00002179/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2180/// completely if the trip count is known at compile time and unroll partially
2181/// if the trip count is not known. Specifying unroll(full) is similar to
2182/// unroll(enable) but will unroll the loop only if the trip count is known at
2183/// compile time. Specifying unroll(disable) disables unrolling for the
2184/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2185/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00002186void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2187 PragmaIntroducerKind Introducer,
2188 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002189 // Incoming token is "loop" from "#pragma clang loop".
2190 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00002191 SmallVector<Token, 1> TokenList;
2192
2193 // Lex the optimization option and verify it is an identifier.
2194 PP.Lex(Tok);
2195 if (Tok.isNot(tok::identifier)) {
2196 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2197 << /*MissingOption=*/true << "";
2198 return;
2199 }
2200
2201 while (Tok.is(tok::identifier)) {
2202 Token Option = Tok;
2203 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2204
Eli Bendersky86483b32014-06-11 17:56:26 +00002205 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002206 .Case("vectorize", true)
2207 .Case("interleave", true)
2208 .Case("unroll", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00002209 .Case("distribute", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002210 .Case("vectorize_width", true)
2211 .Case("interleave_count", true)
2212 .Case("unroll_count", true)
2213 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002214 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002215 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2216 << /*MissingOption=*/false << OptionInfo;
2217 return;
2218 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002219 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002220
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002221 // Read '('
2222 if (Tok.isNot(tok::l_paren)) {
2223 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002224 return;
2225 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002226 PP.Lex(Tok);
2227
2228 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2229 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2230 *Info))
2231 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002232
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002233 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002234 Token LoopHintTok;
2235 LoopHintTok.startToken();
2236 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002237 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002238 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002239 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2240 TokenList.push_back(LoopHintTok);
2241 }
2242
2243 if (Tok.isNot(tok::eod)) {
2244 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2245 << "clang loop";
2246 return;
2247 }
2248
David Blaikie2eabcc92016-02-09 18:52:09 +00002249 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2250 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002251
David Blaikie2eabcc92016-02-09 18:52:09 +00002252 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2253 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002254}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002255
2256/// \brief Handle the loop unroll optimization pragmas.
2257/// #pragma unroll
2258/// #pragma unroll unroll-hint-value
2259/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002260/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002261///
2262/// unroll-hint-value:
2263/// constant-expression
2264///
Mark Heffernanc888e412014-07-24 18:09:38 +00002265/// Loop unrolling hints can be specified with '#pragma unroll' or
2266/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2267/// contained in parentheses. With no argument the directive instructs llvm to
2268/// try to unroll the loop completely. A positive integer argument can be
2269/// specified to indicate the number of times the loop should be unrolled. To
2270/// maximize compatibility with other compilers the unroll count argument can be
2271/// specified with or without parentheses. Specifying, '#pragma nounroll'
2272/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002273void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2274 PragmaIntroducerKind Introducer,
2275 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002276 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2277 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002278 Token PragmaName = Tok;
2279 PP.Lex(Tok);
2280 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2281 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002282 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002283 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002284 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002285 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2286 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2287 << "nounroll";
2288 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002289 } else {
2290 // Unroll pragma with an argument: "#pragma unroll N" or
2291 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002292 // Read '(' if it exists.
2293 bool ValueInParens = Tok.is(tok::l_paren);
2294 if (ValueInParens)
2295 PP.Lex(Tok);
2296
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002297 Token Option;
2298 Option.startToken();
2299 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002300 return;
2301
2302 // In CUDA, the argument to '#pragma unroll' should not be contained in
2303 // parentheses.
2304 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002305 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002306 diag::warn_pragma_unroll_cuda_value_in_parens);
2307
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002308 if (Tok.isNot(tok::eod)) {
2309 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2310 << "unroll";
2311 return;
2312 }
2313 }
2314
2315 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002316 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002317 TokenArray[0].startToken();
2318 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2319 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002320 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002321 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002322 PP.EnterTokenStream(std::move(TokenArray), 1,
2323 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002324}
Reid Kleckner3f1ec622016-09-07 16:38:32 +00002325
2326/// \brief Handle the Microsoft \#pragma intrinsic extension.
2327///
2328/// The syntax is:
2329/// \code
2330/// #pragma intrinsic(memset)
2331/// #pragma intrinsic(strlen, memcpy)
2332/// \endcode
2333///
2334/// Pragma intrisic tells the compiler to use a builtin version of the
2335/// function. Clang does it anyway, so the pragma doesn't really do anything.
2336/// Anyway, we emit a warning if the function specified in \#pragma intrinsic
2337/// isn't an intrinsic in clang and suggest to include intrin.h.
2338void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
2339 PragmaIntroducerKind Introducer,
2340 Token &Tok) {
2341 PP.Lex(Tok);
2342
2343 if (Tok.isNot(tok::l_paren)) {
2344 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
2345 << "intrinsic";
2346 return;
2347 }
2348 PP.Lex(Tok);
2349
2350 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
2351
2352 while (Tok.is(tok::identifier)) {
2353 IdentifierInfo *II = Tok.getIdentifierInfo();
2354 if (!II->getBuiltinID())
2355 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
2356 << II << SuggestIntrinH;
2357
2358 PP.Lex(Tok);
2359 if (Tok.isNot(tok::comma))
2360 break;
2361 PP.Lex(Tok);
2362 }
2363
2364 if (Tok.isNot(tok::r_paren)) {
2365 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
2366 << "intrinsic";
2367 return;
2368 }
2369 PP.Lex(Tok);
2370
2371 if (Tok.isNot(tok::eod))
2372 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2373 << "intrinsic";
2374}
Justin Lebar67a78a62016-10-08 22:15:58 +00002375void PragmaForceCUDAHostDeviceHandler::HandlePragma(
2376 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
2377 Token FirstTok = Tok;
2378
2379 PP.Lex(Tok);
2380 IdentifierInfo *Info = Tok.getIdentifierInfo();
2381 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
2382 PP.Diag(FirstTok.getLocation(),
2383 diag::warn_pragma_force_cuda_host_device_bad_arg);
2384 return;
2385 }
2386
2387 if (Info->isStr("begin"))
2388 Actions.PushForceCUDAHostDevice();
2389 else if (!Actions.PopForceCUDAHostDevice())
2390 PP.Diag(FirstTok.getLocation(),
2391 diag::err_pragma_cannot_end_force_cuda_host_device);
2392
2393 PP.Lex(Tok);
2394 if (!Tok.is(tok::eod))
2395 PP.Diag(FirstTok.getLocation(),
2396 diag::warn_pragma_force_cuda_host_device_bad_arg);
2397}