blob: 06c1a17bdd4e88a7aabf7aa8bac2e8111fe2fb30 [file] [log] [blame]
Daniel Dunbar921b9682008-10-04 19:21:03 +00001//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the language specific #pragma handlers.
11//
12//===----------------------------------------------------------------------===//
13
Warren Huntc3b18962014-04-08 22:30:47 +000014#include "RAIIObjectsForParser.h"
Hans Wennborg899ded92014-10-16 20:52:46 +000015#include "clang/AST/ASTContext.h"
Nico Weber66220292016-03-02 17:28:48 +000016#include "clang/Basic/PragmaKinds.h"
David Majnemerad2986e2014-08-14 06:35:08 +000017#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000018#include "clang/Lex/Preprocessor.h"
19#include "clang/Parse/ParseDiagnostic.h"
20#include "clang/Parse/Parser.h"
21#include "clang/Sema/LoopHint.h"
22#include "clang/Sema/Scope.h"
23#include "llvm/ADT/StringSwitch.h"
24using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000025
Reid Kleckner5b086462014-02-20 22:52:09 +000026namespace {
27
28struct PragmaAlignHandler : public PragmaHandler {
29 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Craig Topper2b07f022014-03-12 05:09:18 +000030 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
31 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000032};
33
34struct PragmaGCCVisibilityHandler : public PragmaHandler {
35 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000036 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
37 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000038};
39
40struct PragmaOptionsHandler : public PragmaHandler {
41 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000042 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
43 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000044};
45
46struct PragmaPackHandler : public PragmaHandler {
47 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000048 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
49 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000050};
51
52struct PragmaMSStructHandler : public PragmaHandler {
53 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000054 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
55 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000056};
57
58struct PragmaUnusedHandler : public PragmaHandler {
59 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000060 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
61 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000062};
63
64struct PragmaWeakHandler : public PragmaHandler {
65 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000066 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
67 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000068};
69
70struct PragmaRedefineExtnameHandler : public PragmaHandler {
71 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000072 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
73 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000074};
75
76struct PragmaOpenCLExtensionHandler : public PragmaHandler {
77 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000078 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
79 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000080};
81
82
83struct PragmaFPContractHandler : public PragmaHandler {
84 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000085 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
86 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000087};
88
89struct PragmaNoOpenMPHandler : public PragmaHandler {
90 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000091 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
92 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000093};
94
95struct PragmaOpenMPHandler : public PragmaHandler {
96 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000097 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
98 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000099};
100
101/// PragmaCommentHandler - "\#pragma comment ...".
102struct PragmaCommentHandler : public PragmaHandler {
103 PragmaCommentHandler(Sema &Actions)
104 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000105 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
106 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000107private:
108 Sema &Actions;
109};
110
111struct PragmaDetectMismatchHandler : public PragmaHandler {
112 PragmaDetectMismatchHandler(Sema &Actions)
113 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000114 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
115 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000116private:
117 Sema &Actions;
118};
119
120struct PragmaMSPointersToMembers : public PragmaHandler {
121 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000122 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
123 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000124};
125
126struct PragmaMSVtorDisp : public PragmaHandler {
127 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000128 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
129 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000130};
131
Warren Huntc3b18962014-04-08 22:30:47 +0000132struct PragmaMSPragma : public PragmaHandler {
133 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000134 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
135 Token &FirstToken) override;
136};
137
Dario Domizioli13a0a382014-05-23 12:13:25 +0000138/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
139struct PragmaOptimizeHandler : public PragmaHandler {
140 PragmaOptimizeHandler(Sema &S)
141 : PragmaHandler("optimize"), Actions(S) {}
142 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
143 Token &FirstToken) override;
144private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000145 Sema &Actions;
146};
147
148struct PragmaLoopHintHandler : public PragmaHandler {
149 PragmaLoopHintHandler() : PragmaHandler("loop") {}
150 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
151 Token &FirstToken) override;
152};
153
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000154struct PragmaUnrollHintHandler : public PragmaHandler {
155 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
156 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
157 Token &FirstToken) override;
158};
159
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000160struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
161 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
162};
163
Eli Bendersky06a40422014-06-06 20:31:48 +0000164} // end namespace
165
166void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000167 AlignHandler.reset(new PragmaAlignHandler());
168 PP.AddPragmaHandler(AlignHandler.get());
169
170 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
171 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
172
173 OptionsHandler.reset(new PragmaOptionsHandler());
174 PP.AddPragmaHandler(OptionsHandler.get());
175
176 PackHandler.reset(new PragmaPackHandler());
177 PP.AddPragmaHandler(PackHandler.get());
178
179 MSStructHandler.reset(new PragmaMSStructHandler());
180 PP.AddPragmaHandler(MSStructHandler.get());
181
182 UnusedHandler.reset(new PragmaUnusedHandler());
183 PP.AddPragmaHandler(UnusedHandler.get());
184
185 WeakHandler.reset(new PragmaWeakHandler());
186 PP.AddPragmaHandler(WeakHandler.get());
187
188 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
189 PP.AddPragmaHandler(RedefineExtnameHandler.get());
190
191 FPContractHandler.reset(new PragmaFPContractHandler());
192 PP.AddPragmaHandler("STDC", FPContractHandler.get());
193
194 if (getLangOpts().OpenCL) {
195 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
196 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
197
198 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
199 }
200 if (getLangOpts().OpenMP)
201 OpenMPHandler.reset(new PragmaOpenMPHandler());
202 else
203 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
204 PP.AddPragmaHandler(OpenMPHandler.get());
205
Yunzhong Gao99efc032015-03-23 20:41:42 +0000206 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000207 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
208 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000209 }
210
211 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000212 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
213 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
214 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
215 PP.AddPragmaHandler(MSPointersToMembers.get());
216 MSVtorDisp.reset(new PragmaMSVtorDisp());
217 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000218 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000219 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000220 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
221 PP.AddPragmaHandler(MSDataSeg.get());
222 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
223 PP.AddPragmaHandler(MSBSSSeg.get());
224 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
225 PP.AddPragmaHandler(MSConstSeg.get());
226 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
227 PP.AddPragmaHandler(MSCodeSeg.get());
228 MSSection.reset(new PragmaMSPragma("section"));
229 PP.AddPragmaHandler(MSSection.get());
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000230 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
231 PP.AddPragmaHandler(MSRuntimeChecks.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000232 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000233
234 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
235 PP.AddPragmaHandler("clang", OptimizeHandler.get());
236
237 LoopHintHandler.reset(new PragmaLoopHintHandler());
238 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000239
240 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
241 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000242
243 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
244 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000245}
246
247void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000248 // Remove the pragma handlers we installed.
249 PP.RemovePragmaHandler(AlignHandler.get());
250 AlignHandler.reset();
251 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
252 GCCVisibilityHandler.reset();
253 PP.RemovePragmaHandler(OptionsHandler.get());
254 OptionsHandler.reset();
255 PP.RemovePragmaHandler(PackHandler.get());
256 PackHandler.reset();
257 PP.RemovePragmaHandler(MSStructHandler.get());
258 MSStructHandler.reset();
259 PP.RemovePragmaHandler(UnusedHandler.get());
260 UnusedHandler.reset();
261 PP.RemovePragmaHandler(WeakHandler.get());
262 WeakHandler.reset();
263 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
264 RedefineExtnameHandler.reset();
265
266 if (getLangOpts().OpenCL) {
267 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
268 OpenCLExtensionHandler.reset();
269 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
270 }
271 PP.RemovePragmaHandler(OpenMPHandler.get());
272 OpenMPHandler.reset();
273
Yunzhong Gao99efc032015-03-23 20:41:42 +0000274 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000275 PP.RemovePragmaHandler(MSCommentHandler.get());
276 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000277 }
278
279 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000280 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
281 MSDetectMismatchHandler.reset();
282 PP.RemovePragmaHandler(MSPointersToMembers.get());
283 MSPointersToMembers.reset();
284 PP.RemovePragmaHandler(MSVtorDisp.get());
285 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000286 PP.RemovePragmaHandler(MSInitSeg.get());
287 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000288 PP.RemovePragmaHandler(MSDataSeg.get());
289 MSDataSeg.reset();
290 PP.RemovePragmaHandler(MSBSSSeg.get());
291 MSBSSSeg.reset();
292 PP.RemovePragmaHandler(MSConstSeg.get());
293 MSConstSeg.reset();
294 PP.RemovePragmaHandler(MSCodeSeg.get());
295 MSCodeSeg.reset();
296 PP.RemovePragmaHandler(MSSection.get());
297 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000298 PP.RemovePragmaHandler(MSRuntimeChecks.get());
299 MSRuntimeChecks.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000300 }
301
302 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
303 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000304
305 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
306 OptimizeHandler.reset();
307
308 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
309 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000310
311 PP.RemovePragmaHandler(UnrollHintHandler.get());
312 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000313
314 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
315 NoUnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000316}
317
318/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000319///
320/// Each annot_pragma_unused is followed by the argument token so e.g.
321/// "#pragma unused(x,y)" becomes:
322/// annot_pragma_unused 'x' annot_pragma_unused 'y'
323void Parser::HandlePragmaUnused() {
324 assert(Tok.is(tok::annot_pragma_unused));
325 SourceLocation UnusedLoc = ConsumeToken();
326 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
327 ConsumeToken(); // The argument token.
328}
Eli Friedman570024a2010-08-05 06:57:20 +0000329
Rafael Espindola273fd772012-01-26 02:02:57 +0000330void Parser::HandlePragmaVisibility() {
331 assert(Tok.is(tok::annot_pragma_vis));
332 const IdentifierInfo *VisType =
333 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
334 SourceLocation VisLoc = ConsumeToken();
335 Actions.ActOnPragmaVisibility(VisType, VisLoc);
336}
337
Benjamin Kramere003ca22015-10-28 13:54:16 +0000338namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000339struct PragmaPackInfo {
340 Sema::PragmaPackKind Kind;
341 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000342 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000343 SourceLocation LParenLoc;
344 SourceLocation RParenLoc;
345};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000346} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000347
348void Parser::HandlePragmaPack() {
349 assert(Tok.is(tok::annot_pragma_pack));
350 PragmaPackInfo *Info =
351 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
352 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000353 ExprResult Alignment;
354 if (Info->Alignment.is(tok::numeric_constant)) {
355 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
356 if (Alignment.isInvalid())
357 return;
358 }
359 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000360 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000361}
362
Eli Friedman68be1642012-10-04 02:36:51 +0000363void Parser::HandlePragmaMSStruct() {
364 assert(Tok.is(tok::annot_pragma_msstruct));
365 Sema::PragmaMSStructKind Kind =
366 static_cast<Sema::PragmaMSStructKind>(
367 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
368 Actions.ActOnPragmaMSStruct(Kind);
369 ConsumeToken(); // The annotation token.
370}
371
372void Parser::HandlePragmaAlign() {
373 assert(Tok.is(tok::annot_pragma_align));
374 Sema::PragmaOptionsAlignKind Kind =
375 static_cast<Sema::PragmaOptionsAlignKind>(
376 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
377 SourceLocation PragmaLoc = ConsumeToken();
378 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
379}
380
Richard Smithba3a4f92016-01-12 21:59:26 +0000381void Parser::HandlePragmaDump() {
382 assert(Tok.is(tok::annot_pragma_dump));
383 IdentifierInfo *II =
384 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
385 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
386 ConsumeToken();
387}
388
Eli Friedman68be1642012-10-04 02:36:51 +0000389void Parser::HandlePragmaWeak() {
390 assert(Tok.is(tok::annot_pragma_weak));
391 SourceLocation PragmaLoc = ConsumeToken();
392 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
393 Tok.getLocation());
394 ConsumeToken(); // The weak name.
395}
396
397void Parser::HandlePragmaWeakAlias() {
398 assert(Tok.is(tok::annot_pragma_weakalias));
399 SourceLocation PragmaLoc = ConsumeToken();
400 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
401 SourceLocation WeakNameLoc = Tok.getLocation();
402 ConsumeToken();
403 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
404 SourceLocation AliasNameLoc = Tok.getLocation();
405 ConsumeToken();
406 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
407 WeakNameLoc, AliasNameLoc);
408
409}
410
411void Parser::HandlePragmaRedefineExtname() {
412 assert(Tok.is(tok::annot_pragma_redefine_extname));
413 SourceLocation RedefLoc = ConsumeToken();
414 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
415 SourceLocation RedefNameLoc = Tok.getLocation();
416 ConsumeToken();
417 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
418 SourceLocation AliasNameLoc = Tok.getLocation();
419 ConsumeToken();
420 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
421 RedefNameLoc, AliasNameLoc);
422}
423
424void Parser::HandlePragmaFPContract() {
425 assert(Tok.is(tok::annot_pragma_fp_contract));
426 tok::OnOffSwitch OOS =
427 static_cast<tok::OnOffSwitch>(
428 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
429 Actions.ActOnPragmaFPContract(OOS);
430 ConsumeToken(); // The annotation token.
431}
432
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000433StmtResult Parser::HandlePragmaCaptured()
434{
435 assert(Tok.is(tok::annot_pragma_captured));
436 ConsumeToken();
437
438 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000439 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000440 return StmtError();
441 }
442
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000443 SourceLocation Loc = Tok.getLocation();
444
445 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000446 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
447 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000448
449 StmtResult R = ParseCompoundStatement();
450 CapturedRegionScope.Exit();
451
452 if (R.isInvalid()) {
453 Actions.ActOnCapturedRegionError();
454 return StmtError();
455 }
456
457 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000458}
459
Eli Friedman68be1642012-10-04 02:36:51 +0000460namespace {
461 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
462}
463
464void Parser::HandlePragmaOpenCLExtension() {
465 assert(Tok.is(tok::annot_pragma_opencl_extension));
466 OpenCLExtData data =
467 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
468 unsigned state = data.getInt();
469 IdentifierInfo *ename = data.getPointer();
470 SourceLocation NameLoc = Tok.getLocation();
471 ConsumeToken(); // The annotation token.
472
473 OpenCLOptions &f = Actions.getOpenCLOptions();
474 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
475 // overriding all previously issued extension directives, but only if the
476 // behavior is set to disable."
477 if (state == 0 && ename->isStr("all")) {
478#define OPENCLEXT(nm) f.nm = 0;
479#include "clang/Basic/OpenCLExtensions.def"
480 }
481#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
482#include "clang/Basic/OpenCLExtensions.def"
483 else {
484 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
485 return;
486 }
487}
488
David Majnemer4bb09802014-02-10 19:50:15 +0000489void Parser::HandlePragmaMSPointersToMembers() {
490 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000491 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
492 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000493 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
494 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
495 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
496}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000497
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000498void Parser::HandlePragmaMSVtorDisp() {
499 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
500 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
501 Sema::PragmaVtorDispKind Kind =
502 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
503 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
504 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
505 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
506}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000507
Warren Huntc3b18962014-04-08 22:30:47 +0000508void Parser::HandlePragmaMSPragma() {
509 assert(Tok.is(tok::annot_pragma_ms_pragma));
510 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000511 auto TheTokens =
512 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
513 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Warren Huntc3b18962014-04-08 22:30:47 +0000514 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
515 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000516 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000517 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000518
Warren Huntc3b18962014-04-08 22:30:47 +0000519 // Figure out which #pragma we're dealing with. The switch has no default
520 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000521 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000522 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
523 .Case("data_seg", &Parser::HandlePragmaMSSegment)
524 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
525 .Case("const_seg", &Parser::HandlePragmaMSSegment)
526 .Case("code_seg", &Parser::HandlePragmaMSSegment)
527 .Case("section", &Parser::HandlePragmaMSSection)
528 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000529
530 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
531 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
532 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000533 while (Tok.isNot(tok::eof))
534 PP.Lex(Tok);
535 PP.Lex(Tok);
536 }
537}
538
Reid Kleckner722b1df2014-07-18 00:13:16 +0000539bool Parser::HandlePragmaMSSection(StringRef PragmaName,
540 SourceLocation PragmaLocation) {
541 if (Tok.isNot(tok::l_paren)) {
542 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
543 return false;
544 }
Warren Huntc3b18962014-04-08 22:30:47 +0000545 PP.Lex(Tok); // (
546 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000547 if (Tok.isNot(tok::string_literal)) {
548 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
549 << PragmaName;
550 return false;
551 }
552 ExprResult StringResult = ParseStringLiteralExpression();
553 if (StringResult.isInvalid())
554 return false; // Already diagnosed.
555 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
556 if (SegmentName->getCharByteWidth() != 1) {
557 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
558 << PragmaName;
559 return false;
560 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000561 int SectionFlags = ASTContext::PSF_Read;
562 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000563 while (Tok.is(tok::comma)) {
564 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000565 // Ignore "long" and "short".
566 // They are undocumented, but widely used, section attributes which appear
567 // to do nothing.
568 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
569 PP.Lex(Tok); // long/short
570 continue;
571 }
572
Reid Kleckner722b1df2014-07-18 00:13:16 +0000573 if (!Tok.isAnyIdentifier()) {
574 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
575 << PragmaName;
576 return false;
577 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000578 ASTContext::PragmaSectionFlag Flag =
579 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000580 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000581 .Case("read", ASTContext::PSF_Read)
582 .Case("write", ASTContext::PSF_Write)
583 .Case("execute", ASTContext::PSF_Execute)
584 .Case("shared", ASTContext::PSF_Invalid)
585 .Case("nopage", ASTContext::PSF_Invalid)
586 .Case("nocache", ASTContext::PSF_Invalid)
587 .Case("discard", ASTContext::PSF_Invalid)
588 .Case("remove", ASTContext::PSF_Invalid)
589 .Default(ASTContext::PSF_None);
590 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
591 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000592 ? diag::warn_pragma_invalid_specific_action
593 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000594 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000595 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000596 }
597 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000598 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000599 PP.Lex(Tok); // Identifier
600 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000601 // If no section attributes are specified, the section will be marked as
602 // read/write.
603 if (SectionFlagsAreDefault)
604 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000605 if (Tok.isNot(tok::r_paren)) {
606 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
607 return false;
608 }
Warren Huntc3b18962014-04-08 22:30:47 +0000609 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000610 if (Tok.isNot(tok::eof)) {
611 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
612 << PragmaName;
613 return false;
614 }
Warren Huntc3b18962014-04-08 22:30:47 +0000615 PP.Lex(Tok); // eof
616 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000617 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000618}
619
Reid Kleckner722b1df2014-07-18 00:13:16 +0000620bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
621 SourceLocation PragmaLocation) {
622 if (Tok.isNot(tok::l_paren)) {
623 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
624 return false;
625 }
Warren Huntc3b18962014-04-08 22:30:47 +0000626 PP.Lex(Tok); // (
627 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000628 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000629 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000630 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000631 if (PushPop == "push")
632 Action = Sema::PSK_Push;
633 else if (PushPop == "pop")
634 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000635 else {
636 PP.Diag(PragmaLocation,
637 diag::warn_pragma_expected_section_push_pop_or_name)
638 << PragmaName;
639 return false;
640 }
Warren Huntc3b18962014-04-08 22:30:47 +0000641 if (Action != Sema::PSK_Reset) {
642 PP.Lex(Tok); // push | pop
643 if (Tok.is(tok::comma)) {
644 PP.Lex(Tok); // ,
645 // If we've got a comma, we either need a label or a string.
646 if (Tok.isAnyIdentifier()) {
647 SlotLabel = Tok.getIdentifierInfo()->getName();
648 PP.Lex(Tok); // identifier
649 if (Tok.is(tok::comma))
650 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000651 else if (Tok.isNot(tok::r_paren)) {
652 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
653 << PragmaName;
654 return false;
655 }
Warren Huntc3b18962014-04-08 22:30:47 +0000656 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000657 } else if (Tok.isNot(tok::r_paren)) {
658 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
659 return false;
660 }
Warren Huntc3b18962014-04-08 22:30:47 +0000661 }
662 }
663 // Grab the string literal for our section name.
664 StringLiteral *SegmentName = nullptr;
665 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000666 if (Tok.isNot(tok::string_literal)) {
667 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000668 diag::warn_pragma_expected_section_name :
669 diag::warn_pragma_expected_section_label_or_name :
670 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000671 PP.Diag(PragmaLocation, DiagID) << PragmaName;
672 return false;
673 }
674 ExprResult StringResult = ParseStringLiteralExpression();
675 if (StringResult.isInvalid())
676 return false; // Already diagnosed.
677 SegmentName = cast<StringLiteral>(StringResult.get());
678 if (SegmentName->getCharByteWidth() != 1) {
679 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
680 << PragmaName;
681 return false;
682 }
Warren Huntc3b18962014-04-08 22:30:47 +0000683 // Setting section "" has no effect
684 if (SegmentName->getLength())
685 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
686 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000687 if (Tok.isNot(tok::r_paren)) {
688 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
689 return false;
690 }
Warren Huntc3b18962014-04-08 22:30:47 +0000691 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000692 if (Tok.isNot(tok::eof)) {
693 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
694 << PragmaName;
695 return false;
696 }
Warren Huntc3b18962014-04-08 22:30:47 +0000697 PP.Lex(Tok); // eof
698 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
699 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000700 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000701}
702
Reid Kleckner1a711b12014-07-22 00:53:05 +0000703// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000704bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
705 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000706 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
707 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
708 return false;
709 }
710
Reid Kleckner1a711b12014-07-22 00:53:05 +0000711 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
712 PragmaName))
713 return false;
714
715 // Parse either the known section names or the string section name.
716 StringLiteral *SegmentName = nullptr;
717 if (Tok.isAnyIdentifier()) {
718 auto *II = Tok.getIdentifierInfo();
719 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
720 .Case("compiler", "\".CRT$XCC\"")
721 .Case("lib", "\".CRT$XCL\"")
722 .Case("user", "\".CRT$XCU\"")
723 .Default("");
724
725 if (!Section.empty()) {
726 // Pretend the user wrote the appropriate string literal here.
727 Token Toks[1];
728 Toks[0].startToken();
729 Toks[0].setKind(tok::string_literal);
730 Toks[0].setLocation(Tok.getLocation());
731 Toks[0].setLiteralData(Section.data());
732 Toks[0].setLength(Section.size());
733 SegmentName =
734 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
735 PP.Lex(Tok);
736 }
737 } else if (Tok.is(tok::string_literal)) {
738 ExprResult StringResult = ParseStringLiteralExpression();
739 if (StringResult.isInvalid())
740 return false;
741 SegmentName = cast<StringLiteral>(StringResult.get());
742 if (SegmentName->getCharByteWidth() != 1) {
743 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
744 << PragmaName;
745 return false;
746 }
747 // FIXME: Add support for the '[, func-name]' part of the pragma.
748 }
749
750 if (!SegmentName) {
751 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
752 return false;
753 }
754
755 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
756 PragmaName) ||
757 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
758 PragmaName))
759 return false;
760
761 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
762 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000763}
764
Benjamin Kramere003ca22015-10-28 13:54:16 +0000765namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000766struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000767 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000768 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000769 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000770};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000771} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +0000772
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000773static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
774 std::string PragmaString;
775 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
776 PragmaString = "clang loop ";
777 PragmaString += Option.getIdentifierInfo()->getName();
778 } else {
779 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
780 "Unexpected pragma name");
781 PragmaString = "unroll";
782 }
783 return PragmaString;
784}
785
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000786bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000787 assert(Tok.is(tok::annot_pragma_loop_hint));
788 PragmaLoopHintInfo *Info =
789 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
790
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000791 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
792 Hint.PragmaNameLoc = IdentifierLoc::create(
793 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000794
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000795 // It is possible that the loop hint has no option identifier, such as
796 // #pragma unroll(4).
797 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
798 ? Info->Option.getIdentifierInfo()
799 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000800 Hint.OptionLoc = IdentifierLoc::create(
801 Actions.Context, Info->Option.getLocation(), OptionInfo);
802
David Blaikie2eabcc92016-02-09 18:52:09 +0000803 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000804
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000805 // Return a valid hint if pragma unroll or nounroll were specified
806 // without an argument.
807 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
808 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Blaikie2eabcc92016-02-09 18:52:09 +0000809 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000810 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000811 Hint.Range = Info->PragmaName.getLocation();
812 return true;
813 }
814
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000815 // The constant expression is always followed by an eof token, which increases
816 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +0000817 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000818 "PragmaLoopHintInfo::Toks must contain at least one token.");
819
820 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000821 bool OptionUnroll = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000822 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000823 if (OptionInfo) { // Pragma Unroll does not specify an option.
824 OptionUnroll = OptionInfo->isStr("unroll");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000825 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
826 .Case("vectorize", true)
827 .Case("interleave", true)
828 .Case("unroll", true)
829 .Default(false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000830 }
831
832 // Verify loop hint has an argument.
833 if (Toks[0].is(tok::eof)) {
834 ConsumeToken(); // The annotation token.
835 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Tyler Nowicki24853c12015-06-08 23:13:43 +0000836 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000837 return false;
838 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000839
840 // Validate the argument.
841 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000842 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000843 SourceLocation StateLoc = Toks[0].getLocation();
844 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000845 if (!StateInfo ||
Mark Heffernan397a98d2015-08-10 17:29:39 +0000846 (!StateInfo->isStr("enable") && !StateInfo->isStr("disable") &&
847 ((OptionUnroll && !StateInfo->isStr("full")) ||
848 (!OptionUnroll && !StateInfo->isStr("assume_safety"))))) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000849 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
850 << /*FullKeyword=*/OptionUnroll;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000851 return false;
852 }
David Blaikie2eabcc92016-02-09 18:52:09 +0000853 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000854 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
855 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000856 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
857 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000858 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000859 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000860 ConsumeToken(); // The annotation token.
861
862 ExprResult R = ParseConstantExpression();
863
864 // Tokens following an error in an ill-formed constant expression will
865 // remain in the token stream and must be removed.
866 if (Tok.isNot(tok::eof)) {
867 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
868 << PragmaLoopHintString(Info->PragmaName, Info->Option);
869 while (Tok.isNot(tok::eof))
870 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000871 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000872
873 ConsumeToken(); // Consume the constant expression eof terminator.
874
875 if (R.isInvalid() ||
876 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
877 return false;
878
879 // Argument is a constant expression with an integer type.
880 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000881 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000882
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000883 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +0000884 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000885 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000886}
887
888// #pragma GCC visibility comes in two variants:
889// 'push' '(' [visibility] ')'
890// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000891void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
892 PragmaIntroducerKind Introducer,
893 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000894 SourceLocation VisLoc = VisTok.getLocation();
895
896 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000897 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000898
899 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
900
Eli Friedman570024a2010-08-05 06:57:20 +0000901 const IdentifierInfo *VisType;
902 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000903 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000904 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000905 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000906 if (Tok.isNot(tok::l_paren)) {
907 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
908 << "visibility";
909 return;
910 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000911 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000912 VisType = Tok.getIdentifierInfo();
913 if (!VisType) {
914 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
915 << "visibility";
916 return;
917 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000918 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000919 if (Tok.isNot(tok::r_paren)) {
920 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
921 << "visibility";
922 return;
923 }
924 } else {
925 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
926 << "visibility";
927 return;
928 }
David Majnemera8f2f1d2015-03-19 00:10:23 +0000929 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000930 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000931 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000932 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
933 << "visibility";
934 return;
935 }
936
David Blaikie2eabcc92016-02-09 18:52:09 +0000937 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +0000938 Toks[0].startToken();
939 Toks[0].setKind(tok::annot_pragma_vis);
940 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000941 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +0000942 Toks[0].setAnnotationValue(
943 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +0000944 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000945}
946
Daniel Dunbar921b9682008-10-04 19:21:03 +0000947// #pragma pack(...) comes in the following delicious flavors:
948// pack '(' [integer] ')'
949// pack '(' 'show' ')'
950// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000951void PragmaPackHandler::HandlePragma(Preprocessor &PP,
952 PragmaIntroducerKind Introducer,
953 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000954 SourceLocation PackLoc = PackTok.getLocation();
955
956 Token Tok;
957 PP.Lex(Tok);
958 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000959 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000960 return;
961 }
962
John McCallfaf5fb42010-08-26 23:41:50 +0000963 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000964 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000965 Token Alignment;
966 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000967 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000968 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000969 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000970 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000971
972 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000973
974 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
975 // the push/pop stack.
976 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000977 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000978 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000979 } else if (Tok.is(tok::identifier)) {
980 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000981 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000982 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000983 PP.Lex(Tok);
984 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000985 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000986 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000987 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000988 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000989 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000990 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000991 return;
Mike Stump11289f42009-09-09 15:08:12 +0000992 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000993 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000994
Daniel Dunbar921b9682008-10-04 19:21:03 +0000995 if (Tok.is(tok::comma)) {
996 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000997
Daniel Dunbar921b9682008-10-04 19:21:03 +0000998 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000999 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001000
1001 PP.Lex(Tok);
1002 } else if (Tok.is(tok::identifier)) {
1003 Name = Tok.getIdentifierInfo();
1004 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001005
Daniel Dunbar921b9682008-10-04 19:21:03 +00001006 if (Tok.is(tok::comma)) {
1007 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001008
Daniel Dunbar921b9682008-10-04 19:21:03 +00001009 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001010 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001011 return;
1012 }
Mike Stump11289f42009-09-09 15:08:12 +00001013
Eli Friedman68be1642012-10-04 02:36:51 +00001014 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001015
1016 PP.Lex(Tok);
1017 }
1018 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001019 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001020 return;
1021 }
1022 }
1023 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001024 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001025 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1026 // the push/pop stack.
1027 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1028 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001029 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001030
1031 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001032 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001033 return;
1034 }
1035
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001036 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001037 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001038 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001039 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1040 return;
1041 }
1042
David Blaikie2eabcc92016-02-09 18:52:09 +00001043 PragmaPackInfo *Info =
1044 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001045 Info->Kind = Kind;
1046 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +00001047 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001048 Info->LParenLoc = LParenLoc;
1049 Info->RParenLoc = RParenLoc;
1050
David Blaikie2eabcc92016-02-09 18:52:09 +00001051 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1052 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001053 Toks[0].startToken();
1054 Toks[0].setKind(tok::annot_pragma_pack);
1055 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001056 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001057 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001058 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001059}
1060
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001061// #pragma ms_struct on
1062// #pragma ms_struct off
1063void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1064 PragmaIntroducerKind Introducer,
1065 Token &MSStructTok) {
1066 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
1067
1068 Token Tok;
1069 PP.Lex(Tok);
1070 if (Tok.isNot(tok::identifier)) {
1071 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1072 return;
1073 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001074 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001075 const IdentifierInfo *II = Tok.getIdentifierInfo();
1076 if (II->isStr("on")) {
1077 Kind = Sema::PMSST_ON;
1078 PP.Lex(Tok);
1079 }
1080 else if (II->isStr("off") || II->isStr("reset"))
1081 PP.Lex(Tok);
1082 else {
1083 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1084 return;
1085 }
1086
1087 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001088 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1089 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001090 return;
1091 }
Eli Friedman68be1642012-10-04 02:36:51 +00001092
David Blaikie2eabcc92016-02-09 18:52:09 +00001093 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1094 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001095 Toks[0].startToken();
1096 Toks[0].setKind(tok::annot_pragma_msstruct);
1097 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001098 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001099 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1100 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001101 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001102}
1103
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001104// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1105// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001106static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001107 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001108 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001109
1110 if (IsOptions) {
1111 PP.Lex(Tok);
1112 if (Tok.isNot(tok::identifier) ||
1113 !Tok.getIdentifierInfo()->isStr("align")) {
1114 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1115 return;
1116 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001117 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001118
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001119 PP.Lex(Tok);
1120 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001121 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1122 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001123 return;
1124 }
1125
1126 PP.Lex(Tok);
1127 if (Tok.isNot(tok::identifier)) {
1128 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001129 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001130 return;
1131 }
1132
John McCallfaf5fb42010-08-26 23:41:50 +00001133 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001134 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001135 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001136 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001137 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001138 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001139 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001140 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001141 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001142 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001143 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001144 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001145 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001146 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001147 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001148 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1149 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001150 return;
1151 }
1152
David Majnemera8f2f1d2015-03-19 00:10:23 +00001153 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001154 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001155 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001156 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001157 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001158 return;
1159 }
1160
David Blaikie2eabcc92016-02-09 18:52:09 +00001161 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1162 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001163 Toks[0].startToken();
1164 Toks[0].setKind(tok::annot_pragma_align);
1165 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001166 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001167 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1168 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001169 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001170}
1171
Douglas Gregorc7d65762010-09-09 22:45:38 +00001172void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1173 PragmaIntroducerKind Introducer,
1174 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001175 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001176}
1177
Douglas Gregorc7d65762010-09-09 22:45:38 +00001178void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1179 PragmaIntroducerKind Introducer,
1180 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001181 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001182}
1183
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001184// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001185void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1186 PragmaIntroducerKind Introducer,
1187 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001188 // FIXME: Should we be expanding macros here? My guess is no.
1189 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001190
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001191 // Lex the left '('.
1192 Token Tok;
1193 PP.Lex(Tok);
1194 if (Tok.isNot(tok::l_paren)) {
1195 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1196 return;
1197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001199 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001200 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001201 SourceLocation RParenLoc;
1202 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001204 while (true) {
1205 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001206
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001207 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001208 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001209 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001210 LexID = false;
1211 continue;
1212 }
1213
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001214 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001215 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1216 return;
1217 }
Mike Stump11289f42009-09-09 15:08:12 +00001218
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001219 // We are execting a ')' or a ','.
1220 if (Tok.is(tok::comma)) {
1221 LexID = true;
1222 continue;
1223 }
Mike Stump11289f42009-09-09 15:08:12 +00001224
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001225 if (Tok.is(tok::r_paren)) {
1226 RParenLoc = Tok.getLocation();
1227 break;
1228 }
Mike Stump11289f42009-09-09 15:08:12 +00001229
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001230 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001231 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001232 return;
1233 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001234
1235 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001236 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001237 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1238 "unused";
1239 return;
1240 }
1241
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001242 // Verify that we have a location for the right parenthesis.
1243 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001244 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001245
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001246 // For each identifier token, insert into the token stream a
1247 // annot_pragma_unused token followed by the identifier token.
1248 // This allows us to cache a "#pragma unused" that occurs inside an inline
1249 // C++ member function.
1250
David Blaikie2eabcc92016-02-09 18:52:09 +00001251 MutableArrayRef<Token> Toks(
1252 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1253 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001254 for (unsigned i=0; i != Identifiers.size(); i++) {
1255 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1256 pragmaUnusedTok.startToken();
1257 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1258 pragmaUnusedTok.setLocation(UnusedLoc);
1259 idTok = Identifiers[i];
1260 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001261 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001262}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001263
1264// #pragma weak identifier
1265// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001266void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1267 PragmaIntroducerKind Introducer,
1268 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001269 SourceLocation WeakLoc = WeakTok.getLocation();
1270
1271 Token Tok;
1272 PP.Lex(Tok);
1273 if (Tok.isNot(tok::identifier)) {
1274 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1275 return;
1276 }
1277
Eli Friedman68be1642012-10-04 02:36:51 +00001278 Token WeakName = Tok;
1279 bool HasAlias = false;
1280 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001281
1282 PP.Lex(Tok);
1283 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001284 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001285 PP.Lex(Tok);
1286 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001287 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001288 << "weak";
1289 return;
1290 }
Eli Friedman68be1642012-10-04 02:36:51 +00001291 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001292 PP.Lex(Tok);
1293 }
1294
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001295 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001296 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1297 return;
1298 }
1299
Eli Friedman68be1642012-10-04 02:36:51 +00001300 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00001301 MutableArrayRef<Token> Toks(
1302 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001303 Token &pragmaUnusedTok = Toks[0];
1304 pragmaUnusedTok.startToken();
1305 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1306 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001307 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001308 Toks[1] = WeakName;
1309 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001310 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001311 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00001312 MutableArrayRef<Token> Toks(
1313 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00001314 Token &pragmaUnusedTok = Toks[0];
1315 pragmaUnusedTok.startToken();
1316 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1317 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001318 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001319 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001320 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001321 }
1322}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001323
David Chisnall0867d9c2012-02-18 16:12:34 +00001324// #pragma redefine_extname identifier identifier
1325void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1326 PragmaIntroducerKind Introducer,
1327 Token &RedefToken) {
1328 SourceLocation RedefLoc = RedefToken.getLocation();
1329
1330 Token Tok;
1331 PP.Lex(Tok);
1332 if (Tok.isNot(tok::identifier)) {
1333 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1334 "redefine_extname";
1335 return;
1336 }
1337
Eli Friedman68be1642012-10-04 02:36:51 +00001338 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001339 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001340
David Chisnall0867d9c2012-02-18 16:12:34 +00001341 if (Tok.isNot(tok::identifier)) {
1342 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1343 << "redefine_extname";
1344 return;
1345 }
Eli Friedman68be1642012-10-04 02:36:51 +00001346
1347 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001348 PP.Lex(Tok);
1349
1350 if (Tok.isNot(tok::eod)) {
1351 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1352 "redefine_extname";
1353 return;
1354 }
1355
David Blaikie2eabcc92016-02-09 18:52:09 +00001356 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1357 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001358 Token &pragmaRedefTok = Toks[0];
1359 pragmaRedefTok.startToken();
1360 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1361 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001362 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001363 Toks[1] = RedefName;
1364 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001365 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
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
David Blaikie2eabcc92016-02-09 18:52:09 +00001377 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1378 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001379 Toks[0].startToken();
1380 Toks[0].setKind(tok::annot_pragma_fp_contract);
1381 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001382 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001383 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1384 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001385 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001386}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001387
1388void
1389PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1390 PragmaIntroducerKind Introducer,
1391 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001392 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001393 if (Tok.isNot(tok::identifier)) {
1394 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1395 "OPENCL";
1396 return;
1397 }
1398 IdentifierInfo *ename = Tok.getIdentifierInfo();
1399 SourceLocation NameLoc = Tok.getLocation();
1400
1401 PP.Lex(Tok);
1402 if (Tok.isNot(tok::colon)) {
1403 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1404 return;
1405 }
1406
1407 PP.Lex(Tok);
1408 if (Tok.isNot(tok::identifier)) {
1409 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1410 return;
1411 }
1412 IdentifierInfo *op = Tok.getIdentifierInfo();
1413
1414 unsigned state;
1415 if (op->isStr("enable")) {
1416 state = 1;
1417 } else if (op->isStr("disable")) {
1418 state = 0;
1419 } else {
1420 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1421 return;
1422 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001423 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001424
Eli Friedman68be1642012-10-04 02:36:51 +00001425 PP.Lex(Tok);
1426 if (Tok.isNot(tok::eod)) {
1427 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1428 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001429 return;
1430 }
Eli Friedman68be1642012-10-04 02:36:51 +00001431
1432 OpenCLExtData data(ename, state);
David Blaikie2eabcc92016-02-09 18:52:09 +00001433 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1434 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001435 Toks[0].startToken();
1436 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1437 Toks[0].setLocation(NameLoc);
1438 Toks[0].setAnnotationValue(data.getOpaqueValue());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001439 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00001440 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001441
1442 if (PP.getPPCallbacks())
1443 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1444 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001445}
1446
Alexey Bataeva769e072013-03-22 06:34:35 +00001447/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1448///
1449void
1450PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1451 PragmaIntroducerKind Introducer,
1452 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001453 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1454 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001455 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001456 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1457 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001458 }
1459 PP.DiscardUntilEndOfDirective();
1460}
1461
1462/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1463///
1464void
1465PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1466 PragmaIntroducerKind Introducer,
1467 Token &FirstTok) {
1468 SmallVector<Token, 16> Pragma;
1469 Token Tok;
1470 Tok.startToken();
1471 Tok.setKind(tok::annot_pragma_openmp);
1472 Tok.setLocation(FirstTok.getLocation());
1473
1474 while (Tok.isNot(tok::eod)) {
1475 Pragma.push_back(Tok);
1476 PP.Lex(Tok);
1477 }
1478 SourceLocation EodLoc = Tok.getLocation();
1479 Tok.startToken();
1480 Tok.setKind(tok::annot_pragma_openmp_end);
1481 Tok.setLocation(EodLoc);
1482 Pragma.push_back(Tok);
1483
David Blaikie2eabcc92016-02-09 18:52:09 +00001484 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
1485 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
1486 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
1487 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00001488}
Reid Kleckner002562a2013-05-06 21:02:12 +00001489
David Majnemer4bb09802014-02-10 19:50:15 +00001490/// \brief Handle '#pragma pointers_to_members'
1491// The grammar for this pragma is as follows:
1492//
1493// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1494//
1495// #pragma pointers_to_members '(' 'best_case' ')'
1496// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1497// #pragma pointers_to_members '(' inheritance-model ')'
1498void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1499 PragmaIntroducerKind Introducer,
1500 Token &Tok) {
1501 SourceLocation PointersToMembersLoc = Tok.getLocation();
1502 PP.Lex(Tok);
1503 if (Tok.isNot(tok::l_paren)) {
1504 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1505 << "pointers_to_members";
1506 return;
1507 }
1508 PP.Lex(Tok);
1509 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1510 if (!Arg) {
1511 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1512 << "pointers_to_members";
1513 return;
1514 }
1515 PP.Lex(Tok);
1516
David Majnemer86c318f2014-02-11 21:05:00 +00001517 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001518 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001519 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001520 } else {
1521 if (Arg->isStr("full_generality")) {
1522 if (Tok.is(tok::comma)) {
1523 PP.Lex(Tok);
1524
1525 Arg = Tok.getIdentifierInfo();
1526 if (!Arg) {
1527 PP.Diag(Tok.getLocation(),
1528 diag::err_pragma_pointers_to_members_unknown_kind)
1529 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1530 return;
1531 }
1532 PP.Lex(Tok);
1533 } else if (Tok.is(tok::r_paren)) {
1534 // #pragma pointers_to_members(full_generality) implicitly specifies
1535 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001536 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001537 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001538 } else {
1539 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1540 << "full_generality";
1541 return;
1542 }
1543 }
1544
1545 if (Arg) {
1546 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001547 RepresentationMethod =
1548 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001549 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001550 RepresentationMethod =
1551 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001552 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001553 RepresentationMethod =
1554 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001555 } else {
1556 PP.Diag(Tok.getLocation(),
1557 diag::err_pragma_pointers_to_members_unknown_kind)
1558 << Arg << /*HasPointerDeclaration*/ 1;
1559 return;
1560 }
1561 }
1562 }
1563
1564 if (Tok.isNot(tok::r_paren)) {
1565 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1566 << (Arg ? Arg->getName() : "full_generality");
1567 return;
1568 }
1569
David Majnemera8f2f1d2015-03-19 00:10:23 +00001570 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001571 PP.Lex(Tok);
1572 if (Tok.isNot(tok::eod)) {
1573 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1574 << "pointers_to_members";
1575 return;
1576 }
1577
1578 Token AnnotTok;
1579 AnnotTok.startToken();
1580 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1581 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001582 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001583 AnnotTok.setAnnotationValue(
1584 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1585 PP.EnterToken(AnnotTok);
1586}
1587
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001588/// \brief Handle '#pragma vtordisp'
1589// The grammar for this pragma is as follows:
1590//
1591// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1592//
1593// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1594// #pragma vtordisp '(' 'pop' ')'
1595// #pragma vtordisp '(' ')'
1596void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1597 PragmaIntroducerKind Introducer,
1598 Token &Tok) {
1599 SourceLocation VtorDispLoc = Tok.getLocation();
1600 PP.Lex(Tok);
1601 if (Tok.isNot(tok::l_paren)) {
1602 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1603 return;
1604 }
1605 PP.Lex(Tok);
1606
1607 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1608 const IdentifierInfo *II = Tok.getIdentifierInfo();
1609 if (II) {
1610 if (II->isStr("push")) {
1611 // #pragma vtordisp(push, mode)
1612 PP.Lex(Tok);
1613 if (Tok.isNot(tok::comma)) {
1614 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1615 return;
1616 }
1617 PP.Lex(Tok);
1618 Kind = Sema::PVDK_Push;
1619 // not push, could be on/off
1620 } else if (II->isStr("pop")) {
1621 // #pragma vtordisp(pop)
1622 PP.Lex(Tok);
1623 Kind = Sema::PVDK_Pop;
1624 }
1625 // not push or pop, could be on/off
1626 } else {
1627 if (Tok.is(tok::r_paren)) {
1628 // #pragma vtordisp()
1629 Kind = Sema::PVDK_Reset;
1630 }
1631 }
1632
1633
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001634 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001635 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1636 const IdentifierInfo *II = Tok.getIdentifierInfo();
1637 if (II && II->isStr("off")) {
1638 PP.Lex(Tok);
1639 Value = 0;
1640 } else if (II && II->isStr("on")) {
1641 PP.Lex(Tok);
1642 Value = 1;
1643 } else if (Tok.is(tok::numeric_constant) &&
1644 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1645 if (Value > 2) {
1646 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1647 << 0 << 2 << "vtordisp";
1648 return;
1649 }
1650 } else {
1651 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1652 << "vtordisp";
1653 return;
1654 }
1655 }
1656
1657 // Finish the pragma: ')' $
1658 if (Tok.isNot(tok::r_paren)) {
1659 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1660 return;
1661 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001662 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001663 PP.Lex(Tok);
1664 if (Tok.isNot(tok::eod)) {
1665 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1666 << "vtordisp";
1667 return;
1668 }
1669
1670 // Enter the annotation.
1671 Token AnnotTok;
1672 AnnotTok.startToken();
1673 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1674 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001675 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001676 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1677 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001678 PP.EnterToken(AnnotTok);
1679}
1680
Warren Huntc3b18962014-04-08 22:30:47 +00001681/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1682/// an annotation token.
1683void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1684 PragmaIntroducerKind Introducer,
1685 Token &Tok) {
1686 Token EoF, AnnotTok;
1687 EoF.startToken();
1688 EoF.setKind(tok::eof);
1689 AnnotTok.startToken();
1690 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1691 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001692 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001693 SmallVector<Token, 8> TokenVector;
1694 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001695 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001696 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001697 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1698 }
Warren Huntc3b18962014-04-08 22:30:47 +00001699 // Add a sentinal EoF token to the end of the list.
1700 TokenVector.push_back(EoF);
1701 // We must allocate this array with new because EnterTokenStream is going to
1702 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00001703 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
1704 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00001705 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00001706 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
1707 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00001708 AnnotTok.setAnnotationValue(Value);
1709 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001710}
1711
Aaron Ballman5d041be2013-06-04 02:07:14 +00001712/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1713///
1714/// The syntax is:
1715/// \code
1716/// #pragma detect_mismatch("name", "value")
1717/// \endcode
1718/// Where 'name' and 'value' are quoted strings. The values are embedded in
1719/// the object file and passed along to the linker. If the linker detects a
1720/// mismatch in the object file's values for the given name, a LNK2038 error
1721/// is emitted. See MSDN for more details.
1722void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1723 PragmaIntroducerKind Introducer,
1724 Token &Tok) {
1725 SourceLocation CommentLoc = Tok.getLocation();
1726 PP.Lex(Tok);
1727 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001728 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001729 return;
1730 }
1731
1732 // Read the name to embed, which must be a string literal.
1733 std::string NameString;
1734 if (!PP.LexStringLiteral(Tok, NameString,
1735 "pragma detect_mismatch",
1736 /*MacroExpansion=*/true))
1737 return;
1738
1739 // Read the comma followed by a second string literal.
1740 std::string ValueString;
1741 if (Tok.isNot(tok::comma)) {
1742 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1743 return;
1744 }
1745
1746 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1747 /*MacroExpansion=*/true))
1748 return;
1749
1750 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001751 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001752 return;
1753 }
1754 PP.Lex(Tok); // Eat the r_paren.
1755
1756 if (Tok.isNot(tok::eod)) {
1757 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1758 return;
1759 }
1760
Reid Kleckner71966c92014-02-20 23:37:45 +00001761 // If the pragma is lexically sound, notify any interested PPCallbacks.
1762 if (PP.getPPCallbacks())
1763 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1764 ValueString);
1765
Aaron Ballman5d041be2013-06-04 02:07:14 +00001766 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1767}
1768
Reid Kleckner002562a2013-05-06 21:02:12 +00001769/// \brief Handle the microsoft \#pragma comment extension.
1770///
1771/// The syntax is:
1772/// \code
1773/// #pragma comment(linker, "foo")
1774/// \endcode
1775/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1776/// "foo" is a string, which is fully macro expanded, and permits string
1777/// concatenation, embedded escape characters etc. See MSDN for more details.
1778void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1779 PragmaIntroducerKind Introducer,
1780 Token &Tok) {
1781 SourceLocation CommentLoc = Tok.getLocation();
1782 PP.Lex(Tok);
1783 if (Tok.isNot(tok::l_paren)) {
1784 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1785 return;
1786 }
1787
1788 // Read the identifier.
1789 PP.Lex(Tok);
1790 if (Tok.isNot(tok::identifier)) {
1791 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1792 return;
1793 }
1794
1795 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001796 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00001797 PragmaMSCommentKind Kind =
1798 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
1799 .Case("linker", PCK_Linker)
1800 .Case("lib", PCK_Lib)
1801 .Case("compiler", PCK_Compiler)
1802 .Case("exestr", PCK_ExeStr)
1803 .Case("user", PCK_User)
1804 .Default(PCK_Unknown);
1805 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001806 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1807 return;
1808 }
1809
Yunzhong Gao99efc032015-03-23 20:41:42 +00001810 // On PS4, issue a warning about any pragma comments other than
1811 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00001812 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00001813 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1814 << II->getName();
1815 return;
1816 }
1817
Reid Kleckner002562a2013-05-06 21:02:12 +00001818 // Read the optional string if present.
1819 PP.Lex(Tok);
1820 std::string ArgumentString;
1821 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1822 "pragma comment",
1823 /*MacroExpansion=*/true))
1824 return;
1825
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001826 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001827 // FIXME: If the kind is "compiler" warn if the string is present (it is
1828 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001829 // The MSDN docs say that "lib" and "linker" require a string and have a short
1830 // whitelist of linker options they support, but in practice MSVC doesn't
1831 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001832
1833 if (Tok.isNot(tok::r_paren)) {
1834 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1835 return;
1836 }
1837 PP.Lex(Tok); // eat the r_paren.
1838
1839 if (Tok.isNot(tok::eod)) {
1840 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1841 return;
1842 }
1843
Reid Kleckner71966c92014-02-20 23:37:45 +00001844 // If the pragma is lexically sound, notify any interested PPCallbacks.
1845 if (PP.getPPCallbacks())
1846 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1847
Nico Weber66220292016-03-02 17:28:48 +00001848 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001849}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001850
1851// #pragma clang optimize off
1852// #pragma clang optimize on
1853void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1854 PragmaIntroducerKind Introducer,
1855 Token &FirstToken) {
1856 Token Tok;
1857 PP.Lex(Tok);
1858 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001859 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001860 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001861 return;
1862 }
1863 if (Tok.isNot(tok::identifier)) {
1864 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1865 << PP.getSpelling(Tok);
1866 return;
1867 }
1868 const IdentifierInfo *II = Tok.getIdentifierInfo();
1869 // The only accepted values are 'on' or 'off'.
1870 bool IsOn = false;
1871 if (II->isStr("on")) {
1872 IsOn = true;
1873 } else if (!II->isStr("off")) {
1874 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1875 << PP.getSpelling(Tok);
1876 return;
1877 }
1878 PP.Lex(Tok);
1879
1880 if (Tok.isNot(tok::eod)) {
1881 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1882 << PP.getSpelling(Tok);
1883 return;
1884 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001885
1886 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1887}
1888
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001889/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001890static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1891 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001892 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001893 SmallVector<Token, 1> ValueList;
1894 int OpenParens = ValueInParens ? 1 : 0;
1895 // Read constant expression.
1896 while (Tok.isNot(tok::eod)) {
1897 if (Tok.is(tok::l_paren))
1898 OpenParens++;
1899 else if (Tok.is(tok::r_paren)) {
1900 OpenParens--;
1901 if (OpenParens == 0 && ValueInParens)
1902 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001903 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001904
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001905 ValueList.push_back(Tok);
1906 PP.Lex(Tok);
1907 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001908
1909 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001910 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001911 if (Tok.isNot(tok::r_paren)) {
1912 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1913 return true;
1914 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001915 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001916 }
1917
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001918 Token EOFTok;
1919 EOFTok.startToken();
1920 EOFTok.setKind(tok::eof);
1921 EOFTok.setLocation(Tok.getLocation());
1922 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1923
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001924 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001925
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001926 Info.PragmaName = PragmaName;
1927 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001928 return false;
1929}
1930
Eli Bendersky06a40422014-06-06 20:31:48 +00001931/// \brief Handle the \#pragma clang loop directive.
1932/// #pragma clang 'loop' loop-hints
1933///
1934/// loop-hints:
1935/// loop-hint loop-hints[opt]
1936///
1937/// loop-hint:
1938/// 'vectorize' '(' loop-hint-keyword ')'
1939/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001940/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001941/// 'vectorize_width' '(' loop-hint-value ')'
1942/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001943/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001944///
1945/// loop-hint-keyword:
1946/// 'enable'
1947/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00001948/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00001949///
Mark Heffernan450c2382014-07-23 17:31:31 +00001950/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00001951/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00001952/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00001953/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00001954///
Eli Bendersky06a40422014-06-06 20:31:48 +00001955/// loop-hint-value:
1956/// constant-expression
1957///
1958/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1959/// try vectorizing the instructions of the loop it precedes. Specifying
1960/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1961/// interleaving multiple iterations of the loop it precedes. The width of the
1962/// vector instructions is specified by vectorize_width() and the number of
1963/// interleaved loop iterations is specified by interleave_count(). Specifying a
1964/// value of 1 effectively disables vectorization/interleaving, even if it is
1965/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1966/// only works on inner loops.
1967///
Eli Bendersky86483b32014-06-11 17:56:26 +00001968/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00001969/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
1970/// completely if the trip count is known at compile time and unroll partially
1971/// if the trip count is not known. Specifying unroll(full) is similar to
1972/// unroll(enable) but will unroll the loop only if the trip count is known at
1973/// compile time. Specifying unroll(disable) disables unrolling for the
1974/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
1975/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001976void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1977 PragmaIntroducerKind Introducer,
1978 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001979 // Incoming token is "loop" from "#pragma clang loop".
1980 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001981 SmallVector<Token, 1> TokenList;
1982
1983 // Lex the optimization option and verify it is an identifier.
1984 PP.Lex(Tok);
1985 if (Tok.isNot(tok::identifier)) {
1986 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1987 << /*MissingOption=*/true << "";
1988 return;
1989 }
1990
1991 while (Tok.is(tok::identifier)) {
1992 Token Option = Tok;
1993 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1994
Eli Bendersky86483b32014-06-11 17:56:26 +00001995 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001996 .Case("vectorize", true)
1997 .Case("interleave", true)
1998 .Case("unroll", true)
1999 .Case("vectorize_width", true)
2000 .Case("interleave_count", true)
2001 .Case("unroll_count", true)
2002 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002003 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002004 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2005 << /*MissingOption=*/false << OptionInfo;
2006 return;
2007 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002008 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002009
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002010 // Read '('
2011 if (Tok.isNot(tok::l_paren)) {
2012 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002013 return;
2014 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002015 PP.Lex(Tok);
2016
2017 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2018 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2019 *Info))
2020 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002021
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002022 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002023 Token LoopHintTok;
2024 LoopHintTok.startToken();
2025 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002026 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002027 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002028 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2029 TokenList.push_back(LoopHintTok);
2030 }
2031
2032 if (Tok.isNot(tok::eod)) {
2033 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2034 << "clang loop";
2035 return;
2036 }
2037
David Blaikie2eabcc92016-02-09 18:52:09 +00002038 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2039 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002040
David Blaikie2eabcc92016-02-09 18:52:09 +00002041 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2042 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002043}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002044
2045/// \brief Handle the loop unroll optimization pragmas.
2046/// #pragma unroll
2047/// #pragma unroll unroll-hint-value
2048/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002049/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002050///
2051/// unroll-hint-value:
2052/// constant-expression
2053///
Mark Heffernanc888e412014-07-24 18:09:38 +00002054/// Loop unrolling hints can be specified with '#pragma unroll' or
2055/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2056/// contained in parentheses. With no argument the directive instructs llvm to
2057/// try to unroll the loop completely. A positive integer argument can be
2058/// specified to indicate the number of times the loop should be unrolled. To
2059/// maximize compatibility with other compilers the unroll count argument can be
2060/// specified with or without parentheses. Specifying, '#pragma nounroll'
2061/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002062void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2063 PragmaIntroducerKind Introducer,
2064 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002065 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2066 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002067 Token PragmaName = Tok;
2068 PP.Lex(Tok);
2069 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2070 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002071 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002072 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002073 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002074 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2075 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2076 << "nounroll";
2077 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002078 } else {
2079 // Unroll pragma with an argument: "#pragma unroll N" or
2080 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002081 // Read '(' if it exists.
2082 bool ValueInParens = Tok.is(tok::l_paren);
2083 if (ValueInParens)
2084 PP.Lex(Tok);
2085
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002086 Token Option;
2087 Option.startToken();
2088 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002089 return;
2090
2091 // In CUDA, the argument to '#pragma unroll' should not be contained in
2092 // parentheses.
2093 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002094 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002095 diag::warn_pragma_unroll_cuda_value_in_parens);
2096
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002097 if (Tok.isNot(tok::eod)) {
2098 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2099 << "unroll";
2100 return;
2101 }
2102 }
2103
2104 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002105 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002106 TokenArray[0].startToken();
2107 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2108 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002109 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002110 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002111 PP.EnterTokenStream(std::move(TokenArray), 1,
2112 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002113}