blob: 4430eb8d03da4b760b7242e5d64fc41410303550 [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
Benjamin Kramere003ca22015-10-28 13:54:16 +0000337namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000338struct PragmaPackInfo {
339 Sema::PragmaPackKind Kind;
340 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000341 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000342 SourceLocation LParenLoc;
343 SourceLocation RParenLoc;
344};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000345} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000346
347void Parser::HandlePragmaPack() {
348 assert(Tok.is(tok::annot_pragma_pack));
349 PragmaPackInfo *Info =
350 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
351 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000352 ExprResult Alignment;
353 if (Info->Alignment.is(tok::numeric_constant)) {
354 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
355 if (Alignment.isInvalid())
356 return;
357 }
358 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000359 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000360}
361
Eli Friedman68be1642012-10-04 02:36:51 +0000362void Parser::HandlePragmaMSStruct() {
363 assert(Tok.is(tok::annot_pragma_msstruct));
364 Sema::PragmaMSStructKind Kind =
365 static_cast<Sema::PragmaMSStructKind>(
366 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
367 Actions.ActOnPragmaMSStruct(Kind);
368 ConsumeToken(); // The annotation token.
369}
370
371void Parser::HandlePragmaAlign() {
372 assert(Tok.is(tok::annot_pragma_align));
373 Sema::PragmaOptionsAlignKind Kind =
374 static_cast<Sema::PragmaOptionsAlignKind>(
375 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
376 SourceLocation PragmaLoc = ConsumeToken();
377 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
378}
379
380void Parser::HandlePragmaWeak() {
381 assert(Tok.is(tok::annot_pragma_weak));
382 SourceLocation PragmaLoc = ConsumeToken();
383 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
384 Tok.getLocation());
385 ConsumeToken(); // The weak name.
386}
387
388void Parser::HandlePragmaWeakAlias() {
389 assert(Tok.is(tok::annot_pragma_weakalias));
390 SourceLocation PragmaLoc = ConsumeToken();
391 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
392 SourceLocation WeakNameLoc = Tok.getLocation();
393 ConsumeToken();
394 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
395 SourceLocation AliasNameLoc = Tok.getLocation();
396 ConsumeToken();
397 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
398 WeakNameLoc, AliasNameLoc);
399
400}
401
402void Parser::HandlePragmaRedefineExtname() {
403 assert(Tok.is(tok::annot_pragma_redefine_extname));
404 SourceLocation RedefLoc = ConsumeToken();
405 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
406 SourceLocation RedefNameLoc = Tok.getLocation();
407 ConsumeToken();
408 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
409 SourceLocation AliasNameLoc = Tok.getLocation();
410 ConsumeToken();
411 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
412 RedefNameLoc, AliasNameLoc);
413}
414
415void Parser::HandlePragmaFPContract() {
416 assert(Tok.is(tok::annot_pragma_fp_contract));
417 tok::OnOffSwitch OOS =
418 static_cast<tok::OnOffSwitch>(
419 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
420 Actions.ActOnPragmaFPContract(OOS);
421 ConsumeToken(); // The annotation token.
422}
423
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000424StmtResult Parser::HandlePragmaCaptured()
425{
426 assert(Tok.is(tok::annot_pragma_captured));
427 ConsumeToken();
428
429 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000430 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000431 return StmtError();
432 }
433
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000434 SourceLocation Loc = Tok.getLocation();
435
436 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000437 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
438 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000439
440 StmtResult R = ParseCompoundStatement();
441 CapturedRegionScope.Exit();
442
443 if (R.isInvalid()) {
444 Actions.ActOnCapturedRegionError();
445 return StmtError();
446 }
447
448 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000449}
450
Eli Friedman68be1642012-10-04 02:36:51 +0000451namespace {
452 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
453}
454
455void Parser::HandlePragmaOpenCLExtension() {
456 assert(Tok.is(tok::annot_pragma_opencl_extension));
457 OpenCLExtData data =
458 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
459 unsigned state = data.getInt();
460 IdentifierInfo *ename = data.getPointer();
461 SourceLocation NameLoc = Tok.getLocation();
462 ConsumeToken(); // The annotation token.
463
464 OpenCLOptions &f = Actions.getOpenCLOptions();
465 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
466 // overriding all previously issued extension directives, but only if the
467 // behavior is set to disable."
468 if (state == 0 && ename->isStr("all")) {
469#define OPENCLEXT(nm) f.nm = 0;
470#include "clang/Basic/OpenCLExtensions.def"
471 }
472#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
473#include "clang/Basic/OpenCLExtensions.def"
474 else {
475 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
476 return;
477 }
478}
479
David Majnemer4bb09802014-02-10 19:50:15 +0000480void Parser::HandlePragmaMSPointersToMembers() {
481 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000482 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
483 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000484 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
485 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
486 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
487}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000488
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000489void Parser::HandlePragmaMSVtorDisp() {
490 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
491 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
492 Sema::PragmaVtorDispKind Kind =
493 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
494 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
495 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
496 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
497}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000498
Warren Huntc3b18962014-04-08 22:30:47 +0000499void Parser::HandlePragmaMSPragma() {
500 assert(Tok.is(tok::annot_pragma_ms_pragma));
501 // Grab the tokens out of the annotation and enter them into the stream.
502 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
503 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
504 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
505 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000506 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000507 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000508
Warren Huntc3b18962014-04-08 22:30:47 +0000509 // Figure out which #pragma we're dealing with. The switch has no default
510 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000511 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000512 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
513 .Case("data_seg", &Parser::HandlePragmaMSSegment)
514 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
515 .Case("const_seg", &Parser::HandlePragmaMSSegment)
516 .Case("code_seg", &Parser::HandlePragmaMSSegment)
517 .Case("section", &Parser::HandlePragmaMSSection)
518 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000519
520 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
521 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
522 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000523 while (Tok.isNot(tok::eof))
524 PP.Lex(Tok);
525 PP.Lex(Tok);
526 }
527}
528
Reid Kleckner722b1df2014-07-18 00:13:16 +0000529bool Parser::HandlePragmaMSSection(StringRef PragmaName,
530 SourceLocation PragmaLocation) {
531 if (Tok.isNot(tok::l_paren)) {
532 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
533 return false;
534 }
Warren Huntc3b18962014-04-08 22:30:47 +0000535 PP.Lex(Tok); // (
536 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000537 if (Tok.isNot(tok::string_literal)) {
538 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
539 << PragmaName;
540 return false;
541 }
542 ExprResult StringResult = ParseStringLiteralExpression();
543 if (StringResult.isInvalid())
544 return false; // Already diagnosed.
545 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
546 if (SegmentName->getCharByteWidth() != 1) {
547 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
548 << PragmaName;
549 return false;
550 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000551 int SectionFlags = ASTContext::PSF_Read;
552 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000553 while (Tok.is(tok::comma)) {
554 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000555 // Ignore "long" and "short".
556 // They are undocumented, but widely used, section attributes which appear
557 // to do nothing.
558 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
559 PP.Lex(Tok); // long/short
560 continue;
561 }
562
Reid Kleckner722b1df2014-07-18 00:13:16 +0000563 if (!Tok.isAnyIdentifier()) {
564 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
565 << PragmaName;
566 return false;
567 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000568 ASTContext::PragmaSectionFlag Flag =
569 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000570 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000571 .Case("read", ASTContext::PSF_Read)
572 .Case("write", ASTContext::PSF_Write)
573 .Case("execute", ASTContext::PSF_Execute)
574 .Case("shared", ASTContext::PSF_Invalid)
575 .Case("nopage", ASTContext::PSF_Invalid)
576 .Case("nocache", ASTContext::PSF_Invalid)
577 .Case("discard", ASTContext::PSF_Invalid)
578 .Case("remove", ASTContext::PSF_Invalid)
579 .Default(ASTContext::PSF_None);
580 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
581 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000582 ? diag::warn_pragma_invalid_specific_action
583 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000584 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000585 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000586 }
587 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000588 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000589 PP.Lex(Tok); // Identifier
590 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000591 // If no section attributes are specified, the section will be marked as
592 // read/write.
593 if (SectionFlagsAreDefault)
594 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000595 if (Tok.isNot(tok::r_paren)) {
596 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
597 return false;
598 }
Warren Huntc3b18962014-04-08 22:30:47 +0000599 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000600 if (Tok.isNot(tok::eof)) {
601 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
602 << PragmaName;
603 return false;
604 }
Warren Huntc3b18962014-04-08 22:30:47 +0000605 PP.Lex(Tok); // eof
606 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000607 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000608}
609
Reid Kleckner722b1df2014-07-18 00:13:16 +0000610bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
611 SourceLocation PragmaLocation) {
612 if (Tok.isNot(tok::l_paren)) {
613 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
614 return false;
615 }
Warren Huntc3b18962014-04-08 22:30:47 +0000616 PP.Lex(Tok); // (
617 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000618 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000619 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000620 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000621 if (PushPop == "push")
622 Action = Sema::PSK_Push;
623 else if (PushPop == "pop")
624 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000625 else {
626 PP.Diag(PragmaLocation,
627 diag::warn_pragma_expected_section_push_pop_or_name)
628 << PragmaName;
629 return false;
630 }
Warren Huntc3b18962014-04-08 22:30:47 +0000631 if (Action != Sema::PSK_Reset) {
632 PP.Lex(Tok); // push | pop
633 if (Tok.is(tok::comma)) {
634 PP.Lex(Tok); // ,
635 // If we've got a comma, we either need a label or a string.
636 if (Tok.isAnyIdentifier()) {
637 SlotLabel = Tok.getIdentifierInfo()->getName();
638 PP.Lex(Tok); // identifier
639 if (Tok.is(tok::comma))
640 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000641 else if (Tok.isNot(tok::r_paren)) {
642 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
643 << PragmaName;
644 return false;
645 }
Warren Huntc3b18962014-04-08 22:30:47 +0000646 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000647 } else if (Tok.isNot(tok::r_paren)) {
648 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
649 return false;
650 }
Warren Huntc3b18962014-04-08 22:30:47 +0000651 }
652 }
653 // Grab the string literal for our section name.
654 StringLiteral *SegmentName = nullptr;
655 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000656 if (Tok.isNot(tok::string_literal)) {
657 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000658 diag::warn_pragma_expected_section_name :
659 diag::warn_pragma_expected_section_label_or_name :
660 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000661 PP.Diag(PragmaLocation, DiagID) << PragmaName;
662 return false;
663 }
664 ExprResult StringResult = ParseStringLiteralExpression();
665 if (StringResult.isInvalid())
666 return false; // Already diagnosed.
667 SegmentName = cast<StringLiteral>(StringResult.get());
668 if (SegmentName->getCharByteWidth() != 1) {
669 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
670 << PragmaName;
671 return false;
672 }
Warren Huntc3b18962014-04-08 22:30:47 +0000673 // Setting section "" has no effect
674 if (SegmentName->getLength())
675 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
676 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000677 if (Tok.isNot(tok::r_paren)) {
678 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
679 return false;
680 }
Warren Huntc3b18962014-04-08 22:30:47 +0000681 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000682 if (Tok.isNot(tok::eof)) {
683 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
684 << PragmaName;
685 return false;
686 }
Warren Huntc3b18962014-04-08 22:30:47 +0000687 PP.Lex(Tok); // eof
688 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
689 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000690 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000691}
692
Reid Kleckner1a711b12014-07-22 00:53:05 +0000693// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000694bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
695 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000696 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
697 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
698 return false;
699 }
700
Reid Kleckner1a711b12014-07-22 00:53:05 +0000701 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
702 PragmaName))
703 return false;
704
705 // Parse either the known section names or the string section name.
706 StringLiteral *SegmentName = nullptr;
707 if (Tok.isAnyIdentifier()) {
708 auto *II = Tok.getIdentifierInfo();
709 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
710 .Case("compiler", "\".CRT$XCC\"")
711 .Case("lib", "\".CRT$XCL\"")
712 .Case("user", "\".CRT$XCU\"")
713 .Default("");
714
715 if (!Section.empty()) {
716 // Pretend the user wrote the appropriate string literal here.
717 Token Toks[1];
718 Toks[0].startToken();
719 Toks[0].setKind(tok::string_literal);
720 Toks[0].setLocation(Tok.getLocation());
721 Toks[0].setLiteralData(Section.data());
722 Toks[0].setLength(Section.size());
723 SegmentName =
724 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
725 PP.Lex(Tok);
726 }
727 } else if (Tok.is(tok::string_literal)) {
728 ExprResult StringResult = ParseStringLiteralExpression();
729 if (StringResult.isInvalid())
730 return false;
731 SegmentName = cast<StringLiteral>(StringResult.get());
732 if (SegmentName->getCharByteWidth() != 1) {
733 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
734 << PragmaName;
735 return false;
736 }
737 // FIXME: Add support for the '[, func-name]' part of the pragma.
738 }
739
740 if (!SegmentName) {
741 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
742 return false;
743 }
744
745 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
746 PragmaName) ||
747 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
748 PragmaName))
749 return false;
750
751 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
752 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000753}
754
Benjamin Kramere003ca22015-10-28 13:54:16 +0000755namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000756struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000757 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000758 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000759 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000760};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000761} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +0000762
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000763static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
764 std::string PragmaString;
765 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
766 PragmaString = "clang loop ";
767 PragmaString += Option.getIdentifierInfo()->getName();
768 } else {
769 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
770 "Unexpected pragma name");
771 PragmaString = "unroll";
772 }
773 return PragmaString;
774}
775
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000776bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000777 assert(Tok.is(tok::annot_pragma_loop_hint));
778 PragmaLoopHintInfo *Info =
779 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
780
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000781 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
782 Hint.PragmaNameLoc = IdentifierLoc::create(
783 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000784
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000785 // It is possible that the loop hint has no option identifier, such as
786 // #pragma unroll(4).
787 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
788 ? Info->Option.getIdentifierInfo()
789 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000790 Hint.OptionLoc = IdentifierLoc::create(
791 Actions.Context, Info->Option.getLocation(), OptionInfo);
792
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000793 const Token *Toks = Info->Toks.data();
794 size_t TokSize = Info->Toks.size();
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000795
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000796 // Return a valid hint if pragma unroll or nounroll were specified
797 // without an argument.
798 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
799 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000800 if (TokSize == 0 && (PragmaUnroll || PragmaNoUnroll)) {
801 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000802 Hint.Range = Info->PragmaName.getLocation();
803 return true;
804 }
805
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000806 // The constant expression is always followed by an eof token, which increases
807 // the TokSize by 1.
808 assert(TokSize > 0 &&
809 "PragmaLoopHintInfo::Toks must contain at least one token.");
810
811 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000812 bool OptionUnroll = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000813 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000814 if (OptionInfo) { // Pragma Unroll does not specify an option.
815 OptionUnroll = OptionInfo->isStr("unroll");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000816 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
817 .Case("vectorize", true)
818 .Case("interleave", true)
819 .Case("unroll", true)
820 .Default(false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000821 }
822
823 // Verify loop hint has an argument.
824 if (Toks[0].is(tok::eof)) {
825 ConsumeToken(); // The annotation token.
826 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Tyler Nowicki24853c12015-06-08 23:13:43 +0000827 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000828 return false;
829 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000830
831 // Validate the argument.
832 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000833 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000834 SourceLocation StateLoc = Toks[0].getLocation();
835 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000836 if (!StateInfo ||
Mark Heffernan397a98d2015-08-10 17:29:39 +0000837 (!StateInfo->isStr("enable") && !StateInfo->isStr("disable") &&
838 ((OptionUnroll && !StateInfo->isStr("full")) ||
839 (!OptionUnroll && !StateInfo->isStr("assume_safety"))))) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000840 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
841 << /*FullKeyword=*/OptionUnroll;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000842 return false;
843 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000844 if (TokSize > 2)
845 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
846 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000847 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
848 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000849 // Enter constant expression including eof terminator into token stream.
850 PP.EnterTokenStream(Toks, TokSize, /*DisableMacroExpansion=*/false,
851 /*OwnsTokens=*/false);
852 ConsumeToken(); // The annotation token.
853
854 ExprResult R = ParseConstantExpression();
855
856 // Tokens following an error in an ill-formed constant expression will
857 // remain in the token stream and must be removed.
858 if (Tok.isNot(tok::eof)) {
859 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
860 << PragmaLoopHintString(Info->PragmaName, Info->Option);
861 while (Tok.isNot(tok::eof))
862 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000863 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000864
865 ConsumeToken(); // Consume the constant expression eof terminator.
866
867 if (R.isInvalid() ||
868 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
869 return false;
870
871 // Argument is a constant expression with an integer type.
872 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000873 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000874
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000875 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
876 Info->Toks[TokSize - 1].getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000877 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000878}
879
880// #pragma GCC visibility comes in two variants:
881// 'push' '(' [visibility] ')'
882// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000883void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
884 PragmaIntroducerKind Introducer,
885 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000886 SourceLocation VisLoc = VisTok.getLocation();
887
888 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000889 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000890
891 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
892
Eli Friedman570024a2010-08-05 06:57:20 +0000893 const IdentifierInfo *VisType;
894 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000895 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000896 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000897 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000898 if (Tok.isNot(tok::l_paren)) {
899 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
900 << "visibility";
901 return;
902 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000903 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000904 VisType = Tok.getIdentifierInfo();
905 if (!VisType) {
906 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
907 << "visibility";
908 return;
909 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000910 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000911 if (Tok.isNot(tok::r_paren)) {
912 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
913 << "visibility";
914 return;
915 }
916 } else {
917 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
918 << "visibility";
919 return;
920 }
David Majnemera8f2f1d2015-03-19 00:10:23 +0000921 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000922 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000923 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000924 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
925 << "visibility";
926 return;
927 }
928
Rafael Espindola273fd772012-01-26 02:02:57 +0000929 Token *Toks = new Token[1];
930 Toks[0].startToken();
931 Toks[0].setKind(tok::annot_pragma_vis);
932 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000933 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +0000934 Toks[0].setAnnotationValue(
935 const_cast<void*>(static_cast<const void*>(VisType)));
936 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
937 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000938}
939
Daniel Dunbar921b9682008-10-04 19:21:03 +0000940// #pragma pack(...) comes in the following delicious flavors:
941// pack '(' [integer] ')'
942// pack '(' 'show' ')'
943// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000944void PragmaPackHandler::HandlePragma(Preprocessor &PP,
945 PragmaIntroducerKind Introducer,
946 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000947 SourceLocation PackLoc = PackTok.getLocation();
948
949 Token Tok;
950 PP.Lex(Tok);
951 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000952 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000953 return;
954 }
955
John McCallfaf5fb42010-08-26 23:41:50 +0000956 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000957 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000958 Token Alignment;
959 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000960 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000961 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000962 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000963 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000964
965 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000966
967 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
968 // the push/pop stack.
969 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000970 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000971 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000972 } else if (Tok.is(tok::identifier)) {
973 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000974 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000975 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000976 PP.Lex(Tok);
977 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000978 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000979 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000980 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000981 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000982 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000983 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000984 return;
Mike Stump11289f42009-09-09 15:08:12 +0000985 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000986 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000987
Daniel Dunbar921b9682008-10-04 19:21:03 +0000988 if (Tok.is(tok::comma)) {
989 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000990
Daniel Dunbar921b9682008-10-04 19:21:03 +0000991 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000992 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000993
994 PP.Lex(Tok);
995 } else if (Tok.is(tok::identifier)) {
996 Name = Tok.getIdentifierInfo();
997 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000998
Daniel Dunbar921b9682008-10-04 19:21:03 +0000999 if (Tok.is(tok::comma)) {
1000 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001001
Daniel Dunbar921b9682008-10-04 19:21:03 +00001002 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001003 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001004 return;
1005 }
Mike Stump11289f42009-09-09 15:08:12 +00001006
Eli Friedman68be1642012-10-04 02:36:51 +00001007 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001008
1009 PP.Lex(Tok);
1010 }
1011 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001012 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001013 return;
1014 }
1015 }
1016 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001017 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001018 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1019 // the push/pop stack.
1020 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1021 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001022 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001023
1024 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001025 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001026 return;
1027 }
1028
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001029 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001030 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001031 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001032 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1033 return;
1034 }
1035
Daniel Dunbar340cf242012-02-29 01:38:22 +00001036 PragmaPackInfo *Info =
1037 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
1038 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
1039 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +00001040 Info->Kind = Kind;
1041 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +00001042 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001043 Info->LParenLoc = LParenLoc;
1044 Info->RParenLoc = RParenLoc;
1045
Daniel Dunbar340cf242012-02-29 01:38:22 +00001046 Token *Toks =
1047 (Token*) PP.getPreprocessorAllocator().Allocate(
1048 sizeof(Token) * 1, llvm::alignOf<Token>());
1049 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +00001050 Toks[0].startToken();
1051 Toks[0].setKind(tok::annot_pragma_pack);
1052 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001053 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001054 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1055 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +00001056 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001057}
1058
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001059// #pragma ms_struct on
1060// #pragma ms_struct off
1061void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1062 PragmaIntroducerKind Introducer,
1063 Token &MSStructTok) {
1064 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
1065
1066 Token Tok;
1067 PP.Lex(Tok);
1068 if (Tok.isNot(tok::identifier)) {
1069 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1070 return;
1071 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001072 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001073 const IdentifierInfo *II = Tok.getIdentifierInfo();
1074 if (II->isStr("on")) {
1075 Kind = Sema::PMSST_ON;
1076 PP.Lex(Tok);
1077 }
1078 else if (II->isStr("off") || II->isStr("reset"))
1079 PP.Lex(Tok);
1080 else {
1081 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1082 return;
1083 }
1084
1085 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001086 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1087 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001088 return;
1089 }
Eli Friedman68be1642012-10-04 02:36:51 +00001090
1091 Token *Toks =
1092 (Token*) PP.getPreprocessorAllocator().Allocate(
1093 sizeof(Token) * 1, llvm::alignOf<Token>());
1094 new (Toks) Token();
1095 Toks[0].startToken();
1096 Toks[0].setKind(tok::annot_pragma_msstruct);
1097 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001098 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001099 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1100 static_cast<uintptr_t>(Kind)));
1101 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1102 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001103}
1104
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001105// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1106// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001107static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001108 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001109 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001110
1111 if (IsOptions) {
1112 PP.Lex(Tok);
1113 if (Tok.isNot(tok::identifier) ||
1114 !Tok.getIdentifierInfo()->isStr("align")) {
1115 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1116 return;
1117 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001118 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001119
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001120 PP.Lex(Tok);
1121 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001122 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1123 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001124 return;
1125 }
1126
1127 PP.Lex(Tok);
1128 if (Tok.isNot(tok::identifier)) {
1129 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001130 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001131 return;
1132 }
1133
John McCallfaf5fb42010-08-26 23:41:50 +00001134 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001135 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001136 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001137 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001138 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001139 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001140 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001141 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001142 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001143 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001144 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001145 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001146 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001147 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001148 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001149 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1150 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001151 return;
1152 }
1153
David Majnemera8f2f1d2015-03-19 00:10:23 +00001154 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001155 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001156 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001157 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001158 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001159 return;
1160 }
1161
Eli Friedman68be1642012-10-04 02:36:51 +00001162 Token *Toks =
1163 (Token*) PP.getPreprocessorAllocator().Allocate(
1164 sizeof(Token) * 1, llvm::alignOf<Token>());
1165 new (Toks) Token();
1166 Toks[0].startToken();
1167 Toks[0].setKind(tok::annot_pragma_align);
1168 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001169 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001170 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1171 static_cast<uintptr_t>(Kind)));
1172 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1173 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001174}
1175
Douglas Gregorc7d65762010-09-09 22:45:38 +00001176void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1177 PragmaIntroducerKind Introducer,
1178 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001179 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001180}
1181
Douglas Gregorc7d65762010-09-09 22:45:38 +00001182void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1183 PragmaIntroducerKind Introducer,
1184 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001185 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001186}
1187
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001188// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001189void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1190 PragmaIntroducerKind Introducer,
1191 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001192 // FIXME: Should we be expanding macros here? My guess is no.
1193 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001194
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001195 // Lex the left '('.
1196 Token Tok;
1197 PP.Lex(Tok);
1198 if (Tok.isNot(tok::l_paren)) {
1199 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1200 return;
1201 }
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001203 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001204 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001205 SourceLocation RParenLoc;
1206 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001207
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001208 while (true) {
1209 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001211 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001212 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001213 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001214 LexID = false;
1215 continue;
1216 }
1217
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001218 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001219 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1220 return;
1221 }
Mike Stump11289f42009-09-09 15:08:12 +00001222
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001223 // We are execting a ')' or a ','.
1224 if (Tok.is(tok::comma)) {
1225 LexID = true;
1226 continue;
1227 }
Mike Stump11289f42009-09-09 15:08:12 +00001228
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001229 if (Tok.is(tok::r_paren)) {
1230 RParenLoc = Tok.getLocation();
1231 break;
1232 }
Mike Stump11289f42009-09-09 15:08:12 +00001233
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001234 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001235 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001236 return;
1237 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001238
1239 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001240 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001241 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1242 "unused";
1243 return;
1244 }
1245
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001246 // Verify that we have a location for the right parenthesis.
1247 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001248 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001249
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001250 // For each identifier token, insert into the token stream a
1251 // annot_pragma_unused token followed by the identifier token.
1252 // This allows us to cache a "#pragma unused" that occurs inside an inline
1253 // C++ member function.
1254
Daniel Dunbar340cf242012-02-29 01:38:22 +00001255 Token *Toks =
1256 (Token*) PP.getPreprocessorAllocator().Allocate(
1257 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001258 for (unsigned i=0; i != Identifiers.size(); i++) {
1259 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1260 pragmaUnusedTok.startToken();
1261 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1262 pragmaUnusedTok.setLocation(UnusedLoc);
1263 idTok = Identifiers[i];
1264 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001265 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1266 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001267}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001268
1269// #pragma weak identifier
1270// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001271void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1272 PragmaIntroducerKind Introducer,
1273 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001274 SourceLocation WeakLoc = WeakTok.getLocation();
1275
1276 Token Tok;
1277 PP.Lex(Tok);
1278 if (Tok.isNot(tok::identifier)) {
1279 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1280 return;
1281 }
1282
Eli Friedman68be1642012-10-04 02:36:51 +00001283 Token WeakName = Tok;
1284 bool HasAlias = false;
1285 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001286
1287 PP.Lex(Tok);
1288 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001289 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001290 PP.Lex(Tok);
1291 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001292 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001293 << "weak";
1294 return;
1295 }
Eli Friedman68be1642012-10-04 02:36:51 +00001296 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001297 PP.Lex(Tok);
1298 }
1299
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001300 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001301 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1302 return;
1303 }
1304
Eli Friedman68be1642012-10-04 02:36:51 +00001305 if (HasAlias) {
1306 Token *Toks =
1307 (Token*) PP.getPreprocessorAllocator().Allocate(
1308 sizeof(Token) * 3, llvm::alignOf<Token>());
1309 Token &pragmaUnusedTok = Toks[0];
1310 pragmaUnusedTok.startToken();
1311 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1312 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001313 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001314 Toks[1] = WeakName;
1315 Toks[2] = AliasName;
1316 PP.EnterTokenStream(Toks, 3,
1317 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001318 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001319 Token *Toks =
1320 (Token*) PP.getPreprocessorAllocator().Allocate(
1321 sizeof(Token) * 2, llvm::alignOf<Token>());
1322 Token &pragmaUnusedTok = Toks[0];
1323 pragmaUnusedTok.startToken();
1324 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1325 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001326 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001327 Toks[1] = WeakName;
1328 PP.EnterTokenStream(Toks, 2,
1329 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001330 }
1331}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001332
David Chisnall0867d9c2012-02-18 16:12:34 +00001333// #pragma redefine_extname identifier identifier
1334void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1335 PragmaIntroducerKind Introducer,
1336 Token &RedefToken) {
1337 SourceLocation RedefLoc = RedefToken.getLocation();
1338
1339 Token Tok;
1340 PP.Lex(Tok);
1341 if (Tok.isNot(tok::identifier)) {
1342 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1343 "redefine_extname";
1344 return;
1345 }
1346
Eli Friedman68be1642012-10-04 02:36:51 +00001347 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001348 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001349
David Chisnall0867d9c2012-02-18 16:12:34 +00001350 if (Tok.isNot(tok::identifier)) {
1351 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1352 << "redefine_extname";
1353 return;
1354 }
Eli Friedman68be1642012-10-04 02:36:51 +00001355
1356 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001357 PP.Lex(Tok);
1358
1359 if (Tok.isNot(tok::eod)) {
1360 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1361 "redefine_extname";
1362 return;
1363 }
1364
Eli Friedman68be1642012-10-04 02:36:51 +00001365 Token *Toks =
1366 (Token*) PP.getPreprocessorAllocator().Allocate(
1367 sizeof(Token) * 3, llvm::alignOf<Token>());
1368 Token &pragmaRedefTok = Toks[0];
1369 pragmaRedefTok.startToken();
1370 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1371 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001372 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001373 Toks[1] = RedefName;
1374 Toks[2] = AliasName;
1375 PP.EnterTokenStream(Toks, 3,
1376 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001377}
1378
1379
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001380void
1381PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1382 PragmaIntroducerKind Introducer,
1383 Token &Tok) {
1384 tok::OnOffSwitch OOS;
1385 if (PP.LexOnOffSwitch(OOS))
1386 return;
1387
Eli Friedman68be1642012-10-04 02:36:51 +00001388 Token *Toks =
1389 (Token*) PP.getPreprocessorAllocator().Allocate(
1390 sizeof(Token) * 1, llvm::alignOf<Token>());
1391 new (Toks) Token();
1392 Toks[0].startToken();
1393 Toks[0].setKind(tok::annot_pragma_fp_contract);
1394 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001395 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001396 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1397 static_cast<uintptr_t>(OOS)));
1398 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1399 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001400}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001401
1402void
1403PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1404 PragmaIntroducerKind Introducer,
1405 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001406 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001407 if (Tok.isNot(tok::identifier)) {
1408 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1409 "OPENCL";
1410 return;
1411 }
1412 IdentifierInfo *ename = Tok.getIdentifierInfo();
1413 SourceLocation NameLoc = Tok.getLocation();
1414
1415 PP.Lex(Tok);
1416 if (Tok.isNot(tok::colon)) {
1417 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1418 return;
1419 }
1420
1421 PP.Lex(Tok);
1422 if (Tok.isNot(tok::identifier)) {
1423 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1424 return;
1425 }
1426 IdentifierInfo *op = Tok.getIdentifierInfo();
1427
1428 unsigned state;
1429 if (op->isStr("enable")) {
1430 state = 1;
1431 } else if (op->isStr("disable")) {
1432 state = 0;
1433 } else {
1434 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1435 return;
1436 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001437 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001438
Eli Friedman68be1642012-10-04 02:36:51 +00001439 PP.Lex(Tok);
1440 if (Tok.isNot(tok::eod)) {
1441 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1442 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001443 return;
1444 }
Eli Friedman68be1642012-10-04 02:36:51 +00001445
1446 OpenCLExtData data(ename, state);
1447 Token *Toks =
1448 (Token*) PP.getPreprocessorAllocator().Allocate(
1449 sizeof(Token) * 1, llvm::alignOf<Token>());
1450 new (Toks) Token();
1451 Toks[0].startToken();
1452 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1453 Toks[0].setLocation(NameLoc);
1454 Toks[0].setAnnotationValue(data.getOpaqueValue());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001455 Toks[0].setAnnotationEndLoc(StateLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001456 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1457 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001458
1459 if (PP.getPPCallbacks())
1460 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1461 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001462}
1463
Alexey Bataeva769e072013-03-22 06:34:35 +00001464/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1465///
1466void
1467PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1468 PragmaIntroducerKind Introducer,
1469 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001470 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1471 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001472 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001473 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1474 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001475 }
1476 PP.DiscardUntilEndOfDirective();
1477}
1478
1479/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1480///
1481void
1482PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1483 PragmaIntroducerKind Introducer,
1484 Token &FirstTok) {
1485 SmallVector<Token, 16> Pragma;
1486 Token Tok;
1487 Tok.startToken();
1488 Tok.setKind(tok::annot_pragma_openmp);
1489 Tok.setLocation(FirstTok.getLocation());
1490
1491 while (Tok.isNot(tok::eod)) {
1492 Pragma.push_back(Tok);
1493 PP.Lex(Tok);
1494 }
1495 SourceLocation EodLoc = Tok.getLocation();
1496 Tok.startToken();
1497 Tok.setKind(tok::annot_pragma_openmp_end);
1498 Tok.setLocation(EodLoc);
1499 Pragma.push_back(Tok);
1500
1501 Token *Toks = new Token[Pragma.size()];
1502 std::copy(Pragma.begin(), Pragma.end(), Toks);
1503 PP.EnterTokenStream(Toks, Pragma.size(),
Alexey Bataevc8956792015-05-05 09:53:25 +00001504 /*DisableMacroExpansion=*/false, /*OwnsTokens=*/true);
Alexey Bataeva769e072013-03-22 06:34:35 +00001505}
Reid Kleckner002562a2013-05-06 21:02:12 +00001506
David Majnemer4bb09802014-02-10 19:50:15 +00001507/// \brief Handle '#pragma pointers_to_members'
1508// The grammar for this pragma is as follows:
1509//
1510// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1511//
1512// #pragma pointers_to_members '(' 'best_case' ')'
1513// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1514// #pragma pointers_to_members '(' inheritance-model ')'
1515void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1516 PragmaIntroducerKind Introducer,
1517 Token &Tok) {
1518 SourceLocation PointersToMembersLoc = Tok.getLocation();
1519 PP.Lex(Tok);
1520 if (Tok.isNot(tok::l_paren)) {
1521 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1522 << "pointers_to_members";
1523 return;
1524 }
1525 PP.Lex(Tok);
1526 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1527 if (!Arg) {
1528 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1529 << "pointers_to_members";
1530 return;
1531 }
1532 PP.Lex(Tok);
1533
David Majnemer86c318f2014-02-11 21:05:00 +00001534 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001535 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001536 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001537 } else {
1538 if (Arg->isStr("full_generality")) {
1539 if (Tok.is(tok::comma)) {
1540 PP.Lex(Tok);
1541
1542 Arg = Tok.getIdentifierInfo();
1543 if (!Arg) {
1544 PP.Diag(Tok.getLocation(),
1545 diag::err_pragma_pointers_to_members_unknown_kind)
1546 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1547 return;
1548 }
1549 PP.Lex(Tok);
1550 } else if (Tok.is(tok::r_paren)) {
1551 // #pragma pointers_to_members(full_generality) implicitly specifies
1552 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001553 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001554 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001555 } else {
1556 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1557 << "full_generality";
1558 return;
1559 }
1560 }
1561
1562 if (Arg) {
1563 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001564 RepresentationMethod =
1565 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001566 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001567 RepresentationMethod =
1568 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001569 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001570 RepresentationMethod =
1571 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001572 } else {
1573 PP.Diag(Tok.getLocation(),
1574 diag::err_pragma_pointers_to_members_unknown_kind)
1575 << Arg << /*HasPointerDeclaration*/ 1;
1576 return;
1577 }
1578 }
1579 }
1580
1581 if (Tok.isNot(tok::r_paren)) {
1582 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1583 << (Arg ? Arg->getName() : "full_generality");
1584 return;
1585 }
1586
David Majnemera8f2f1d2015-03-19 00:10:23 +00001587 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001588 PP.Lex(Tok);
1589 if (Tok.isNot(tok::eod)) {
1590 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1591 << "pointers_to_members";
1592 return;
1593 }
1594
1595 Token AnnotTok;
1596 AnnotTok.startToken();
1597 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1598 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001599 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001600 AnnotTok.setAnnotationValue(
1601 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1602 PP.EnterToken(AnnotTok);
1603}
1604
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001605/// \brief Handle '#pragma vtordisp'
1606// The grammar for this pragma is as follows:
1607//
1608// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1609//
1610// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1611// #pragma vtordisp '(' 'pop' ')'
1612// #pragma vtordisp '(' ')'
1613void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1614 PragmaIntroducerKind Introducer,
1615 Token &Tok) {
1616 SourceLocation VtorDispLoc = Tok.getLocation();
1617 PP.Lex(Tok);
1618 if (Tok.isNot(tok::l_paren)) {
1619 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1620 return;
1621 }
1622 PP.Lex(Tok);
1623
1624 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1625 const IdentifierInfo *II = Tok.getIdentifierInfo();
1626 if (II) {
1627 if (II->isStr("push")) {
1628 // #pragma vtordisp(push, mode)
1629 PP.Lex(Tok);
1630 if (Tok.isNot(tok::comma)) {
1631 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1632 return;
1633 }
1634 PP.Lex(Tok);
1635 Kind = Sema::PVDK_Push;
1636 // not push, could be on/off
1637 } else if (II->isStr("pop")) {
1638 // #pragma vtordisp(pop)
1639 PP.Lex(Tok);
1640 Kind = Sema::PVDK_Pop;
1641 }
1642 // not push or pop, could be on/off
1643 } else {
1644 if (Tok.is(tok::r_paren)) {
1645 // #pragma vtordisp()
1646 Kind = Sema::PVDK_Reset;
1647 }
1648 }
1649
1650
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001651 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001652 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1653 const IdentifierInfo *II = Tok.getIdentifierInfo();
1654 if (II && II->isStr("off")) {
1655 PP.Lex(Tok);
1656 Value = 0;
1657 } else if (II && II->isStr("on")) {
1658 PP.Lex(Tok);
1659 Value = 1;
1660 } else if (Tok.is(tok::numeric_constant) &&
1661 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1662 if (Value > 2) {
1663 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1664 << 0 << 2 << "vtordisp";
1665 return;
1666 }
1667 } else {
1668 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1669 << "vtordisp";
1670 return;
1671 }
1672 }
1673
1674 // Finish the pragma: ')' $
1675 if (Tok.isNot(tok::r_paren)) {
1676 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1677 return;
1678 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001679 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001680 PP.Lex(Tok);
1681 if (Tok.isNot(tok::eod)) {
1682 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1683 << "vtordisp";
1684 return;
1685 }
1686
1687 // Enter the annotation.
1688 Token AnnotTok;
1689 AnnotTok.startToken();
1690 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1691 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001692 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001693 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1694 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001695 PP.EnterToken(AnnotTok);
1696}
1697
Warren Huntc3b18962014-04-08 22:30:47 +00001698/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1699/// an annotation token.
1700void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1701 PragmaIntroducerKind Introducer,
1702 Token &Tok) {
1703 Token EoF, AnnotTok;
1704 EoF.startToken();
1705 EoF.setKind(tok::eof);
1706 AnnotTok.startToken();
1707 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1708 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001709 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001710 SmallVector<Token, 8> TokenVector;
1711 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001712 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001713 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001714 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1715 }
Warren Huntc3b18962014-04-08 22:30:47 +00001716 // Add a sentinal EoF token to the end of the list.
1717 TokenVector.push_back(EoF);
1718 // We must allocate this array with new because EnterTokenStream is going to
1719 // delete it later.
1720 Token *TokenArray = new Token[TokenVector.size()];
1721 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1722 auto Value = new (PP.getPreprocessorAllocator())
1723 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1724 AnnotTok.setAnnotationValue(Value);
1725 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001726}
1727
Aaron Ballman5d041be2013-06-04 02:07:14 +00001728/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1729///
1730/// The syntax is:
1731/// \code
1732/// #pragma detect_mismatch("name", "value")
1733/// \endcode
1734/// Where 'name' and 'value' are quoted strings. The values are embedded in
1735/// the object file and passed along to the linker. If the linker detects a
1736/// mismatch in the object file's values for the given name, a LNK2038 error
1737/// is emitted. See MSDN for more details.
1738void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1739 PragmaIntroducerKind Introducer,
1740 Token &Tok) {
1741 SourceLocation CommentLoc = Tok.getLocation();
1742 PP.Lex(Tok);
1743 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001744 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001745 return;
1746 }
1747
1748 // Read the name to embed, which must be a string literal.
1749 std::string NameString;
1750 if (!PP.LexStringLiteral(Tok, NameString,
1751 "pragma detect_mismatch",
1752 /*MacroExpansion=*/true))
1753 return;
1754
1755 // Read the comma followed by a second string literal.
1756 std::string ValueString;
1757 if (Tok.isNot(tok::comma)) {
1758 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1759 return;
1760 }
1761
1762 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1763 /*MacroExpansion=*/true))
1764 return;
1765
1766 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001767 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001768 return;
1769 }
1770 PP.Lex(Tok); // Eat the r_paren.
1771
1772 if (Tok.isNot(tok::eod)) {
1773 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1774 return;
1775 }
1776
Reid Kleckner71966c92014-02-20 23:37:45 +00001777 // If the pragma is lexically sound, notify any interested PPCallbacks.
1778 if (PP.getPPCallbacks())
1779 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1780 ValueString);
1781
Aaron Ballman5d041be2013-06-04 02:07:14 +00001782 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1783}
1784
Reid Kleckner002562a2013-05-06 21:02:12 +00001785/// \brief Handle the microsoft \#pragma comment extension.
1786///
1787/// The syntax is:
1788/// \code
1789/// #pragma comment(linker, "foo")
1790/// \endcode
1791/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1792/// "foo" is a string, which is fully macro expanded, and permits string
1793/// concatenation, embedded escape characters etc. See MSDN for more details.
1794void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1795 PragmaIntroducerKind Introducer,
1796 Token &Tok) {
1797 SourceLocation CommentLoc = Tok.getLocation();
1798 PP.Lex(Tok);
1799 if (Tok.isNot(tok::l_paren)) {
1800 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1801 return;
1802 }
1803
1804 // Read the identifier.
1805 PP.Lex(Tok);
1806 if (Tok.isNot(tok::identifier)) {
1807 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1808 return;
1809 }
1810
1811 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001812 IdentifierInfo *II = Tok.getIdentifierInfo();
1813 Sema::PragmaMSCommentKind Kind =
1814 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1815 .Case("linker", Sema::PCK_Linker)
1816 .Case("lib", Sema::PCK_Lib)
1817 .Case("compiler", Sema::PCK_Compiler)
1818 .Case("exestr", Sema::PCK_ExeStr)
1819 .Case("user", Sema::PCK_User)
1820 .Default(Sema::PCK_Unknown);
1821 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001822 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1823 return;
1824 }
1825
Yunzhong Gao99efc032015-03-23 20:41:42 +00001826 // On PS4, issue a warning about any pragma comments other than
1827 // #pragma comment lib.
1828 if (PP.getTargetInfo().getTriple().isPS4() && Kind != Sema::PCK_Lib) {
1829 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1830 << II->getName();
1831 return;
1832 }
1833
Reid Kleckner002562a2013-05-06 21:02:12 +00001834 // Read the optional string if present.
1835 PP.Lex(Tok);
1836 std::string ArgumentString;
1837 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1838 "pragma comment",
1839 /*MacroExpansion=*/true))
1840 return;
1841
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001842 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001843 // FIXME: If the kind is "compiler" warn if the string is present (it is
1844 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001845 // The MSDN docs say that "lib" and "linker" require a string and have a short
1846 // whitelist of linker options they support, but in practice MSVC doesn't
1847 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001848
1849 if (Tok.isNot(tok::r_paren)) {
1850 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1851 return;
1852 }
1853 PP.Lex(Tok); // eat the r_paren.
1854
1855 if (Tok.isNot(tok::eod)) {
1856 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1857 return;
1858 }
1859
Reid Kleckner71966c92014-02-20 23:37:45 +00001860 // If the pragma is lexically sound, notify any interested PPCallbacks.
1861 if (PP.getPPCallbacks())
1862 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1863
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001864 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001865}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001866
1867// #pragma clang optimize off
1868// #pragma clang optimize on
1869void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1870 PragmaIntroducerKind Introducer,
1871 Token &FirstToken) {
1872 Token Tok;
1873 PP.Lex(Tok);
1874 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001875 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001876 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001877 return;
1878 }
1879 if (Tok.isNot(tok::identifier)) {
1880 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1881 << PP.getSpelling(Tok);
1882 return;
1883 }
1884 const IdentifierInfo *II = Tok.getIdentifierInfo();
1885 // The only accepted values are 'on' or 'off'.
1886 bool IsOn = false;
1887 if (II->isStr("on")) {
1888 IsOn = true;
1889 } else if (!II->isStr("off")) {
1890 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1891 << PP.getSpelling(Tok);
1892 return;
1893 }
1894 PP.Lex(Tok);
1895
1896 if (Tok.isNot(tok::eod)) {
1897 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1898 << PP.getSpelling(Tok);
1899 return;
1900 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001901
1902 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1903}
1904
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001905/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001906static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1907 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001908 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001909 SmallVector<Token, 1> ValueList;
1910 int OpenParens = ValueInParens ? 1 : 0;
1911 // Read constant expression.
1912 while (Tok.isNot(tok::eod)) {
1913 if (Tok.is(tok::l_paren))
1914 OpenParens++;
1915 else if (Tok.is(tok::r_paren)) {
1916 OpenParens--;
1917 if (OpenParens == 0 && ValueInParens)
1918 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001919 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001920
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001921 ValueList.push_back(Tok);
1922 PP.Lex(Tok);
1923 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001924
1925 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001926 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001927 if (Tok.isNot(tok::r_paren)) {
1928 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1929 return true;
1930 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001931 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001932 }
1933
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001934 Token EOFTok;
1935 EOFTok.startToken();
1936 EOFTok.setKind(tok::eof);
1937 EOFTok.setLocation(Tok.getLocation());
1938 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1939
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001940 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001941
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001942 Info.PragmaName = PragmaName;
1943 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001944 return false;
1945}
1946
Eli Bendersky06a40422014-06-06 20:31:48 +00001947/// \brief Handle the \#pragma clang loop directive.
1948/// #pragma clang 'loop' loop-hints
1949///
1950/// loop-hints:
1951/// loop-hint loop-hints[opt]
1952///
1953/// loop-hint:
1954/// 'vectorize' '(' loop-hint-keyword ')'
1955/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001956/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001957/// 'vectorize_width' '(' loop-hint-value ')'
1958/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001959/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001960///
1961/// loop-hint-keyword:
1962/// 'enable'
1963/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00001964/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00001965///
Mark Heffernan450c2382014-07-23 17:31:31 +00001966/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00001967/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00001968/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00001969/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00001970///
Eli Bendersky06a40422014-06-06 20:31:48 +00001971/// loop-hint-value:
1972/// constant-expression
1973///
1974/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1975/// try vectorizing the instructions of the loop it precedes. Specifying
1976/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1977/// interleaving multiple iterations of the loop it precedes. The width of the
1978/// vector instructions is specified by vectorize_width() and the number of
1979/// interleaved loop iterations is specified by interleave_count(). Specifying a
1980/// value of 1 effectively disables vectorization/interleaving, even if it is
1981/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1982/// only works on inner loops.
1983///
Eli Bendersky86483b32014-06-11 17:56:26 +00001984/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00001985/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
1986/// completely if the trip count is known at compile time and unroll partially
1987/// if the trip count is not known. Specifying unroll(full) is similar to
1988/// unroll(enable) but will unroll the loop only if the trip count is known at
1989/// compile time. Specifying unroll(disable) disables unrolling for the
1990/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
1991/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001992void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1993 PragmaIntroducerKind Introducer,
1994 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001995 // Incoming token is "loop" from "#pragma clang loop".
1996 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001997 SmallVector<Token, 1> TokenList;
1998
1999 // Lex the optimization option and verify it is an identifier.
2000 PP.Lex(Tok);
2001 if (Tok.isNot(tok::identifier)) {
2002 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2003 << /*MissingOption=*/true << "";
2004 return;
2005 }
2006
2007 while (Tok.is(tok::identifier)) {
2008 Token Option = Tok;
2009 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2010
Eli Bendersky86483b32014-06-11 17:56:26 +00002011 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002012 .Case("vectorize", true)
2013 .Case("interleave", true)
2014 .Case("unroll", true)
2015 .Case("vectorize_width", true)
2016 .Case("interleave_count", true)
2017 .Case("unroll_count", true)
2018 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002019 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002020 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2021 << /*MissingOption=*/false << OptionInfo;
2022 return;
2023 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002024 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002025
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002026 // Read '('
2027 if (Tok.isNot(tok::l_paren)) {
2028 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002029 return;
2030 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002031 PP.Lex(Tok);
2032
2033 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2034 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2035 *Info))
2036 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002037
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002038 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002039 Token LoopHintTok;
2040 LoopHintTok.startToken();
2041 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002042 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002043 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002044 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2045 TokenList.push_back(LoopHintTok);
2046 }
2047
2048 if (Tok.isNot(tok::eod)) {
2049 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2050 << "clang loop";
2051 return;
2052 }
2053
2054 Token *TokenArray = new Token[TokenList.size()];
2055 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
2056
2057 PP.EnterTokenStream(TokenArray, TokenList.size(),
2058 /*DisableMacroExpansion=*/false,
2059 /*OwnsTokens=*/true);
2060}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002061
2062/// \brief Handle the loop unroll optimization pragmas.
2063/// #pragma unroll
2064/// #pragma unroll unroll-hint-value
2065/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002066/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002067///
2068/// unroll-hint-value:
2069/// constant-expression
2070///
Mark Heffernanc888e412014-07-24 18:09:38 +00002071/// Loop unrolling hints can be specified with '#pragma unroll' or
2072/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2073/// contained in parentheses. With no argument the directive instructs llvm to
2074/// try to unroll the loop completely. A positive integer argument can be
2075/// specified to indicate the number of times the loop should be unrolled. To
2076/// maximize compatibility with other compilers the unroll count argument can be
2077/// specified with or without parentheses. Specifying, '#pragma nounroll'
2078/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002079void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2080 PragmaIntroducerKind Introducer,
2081 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002082 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2083 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002084 Token PragmaName = Tok;
2085 PP.Lex(Tok);
2086 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2087 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002088 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002089 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002090 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002091 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2092 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2093 << "nounroll";
2094 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002095 } else {
2096 // Unroll pragma with an argument: "#pragma unroll N" or
2097 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002098 // Read '(' if it exists.
2099 bool ValueInParens = Tok.is(tok::l_paren);
2100 if (ValueInParens)
2101 PP.Lex(Tok);
2102
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002103 Token Option;
2104 Option.startToken();
2105 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002106 return;
2107
2108 // In CUDA, the argument to '#pragma unroll' should not be contained in
2109 // parentheses.
2110 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002111 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002112 diag::warn_pragma_unroll_cuda_value_in_parens);
2113
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002114 if (Tok.isNot(tok::eod)) {
2115 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2116 << "unroll";
2117 return;
2118 }
2119 }
2120
2121 // Generate the hint token.
2122 Token *TokenArray = new Token[1];
2123 TokenArray[0].startToken();
2124 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2125 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002126 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002127 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2128 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2129 /*OwnsTokens=*/true);
2130}