blob: aa7b843837d5b213bfbf2f21df3f74608cff1858 [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
Eli Bendersky06a40422014-06-06 20:31:48 +0000159} // end namespace
160
161void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000162 AlignHandler.reset(new PragmaAlignHandler());
163 PP.AddPragmaHandler(AlignHandler.get());
164
165 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
166 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
167
168 OptionsHandler.reset(new PragmaOptionsHandler());
169 PP.AddPragmaHandler(OptionsHandler.get());
170
171 PackHandler.reset(new PragmaPackHandler());
172 PP.AddPragmaHandler(PackHandler.get());
173
174 MSStructHandler.reset(new PragmaMSStructHandler());
175 PP.AddPragmaHandler(MSStructHandler.get());
176
177 UnusedHandler.reset(new PragmaUnusedHandler());
178 PP.AddPragmaHandler(UnusedHandler.get());
179
180 WeakHandler.reset(new PragmaWeakHandler());
181 PP.AddPragmaHandler(WeakHandler.get());
182
183 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
184 PP.AddPragmaHandler(RedefineExtnameHandler.get());
185
186 FPContractHandler.reset(new PragmaFPContractHandler());
187 PP.AddPragmaHandler("STDC", FPContractHandler.get());
188
189 if (getLangOpts().OpenCL) {
190 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
191 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
192
193 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
194 }
195 if (getLangOpts().OpenMP)
196 OpenMPHandler.reset(new PragmaOpenMPHandler());
197 else
198 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
199 PP.AddPragmaHandler(OpenMPHandler.get());
200
Yunzhong Gao99efc032015-03-23 20:41:42 +0000201 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000202 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
203 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000204 }
205
206 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000207 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
208 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
209 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
210 PP.AddPragmaHandler(MSPointersToMembers.get());
211 MSVtorDisp.reset(new PragmaMSVtorDisp());
212 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000213 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000214 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000215 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
216 PP.AddPragmaHandler(MSDataSeg.get());
217 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
218 PP.AddPragmaHandler(MSBSSSeg.get());
219 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
220 PP.AddPragmaHandler(MSConstSeg.get());
221 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
222 PP.AddPragmaHandler(MSCodeSeg.get());
223 MSSection.reset(new PragmaMSPragma("section"));
224 PP.AddPragmaHandler(MSSection.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000225 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000226
227 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
228 PP.AddPragmaHandler("clang", OptimizeHandler.get());
229
230 LoopHintHandler.reset(new PragmaLoopHintHandler());
231 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000232
233 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
234 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000235
236 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
237 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000238}
239
240void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000241 // Remove the pragma handlers we installed.
242 PP.RemovePragmaHandler(AlignHandler.get());
243 AlignHandler.reset();
244 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
245 GCCVisibilityHandler.reset();
246 PP.RemovePragmaHandler(OptionsHandler.get());
247 OptionsHandler.reset();
248 PP.RemovePragmaHandler(PackHandler.get());
249 PackHandler.reset();
250 PP.RemovePragmaHandler(MSStructHandler.get());
251 MSStructHandler.reset();
252 PP.RemovePragmaHandler(UnusedHandler.get());
253 UnusedHandler.reset();
254 PP.RemovePragmaHandler(WeakHandler.get());
255 WeakHandler.reset();
256 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
257 RedefineExtnameHandler.reset();
258
259 if (getLangOpts().OpenCL) {
260 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
261 OpenCLExtensionHandler.reset();
262 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
263 }
264 PP.RemovePragmaHandler(OpenMPHandler.get());
265 OpenMPHandler.reset();
266
Yunzhong Gao99efc032015-03-23 20:41:42 +0000267 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000268 PP.RemovePragmaHandler(MSCommentHandler.get());
269 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000270 }
271
272 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000273 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
274 MSDetectMismatchHandler.reset();
275 PP.RemovePragmaHandler(MSPointersToMembers.get());
276 MSPointersToMembers.reset();
277 PP.RemovePragmaHandler(MSVtorDisp.get());
278 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000279 PP.RemovePragmaHandler(MSInitSeg.get());
280 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000281 PP.RemovePragmaHandler(MSDataSeg.get());
282 MSDataSeg.reset();
283 PP.RemovePragmaHandler(MSBSSSeg.get());
284 MSBSSSeg.reset();
285 PP.RemovePragmaHandler(MSConstSeg.get());
286 MSConstSeg.reset();
287 PP.RemovePragmaHandler(MSCodeSeg.get());
288 MSCodeSeg.reset();
289 PP.RemovePragmaHandler(MSSection.get());
290 MSSection.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000291 }
292
293 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
294 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000295
296 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
297 OptimizeHandler.reset();
298
299 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
300 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000301
302 PP.RemovePragmaHandler(UnrollHintHandler.get());
303 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000304
305 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
306 NoUnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000307}
308
309/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000310///
311/// Each annot_pragma_unused is followed by the argument token so e.g.
312/// "#pragma unused(x,y)" becomes:
313/// annot_pragma_unused 'x' annot_pragma_unused 'y'
314void Parser::HandlePragmaUnused() {
315 assert(Tok.is(tok::annot_pragma_unused));
316 SourceLocation UnusedLoc = ConsumeToken();
317 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
318 ConsumeToken(); // The argument token.
319}
Eli Friedman570024a2010-08-05 06:57:20 +0000320
Rafael Espindola273fd772012-01-26 02:02:57 +0000321void Parser::HandlePragmaVisibility() {
322 assert(Tok.is(tok::annot_pragma_vis));
323 const IdentifierInfo *VisType =
324 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
325 SourceLocation VisLoc = ConsumeToken();
326 Actions.ActOnPragmaVisibility(VisType, VisLoc);
327}
328
Eli Friedmanec52f922012-02-23 23:47:16 +0000329struct PragmaPackInfo {
330 Sema::PragmaPackKind Kind;
331 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000332 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000333 SourceLocation LParenLoc;
334 SourceLocation RParenLoc;
335};
336
337void Parser::HandlePragmaPack() {
338 assert(Tok.is(tok::annot_pragma_pack));
339 PragmaPackInfo *Info =
340 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
341 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000342 ExprResult Alignment;
343 if (Info->Alignment.is(tok::numeric_constant)) {
344 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
345 if (Alignment.isInvalid())
346 return;
347 }
348 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000349 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000350}
351
Eli Friedman68be1642012-10-04 02:36:51 +0000352void Parser::HandlePragmaMSStruct() {
353 assert(Tok.is(tok::annot_pragma_msstruct));
354 Sema::PragmaMSStructKind Kind =
355 static_cast<Sema::PragmaMSStructKind>(
356 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
357 Actions.ActOnPragmaMSStruct(Kind);
358 ConsumeToken(); // The annotation token.
359}
360
361void Parser::HandlePragmaAlign() {
362 assert(Tok.is(tok::annot_pragma_align));
363 Sema::PragmaOptionsAlignKind Kind =
364 static_cast<Sema::PragmaOptionsAlignKind>(
365 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
366 SourceLocation PragmaLoc = ConsumeToken();
367 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
368}
369
370void Parser::HandlePragmaWeak() {
371 assert(Tok.is(tok::annot_pragma_weak));
372 SourceLocation PragmaLoc = ConsumeToken();
373 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
374 Tok.getLocation());
375 ConsumeToken(); // The weak name.
376}
377
378void Parser::HandlePragmaWeakAlias() {
379 assert(Tok.is(tok::annot_pragma_weakalias));
380 SourceLocation PragmaLoc = ConsumeToken();
381 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
382 SourceLocation WeakNameLoc = Tok.getLocation();
383 ConsumeToken();
384 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
385 SourceLocation AliasNameLoc = Tok.getLocation();
386 ConsumeToken();
387 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
388 WeakNameLoc, AliasNameLoc);
389
390}
391
392void Parser::HandlePragmaRedefineExtname() {
393 assert(Tok.is(tok::annot_pragma_redefine_extname));
394 SourceLocation RedefLoc = ConsumeToken();
395 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
396 SourceLocation RedefNameLoc = Tok.getLocation();
397 ConsumeToken();
398 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
399 SourceLocation AliasNameLoc = Tok.getLocation();
400 ConsumeToken();
401 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
402 RedefNameLoc, AliasNameLoc);
403}
404
405void Parser::HandlePragmaFPContract() {
406 assert(Tok.is(tok::annot_pragma_fp_contract));
407 tok::OnOffSwitch OOS =
408 static_cast<tok::OnOffSwitch>(
409 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
410 Actions.ActOnPragmaFPContract(OOS);
411 ConsumeToken(); // The annotation token.
412}
413
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000414StmtResult Parser::HandlePragmaCaptured()
415{
416 assert(Tok.is(tok::annot_pragma_captured));
417 ConsumeToken();
418
419 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000420 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000421 return StmtError();
422 }
423
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000424 SourceLocation Loc = Tok.getLocation();
425
426 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000427 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
428 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000429
430 StmtResult R = ParseCompoundStatement();
431 CapturedRegionScope.Exit();
432
433 if (R.isInvalid()) {
434 Actions.ActOnCapturedRegionError();
435 return StmtError();
436 }
437
438 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000439}
440
Eli Friedman68be1642012-10-04 02:36:51 +0000441namespace {
442 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
443}
444
445void Parser::HandlePragmaOpenCLExtension() {
446 assert(Tok.is(tok::annot_pragma_opencl_extension));
447 OpenCLExtData data =
448 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
449 unsigned state = data.getInt();
450 IdentifierInfo *ename = data.getPointer();
451 SourceLocation NameLoc = Tok.getLocation();
452 ConsumeToken(); // The annotation token.
453
454 OpenCLOptions &f = Actions.getOpenCLOptions();
455 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
456 // overriding all previously issued extension directives, but only if the
457 // behavior is set to disable."
458 if (state == 0 && ename->isStr("all")) {
459#define OPENCLEXT(nm) f.nm = 0;
460#include "clang/Basic/OpenCLExtensions.def"
461 }
462#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
463#include "clang/Basic/OpenCLExtensions.def"
464 else {
465 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
466 return;
467 }
468}
469
David Majnemer4bb09802014-02-10 19:50:15 +0000470void Parser::HandlePragmaMSPointersToMembers() {
471 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000472 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
473 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000474 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
475 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
476 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
477}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000478
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000479void Parser::HandlePragmaMSVtorDisp() {
480 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
481 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
482 Sema::PragmaVtorDispKind Kind =
483 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
484 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
485 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
486 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
487}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000488
Warren Huntc3b18962014-04-08 22:30:47 +0000489void Parser::HandlePragmaMSPragma() {
490 assert(Tok.is(tok::annot_pragma_ms_pragma));
491 // Grab the tokens out of the annotation and enter them into the stream.
492 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
493 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
494 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
495 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000496 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000497 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000498
Warren Huntc3b18962014-04-08 22:30:47 +0000499 // Figure out which #pragma we're dealing with. The switch has no default
500 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000501 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000502 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
503 .Case("data_seg", &Parser::HandlePragmaMSSegment)
504 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
505 .Case("const_seg", &Parser::HandlePragmaMSSegment)
506 .Case("code_seg", &Parser::HandlePragmaMSSegment)
507 .Case("section", &Parser::HandlePragmaMSSection)
508 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000509
510 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
511 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
512 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000513 while (Tok.isNot(tok::eof))
514 PP.Lex(Tok);
515 PP.Lex(Tok);
516 }
517}
518
Reid Kleckner722b1df2014-07-18 00:13:16 +0000519bool Parser::HandlePragmaMSSection(StringRef PragmaName,
520 SourceLocation PragmaLocation) {
521 if (Tok.isNot(tok::l_paren)) {
522 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
523 return false;
524 }
Warren Huntc3b18962014-04-08 22:30:47 +0000525 PP.Lex(Tok); // (
526 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000527 if (Tok.isNot(tok::string_literal)) {
528 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
529 << PragmaName;
530 return false;
531 }
532 ExprResult StringResult = ParseStringLiteralExpression();
533 if (StringResult.isInvalid())
534 return false; // Already diagnosed.
535 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
536 if (SegmentName->getCharByteWidth() != 1) {
537 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
538 << PragmaName;
539 return false;
540 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000541 int SectionFlags = ASTContext::PSF_Read;
542 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000543 while (Tok.is(tok::comma)) {
544 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000545 // Ignore "long" and "short".
546 // They are undocumented, but widely used, section attributes which appear
547 // to do nothing.
548 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
549 PP.Lex(Tok); // long/short
550 continue;
551 }
552
Reid Kleckner722b1df2014-07-18 00:13:16 +0000553 if (!Tok.isAnyIdentifier()) {
554 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
555 << PragmaName;
556 return false;
557 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000558 ASTContext::PragmaSectionFlag Flag =
559 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000560 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000561 .Case("read", ASTContext::PSF_Read)
562 .Case("write", ASTContext::PSF_Write)
563 .Case("execute", ASTContext::PSF_Execute)
564 .Case("shared", ASTContext::PSF_Invalid)
565 .Case("nopage", ASTContext::PSF_Invalid)
566 .Case("nocache", ASTContext::PSF_Invalid)
567 .Case("discard", ASTContext::PSF_Invalid)
568 .Case("remove", ASTContext::PSF_Invalid)
569 .Default(ASTContext::PSF_None);
570 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
571 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000572 ? diag::warn_pragma_invalid_specific_action
573 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000574 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000575 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000576 }
577 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000578 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000579 PP.Lex(Tok); // Identifier
580 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000581 // If no section attributes are specified, the section will be marked as
582 // read/write.
583 if (SectionFlagsAreDefault)
584 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000585 if (Tok.isNot(tok::r_paren)) {
586 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
587 return false;
588 }
Warren Huntc3b18962014-04-08 22:30:47 +0000589 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000590 if (Tok.isNot(tok::eof)) {
591 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
592 << PragmaName;
593 return false;
594 }
Warren Huntc3b18962014-04-08 22:30:47 +0000595 PP.Lex(Tok); // eof
596 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000597 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000598}
599
Reid Kleckner722b1df2014-07-18 00:13:16 +0000600bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
601 SourceLocation PragmaLocation) {
602 if (Tok.isNot(tok::l_paren)) {
603 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
604 return false;
605 }
Warren Huntc3b18962014-04-08 22:30:47 +0000606 PP.Lex(Tok); // (
607 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000608 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000609 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000610 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000611 if (PushPop == "push")
612 Action = Sema::PSK_Push;
613 else if (PushPop == "pop")
614 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000615 else {
616 PP.Diag(PragmaLocation,
617 diag::warn_pragma_expected_section_push_pop_or_name)
618 << PragmaName;
619 return false;
620 }
Warren Huntc3b18962014-04-08 22:30:47 +0000621 if (Action != Sema::PSK_Reset) {
622 PP.Lex(Tok); // push | pop
623 if (Tok.is(tok::comma)) {
624 PP.Lex(Tok); // ,
625 // If we've got a comma, we either need a label or a string.
626 if (Tok.isAnyIdentifier()) {
627 SlotLabel = Tok.getIdentifierInfo()->getName();
628 PP.Lex(Tok); // identifier
629 if (Tok.is(tok::comma))
630 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000631 else if (Tok.isNot(tok::r_paren)) {
632 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
633 << PragmaName;
634 return false;
635 }
Warren Huntc3b18962014-04-08 22:30:47 +0000636 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000637 } else if (Tok.isNot(tok::r_paren)) {
638 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
639 return false;
640 }
Warren Huntc3b18962014-04-08 22:30:47 +0000641 }
642 }
643 // Grab the string literal for our section name.
644 StringLiteral *SegmentName = nullptr;
645 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000646 if (Tok.isNot(tok::string_literal)) {
647 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000648 diag::warn_pragma_expected_section_name :
649 diag::warn_pragma_expected_section_label_or_name :
650 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000651 PP.Diag(PragmaLocation, DiagID) << PragmaName;
652 return false;
653 }
654 ExprResult StringResult = ParseStringLiteralExpression();
655 if (StringResult.isInvalid())
656 return false; // Already diagnosed.
657 SegmentName = cast<StringLiteral>(StringResult.get());
658 if (SegmentName->getCharByteWidth() != 1) {
659 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
660 << PragmaName;
661 return false;
662 }
Warren Huntc3b18962014-04-08 22:30:47 +0000663 // Setting section "" has no effect
664 if (SegmentName->getLength())
665 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
666 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000667 if (Tok.isNot(tok::r_paren)) {
668 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
669 return false;
670 }
Warren Huntc3b18962014-04-08 22:30:47 +0000671 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000672 if (Tok.isNot(tok::eof)) {
673 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
674 << PragmaName;
675 return false;
676 }
Warren Huntc3b18962014-04-08 22:30:47 +0000677 PP.Lex(Tok); // eof
678 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
679 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000680 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000681}
682
Reid Kleckner1a711b12014-07-22 00:53:05 +0000683// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000684bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
685 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000686 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
687 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
688 return false;
689 }
690
Reid Kleckner1a711b12014-07-22 00:53:05 +0000691 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
692 PragmaName))
693 return false;
694
695 // Parse either the known section names or the string section name.
696 StringLiteral *SegmentName = nullptr;
697 if (Tok.isAnyIdentifier()) {
698 auto *II = Tok.getIdentifierInfo();
699 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
700 .Case("compiler", "\".CRT$XCC\"")
701 .Case("lib", "\".CRT$XCL\"")
702 .Case("user", "\".CRT$XCU\"")
703 .Default("");
704
705 if (!Section.empty()) {
706 // Pretend the user wrote the appropriate string literal here.
707 Token Toks[1];
708 Toks[0].startToken();
709 Toks[0].setKind(tok::string_literal);
710 Toks[0].setLocation(Tok.getLocation());
711 Toks[0].setLiteralData(Section.data());
712 Toks[0].setLength(Section.size());
713 SegmentName =
714 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
715 PP.Lex(Tok);
716 }
717 } else if (Tok.is(tok::string_literal)) {
718 ExprResult StringResult = ParseStringLiteralExpression();
719 if (StringResult.isInvalid())
720 return false;
721 SegmentName = cast<StringLiteral>(StringResult.get());
722 if (SegmentName->getCharByteWidth() != 1) {
723 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
724 << PragmaName;
725 return false;
726 }
727 // FIXME: Add support for the '[, func-name]' part of the pragma.
728 }
729
730 if (!SegmentName) {
731 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
732 return false;
733 }
734
735 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
736 PragmaName) ||
737 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
738 PragmaName))
739 return false;
740
741 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
742 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000743}
744
745struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000746 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000747 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000748 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000749};
750
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000751static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
752 std::string PragmaString;
753 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
754 PragmaString = "clang loop ";
755 PragmaString += Option.getIdentifierInfo()->getName();
756 } else {
757 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
758 "Unexpected pragma name");
759 PragmaString = "unroll";
760 }
761 return PragmaString;
762}
763
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000764bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000765 assert(Tok.is(tok::annot_pragma_loop_hint));
766 PragmaLoopHintInfo *Info =
767 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
768
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000769 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
770 Hint.PragmaNameLoc = IdentifierLoc::create(
771 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000772
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000773 // It is possible that the loop hint has no option identifier, such as
774 // #pragma unroll(4).
775 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
776 ? Info->Option.getIdentifierInfo()
777 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000778 Hint.OptionLoc = IdentifierLoc::create(
779 Actions.Context, Info->Option.getLocation(), OptionInfo);
780
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000781 const Token *Toks = Info->Toks.data();
782 size_t TokSize = Info->Toks.size();
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000783
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000784 // Return a valid hint if pragma unroll or nounroll were specified
785 // without an argument.
786 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
787 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000788 if (TokSize == 0 && (PragmaUnroll || PragmaNoUnroll)) {
789 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000790 Hint.Range = Info->PragmaName.getLocation();
791 return true;
792 }
793
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000794 // The constant expression is always followed by an eof token, which increases
795 // the TokSize by 1.
796 assert(TokSize > 0 &&
797 "PragmaLoopHintInfo::Toks must contain at least one token.");
798
799 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000800 bool OptionUnroll = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000801 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000802 if (OptionInfo) { // Pragma Unroll does not specify an option.
803 OptionUnroll = OptionInfo->isStr("unroll");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000804 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
805 .Case("vectorize", true)
806 .Case("interleave", true)
807 .Case("unroll", true)
808 .Default(false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000809 }
810
811 // Verify loop hint has an argument.
812 if (Toks[0].is(tok::eof)) {
813 ConsumeToken(); // The annotation token.
814 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Tyler Nowicki24853c12015-06-08 23:13:43 +0000815 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000816 return false;
817 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000818
819 // Validate the argument.
820 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000821 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000822 SourceLocation StateLoc = Toks[0].getLocation();
823 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000824 if (!StateInfo ||
825 ((OptionUnroll ? !StateInfo->isStr("full")
826 : !StateInfo->isStr("enable") &&
827 !StateInfo->isStr("assume_safety")) &&
828 !StateInfo->isStr("disable"))) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000829 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
830 << /*FullKeyword=*/OptionUnroll;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000831 return false;
832 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000833 if (TokSize > 2)
834 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
835 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000836 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
837 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000838 // Enter constant expression including eof terminator into token stream.
839 PP.EnterTokenStream(Toks, TokSize, /*DisableMacroExpansion=*/false,
840 /*OwnsTokens=*/false);
841 ConsumeToken(); // The annotation token.
842
843 ExprResult R = ParseConstantExpression();
844
845 // Tokens following an error in an ill-formed constant expression will
846 // remain in the token stream and must be removed.
847 if (Tok.isNot(tok::eof)) {
848 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
849 << PragmaLoopHintString(Info->PragmaName, Info->Option);
850 while (Tok.isNot(tok::eof))
851 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000852 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000853
854 ConsumeToken(); // Consume the constant expression eof terminator.
855
856 if (R.isInvalid() ||
857 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
858 return false;
859
860 // Argument is a constant expression with an integer type.
861 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000862 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000863
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000864 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
865 Info->Toks[TokSize - 1].getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000866 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000867}
868
869// #pragma GCC visibility comes in two variants:
870// 'push' '(' [visibility] ')'
871// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000872void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
873 PragmaIntroducerKind Introducer,
874 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000875 SourceLocation VisLoc = VisTok.getLocation();
876
877 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000878 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000879
880 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
881
Eli Friedman570024a2010-08-05 06:57:20 +0000882 const IdentifierInfo *VisType;
883 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000884 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000885 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000886 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000887 if (Tok.isNot(tok::l_paren)) {
888 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
889 << "visibility";
890 return;
891 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000892 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000893 VisType = Tok.getIdentifierInfo();
894 if (!VisType) {
895 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
896 << "visibility";
897 return;
898 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000899 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000900 if (Tok.isNot(tok::r_paren)) {
901 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
902 << "visibility";
903 return;
904 }
905 } else {
906 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
907 << "visibility";
908 return;
909 }
David Majnemera8f2f1d2015-03-19 00:10:23 +0000910 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000911 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000912 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000913 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
914 << "visibility";
915 return;
916 }
917
Rafael Espindola273fd772012-01-26 02:02:57 +0000918 Token *Toks = new Token[1];
919 Toks[0].startToken();
920 Toks[0].setKind(tok::annot_pragma_vis);
921 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000922 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +0000923 Toks[0].setAnnotationValue(
924 const_cast<void*>(static_cast<const void*>(VisType)));
925 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
926 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000927}
928
Daniel Dunbar921b9682008-10-04 19:21:03 +0000929// #pragma pack(...) comes in the following delicious flavors:
930// pack '(' [integer] ')'
931// pack '(' 'show' ')'
932// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000933void PragmaPackHandler::HandlePragma(Preprocessor &PP,
934 PragmaIntroducerKind Introducer,
935 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000936 SourceLocation PackLoc = PackTok.getLocation();
937
938 Token Tok;
939 PP.Lex(Tok);
940 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000941 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000942 return;
943 }
944
John McCallfaf5fb42010-08-26 23:41:50 +0000945 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000946 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000947 Token Alignment;
948 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000949 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000950 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000951 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000952 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000953
954 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000955
956 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
957 // the push/pop stack.
958 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000959 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000960 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000961 } else if (Tok.is(tok::identifier)) {
962 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000963 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000964 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000965 PP.Lex(Tok);
966 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000967 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000968 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000969 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000970 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000971 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000972 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000973 return;
Mike Stump11289f42009-09-09 15:08:12 +0000974 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000975 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000976
Daniel Dunbar921b9682008-10-04 19:21:03 +0000977 if (Tok.is(tok::comma)) {
978 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000979
Daniel Dunbar921b9682008-10-04 19:21:03 +0000980 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000981 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000982
983 PP.Lex(Tok);
984 } else if (Tok.is(tok::identifier)) {
985 Name = Tok.getIdentifierInfo();
986 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.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000992 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000993 return;
994 }
Mike Stump11289f42009-09-09 15:08:12 +0000995
Eli Friedman68be1642012-10-04 02:36:51 +0000996 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000997
998 PP.Lex(Tok);
999 }
1000 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001001 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001002 return;
1003 }
1004 }
1005 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001006 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001007 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1008 // the push/pop stack.
1009 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1010 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001011 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001012
1013 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001014 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001015 return;
1016 }
1017
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001018 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001019 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001020 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001021 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1022 return;
1023 }
1024
Daniel Dunbar340cf242012-02-29 01:38:22 +00001025 PragmaPackInfo *Info =
1026 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
1027 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
1028 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +00001029 Info->Kind = Kind;
1030 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +00001031 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001032 Info->LParenLoc = LParenLoc;
1033 Info->RParenLoc = RParenLoc;
1034
Daniel Dunbar340cf242012-02-29 01:38:22 +00001035 Token *Toks =
1036 (Token*) PP.getPreprocessorAllocator().Allocate(
1037 sizeof(Token) * 1, llvm::alignOf<Token>());
1038 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +00001039 Toks[0].startToken();
1040 Toks[0].setKind(tok::annot_pragma_pack);
1041 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001042 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001043 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1044 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +00001045 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001046}
1047
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001048// #pragma ms_struct on
1049// #pragma ms_struct off
1050void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1051 PragmaIntroducerKind Introducer,
1052 Token &MSStructTok) {
1053 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
1054
1055 Token Tok;
1056 PP.Lex(Tok);
1057 if (Tok.isNot(tok::identifier)) {
1058 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1059 return;
1060 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001061 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001062 const IdentifierInfo *II = Tok.getIdentifierInfo();
1063 if (II->isStr("on")) {
1064 Kind = Sema::PMSST_ON;
1065 PP.Lex(Tok);
1066 }
1067 else if (II->isStr("off") || II->isStr("reset"))
1068 PP.Lex(Tok);
1069 else {
1070 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1071 return;
1072 }
1073
1074 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001075 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1076 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001077 return;
1078 }
Eli Friedman68be1642012-10-04 02:36:51 +00001079
1080 Token *Toks =
1081 (Token*) PP.getPreprocessorAllocator().Allocate(
1082 sizeof(Token) * 1, llvm::alignOf<Token>());
1083 new (Toks) Token();
1084 Toks[0].startToken();
1085 Toks[0].setKind(tok::annot_pragma_msstruct);
1086 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001087 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001088 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1089 static_cast<uintptr_t>(Kind)));
1090 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1091 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001092}
1093
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001094// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1095// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001096static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001097 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001098 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001099
1100 if (IsOptions) {
1101 PP.Lex(Tok);
1102 if (Tok.isNot(tok::identifier) ||
1103 !Tok.getIdentifierInfo()->isStr("align")) {
1104 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1105 return;
1106 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001107 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001108
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001109 PP.Lex(Tok);
1110 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001111 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1112 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001113 return;
1114 }
1115
1116 PP.Lex(Tok);
1117 if (Tok.isNot(tok::identifier)) {
1118 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001119 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001120 return;
1121 }
1122
John McCallfaf5fb42010-08-26 23:41:50 +00001123 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001124 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001125 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001126 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001127 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001128 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001129 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001130 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001131 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001132 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001133 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001134 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001135 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001136 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001137 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001138 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1139 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001140 return;
1141 }
1142
David Majnemera8f2f1d2015-03-19 00:10:23 +00001143 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001144 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001145 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001146 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001147 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001148 return;
1149 }
1150
Eli Friedman68be1642012-10-04 02:36:51 +00001151 Token *Toks =
1152 (Token*) PP.getPreprocessorAllocator().Allocate(
1153 sizeof(Token) * 1, llvm::alignOf<Token>());
1154 new (Toks) Token();
1155 Toks[0].startToken();
1156 Toks[0].setKind(tok::annot_pragma_align);
1157 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001158 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001159 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1160 static_cast<uintptr_t>(Kind)));
1161 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1162 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001163}
1164
Douglas Gregorc7d65762010-09-09 22:45:38 +00001165void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1166 PragmaIntroducerKind Introducer,
1167 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001168 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001169}
1170
Douglas Gregorc7d65762010-09-09 22:45:38 +00001171void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1172 PragmaIntroducerKind Introducer,
1173 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001174 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001175}
1176
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001177// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001178void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1179 PragmaIntroducerKind Introducer,
1180 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001181 // FIXME: Should we be expanding macros here? My guess is no.
1182 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001183
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001184 // Lex the left '('.
1185 Token Tok;
1186 PP.Lex(Tok);
1187 if (Tok.isNot(tok::l_paren)) {
1188 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1189 return;
1190 }
Mike Stump11289f42009-09-09 15:08:12 +00001191
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001192 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001193 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001194 SourceLocation RParenLoc;
1195 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001196
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001197 while (true) {
1198 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001199
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001200 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001201 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001202 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001203 LexID = false;
1204 continue;
1205 }
1206
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001207 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001208 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1209 return;
1210 }
Mike Stump11289f42009-09-09 15:08:12 +00001211
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001212 // We are execting a ')' or a ','.
1213 if (Tok.is(tok::comma)) {
1214 LexID = true;
1215 continue;
1216 }
Mike Stump11289f42009-09-09 15:08:12 +00001217
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001218 if (Tok.is(tok::r_paren)) {
1219 RParenLoc = Tok.getLocation();
1220 break;
1221 }
Mike Stump11289f42009-09-09 15:08:12 +00001222
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001223 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001224 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001225 return;
1226 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001227
1228 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001229 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001230 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1231 "unused";
1232 return;
1233 }
1234
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001235 // Verify that we have a location for the right parenthesis.
1236 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001237 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001238
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001239 // For each identifier token, insert into the token stream a
1240 // annot_pragma_unused token followed by the identifier token.
1241 // This allows us to cache a "#pragma unused" that occurs inside an inline
1242 // C++ member function.
1243
Daniel Dunbar340cf242012-02-29 01:38:22 +00001244 Token *Toks =
1245 (Token*) PP.getPreprocessorAllocator().Allocate(
1246 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001247 for (unsigned i=0; i != Identifiers.size(); i++) {
1248 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1249 pragmaUnusedTok.startToken();
1250 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1251 pragmaUnusedTok.setLocation(UnusedLoc);
1252 idTok = Identifiers[i];
1253 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001254 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1255 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001256}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001257
1258// #pragma weak identifier
1259// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001260void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1261 PragmaIntroducerKind Introducer,
1262 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001263 SourceLocation WeakLoc = WeakTok.getLocation();
1264
1265 Token Tok;
1266 PP.Lex(Tok);
1267 if (Tok.isNot(tok::identifier)) {
1268 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1269 return;
1270 }
1271
Eli Friedman68be1642012-10-04 02:36:51 +00001272 Token WeakName = Tok;
1273 bool HasAlias = false;
1274 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001275
1276 PP.Lex(Tok);
1277 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001278 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001279 PP.Lex(Tok);
1280 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001281 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001282 << "weak";
1283 return;
1284 }
Eli Friedman68be1642012-10-04 02:36:51 +00001285 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001286 PP.Lex(Tok);
1287 }
1288
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001289 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001290 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1291 return;
1292 }
1293
Eli Friedman68be1642012-10-04 02:36:51 +00001294 if (HasAlias) {
1295 Token *Toks =
1296 (Token*) PP.getPreprocessorAllocator().Allocate(
1297 sizeof(Token) * 3, llvm::alignOf<Token>());
1298 Token &pragmaUnusedTok = Toks[0];
1299 pragmaUnusedTok.startToken();
1300 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1301 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001302 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001303 Toks[1] = WeakName;
1304 Toks[2] = AliasName;
1305 PP.EnterTokenStream(Toks, 3,
1306 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001307 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001308 Token *Toks =
1309 (Token*) PP.getPreprocessorAllocator().Allocate(
1310 sizeof(Token) * 2, llvm::alignOf<Token>());
1311 Token &pragmaUnusedTok = Toks[0];
1312 pragmaUnusedTok.startToken();
1313 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1314 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001315 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001316 Toks[1] = WeakName;
1317 PP.EnterTokenStream(Toks, 2,
1318 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001319 }
1320}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001321
David Chisnall0867d9c2012-02-18 16:12:34 +00001322// #pragma redefine_extname identifier identifier
1323void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1324 PragmaIntroducerKind Introducer,
1325 Token &RedefToken) {
1326 SourceLocation RedefLoc = RedefToken.getLocation();
1327
1328 Token Tok;
1329 PP.Lex(Tok);
1330 if (Tok.isNot(tok::identifier)) {
1331 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1332 "redefine_extname";
1333 return;
1334 }
1335
Eli Friedman68be1642012-10-04 02:36:51 +00001336 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001337 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001338
David Chisnall0867d9c2012-02-18 16:12:34 +00001339 if (Tok.isNot(tok::identifier)) {
1340 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1341 << "redefine_extname";
1342 return;
1343 }
Eli Friedman68be1642012-10-04 02:36:51 +00001344
1345 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001346 PP.Lex(Tok);
1347
1348 if (Tok.isNot(tok::eod)) {
1349 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1350 "redefine_extname";
1351 return;
1352 }
1353
Eli Friedman68be1642012-10-04 02:36:51 +00001354 Token *Toks =
1355 (Token*) PP.getPreprocessorAllocator().Allocate(
1356 sizeof(Token) * 3, llvm::alignOf<Token>());
1357 Token &pragmaRedefTok = Toks[0];
1358 pragmaRedefTok.startToken();
1359 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1360 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001361 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001362 Toks[1] = RedefName;
1363 Toks[2] = AliasName;
1364 PP.EnterTokenStream(Toks, 3,
1365 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001366}
1367
1368
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001369void
1370PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1371 PragmaIntroducerKind Introducer,
1372 Token &Tok) {
1373 tok::OnOffSwitch OOS;
1374 if (PP.LexOnOffSwitch(OOS))
1375 return;
1376
Eli Friedman68be1642012-10-04 02:36:51 +00001377 Token *Toks =
1378 (Token*) PP.getPreprocessorAllocator().Allocate(
1379 sizeof(Token) * 1, llvm::alignOf<Token>());
1380 new (Toks) Token();
1381 Toks[0].startToken();
1382 Toks[0].setKind(tok::annot_pragma_fp_contract);
1383 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001384 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001385 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1386 static_cast<uintptr_t>(OOS)));
1387 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1388 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001389}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001390
1391void
1392PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1393 PragmaIntroducerKind Introducer,
1394 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001395 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001396 if (Tok.isNot(tok::identifier)) {
1397 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1398 "OPENCL";
1399 return;
1400 }
1401 IdentifierInfo *ename = Tok.getIdentifierInfo();
1402 SourceLocation NameLoc = Tok.getLocation();
1403
1404 PP.Lex(Tok);
1405 if (Tok.isNot(tok::colon)) {
1406 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1407 return;
1408 }
1409
1410 PP.Lex(Tok);
1411 if (Tok.isNot(tok::identifier)) {
1412 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1413 return;
1414 }
1415 IdentifierInfo *op = Tok.getIdentifierInfo();
1416
1417 unsigned state;
1418 if (op->isStr("enable")) {
1419 state = 1;
1420 } else if (op->isStr("disable")) {
1421 state = 0;
1422 } else {
1423 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1424 return;
1425 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001426 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001427
Eli Friedman68be1642012-10-04 02:36:51 +00001428 PP.Lex(Tok);
1429 if (Tok.isNot(tok::eod)) {
1430 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1431 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001432 return;
1433 }
Eli Friedman68be1642012-10-04 02:36:51 +00001434
1435 OpenCLExtData data(ename, state);
1436 Token *Toks =
1437 (Token*) PP.getPreprocessorAllocator().Allocate(
1438 sizeof(Token) * 1, llvm::alignOf<Token>());
1439 new (Toks) Token();
1440 Toks[0].startToken();
1441 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1442 Toks[0].setLocation(NameLoc);
1443 Toks[0].setAnnotationValue(data.getOpaqueValue());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001444 Toks[0].setAnnotationEndLoc(StateLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001445 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1446 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001447
1448 if (PP.getPPCallbacks())
1449 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1450 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001451}
1452
Alexey Bataeva769e072013-03-22 06:34:35 +00001453/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1454///
1455void
1456PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1457 PragmaIntroducerKind Introducer,
1458 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001459 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1460 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001461 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001462 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1463 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001464 }
1465 PP.DiscardUntilEndOfDirective();
1466}
1467
1468/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1469///
1470void
1471PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1472 PragmaIntroducerKind Introducer,
1473 Token &FirstTok) {
1474 SmallVector<Token, 16> Pragma;
1475 Token Tok;
1476 Tok.startToken();
1477 Tok.setKind(tok::annot_pragma_openmp);
1478 Tok.setLocation(FirstTok.getLocation());
1479
1480 while (Tok.isNot(tok::eod)) {
1481 Pragma.push_back(Tok);
1482 PP.Lex(Tok);
1483 }
1484 SourceLocation EodLoc = Tok.getLocation();
1485 Tok.startToken();
1486 Tok.setKind(tok::annot_pragma_openmp_end);
1487 Tok.setLocation(EodLoc);
1488 Pragma.push_back(Tok);
1489
1490 Token *Toks = new Token[Pragma.size()];
1491 std::copy(Pragma.begin(), Pragma.end(), Toks);
1492 PP.EnterTokenStream(Toks, Pragma.size(),
Alexey Bataevc8956792015-05-05 09:53:25 +00001493 /*DisableMacroExpansion=*/false, /*OwnsTokens=*/true);
Alexey Bataeva769e072013-03-22 06:34:35 +00001494}
Reid Kleckner002562a2013-05-06 21:02:12 +00001495
David Majnemer4bb09802014-02-10 19:50:15 +00001496/// \brief Handle '#pragma pointers_to_members'
1497// The grammar for this pragma is as follows:
1498//
1499// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1500//
1501// #pragma pointers_to_members '(' 'best_case' ')'
1502// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1503// #pragma pointers_to_members '(' inheritance-model ')'
1504void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1505 PragmaIntroducerKind Introducer,
1506 Token &Tok) {
1507 SourceLocation PointersToMembersLoc = Tok.getLocation();
1508 PP.Lex(Tok);
1509 if (Tok.isNot(tok::l_paren)) {
1510 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1511 << "pointers_to_members";
1512 return;
1513 }
1514 PP.Lex(Tok);
1515 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1516 if (!Arg) {
1517 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1518 << "pointers_to_members";
1519 return;
1520 }
1521 PP.Lex(Tok);
1522
David Majnemer86c318f2014-02-11 21:05:00 +00001523 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001524 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001525 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001526 } else {
1527 if (Arg->isStr("full_generality")) {
1528 if (Tok.is(tok::comma)) {
1529 PP.Lex(Tok);
1530
1531 Arg = Tok.getIdentifierInfo();
1532 if (!Arg) {
1533 PP.Diag(Tok.getLocation(),
1534 diag::err_pragma_pointers_to_members_unknown_kind)
1535 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1536 return;
1537 }
1538 PP.Lex(Tok);
1539 } else if (Tok.is(tok::r_paren)) {
1540 // #pragma pointers_to_members(full_generality) implicitly specifies
1541 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001542 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001543 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001544 } else {
1545 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1546 << "full_generality";
1547 return;
1548 }
1549 }
1550
1551 if (Arg) {
1552 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001553 RepresentationMethod =
1554 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001555 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001556 RepresentationMethod =
1557 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001558 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001559 RepresentationMethod =
1560 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001561 } else {
1562 PP.Diag(Tok.getLocation(),
1563 diag::err_pragma_pointers_to_members_unknown_kind)
1564 << Arg << /*HasPointerDeclaration*/ 1;
1565 return;
1566 }
1567 }
1568 }
1569
1570 if (Tok.isNot(tok::r_paren)) {
1571 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1572 << (Arg ? Arg->getName() : "full_generality");
1573 return;
1574 }
1575
David Majnemera8f2f1d2015-03-19 00:10:23 +00001576 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001577 PP.Lex(Tok);
1578 if (Tok.isNot(tok::eod)) {
1579 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1580 << "pointers_to_members";
1581 return;
1582 }
1583
1584 Token AnnotTok;
1585 AnnotTok.startToken();
1586 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1587 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001588 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001589 AnnotTok.setAnnotationValue(
1590 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1591 PP.EnterToken(AnnotTok);
1592}
1593
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001594/// \brief Handle '#pragma vtordisp'
1595// The grammar for this pragma is as follows:
1596//
1597// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1598//
1599// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1600// #pragma vtordisp '(' 'pop' ')'
1601// #pragma vtordisp '(' ')'
1602void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1603 PragmaIntroducerKind Introducer,
1604 Token &Tok) {
1605 SourceLocation VtorDispLoc = Tok.getLocation();
1606 PP.Lex(Tok);
1607 if (Tok.isNot(tok::l_paren)) {
1608 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1609 return;
1610 }
1611 PP.Lex(Tok);
1612
1613 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1614 const IdentifierInfo *II = Tok.getIdentifierInfo();
1615 if (II) {
1616 if (II->isStr("push")) {
1617 // #pragma vtordisp(push, mode)
1618 PP.Lex(Tok);
1619 if (Tok.isNot(tok::comma)) {
1620 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1621 return;
1622 }
1623 PP.Lex(Tok);
1624 Kind = Sema::PVDK_Push;
1625 // not push, could be on/off
1626 } else if (II->isStr("pop")) {
1627 // #pragma vtordisp(pop)
1628 PP.Lex(Tok);
1629 Kind = Sema::PVDK_Pop;
1630 }
1631 // not push or pop, could be on/off
1632 } else {
1633 if (Tok.is(tok::r_paren)) {
1634 // #pragma vtordisp()
1635 Kind = Sema::PVDK_Reset;
1636 }
1637 }
1638
1639
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001640 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001641 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1642 const IdentifierInfo *II = Tok.getIdentifierInfo();
1643 if (II && II->isStr("off")) {
1644 PP.Lex(Tok);
1645 Value = 0;
1646 } else if (II && II->isStr("on")) {
1647 PP.Lex(Tok);
1648 Value = 1;
1649 } else if (Tok.is(tok::numeric_constant) &&
1650 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1651 if (Value > 2) {
1652 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1653 << 0 << 2 << "vtordisp";
1654 return;
1655 }
1656 } else {
1657 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1658 << "vtordisp";
1659 return;
1660 }
1661 }
1662
1663 // Finish the pragma: ')' $
1664 if (Tok.isNot(tok::r_paren)) {
1665 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1666 return;
1667 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001668 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001669 PP.Lex(Tok);
1670 if (Tok.isNot(tok::eod)) {
1671 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1672 << "vtordisp";
1673 return;
1674 }
1675
1676 // Enter the annotation.
1677 Token AnnotTok;
1678 AnnotTok.startToken();
1679 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1680 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001681 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001682 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1683 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001684 PP.EnterToken(AnnotTok);
1685}
1686
Warren Huntc3b18962014-04-08 22:30:47 +00001687/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1688/// an annotation token.
1689void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1690 PragmaIntroducerKind Introducer,
1691 Token &Tok) {
1692 Token EoF, AnnotTok;
1693 EoF.startToken();
1694 EoF.setKind(tok::eof);
1695 AnnotTok.startToken();
1696 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1697 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001698 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001699 SmallVector<Token, 8> TokenVector;
1700 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001701 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001702 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001703 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1704 }
Warren Huntc3b18962014-04-08 22:30:47 +00001705 // Add a sentinal EoF token to the end of the list.
1706 TokenVector.push_back(EoF);
1707 // We must allocate this array with new because EnterTokenStream is going to
1708 // delete it later.
1709 Token *TokenArray = new Token[TokenVector.size()];
1710 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1711 auto Value = new (PP.getPreprocessorAllocator())
1712 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1713 AnnotTok.setAnnotationValue(Value);
1714 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001715}
1716
Aaron Ballman5d041be2013-06-04 02:07:14 +00001717/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1718///
1719/// The syntax is:
1720/// \code
1721/// #pragma detect_mismatch("name", "value")
1722/// \endcode
1723/// Where 'name' and 'value' are quoted strings. The values are embedded in
1724/// the object file and passed along to the linker. If the linker detects a
1725/// mismatch in the object file's values for the given name, a LNK2038 error
1726/// is emitted. See MSDN for more details.
1727void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1728 PragmaIntroducerKind Introducer,
1729 Token &Tok) {
1730 SourceLocation CommentLoc = Tok.getLocation();
1731 PP.Lex(Tok);
1732 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001733 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001734 return;
1735 }
1736
1737 // Read the name to embed, which must be a string literal.
1738 std::string NameString;
1739 if (!PP.LexStringLiteral(Tok, NameString,
1740 "pragma detect_mismatch",
1741 /*MacroExpansion=*/true))
1742 return;
1743
1744 // Read the comma followed by a second string literal.
1745 std::string ValueString;
1746 if (Tok.isNot(tok::comma)) {
1747 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1748 return;
1749 }
1750
1751 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1752 /*MacroExpansion=*/true))
1753 return;
1754
1755 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001756 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001757 return;
1758 }
1759 PP.Lex(Tok); // Eat the r_paren.
1760
1761 if (Tok.isNot(tok::eod)) {
1762 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1763 return;
1764 }
1765
Reid Kleckner71966c92014-02-20 23:37:45 +00001766 // If the pragma is lexically sound, notify any interested PPCallbacks.
1767 if (PP.getPPCallbacks())
1768 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1769 ValueString);
1770
Aaron Ballman5d041be2013-06-04 02:07:14 +00001771 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1772}
1773
Reid Kleckner002562a2013-05-06 21:02:12 +00001774/// \brief Handle the microsoft \#pragma comment extension.
1775///
1776/// The syntax is:
1777/// \code
1778/// #pragma comment(linker, "foo")
1779/// \endcode
1780/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1781/// "foo" is a string, which is fully macro expanded, and permits string
1782/// concatenation, embedded escape characters etc. See MSDN for more details.
1783void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1784 PragmaIntroducerKind Introducer,
1785 Token &Tok) {
1786 SourceLocation CommentLoc = Tok.getLocation();
1787 PP.Lex(Tok);
1788 if (Tok.isNot(tok::l_paren)) {
1789 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1790 return;
1791 }
1792
1793 // Read the identifier.
1794 PP.Lex(Tok);
1795 if (Tok.isNot(tok::identifier)) {
1796 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1797 return;
1798 }
1799
1800 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001801 IdentifierInfo *II = Tok.getIdentifierInfo();
1802 Sema::PragmaMSCommentKind Kind =
1803 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1804 .Case("linker", Sema::PCK_Linker)
1805 .Case("lib", Sema::PCK_Lib)
1806 .Case("compiler", Sema::PCK_Compiler)
1807 .Case("exestr", Sema::PCK_ExeStr)
1808 .Case("user", Sema::PCK_User)
1809 .Default(Sema::PCK_Unknown);
1810 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001811 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1812 return;
1813 }
1814
Yunzhong Gao99efc032015-03-23 20:41:42 +00001815 // On PS4, issue a warning about any pragma comments other than
1816 // #pragma comment lib.
1817 if (PP.getTargetInfo().getTriple().isPS4() && Kind != Sema::PCK_Lib) {
1818 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1819 << II->getName();
1820 return;
1821 }
1822
Reid Kleckner002562a2013-05-06 21:02:12 +00001823 // Read the optional string if present.
1824 PP.Lex(Tok);
1825 std::string ArgumentString;
1826 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1827 "pragma comment",
1828 /*MacroExpansion=*/true))
1829 return;
1830
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001831 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001832 // FIXME: If the kind is "compiler" warn if the string is present (it is
1833 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001834 // The MSDN docs say that "lib" and "linker" require a string and have a short
1835 // whitelist of linker options they support, but in practice MSVC doesn't
1836 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001837
1838 if (Tok.isNot(tok::r_paren)) {
1839 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1840 return;
1841 }
1842 PP.Lex(Tok); // eat the r_paren.
1843
1844 if (Tok.isNot(tok::eod)) {
1845 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1846 return;
1847 }
1848
Reid Kleckner71966c92014-02-20 23:37:45 +00001849 // If the pragma is lexically sound, notify any interested PPCallbacks.
1850 if (PP.getPPCallbacks())
1851 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1852
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001853 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001854}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001855
1856// #pragma clang optimize off
1857// #pragma clang optimize on
1858void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1859 PragmaIntroducerKind Introducer,
1860 Token &FirstToken) {
1861 Token Tok;
1862 PP.Lex(Tok);
1863 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001864 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001865 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001866 return;
1867 }
1868 if (Tok.isNot(tok::identifier)) {
1869 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1870 << PP.getSpelling(Tok);
1871 return;
1872 }
1873 const IdentifierInfo *II = Tok.getIdentifierInfo();
1874 // The only accepted values are 'on' or 'off'.
1875 bool IsOn = false;
1876 if (II->isStr("on")) {
1877 IsOn = true;
1878 } else if (!II->isStr("off")) {
1879 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1880 << PP.getSpelling(Tok);
1881 return;
1882 }
1883 PP.Lex(Tok);
1884
1885 if (Tok.isNot(tok::eod)) {
1886 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1887 << PP.getSpelling(Tok);
1888 return;
1889 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001890
1891 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1892}
1893
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001894/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001895static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1896 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001897 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001898 SmallVector<Token, 1> ValueList;
1899 int OpenParens = ValueInParens ? 1 : 0;
1900 // Read constant expression.
1901 while (Tok.isNot(tok::eod)) {
1902 if (Tok.is(tok::l_paren))
1903 OpenParens++;
1904 else if (Tok.is(tok::r_paren)) {
1905 OpenParens--;
1906 if (OpenParens == 0 && ValueInParens)
1907 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001908 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001909
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001910 ValueList.push_back(Tok);
1911 PP.Lex(Tok);
1912 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001913
1914 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001915 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001916 if (Tok.isNot(tok::r_paren)) {
1917 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1918 return true;
1919 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001920 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001921 }
1922
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001923 Token EOFTok;
1924 EOFTok.startToken();
1925 EOFTok.setKind(tok::eof);
1926 EOFTok.setLocation(Tok.getLocation());
1927 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1928
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001929 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001930
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001931 Info.PragmaName = PragmaName;
1932 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001933 return false;
1934}
1935
Eli Bendersky06a40422014-06-06 20:31:48 +00001936/// \brief Handle the \#pragma clang loop directive.
1937/// #pragma clang 'loop' loop-hints
1938///
1939/// loop-hints:
1940/// loop-hint loop-hints[opt]
1941///
1942/// loop-hint:
1943/// 'vectorize' '(' loop-hint-keyword ')'
1944/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001945/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001946/// 'vectorize_width' '(' loop-hint-value ')'
1947/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001948/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001949///
1950/// loop-hint-keyword:
1951/// 'enable'
1952/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00001953/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00001954///
Mark Heffernan450c2382014-07-23 17:31:31 +00001955/// unroll-hint-keyword:
1956/// 'full'
1957/// 'disable'
1958///
Eli Bendersky06a40422014-06-06 20:31:48 +00001959/// loop-hint-value:
1960/// constant-expression
1961///
1962/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1963/// try vectorizing the instructions of the loop it precedes. Specifying
1964/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1965/// interleaving multiple iterations of the loop it precedes. The width of the
1966/// vector instructions is specified by vectorize_width() and the number of
1967/// interleaved loop iterations is specified by interleave_count(). Specifying a
1968/// value of 1 effectively disables vectorization/interleaving, even if it is
1969/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1970/// only works on inner loops.
1971///
Eli Bendersky86483b32014-06-11 17:56:26 +00001972/// The unroll and unroll_count directives control the concatenation
Mark Heffernan450c2382014-07-23 17:31:31 +00001973/// unroller. Specifying unroll(full) instructs llvm to try to
Eli Bendersky86483b32014-06-11 17:56:26 +00001974/// unroll the loop completely, and unroll(disable) disables unrolling
1975/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1976/// try to unroll the loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001977void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1978 PragmaIntroducerKind Introducer,
1979 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001980 // Incoming token is "loop" from "#pragma clang loop".
1981 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001982 SmallVector<Token, 1> TokenList;
1983
1984 // Lex the optimization option and verify it is an identifier.
1985 PP.Lex(Tok);
1986 if (Tok.isNot(tok::identifier)) {
1987 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1988 << /*MissingOption=*/true << "";
1989 return;
1990 }
1991
1992 while (Tok.is(tok::identifier)) {
1993 Token Option = Tok;
1994 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1995
Eli Bendersky86483b32014-06-11 17:56:26 +00001996 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001997 .Case("vectorize", true)
1998 .Case("interleave", true)
1999 .Case("unroll", true)
2000 .Case("vectorize_width", true)
2001 .Case("interleave_count", true)
2002 .Case("unroll_count", true)
2003 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002004 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002005 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2006 << /*MissingOption=*/false << OptionInfo;
2007 return;
2008 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002009 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002010
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002011 // Read '('
2012 if (Tok.isNot(tok::l_paren)) {
2013 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002014 return;
2015 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002016 PP.Lex(Tok);
2017
2018 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2019 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2020 *Info))
2021 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002022
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002023 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002024 Token LoopHintTok;
2025 LoopHintTok.startToken();
2026 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002027 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002028 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002029 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2030 TokenList.push_back(LoopHintTok);
2031 }
2032
2033 if (Tok.isNot(tok::eod)) {
2034 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2035 << "clang loop";
2036 return;
2037 }
2038
2039 Token *TokenArray = new Token[TokenList.size()];
2040 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
2041
2042 PP.EnterTokenStream(TokenArray, TokenList.size(),
2043 /*DisableMacroExpansion=*/false,
2044 /*OwnsTokens=*/true);
2045}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002046
2047/// \brief Handle the loop unroll optimization pragmas.
2048/// #pragma unroll
2049/// #pragma unroll unroll-hint-value
2050/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002051/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002052///
2053/// unroll-hint-value:
2054/// constant-expression
2055///
Mark Heffernanc888e412014-07-24 18:09:38 +00002056/// Loop unrolling hints can be specified with '#pragma unroll' or
2057/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2058/// contained in parentheses. With no argument the directive instructs llvm to
2059/// try to unroll the loop completely. A positive integer argument can be
2060/// specified to indicate the number of times the loop should be unrolled. To
2061/// maximize compatibility with other compilers the unroll count argument can be
2062/// specified with or without parentheses. Specifying, '#pragma nounroll'
2063/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002064void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2065 PragmaIntroducerKind Introducer,
2066 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002067 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2068 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002069 Token PragmaName = Tok;
2070 PP.Lex(Tok);
2071 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2072 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002073 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002074 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002075 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002076 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2077 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2078 << "nounroll";
2079 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002080 } else {
2081 // Unroll pragma with an argument: "#pragma unroll N" or
2082 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002083 // Read '(' if it exists.
2084 bool ValueInParens = Tok.is(tok::l_paren);
2085 if (ValueInParens)
2086 PP.Lex(Tok);
2087
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002088 Token Option;
2089 Option.startToken();
2090 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002091 return;
2092
2093 // In CUDA, the argument to '#pragma unroll' should not be contained in
2094 // parentheses.
2095 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002096 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002097 diag::warn_pragma_unroll_cuda_value_in_parens);
2098
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002099 if (Tok.isNot(tok::eod)) {
2100 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2101 << "unroll";
2102 return;
2103 }
2104 }
2105
2106 // Generate the hint token.
2107 Token *TokenArray = new Token[1];
2108 TokenArray[0].startToken();
2109 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2110 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002111 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002112 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2113 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2114 /*OwnsTokens=*/true);
2115}