blob: ab2b52ec396cedcc1531867f496a92dbedeb9885 [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"
David Majnemerad2986e2014-08-14 06:35:08 +000016#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/ParseDiagnostic.h"
19#include "clang/Parse/Parser.h"
20#include "clang/Sema/LoopHint.h"
21#include "clang/Sema/Scope.h"
22#include "llvm/ADT/StringSwitch.h"
23using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000024
Reid Kleckner5b086462014-02-20 22:52:09 +000025namespace {
26
27struct PragmaAlignHandler : public PragmaHandler {
28 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Craig Topper2b07f022014-03-12 05:09:18 +000029 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
30 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000031};
32
33struct PragmaGCCVisibilityHandler : public PragmaHandler {
34 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000035 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
36 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000037};
38
39struct PragmaOptionsHandler : public PragmaHandler {
40 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000041 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
42 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000043};
44
45struct PragmaPackHandler : public PragmaHandler {
46 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000047 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
48 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000049};
50
51struct PragmaMSStructHandler : public PragmaHandler {
52 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000053 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
54 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000055};
56
57struct PragmaUnusedHandler : public PragmaHandler {
58 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000059 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
60 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000061};
62
63struct PragmaWeakHandler : public PragmaHandler {
64 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000065 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
66 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000067};
68
69struct PragmaRedefineExtnameHandler : public PragmaHandler {
70 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000071 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
72 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000073};
74
75struct PragmaOpenCLExtensionHandler : public PragmaHandler {
76 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000077 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
78 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000079};
80
81
82struct PragmaFPContractHandler : public PragmaHandler {
83 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000084 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
85 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000086};
87
88struct PragmaNoOpenMPHandler : public PragmaHandler {
89 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000090 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
91 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000092};
93
94struct PragmaOpenMPHandler : public PragmaHandler {
95 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000096 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
97 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000098};
99
100/// PragmaCommentHandler - "\#pragma comment ...".
101struct PragmaCommentHandler : public PragmaHandler {
102 PragmaCommentHandler(Sema &Actions)
103 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000104 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
105 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000106private:
107 Sema &Actions;
108};
109
110struct PragmaDetectMismatchHandler : public PragmaHandler {
111 PragmaDetectMismatchHandler(Sema &Actions)
112 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000113 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
114 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000115private:
116 Sema &Actions;
117};
118
119struct PragmaMSPointersToMembers : public PragmaHandler {
120 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000121 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
122 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000123};
124
125struct PragmaMSVtorDisp : public PragmaHandler {
126 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000127 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
128 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000129};
130
Warren Huntc3b18962014-04-08 22:30:47 +0000131struct PragmaMSPragma : public PragmaHandler {
132 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000133 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
134 Token &FirstToken) override;
135};
136
Dario Domizioli13a0a382014-05-23 12:13:25 +0000137/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
138struct PragmaOptimizeHandler : public PragmaHandler {
139 PragmaOptimizeHandler(Sema &S)
140 : PragmaHandler("optimize"), Actions(S) {}
141 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
142 Token &FirstToken) override;
143private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000144 Sema &Actions;
145};
146
147struct PragmaLoopHintHandler : public PragmaHandler {
148 PragmaLoopHintHandler() : PragmaHandler("loop") {}
149 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
150 Token &FirstToken) override;
151};
152
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000153struct PragmaUnrollHintHandler : public PragmaHandler {
154 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
155 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
156 Token &FirstToken) override;
157};
158
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000159struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
160 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
161};
162
Eli Bendersky06a40422014-06-06 20:31:48 +0000163} // end namespace
164
165void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000166 AlignHandler.reset(new PragmaAlignHandler());
167 PP.AddPragmaHandler(AlignHandler.get());
168
169 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
170 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
171
172 OptionsHandler.reset(new PragmaOptionsHandler());
173 PP.AddPragmaHandler(OptionsHandler.get());
174
175 PackHandler.reset(new PragmaPackHandler());
176 PP.AddPragmaHandler(PackHandler.get());
177
178 MSStructHandler.reset(new PragmaMSStructHandler());
179 PP.AddPragmaHandler(MSStructHandler.get());
180
181 UnusedHandler.reset(new PragmaUnusedHandler());
182 PP.AddPragmaHandler(UnusedHandler.get());
183
184 WeakHandler.reset(new PragmaWeakHandler());
185 PP.AddPragmaHandler(WeakHandler.get());
186
187 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
188 PP.AddPragmaHandler(RedefineExtnameHandler.get());
189
190 FPContractHandler.reset(new PragmaFPContractHandler());
191 PP.AddPragmaHandler("STDC", FPContractHandler.get());
192
193 if (getLangOpts().OpenCL) {
194 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
195 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
196
197 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
198 }
199 if (getLangOpts().OpenMP)
200 OpenMPHandler.reset(new PragmaOpenMPHandler());
201 else
202 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
203 PP.AddPragmaHandler(OpenMPHandler.get());
204
Yunzhong Gao99efc032015-03-23 20:41:42 +0000205 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000206 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
207 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000208 }
209
210 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000211 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
212 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
213 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
214 PP.AddPragmaHandler(MSPointersToMembers.get());
215 MSVtorDisp.reset(new PragmaMSVtorDisp());
216 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000217 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000218 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000219 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
220 PP.AddPragmaHandler(MSDataSeg.get());
221 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
222 PP.AddPragmaHandler(MSBSSSeg.get());
223 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
224 PP.AddPragmaHandler(MSConstSeg.get());
225 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
226 PP.AddPragmaHandler(MSCodeSeg.get());
227 MSSection.reset(new PragmaMSPragma("section"));
228 PP.AddPragmaHandler(MSSection.get());
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000229 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
230 PP.AddPragmaHandler(MSRuntimeChecks.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000231 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000232
233 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
234 PP.AddPragmaHandler("clang", OptimizeHandler.get());
235
236 LoopHintHandler.reset(new PragmaLoopHintHandler());
237 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000238
239 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
240 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000241
242 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
243 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000244}
245
246void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000247 // Remove the pragma handlers we installed.
248 PP.RemovePragmaHandler(AlignHandler.get());
249 AlignHandler.reset();
250 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
251 GCCVisibilityHandler.reset();
252 PP.RemovePragmaHandler(OptionsHandler.get());
253 OptionsHandler.reset();
254 PP.RemovePragmaHandler(PackHandler.get());
255 PackHandler.reset();
256 PP.RemovePragmaHandler(MSStructHandler.get());
257 MSStructHandler.reset();
258 PP.RemovePragmaHandler(UnusedHandler.get());
259 UnusedHandler.reset();
260 PP.RemovePragmaHandler(WeakHandler.get());
261 WeakHandler.reset();
262 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
263 RedefineExtnameHandler.reset();
264
265 if (getLangOpts().OpenCL) {
266 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
267 OpenCLExtensionHandler.reset();
268 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
269 }
270 PP.RemovePragmaHandler(OpenMPHandler.get());
271 OpenMPHandler.reset();
272
Yunzhong Gao99efc032015-03-23 20:41:42 +0000273 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000274 PP.RemovePragmaHandler(MSCommentHandler.get());
275 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000276 }
277
278 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000279 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
280 MSDetectMismatchHandler.reset();
281 PP.RemovePragmaHandler(MSPointersToMembers.get());
282 MSPointersToMembers.reset();
283 PP.RemovePragmaHandler(MSVtorDisp.get());
284 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000285 PP.RemovePragmaHandler(MSInitSeg.get());
286 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000287 PP.RemovePragmaHandler(MSDataSeg.get());
288 MSDataSeg.reset();
289 PP.RemovePragmaHandler(MSBSSSeg.get());
290 MSBSSSeg.reset();
291 PP.RemovePragmaHandler(MSConstSeg.get());
292 MSConstSeg.reset();
293 PP.RemovePragmaHandler(MSCodeSeg.get());
294 MSCodeSeg.reset();
295 PP.RemovePragmaHandler(MSSection.get());
296 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000297 PP.RemovePragmaHandler(MSRuntimeChecks.get());
298 MSRuntimeChecks.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000299 }
300
301 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
302 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000303
304 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
305 OptimizeHandler.reset();
306
307 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
308 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000309
310 PP.RemovePragmaHandler(UnrollHintHandler.get());
311 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000312
313 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
314 NoUnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000315}
316
317/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000318///
319/// Each annot_pragma_unused is followed by the argument token so e.g.
320/// "#pragma unused(x,y)" becomes:
321/// annot_pragma_unused 'x' annot_pragma_unused 'y'
322void Parser::HandlePragmaUnused() {
323 assert(Tok.is(tok::annot_pragma_unused));
324 SourceLocation UnusedLoc = ConsumeToken();
325 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
326 ConsumeToken(); // The argument token.
327}
Eli Friedman570024a2010-08-05 06:57:20 +0000328
Rafael Espindola273fd772012-01-26 02:02:57 +0000329void Parser::HandlePragmaVisibility() {
330 assert(Tok.is(tok::annot_pragma_vis));
331 const IdentifierInfo *VisType =
332 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
333 SourceLocation VisLoc = ConsumeToken();
334 Actions.ActOnPragmaVisibility(VisType, VisLoc);
335}
336
Eli Friedmanec52f922012-02-23 23:47:16 +0000337struct PragmaPackInfo {
338 Sema::PragmaPackKind Kind;
339 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000340 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000341 SourceLocation LParenLoc;
342 SourceLocation RParenLoc;
343};
344
345void Parser::HandlePragmaPack() {
346 assert(Tok.is(tok::annot_pragma_pack));
347 PragmaPackInfo *Info =
348 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
349 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000350 ExprResult Alignment;
351 if (Info->Alignment.is(tok::numeric_constant)) {
352 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
353 if (Alignment.isInvalid())
354 return;
355 }
356 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000357 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000358}
359
Eli Friedman68be1642012-10-04 02:36:51 +0000360void Parser::HandlePragmaMSStruct() {
361 assert(Tok.is(tok::annot_pragma_msstruct));
362 Sema::PragmaMSStructKind Kind =
363 static_cast<Sema::PragmaMSStructKind>(
364 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
365 Actions.ActOnPragmaMSStruct(Kind);
366 ConsumeToken(); // The annotation token.
367}
368
369void Parser::HandlePragmaAlign() {
370 assert(Tok.is(tok::annot_pragma_align));
371 Sema::PragmaOptionsAlignKind Kind =
372 static_cast<Sema::PragmaOptionsAlignKind>(
373 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
374 SourceLocation PragmaLoc = ConsumeToken();
375 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
376}
377
378void Parser::HandlePragmaWeak() {
379 assert(Tok.is(tok::annot_pragma_weak));
380 SourceLocation PragmaLoc = ConsumeToken();
381 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
382 Tok.getLocation());
383 ConsumeToken(); // The weak name.
384}
385
386void Parser::HandlePragmaWeakAlias() {
387 assert(Tok.is(tok::annot_pragma_weakalias));
388 SourceLocation PragmaLoc = ConsumeToken();
389 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
390 SourceLocation WeakNameLoc = Tok.getLocation();
391 ConsumeToken();
392 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
393 SourceLocation AliasNameLoc = Tok.getLocation();
394 ConsumeToken();
395 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
396 WeakNameLoc, AliasNameLoc);
397
398}
399
400void Parser::HandlePragmaRedefineExtname() {
401 assert(Tok.is(tok::annot_pragma_redefine_extname));
402 SourceLocation RedefLoc = ConsumeToken();
403 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
404 SourceLocation RedefNameLoc = Tok.getLocation();
405 ConsumeToken();
406 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
407 SourceLocation AliasNameLoc = Tok.getLocation();
408 ConsumeToken();
409 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
410 RedefNameLoc, AliasNameLoc);
411}
412
413void Parser::HandlePragmaFPContract() {
414 assert(Tok.is(tok::annot_pragma_fp_contract));
415 tok::OnOffSwitch OOS =
416 static_cast<tok::OnOffSwitch>(
417 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
418 Actions.ActOnPragmaFPContract(OOS);
419 ConsumeToken(); // The annotation token.
420}
421
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000422StmtResult Parser::HandlePragmaCaptured()
423{
424 assert(Tok.is(tok::annot_pragma_captured));
425 ConsumeToken();
426
427 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000428 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000429 return StmtError();
430 }
431
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000432 SourceLocation Loc = Tok.getLocation();
433
434 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000435 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
436 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000437
438 StmtResult R = ParseCompoundStatement();
439 CapturedRegionScope.Exit();
440
441 if (R.isInvalid()) {
442 Actions.ActOnCapturedRegionError();
443 return StmtError();
444 }
445
446 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000447}
448
Eli Friedman68be1642012-10-04 02:36:51 +0000449namespace {
450 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
451}
452
453void Parser::HandlePragmaOpenCLExtension() {
454 assert(Tok.is(tok::annot_pragma_opencl_extension));
455 OpenCLExtData data =
456 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
457 unsigned state = data.getInt();
458 IdentifierInfo *ename = data.getPointer();
459 SourceLocation NameLoc = Tok.getLocation();
460 ConsumeToken(); // The annotation token.
461
462 OpenCLOptions &f = Actions.getOpenCLOptions();
463 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
464 // overriding all previously issued extension directives, but only if the
465 // behavior is set to disable."
466 if (state == 0 && ename->isStr("all")) {
467#define OPENCLEXT(nm) f.nm = 0;
468#include "clang/Basic/OpenCLExtensions.def"
469 }
470#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
471#include "clang/Basic/OpenCLExtensions.def"
472 else {
473 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
474 return;
475 }
476}
477
David Majnemer4bb09802014-02-10 19:50:15 +0000478void Parser::HandlePragmaMSPointersToMembers() {
479 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000480 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
481 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000482 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
483 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
484 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
485}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000486
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000487void Parser::HandlePragmaMSVtorDisp() {
488 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
489 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
490 Sema::PragmaVtorDispKind Kind =
491 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
492 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
493 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
494 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
495}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000496
Warren Huntc3b18962014-04-08 22:30:47 +0000497void Parser::HandlePragmaMSPragma() {
498 assert(Tok.is(tok::annot_pragma_ms_pragma));
499 // Grab the tokens out of the annotation and enter them into the stream.
500 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
501 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
502 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
503 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000504 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000505 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000506
Warren Huntc3b18962014-04-08 22:30:47 +0000507 // Figure out which #pragma we're dealing with. The switch has no default
508 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000509 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000510 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
511 .Case("data_seg", &Parser::HandlePragmaMSSegment)
512 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
513 .Case("const_seg", &Parser::HandlePragmaMSSegment)
514 .Case("code_seg", &Parser::HandlePragmaMSSegment)
515 .Case("section", &Parser::HandlePragmaMSSection)
516 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000517
518 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
519 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
520 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000521 while (Tok.isNot(tok::eof))
522 PP.Lex(Tok);
523 PP.Lex(Tok);
524 }
525}
526
Reid Kleckner722b1df2014-07-18 00:13:16 +0000527bool Parser::HandlePragmaMSSection(StringRef PragmaName,
528 SourceLocation PragmaLocation) {
529 if (Tok.isNot(tok::l_paren)) {
530 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
531 return false;
532 }
Warren Huntc3b18962014-04-08 22:30:47 +0000533 PP.Lex(Tok); // (
534 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000535 if (Tok.isNot(tok::string_literal)) {
536 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
537 << PragmaName;
538 return false;
539 }
540 ExprResult StringResult = ParseStringLiteralExpression();
541 if (StringResult.isInvalid())
542 return false; // Already diagnosed.
543 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
544 if (SegmentName->getCharByteWidth() != 1) {
545 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
546 << PragmaName;
547 return false;
548 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000549 int SectionFlags = ASTContext::PSF_Read;
550 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000551 while (Tok.is(tok::comma)) {
552 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000553 // Ignore "long" and "short".
554 // They are undocumented, but widely used, section attributes which appear
555 // to do nothing.
556 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
557 PP.Lex(Tok); // long/short
558 continue;
559 }
560
Reid Kleckner722b1df2014-07-18 00:13:16 +0000561 if (!Tok.isAnyIdentifier()) {
562 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
563 << PragmaName;
564 return false;
565 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000566 ASTContext::PragmaSectionFlag Flag =
567 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000568 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000569 .Case("read", ASTContext::PSF_Read)
570 .Case("write", ASTContext::PSF_Write)
571 .Case("execute", ASTContext::PSF_Execute)
572 .Case("shared", ASTContext::PSF_Invalid)
573 .Case("nopage", ASTContext::PSF_Invalid)
574 .Case("nocache", ASTContext::PSF_Invalid)
575 .Case("discard", ASTContext::PSF_Invalid)
576 .Case("remove", ASTContext::PSF_Invalid)
577 .Default(ASTContext::PSF_None);
578 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
579 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000580 ? diag::warn_pragma_invalid_specific_action
581 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000582 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000583 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000584 }
585 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000586 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000587 PP.Lex(Tok); // Identifier
588 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000589 // If no section attributes are specified, the section will be marked as
590 // read/write.
591 if (SectionFlagsAreDefault)
592 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000593 if (Tok.isNot(tok::r_paren)) {
594 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
595 return false;
596 }
Warren Huntc3b18962014-04-08 22:30:47 +0000597 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000598 if (Tok.isNot(tok::eof)) {
599 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
600 << PragmaName;
601 return false;
602 }
Warren Huntc3b18962014-04-08 22:30:47 +0000603 PP.Lex(Tok); // eof
604 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000605 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000606}
607
Reid Kleckner722b1df2014-07-18 00:13:16 +0000608bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
609 SourceLocation PragmaLocation) {
610 if (Tok.isNot(tok::l_paren)) {
611 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
612 return false;
613 }
Warren Huntc3b18962014-04-08 22:30:47 +0000614 PP.Lex(Tok); // (
615 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000616 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000617 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000618 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000619 if (PushPop == "push")
620 Action = Sema::PSK_Push;
621 else if (PushPop == "pop")
622 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000623 else {
624 PP.Diag(PragmaLocation,
625 diag::warn_pragma_expected_section_push_pop_or_name)
626 << PragmaName;
627 return false;
628 }
Warren Huntc3b18962014-04-08 22:30:47 +0000629 if (Action != Sema::PSK_Reset) {
630 PP.Lex(Tok); // push | pop
631 if (Tok.is(tok::comma)) {
632 PP.Lex(Tok); // ,
633 // If we've got a comma, we either need a label or a string.
634 if (Tok.isAnyIdentifier()) {
635 SlotLabel = Tok.getIdentifierInfo()->getName();
636 PP.Lex(Tok); // identifier
637 if (Tok.is(tok::comma))
638 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000639 else if (Tok.isNot(tok::r_paren)) {
640 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
641 << PragmaName;
642 return false;
643 }
Warren Huntc3b18962014-04-08 22:30:47 +0000644 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000645 } else if (Tok.isNot(tok::r_paren)) {
646 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
647 return false;
648 }
Warren Huntc3b18962014-04-08 22:30:47 +0000649 }
650 }
651 // Grab the string literal for our section name.
652 StringLiteral *SegmentName = nullptr;
653 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000654 if (Tok.isNot(tok::string_literal)) {
655 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000656 diag::warn_pragma_expected_section_name :
657 diag::warn_pragma_expected_section_label_or_name :
658 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000659 PP.Diag(PragmaLocation, DiagID) << PragmaName;
660 return false;
661 }
662 ExprResult StringResult = ParseStringLiteralExpression();
663 if (StringResult.isInvalid())
664 return false; // Already diagnosed.
665 SegmentName = cast<StringLiteral>(StringResult.get());
666 if (SegmentName->getCharByteWidth() != 1) {
667 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
668 << PragmaName;
669 return false;
670 }
Warren Huntc3b18962014-04-08 22:30:47 +0000671 // Setting section "" has no effect
672 if (SegmentName->getLength())
673 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
674 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000675 if (Tok.isNot(tok::r_paren)) {
676 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
677 return false;
678 }
Warren Huntc3b18962014-04-08 22:30:47 +0000679 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000680 if (Tok.isNot(tok::eof)) {
681 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
682 << PragmaName;
683 return false;
684 }
Warren Huntc3b18962014-04-08 22:30:47 +0000685 PP.Lex(Tok); // eof
686 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
687 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000688 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000689}
690
Reid Kleckner1a711b12014-07-22 00:53:05 +0000691// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000692bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
693 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000694 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
695 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
696 return false;
697 }
698
Reid Kleckner1a711b12014-07-22 00:53:05 +0000699 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
700 PragmaName))
701 return false;
702
703 // Parse either the known section names or the string section name.
704 StringLiteral *SegmentName = nullptr;
705 if (Tok.isAnyIdentifier()) {
706 auto *II = Tok.getIdentifierInfo();
707 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
708 .Case("compiler", "\".CRT$XCC\"")
709 .Case("lib", "\".CRT$XCL\"")
710 .Case("user", "\".CRT$XCU\"")
711 .Default("");
712
713 if (!Section.empty()) {
714 // Pretend the user wrote the appropriate string literal here.
715 Token Toks[1];
716 Toks[0].startToken();
717 Toks[0].setKind(tok::string_literal);
718 Toks[0].setLocation(Tok.getLocation());
719 Toks[0].setLiteralData(Section.data());
720 Toks[0].setLength(Section.size());
721 SegmentName =
722 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
723 PP.Lex(Tok);
724 }
725 } else if (Tok.is(tok::string_literal)) {
726 ExprResult StringResult = ParseStringLiteralExpression();
727 if (StringResult.isInvalid())
728 return false;
729 SegmentName = cast<StringLiteral>(StringResult.get());
730 if (SegmentName->getCharByteWidth() != 1) {
731 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
732 << PragmaName;
733 return false;
734 }
735 // FIXME: Add support for the '[, func-name]' part of the pragma.
736 }
737
738 if (!SegmentName) {
739 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
740 return false;
741 }
742
743 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
744 PragmaName) ||
745 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
746 PragmaName))
747 return false;
748
749 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
750 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000751}
752
753struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000754 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000755 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000756 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000757};
758
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000759static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
760 std::string PragmaString;
761 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
762 PragmaString = "clang loop ";
763 PragmaString += Option.getIdentifierInfo()->getName();
764 } else {
765 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
766 "Unexpected pragma name");
767 PragmaString = "unroll";
768 }
769 return PragmaString;
770}
771
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000772bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000773 assert(Tok.is(tok::annot_pragma_loop_hint));
774 PragmaLoopHintInfo *Info =
775 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
776
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000777 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
778 Hint.PragmaNameLoc = IdentifierLoc::create(
779 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000780
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000781 // It is possible that the loop hint has no option identifier, such as
782 // #pragma unroll(4).
783 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
784 ? Info->Option.getIdentifierInfo()
785 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000786 Hint.OptionLoc = IdentifierLoc::create(
787 Actions.Context, Info->Option.getLocation(), OptionInfo);
788
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000789 const Token *Toks = Info->Toks.data();
790 size_t TokSize = Info->Toks.size();
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000791
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000792 // Return a valid hint if pragma unroll or nounroll were specified
793 // without an argument.
794 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
795 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000796 if (TokSize == 0 && (PragmaUnroll || PragmaNoUnroll)) {
797 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000798 Hint.Range = Info->PragmaName.getLocation();
799 return true;
800 }
801
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000802 // The constant expression is always followed by an eof token, which increases
803 // the TokSize by 1.
804 assert(TokSize > 0 &&
805 "PragmaLoopHintInfo::Toks must contain at least one token.");
806
807 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000808 bool OptionUnroll = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000809 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000810 if (OptionInfo) { // Pragma Unroll does not specify an option.
811 OptionUnroll = OptionInfo->isStr("unroll");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000812 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
813 .Case("vectorize", true)
814 .Case("interleave", true)
815 .Case("unroll", true)
816 .Default(false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000817 }
818
819 // Verify loop hint has an argument.
820 if (Toks[0].is(tok::eof)) {
821 ConsumeToken(); // The annotation token.
822 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Tyler Nowicki24853c12015-06-08 23:13:43 +0000823 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000824 return false;
825 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000826
827 // Validate the argument.
828 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000829 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000830 SourceLocation StateLoc = Toks[0].getLocation();
831 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000832 if (!StateInfo ||
Mark Heffernan397a98d2015-08-10 17:29:39 +0000833 (!StateInfo->isStr("enable") && !StateInfo->isStr("disable") &&
834 ((OptionUnroll && !StateInfo->isStr("full")) ||
835 (!OptionUnroll && !StateInfo->isStr("assume_safety"))))) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000836 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
837 << /*FullKeyword=*/OptionUnroll;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000838 return false;
839 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000840 if (TokSize > 2)
841 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
842 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000843 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
844 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000845 // Enter constant expression including eof terminator into token stream.
846 PP.EnterTokenStream(Toks, TokSize, /*DisableMacroExpansion=*/false,
847 /*OwnsTokens=*/false);
848 ConsumeToken(); // The annotation token.
849
850 ExprResult R = ParseConstantExpression();
851
852 // Tokens following an error in an ill-formed constant expression will
853 // remain in the token stream and must be removed.
854 if (Tok.isNot(tok::eof)) {
855 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
856 << PragmaLoopHintString(Info->PragmaName, Info->Option);
857 while (Tok.isNot(tok::eof))
858 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000859 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000860
861 ConsumeToken(); // Consume the constant expression eof terminator.
862
863 if (R.isInvalid() ||
864 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
865 return false;
866
867 // Argument is a constant expression with an integer type.
868 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000869 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000870
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000871 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
872 Info->Toks[TokSize - 1].getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000873 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000874}
875
876// #pragma GCC visibility comes in two variants:
877// 'push' '(' [visibility] ')'
878// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000879void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
880 PragmaIntroducerKind Introducer,
881 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000882 SourceLocation VisLoc = VisTok.getLocation();
883
884 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000885 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000886
887 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
888
Eli Friedman570024a2010-08-05 06:57:20 +0000889 const IdentifierInfo *VisType;
890 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000891 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000892 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000893 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000894 if (Tok.isNot(tok::l_paren)) {
895 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
896 << "visibility";
897 return;
898 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000899 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000900 VisType = Tok.getIdentifierInfo();
901 if (!VisType) {
902 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
903 << "visibility";
904 return;
905 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000906 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000907 if (Tok.isNot(tok::r_paren)) {
908 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
909 << "visibility";
910 return;
911 }
912 } else {
913 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
914 << "visibility";
915 return;
916 }
David Majnemera8f2f1d2015-03-19 00:10:23 +0000917 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000918 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000919 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000920 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
921 << "visibility";
922 return;
923 }
924
Rafael Espindola273fd772012-01-26 02:02:57 +0000925 Token *Toks = new Token[1];
926 Toks[0].startToken();
927 Toks[0].setKind(tok::annot_pragma_vis);
928 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000929 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +0000930 Toks[0].setAnnotationValue(
931 const_cast<void*>(static_cast<const void*>(VisType)));
932 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
933 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000934}
935
Daniel Dunbar921b9682008-10-04 19:21:03 +0000936// #pragma pack(...) comes in the following delicious flavors:
937// pack '(' [integer] ')'
938// pack '(' 'show' ')'
939// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000940void PragmaPackHandler::HandlePragma(Preprocessor &PP,
941 PragmaIntroducerKind Introducer,
942 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000943 SourceLocation PackLoc = PackTok.getLocation();
944
945 Token Tok;
946 PP.Lex(Tok);
947 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000948 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000949 return;
950 }
951
John McCallfaf5fb42010-08-26 23:41:50 +0000952 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000953 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000954 Token Alignment;
955 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000956 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000957 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000958 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000959 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000960
961 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000962
963 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
964 // the push/pop stack.
965 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000966 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000967 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000968 } else if (Tok.is(tok::identifier)) {
969 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000970 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000971 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000972 PP.Lex(Tok);
973 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000974 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000975 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000976 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000977 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000978 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000979 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000980 return;
Mike Stump11289f42009-09-09 15:08:12 +0000981 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000982 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000983
Daniel Dunbar921b9682008-10-04 19:21:03 +0000984 if (Tok.is(tok::comma)) {
985 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000986
Daniel Dunbar921b9682008-10-04 19:21:03 +0000987 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000988 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000989
990 PP.Lex(Tok);
991 } else if (Tok.is(tok::identifier)) {
992 Name = Tok.getIdentifierInfo();
993 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000994
Daniel Dunbar921b9682008-10-04 19:21:03 +0000995 if (Tok.is(tok::comma)) {
996 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000997
Daniel Dunbar921b9682008-10-04 19:21:03 +0000998 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000999 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001000 return;
1001 }
Mike Stump11289f42009-09-09 15:08:12 +00001002
Eli Friedman68be1642012-10-04 02:36:51 +00001003 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001004
1005 PP.Lex(Tok);
1006 }
1007 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001008 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001009 return;
1010 }
1011 }
1012 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001013 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001014 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1015 // the push/pop stack.
1016 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1017 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001018 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001019
1020 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001021 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001022 return;
1023 }
1024
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001025 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001026 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001027 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001028 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1029 return;
1030 }
1031
Daniel Dunbar340cf242012-02-29 01:38:22 +00001032 PragmaPackInfo *Info =
1033 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
1034 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
1035 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +00001036 Info->Kind = Kind;
1037 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +00001038 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001039 Info->LParenLoc = LParenLoc;
1040 Info->RParenLoc = RParenLoc;
1041
Daniel Dunbar340cf242012-02-29 01:38:22 +00001042 Token *Toks =
1043 (Token*) PP.getPreprocessorAllocator().Allocate(
1044 sizeof(Token) * 1, llvm::alignOf<Token>());
1045 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +00001046 Toks[0].startToken();
1047 Toks[0].setKind(tok::annot_pragma_pack);
1048 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001049 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001050 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1051 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +00001052 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001053}
1054
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001055// #pragma ms_struct on
1056// #pragma ms_struct off
1057void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1058 PragmaIntroducerKind Introducer,
1059 Token &MSStructTok) {
1060 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
1061
1062 Token Tok;
1063 PP.Lex(Tok);
1064 if (Tok.isNot(tok::identifier)) {
1065 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1066 return;
1067 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001068 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001069 const IdentifierInfo *II = Tok.getIdentifierInfo();
1070 if (II->isStr("on")) {
1071 Kind = Sema::PMSST_ON;
1072 PP.Lex(Tok);
1073 }
1074 else if (II->isStr("off") || II->isStr("reset"))
1075 PP.Lex(Tok);
1076 else {
1077 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1078 return;
1079 }
1080
1081 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001082 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1083 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001084 return;
1085 }
Eli Friedman68be1642012-10-04 02:36:51 +00001086
1087 Token *Toks =
1088 (Token*) PP.getPreprocessorAllocator().Allocate(
1089 sizeof(Token) * 1, llvm::alignOf<Token>());
1090 new (Toks) Token();
1091 Toks[0].startToken();
1092 Toks[0].setKind(tok::annot_pragma_msstruct);
1093 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001094 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001095 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1096 static_cast<uintptr_t>(Kind)));
1097 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1098 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001099}
1100
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001101// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1102// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001103static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001104 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001105 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001106
1107 if (IsOptions) {
1108 PP.Lex(Tok);
1109 if (Tok.isNot(tok::identifier) ||
1110 !Tok.getIdentifierInfo()->isStr("align")) {
1111 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1112 return;
1113 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001114 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001115
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001116 PP.Lex(Tok);
1117 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001118 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1119 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001120 return;
1121 }
1122
1123 PP.Lex(Tok);
1124 if (Tok.isNot(tok::identifier)) {
1125 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001126 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001127 return;
1128 }
1129
John McCallfaf5fb42010-08-26 23:41:50 +00001130 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001131 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001132 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001133 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001134 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001135 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001136 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001137 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001138 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001139 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001140 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001141 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001142 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001143 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001144 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001145 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1146 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001147 return;
1148 }
1149
David Majnemera8f2f1d2015-03-19 00:10:23 +00001150 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001151 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001152 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001153 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001154 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001155 return;
1156 }
1157
Eli Friedman68be1642012-10-04 02:36:51 +00001158 Token *Toks =
1159 (Token*) PP.getPreprocessorAllocator().Allocate(
1160 sizeof(Token) * 1, llvm::alignOf<Token>());
1161 new (Toks) Token();
1162 Toks[0].startToken();
1163 Toks[0].setKind(tok::annot_pragma_align);
1164 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001165 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001166 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1167 static_cast<uintptr_t>(Kind)));
1168 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1169 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001170}
1171
Douglas Gregorc7d65762010-09-09 22:45:38 +00001172void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1173 PragmaIntroducerKind Introducer,
1174 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001175 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001176}
1177
Douglas Gregorc7d65762010-09-09 22:45:38 +00001178void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1179 PragmaIntroducerKind Introducer,
1180 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001181 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001182}
1183
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001184// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001185void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1186 PragmaIntroducerKind Introducer,
1187 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001188 // FIXME: Should we be expanding macros here? My guess is no.
1189 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001190
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001191 // Lex the left '('.
1192 Token Tok;
1193 PP.Lex(Tok);
1194 if (Tok.isNot(tok::l_paren)) {
1195 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1196 return;
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001199 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001200 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001201 SourceLocation RParenLoc;
1202 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001204 while (true) {
1205 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001207 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001208 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001209 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001210 LexID = false;
1211 continue;
1212 }
1213
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001214 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001215 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1216 return;
1217 }
Mike Stump11289f42009-09-09 15:08:12 +00001218
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001219 // We are execting a ')' or a ','.
1220 if (Tok.is(tok::comma)) {
1221 LexID = true;
1222 continue;
1223 }
Mike Stump11289f42009-09-09 15:08:12 +00001224
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001225 if (Tok.is(tok::r_paren)) {
1226 RParenLoc = Tok.getLocation();
1227 break;
1228 }
Mike Stump11289f42009-09-09 15:08:12 +00001229
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001230 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001231 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001232 return;
1233 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001234
1235 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001236 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001237 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1238 "unused";
1239 return;
1240 }
1241
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001242 // Verify that we have a location for the right parenthesis.
1243 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001244 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001245
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001246 // For each identifier token, insert into the token stream a
1247 // annot_pragma_unused token followed by the identifier token.
1248 // This allows us to cache a "#pragma unused" that occurs inside an inline
1249 // C++ member function.
1250
Daniel Dunbar340cf242012-02-29 01:38:22 +00001251 Token *Toks =
1252 (Token*) PP.getPreprocessorAllocator().Allocate(
1253 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001254 for (unsigned i=0; i != Identifiers.size(); i++) {
1255 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1256 pragmaUnusedTok.startToken();
1257 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1258 pragmaUnusedTok.setLocation(UnusedLoc);
1259 idTok = Identifiers[i];
1260 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001261 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1262 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001263}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001264
1265// #pragma weak identifier
1266// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001267void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1268 PragmaIntroducerKind Introducer,
1269 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001270 SourceLocation WeakLoc = WeakTok.getLocation();
1271
1272 Token Tok;
1273 PP.Lex(Tok);
1274 if (Tok.isNot(tok::identifier)) {
1275 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1276 return;
1277 }
1278
Eli Friedman68be1642012-10-04 02:36:51 +00001279 Token WeakName = Tok;
1280 bool HasAlias = false;
1281 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001282
1283 PP.Lex(Tok);
1284 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001285 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001286 PP.Lex(Tok);
1287 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001288 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001289 << "weak";
1290 return;
1291 }
Eli Friedman68be1642012-10-04 02:36:51 +00001292 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001293 PP.Lex(Tok);
1294 }
1295
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001296 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001297 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1298 return;
1299 }
1300
Eli Friedman68be1642012-10-04 02:36:51 +00001301 if (HasAlias) {
1302 Token *Toks =
1303 (Token*) PP.getPreprocessorAllocator().Allocate(
1304 sizeof(Token) * 3, llvm::alignOf<Token>());
1305 Token &pragmaUnusedTok = Toks[0];
1306 pragmaUnusedTok.startToken();
1307 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1308 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001309 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001310 Toks[1] = WeakName;
1311 Toks[2] = AliasName;
1312 PP.EnterTokenStream(Toks, 3,
1313 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001314 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001315 Token *Toks =
1316 (Token*) PP.getPreprocessorAllocator().Allocate(
1317 sizeof(Token) * 2, llvm::alignOf<Token>());
1318 Token &pragmaUnusedTok = Toks[0];
1319 pragmaUnusedTok.startToken();
1320 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1321 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001322 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001323 Toks[1] = WeakName;
1324 PP.EnterTokenStream(Toks, 2,
1325 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001326 }
1327}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001328
David Chisnall0867d9c2012-02-18 16:12:34 +00001329// #pragma redefine_extname identifier identifier
1330void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1331 PragmaIntroducerKind Introducer,
1332 Token &RedefToken) {
1333 SourceLocation RedefLoc = RedefToken.getLocation();
1334
1335 Token Tok;
1336 PP.Lex(Tok);
1337 if (Tok.isNot(tok::identifier)) {
1338 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1339 "redefine_extname";
1340 return;
1341 }
1342
Eli Friedman68be1642012-10-04 02:36:51 +00001343 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001344 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001345
David Chisnall0867d9c2012-02-18 16:12:34 +00001346 if (Tok.isNot(tok::identifier)) {
1347 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1348 << "redefine_extname";
1349 return;
1350 }
Eli Friedman68be1642012-10-04 02:36:51 +00001351
1352 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001353 PP.Lex(Tok);
1354
1355 if (Tok.isNot(tok::eod)) {
1356 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1357 "redefine_extname";
1358 return;
1359 }
1360
Eli Friedman68be1642012-10-04 02:36:51 +00001361 Token *Toks =
1362 (Token*) PP.getPreprocessorAllocator().Allocate(
1363 sizeof(Token) * 3, llvm::alignOf<Token>());
1364 Token &pragmaRedefTok = Toks[0];
1365 pragmaRedefTok.startToken();
1366 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1367 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001368 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001369 Toks[1] = RedefName;
1370 Toks[2] = AliasName;
1371 PP.EnterTokenStream(Toks, 3,
1372 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001373}
1374
1375
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001376void
1377PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1378 PragmaIntroducerKind Introducer,
1379 Token &Tok) {
1380 tok::OnOffSwitch OOS;
1381 if (PP.LexOnOffSwitch(OOS))
1382 return;
1383
Eli Friedman68be1642012-10-04 02:36:51 +00001384 Token *Toks =
1385 (Token*) PP.getPreprocessorAllocator().Allocate(
1386 sizeof(Token) * 1, llvm::alignOf<Token>());
1387 new (Toks) Token();
1388 Toks[0].startToken();
1389 Toks[0].setKind(tok::annot_pragma_fp_contract);
1390 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001391 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001392 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1393 static_cast<uintptr_t>(OOS)));
1394 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1395 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001396}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001397
1398void
1399PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1400 PragmaIntroducerKind Introducer,
1401 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001402 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001403 if (Tok.isNot(tok::identifier)) {
1404 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1405 "OPENCL";
1406 return;
1407 }
1408 IdentifierInfo *ename = Tok.getIdentifierInfo();
1409 SourceLocation NameLoc = Tok.getLocation();
1410
1411 PP.Lex(Tok);
1412 if (Tok.isNot(tok::colon)) {
1413 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1414 return;
1415 }
1416
1417 PP.Lex(Tok);
1418 if (Tok.isNot(tok::identifier)) {
1419 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1420 return;
1421 }
1422 IdentifierInfo *op = Tok.getIdentifierInfo();
1423
1424 unsigned state;
1425 if (op->isStr("enable")) {
1426 state = 1;
1427 } else if (op->isStr("disable")) {
1428 state = 0;
1429 } else {
1430 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1431 return;
1432 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001433 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001434
Eli Friedman68be1642012-10-04 02:36:51 +00001435 PP.Lex(Tok);
1436 if (Tok.isNot(tok::eod)) {
1437 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1438 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001439 return;
1440 }
Eli Friedman68be1642012-10-04 02:36:51 +00001441
1442 OpenCLExtData data(ename, state);
1443 Token *Toks =
1444 (Token*) PP.getPreprocessorAllocator().Allocate(
1445 sizeof(Token) * 1, llvm::alignOf<Token>());
1446 new (Toks) Token();
1447 Toks[0].startToken();
1448 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1449 Toks[0].setLocation(NameLoc);
1450 Toks[0].setAnnotationValue(data.getOpaqueValue());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001451 Toks[0].setAnnotationEndLoc(StateLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001452 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1453 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001454
1455 if (PP.getPPCallbacks())
1456 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1457 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001458}
1459
Alexey Bataeva769e072013-03-22 06:34:35 +00001460/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1461///
1462void
1463PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1464 PragmaIntroducerKind Introducer,
1465 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001466 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1467 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001468 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001469 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1470 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001471 }
1472 PP.DiscardUntilEndOfDirective();
1473}
1474
1475/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1476///
1477void
1478PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1479 PragmaIntroducerKind Introducer,
1480 Token &FirstTok) {
1481 SmallVector<Token, 16> Pragma;
1482 Token Tok;
1483 Tok.startToken();
1484 Tok.setKind(tok::annot_pragma_openmp);
1485 Tok.setLocation(FirstTok.getLocation());
1486
1487 while (Tok.isNot(tok::eod)) {
1488 Pragma.push_back(Tok);
1489 PP.Lex(Tok);
1490 }
1491 SourceLocation EodLoc = Tok.getLocation();
1492 Tok.startToken();
1493 Tok.setKind(tok::annot_pragma_openmp_end);
1494 Tok.setLocation(EodLoc);
1495 Pragma.push_back(Tok);
1496
1497 Token *Toks = new Token[Pragma.size()];
1498 std::copy(Pragma.begin(), Pragma.end(), Toks);
1499 PP.EnterTokenStream(Toks, Pragma.size(),
Alexey Bataevc8956792015-05-05 09:53:25 +00001500 /*DisableMacroExpansion=*/false, /*OwnsTokens=*/true);
Alexey Bataeva769e072013-03-22 06:34:35 +00001501}
Reid Kleckner002562a2013-05-06 21:02:12 +00001502
David Majnemer4bb09802014-02-10 19:50:15 +00001503/// \brief Handle '#pragma pointers_to_members'
1504// The grammar for this pragma is as follows:
1505//
1506// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1507//
1508// #pragma pointers_to_members '(' 'best_case' ')'
1509// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1510// #pragma pointers_to_members '(' inheritance-model ')'
1511void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1512 PragmaIntroducerKind Introducer,
1513 Token &Tok) {
1514 SourceLocation PointersToMembersLoc = Tok.getLocation();
1515 PP.Lex(Tok);
1516 if (Tok.isNot(tok::l_paren)) {
1517 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1518 << "pointers_to_members";
1519 return;
1520 }
1521 PP.Lex(Tok);
1522 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1523 if (!Arg) {
1524 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1525 << "pointers_to_members";
1526 return;
1527 }
1528 PP.Lex(Tok);
1529
David Majnemer86c318f2014-02-11 21:05:00 +00001530 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001531 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001532 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001533 } else {
1534 if (Arg->isStr("full_generality")) {
1535 if (Tok.is(tok::comma)) {
1536 PP.Lex(Tok);
1537
1538 Arg = Tok.getIdentifierInfo();
1539 if (!Arg) {
1540 PP.Diag(Tok.getLocation(),
1541 diag::err_pragma_pointers_to_members_unknown_kind)
1542 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1543 return;
1544 }
1545 PP.Lex(Tok);
1546 } else if (Tok.is(tok::r_paren)) {
1547 // #pragma pointers_to_members(full_generality) implicitly specifies
1548 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001549 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001550 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001551 } else {
1552 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1553 << "full_generality";
1554 return;
1555 }
1556 }
1557
1558 if (Arg) {
1559 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001560 RepresentationMethod =
1561 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001562 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001563 RepresentationMethod =
1564 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001565 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001566 RepresentationMethod =
1567 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001568 } else {
1569 PP.Diag(Tok.getLocation(),
1570 diag::err_pragma_pointers_to_members_unknown_kind)
1571 << Arg << /*HasPointerDeclaration*/ 1;
1572 return;
1573 }
1574 }
1575 }
1576
1577 if (Tok.isNot(tok::r_paren)) {
1578 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1579 << (Arg ? Arg->getName() : "full_generality");
1580 return;
1581 }
1582
David Majnemera8f2f1d2015-03-19 00:10:23 +00001583 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001584 PP.Lex(Tok);
1585 if (Tok.isNot(tok::eod)) {
1586 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1587 << "pointers_to_members";
1588 return;
1589 }
1590
1591 Token AnnotTok;
1592 AnnotTok.startToken();
1593 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1594 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001595 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001596 AnnotTok.setAnnotationValue(
1597 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1598 PP.EnterToken(AnnotTok);
1599}
1600
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001601/// \brief Handle '#pragma vtordisp'
1602// The grammar for this pragma is as follows:
1603//
1604// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1605//
1606// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1607// #pragma vtordisp '(' 'pop' ')'
1608// #pragma vtordisp '(' ')'
1609void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1610 PragmaIntroducerKind Introducer,
1611 Token &Tok) {
1612 SourceLocation VtorDispLoc = Tok.getLocation();
1613 PP.Lex(Tok);
1614 if (Tok.isNot(tok::l_paren)) {
1615 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1616 return;
1617 }
1618 PP.Lex(Tok);
1619
1620 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1621 const IdentifierInfo *II = Tok.getIdentifierInfo();
1622 if (II) {
1623 if (II->isStr("push")) {
1624 // #pragma vtordisp(push, mode)
1625 PP.Lex(Tok);
1626 if (Tok.isNot(tok::comma)) {
1627 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1628 return;
1629 }
1630 PP.Lex(Tok);
1631 Kind = Sema::PVDK_Push;
1632 // not push, could be on/off
1633 } else if (II->isStr("pop")) {
1634 // #pragma vtordisp(pop)
1635 PP.Lex(Tok);
1636 Kind = Sema::PVDK_Pop;
1637 }
1638 // not push or pop, could be on/off
1639 } else {
1640 if (Tok.is(tok::r_paren)) {
1641 // #pragma vtordisp()
1642 Kind = Sema::PVDK_Reset;
1643 }
1644 }
1645
1646
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001647 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001648 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1649 const IdentifierInfo *II = Tok.getIdentifierInfo();
1650 if (II && II->isStr("off")) {
1651 PP.Lex(Tok);
1652 Value = 0;
1653 } else if (II && II->isStr("on")) {
1654 PP.Lex(Tok);
1655 Value = 1;
1656 } else if (Tok.is(tok::numeric_constant) &&
1657 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1658 if (Value > 2) {
1659 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1660 << 0 << 2 << "vtordisp";
1661 return;
1662 }
1663 } else {
1664 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1665 << "vtordisp";
1666 return;
1667 }
1668 }
1669
1670 // Finish the pragma: ')' $
1671 if (Tok.isNot(tok::r_paren)) {
1672 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1673 return;
1674 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001675 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001676 PP.Lex(Tok);
1677 if (Tok.isNot(tok::eod)) {
1678 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1679 << "vtordisp";
1680 return;
1681 }
1682
1683 // Enter the annotation.
1684 Token AnnotTok;
1685 AnnotTok.startToken();
1686 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1687 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001688 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001689 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1690 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001691 PP.EnterToken(AnnotTok);
1692}
1693
Warren Huntc3b18962014-04-08 22:30:47 +00001694/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1695/// an annotation token.
1696void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1697 PragmaIntroducerKind Introducer,
1698 Token &Tok) {
1699 Token EoF, AnnotTok;
1700 EoF.startToken();
1701 EoF.setKind(tok::eof);
1702 AnnotTok.startToken();
1703 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1704 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001705 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001706 SmallVector<Token, 8> TokenVector;
1707 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001708 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001709 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001710 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1711 }
Warren Huntc3b18962014-04-08 22:30:47 +00001712 // Add a sentinal EoF token to the end of the list.
1713 TokenVector.push_back(EoF);
1714 // We must allocate this array with new because EnterTokenStream is going to
1715 // delete it later.
1716 Token *TokenArray = new Token[TokenVector.size()];
1717 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1718 auto Value = new (PP.getPreprocessorAllocator())
1719 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1720 AnnotTok.setAnnotationValue(Value);
1721 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001722}
1723
Aaron Ballman5d041be2013-06-04 02:07:14 +00001724/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1725///
1726/// The syntax is:
1727/// \code
1728/// #pragma detect_mismatch("name", "value")
1729/// \endcode
1730/// Where 'name' and 'value' are quoted strings. The values are embedded in
1731/// the object file and passed along to the linker. If the linker detects a
1732/// mismatch in the object file's values for the given name, a LNK2038 error
1733/// is emitted. See MSDN for more details.
1734void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1735 PragmaIntroducerKind Introducer,
1736 Token &Tok) {
1737 SourceLocation CommentLoc = Tok.getLocation();
1738 PP.Lex(Tok);
1739 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001740 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001741 return;
1742 }
1743
1744 // Read the name to embed, which must be a string literal.
1745 std::string NameString;
1746 if (!PP.LexStringLiteral(Tok, NameString,
1747 "pragma detect_mismatch",
1748 /*MacroExpansion=*/true))
1749 return;
1750
1751 // Read the comma followed by a second string literal.
1752 std::string ValueString;
1753 if (Tok.isNot(tok::comma)) {
1754 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1755 return;
1756 }
1757
1758 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1759 /*MacroExpansion=*/true))
1760 return;
1761
1762 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001763 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001764 return;
1765 }
1766 PP.Lex(Tok); // Eat the r_paren.
1767
1768 if (Tok.isNot(tok::eod)) {
1769 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1770 return;
1771 }
1772
Reid Kleckner71966c92014-02-20 23:37:45 +00001773 // If the pragma is lexically sound, notify any interested PPCallbacks.
1774 if (PP.getPPCallbacks())
1775 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1776 ValueString);
1777
Aaron Ballman5d041be2013-06-04 02:07:14 +00001778 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1779}
1780
Reid Kleckner002562a2013-05-06 21:02:12 +00001781/// \brief Handle the microsoft \#pragma comment extension.
1782///
1783/// The syntax is:
1784/// \code
1785/// #pragma comment(linker, "foo")
1786/// \endcode
1787/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1788/// "foo" is a string, which is fully macro expanded, and permits string
1789/// concatenation, embedded escape characters etc. See MSDN for more details.
1790void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1791 PragmaIntroducerKind Introducer,
1792 Token &Tok) {
1793 SourceLocation CommentLoc = Tok.getLocation();
1794 PP.Lex(Tok);
1795 if (Tok.isNot(tok::l_paren)) {
1796 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1797 return;
1798 }
1799
1800 // Read the identifier.
1801 PP.Lex(Tok);
1802 if (Tok.isNot(tok::identifier)) {
1803 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1804 return;
1805 }
1806
1807 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001808 IdentifierInfo *II = Tok.getIdentifierInfo();
1809 Sema::PragmaMSCommentKind Kind =
1810 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1811 .Case("linker", Sema::PCK_Linker)
1812 .Case("lib", Sema::PCK_Lib)
1813 .Case("compiler", Sema::PCK_Compiler)
1814 .Case("exestr", Sema::PCK_ExeStr)
1815 .Case("user", Sema::PCK_User)
1816 .Default(Sema::PCK_Unknown);
1817 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001818 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1819 return;
1820 }
1821
Yunzhong Gao99efc032015-03-23 20:41:42 +00001822 // On PS4, issue a warning about any pragma comments other than
1823 // #pragma comment lib.
1824 if (PP.getTargetInfo().getTriple().isPS4() && Kind != Sema::PCK_Lib) {
1825 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1826 << II->getName();
1827 return;
1828 }
1829
Reid Kleckner002562a2013-05-06 21:02:12 +00001830 // Read the optional string if present.
1831 PP.Lex(Tok);
1832 std::string ArgumentString;
1833 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1834 "pragma comment",
1835 /*MacroExpansion=*/true))
1836 return;
1837
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001838 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001839 // FIXME: If the kind is "compiler" warn if the string is present (it is
1840 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001841 // The MSDN docs say that "lib" and "linker" require a string and have a short
1842 // whitelist of linker options they support, but in practice MSVC doesn't
1843 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001844
1845 if (Tok.isNot(tok::r_paren)) {
1846 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1847 return;
1848 }
1849 PP.Lex(Tok); // eat the r_paren.
1850
1851 if (Tok.isNot(tok::eod)) {
1852 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1853 return;
1854 }
1855
Reid Kleckner71966c92014-02-20 23:37:45 +00001856 // If the pragma is lexically sound, notify any interested PPCallbacks.
1857 if (PP.getPPCallbacks())
1858 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1859
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001860 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001861}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001862
1863// #pragma clang optimize off
1864// #pragma clang optimize on
1865void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1866 PragmaIntroducerKind Introducer,
1867 Token &FirstToken) {
1868 Token Tok;
1869 PP.Lex(Tok);
1870 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001871 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001872 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001873 return;
1874 }
1875 if (Tok.isNot(tok::identifier)) {
1876 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1877 << PP.getSpelling(Tok);
1878 return;
1879 }
1880 const IdentifierInfo *II = Tok.getIdentifierInfo();
1881 // The only accepted values are 'on' or 'off'.
1882 bool IsOn = false;
1883 if (II->isStr("on")) {
1884 IsOn = true;
1885 } else if (!II->isStr("off")) {
1886 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1887 << PP.getSpelling(Tok);
1888 return;
1889 }
1890 PP.Lex(Tok);
1891
1892 if (Tok.isNot(tok::eod)) {
1893 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1894 << PP.getSpelling(Tok);
1895 return;
1896 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001897
1898 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1899}
1900
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001901/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001902static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1903 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001904 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001905 SmallVector<Token, 1> ValueList;
1906 int OpenParens = ValueInParens ? 1 : 0;
1907 // Read constant expression.
1908 while (Tok.isNot(tok::eod)) {
1909 if (Tok.is(tok::l_paren))
1910 OpenParens++;
1911 else if (Tok.is(tok::r_paren)) {
1912 OpenParens--;
1913 if (OpenParens == 0 && ValueInParens)
1914 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001915 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001916
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001917 ValueList.push_back(Tok);
1918 PP.Lex(Tok);
1919 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001920
1921 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001922 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001923 if (Tok.isNot(tok::r_paren)) {
1924 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1925 return true;
1926 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001927 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001928 }
1929
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001930 Token EOFTok;
1931 EOFTok.startToken();
1932 EOFTok.setKind(tok::eof);
1933 EOFTok.setLocation(Tok.getLocation());
1934 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1935
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001936 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001937
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001938 Info.PragmaName = PragmaName;
1939 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001940 return false;
1941}
1942
Eli Bendersky06a40422014-06-06 20:31:48 +00001943/// \brief Handle the \#pragma clang loop directive.
1944/// #pragma clang 'loop' loop-hints
1945///
1946/// loop-hints:
1947/// loop-hint loop-hints[opt]
1948///
1949/// loop-hint:
1950/// 'vectorize' '(' loop-hint-keyword ')'
1951/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001952/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001953/// 'vectorize_width' '(' loop-hint-value ')'
1954/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001955/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001956///
1957/// loop-hint-keyword:
1958/// 'enable'
1959/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00001960/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00001961///
Mark Heffernan450c2382014-07-23 17:31:31 +00001962/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00001963/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00001964/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00001965/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00001966///
Eli Bendersky06a40422014-06-06 20:31:48 +00001967/// loop-hint-value:
1968/// constant-expression
1969///
1970/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1971/// try vectorizing the instructions of the loop it precedes. Specifying
1972/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1973/// interleaving multiple iterations of the loop it precedes. The width of the
1974/// vector instructions is specified by vectorize_width() and the number of
1975/// interleaved loop iterations is specified by interleave_count(). Specifying a
1976/// value of 1 effectively disables vectorization/interleaving, even if it is
1977/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1978/// only works on inner loops.
1979///
Eli Bendersky86483b32014-06-11 17:56:26 +00001980/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00001981/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
1982/// completely if the trip count is known at compile time and unroll partially
1983/// if the trip count is not known. Specifying unroll(full) is similar to
1984/// unroll(enable) but will unroll the loop only if the trip count is known at
1985/// compile time. Specifying unroll(disable) disables unrolling for the
1986/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
1987/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001988void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1989 PragmaIntroducerKind Introducer,
1990 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001991 // Incoming token is "loop" from "#pragma clang loop".
1992 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001993 SmallVector<Token, 1> TokenList;
1994
1995 // Lex the optimization option and verify it is an identifier.
1996 PP.Lex(Tok);
1997 if (Tok.isNot(tok::identifier)) {
1998 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1999 << /*MissingOption=*/true << "";
2000 return;
2001 }
2002
2003 while (Tok.is(tok::identifier)) {
2004 Token Option = Tok;
2005 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2006
Eli Bendersky86483b32014-06-11 17:56:26 +00002007 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002008 .Case("vectorize", true)
2009 .Case("interleave", true)
2010 .Case("unroll", true)
2011 .Case("vectorize_width", true)
2012 .Case("interleave_count", true)
2013 .Case("unroll_count", true)
2014 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002015 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002016 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2017 << /*MissingOption=*/false << OptionInfo;
2018 return;
2019 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002020 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002021
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002022 // Read '('
2023 if (Tok.isNot(tok::l_paren)) {
2024 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002025 return;
2026 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002027 PP.Lex(Tok);
2028
2029 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2030 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2031 *Info))
2032 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002033
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002034 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002035 Token LoopHintTok;
2036 LoopHintTok.startToken();
2037 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002038 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002039 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002040 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2041 TokenList.push_back(LoopHintTok);
2042 }
2043
2044 if (Tok.isNot(tok::eod)) {
2045 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2046 << "clang loop";
2047 return;
2048 }
2049
2050 Token *TokenArray = new Token[TokenList.size()];
2051 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
2052
2053 PP.EnterTokenStream(TokenArray, TokenList.size(),
2054 /*DisableMacroExpansion=*/false,
2055 /*OwnsTokens=*/true);
2056}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002057
2058/// \brief Handle the loop unroll optimization pragmas.
2059/// #pragma unroll
2060/// #pragma unroll unroll-hint-value
2061/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002062/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002063///
2064/// unroll-hint-value:
2065/// constant-expression
2066///
Mark Heffernanc888e412014-07-24 18:09:38 +00002067/// Loop unrolling hints can be specified with '#pragma unroll' or
2068/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2069/// contained in parentheses. With no argument the directive instructs llvm to
2070/// try to unroll the loop completely. A positive integer argument can be
2071/// specified to indicate the number of times the loop should be unrolled. To
2072/// maximize compatibility with other compilers the unroll count argument can be
2073/// specified with or without parentheses. Specifying, '#pragma nounroll'
2074/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002075void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2076 PragmaIntroducerKind Introducer,
2077 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002078 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2079 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002080 Token PragmaName = Tok;
2081 PP.Lex(Tok);
2082 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2083 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002084 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002085 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002086 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002087 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2088 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2089 << "nounroll";
2090 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002091 } else {
2092 // Unroll pragma with an argument: "#pragma unroll N" or
2093 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002094 // Read '(' if it exists.
2095 bool ValueInParens = Tok.is(tok::l_paren);
2096 if (ValueInParens)
2097 PP.Lex(Tok);
2098
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002099 Token Option;
2100 Option.startToken();
2101 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002102 return;
2103
2104 // In CUDA, the argument to '#pragma unroll' should not be contained in
2105 // parentheses.
2106 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002107 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002108 diag::warn_pragma_unroll_cuda_value_in_parens);
2109
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002110 if (Tok.isNot(tok::eod)) {
2111 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2112 << "unroll";
2113 return;
2114 }
2115 }
2116
2117 // Generate the hint token.
2118 Token *TokenArray = new Token[1];
2119 TokenArray[0].startToken();
2120 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2121 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002122 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002123 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2124 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2125 /*OwnsTokens=*/true);
2126}