blob: bff5d1170fe08ed9d900d0f56023644bf07a3908 [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 {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000340 Sema::PragmaMsStackAction Action;
341 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000342 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000343};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000344} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000345
346void Parser::HandlePragmaPack() {
347 assert(Tok.is(tok::annot_pragma_pack));
348 PragmaPackInfo *Info =
349 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
350 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000351 ExprResult Alignment;
352 if (Info->Alignment.is(tok::numeric_constant)) {
353 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
354 if (Alignment.isInvalid())
355 return;
356 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000357 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
358 Alignment.get());
Eli Friedmanec52f922012-02-23 23:47:16 +0000359}
360
Eli Friedman68be1642012-10-04 02:36:51 +0000361void Parser::HandlePragmaMSStruct() {
362 assert(Tok.is(tok::annot_pragma_msstruct));
Nico Weber779355f2016-03-02 23:22:00 +0000363 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
364 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Eli Friedman68be1642012-10-04 02:36:51 +0000365 Actions.ActOnPragmaMSStruct(Kind);
366 ConsumeToken(); // The annotation token.
367}
368
369void Parser::HandlePragmaAlign() {
370 assert(Tok.is(tok::annot_pragma_align));
371 Sema::PragmaOptionsAlignKind Kind =
372 static_cast<Sema::PragmaOptionsAlignKind>(
373 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
374 SourceLocation PragmaLoc = ConsumeToken();
375 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
376}
377
Richard Smithba3a4f92016-01-12 21:59:26 +0000378void Parser::HandlePragmaDump() {
379 assert(Tok.is(tok::annot_pragma_dump));
380 IdentifierInfo *II =
381 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
382 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
383 ConsumeToken();
384}
385
Eli Friedman68be1642012-10-04 02:36:51 +0000386void Parser::HandlePragmaWeak() {
387 assert(Tok.is(tok::annot_pragma_weak));
388 SourceLocation PragmaLoc = ConsumeToken();
389 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
390 Tok.getLocation());
391 ConsumeToken(); // The weak name.
392}
393
394void Parser::HandlePragmaWeakAlias() {
395 assert(Tok.is(tok::annot_pragma_weakalias));
396 SourceLocation PragmaLoc = ConsumeToken();
397 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
398 SourceLocation WeakNameLoc = Tok.getLocation();
399 ConsumeToken();
400 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
401 SourceLocation AliasNameLoc = Tok.getLocation();
402 ConsumeToken();
403 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
404 WeakNameLoc, AliasNameLoc);
405
406}
407
408void Parser::HandlePragmaRedefineExtname() {
409 assert(Tok.is(tok::annot_pragma_redefine_extname));
410 SourceLocation RedefLoc = ConsumeToken();
411 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
412 SourceLocation RedefNameLoc = Tok.getLocation();
413 ConsumeToken();
414 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
415 SourceLocation AliasNameLoc = Tok.getLocation();
416 ConsumeToken();
417 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
418 RedefNameLoc, AliasNameLoc);
419}
420
421void Parser::HandlePragmaFPContract() {
422 assert(Tok.is(tok::annot_pragma_fp_contract));
423 tok::OnOffSwitch OOS =
424 static_cast<tok::OnOffSwitch>(
425 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
426 Actions.ActOnPragmaFPContract(OOS);
427 ConsumeToken(); // The annotation token.
428}
429
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000430StmtResult Parser::HandlePragmaCaptured()
431{
432 assert(Tok.is(tok::annot_pragma_captured));
433 ConsumeToken();
434
435 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000436 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000437 return StmtError();
438 }
439
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000440 SourceLocation Loc = Tok.getLocation();
441
442 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000443 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
444 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000445
446 StmtResult R = ParseCompoundStatement();
447 CapturedRegionScope.Exit();
448
449 if (R.isInvalid()) {
450 Actions.ActOnCapturedRegionError();
451 return StmtError();
452 }
453
454 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000455}
456
Eli Friedman68be1642012-10-04 02:36:51 +0000457namespace {
458 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
459}
460
461void Parser::HandlePragmaOpenCLExtension() {
462 assert(Tok.is(tok::annot_pragma_opencl_extension));
463 OpenCLExtData data =
464 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
465 unsigned state = data.getInt();
466 IdentifierInfo *ename = data.getPointer();
467 SourceLocation NameLoc = Tok.getLocation();
468 ConsumeToken(); // The annotation token.
469
470 OpenCLOptions &f = Actions.getOpenCLOptions();
Yaxun Liu39cf40f2016-05-16 17:06:34 +0000471 auto CLVer = getLangOpts().OpenCLVersion;
472 auto &Supp = getTargetInfo().getSupportedOpenCLOpts();
Eli Friedman68be1642012-10-04 02:36:51 +0000473 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
474 // overriding all previously issued extension directives, but only if the
475 // behavior is set to disable."
476 if (state == 0 && ename->isStr("all")) {
Yaxun Liu39cf40f2016-05-16 17:06:34 +0000477#define OPENCLEXT(nm) \
478 if (Supp.is_##nm##_supported_extension(CLVer)) \
479 f.nm = 0;
Eli Friedman68be1642012-10-04 02:36:51 +0000480#include "clang/Basic/OpenCLExtensions.def"
481 }
Yaxun Liu39cf40f2016-05-16 17:06:34 +0000482#define OPENCLEXT(nm) else if (ename->isStr(#nm)) \
483 if (Supp.is_##nm##_supported_extension(CLVer)) \
484 f.nm = state; \
485 else if (Supp.is_##nm##_supported_core(CLVer)) \
486 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << ename; \
487 else \
488 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << ename;
Eli Friedman68be1642012-10-04 02:36:51 +0000489#include "clang/Basic/OpenCLExtensions.def"
490 else {
491 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
492 return;
493 }
494}
495
David Majnemer4bb09802014-02-10 19:50:15 +0000496void Parser::HandlePragmaMSPointersToMembers() {
497 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000498 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
499 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000500 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
501 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
502 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
503}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000504
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000505void Parser::HandlePragmaMSVtorDisp() {
506 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
507 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
Denis Zobnin2290dac2016-04-29 11:27:00 +0000508 Sema::PragmaMsStackAction Action =
509 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000510 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
511 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
Denis Zobnin2290dac2016-04-29 11:27:00 +0000512 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000513}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000514
Warren Huntc3b18962014-04-08 22:30:47 +0000515void Parser::HandlePragmaMSPragma() {
516 assert(Tok.is(tok::annot_pragma_ms_pragma));
517 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000518 auto TheTokens =
519 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
520 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Warren Huntc3b18962014-04-08 22:30:47 +0000521 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
522 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000523 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000524 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000525
Warren Huntc3b18962014-04-08 22:30:47 +0000526 // Figure out which #pragma we're dealing with. The switch has no default
527 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000528 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000529 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
530 .Case("data_seg", &Parser::HandlePragmaMSSegment)
531 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
532 .Case("const_seg", &Parser::HandlePragmaMSSegment)
533 .Case("code_seg", &Parser::HandlePragmaMSSegment)
534 .Case("section", &Parser::HandlePragmaMSSection)
535 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000536
537 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
538 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
539 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000540 while (Tok.isNot(tok::eof))
541 PP.Lex(Tok);
542 PP.Lex(Tok);
543 }
544}
545
Reid Kleckner722b1df2014-07-18 00:13:16 +0000546bool Parser::HandlePragmaMSSection(StringRef PragmaName,
547 SourceLocation PragmaLocation) {
548 if (Tok.isNot(tok::l_paren)) {
549 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
550 return false;
551 }
Warren Huntc3b18962014-04-08 22:30:47 +0000552 PP.Lex(Tok); // (
553 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000554 if (Tok.isNot(tok::string_literal)) {
555 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
556 << PragmaName;
557 return false;
558 }
559 ExprResult StringResult = ParseStringLiteralExpression();
560 if (StringResult.isInvalid())
561 return false; // Already diagnosed.
562 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
563 if (SegmentName->getCharByteWidth() != 1) {
564 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
565 << PragmaName;
566 return false;
567 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000568 int SectionFlags = ASTContext::PSF_Read;
569 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000570 while (Tok.is(tok::comma)) {
571 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000572 // Ignore "long" and "short".
573 // They are undocumented, but widely used, section attributes which appear
574 // to do nothing.
575 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
576 PP.Lex(Tok); // long/short
577 continue;
578 }
579
Reid Kleckner722b1df2014-07-18 00:13:16 +0000580 if (!Tok.isAnyIdentifier()) {
581 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
582 << PragmaName;
583 return false;
584 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000585 ASTContext::PragmaSectionFlag Flag =
586 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000587 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000588 .Case("read", ASTContext::PSF_Read)
589 .Case("write", ASTContext::PSF_Write)
590 .Case("execute", ASTContext::PSF_Execute)
591 .Case("shared", ASTContext::PSF_Invalid)
592 .Case("nopage", ASTContext::PSF_Invalid)
593 .Case("nocache", ASTContext::PSF_Invalid)
594 .Case("discard", ASTContext::PSF_Invalid)
595 .Case("remove", ASTContext::PSF_Invalid)
596 .Default(ASTContext::PSF_None);
597 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
598 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000599 ? diag::warn_pragma_invalid_specific_action
600 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000601 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000602 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000603 }
604 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000605 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000606 PP.Lex(Tok); // Identifier
607 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000608 // If no section attributes are specified, the section will be marked as
609 // read/write.
610 if (SectionFlagsAreDefault)
611 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000612 if (Tok.isNot(tok::r_paren)) {
613 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
614 return false;
615 }
Warren Huntc3b18962014-04-08 22:30:47 +0000616 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000617 if (Tok.isNot(tok::eof)) {
618 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
619 << PragmaName;
620 return false;
621 }
Warren Huntc3b18962014-04-08 22:30:47 +0000622 PP.Lex(Tok); // eof
623 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000624 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000625}
626
Reid Kleckner722b1df2014-07-18 00:13:16 +0000627bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
628 SourceLocation PragmaLocation) {
629 if (Tok.isNot(tok::l_paren)) {
630 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
631 return false;
632 }
Warren Huntc3b18962014-04-08 22:30:47 +0000633 PP.Lex(Tok); // (
634 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000635 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000636 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000637 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000638 if (PushPop == "push")
639 Action = Sema::PSK_Push;
640 else if (PushPop == "pop")
641 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000642 else {
643 PP.Diag(PragmaLocation,
644 diag::warn_pragma_expected_section_push_pop_or_name)
645 << PragmaName;
646 return false;
647 }
Warren Huntc3b18962014-04-08 22:30:47 +0000648 if (Action != Sema::PSK_Reset) {
649 PP.Lex(Tok); // push | pop
650 if (Tok.is(tok::comma)) {
651 PP.Lex(Tok); // ,
652 // If we've got a comma, we either need a label or a string.
653 if (Tok.isAnyIdentifier()) {
654 SlotLabel = Tok.getIdentifierInfo()->getName();
655 PP.Lex(Tok); // identifier
656 if (Tok.is(tok::comma))
657 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000658 else if (Tok.isNot(tok::r_paren)) {
659 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
660 << PragmaName;
661 return false;
662 }
Warren Huntc3b18962014-04-08 22:30:47 +0000663 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000664 } else if (Tok.isNot(tok::r_paren)) {
665 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
666 return false;
667 }
Warren Huntc3b18962014-04-08 22:30:47 +0000668 }
669 }
670 // Grab the string literal for our section name.
671 StringLiteral *SegmentName = nullptr;
672 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000673 if (Tok.isNot(tok::string_literal)) {
674 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000675 diag::warn_pragma_expected_section_name :
676 diag::warn_pragma_expected_section_label_or_name :
677 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000678 PP.Diag(PragmaLocation, DiagID) << PragmaName;
679 return false;
680 }
681 ExprResult StringResult = ParseStringLiteralExpression();
682 if (StringResult.isInvalid())
683 return false; // Already diagnosed.
684 SegmentName = cast<StringLiteral>(StringResult.get());
685 if (SegmentName->getCharByteWidth() != 1) {
686 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
687 << PragmaName;
688 return false;
689 }
Warren Huntc3b18962014-04-08 22:30:47 +0000690 // Setting section "" has no effect
691 if (SegmentName->getLength())
692 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
693 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000694 if (Tok.isNot(tok::r_paren)) {
695 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
696 return false;
697 }
Warren Huntc3b18962014-04-08 22:30:47 +0000698 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000699 if (Tok.isNot(tok::eof)) {
700 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
701 << PragmaName;
702 return false;
703 }
Warren Huntc3b18962014-04-08 22:30:47 +0000704 PP.Lex(Tok); // eof
705 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
706 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000707 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000708}
709
Reid Kleckner1a711b12014-07-22 00:53:05 +0000710// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000711bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
712 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000713 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
714 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
715 return false;
716 }
717
Reid Kleckner1a711b12014-07-22 00:53:05 +0000718 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
719 PragmaName))
720 return false;
721
722 // Parse either the known section names or the string section name.
723 StringLiteral *SegmentName = nullptr;
724 if (Tok.isAnyIdentifier()) {
725 auto *II = Tok.getIdentifierInfo();
726 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
727 .Case("compiler", "\".CRT$XCC\"")
728 .Case("lib", "\".CRT$XCL\"")
729 .Case("user", "\".CRT$XCU\"")
730 .Default("");
731
732 if (!Section.empty()) {
733 // Pretend the user wrote the appropriate string literal here.
734 Token Toks[1];
735 Toks[0].startToken();
736 Toks[0].setKind(tok::string_literal);
737 Toks[0].setLocation(Tok.getLocation());
738 Toks[0].setLiteralData(Section.data());
739 Toks[0].setLength(Section.size());
740 SegmentName =
741 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
742 PP.Lex(Tok);
743 }
744 } else if (Tok.is(tok::string_literal)) {
745 ExprResult StringResult = ParseStringLiteralExpression();
746 if (StringResult.isInvalid())
747 return false;
748 SegmentName = cast<StringLiteral>(StringResult.get());
749 if (SegmentName->getCharByteWidth() != 1) {
750 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
751 << PragmaName;
752 return false;
753 }
754 // FIXME: Add support for the '[, func-name]' part of the pragma.
755 }
756
757 if (!SegmentName) {
758 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
759 return false;
760 }
761
762 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
763 PragmaName) ||
764 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
765 PragmaName))
766 return false;
767
768 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
769 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000770}
771
Benjamin Kramere003ca22015-10-28 13:54:16 +0000772namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000773struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000774 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000775 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000776 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000777};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000778} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +0000779
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000780static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
781 std::string PragmaString;
782 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
783 PragmaString = "clang loop ";
784 PragmaString += Option.getIdentifierInfo()->getName();
785 } else {
786 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
787 "Unexpected pragma name");
788 PragmaString = "unroll";
789 }
790 return PragmaString;
791}
792
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000793bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000794 assert(Tok.is(tok::annot_pragma_loop_hint));
795 PragmaLoopHintInfo *Info =
796 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
797
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000798 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
799 Hint.PragmaNameLoc = IdentifierLoc::create(
800 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000801
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000802 // It is possible that the loop hint has no option identifier, such as
803 // #pragma unroll(4).
804 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
805 ? Info->Option.getIdentifierInfo()
806 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000807 Hint.OptionLoc = IdentifierLoc::create(
808 Actions.Context, Info->Option.getLocation(), OptionInfo);
809
David Blaikie2eabcc92016-02-09 18:52:09 +0000810 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000811
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000812 // Return a valid hint if pragma unroll or nounroll were specified
813 // without an argument.
814 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
815 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Blaikie2eabcc92016-02-09 18:52:09 +0000816 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000817 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000818 Hint.Range = Info->PragmaName.getLocation();
819 return true;
820 }
821
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000822 // The constant expression is always followed by an eof token, which increases
823 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +0000824 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000825 "PragmaLoopHintInfo::Toks must contain at least one token.");
826
827 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000828 bool OptionUnroll = false;
Adam Nemet2de463e2016-06-14 12:04:26 +0000829 bool OptionDistribute = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000830 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000831 if (OptionInfo) { // Pragma Unroll does not specify an option.
832 OptionUnroll = OptionInfo->isStr("unroll");
Adam Nemet2de463e2016-06-14 12:04:26 +0000833 OptionDistribute = OptionInfo->isStr("distribute");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000834 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
835 .Case("vectorize", true)
836 .Case("interleave", true)
Adam Nemet2de463e2016-06-14 12:04:26 +0000837 .Default(false) ||
838 OptionUnroll || OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000839 }
840
Adam Nemet2de463e2016-06-14 12:04:26 +0000841 bool AssumeSafetyArg = !OptionUnroll && !OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000842 // Verify loop hint has an argument.
843 if (Toks[0].is(tok::eof)) {
844 ConsumeToken(); // The annotation token.
845 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Adam Nemet2de463e2016-06-14 12:04:26 +0000846 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll
847 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000848 return false;
849 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000850
851 // Validate the argument.
852 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000853 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000854 SourceLocation StateLoc = Toks[0].getLocation();
855 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Adam Nemet50de4e82016-04-19 22:17:45 +0000856
857 bool Valid = StateInfo &&
858 llvm::StringSwitch<bool>(StateInfo->getName())
859 .Cases("enable", "disable", true)
860 .Case("full", OptionUnroll)
Adam Nemet2de463e2016-06-14 12:04:26 +0000861 .Case("assume_safety", AssumeSafetyArg)
Adam Nemet50de4e82016-04-19 22:17:45 +0000862 .Default(false);
863 if (!Valid) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000864 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
Adam Nemet2de463e2016-06-14 12:04:26 +0000865 << /*FullKeyword=*/OptionUnroll
866 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000867 return false;
868 }
David Blaikie2eabcc92016-02-09 18:52:09 +0000869 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000870 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
871 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000872 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
873 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000874 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000875 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000876 ConsumeToken(); // The annotation token.
877
878 ExprResult R = ParseConstantExpression();
879
880 // Tokens following an error in an ill-formed constant expression will
881 // remain in the token stream and must be removed.
882 if (Tok.isNot(tok::eof)) {
883 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
884 << PragmaLoopHintString(Info->PragmaName, Info->Option);
885 while (Tok.isNot(tok::eof))
886 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000887 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000888
889 ConsumeToken(); // Consume the constant expression eof terminator.
890
891 if (R.isInvalid() ||
892 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
893 return false;
894
895 // Argument is a constant expression with an integer type.
896 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000897 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000898
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000899 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +0000900 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000901 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000902}
903
904// #pragma GCC visibility comes in two variants:
905// 'push' '(' [visibility] ')'
906// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000907void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
908 PragmaIntroducerKind Introducer,
909 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000910 SourceLocation VisLoc = VisTok.getLocation();
911
912 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000913 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000914
915 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
916
Eli Friedman570024a2010-08-05 06:57:20 +0000917 const IdentifierInfo *VisType;
918 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000919 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000920 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000921 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000922 if (Tok.isNot(tok::l_paren)) {
923 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
924 << "visibility";
925 return;
926 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000927 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000928 VisType = Tok.getIdentifierInfo();
929 if (!VisType) {
930 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
931 << "visibility";
932 return;
933 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000934 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000935 if (Tok.isNot(tok::r_paren)) {
936 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
937 << "visibility";
938 return;
939 }
940 } else {
941 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
942 << "visibility";
943 return;
944 }
David Majnemera8f2f1d2015-03-19 00:10:23 +0000945 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000946 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000947 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000948 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
949 << "visibility";
950 return;
951 }
952
David Blaikie2eabcc92016-02-09 18:52:09 +0000953 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +0000954 Toks[0].startToken();
955 Toks[0].setKind(tok::annot_pragma_vis);
956 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000957 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +0000958 Toks[0].setAnnotationValue(
959 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +0000960 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000961}
962
Daniel Dunbar921b9682008-10-04 19:21:03 +0000963// #pragma pack(...) comes in the following delicious flavors:
964// pack '(' [integer] ')'
965// pack '(' 'show' ')'
966// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000967void PragmaPackHandler::HandlePragma(Preprocessor &PP,
968 PragmaIntroducerKind Introducer,
969 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000970 SourceLocation PackLoc = PackTok.getLocation();
971
972 Token Tok;
973 PP.Lex(Tok);
974 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000975 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000976 return;
977 }
978
Denis Zobnin10c4f452016-04-29 18:17:40 +0000979 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
980 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000981 Token Alignment;
982 Alignment.startToken();
Mike Stump11289f42009-09-09 15:08:12 +0000983 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000984 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000985 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000986
987 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000988
989 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
990 // the push/pop stack.
991 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
Denis Zobnin10c4f452016-04-29 18:17:40 +0000992 Action =
993 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000994 } else if (Tok.is(tok::identifier)) {
995 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000996 if (II->isStr("show")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000997 Action = Sema::PSK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000998 PP.Lex(Tok);
999 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001000 if (II->isStr("push")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001001 Action = Sema::PSK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +00001002 } else if (II->isStr("pop")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001003 Action = Sema::PSK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001004 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001005 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001006 return;
Mike Stump11289f42009-09-09 15:08:12 +00001007 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001008 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001009
Daniel Dunbar921b9682008-10-04 19:21:03 +00001010 if (Tok.is(tok::comma)) {
1011 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001012
Daniel Dunbar921b9682008-10-04 19:21:03 +00001013 if (Tok.is(tok::numeric_constant)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001014 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001015 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001016
1017 PP.Lex(Tok);
1018 } else if (Tok.is(tok::identifier)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001019 SlotLabel = Tok.getIdentifierInfo()->getName();
Daniel Dunbar921b9682008-10-04 19:21:03 +00001020 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001021
Daniel Dunbar921b9682008-10-04 19:21:03 +00001022 if (Tok.is(tok::comma)) {
1023 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001024
Daniel Dunbar921b9682008-10-04 19:21:03 +00001025 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001026 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001027 return;
1028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Denis Zobnin10c4f452016-04-29 18:17:40 +00001030 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001031 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001032
1033 PP.Lex(Tok);
1034 }
1035 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001036 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001037 return;
1038 }
1039 }
1040 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001041 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001042 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1043 // the push/pop stack.
1044 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
Denis Zobnin10c4f452016-04-29 18:17:40 +00001045 Action = Sema::PSK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001046 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001047
1048 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001049 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001050 return;
1051 }
1052
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001053 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001054 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001055 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001056 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1057 return;
1058 }
1059
David Blaikie2eabcc92016-02-09 18:52:09 +00001060 PragmaPackInfo *Info =
1061 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Denis Zobnin10c4f452016-04-29 18:17:40 +00001062 Info->Action = Action;
1063 Info->SlotLabel = SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001064 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001065
David Blaikie2eabcc92016-02-09 18:52:09 +00001066 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1067 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001068 Toks[0].startToken();
1069 Toks[0].setKind(tok::annot_pragma_pack);
1070 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001071 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001072 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001073 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001074}
1075
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001076// #pragma ms_struct on
1077// #pragma ms_struct off
1078void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1079 PragmaIntroducerKind Introducer,
1080 Token &MSStructTok) {
Nico Weber779355f2016-03-02 23:22:00 +00001081 PragmaMSStructKind Kind = PMSST_OFF;
1082
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001083 Token Tok;
1084 PP.Lex(Tok);
1085 if (Tok.isNot(tok::identifier)) {
1086 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1087 return;
1088 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001089 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001090 const IdentifierInfo *II = Tok.getIdentifierInfo();
1091 if (II->isStr("on")) {
Nico Weber779355f2016-03-02 23:22:00 +00001092 Kind = PMSST_ON;
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001093 PP.Lex(Tok);
1094 }
1095 else if (II->isStr("off") || II->isStr("reset"))
1096 PP.Lex(Tok);
1097 else {
1098 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1099 return;
1100 }
1101
1102 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001103 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1104 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001105 return;
1106 }
Eli Friedman68be1642012-10-04 02:36:51 +00001107
David Blaikie2eabcc92016-02-09 18:52:09 +00001108 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1109 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001110 Toks[0].startToken();
1111 Toks[0].setKind(tok::annot_pragma_msstruct);
1112 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001113 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001114 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1115 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001116 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001117}
1118
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001119// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1120// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001121static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001122 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001123 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001124
1125 if (IsOptions) {
1126 PP.Lex(Tok);
1127 if (Tok.isNot(tok::identifier) ||
1128 !Tok.getIdentifierInfo()->isStr("align")) {
1129 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1130 return;
1131 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001132 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001133
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001134 PP.Lex(Tok);
1135 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001136 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1137 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001138 return;
1139 }
1140
1141 PP.Lex(Tok);
1142 if (Tok.isNot(tok::identifier)) {
1143 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001144 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001145 return;
1146 }
1147
John McCallfaf5fb42010-08-26 23:41:50 +00001148 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001149 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001150 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001151 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001152 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001153 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001154 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001155 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001156 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001157 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001158 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001159 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001160 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001161 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001162 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001163 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1164 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001165 return;
1166 }
1167
David Majnemera8f2f1d2015-03-19 00:10:23 +00001168 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001169 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001170 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001171 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001172 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001173 return;
1174 }
1175
David Blaikie2eabcc92016-02-09 18:52:09 +00001176 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1177 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001178 Toks[0].startToken();
1179 Toks[0].setKind(tok::annot_pragma_align);
1180 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001181 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001182 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1183 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001184 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001185}
1186
Douglas Gregorc7d65762010-09-09 22:45:38 +00001187void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1188 PragmaIntroducerKind Introducer,
1189 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001190 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001191}
1192
Douglas Gregorc7d65762010-09-09 22:45:38 +00001193void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1194 PragmaIntroducerKind Introducer,
1195 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001196 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001197}
1198
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001199// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001200void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1201 PragmaIntroducerKind Introducer,
1202 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001203 // FIXME: Should we be expanding macros here? My guess is no.
1204 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001205
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001206 // Lex the left '('.
1207 Token Tok;
1208 PP.Lex(Tok);
1209 if (Tok.isNot(tok::l_paren)) {
1210 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1211 return;
1212 }
Mike Stump11289f42009-09-09 15:08:12 +00001213
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001214 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001215 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001216 SourceLocation RParenLoc;
1217 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001218
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001219 while (true) {
1220 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001221
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001222 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001223 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001224 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001225 LexID = false;
1226 continue;
1227 }
1228
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001229 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001230 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1231 return;
1232 }
Mike Stump11289f42009-09-09 15:08:12 +00001233
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001234 // We are execting a ')' or a ','.
1235 if (Tok.is(tok::comma)) {
1236 LexID = true;
1237 continue;
1238 }
Mike Stump11289f42009-09-09 15:08:12 +00001239
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001240 if (Tok.is(tok::r_paren)) {
1241 RParenLoc = Tok.getLocation();
1242 break;
1243 }
Mike Stump11289f42009-09-09 15:08:12 +00001244
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001245 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001246 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001247 return;
1248 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001249
1250 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001251 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001252 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1253 "unused";
1254 return;
1255 }
1256
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001257 // Verify that we have a location for the right parenthesis.
1258 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001259 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001260
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001261 // For each identifier token, insert into the token stream a
1262 // annot_pragma_unused token followed by the identifier token.
1263 // This allows us to cache a "#pragma unused" that occurs inside an inline
1264 // C++ member function.
1265
David Blaikie2eabcc92016-02-09 18:52:09 +00001266 MutableArrayRef<Token> Toks(
1267 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1268 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001269 for (unsigned i=0; i != Identifiers.size(); i++) {
1270 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1271 pragmaUnusedTok.startToken();
1272 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1273 pragmaUnusedTok.setLocation(UnusedLoc);
1274 idTok = Identifiers[i];
1275 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001276 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001277}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001278
1279// #pragma weak identifier
1280// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001281void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1282 PragmaIntroducerKind Introducer,
1283 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001284 SourceLocation WeakLoc = WeakTok.getLocation();
1285
1286 Token Tok;
1287 PP.Lex(Tok);
1288 if (Tok.isNot(tok::identifier)) {
1289 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1290 return;
1291 }
1292
Eli Friedman68be1642012-10-04 02:36:51 +00001293 Token WeakName = Tok;
1294 bool HasAlias = false;
1295 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001296
1297 PP.Lex(Tok);
1298 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001299 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001300 PP.Lex(Tok);
1301 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001302 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001303 << "weak";
1304 return;
1305 }
Eli Friedman68be1642012-10-04 02:36:51 +00001306 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001307 PP.Lex(Tok);
1308 }
1309
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001310 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001311 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1312 return;
1313 }
1314
Eli Friedman68be1642012-10-04 02:36:51 +00001315 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00001316 MutableArrayRef<Token> Toks(
1317 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001318 Token &pragmaUnusedTok = Toks[0];
1319 pragmaUnusedTok.startToken();
1320 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1321 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001322 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001323 Toks[1] = WeakName;
1324 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001325 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001326 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00001327 MutableArrayRef<Token> Toks(
1328 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00001329 Token &pragmaUnusedTok = Toks[0];
1330 pragmaUnusedTok.startToken();
1331 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1332 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001333 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001334 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001335 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001336 }
1337}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001338
David Chisnall0867d9c2012-02-18 16:12:34 +00001339// #pragma redefine_extname identifier identifier
1340void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1341 PragmaIntroducerKind Introducer,
1342 Token &RedefToken) {
1343 SourceLocation RedefLoc = RedefToken.getLocation();
1344
1345 Token Tok;
1346 PP.Lex(Tok);
1347 if (Tok.isNot(tok::identifier)) {
1348 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1349 "redefine_extname";
1350 return;
1351 }
1352
Eli Friedman68be1642012-10-04 02:36:51 +00001353 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001354 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001355
David Chisnall0867d9c2012-02-18 16:12:34 +00001356 if (Tok.isNot(tok::identifier)) {
1357 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1358 << "redefine_extname";
1359 return;
1360 }
Eli Friedman68be1642012-10-04 02:36:51 +00001361
1362 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001363 PP.Lex(Tok);
1364
1365 if (Tok.isNot(tok::eod)) {
1366 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1367 "redefine_extname";
1368 return;
1369 }
1370
David Blaikie2eabcc92016-02-09 18:52:09 +00001371 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1372 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001373 Token &pragmaRedefTok = Toks[0];
1374 pragmaRedefTok.startToken();
1375 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1376 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001377 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001378 Toks[1] = RedefName;
1379 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001380 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
David Chisnall0867d9c2012-02-18 16:12:34 +00001381}
1382
1383
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001384void
1385PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1386 PragmaIntroducerKind Introducer,
1387 Token &Tok) {
1388 tok::OnOffSwitch OOS;
1389 if (PP.LexOnOffSwitch(OOS))
1390 return;
1391
David Blaikie2eabcc92016-02-09 18:52:09 +00001392 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1393 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001394 Toks[0].startToken();
1395 Toks[0].setKind(tok::annot_pragma_fp_contract);
1396 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001397 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001398 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1399 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001400 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001401}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001402
1403void
1404PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1405 PragmaIntroducerKind Introducer,
1406 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001407 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001408 if (Tok.isNot(tok::identifier)) {
1409 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1410 "OPENCL";
1411 return;
1412 }
1413 IdentifierInfo *ename = Tok.getIdentifierInfo();
1414 SourceLocation NameLoc = Tok.getLocation();
1415
1416 PP.Lex(Tok);
1417 if (Tok.isNot(tok::colon)) {
1418 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1419 return;
1420 }
1421
1422 PP.Lex(Tok);
1423 if (Tok.isNot(tok::identifier)) {
1424 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1425 return;
1426 }
1427 IdentifierInfo *op = Tok.getIdentifierInfo();
1428
1429 unsigned state;
1430 if (op->isStr("enable")) {
1431 state = 1;
1432 } else if (op->isStr("disable")) {
1433 state = 0;
1434 } else {
1435 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1436 return;
1437 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001438 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001439
Eli Friedman68be1642012-10-04 02:36:51 +00001440 PP.Lex(Tok);
1441 if (Tok.isNot(tok::eod)) {
1442 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1443 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001444 return;
1445 }
Eli Friedman68be1642012-10-04 02:36:51 +00001446
1447 OpenCLExtData data(ename, state);
David Blaikie2eabcc92016-02-09 18:52:09 +00001448 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1449 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001450 Toks[0].startToken();
1451 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1452 Toks[0].setLocation(NameLoc);
1453 Toks[0].setAnnotationValue(data.getOpaqueValue());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001454 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00001455 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001456
1457 if (PP.getPPCallbacks())
1458 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1459 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001460}
1461
Alexey Bataeva769e072013-03-22 06:34:35 +00001462/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1463///
1464void
1465PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1466 PragmaIntroducerKind Introducer,
1467 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001468 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1469 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001470 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001471 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1472 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001473 }
1474 PP.DiscardUntilEndOfDirective();
1475}
1476
1477/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1478///
1479void
1480PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1481 PragmaIntroducerKind Introducer,
1482 Token &FirstTok) {
1483 SmallVector<Token, 16> Pragma;
1484 Token Tok;
1485 Tok.startToken();
1486 Tok.setKind(tok::annot_pragma_openmp);
1487 Tok.setLocation(FirstTok.getLocation());
1488
1489 while (Tok.isNot(tok::eod)) {
1490 Pragma.push_back(Tok);
1491 PP.Lex(Tok);
1492 }
1493 SourceLocation EodLoc = Tok.getLocation();
1494 Tok.startToken();
1495 Tok.setKind(tok::annot_pragma_openmp_end);
1496 Tok.setLocation(EodLoc);
1497 Pragma.push_back(Tok);
1498
David Blaikie2eabcc92016-02-09 18:52:09 +00001499 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
1500 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
1501 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
1502 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00001503}
Reid Kleckner002562a2013-05-06 21:02:12 +00001504
David Majnemer4bb09802014-02-10 19:50:15 +00001505/// \brief Handle '#pragma pointers_to_members'
1506// The grammar for this pragma is as follows:
1507//
1508// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1509//
1510// #pragma pointers_to_members '(' 'best_case' ')'
1511// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1512// #pragma pointers_to_members '(' inheritance-model ')'
1513void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1514 PragmaIntroducerKind Introducer,
1515 Token &Tok) {
1516 SourceLocation PointersToMembersLoc = Tok.getLocation();
1517 PP.Lex(Tok);
1518 if (Tok.isNot(tok::l_paren)) {
1519 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1520 << "pointers_to_members";
1521 return;
1522 }
1523 PP.Lex(Tok);
1524 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1525 if (!Arg) {
1526 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1527 << "pointers_to_members";
1528 return;
1529 }
1530 PP.Lex(Tok);
1531
David Majnemer86c318f2014-02-11 21:05:00 +00001532 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001533 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001534 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001535 } else {
1536 if (Arg->isStr("full_generality")) {
1537 if (Tok.is(tok::comma)) {
1538 PP.Lex(Tok);
1539
1540 Arg = Tok.getIdentifierInfo();
1541 if (!Arg) {
1542 PP.Diag(Tok.getLocation(),
1543 diag::err_pragma_pointers_to_members_unknown_kind)
1544 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1545 return;
1546 }
1547 PP.Lex(Tok);
1548 } else if (Tok.is(tok::r_paren)) {
1549 // #pragma pointers_to_members(full_generality) implicitly specifies
1550 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001551 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001552 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001553 } else {
1554 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1555 << "full_generality";
1556 return;
1557 }
1558 }
1559
1560 if (Arg) {
1561 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001562 RepresentationMethod =
1563 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001564 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001565 RepresentationMethod =
1566 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001567 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001568 RepresentationMethod =
1569 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001570 } else {
1571 PP.Diag(Tok.getLocation(),
1572 diag::err_pragma_pointers_to_members_unknown_kind)
1573 << Arg << /*HasPointerDeclaration*/ 1;
1574 return;
1575 }
1576 }
1577 }
1578
1579 if (Tok.isNot(tok::r_paren)) {
1580 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1581 << (Arg ? Arg->getName() : "full_generality");
1582 return;
1583 }
1584
David Majnemera8f2f1d2015-03-19 00:10:23 +00001585 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001586 PP.Lex(Tok);
1587 if (Tok.isNot(tok::eod)) {
1588 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1589 << "pointers_to_members";
1590 return;
1591 }
1592
1593 Token AnnotTok;
1594 AnnotTok.startToken();
1595 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1596 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001597 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001598 AnnotTok.setAnnotationValue(
1599 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1600 PP.EnterToken(AnnotTok);
1601}
1602
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001603/// \brief Handle '#pragma vtordisp'
1604// The grammar for this pragma is as follows:
1605//
1606// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1607//
1608// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1609// #pragma vtordisp '(' 'pop' ')'
1610// #pragma vtordisp '(' ')'
1611void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1612 PragmaIntroducerKind Introducer,
1613 Token &Tok) {
1614 SourceLocation VtorDispLoc = Tok.getLocation();
1615 PP.Lex(Tok);
1616 if (Tok.isNot(tok::l_paren)) {
1617 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1618 return;
1619 }
1620 PP.Lex(Tok);
1621
Denis Zobnin2290dac2016-04-29 11:27:00 +00001622 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001623 const IdentifierInfo *II = Tok.getIdentifierInfo();
1624 if (II) {
1625 if (II->isStr("push")) {
1626 // #pragma vtordisp(push, mode)
1627 PP.Lex(Tok);
1628 if (Tok.isNot(tok::comma)) {
1629 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1630 return;
1631 }
1632 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001633 Action = Sema::PSK_Push_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001634 // not push, could be on/off
1635 } else if (II->isStr("pop")) {
1636 // #pragma vtordisp(pop)
1637 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00001638 Action = Sema::PSK_Pop;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001639 }
1640 // not push or pop, could be on/off
1641 } else {
1642 if (Tok.is(tok::r_paren)) {
1643 // #pragma vtordisp()
Denis Zobnin2290dac2016-04-29 11:27:00 +00001644 Action = Sema::PSK_Reset;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001645 }
1646 }
1647
1648
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001649 uint64_t Value = 0;
Denis Zobnin2290dac2016-04-29 11:27:00 +00001650 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001651 const IdentifierInfo *II = Tok.getIdentifierInfo();
1652 if (II && II->isStr("off")) {
1653 PP.Lex(Tok);
1654 Value = 0;
1655 } else if (II && II->isStr("on")) {
1656 PP.Lex(Tok);
1657 Value = 1;
1658 } else if (Tok.is(tok::numeric_constant) &&
1659 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1660 if (Value > 2) {
1661 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1662 << 0 << 2 << "vtordisp";
1663 return;
1664 }
1665 } else {
1666 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1667 << "vtordisp";
1668 return;
1669 }
1670 }
1671
1672 // Finish the pragma: ')' $
1673 if (Tok.isNot(tok::r_paren)) {
1674 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1675 return;
1676 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001677 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001678 PP.Lex(Tok);
1679 if (Tok.isNot(tok::eod)) {
1680 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1681 << "vtordisp";
1682 return;
1683 }
1684
1685 // Enter the annotation.
1686 Token AnnotTok;
1687 AnnotTok.startToken();
1688 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1689 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001690 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001691 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
Denis Zobnin2290dac2016-04-29 11:27:00 +00001692 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001693 PP.EnterToken(AnnotTok);
1694}
1695
Warren Huntc3b18962014-04-08 22:30:47 +00001696/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1697/// an annotation token.
1698void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1699 PragmaIntroducerKind Introducer,
1700 Token &Tok) {
1701 Token EoF, AnnotTok;
1702 EoF.startToken();
1703 EoF.setKind(tok::eof);
1704 AnnotTok.startToken();
1705 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1706 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001707 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001708 SmallVector<Token, 8> TokenVector;
1709 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001710 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001711 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001712 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1713 }
Warren Huntc3b18962014-04-08 22:30:47 +00001714 // Add a sentinal EoF token to the end of the list.
1715 TokenVector.push_back(EoF);
1716 // We must allocate this array with new because EnterTokenStream is going to
1717 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00001718 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
1719 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00001720 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00001721 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
1722 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00001723 AnnotTok.setAnnotationValue(Value);
1724 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001725}
1726
Aaron Ballman5d041be2013-06-04 02:07:14 +00001727/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1728///
1729/// The syntax is:
1730/// \code
1731/// #pragma detect_mismatch("name", "value")
1732/// \endcode
1733/// Where 'name' and 'value' are quoted strings. The values are embedded in
1734/// the object file and passed along to the linker. If the linker detects a
1735/// mismatch in the object file's values for the given name, a LNK2038 error
1736/// is emitted. See MSDN for more details.
1737void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1738 PragmaIntroducerKind Introducer,
1739 Token &Tok) {
Nico Webercbbaeb12016-03-02 19:28:54 +00001740 SourceLocation DetectMismatchLoc = Tok.getLocation();
Aaron Ballman5d041be2013-06-04 02:07:14 +00001741 PP.Lex(Tok);
1742 if (Tok.isNot(tok::l_paren)) {
Nico Webercbbaeb12016-03-02 19:28:54 +00001743 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001744 return;
1745 }
1746
1747 // Read the name to embed, which must be a string literal.
1748 std::string NameString;
1749 if (!PP.LexStringLiteral(Tok, NameString,
1750 "pragma detect_mismatch",
1751 /*MacroExpansion=*/true))
1752 return;
1753
1754 // Read the comma followed by a second string literal.
1755 std::string ValueString;
1756 if (Tok.isNot(tok::comma)) {
1757 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1758 return;
1759 }
1760
1761 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1762 /*MacroExpansion=*/true))
1763 return;
1764
1765 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001766 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001767 return;
1768 }
1769 PP.Lex(Tok); // Eat the r_paren.
1770
1771 if (Tok.isNot(tok::eod)) {
1772 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1773 return;
1774 }
1775
Reid Kleckner71966c92014-02-20 23:37:45 +00001776 // If the pragma is lexically sound, notify any interested PPCallbacks.
1777 if (PP.getPPCallbacks())
Nico Webercbbaeb12016-03-02 19:28:54 +00001778 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
Reid Kleckner71966c92014-02-20 23:37:45 +00001779 ValueString);
1780
Nico Webercbbaeb12016-03-02 19:28:54 +00001781 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
Aaron Ballman5d041be2013-06-04 02:07:14 +00001782}
1783
Reid Kleckner002562a2013-05-06 21:02:12 +00001784/// \brief Handle the microsoft \#pragma comment extension.
1785///
1786/// The syntax is:
1787/// \code
1788/// #pragma comment(linker, "foo")
1789/// \endcode
1790/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1791/// "foo" is a string, which is fully macro expanded, and permits string
1792/// concatenation, embedded escape characters etc. See MSDN for more details.
1793void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1794 PragmaIntroducerKind Introducer,
1795 Token &Tok) {
1796 SourceLocation CommentLoc = Tok.getLocation();
1797 PP.Lex(Tok);
1798 if (Tok.isNot(tok::l_paren)) {
1799 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1800 return;
1801 }
1802
1803 // Read the identifier.
1804 PP.Lex(Tok);
1805 if (Tok.isNot(tok::identifier)) {
1806 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1807 return;
1808 }
1809
1810 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001811 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00001812 PragmaMSCommentKind Kind =
1813 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
1814 .Case("linker", PCK_Linker)
1815 .Case("lib", PCK_Lib)
1816 .Case("compiler", PCK_Compiler)
1817 .Case("exestr", PCK_ExeStr)
1818 .Case("user", PCK_User)
1819 .Default(PCK_Unknown);
1820 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001821 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1822 return;
1823 }
1824
Yunzhong Gao99efc032015-03-23 20:41:42 +00001825 // On PS4, issue a warning about any pragma comments other than
1826 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00001827 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00001828 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1829 << II->getName();
1830 return;
1831 }
1832
Reid Kleckner002562a2013-05-06 21:02:12 +00001833 // Read the optional string if present.
1834 PP.Lex(Tok);
1835 std::string ArgumentString;
1836 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1837 "pragma comment",
1838 /*MacroExpansion=*/true))
1839 return;
1840
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001841 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001842 // FIXME: If the kind is "compiler" warn if the string is present (it is
1843 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001844 // The MSDN docs say that "lib" and "linker" require a string and have a short
1845 // whitelist of linker options they support, but in practice MSVC doesn't
1846 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001847
1848 if (Tok.isNot(tok::r_paren)) {
1849 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1850 return;
1851 }
1852 PP.Lex(Tok); // eat the r_paren.
1853
1854 if (Tok.isNot(tok::eod)) {
1855 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1856 return;
1857 }
1858
Reid Kleckner71966c92014-02-20 23:37:45 +00001859 // If the pragma is lexically sound, notify any interested PPCallbacks.
1860 if (PP.getPPCallbacks())
1861 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1862
Nico Weber66220292016-03-02 17:28:48 +00001863 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001864}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001865
1866// #pragma clang optimize off
1867// #pragma clang optimize on
1868void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1869 PragmaIntroducerKind Introducer,
1870 Token &FirstToken) {
1871 Token Tok;
1872 PP.Lex(Tok);
1873 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001874 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001875 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001876 return;
1877 }
1878 if (Tok.isNot(tok::identifier)) {
1879 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1880 << PP.getSpelling(Tok);
1881 return;
1882 }
1883 const IdentifierInfo *II = Tok.getIdentifierInfo();
1884 // The only accepted values are 'on' or 'off'.
1885 bool IsOn = false;
1886 if (II->isStr("on")) {
1887 IsOn = true;
1888 } else if (!II->isStr("off")) {
1889 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1890 << PP.getSpelling(Tok);
1891 return;
1892 }
1893 PP.Lex(Tok);
1894
1895 if (Tok.isNot(tok::eod)) {
1896 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1897 << PP.getSpelling(Tok);
1898 return;
1899 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001900
1901 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1902}
1903
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001904/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001905static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1906 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001907 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001908 SmallVector<Token, 1> ValueList;
1909 int OpenParens = ValueInParens ? 1 : 0;
1910 // Read constant expression.
1911 while (Tok.isNot(tok::eod)) {
1912 if (Tok.is(tok::l_paren))
1913 OpenParens++;
1914 else if (Tok.is(tok::r_paren)) {
1915 OpenParens--;
1916 if (OpenParens == 0 && ValueInParens)
1917 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001918 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001919
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001920 ValueList.push_back(Tok);
1921 PP.Lex(Tok);
1922 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001923
1924 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001925 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001926 if (Tok.isNot(tok::r_paren)) {
1927 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1928 return true;
1929 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001930 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001931 }
1932
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001933 Token EOFTok;
1934 EOFTok.startToken();
1935 EOFTok.setKind(tok::eof);
1936 EOFTok.setLocation(Tok.getLocation());
1937 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1938
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001939 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001940
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001941 Info.PragmaName = PragmaName;
1942 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001943 return false;
1944}
1945
Eli Bendersky06a40422014-06-06 20:31:48 +00001946/// \brief Handle the \#pragma clang loop directive.
1947/// #pragma clang 'loop' loop-hints
1948///
1949/// loop-hints:
1950/// loop-hint loop-hints[opt]
1951///
1952/// loop-hint:
1953/// 'vectorize' '(' loop-hint-keyword ')'
1954/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001955/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001956/// 'vectorize_width' '(' loop-hint-value ')'
1957/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001958/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001959///
1960/// loop-hint-keyword:
1961/// 'enable'
1962/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00001963/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00001964///
Mark Heffernan450c2382014-07-23 17:31:31 +00001965/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00001966/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00001967/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00001968/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00001969///
Eli Bendersky06a40422014-06-06 20:31:48 +00001970/// loop-hint-value:
1971/// constant-expression
1972///
1973/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1974/// try vectorizing the instructions of the loop it precedes. Specifying
1975/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1976/// interleaving multiple iterations of the loop it precedes. The width of the
1977/// vector instructions is specified by vectorize_width() and the number of
1978/// interleaved loop iterations is specified by interleave_count(). Specifying a
1979/// value of 1 effectively disables vectorization/interleaving, even if it is
1980/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1981/// only works on inner loops.
1982///
Eli Bendersky86483b32014-06-11 17:56:26 +00001983/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00001984/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
1985/// completely if the trip count is known at compile time and unroll partially
1986/// if the trip count is not known. Specifying unroll(full) is similar to
1987/// unroll(enable) but will unroll the loop only if the trip count is known at
1988/// compile time. Specifying unroll(disable) disables unrolling for the
1989/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
1990/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001991void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1992 PragmaIntroducerKind Introducer,
1993 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001994 // Incoming token is "loop" from "#pragma clang loop".
1995 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001996 SmallVector<Token, 1> TokenList;
1997
1998 // Lex the optimization option and verify it is an identifier.
1999 PP.Lex(Tok);
2000 if (Tok.isNot(tok::identifier)) {
2001 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2002 << /*MissingOption=*/true << "";
2003 return;
2004 }
2005
2006 while (Tok.is(tok::identifier)) {
2007 Token Option = Tok;
2008 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2009
Eli Bendersky86483b32014-06-11 17:56:26 +00002010 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002011 .Case("vectorize", true)
2012 .Case("interleave", true)
2013 .Case("unroll", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00002014 .Case("distribute", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002015 .Case("vectorize_width", true)
2016 .Case("interleave_count", true)
2017 .Case("unroll_count", true)
2018 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002019 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002020 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2021 << /*MissingOption=*/false << OptionInfo;
2022 return;
2023 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002024 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002025
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002026 // Read '('
2027 if (Tok.isNot(tok::l_paren)) {
2028 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002029 return;
2030 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002031 PP.Lex(Tok);
2032
2033 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2034 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2035 *Info))
2036 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002037
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002038 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002039 Token LoopHintTok;
2040 LoopHintTok.startToken();
2041 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002042 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002043 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002044 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2045 TokenList.push_back(LoopHintTok);
2046 }
2047
2048 if (Tok.isNot(tok::eod)) {
2049 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2050 << "clang loop";
2051 return;
2052 }
2053
David Blaikie2eabcc92016-02-09 18:52:09 +00002054 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2055 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002056
David Blaikie2eabcc92016-02-09 18:52:09 +00002057 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2058 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002059}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002060
2061/// \brief Handle the loop unroll optimization pragmas.
2062/// #pragma unroll
2063/// #pragma unroll unroll-hint-value
2064/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002065/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002066///
2067/// unroll-hint-value:
2068/// constant-expression
2069///
Mark Heffernanc888e412014-07-24 18:09:38 +00002070/// Loop unrolling hints can be specified with '#pragma unroll' or
2071/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2072/// contained in parentheses. With no argument the directive instructs llvm to
2073/// try to unroll the loop completely. A positive integer argument can be
2074/// specified to indicate the number of times the loop should be unrolled. To
2075/// maximize compatibility with other compilers the unroll count argument can be
2076/// specified with or without parentheses. Specifying, '#pragma nounroll'
2077/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002078void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2079 PragmaIntroducerKind Introducer,
2080 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002081 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2082 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002083 Token PragmaName = Tok;
2084 PP.Lex(Tok);
2085 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2086 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002087 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002088 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002089 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002090 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2091 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2092 << "nounroll";
2093 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002094 } else {
2095 // Unroll pragma with an argument: "#pragma unroll N" or
2096 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002097 // Read '(' if it exists.
2098 bool ValueInParens = Tok.is(tok::l_paren);
2099 if (ValueInParens)
2100 PP.Lex(Tok);
2101
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002102 Token Option;
2103 Option.startToken();
2104 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002105 return;
2106
2107 // In CUDA, the argument to '#pragma unroll' should not be contained in
2108 // parentheses.
2109 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002110 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002111 diag::warn_pragma_unroll_cuda_value_in_parens);
2112
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002113 if (Tok.isNot(tok::eod)) {
2114 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2115 << "unroll";
2116 return;
2117 }
2118 }
2119
2120 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002121 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002122 TokenArray[0].startToken();
2123 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2124 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002125 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002126 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002127 PP.EnterTokenStream(std::move(TokenArray), 1,
2128 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002129}