blob: 7ae03af2b16066390a72a8e02878093b1b5ef163 [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
Warren Huntc3b18962014-04-08 22:30:47 +000014#include "RAIIObjectsForParser.h"
Hans Wennborg899ded92014-10-16 20:52:46 +000015#include "clang/AST/ASTContext.h"
Nico Weber66220292016-03-02 17:28:48 +000016#include "clang/Basic/PragmaKinds.h"
David Majnemerad2986e2014-08-14 06:35:08 +000017#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000018#include "clang/Lex/Preprocessor.h"
19#include "clang/Parse/ParseDiagnostic.h"
20#include "clang/Parse/Parser.h"
21#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
89struct PragmaNoOpenMPHandler : public PragmaHandler {
90 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000091 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
92 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000093};
94
95struct PragmaOpenMPHandler : public PragmaHandler {
96 PragmaOpenMPHandler() : 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
101/// PragmaCommentHandler - "\#pragma comment ...".
102struct PragmaCommentHandler : public PragmaHandler {
103 PragmaCommentHandler(Sema &Actions)
104 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000105 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
106 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000107private:
108 Sema &Actions;
109};
110
111struct PragmaDetectMismatchHandler : public PragmaHandler {
112 PragmaDetectMismatchHandler(Sema &Actions)
113 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000114 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
115 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000116private:
117 Sema &Actions;
118};
119
120struct PragmaMSPointersToMembers : public PragmaHandler {
121 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000122 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
123 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000124};
125
126struct PragmaMSVtorDisp : public PragmaHandler {
127 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
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
Warren Huntc3b18962014-04-08 22:30:47 +0000132struct PragmaMSPragma : public PragmaHandler {
133 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000134 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
135 Token &FirstToken) override;
136};
137
Dario Domizioli13a0a382014-05-23 12:13:25 +0000138/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
139struct PragmaOptimizeHandler : public PragmaHandler {
140 PragmaOptimizeHandler(Sema &S)
141 : PragmaHandler("optimize"), Actions(S) {}
142 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
143 Token &FirstToken) override;
144private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000145 Sema &Actions;
146};
147
148struct PragmaLoopHintHandler : public PragmaHandler {
149 PragmaLoopHintHandler() : PragmaHandler("loop") {}
150 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
151 Token &FirstToken) override;
152};
153
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000154struct PragmaUnrollHintHandler : public PragmaHandler {
155 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
156 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
157 Token &FirstToken) override;
158};
159
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000160struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
161 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
162};
163
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000164struct PragmaMSIntrinsicHandler : public PragmaHandler {
165 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
166 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
167 Token &FirstToken) override;
168};
169
Eli Bendersky06a40422014-06-06 20:31:48 +0000170} // end namespace
171
172void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000173 AlignHandler.reset(new PragmaAlignHandler());
174 PP.AddPragmaHandler(AlignHandler.get());
175
176 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
177 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
178
179 OptionsHandler.reset(new PragmaOptionsHandler());
180 PP.AddPragmaHandler(OptionsHandler.get());
181
182 PackHandler.reset(new PragmaPackHandler());
183 PP.AddPragmaHandler(PackHandler.get());
184
185 MSStructHandler.reset(new PragmaMSStructHandler());
186 PP.AddPragmaHandler(MSStructHandler.get());
187
188 UnusedHandler.reset(new PragmaUnusedHandler());
189 PP.AddPragmaHandler(UnusedHandler.get());
190
191 WeakHandler.reset(new PragmaWeakHandler());
192 PP.AddPragmaHandler(WeakHandler.get());
193
194 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
195 PP.AddPragmaHandler(RedefineExtnameHandler.get());
196
197 FPContractHandler.reset(new PragmaFPContractHandler());
198 PP.AddPragmaHandler("STDC", FPContractHandler.get());
199
200 if (getLangOpts().OpenCL) {
201 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
202 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
203
204 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
205 }
206 if (getLangOpts().OpenMP)
207 OpenMPHandler.reset(new PragmaOpenMPHandler());
208 else
209 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
210 PP.AddPragmaHandler(OpenMPHandler.get());
211
Yunzhong Gao99efc032015-03-23 20:41:42 +0000212 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000213 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
214 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000215 }
216
217 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000218 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
219 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
220 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
221 PP.AddPragmaHandler(MSPointersToMembers.get());
222 MSVtorDisp.reset(new PragmaMSVtorDisp());
223 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000224 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000225 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000226 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
227 PP.AddPragmaHandler(MSDataSeg.get());
228 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
229 PP.AddPragmaHandler(MSBSSSeg.get());
230 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
231 PP.AddPragmaHandler(MSConstSeg.get());
232 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
233 PP.AddPragmaHandler(MSCodeSeg.get());
234 MSSection.reset(new PragmaMSPragma("section"));
235 PP.AddPragmaHandler(MSSection.get());
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000236 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
237 PP.AddPragmaHandler(MSRuntimeChecks.get());
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000238 MSIntrinsic.reset(new PragmaMSIntrinsicHandler());
239 PP.AddPragmaHandler(MSIntrinsic.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000240 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000241
242 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
243 PP.AddPragmaHandler("clang", OptimizeHandler.get());
244
245 LoopHintHandler.reset(new PragmaLoopHintHandler());
246 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000247
248 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
249 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000250
251 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
252 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000253}
254
255void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000256 // Remove the pragma handlers we installed.
257 PP.RemovePragmaHandler(AlignHandler.get());
258 AlignHandler.reset();
259 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
260 GCCVisibilityHandler.reset();
261 PP.RemovePragmaHandler(OptionsHandler.get());
262 OptionsHandler.reset();
263 PP.RemovePragmaHandler(PackHandler.get());
264 PackHandler.reset();
265 PP.RemovePragmaHandler(MSStructHandler.get());
266 MSStructHandler.reset();
267 PP.RemovePragmaHandler(UnusedHandler.get());
268 UnusedHandler.reset();
269 PP.RemovePragmaHandler(WeakHandler.get());
270 WeakHandler.reset();
271 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
272 RedefineExtnameHandler.reset();
273
274 if (getLangOpts().OpenCL) {
275 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
276 OpenCLExtensionHandler.reset();
277 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
278 }
279 PP.RemovePragmaHandler(OpenMPHandler.get());
280 OpenMPHandler.reset();
281
Yunzhong Gao99efc032015-03-23 20:41:42 +0000282 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000283 PP.RemovePragmaHandler(MSCommentHandler.get());
284 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000285 }
286
287 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000288 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
289 MSDetectMismatchHandler.reset();
290 PP.RemovePragmaHandler(MSPointersToMembers.get());
291 MSPointersToMembers.reset();
292 PP.RemovePragmaHandler(MSVtorDisp.get());
293 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000294 PP.RemovePragmaHandler(MSInitSeg.get());
295 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000296 PP.RemovePragmaHandler(MSDataSeg.get());
297 MSDataSeg.reset();
298 PP.RemovePragmaHandler(MSBSSSeg.get());
299 MSBSSSeg.reset();
300 PP.RemovePragmaHandler(MSConstSeg.get());
301 MSConstSeg.reset();
302 PP.RemovePragmaHandler(MSCodeSeg.get());
303 MSCodeSeg.reset();
304 PP.RemovePragmaHandler(MSSection.get());
305 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000306 PP.RemovePragmaHandler(MSRuntimeChecks.get());
307 MSRuntimeChecks.reset();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000308 PP.RemovePragmaHandler(MSIntrinsic.get());
309 MSIntrinsic.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000310 }
311
312 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
313 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000314
315 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
316 OptimizeHandler.reset();
317
318 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
319 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000320
321 PP.RemovePragmaHandler(UnrollHintHandler.get());
322 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000323
324 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
325 NoUnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000326}
327
328/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000329///
330/// Each annot_pragma_unused is followed by the argument token so e.g.
331/// "#pragma unused(x,y)" becomes:
332/// annot_pragma_unused 'x' annot_pragma_unused 'y'
333void Parser::HandlePragmaUnused() {
334 assert(Tok.is(tok::annot_pragma_unused));
335 SourceLocation UnusedLoc = ConsumeToken();
336 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
337 ConsumeToken(); // The argument token.
338}
Eli Friedman570024a2010-08-05 06:57:20 +0000339
Rafael Espindola273fd772012-01-26 02:02:57 +0000340void Parser::HandlePragmaVisibility() {
341 assert(Tok.is(tok::annot_pragma_vis));
342 const IdentifierInfo *VisType =
343 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
344 SourceLocation VisLoc = ConsumeToken();
345 Actions.ActOnPragmaVisibility(VisType, VisLoc);
346}
347
Benjamin Kramere003ca22015-10-28 13:54:16 +0000348namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000349struct PragmaPackInfo {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000350 Sema::PragmaMsStackAction Action;
351 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000352 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000353};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000354} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000355
356void Parser::HandlePragmaPack() {
357 assert(Tok.is(tok::annot_pragma_pack));
358 PragmaPackInfo *Info =
359 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
360 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000361 ExprResult Alignment;
362 if (Info->Alignment.is(tok::numeric_constant)) {
363 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
364 if (Alignment.isInvalid())
365 return;
366 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000367 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
368 Alignment.get());
Eli Friedmanec52f922012-02-23 23:47:16 +0000369}
370
Eli Friedman68be1642012-10-04 02:36:51 +0000371void Parser::HandlePragmaMSStruct() {
372 assert(Tok.is(tok::annot_pragma_msstruct));
Nico Weber779355f2016-03-02 23:22:00 +0000373 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
374 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Eli Friedman68be1642012-10-04 02:36:51 +0000375 Actions.ActOnPragmaMSStruct(Kind);
376 ConsumeToken(); // The annotation token.
377}
378
379void Parser::HandlePragmaAlign() {
380 assert(Tok.is(tok::annot_pragma_align));
381 Sema::PragmaOptionsAlignKind Kind =
382 static_cast<Sema::PragmaOptionsAlignKind>(
383 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
384 SourceLocation PragmaLoc = ConsumeToken();
385 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
386}
387
Richard Smithba3a4f92016-01-12 21:59:26 +0000388void Parser::HandlePragmaDump() {
389 assert(Tok.is(tok::annot_pragma_dump));
390 IdentifierInfo *II =
391 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
392 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
393 ConsumeToken();
394}
395
Eli Friedman68be1642012-10-04 02:36:51 +0000396void Parser::HandlePragmaWeak() {
397 assert(Tok.is(tok::annot_pragma_weak));
398 SourceLocation PragmaLoc = ConsumeToken();
399 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
400 Tok.getLocation());
401 ConsumeToken(); // The weak name.
402}
403
404void Parser::HandlePragmaWeakAlias() {
405 assert(Tok.is(tok::annot_pragma_weakalias));
406 SourceLocation PragmaLoc = ConsumeToken();
407 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
408 SourceLocation WeakNameLoc = Tok.getLocation();
409 ConsumeToken();
410 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
411 SourceLocation AliasNameLoc = Tok.getLocation();
412 ConsumeToken();
413 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
414 WeakNameLoc, AliasNameLoc);
415
416}
417
418void Parser::HandlePragmaRedefineExtname() {
419 assert(Tok.is(tok::annot_pragma_redefine_extname));
420 SourceLocation RedefLoc = ConsumeToken();
421 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
422 SourceLocation RedefNameLoc = Tok.getLocation();
423 ConsumeToken();
424 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
425 SourceLocation AliasNameLoc = Tok.getLocation();
426 ConsumeToken();
427 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
428 RedefNameLoc, AliasNameLoc);
429}
430
431void Parser::HandlePragmaFPContract() {
432 assert(Tok.is(tok::annot_pragma_fp_contract));
433 tok::OnOffSwitch OOS =
434 static_cast<tok::OnOffSwitch>(
435 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
436 Actions.ActOnPragmaFPContract(OOS);
437 ConsumeToken(); // The annotation token.
438}
439
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000440StmtResult Parser::HandlePragmaCaptured()
441{
442 assert(Tok.is(tok::annot_pragma_captured));
443 ConsumeToken();
444
445 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000446 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000447 return StmtError();
448 }
449
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000450 SourceLocation Loc = Tok.getLocation();
451
452 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000453 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
454 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000455
456 StmtResult R = ParseCompoundStatement();
457 CapturedRegionScope.Exit();
458
459 if (R.isInvalid()) {
460 Actions.ActOnCapturedRegionError();
461 return StmtError();
462 }
463
464 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000465}
466
Eli Friedman68be1642012-10-04 02:36:51 +0000467namespace {
468 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
469}
470
471void Parser::HandlePragmaOpenCLExtension() {
472 assert(Tok.is(tok::annot_pragma_opencl_extension));
473 OpenCLExtData data =
474 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
475 unsigned state = data.getInt();
476 IdentifierInfo *ename = data.getPointer();
477 SourceLocation NameLoc = Tok.getLocation();
478 ConsumeToken(); // The annotation token.
479
480 OpenCLOptions &f = Actions.getOpenCLOptions();
Yaxun Liu39cf40f2016-05-16 17:06:34 +0000481 auto CLVer = getLangOpts().OpenCLVersion;
482 auto &Supp = getTargetInfo().getSupportedOpenCLOpts();
Eli Friedman68be1642012-10-04 02:36:51 +0000483 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
484 // overriding all previously issued extension directives, but only if the
485 // behavior is set to disable."
486 if (state == 0 && ename->isStr("all")) {
Yaxun Liu39cf40f2016-05-16 17:06:34 +0000487#define OPENCLEXT(nm) \
488 if (Supp.is_##nm##_supported_extension(CLVer)) \
489 f.nm = 0;
Eli Friedman68be1642012-10-04 02:36:51 +0000490#include "clang/Basic/OpenCLExtensions.def"
491 }
Yaxun Liu39cf40f2016-05-16 17:06:34 +0000492#define OPENCLEXT(nm) else if (ename->isStr(#nm)) \
493 if (Supp.is_##nm##_supported_extension(CLVer)) \
494 f.nm = state; \
495 else if (Supp.is_##nm##_supported_core(CLVer)) \
496 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << ename; \
497 else \
498 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << ename;
Eli Friedman68be1642012-10-04 02:36:51 +0000499#include "clang/Basic/OpenCLExtensions.def"
500 else {
501 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
502 return;
503 }
504}
505
David Majnemer4bb09802014-02-10 19:50:15 +0000506void Parser::HandlePragmaMSPointersToMembers() {
507 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000508 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
509 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000510 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
511 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
512 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
513}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000514
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000515void Parser::HandlePragmaMSVtorDisp() {
516 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
517 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
Denis Zobnin2290dac2016-04-29 11:27:00 +0000518 Sema::PragmaMsStackAction Action =
519 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000520 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
521 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
Denis Zobnin2290dac2016-04-29 11:27:00 +0000522 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000523}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000524
Warren Huntc3b18962014-04-08 22:30:47 +0000525void Parser::HandlePragmaMSPragma() {
526 assert(Tok.is(tok::annot_pragma_ms_pragma));
527 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000528 auto TheTokens =
529 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
530 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Warren Huntc3b18962014-04-08 22:30:47 +0000531 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
532 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000533 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000534 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000535
Warren Huntc3b18962014-04-08 22:30:47 +0000536 // Figure out which #pragma we're dealing with. The switch has no default
537 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000538 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000539 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
540 .Case("data_seg", &Parser::HandlePragmaMSSegment)
541 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
542 .Case("const_seg", &Parser::HandlePragmaMSSegment)
543 .Case("code_seg", &Parser::HandlePragmaMSSegment)
544 .Case("section", &Parser::HandlePragmaMSSection)
545 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000546
547 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
548 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
549 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000550 while (Tok.isNot(tok::eof))
551 PP.Lex(Tok);
552 PP.Lex(Tok);
553 }
554}
555
Reid Kleckner722b1df2014-07-18 00:13:16 +0000556bool Parser::HandlePragmaMSSection(StringRef PragmaName,
557 SourceLocation PragmaLocation) {
558 if (Tok.isNot(tok::l_paren)) {
559 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
560 return false;
561 }
Warren Huntc3b18962014-04-08 22:30:47 +0000562 PP.Lex(Tok); // (
563 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000564 if (Tok.isNot(tok::string_literal)) {
565 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
566 << PragmaName;
567 return false;
568 }
569 ExprResult StringResult = ParseStringLiteralExpression();
570 if (StringResult.isInvalid())
571 return false; // Already diagnosed.
572 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
573 if (SegmentName->getCharByteWidth() != 1) {
574 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
575 << PragmaName;
576 return false;
577 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000578 int SectionFlags = ASTContext::PSF_Read;
579 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000580 while (Tok.is(tok::comma)) {
581 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000582 // Ignore "long" and "short".
583 // They are undocumented, but widely used, section attributes which appear
584 // to do nothing.
585 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
586 PP.Lex(Tok); // long/short
587 continue;
588 }
589
Reid Kleckner722b1df2014-07-18 00:13:16 +0000590 if (!Tok.isAnyIdentifier()) {
591 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
592 << PragmaName;
593 return false;
594 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000595 ASTContext::PragmaSectionFlag Flag =
596 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000597 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000598 .Case("read", ASTContext::PSF_Read)
599 .Case("write", ASTContext::PSF_Write)
600 .Case("execute", ASTContext::PSF_Execute)
601 .Case("shared", ASTContext::PSF_Invalid)
602 .Case("nopage", ASTContext::PSF_Invalid)
603 .Case("nocache", ASTContext::PSF_Invalid)
604 .Case("discard", ASTContext::PSF_Invalid)
605 .Case("remove", ASTContext::PSF_Invalid)
606 .Default(ASTContext::PSF_None);
607 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
608 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000609 ? diag::warn_pragma_invalid_specific_action
610 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000611 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000612 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000613 }
614 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000615 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000616 PP.Lex(Tok); // Identifier
617 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000618 // If no section attributes are specified, the section will be marked as
619 // read/write.
620 if (SectionFlagsAreDefault)
621 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000622 if (Tok.isNot(tok::r_paren)) {
623 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
624 return false;
625 }
Warren Huntc3b18962014-04-08 22:30:47 +0000626 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000627 if (Tok.isNot(tok::eof)) {
628 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
629 << PragmaName;
630 return false;
631 }
Warren Huntc3b18962014-04-08 22:30:47 +0000632 PP.Lex(Tok); // eof
633 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000634 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000635}
636
Reid Kleckner722b1df2014-07-18 00:13:16 +0000637bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
638 SourceLocation PragmaLocation) {
639 if (Tok.isNot(tok::l_paren)) {
640 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
641 return false;
642 }
Warren Huntc3b18962014-04-08 22:30:47 +0000643 PP.Lex(Tok); // (
644 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000645 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000646 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000647 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000648 if (PushPop == "push")
649 Action = Sema::PSK_Push;
650 else if (PushPop == "pop")
651 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000652 else {
653 PP.Diag(PragmaLocation,
654 diag::warn_pragma_expected_section_push_pop_or_name)
655 << PragmaName;
656 return false;
657 }
Warren Huntc3b18962014-04-08 22:30:47 +0000658 if (Action != Sema::PSK_Reset) {
659 PP.Lex(Tok); // push | pop
660 if (Tok.is(tok::comma)) {
661 PP.Lex(Tok); // ,
662 // If we've got a comma, we either need a label or a string.
663 if (Tok.isAnyIdentifier()) {
664 SlotLabel = Tok.getIdentifierInfo()->getName();
665 PP.Lex(Tok); // identifier
666 if (Tok.is(tok::comma))
667 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000668 else if (Tok.isNot(tok::r_paren)) {
669 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
670 << PragmaName;
671 return false;
672 }
Warren Huntc3b18962014-04-08 22:30:47 +0000673 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000674 } else if (Tok.isNot(tok::r_paren)) {
675 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
676 return false;
677 }
Warren Huntc3b18962014-04-08 22:30:47 +0000678 }
679 }
680 // Grab the string literal for our section name.
681 StringLiteral *SegmentName = nullptr;
682 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000683 if (Tok.isNot(tok::string_literal)) {
684 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000685 diag::warn_pragma_expected_section_name :
686 diag::warn_pragma_expected_section_label_or_name :
687 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000688 PP.Diag(PragmaLocation, DiagID) << PragmaName;
689 return false;
690 }
691 ExprResult StringResult = ParseStringLiteralExpression();
692 if (StringResult.isInvalid())
693 return false; // Already diagnosed.
694 SegmentName = cast<StringLiteral>(StringResult.get());
695 if (SegmentName->getCharByteWidth() != 1) {
696 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
697 << PragmaName;
698 return false;
699 }
Warren Huntc3b18962014-04-08 22:30:47 +0000700 // Setting section "" has no effect
701 if (SegmentName->getLength())
702 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
703 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000704 if (Tok.isNot(tok::r_paren)) {
705 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
706 return false;
707 }
Warren Huntc3b18962014-04-08 22:30:47 +0000708 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000709 if (Tok.isNot(tok::eof)) {
710 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
711 << PragmaName;
712 return false;
713 }
Warren Huntc3b18962014-04-08 22:30:47 +0000714 PP.Lex(Tok); // eof
715 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
716 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000717 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000718}
719
Reid Kleckner1a711b12014-07-22 00:53:05 +0000720// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000721bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
722 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000723 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
724 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
725 return false;
726 }
727
Reid Kleckner1a711b12014-07-22 00:53:05 +0000728 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
729 PragmaName))
730 return false;
731
732 // Parse either the known section names or the string section name.
733 StringLiteral *SegmentName = nullptr;
734 if (Tok.isAnyIdentifier()) {
735 auto *II = Tok.getIdentifierInfo();
736 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
737 .Case("compiler", "\".CRT$XCC\"")
738 .Case("lib", "\".CRT$XCL\"")
739 .Case("user", "\".CRT$XCU\"")
740 .Default("");
741
742 if (!Section.empty()) {
743 // Pretend the user wrote the appropriate string literal here.
744 Token Toks[1];
745 Toks[0].startToken();
746 Toks[0].setKind(tok::string_literal);
747 Toks[0].setLocation(Tok.getLocation());
748 Toks[0].setLiteralData(Section.data());
749 Toks[0].setLength(Section.size());
750 SegmentName =
751 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
752 PP.Lex(Tok);
753 }
754 } else if (Tok.is(tok::string_literal)) {
755 ExprResult StringResult = ParseStringLiteralExpression();
756 if (StringResult.isInvalid())
757 return false;
758 SegmentName = cast<StringLiteral>(StringResult.get());
759 if (SegmentName->getCharByteWidth() != 1) {
760 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
761 << PragmaName;
762 return false;
763 }
764 // FIXME: Add support for the '[, func-name]' part of the pragma.
765 }
766
767 if (!SegmentName) {
768 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
769 return false;
770 }
771
772 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
773 PragmaName) ||
774 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
775 PragmaName))
776 return false;
777
778 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
779 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000780}
781
Benjamin Kramere003ca22015-10-28 13:54:16 +0000782namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000783struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000784 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000785 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000786 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000787};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000788} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +0000789
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000790static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
791 std::string PragmaString;
792 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
793 PragmaString = "clang loop ";
794 PragmaString += Option.getIdentifierInfo()->getName();
795 } else {
796 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
797 "Unexpected pragma name");
798 PragmaString = "unroll";
799 }
800 return PragmaString;
801}
802
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000803bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000804 assert(Tok.is(tok::annot_pragma_loop_hint));
805 PragmaLoopHintInfo *Info =
806 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
807
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000808 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
809 Hint.PragmaNameLoc = IdentifierLoc::create(
810 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000811
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000812 // It is possible that the loop hint has no option identifier, such as
813 // #pragma unroll(4).
814 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
815 ? Info->Option.getIdentifierInfo()
816 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000817 Hint.OptionLoc = IdentifierLoc::create(
818 Actions.Context, Info->Option.getLocation(), OptionInfo);
819
David Blaikie2eabcc92016-02-09 18:52:09 +0000820 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000821
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000822 // Return a valid hint if pragma unroll or nounroll were specified
823 // without an argument.
824 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
825 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Blaikie2eabcc92016-02-09 18:52:09 +0000826 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000827 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000828 Hint.Range = Info->PragmaName.getLocation();
829 return true;
830 }
831
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000832 // The constant expression is always followed by an eof token, which increases
833 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +0000834 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000835 "PragmaLoopHintInfo::Toks must contain at least one token.");
836
837 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000838 bool OptionUnroll = false;
Adam Nemet2de463e2016-06-14 12:04:26 +0000839 bool OptionDistribute = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000840 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000841 if (OptionInfo) { // Pragma Unroll does not specify an option.
842 OptionUnroll = OptionInfo->isStr("unroll");
Adam Nemet2de463e2016-06-14 12:04:26 +0000843 OptionDistribute = OptionInfo->isStr("distribute");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000844 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
845 .Case("vectorize", true)
846 .Case("interleave", true)
Adam Nemet2de463e2016-06-14 12:04:26 +0000847 .Default(false) ||
848 OptionUnroll || OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000849 }
850
Adam Nemet2de463e2016-06-14 12:04:26 +0000851 bool AssumeSafetyArg = !OptionUnroll && !OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000852 // Verify loop hint has an argument.
853 if (Toks[0].is(tok::eof)) {
854 ConsumeToken(); // The annotation token.
855 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Adam Nemet2de463e2016-06-14 12:04:26 +0000856 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll
857 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000858 return false;
859 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000860
861 // Validate the argument.
862 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000863 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000864 SourceLocation StateLoc = Toks[0].getLocation();
865 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Adam Nemet50de4e82016-04-19 22:17:45 +0000866
867 bool Valid = StateInfo &&
868 llvm::StringSwitch<bool>(StateInfo->getName())
869 .Cases("enable", "disable", true)
870 .Case("full", OptionUnroll)
Adam Nemet2de463e2016-06-14 12:04:26 +0000871 .Case("assume_safety", AssumeSafetyArg)
Adam Nemet50de4e82016-04-19 22:17:45 +0000872 .Default(false);
873 if (!Valid) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000874 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
Adam Nemet2de463e2016-06-14 12:04:26 +0000875 << /*FullKeyword=*/OptionUnroll
876 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000877 return false;
878 }
David Blaikie2eabcc92016-02-09 18:52:09 +0000879 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000880 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
881 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000882 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
883 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000884 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000885 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000886 ConsumeToken(); // The annotation token.
887
888 ExprResult R = ParseConstantExpression();
889
890 // Tokens following an error in an ill-formed constant expression will
891 // remain in the token stream and must be removed.
892 if (Tok.isNot(tok::eof)) {
893 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
894 << PragmaLoopHintString(Info->PragmaName, Info->Option);
895 while (Tok.isNot(tok::eof))
896 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000897 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000898
899 ConsumeToken(); // Consume the constant expression eof terminator.
900
901 if (R.isInvalid() ||
902 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
903 return false;
904
905 // Argument is a constant expression with an integer type.
906 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000907 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000908
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000909 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +0000910 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000911 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000912}
913
914// #pragma GCC visibility comes in two variants:
915// 'push' '(' [visibility] ')'
916// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000917void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
918 PragmaIntroducerKind Introducer,
919 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000920 SourceLocation VisLoc = VisTok.getLocation();
921
922 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000923 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000924
925 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
926
Eli Friedman570024a2010-08-05 06:57:20 +0000927 const IdentifierInfo *VisType;
928 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000929 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000930 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000931 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000932 if (Tok.isNot(tok::l_paren)) {
933 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
934 << "visibility";
935 return;
936 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000937 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000938 VisType = Tok.getIdentifierInfo();
939 if (!VisType) {
940 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
941 << "visibility";
942 return;
943 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000944 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000945 if (Tok.isNot(tok::r_paren)) {
946 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
947 << "visibility";
948 return;
949 }
950 } else {
951 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
952 << "visibility";
953 return;
954 }
David Majnemera8f2f1d2015-03-19 00:10:23 +0000955 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000956 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000957 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000958 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
959 << "visibility";
960 return;
961 }
962
David Blaikie2eabcc92016-02-09 18:52:09 +0000963 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +0000964 Toks[0].startToken();
965 Toks[0].setKind(tok::annot_pragma_vis);
966 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000967 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +0000968 Toks[0].setAnnotationValue(
969 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +0000970 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000971}
972
Daniel Dunbar921b9682008-10-04 19:21:03 +0000973// #pragma pack(...) comes in the following delicious flavors:
974// pack '(' [integer] ')'
975// pack '(' 'show' ')'
976// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000977void PragmaPackHandler::HandlePragma(Preprocessor &PP,
978 PragmaIntroducerKind Introducer,
979 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000980 SourceLocation PackLoc = PackTok.getLocation();
981
982 Token Tok;
983 PP.Lex(Tok);
984 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000985 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000986 return;
987 }
988
Denis Zobnin10c4f452016-04-29 18:17:40 +0000989 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
990 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000991 Token Alignment;
992 Alignment.startToken();
Mike Stump11289f42009-09-09 15:08:12 +0000993 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000994 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000995 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000996
997 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000998
999 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1000 // the push/pop stack.
1001 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
Denis Zobnin10c4f452016-04-29 18:17:40 +00001002 Action =
1003 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001004 } else if (Tok.is(tok::identifier)) {
1005 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +00001006 if (II->isStr("show")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001007 Action = Sema::PSK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001008 PP.Lex(Tok);
1009 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001010 if (II->isStr("push")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001011 Action = Sema::PSK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +00001012 } else if (II->isStr("pop")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001013 Action = Sema::PSK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001014 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001015 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001016 return;
Mike Stump11289f42009-09-09 15:08:12 +00001017 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001018 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001019
Daniel Dunbar921b9682008-10-04 19:21:03 +00001020 if (Tok.is(tok::comma)) {
1021 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001022
Daniel Dunbar921b9682008-10-04 19:21:03 +00001023 if (Tok.is(tok::numeric_constant)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001024 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001025 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001026
1027 PP.Lex(Tok);
1028 } else if (Tok.is(tok::identifier)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001029 SlotLabel = Tok.getIdentifierInfo()->getName();
Daniel Dunbar921b9682008-10-04 19:21:03 +00001030 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001031
Daniel Dunbar921b9682008-10-04 19:21:03 +00001032 if (Tok.is(tok::comma)) {
1033 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001034
Daniel Dunbar921b9682008-10-04 19:21:03 +00001035 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001036 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001037 return;
1038 }
Mike Stump11289f42009-09-09 15:08:12 +00001039
Denis Zobnin10c4f452016-04-29 18:17:40 +00001040 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001041 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001042
1043 PP.Lex(Tok);
1044 }
1045 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001046 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001047 return;
1048 }
1049 }
1050 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001051 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001052 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1053 // the push/pop stack.
1054 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
Denis Zobnin10c4f452016-04-29 18:17:40 +00001055 Action = Sema::PSK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001056 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001057
1058 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001059 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001060 return;
1061 }
1062
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001063 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001064 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001065 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001066 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1067 return;
1068 }
1069
David Blaikie2eabcc92016-02-09 18:52:09 +00001070 PragmaPackInfo *Info =
1071 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Denis Zobnin10c4f452016-04-29 18:17:40 +00001072 Info->Action = Action;
1073 Info->SlotLabel = SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001074 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001075
David Blaikie2eabcc92016-02-09 18:52:09 +00001076 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1077 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001078 Toks[0].startToken();
1079 Toks[0].setKind(tok::annot_pragma_pack);
1080 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001081 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001082 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001083 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001084}
1085
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001086// #pragma ms_struct on
1087// #pragma ms_struct off
1088void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1089 PragmaIntroducerKind Introducer,
1090 Token &MSStructTok) {
Nico Weber779355f2016-03-02 23:22:00 +00001091 PragmaMSStructKind Kind = PMSST_OFF;
1092
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001093 Token Tok;
1094 PP.Lex(Tok);
1095 if (Tok.isNot(tok::identifier)) {
1096 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1097 return;
1098 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001099 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001100 const IdentifierInfo *II = Tok.getIdentifierInfo();
1101 if (II->isStr("on")) {
Nico Weber779355f2016-03-02 23:22:00 +00001102 Kind = PMSST_ON;
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001103 PP.Lex(Tok);
1104 }
1105 else if (II->isStr("off") || II->isStr("reset"))
1106 PP.Lex(Tok);
1107 else {
1108 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1109 return;
1110 }
1111
1112 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001113 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1114 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001115 return;
1116 }
Eli Friedman68be1642012-10-04 02:36:51 +00001117
David Blaikie2eabcc92016-02-09 18:52:09 +00001118 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1119 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001120 Toks[0].startToken();
1121 Toks[0].setKind(tok::annot_pragma_msstruct);
1122 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001123 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001124 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1125 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001126 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001127}
1128
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001129// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1130// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001131static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001132 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001133 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001134
1135 if (IsOptions) {
1136 PP.Lex(Tok);
1137 if (Tok.isNot(tok::identifier) ||
1138 !Tok.getIdentifierInfo()->isStr("align")) {
1139 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1140 return;
1141 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001142 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001143
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001144 PP.Lex(Tok);
1145 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001146 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1147 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001148 return;
1149 }
1150
1151 PP.Lex(Tok);
1152 if (Tok.isNot(tok::identifier)) {
1153 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001154 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001155 return;
1156 }
1157
John McCallfaf5fb42010-08-26 23:41:50 +00001158 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001159 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001160 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001161 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001162 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001163 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001164 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001165 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001166 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001167 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001168 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001169 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001170 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001171 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001172 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001173 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1174 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001175 return;
1176 }
1177
David Majnemera8f2f1d2015-03-19 00:10:23 +00001178 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001179 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001180 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001181 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001182 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001183 return;
1184 }
1185
David Blaikie2eabcc92016-02-09 18:52:09 +00001186 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1187 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001188 Toks[0].startToken();
1189 Toks[0].setKind(tok::annot_pragma_align);
1190 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001191 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001192 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1193 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001194 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001195}
1196
Douglas Gregorc7d65762010-09-09 22:45:38 +00001197void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1198 PragmaIntroducerKind Introducer,
1199 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001200 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001201}
1202
Douglas Gregorc7d65762010-09-09 22:45:38 +00001203void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1204 PragmaIntroducerKind Introducer,
1205 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001206 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001207}
1208
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001209// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001210void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1211 PragmaIntroducerKind Introducer,
1212 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001213 // FIXME: Should we be expanding macros here? My guess is no.
1214 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001215
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001216 // Lex the left '('.
1217 Token Tok;
1218 PP.Lex(Tok);
1219 if (Tok.isNot(tok::l_paren)) {
1220 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1221 return;
1222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001224 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001225 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001226 SourceLocation RParenLoc;
1227 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001228
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001229 while (true) {
1230 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001231
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001232 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001233 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001234 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001235 LexID = false;
1236 continue;
1237 }
1238
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001239 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001240 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1241 return;
1242 }
Mike Stump11289f42009-09-09 15:08:12 +00001243
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001244 // We are execting a ')' or a ','.
1245 if (Tok.is(tok::comma)) {
1246 LexID = true;
1247 continue;
1248 }
Mike Stump11289f42009-09-09 15:08:12 +00001249
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001250 if (Tok.is(tok::r_paren)) {
1251 RParenLoc = Tok.getLocation();
1252 break;
1253 }
Mike Stump11289f42009-09-09 15:08:12 +00001254
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001255 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001256 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001257 return;
1258 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001259
1260 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001261 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001262 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1263 "unused";
1264 return;
1265 }
1266
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001267 // Verify that we have a location for the right parenthesis.
1268 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001269 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001270
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001271 // For each identifier token, insert into the token stream a
1272 // annot_pragma_unused token followed by the identifier token.
1273 // This allows us to cache a "#pragma unused" that occurs inside an inline
1274 // C++ member function.
1275
David Blaikie2eabcc92016-02-09 18:52:09 +00001276 MutableArrayRef<Token> Toks(
1277 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1278 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001279 for (unsigned i=0; i != Identifiers.size(); i++) {
1280 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1281 pragmaUnusedTok.startToken();
1282 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1283 pragmaUnusedTok.setLocation(UnusedLoc);
1284 idTok = Identifiers[i];
1285 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001286 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001287}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001288
1289// #pragma weak identifier
1290// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001291void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1292 PragmaIntroducerKind Introducer,
1293 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001294 SourceLocation WeakLoc = WeakTok.getLocation();
1295
1296 Token Tok;
1297 PP.Lex(Tok);
1298 if (Tok.isNot(tok::identifier)) {
1299 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1300 return;
1301 }
1302
Eli Friedman68be1642012-10-04 02:36:51 +00001303 Token WeakName = Tok;
1304 bool HasAlias = false;
1305 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001306
1307 PP.Lex(Tok);
1308 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001309 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001310 PP.Lex(Tok);
1311 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001312 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001313 << "weak";
1314 return;
1315 }
Eli Friedman68be1642012-10-04 02:36:51 +00001316 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001317 PP.Lex(Tok);
1318 }
1319
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001320 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001321 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1322 return;
1323 }
1324
Eli Friedman68be1642012-10-04 02:36:51 +00001325 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00001326 MutableArrayRef<Token> Toks(
1327 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001328 Token &pragmaUnusedTok = Toks[0];
1329 pragmaUnusedTok.startToken();
1330 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1331 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001332 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001333 Toks[1] = WeakName;
1334 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001335 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001336 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00001337 MutableArrayRef<Token> Toks(
1338 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00001339 Token &pragmaUnusedTok = Toks[0];
1340 pragmaUnusedTok.startToken();
1341 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1342 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001343 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001344 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001345 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001346 }
1347}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001348
David Chisnall0867d9c2012-02-18 16:12:34 +00001349// #pragma redefine_extname identifier identifier
1350void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1351 PragmaIntroducerKind Introducer,
1352 Token &RedefToken) {
1353 SourceLocation RedefLoc = RedefToken.getLocation();
1354
1355 Token Tok;
1356 PP.Lex(Tok);
1357 if (Tok.isNot(tok::identifier)) {
1358 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1359 "redefine_extname";
1360 return;
1361 }
1362
Eli Friedman68be1642012-10-04 02:36:51 +00001363 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001364 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001365
David Chisnall0867d9c2012-02-18 16:12:34 +00001366 if (Tok.isNot(tok::identifier)) {
1367 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1368 << "redefine_extname";
1369 return;
1370 }
Eli Friedman68be1642012-10-04 02:36:51 +00001371
1372 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001373 PP.Lex(Tok);
1374
1375 if (Tok.isNot(tok::eod)) {
1376 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1377 "redefine_extname";
1378 return;
1379 }
1380
David Blaikie2eabcc92016-02-09 18:52:09 +00001381 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1382 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001383 Token &pragmaRedefTok = Toks[0];
1384 pragmaRedefTok.startToken();
1385 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1386 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001387 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001388 Toks[1] = RedefName;
1389 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001390 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
David Chisnall0867d9c2012-02-18 16:12:34 +00001391}
1392
1393
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001394void
1395PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1396 PragmaIntroducerKind Introducer,
1397 Token &Tok) {
1398 tok::OnOffSwitch OOS;
1399 if (PP.LexOnOffSwitch(OOS))
1400 return;
1401
David Blaikie2eabcc92016-02-09 18:52:09 +00001402 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1403 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001404 Toks[0].startToken();
1405 Toks[0].setKind(tok::annot_pragma_fp_contract);
1406 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001407 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001408 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1409 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001410 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001411}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001412
1413void
1414PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1415 PragmaIntroducerKind Introducer,
1416 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001417 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001418 if (Tok.isNot(tok::identifier)) {
1419 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1420 "OPENCL";
1421 return;
1422 }
1423 IdentifierInfo *ename = Tok.getIdentifierInfo();
1424 SourceLocation NameLoc = Tok.getLocation();
1425
1426 PP.Lex(Tok);
1427 if (Tok.isNot(tok::colon)) {
1428 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1429 return;
1430 }
1431
1432 PP.Lex(Tok);
1433 if (Tok.isNot(tok::identifier)) {
1434 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1435 return;
1436 }
1437 IdentifierInfo *op = Tok.getIdentifierInfo();
1438
1439 unsigned state;
1440 if (op->isStr("enable")) {
1441 state = 1;
1442 } else if (op->isStr("disable")) {
1443 state = 0;
1444 } else {
1445 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1446 return;
1447 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001448 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001449
Eli Friedman68be1642012-10-04 02:36:51 +00001450 PP.Lex(Tok);
1451 if (Tok.isNot(tok::eod)) {
1452 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1453 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001454 return;
1455 }
Eli Friedman68be1642012-10-04 02:36:51 +00001456
1457 OpenCLExtData data(ename, state);
David Blaikie2eabcc92016-02-09 18:52:09 +00001458 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1459 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001460 Toks[0].startToken();
1461 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1462 Toks[0].setLocation(NameLoc);
1463 Toks[0].setAnnotationValue(data.getOpaqueValue());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001464 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00001465 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001466
1467 if (PP.getPPCallbacks())
1468 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1469 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001470}
1471
Alexey Bataeva769e072013-03-22 06:34:35 +00001472/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1473///
1474void
1475PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1476 PragmaIntroducerKind Introducer,
1477 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001478 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1479 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001480 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001481 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1482 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001483 }
1484 PP.DiscardUntilEndOfDirective();
1485}
1486
1487/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1488///
1489void
1490PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1491 PragmaIntroducerKind Introducer,
1492 Token &FirstTok) {
1493 SmallVector<Token, 16> Pragma;
1494 Token Tok;
1495 Tok.startToken();
1496 Tok.setKind(tok::annot_pragma_openmp);
1497 Tok.setLocation(FirstTok.getLocation());
1498
1499 while (Tok.isNot(tok::eod)) {
1500 Pragma.push_back(Tok);
1501 PP.Lex(Tok);
1502 }
1503 SourceLocation EodLoc = Tok.getLocation();
1504 Tok.startToken();
1505 Tok.setKind(tok::annot_pragma_openmp_end);
1506 Tok.setLocation(EodLoc);
1507 Pragma.push_back(Tok);
1508
David Blaikie2eabcc92016-02-09 18:52:09 +00001509 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
1510 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
1511 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
1512 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00001513}
Reid Kleckner002562a2013-05-06 21:02:12 +00001514
David Majnemer4bb09802014-02-10 19:50:15 +00001515/// \brief Handle '#pragma pointers_to_members'
1516// The grammar for this pragma is as follows:
1517//
1518// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1519//
1520// #pragma pointers_to_members '(' 'best_case' ')'
1521// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1522// #pragma pointers_to_members '(' inheritance-model ')'
1523void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1524 PragmaIntroducerKind Introducer,
1525 Token &Tok) {
1526 SourceLocation PointersToMembersLoc = Tok.getLocation();
1527 PP.Lex(Tok);
1528 if (Tok.isNot(tok::l_paren)) {
1529 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1530 << "pointers_to_members";
1531 return;
1532 }
1533 PP.Lex(Tok);
1534 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1535 if (!Arg) {
1536 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1537 << "pointers_to_members";
1538 return;
1539 }
1540 PP.Lex(Tok);
1541
David Majnemer86c318f2014-02-11 21:05:00 +00001542 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001543 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001544 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001545 } else {
1546 if (Arg->isStr("full_generality")) {
1547 if (Tok.is(tok::comma)) {
1548 PP.Lex(Tok);
1549
1550 Arg = Tok.getIdentifierInfo();
1551 if (!Arg) {
1552 PP.Diag(Tok.getLocation(),
1553 diag::err_pragma_pointers_to_members_unknown_kind)
1554 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1555 return;
1556 }
1557 PP.Lex(Tok);
1558 } else if (Tok.is(tok::r_paren)) {
1559 // #pragma pointers_to_members(full_generality) implicitly specifies
1560 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001561 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001562 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001563 } else {
1564 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1565 << "full_generality";
1566 return;
1567 }
1568 }
1569
1570 if (Arg) {
1571 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001572 RepresentationMethod =
1573 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001574 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001575 RepresentationMethod =
1576 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001577 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001578 RepresentationMethod =
1579 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001580 } else {
1581 PP.Diag(Tok.getLocation(),
1582 diag::err_pragma_pointers_to_members_unknown_kind)
1583 << Arg << /*HasPointerDeclaration*/ 1;
1584 return;
1585 }
1586 }
1587 }
1588
1589 if (Tok.isNot(tok::r_paren)) {
1590 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1591 << (Arg ? Arg->getName() : "full_generality");
1592 return;
1593 }
1594
David Majnemera8f2f1d2015-03-19 00:10:23 +00001595 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001596 PP.Lex(Tok);
1597 if (Tok.isNot(tok::eod)) {
1598 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1599 << "pointers_to_members";
1600 return;
1601 }
1602
1603 Token AnnotTok;
1604 AnnotTok.startToken();
1605 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1606 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001607 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001608 AnnotTok.setAnnotationValue(
1609 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1610 PP.EnterToken(AnnotTok);
1611}
1612
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001613/// \brief Handle '#pragma vtordisp'
1614// The grammar for this pragma is as follows:
1615//
1616// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1617//
1618// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1619// #pragma vtordisp '(' 'pop' ')'
1620// #pragma vtordisp '(' ')'
1621void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1622 PragmaIntroducerKind Introducer,
1623 Token &Tok) {
1624 SourceLocation VtorDispLoc = Tok.getLocation();
1625 PP.Lex(Tok);
1626 if (Tok.isNot(tok::l_paren)) {
1627 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1628 return;
1629 }
1630 PP.Lex(Tok);
1631
Denis Zobnin2290dac2016-04-29 11:27:00 +00001632 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001633 const IdentifierInfo *II = Tok.getIdentifierInfo();
1634 if (II) {
1635 if (II->isStr("push")) {
1636 // #pragma vtordisp(push, mode)
1637 PP.Lex(Tok);
1638 if (Tok.isNot(tok::comma)) {
1639 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1640 return;
1641 }
1642 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001643 Action = Sema::PSK_Push_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001644 // not push, could be on/off
1645 } else if (II->isStr("pop")) {
1646 // #pragma vtordisp(pop)
1647 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001648 Action = Sema::PSK_Pop;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001649 }
1650 // not push or pop, could be on/off
1651 } else {
1652 if (Tok.is(tok::r_paren)) {
1653 // #pragma vtordisp()
Denis Zobnin2290dac2016-04-29 11:27:00 +00001654 Action = Sema::PSK_Reset;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001655 }
1656 }
1657
1658
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001659 uint64_t Value = 0;
Denis Zobnin2290dac2016-04-29 11:27:00 +00001660 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001661 const IdentifierInfo *II = Tok.getIdentifierInfo();
1662 if (II && II->isStr("off")) {
1663 PP.Lex(Tok);
1664 Value = 0;
1665 } else if (II && II->isStr("on")) {
1666 PP.Lex(Tok);
1667 Value = 1;
1668 } else if (Tok.is(tok::numeric_constant) &&
1669 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1670 if (Value > 2) {
1671 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1672 << 0 << 2 << "vtordisp";
1673 return;
1674 }
1675 } else {
1676 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1677 << "vtordisp";
1678 return;
1679 }
1680 }
1681
1682 // Finish the pragma: ')' $
1683 if (Tok.isNot(tok::r_paren)) {
1684 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1685 return;
1686 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001687 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001688 PP.Lex(Tok);
1689 if (Tok.isNot(tok::eod)) {
1690 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1691 << "vtordisp";
1692 return;
1693 }
1694
1695 // Enter the annotation.
1696 Token AnnotTok;
1697 AnnotTok.startToken();
1698 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1699 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001700 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001701 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
Denis Zobnin2290dac2016-04-29 11:27:00 +00001702 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001703 PP.EnterToken(AnnotTok);
1704}
1705
Warren Huntc3b18962014-04-08 22:30:47 +00001706/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1707/// an annotation token.
1708void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1709 PragmaIntroducerKind Introducer,
1710 Token &Tok) {
1711 Token EoF, AnnotTok;
1712 EoF.startToken();
1713 EoF.setKind(tok::eof);
1714 AnnotTok.startToken();
1715 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1716 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001717 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001718 SmallVector<Token, 8> TokenVector;
1719 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001720 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001721 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001722 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1723 }
Warren Huntc3b18962014-04-08 22:30:47 +00001724 // Add a sentinal EoF token to the end of the list.
1725 TokenVector.push_back(EoF);
1726 // We must allocate this array with new because EnterTokenStream is going to
1727 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00001728 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
1729 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00001730 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00001731 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
1732 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00001733 AnnotTok.setAnnotationValue(Value);
1734 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001735}
1736
Aaron Ballman5d041be2013-06-04 02:07:14 +00001737/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1738///
1739/// The syntax is:
1740/// \code
1741/// #pragma detect_mismatch("name", "value")
1742/// \endcode
1743/// Where 'name' and 'value' are quoted strings. The values are embedded in
1744/// the object file and passed along to the linker. If the linker detects a
1745/// mismatch in the object file's values for the given name, a LNK2038 error
1746/// is emitted. See MSDN for more details.
1747void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1748 PragmaIntroducerKind Introducer,
1749 Token &Tok) {
Nico Webercbbaeb12016-03-02 19:28:54 +00001750 SourceLocation DetectMismatchLoc = Tok.getLocation();
Aaron Ballman5d041be2013-06-04 02:07:14 +00001751 PP.Lex(Tok);
1752 if (Tok.isNot(tok::l_paren)) {
Nico Webercbbaeb12016-03-02 19:28:54 +00001753 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001754 return;
1755 }
1756
1757 // Read the name to embed, which must be a string literal.
1758 std::string NameString;
1759 if (!PP.LexStringLiteral(Tok, NameString,
1760 "pragma detect_mismatch",
1761 /*MacroExpansion=*/true))
1762 return;
1763
1764 // Read the comma followed by a second string literal.
1765 std::string ValueString;
1766 if (Tok.isNot(tok::comma)) {
1767 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1768 return;
1769 }
1770
1771 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1772 /*MacroExpansion=*/true))
1773 return;
1774
1775 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001776 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001777 return;
1778 }
1779 PP.Lex(Tok); // Eat the r_paren.
1780
1781 if (Tok.isNot(tok::eod)) {
1782 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1783 return;
1784 }
1785
Reid Kleckner71966c92014-02-20 23:37:45 +00001786 // If the pragma is lexically sound, notify any interested PPCallbacks.
1787 if (PP.getPPCallbacks())
Nico Webercbbaeb12016-03-02 19:28:54 +00001788 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
Reid Kleckner71966c92014-02-20 23:37:45 +00001789 ValueString);
1790
Nico Webercbbaeb12016-03-02 19:28:54 +00001791 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
Aaron Ballman5d041be2013-06-04 02:07:14 +00001792}
1793
Reid Kleckner002562a2013-05-06 21:02:12 +00001794/// \brief Handle the microsoft \#pragma comment extension.
1795///
1796/// The syntax is:
1797/// \code
1798/// #pragma comment(linker, "foo")
1799/// \endcode
1800/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1801/// "foo" is a string, which is fully macro expanded, and permits string
1802/// concatenation, embedded escape characters etc. See MSDN for more details.
1803void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1804 PragmaIntroducerKind Introducer,
1805 Token &Tok) {
1806 SourceLocation CommentLoc = Tok.getLocation();
1807 PP.Lex(Tok);
1808 if (Tok.isNot(tok::l_paren)) {
1809 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1810 return;
1811 }
1812
1813 // Read the identifier.
1814 PP.Lex(Tok);
1815 if (Tok.isNot(tok::identifier)) {
1816 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1817 return;
1818 }
1819
1820 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001821 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00001822 PragmaMSCommentKind Kind =
1823 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
1824 .Case("linker", PCK_Linker)
1825 .Case("lib", PCK_Lib)
1826 .Case("compiler", PCK_Compiler)
1827 .Case("exestr", PCK_ExeStr)
1828 .Case("user", PCK_User)
1829 .Default(PCK_Unknown);
1830 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001831 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1832 return;
1833 }
1834
Yunzhong Gao99efc032015-03-23 20:41:42 +00001835 // On PS4, issue a warning about any pragma comments other than
1836 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00001837 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00001838 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1839 << II->getName();
1840 return;
1841 }
1842
Reid Kleckner002562a2013-05-06 21:02:12 +00001843 // Read the optional string if present.
1844 PP.Lex(Tok);
1845 std::string ArgumentString;
1846 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1847 "pragma comment",
1848 /*MacroExpansion=*/true))
1849 return;
1850
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001851 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001852 // FIXME: If the kind is "compiler" warn if the string is present (it is
1853 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001854 // The MSDN docs say that "lib" and "linker" require a string and have a short
1855 // whitelist of linker options they support, but in practice MSVC doesn't
1856 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001857
1858 if (Tok.isNot(tok::r_paren)) {
1859 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1860 return;
1861 }
1862 PP.Lex(Tok); // eat the r_paren.
1863
1864 if (Tok.isNot(tok::eod)) {
1865 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1866 return;
1867 }
1868
Reid Kleckner71966c92014-02-20 23:37:45 +00001869 // If the pragma is lexically sound, notify any interested PPCallbacks.
1870 if (PP.getPPCallbacks())
1871 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1872
Nico Weber66220292016-03-02 17:28:48 +00001873 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001874}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001875
1876// #pragma clang optimize off
1877// #pragma clang optimize on
1878void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1879 PragmaIntroducerKind Introducer,
1880 Token &FirstToken) {
1881 Token Tok;
1882 PP.Lex(Tok);
1883 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001884 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001885 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001886 return;
1887 }
1888 if (Tok.isNot(tok::identifier)) {
1889 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1890 << PP.getSpelling(Tok);
1891 return;
1892 }
1893 const IdentifierInfo *II = Tok.getIdentifierInfo();
1894 // The only accepted values are 'on' or 'off'.
1895 bool IsOn = false;
1896 if (II->isStr("on")) {
1897 IsOn = true;
1898 } else if (!II->isStr("off")) {
1899 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1900 << PP.getSpelling(Tok);
1901 return;
1902 }
1903 PP.Lex(Tok);
1904
1905 if (Tok.isNot(tok::eod)) {
1906 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1907 << PP.getSpelling(Tok);
1908 return;
1909 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001910
1911 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1912}
1913
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001914/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001915static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1916 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001917 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001918 SmallVector<Token, 1> ValueList;
1919 int OpenParens = ValueInParens ? 1 : 0;
1920 // Read constant expression.
1921 while (Tok.isNot(tok::eod)) {
1922 if (Tok.is(tok::l_paren))
1923 OpenParens++;
1924 else if (Tok.is(tok::r_paren)) {
1925 OpenParens--;
1926 if (OpenParens == 0 && ValueInParens)
1927 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001928 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001929
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001930 ValueList.push_back(Tok);
1931 PP.Lex(Tok);
1932 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001933
1934 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001935 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001936 if (Tok.isNot(tok::r_paren)) {
1937 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1938 return true;
1939 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001940 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001941 }
1942
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001943 Token EOFTok;
1944 EOFTok.startToken();
1945 EOFTok.setKind(tok::eof);
1946 EOFTok.setLocation(Tok.getLocation());
1947 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1948
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001949 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001950
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001951 Info.PragmaName = PragmaName;
1952 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001953 return false;
1954}
1955
Eli Bendersky06a40422014-06-06 20:31:48 +00001956/// \brief Handle the \#pragma clang loop directive.
1957/// #pragma clang 'loop' loop-hints
1958///
1959/// loop-hints:
1960/// loop-hint loop-hints[opt]
1961///
1962/// loop-hint:
1963/// 'vectorize' '(' loop-hint-keyword ')'
1964/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001965/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001966/// 'vectorize_width' '(' loop-hint-value ')'
1967/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001968/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001969///
1970/// loop-hint-keyword:
1971/// 'enable'
1972/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00001973/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00001974///
Mark Heffernan450c2382014-07-23 17:31:31 +00001975/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00001976/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00001977/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00001978/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00001979///
Eli Bendersky06a40422014-06-06 20:31:48 +00001980/// loop-hint-value:
1981/// constant-expression
1982///
1983/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1984/// try vectorizing the instructions of the loop it precedes. Specifying
1985/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1986/// interleaving multiple iterations of the loop it precedes. The width of the
1987/// vector instructions is specified by vectorize_width() and the number of
1988/// interleaved loop iterations is specified by interleave_count(). Specifying a
1989/// value of 1 effectively disables vectorization/interleaving, even if it is
1990/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1991/// only works on inner loops.
1992///
Eli Bendersky86483b32014-06-11 17:56:26 +00001993/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00001994/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
1995/// completely if the trip count is known at compile time and unroll partially
1996/// if the trip count is not known. Specifying unroll(full) is similar to
1997/// unroll(enable) but will unroll the loop only if the trip count is known at
1998/// compile time. Specifying unroll(disable) disables unrolling for the
1999/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2000/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00002001void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2002 PragmaIntroducerKind Introducer,
2003 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002004 // Incoming token is "loop" from "#pragma clang loop".
2005 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00002006 SmallVector<Token, 1> TokenList;
2007
2008 // Lex the optimization option and verify it is an identifier.
2009 PP.Lex(Tok);
2010 if (Tok.isNot(tok::identifier)) {
2011 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2012 << /*MissingOption=*/true << "";
2013 return;
2014 }
2015
2016 while (Tok.is(tok::identifier)) {
2017 Token Option = Tok;
2018 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2019
Eli Bendersky86483b32014-06-11 17:56:26 +00002020 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002021 .Case("vectorize", true)
2022 .Case("interleave", true)
2023 .Case("unroll", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00002024 .Case("distribute", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002025 .Case("vectorize_width", true)
2026 .Case("interleave_count", true)
2027 .Case("unroll_count", true)
2028 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002029 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002030 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2031 << /*MissingOption=*/false << OptionInfo;
2032 return;
2033 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002034 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002035
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002036 // Read '('
2037 if (Tok.isNot(tok::l_paren)) {
2038 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002039 return;
2040 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002041 PP.Lex(Tok);
2042
2043 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2044 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2045 *Info))
2046 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002047
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002048 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002049 Token LoopHintTok;
2050 LoopHintTok.startToken();
2051 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002052 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002053 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002054 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2055 TokenList.push_back(LoopHintTok);
2056 }
2057
2058 if (Tok.isNot(tok::eod)) {
2059 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2060 << "clang loop";
2061 return;
2062 }
2063
David Blaikie2eabcc92016-02-09 18:52:09 +00002064 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2065 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002066
David Blaikie2eabcc92016-02-09 18:52:09 +00002067 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2068 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002069}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002070
2071/// \brief Handle the loop unroll optimization pragmas.
2072/// #pragma unroll
2073/// #pragma unroll unroll-hint-value
2074/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002075/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002076///
2077/// unroll-hint-value:
2078/// constant-expression
2079///
Mark Heffernanc888e412014-07-24 18:09:38 +00002080/// Loop unrolling hints can be specified with '#pragma unroll' or
2081/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2082/// contained in parentheses. With no argument the directive instructs llvm to
2083/// try to unroll the loop completely. A positive integer argument can be
2084/// specified to indicate the number of times the loop should be unrolled. To
2085/// maximize compatibility with other compilers the unroll count argument can be
2086/// specified with or without parentheses. Specifying, '#pragma nounroll'
2087/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002088void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2089 PragmaIntroducerKind Introducer,
2090 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002091 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2092 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002093 Token PragmaName = Tok;
2094 PP.Lex(Tok);
2095 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2096 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002097 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002098 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002099 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002100 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2101 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2102 << "nounroll";
2103 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002104 } else {
2105 // Unroll pragma with an argument: "#pragma unroll N" or
2106 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002107 // Read '(' if it exists.
2108 bool ValueInParens = Tok.is(tok::l_paren);
2109 if (ValueInParens)
2110 PP.Lex(Tok);
2111
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002112 Token Option;
2113 Option.startToken();
2114 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002115 return;
2116
2117 // In CUDA, the argument to '#pragma unroll' should not be contained in
2118 // parentheses.
2119 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002120 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002121 diag::warn_pragma_unroll_cuda_value_in_parens);
2122
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002123 if (Tok.isNot(tok::eod)) {
2124 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2125 << "unroll";
2126 return;
2127 }
2128 }
2129
2130 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002131 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002132 TokenArray[0].startToken();
2133 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2134 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002135 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002136 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002137 PP.EnterTokenStream(std::move(TokenArray), 1,
2138 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002139}
Reid Kleckner3f1ec622016-09-07 16:38:32 +00002140
2141/// \brief Handle the Microsoft \#pragma intrinsic extension.
2142///
2143/// The syntax is:
2144/// \code
2145/// #pragma intrinsic(memset)
2146/// #pragma intrinsic(strlen, memcpy)
2147/// \endcode
2148///
2149/// Pragma intrisic tells the compiler to use a builtin version of the
2150/// function. Clang does it anyway, so the pragma doesn't really do anything.
2151/// Anyway, we emit a warning if the function specified in \#pragma intrinsic
2152/// isn't an intrinsic in clang and suggest to include intrin.h.
2153void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
2154 PragmaIntroducerKind Introducer,
2155 Token &Tok) {
2156 PP.Lex(Tok);
2157
2158 if (Tok.isNot(tok::l_paren)) {
2159 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
2160 << "intrinsic";
2161 return;
2162 }
2163 PP.Lex(Tok);
2164
2165 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
2166
2167 while (Tok.is(tok::identifier)) {
2168 IdentifierInfo *II = Tok.getIdentifierInfo();
2169 if (!II->getBuiltinID())
2170 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
2171 << II << SuggestIntrinH;
2172
2173 PP.Lex(Tok);
2174 if (Tok.isNot(tok::comma))
2175 break;
2176 PP.Lex(Tok);
2177 }
2178
2179 if (Tok.isNot(tok::r_paren)) {
2180 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
2181 << "intrinsic";
2182 return;
2183 }
2184 PP.Lex(Tok);
2185
2186 if (Tok.isNot(tok::eod))
2187 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2188 << "intrinsic";
2189}