blob: 42f8b518b17d75ccea4f53941de11499ff7f5cd9 [file] [log] [blame]
Daniel Dunbar921b9682008-10-04 19:21:03 +00001//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the language specific #pragma handlers.
11//
12//===----------------------------------------------------------------------===//
13
Warren Huntc3b18962014-04-08 22:30:47 +000014#include "RAIIObjectsForParser.h"
Hans Wennborg899ded92014-10-16 20:52:46 +000015#include "clang/AST/ASTContext.h"
David Majnemerad2986e2014-08-14 06:35:08 +000016#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/ParseDiagnostic.h"
19#include "clang/Parse/Parser.h"
20#include "clang/Sema/LoopHint.h"
21#include "clang/Sema/Scope.h"
22#include "llvm/ADT/StringSwitch.h"
23using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000024
Reid Kleckner5b086462014-02-20 22:52:09 +000025namespace {
26
27struct PragmaAlignHandler : public PragmaHandler {
28 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Craig Topper2b07f022014-03-12 05:09:18 +000029 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
30 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000031};
32
33struct PragmaGCCVisibilityHandler : public PragmaHandler {
34 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000035 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
36 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000037};
38
39struct PragmaOptionsHandler : public PragmaHandler {
40 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000041 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
42 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000043};
44
45struct PragmaPackHandler : public PragmaHandler {
46 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000047 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
48 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000049};
50
51struct PragmaMSStructHandler : public PragmaHandler {
52 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000053 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
54 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000055};
56
57struct PragmaUnusedHandler : public PragmaHandler {
58 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000059 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
60 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000061};
62
63struct PragmaWeakHandler : public PragmaHandler {
64 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000065 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
66 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000067};
68
69struct PragmaRedefineExtnameHandler : public PragmaHandler {
70 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000071 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
72 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000073};
74
75struct PragmaOpenCLExtensionHandler : public PragmaHandler {
76 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000077 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
78 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000079};
80
81
82struct PragmaFPContractHandler : public PragmaHandler {
83 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000084 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
85 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000086};
87
88struct PragmaNoOpenMPHandler : public PragmaHandler {
89 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000090 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
91 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000092};
93
94struct PragmaOpenMPHandler : public PragmaHandler {
95 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000096 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
97 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000098};
99
100/// PragmaCommentHandler - "\#pragma comment ...".
101struct PragmaCommentHandler : public PragmaHandler {
102 PragmaCommentHandler(Sema &Actions)
103 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000104 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
105 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000106private:
107 Sema &Actions;
108};
109
110struct PragmaDetectMismatchHandler : public PragmaHandler {
111 PragmaDetectMismatchHandler(Sema &Actions)
112 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000113 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
114 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000115private:
116 Sema &Actions;
117};
118
119struct PragmaMSPointersToMembers : public PragmaHandler {
120 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000121 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
122 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000123};
124
125struct PragmaMSVtorDisp : public PragmaHandler {
126 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000127 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
128 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000129};
130
Warren Huntc3b18962014-04-08 22:30:47 +0000131struct PragmaMSPragma : public PragmaHandler {
132 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000133 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
134 Token &FirstToken) override;
135};
136
Dario Domizioli13a0a382014-05-23 12:13:25 +0000137/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
138struct PragmaOptimizeHandler : public PragmaHandler {
139 PragmaOptimizeHandler(Sema &S)
140 : PragmaHandler("optimize"), Actions(S) {}
141 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
142 Token &FirstToken) override;
143private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000144 Sema &Actions;
145};
146
147struct PragmaLoopHintHandler : public PragmaHandler {
148 PragmaLoopHintHandler() : PragmaHandler("loop") {}
149 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
150 Token &FirstToken) override;
151};
152
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000153struct PragmaUnrollHintHandler : public PragmaHandler {
154 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
155 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
156 Token &FirstToken) override;
157};
158
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000159struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
160 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
161};
162
Eli Bendersky06a40422014-06-06 20:31:48 +0000163} // end namespace
164
165void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000166 AlignHandler.reset(new PragmaAlignHandler());
167 PP.AddPragmaHandler(AlignHandler.get());
168
169 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
170 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
171
172 OptionsHandler.reset(new PragmaOptionsHandler());
173 PP.AddPragmaHandler(OptionsHandler.get());
174
175 PackHandler.reset(new PragmaPackHandler());
176 PP.AddPragmaHandler(PackHandler.get());
177
178 MSStructHandler.reset(new PragmaMSStructHandler());
179 PP.AddPragmaHandler(MSStructHandler.get());
180
181 UnusedHandler.reset(new PragmaUnusedHandler());
182 PP.AddPragmaHandler(UnusedHandler.get());
183
184 WeakHandler.reset(new PragmaWeakHandler());
185 PP.AddPragmaHandler(WeakHandler.get());
186
187 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
188 PP.AddPragmaHandler(RedefineExtnameHandler.get());
189
190 FPContractHandler.reset(new PragmaFPContractHandler());
191 PP.AddPragmaHandler("STDC", FPContractHandler.get());
192
193 if (getLangOpts().OpenCL) {
194 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
195 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
196
197 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
198 }
199 if (getLangOpts().OpenMP)
200 OpenMPHandler.reset(new PragmaOpenMPHandler());
201 else
202 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
203 PP.AddPragmaHandler(OpenMPHandler.get());
204
Yunzhong Gao99efc032015-03-23 20:41:42 +0000205 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000206 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
207 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000208 }
209
210 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000211 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
212 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
213 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
214 PP.AddPragmaHandler(MSPointersToMembers.get());
215 MSVtorDisp.reset(new PragmaMSVtorDisp());
216 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000217 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000218 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000219 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
220 PP.AddPragmaHandler(MSDataSeg.get());
221 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
222 PP.AddPragmaHandler(MSBSSSeg.get());
223 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
224 PP.AddPragmaHandler(MSConstSeg.get());
225 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
226 PP.AddPragmaHandler(MSCodeSeg.get());
227 MSSection.reset(new PragmaMSPragma("section"));
228 PP.AddPragmaHandler(MSSection.get());
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000229 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
230 PP.AddPragmaHandler(MSRuntimeChecks.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000231 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000232
233 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
234 PP.AddPragmaHandler("clang", OptimizeHandler.get());
235
236 LoopHintHandler.reset(new PragmaLoopHintHandler());
237 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000238
239 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
240 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000241
242 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
243 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000244}
245
246void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000247 // Remove the pragma handlers we installed.
248 PP.RemovePragmaHandler(AlignHandler.get());
249 AlignHandler.reset();
250 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
251 GCCVisibilityHandler.reset();
252 PP.RemovePragmaHandler(OptionsHandler.get());
253 OptionsHandler.reset();
254 PP.RemovePragmaHandler(PackHandler.get());
255 PackHandler.reset();
256 PP.RemovePragmaHandler(MSStructHandler.get());
257 MSStructHandler.reset();
258 PP.RemovePragmaHandler(UnusedHandler.get());
259 UnusedHandler.reset();
260 PP.RemovePragmaHandler(WeakHandler.get());
261 WeakHandler.reset();
262 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
263 RedefineExtnameHandler.reset();
264
265 if (getLangOpts().OpenCL) {
266 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
267 OpenCLExtensionHandler.reset();
268 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
269 }
270 PP.RemovePragmaHandler(OpenMPHandler.get());
271 OpenMPHandler.reset();
272
Yunzhong Gao99efc032015-03-23 20:41:42 +0000273 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000274 PP.RemovePragmaHandler(MSCommentHandler.get());
275 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000276 }
277
278 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000279 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
280 MSDetectMismatchHandler.reset();
281 PP.RemovePragmaHandler(MSPointersToMembers.get());
282 MSPointersToMembers.reset();
283 PP.RemovePragmaHandler(MSVtorDisp.get());
284 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000285 PP.RemovePragmaHandler(MSInitSeg.get());
286 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000287 PP.RemovePragmaHandler(MSDataSeg.get());
288 MSDataSeg.reset();
289 PP.RemovePragmaHandler(MSBSSSeg.get());
290 MSBSSSeg.reset();
291 PP.RemovePragmaHandler(MSConstSeg.get());
292 MSConstSeg.reset();
293 PP.RemovePragmaHandler(MSCodeSeg.get());
294 MSCodeSeg.reset();
295 PP.RemovePragmaHandler(MSSection.get());
296 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000297 PP.RemovePragmaHandler(MSRuntimeChecks.get());
298 MSRuntimeChecks.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000299 }
300
301 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
302 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000303
304 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
305 OptimizeHandler.reset();
306
307 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
308 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000309
310 PP.RemovePragmaHandler(UnrollHintHandler.get());
311 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000312
313 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
314 NoUnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000315}
316
317/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000318///
319/// Each annot_pragma_unused is followed by the argument token so e.g.
320/// "#pragma unused(x,y)" becomes:
321/// annot_pragma_unused 'x' annot_pragma_unused 'y'
322void Parser::HandlePragmaUnused() {
323 assert(Tok.is(tok::annot_pragma_unused));
324 SourceLocation UnusedLoc = ConsumeToken();
325 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
326 ConsumeToken(); // The argument token.
327}
Eli Friedman570024a2010-08-05 06:57:20 +0000328
Rafael Espindola273fd772012-01-26 02:02:57 +0000329void Parser::HandlePragmaVisibility() {
330 assert(Tok.is(tok::annot_pragma_vis));
331 const IdentifierInfo *VisType =
332 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
333 SourceLocation VisLoc = ConsumeToken();
334 Actions.ActOnPragmaVisibility(VisType, VisLoc);
335}
336
Benjamin Kramere003ca22015-10-28 13:54:16 +0000337namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000338struct PragmaPackInfo {
339 Sema::PragmaPackKind Kind;
340 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000341 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000342 SourceLocation LParenLoc;
343 SourceLocation RParenLoc;
344};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000345} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000346
347void Parser::HandlePragmaPack() {
348 assert(Tok.is(tok::annot_pragma_pack));
349 PragmaPackInfo *Info =
350 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
351 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000352 ExprResult Alignment;
353 if (Info->Alignment.is(tok::numeric_constant)) {
354 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
355 if (Alignment.isInvalid())
356 return;
357 }
358 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000359 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000360}
361
Eli Friedman68be1642012-10-04 02:36:51 +0000362void Parser::HandlePragmaMSStruct() {
363 assert(Tok.is(tok::annot_pragma_msstruct));
364 Sema::PragmaMSStructKind Kind =
365 static_cast<Sema::PragmaMSStructKind>(
366 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
367 Actions.ActOnPragmaMSStruct(Kind);
368 ConsumeToken(); // The annotation token.
369}
370
371void Parser::HandlePragmaAlign() {
372 assert(Tok.is(tok::annot_pragma_align));
373 Sema::PragmaOptionsAlignKind Kind =
374 static_cast<Sema::PragmaOptionsAlignKind>(
375 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
376 SourceLocation PragmaLoc = ConsumeToken();
377 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
378}
379
Richard Smithba3a4f92016-01-12 21:59:26 +0000380void Parser::HandlePragmaDump() {
381 assert(Tok.is(tok::annot_pragma_dump));
382 IdentifierInfo *II =
383 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
384 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
385 ConsumeToken();
386}
387
Eli Friedman68be1642012-10-04 02:36:51 +0000388void Parser::HandlePragmaWeak() {
389 assert(Tok.is(tok::annot_pragma_weak));
390 SourceLocation PragmaLoc = ConsumeToken();
391 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
392 Tok.getLocation());
393 ConsumeToken(); // The weak name.
394}
395
396void Parser::HandlePragmaWeakAlias() {
397 assert(Tok.is(tok::annot_pragma_weakalias));
398 SourceLocation PragmaLoc = ConsumeToken();
399 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
400 SourceLocation WeakNameLoc = Tok.getLocation();
401 ConsumeToken();
402 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
403 SourceLocation AliasNameLoc = Tok.getLocation();
404 ConsumeToken();
405 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
406 WeakNameLoc, AliasNameLoc);
407
408}
409
410void Parser::HandlePragmaRedefineExtname() {
411 assert(Tok.is(tok::annot_pragma_redefine_extname));
412 SourceLocation RedefLoc = ConsumeToken();
413 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
414 SourceLocation RedefNameLoc = Tok.getLocation();
415 ConsumeToken();
416 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
417 SourceLocation AliasNameLoc = Tok.getLocation();
418 ConsumeToken();
419 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
420 RedefNameLoc, AliasNameLoc);
421}
422
423void Parser::HandlePragmaFPContract() {
424 assert(Tok.is(tok::annot_pragma_fp_contract));
425 tok::OnOffSwitch OOS =
426 static_cast<tok::OnOffSwitch>(
427 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
428 Actions.ActOnPragmaFPContract(OOS);
429 ConsumeToken(); // The annotation token.
430}
431
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000432StmtResult Parser::HandlePragmaCaptured()
433{
434 assert(Tok.is(tok::annot_pragma_captured));
435 ConsumeToken();
436
437 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000438 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000439 return StmtError();
440 }
441
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000442 SourceLocation Loc = Tok.getLocation();
443
444 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000445 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
446 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000447
448 StmtResult R = ParseCompoundStatement();
449 CapturedRegionScope.Exit();
450
451 if (R.isInvalid()) {
452 Actions.ActOnCapturedRegionError();
453 return StmtError();
454 }
455
456 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000457}
458
Eli Friedman68be1642012-10-04 02:36:51 +0000459namespace {
460 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
461}
462
463void Parser::HandlePragmaOpenCLExtension() {
464 assert(Tok.is(tok::annot_pragma_opencl_extension));
465 OpenCLExtData data =
466 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
467 unsigned state = data.getInt();
468 IdentifierInfo *ename = data.getPointer();
469 SourceLocation NameLoc = Tok.getLocation();
470 ConsumeToken(); // The annotation token.
471
472 OpenCLOptions &f = Actions.getOpenCLOptions();
473 // 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")) {
477#define OPENCLEXT(nm) f.nm = 0;
478#include "clang/Basic/OpenCLExtensions.def"
479 }
480#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
481#include "clang/Basic/OpenCLExtensions.def"
482 else {
483 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
484 return;
485 }
486}
487
David Majnemer4bb09802014-02-10 19:50:15 +0000488void Parser::HandlePragmaMSPointersToMembers() {
489 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000490 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
491 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000492 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
493 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
494 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
495}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000496
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000497void Parser::HandlePragmaMSVtorDisp() {
498 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
499 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
500 Sema::PragmaVtorDispKind Kind =
501 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
502 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
503 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
504 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
505}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000506
Warren Huntc3b18962014-04-08 22:30:47 +0000507void Parser::HandlePragmaMSPragma() {
508 assert(Tok.is(tok::annot_pragma_ms_pragma));
509 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000510 auto TheTokens =
511 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
512 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Warren Huntc3b18962014-04-08 22:30:47 +0000513 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
514 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000515 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000516 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000517
Warren Huntc3b18962014-04-08 22:30:47 +0000518 // Figure out which #pragma we're dealing with. The switch has no default
519 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000520 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000521 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
522 .Case("data_seg", &Parser::HandlePragmaMSSegment)
523 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
524 .Case("const_seg", &Parser::HandlePragmaMSSegment)
525 .Case("code_seg", &Parser::HandlePragmaMSSegment)
526 .Case("section", &Parser::HandlePragmaMSSection)
527 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000528
529 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
530 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
531 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000532 while (Tok.isNot(tok::eof))
533 PP.Lex(Tok);
534 PP.Lex(Tok);
535 }
536}
537
Reid Kleckner722b1df2014-07-18 00:13:16 +0000538bool Parser::HandlePragmaMSSection(StringRef PragmaName,
539 SourceLocation PragmaLocation) {
540 if (Tok.isNot(tok::l_paren)) {
541 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
542 return false;
543 }
Warren Huntc3b18962014-04-08 22:30:47 +0000544 PP.Lex(Tok); // (
545 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000546 if (Tok.isNot(tok::string_literal)) {
547 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
548 << PragmaName;
549 return false;
550 }
551 ExprResult StringResult = ParseStringLiteralExpression();
552 if (StringResult.isInvalid())
553 return false; // Already diagnosed.
554 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
555 if (SegmentName->getCharByteWidth() != 1) {
556 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
557 << PragmaName;
558 return false;
559 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000560 int SectionFlags = ASTContext::PSF_Read;
561 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000562 while (Tok.is(tok::comma)) {
563 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000564 // Ignore "long" and "short".
565 // They are undocumented, but widely used, section attributes which appear
566 // to do nothing.
567 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
568 PP.Lex(Tok); // long/short
569 continue;
570 }
571
Reid Kleckner722b1df2014-07-18 00:13:16 +0000572 if (!Tok.isAnyIdentifier()) {
573 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
574 << PragmaName;
575 return false;
576 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000577 ASTContext::PragmaSectionFlag Flag =
578 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000579 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000580 .Case("read", ASTContext::PSF_Read)
581 .Case("write", ASTContext::PSF_Write)
582 .Case("execute", ASTContext::PSF_Execute)
583 .Case("shared", ASTContext::PSF_Invalid)
584 .Case("nopage", ASTContext::PSF_Invalid)
585 .Case("nocache", ASTContext::PSF_Invalid)
586 .Case("discard", ASTContext::PSF_Invalid)
587 .Case("remove", ASTContext::PSF_Invalid)
588 .Default(ASTContext::PSF_None);
589 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
590 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000591 ? diag::warn_pragma_invalid_specific_action
592 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000593 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000594 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000595 }
596 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000597 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000598 PP.Lex(Tok); // Identifier
599 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000600 // If no section attributes are specified, the section will be marked as
601 // read/write.
602 if (SectionFlagsAreDefault)
603 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000604 if (Tok.isNot(tok::r_paren)) {
605 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
606 return false;
607 }
Warren Huntc3b18962014-04-08 22:30:47 +0000608 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000609 if (Tok.isNot(tok::eof)) {
610 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
611 << PragmaName;
612 return false;
613 }
Warren Huntc3b18962014-04-08 22:30:47 +0000614 PP.Lex(Tok); // eof
615 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000616 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000617}
618
Reid Kleckner722b1df2014-07-18 00:13:16 +0000619bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
620 SourceLocation PragmaLocation) {
621 if (Tok.isNot(tok::l_paren)) {
622 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
623 return false;
624 }
Warren Huntc3b18962014-04-08 22:30:47 +0000625 PP.Lex(Tok); // (
626 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000627 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000628 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000629 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000630 if (PushPop == "push")
631 Action = Sema::PSK_Push;
632 else if (PushPop == "pop")
633 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000634 else {
635 PP.Diag(PragmaLocation,
636 diag::warn_pragma_expected_section_push_pop_or_name)
637 << PragmaName;
638 return false;
639 }
Warren Huntc3b18962014-04-08 22:30:47 +0000640 if (Action != Sema::PSK_Reset) {
641 PP.Lex(Tok); // push | pop
642 if (Tok.is(tok::comma)) {
643 PP.Lex(Tok); // ,
644 // If we've got a comma, we either need a label or a string.
645 if (Tok.isAnyIdentifier()) {
646 SlotLabel = Tok.getIdentifierInfo()->getName();
647 PP.Lex(Tok); // identifier
648 if (Tok.is(tok::comma))
649 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000650 else if (Tok.isNot(tok::r_paren)) {
651 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
652 << PragmaName;
653 return false;
654 }
Warren Huntc3b18962014-04-08 22:30:47 +0000655 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000656 } else if (Tok.isNot(tok::r_paren)) {
657 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
658 return false;
659 }
Warren Huntc3b18962014-04-08 22:30:47 +0000660 }
661 }
662 // Grab the string literal for our section name.
663 StringLiteral *SegmentName = nullptr;
664 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000665 if (Tok.isNot(tok::string_literal)) {
666 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000667 diag::warn_pragma_expected_section_name :
668 diag::warn_pragma_expected_section_label_or_name :
669 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000670 PP.Diag(PragmaLocation, DiagID) << PragmaName;
671 return false;
672 }
673 ExprResult StringResult = ParseStringLiteralExpression();
674 if (StringResult.isInvalid())
675 return false; // Already diagnosed.
676 SegmentName = cast<StringLiteral>(StringResult.get());
677 if (SegmentName->getCharByteWidth() != 1) {
678 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
679 << PragmaName;
680 return false;
681 }
Warren Huntc3b18962014-04-08 22:30:47 +0000682 // Setting section "" has no effect
683 if (SegmentName->getLength())
684 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
685 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000686 if (Tok.isNot(tok::r_paren)) {
687 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
688 return false;
689 }
Warren Huntc3b18962014-04-08 22:30:47 +0000690 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000691 if (Tok.isNot(tok::eof)) {
692 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
693 << PragmaName;
694 return false;
695 }
Warren Huntc3b18962014-04-08 22:30:47 +0000696 PP.Lex(Tok); // eof
697 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
698 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000699 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000700}
701
Reid Kleckner1a711b12014-07-22 00:53:05 +0000702// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000703bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
704 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000705 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
706 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
707 return false;
708 }
709
Reid Kleckner1a711b12014-07-22 00:53:05 +0000710 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
711 PragmaName))
712 return false;
713
714 // Parse either the known section names or the string section name.
715 StringLiteral *SegmentName = nullptr;
716 if (Tok.isAnyIdentifier()) {
717 auto *II = Tok.getIdentifierInfo();
718 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
719 .Case("compiler", "\".CRT$XCC\"")
720 .Case("lib", "\".CRT$XCL\"")
721 .Case("user", "\".CRT$XCU\"")
722 .Default("");
723
724 if (!Section.empty()) {
725 // Pretend the user wrote the appropriate string literal here.
726 Token Toks[1];
727 Toks[0].startToken();
728 Toks[0].setKind(tok::string_literal);
729 Toks[0].setLocation(Tok.getLocation());
730 Toks[0].setLiteralData(Section.data());
731 Toks[0].setLength(Section.size());
732 SegmentName =
733 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
734 PP.Lex(Tok);
735 }
736 } else if (Tok.is(tok::string_literal)) {
737 ExprResult StringResult = ParseStringLiteralExpression();
738 if (StringResult.isInvalid())
739 return false;
740 SegmentName = cast<StringLiteral>(StringResult.get());
741 if (SegmentName->getCharByteWidth() != 1) {
742 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
743 << PragmaName;
744 return false;
745 }
746 // FIXME: Add support for the '[, func-name]' part of the pragma.
747 }
748
749 if (!SegmentName) {
750 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
751 return false;
752 }
753
754 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
755 PragmaName) ||
756 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
757 PragmaName))
758 return false;
759
760 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
761 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000762}
763
Benjamin Kramere003ca22015-10-28 13:54:16 +0000764namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000765struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000766 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000767 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000768 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000769};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000770} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +0000771
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000772static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
773 std::string PragmaString;
774 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
775 PragmaString = "clang loop ";
776 PragmaString += Option.getIdentifierInfo()->getName();
777 } else {
778 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
779 "Unexpected pragma name");
780 PragmaString = "unroll";
781 }
782 return PragmaString;
783}
784
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000785bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000786 assert(Tok.is(tok::annot_pragma_loop_hint));
787 PragmaLoopHintInfo *Info =
788 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
789
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000790 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
791 Hint.PragmaNameLoc = IdentifierLoc::create(
792 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000793
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000794 // It is possible that the loop hint has no option identifier, such as
795 // #pragma unroll(4).
796 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
797 ? Info->Option.getIdentifierInfo()
798 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000799 Hint.OptionLoc = IdentifierLoc::create(
800 Actions.Context, Info->Option.getLocation(), OptionInfo);
801
David Blaikie2eabcc92016-02-09 18:52:09 +0000802 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000803
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000804 // Return a valid hint if pragma unroll or nounroll were specified
805 // without an argument.
806 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
807 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Blaikie2eabcc92016-02-09 18:52:09 +0000808 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000809 ConsumeToken(); // The annotation token.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000810 Hint.Range = Info->PragmaName.getLocation();
811 return true;
812 }
813
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000814 // The constant expression is always followed by an eof token, which increases
815 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +0000816 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000817 "PragmaLoopHintInfo::Toks must contain at least one token.");
818
819 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +0000820 bool OptionUnroll = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000821 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +0000822 if (OptionInfo) { // Pragma Unroll does not specify an option.
823 OptionUnroll = OptionInfo->isStr("unroll");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000824 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
825 .Case("vectorize", true)
826 .Case("interleave", true)
827 .Case("unroll", true)
828 .Default(false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000829 }
830
831 // Verify loop hint has an argument.
832 if (Toks[0].is(tok::eof)) {
833 ConsumeToken(); // The annotation token.
834 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
Tyler Nowicki24853c12015-06-08 23:13:43 +0000835 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll;
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000836 return false;
837 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000838
839 // Validate the argument.
840 if (StateOption) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000841 ConsumeToken(); // The annotation token.
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000842 SourceLocation StateLoc = Toks[0].getLocation();
843 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Tyler Nowicki9d268e12015-06-11 23:23:17 +0000844 if (!StateInfo ||
Mark Heffernan397a98d2015-08-10 17:29:39 +0000845 (!StateInfo->isStr("enable") && !StateInfo->isStr("disable") &&
846 ((OptionUnroll && !StateInfo->isStr("full")) ||
847 (!OptionUnroll && !StateInfo->isStr("assume_safety"))))) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000848 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
849 << /*FullKeyword=*/OptionUnroll;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000850 return false;
851 }
David Blaikie2eabcc92016-02-09 18:52:09 +0000852 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000853 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
854 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000855 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
856 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000857 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000858 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000859 ConsumeToken(); // The annotation token.
860
861 ExprResult R = ParseConstantExpression();
862
863 // Tokens following an error in an ill-formed constant expression will
864 // remain in the token stream and must be removed.
865 if (Tok.isNot(tok::eof)) {
866 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
867 << PragmaLoopHintString(Info->PragmaName, Info->Option);
868 while (Tok.isNot(tok::eof))
869 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000870 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000871
872 ConsumeToken(); // Consume the constant expression eof terminator.
873
874 if (R.isInvalid() ||
875 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
876 return false;
877
878 // Argument is a constant expression with an integer type.
879 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000880 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000881
Tyler Nowickic724a83e2014-10-12 20:46:07 +0000882 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +0000883 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000884 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000885}
886
887// #pragma GCC visibility comes in two variants:
888// 'push' '(' [visibility] ')'
889// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000890void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
891 PragmaIntroducerKind Introducer,
892 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000893 SourceLocation VisLoc = VisTok.getLocation();
894
895 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000896 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000897
898 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
899
Eli Friedman570024a2010-08-05 06:57:20 +0000900 const IdentifierInfo *VisType;
901 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000902 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000903 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000904 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000905 if (Tok.isNot(tok::l_paren)) {
906 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
907 << "visibility";
908 return;
909 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000910 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000911 VisType = Tok.getIdentifierInfo();
912 if (!VisType) {
913 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
914 << "visibility";
915 return;
916 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000917 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000918 if (Tok.isNot(tok::r_paren)) {
919 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
920 << "visibility";
921 return;
922 }
923 } else {
924 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
925 << "visibility";
926 return;
927 }
David Majnemera8f2f1d2015-03-19 00:10:23 +0000928 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000929 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000930 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000931 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
932 << "visibility";
933 return;
934 }
935
David Blaikie2eabcc92016-02-09 18:52:09 +0000936 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +0000937 Toks[0].startToken();
938 Toks[0].setKind(tok::annot_pragma_vis);
939 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +0000940 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +0000941 Toks[0].setAnnotationValue(
942 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +0000943 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000944}
945
Daniel Dunbar921b9682008-10-04 19:21:03 +0000946// #pragma pack(...) comes in the following delicious flavors:
947// pack '(' [integer] ')'
948// pack '(' 'show' ')'
949// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000950void PragmaPackHandler::HandlePragma(Preprocessor &PP,
951 PragmaIntroducerKind Introducer,
952 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000953 SourceLocation PackLoc = PackTok.getLocation();
954
955 Token Tok;
956 PP.Lex(Tok);
957 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000958 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000959 return;
960 }
961
John McCallfaf5fb42010-08-26 23:41:50 +0000962 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000963 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000964 Token Alignment;
965 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000966 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000967 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000968 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000969 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000970
971 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000972
973 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
974 // the push/pop stack.
975 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000976 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000977 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000978 } else if (Tok.is(tok::identifier)) {
979 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000980 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000981 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000982 PP.Lex(Tok);
983 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000984 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000985 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000986 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000987 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000988 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000989 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000990 return;
Mike Stump11289f42009-09-09 15:08:12 +0000991 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000992 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000993
Daniel Dunbar921b9682008-10-04 19:21:03 +0000994 if (Tok.is(tok::comma)) {
995 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000996
Daniel Dunbar921b9682008-10-04 19:21:03 +0000997 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000998 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000999
1000 PP.Lex(Tok);
1001 } else if (Tok.is(tok::identifier)) {
1002 Name = Tok.getIdentifierInfo();
1003 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001004
Daniel Dunbar921b9682008-10-04 19:21:03 +00001005 if (Tok.is(tok::comma)) {
1006 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001007
Daniel Dunbar921b9682008-10-04 19:21:03 +00001008 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001009 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001010 return;
1011 }
Mike Stump11289f42009-09-09 15:08:12 +00001012
Eli Friedman68be1642012-10-04 02:36:51 +00001013 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001014
1015 PP.Lex(Tok);
1016 }
1017 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001018 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001019 return;
1020 }
1021 }
1022 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001023 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001024 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1025 // the push/pop stack.
1026 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1027 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001028 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001029
1030 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001031 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001032 return;
1033 }
1034
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001035 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001036 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001037 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001038 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1039 return;
1040 }
1041
David Blaikie2eabcc92016-02-09 18:52:09 +00001042 PragmaPackInfo *Info =
1043 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001044 Info->Kind = Kind;
1045 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +00001046 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001047 Info->LParenLoc = LParenLoc;
1048 Info->RParenLoc = RParenLoc;
1049
David Blaikie2eabcc92016-02-09 18:52:09 +00001050 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1051 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001052 Toks[0].startToken();
1053 Toks[0].setKind(tok::annot_pragma_pack);
1054 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001055 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001056 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001057 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001058}
1059
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001060// #pragma ms_struct on
1061// #pragma ms_struct off
1062void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1063 PragmaIntroducerKind Introducer,
1064 Token &MSStructTok) {
1065 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
1066
1067 Token Tok;
1068 PP.Lex(Tok);
1069 if (Tok.isNot(tok::identifier)) {
1070 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1071 return;
1072 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001073 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001074 const IdentifierInfo *II = Tok.getIdentifierInfo();
1075 if (II->isStr("on")) {
1076 Kind = Sema::PMSST_ON;
1077 PP.Lex(Tok);
1078 }
1079 else if (II->isStr("off") || II->isStr("reset"))
1080 PP.Lex(Tok);
1081 else {
1082 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1083 return;
1084 }
1085
1086 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001087 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1088 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001089 return;
1090 }
Eli Friedman68be1642012-10-04 02:36:51 +00001091
David Blaikie2eabcc92016-02-09 18:52:09 +00001092 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1093 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001094 Toks[0].startToken();
1095 Toks[0].setKind(tok::annot_pragma_msstruct);
1096 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001097 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001098 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1099 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001100 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001101}
1102
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001103// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1104// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001105static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001106 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001107 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001108
1109 if (IsOptions) {
1110 PP.Lex(Tok);
1111 if (Tok.isNot(tok::identifier) ||
1112 !Tok.getIdentifierInfo()->isStr("align")) {
1113 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1114 return;
1115 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001116 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001117
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001118 PP.Lex(Tok);
1119 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001120 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1121 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001122 return;
1123 }
1124
1125 PP.Lex(Tok);
1126 if (Tok.isNot(tok::identifier)) {
1127 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001128 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001129 return;
1130 }
1131
John McCallfaf5fb42010-08-26 23:41:50 +00001132 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001133 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001134 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001135 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001136 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001137 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001138 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001139 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001140 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001141 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001142 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001143 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001144 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001145 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001146 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001147 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1148 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001149 return;
1150 }
1151
David Majnemera8f2f1d2015-03-19 00:10:23 +00001152 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001153 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001154 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001155 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001156 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001157 return;
1158 }
1159
David Blaikie2eabcc92016-02-09 18:52:09 +00001160 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1161 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001162 Toks[0].startToken();
1163 Toks[0].setKind(tok::annot_pragma_align);
1164 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001165 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001166 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1167 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001168 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001169}
1170
Douglas Gregorc7d65762010-09-09 22:45:38 +00001171void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1172 PragmaIntroducerKind Introducer,
1173 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001174 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001175}
1176
Douglas Gregorc7d65762010-09-09 22:45:38 +00001177void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1178 PragmaIntroducerKind Introducer,
1179 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001180 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001181}
1182
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001183// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001184void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1185 PragmaIntroducerKind Introducer,
1186 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001187 // FIXME: Should we be expanding macros here? My guess is no.
1188 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001189
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001190 // Lex the left '('.
1191 Token Tok;
1192 PP.Lex(Tok);
1193 if (Tok.isNot(tok::l_paren)) {
1194 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1195 return;
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001198 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001199 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001200 SourceLocation RParenLoc;
1201 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001202
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001203 while (true) {
1204 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001205
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001206 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001207 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001208 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001209 LexID = false;
1210 continue;
1211 }
1212
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001213 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001214 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1215 return;
1216 }
Mike Stump11289f42009-09-09 15:08:12 +00001217
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001218 // We are execting a ')' or a ','.
1219 if (Tok.is(tok::comma)) {
1220 LexID = true;
1221 continue;
1222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001224 if (Tok.is(tok::r_paren)) {
1225 RParenLoc = Tok.getLocation();
1226 break;
1227 }
Mike Stump11289f42009-09-09 15:08:12 +00001228
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001229 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001230 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001231 return;
1232 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001233
1234 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001235 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001236 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1237 "unused";
1238 return;
1239 }
1240
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001241 // Verify that we have a location for the right parenthesis.
1242 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001243 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001244
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001245 // For each identifier token, insert into the token stream a
1246 // annot_pragma_unused token followed by the identifier token.
1247 // This allows us to cache a "#pragma unused" that occurs inside an inline
1248 // C++ member function.
1249
David Blaikie2eabcc92016-02-09 18:52:09 +00001250 MutableArrayRef<Token> Toks(
1251 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1252 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001253 for (unsigned i=0; i != Identifiers.size(); i++) {
1254 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1255 pragmaUnusedTok.startToken();
1256 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1257 pragmaUnusedTok.setLocation(UnusedLoc);
1258 idTok = Identifiers[i];
1259 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001260 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001261}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001262
1263// #pragma weak identifier
1264// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001265void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1266 PragmaIntroducerKind Introducer,
1267 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001268 SourceLocation WeakLoc = WeakTok.getLocation();
1269
1270 Token Tok;
1271 PP.Lex(Tok);
1272 if (Tok.isNot(tok::identifier)) {
1273 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1274 return;
1275 }
1276
Eli Friedman68be1642012-10-04 02:36:51 +00001277 Token WeakName = Tok;
1278 bool HasAlias = false;
1279 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001280
1281 PP.Lex(Tok);
1282 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001283 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001284 PP.Lex(Tok);
1285 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001286 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001287 << "weak";
1288 return;
1289 }
Eli Friedman68be1642012-10-04 02:36:51 +00001290 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001291 PP.Lex(Tok);
1292 }
1293
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001294 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001295 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1296 return;
1297 }
1298
Eli Friedman68be1642012-10-04 02:36:51 +00001299 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00001300 MutableArrayRef<Token> Toks(
1301 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001302 Token &pragmaUnusedTok = Toks[0];
1303 pragmaUnusedTok.startToken();
1304 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1305 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001306 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001307 Toks[1] = WeakName;
1308 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001309 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001310 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00001311 MutableArrayRef<Token> Toks(
1312 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00001313 Token &pragmaUnusedTok = Toks[0];
1314 pragmaUnusedTok.startToken();
1315 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1316 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001317 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001318 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001319 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001320 }
1321}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001322
David Chisnall0867d9c2012-02-18 16:12:34 +00001323// #pragma redefine_extname identifier identifier
1324void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1325 PragmaIntroducerKind Introducer,
1326 Token &RedefToken) {
1327 SourceLocation RedefLoc = RedefToken.getLocation();
1328
1329 Token Tok;
1330 PP.Lex(Tok);
1331 if (Tok.isNot(tok::identifier)) {
1332 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1333 "redefine_extname";
1334 return;
1335 }
1336
Eli Friedman68be1642012-10-04 02:36:51 +00001337 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001338 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001339
David Chisnall0867d9c2012-02-18 16:12:34 +00001340 if (Tok.isNot(tok::identifier)) {
1341 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1342 << "redefine_extname";
1343 return;
1344 }
Eli Friedman68be1642012-10-04 02:36:51 +00001345
1346 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001347 PP.Lex(Tok);
1348
1349 if (Tok.isNot(tok::eod)) {
1350 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1351 "redefine_extname";
1352 return;
1353 }
1354
David Blaikie2eabcc92016-02-09 18:52:09 +00001355 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1356 3);
Eli Friedman68be1642012-10-04 02:36:51 +00001357 Token &pragmaRedefTok = Toks[0];
1358 pragmaRedefTok.startToken();
1359 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1360 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001361 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001362 Toks[1] = RedefName;
1363 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00001364 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
David Chisnall0867d9c2012-02-18 16:12:34 +00001365}
1366
1367
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001368void
1369PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1370 PragmaIntroducerKind Introducer,
1371 Token &Tok) {
1372 tok::OnOffSwitch OOS;
1373 if (PP.LexOnOffSwitch(OOS))
1374 return;
1375
David Blaikie2eabcc92016-02-09 18:52:09 +00001376 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1377 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001378 Toks[0].startToken();
1379 Toks[0].setKind(tok::annot_pragma_fp_contract);
1380 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001381 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00001382 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1383 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001384 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001385}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001386
1387void
1388PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1389 PragmaIntroducerKind Introducer,
1390 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001391 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001392 if (Tok.isNot(tok::identifier)) {
1393 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1394 "OPENCL";
1395 return;
1396 }
1397 IdentifierInfo *ename = Tok.getIdentifierInfo();
1398 SourceLocation NameLoc = Tok.getLocation();
1399
1400 PP.Lex(Tok);
1401 if (Tok.isNot(tok::colon)) {
1402 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1403 return;
1404 }
1405
1406 PP.Lex(Tok);
1407 if (Tok.isNot(tok::identifier)) {
1408 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1409 return;
1410 }
1411 IdentifierInfo *op = Tok.getIdentifierInfo();
1412
1413 unsigned state;
1414 if (op->isStr("enable")) {
1415 state = 1;
1416 } else if (op->isStr("disable")) {
1417 state = 0;
1418 } else {
1419 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1420 return;
1421 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001422 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001423
Eli Friedman68be1642012-10-04 02:36:51 +00001424 PP.Lex(Tok);
1425 if (Tok.isNot(tok::eod)) {
1426 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1427 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001428 return;
1429 }
Eli Friedman68be1642012-10-04 02:36:51 +00001430
1431 OpenCLExtData data(ename, state);
David Blaikie2eabcc92016-02-09 18:52:09 +00001432 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1433 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001434 Toks[0].startToken();
1435 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1436 Toks[0].setLocation(NameLoc);
1437 Toks[0].setAnnotationValue(data.getOpaqueValue());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001438 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00001439 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001440
1441 if (PP.getPPCallbacks())
1442 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1443 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001444}
1445
Alexey Bataeva769e072013-03-22 06:34:35 +00001446/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1447///
1448void
1449PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1450 PragmaIntroducerKind Introducer,
1451 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001452 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1453 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001454 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001455 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1456 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001457 }
1458 PP.DiscardUntilEndOfDirective();
1459}
1460
1461/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1462///
1463void
1464PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1465 PragmaIntroducerKind Introducer,
1466 Token &FirstTok) {
1467 SmallVector<Token, 16> Pragma;
1468 Token Tok;
1469 Tok.startToken();
1470 Tok.setKind(tok::annot_pragma_openmp);
1471 Tok.setLocation(FirstTok.getLocation());
1472
1473 while (Tok.isNot(tok::eod)) {
1474 Pragma.push_back(Tok);
1475 PP.Lex(Tok);
1476 }
1477 SourceLocation EodLoc = Tok.getLocation();
1478 Tok.startToken();
1479 Tok.setKind(tok::annot_pragma_openmp_end);
1480 Tok.setLocation(EodLoc);
1481 Pragma.push_back(Tok);
1482
David Blaikie2eabcc92016-02-09 18:52:09 +00001483 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
1484 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
1485 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
1486 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00001487}
Reid Kleckner002562a2013-05-06 21:02:12 +00001488
David Majnemer4bb09802014-02-10 19:50:15 +00001489/// \brief Handle '#pragma pointers_to_members'
1490// The grammar for this pragma is as follows:
1491//
1492// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1493//
1494// #pragma pointers_to_members '(' 'best_case' ')'
1495// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1496// #pragma pointers_to_members '(' inheritance-model ')'
1497void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1498 PragmaIntroducerKind Introducer,
1499 Token &Tok) {
1500 SourceLocation PointersToMembersLoc = Tok.getLocation();
1501 PP.Lex(Tok);
1502 if (Tok.isNot(tok::l_paren)) {
1503 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1504 << "pointers_to_members";
1505 return;
1506 }
1507 PP.Lex(Tok);
1508 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1509 if (!Arg) {
1510 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1511 << "pointers_to_members";
1512 return;
1513 }
1514 PP.Lex(Tok);
1515
David Majnemer86c318f2014-02-11 21:05:00 +00001516 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001517 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001518 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001519 } else {
1520 if (Arg->isStr("full_generality")) {
1521 if (Tok.is(tok::comma)) {
1522 PP.Lex(Tok);
1523
1524 Arg = Tok.getIdentifierInfo();
1525 if (!Arg) {
1526 PP.Diag(Tok.getLocation(),
1527 diag::err_pragma_pointers_to_members_unknown_kind)
1528 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1529 return;
1530 }
1531 PP.Lex(Tok);
1532 } else if (Tok.is(tok::r_paren)) {
1533 // #pragma pointers_to_members(full_generality) implicitly specifies
1534 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001535 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001536 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001537 } else {
1538 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1539 << "full_generality";
1540 return;
1541 }
1542 }
1543
1544 if (Arg) {
1545 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001546 RepresentationMethod =
1547 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001548 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001549 RepresentationMethod =
1550 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001551 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001552 RepresentationMethod =
1553 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001554 } else {
1555 PP.Diag(Tok.getLocation(),
1556 diag::err_pragma_pointers_to_members_unknown_kind)
1557 << Arg << /*HasPointerDeclaration*/ 1;
1558 return;
1559 }
1560 }
1561 }
1562
1563 if (Tok.isNot(tok::r_paren)) {
1564 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1565 << (Arg ? Arg->getName() : "full_generality");
1566 return;
1567 }
1568
David Majnemera8f2f1d2015-03-19 00:10:23 +00001569 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00001570 PP.Lex(Tok);
1571 if (Tok.isNot(tok::eod)) {
1572 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1573 << "pointers_to_members";
1574 return;
1575 }
1576
1577 Token AnnotTok;
1578 AnnotTok.startToken();
1579 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1580 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001581 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00001582 AnnotTok.setAnnotationValue(
1583 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1584 PP.EnterToken(AnnotTok);
1585}
1586
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001587/// \brief Handle '#pragma vtordisp'
1588// The grammar for this pragma is as follows:
1589//
1590// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1591//
1592// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1593// #pragma vtordisp '(' 'pop' ')'
1594// #pragma vtordisp '(' ')'
1595void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1596 PragmaIntroducerKind Introducer,
1597 Token &Tok) {
1598 SourceLocation VtorDispLoc = Tok.getLocation();
1599 PP.Lex(Tok);
1600 if (Tok.isNot(tok::l_paren)) {
1601 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1602 return;
1603 }
1604 PP.Lex(Tok);
1605
1606 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1607 const IdentifierInfo *II = Tok.getIdentifierInfo();
1608 if (II) {
1609 if (II->isStr("push")) {
1610 // #pragma vtordisp(push, mode)
1611 PP.Lex(Tok);
1612 if (Tok.isNot(tok::comma)) {
1613 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1614 return;
1615 }
1616 PP.Lex(Tok);
1617 Kind = Sema::PVDK_Push;
1618 // not push, could be on/off
1619 } else if (II->isStr("pop")) {
1620 // #pragma vtordisp(pop)
1621 PP.Lex(Tok);
1622 Kind = Sema::PVDK_Pop;
1623 }
1624 // not push or pop, could be on/off
1625 } else {
1626 if (Tok.is(tok::r_paren)) {
1627 // #pragma vtordisp()
1628 Kind = Sema::PVDK_Reset;
1629 }
1630 }
1631
1632
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001633 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001634 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1635 const IdentifierInfo *II = Tok.getIdentifierInfo();
1636 if (II && II->isStr("off")) {
1637 PP.Lex(Tok);
1638 Value = 0;
1639 } else if (II && II->isStr("on")) {
1640 PP.Lex(Tok);
1641 Value = 1;
1642 } else if (Tok.is(tok::numeric_constant) &&
1643 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1644 if (Value > 2) {
1645 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1646 << 0 << 2 << "vtordisp";
1647 return;
1648 }
1649 } else {
1650 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1651 << "vtordisp";
1652 return;
1653 }
1654 }
1655
1656 // Finish the pragma: ')' $
1657 if (Tok.isNot(tok::r_paren)) {
1658 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1659 return;
1660 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001661 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001662 PP.Lex(Tok);
1663 if (Tok.isNot(tok::eod)) {
1664 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1665 << "vtordisp";
1666 return;
1667 }
1668
1669 // Enter the annotation.
1670 Token AnnotTok;
1671 AnnotTok.startToken();
1672 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1673 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001674 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001675 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1676 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001677 PP.EnterToken(AnnotTok);
1678}
1679
Warren Huntc3b18962014-04-08 22:30:47 +00001680/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1681/// an annotation token.
1682void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1683 PragmaIntroducerKind Introducer,
1684 Token &Tok) {
1685 Token EoF, AnnotTok;
1686 EoF.startToken();
1687 EoF.setKind(tok::eof);
1688 AnnotTok.startToken();
1689 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1690 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001691 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00001692 SmallVector<Token, 8> TokenVector;
1693 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00001694 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00001695 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001696 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1697 }
Warren Huntc3b18962014-04-08 22:30:47 +00001698 // Add a sentinal EoF token to the end of the list.
1699 TokenVector.push_back(EoF);
1700 // We must allocate this array with new because EnterTokenStream is going to
1701 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00001702 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
1703 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00001704 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00001705 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
1706 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00001707 AnnotTok.setAnnotationValue(Value);
1708 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001709}
1710
Aaron Ballman5d041be2013-06-04 02:07:14 +00001711/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1712///
1713/// The syntax is:
1714/// \code
1715/// #pragma detect_mismatch("name", "value")
1716/// \endcode
1717/// Where 'name' and 'value' are quoted strings. The values are embedded in
1718/// the object file and passed along to the linker. If the linker detects a
1719/// mismatch in the object file's values for the given name, a LNK2038 error
1720/// is emitted. See MSDN for more details.
1721void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1722 PragmaIntroducerKind Introducer,
1723 Token &Tok) {
1724 SourceLocation CommentLoc = Tok.getLocation();
1725 PP.Lex(Tok);
1726 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001727 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001728 return;
1729 }
1730
1731 // Read the name to embed, which must be a string literal.
1732 std::string NameString;
1733 if (!PP.LexStringLiteral(Tok, NameString,
1734 "pragma detect_mismatch",
1735 /*MacroExpansion=*/true))
1736 return;
1737
1738 // Read the comma followed by a second string literal.
1739 std::string ValueString;
1740 if (Tok.isNot(tok::comma)) {
1741 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1742 return;
1743 }
1744
1745 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1746 /*MacroExpansion=*/true))
1747 return;
1748
1749 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001750 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001751 return;
1752 }
1753 PP.Lex(Tok); // Eat the r_paren.
1754
1755 if (Tok.isNot(tok::eod)) {
1756 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1757 return;
1758 }
1759
Reid Kleckner71966c92014-02-20 23:37:45 +00001760 // If the pragma is lexically sound, notify any interested PPCallbacks.
1761 if (PP.getPPCallbacks())
1762 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1763 ValueString);
1764
Aaron Ballman5d041be2013-06-04 02:07:14 +00001765 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1766}
1767
Reid Kleckner002562a2013-05-06 21:02:12 +00001768/// \brief Handle the microsoft \#pragma comment extension.
1769///
1770/// The syntax is:
1771/// \code
1772/// #pragma comment(linker, "foo")
1773/// \endcode
1774/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1775/// "foo" is a string, which is fully macro expanded, and permits string
1776/// concatenation, embedded escape characters etc. See MSDN for more details.
1777void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1778 PragmaIntroducerKind Introducer,
1779 Token &Tok) {
1780 SourceLocation CommentLoc = Tok.getLocation();
1781 PP.Lex(Tok);
1782 if (Tok.isNot(tok::l_paren)) {
1783 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1784 return;
1785 }
1786
1787 // Read the identifier.
1788 PP.Lex(Tok);
1789 if (Tok.isNot(tok::identifier)) {
1790 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1791 return;
1792 }
1793
1794 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001795 IdentifierInfo *II = Tok.getIdentifierInfo();
1796 Sema::PragmaMSCommentKind Kind =
1797 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1798 .Case("linker", Sema::PCK_Linker)
1799 .Case("lib", Sema::PCK_Lib)
1800 .Case("compiler", Sema::PCK_Compiler)
1801 .Case("exestr", Sema::PCK_ExeStr)
1802 .Case("user", Sema::PCK_User)
1803 .Default(Sema::PCK_Unknown);
1804 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001805 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1806 return;
1807 }
1808
Yunzhong Gao99efc032015-03-23 20:41:42 +00001809 // On PS4, issue a warning about any pragma comments other than
1810 // #pragma comment lib.
1811 if (PP.getTargetInfo().getTriple().isPS4() && Kind != Sema::PCK_Lib) {
1812 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1813 << II->getName();
1814 return;
1815 }
1816
Reid Kleckner002562a2013-05-06 21:02:12 +00001817 // Read the optional string if present.
1818 PP.Lex(Tok);
1819 std::string ArgumentString;
1820 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1821 "pragma comment",
1822 /*MacroExpansion=*/true))
1823 return;
1824
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001825 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001826 // FIXME: If the kind is "compiler" warn if the string is present (it is
1827 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001828 // The MSDN docs say that "lib" and "linker" require a string and have a short
1829 // whitelist of linker options they support, but in practice MSVC doesn't
1830 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001831
1832 if (Tok.isNot(tok::r_paren)) {
1833 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1834 return;
1835 }
1836 PP.Lex(Tok); // eat the r_paren.
1837
1838 if (Tok.isNot(tok::eod)) {
1839 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1840 return;
1841 }
1842
Reid Kleckner71966c92014-02-20 23:37:45 +00001843 // If the pragma is lexically sound, notify any interested PPCallbacks.
1844 if (PP.getPPCallbacks())
1845 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1846
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001847 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001848}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001849
1850// #pragma clang optimize off
1851// #pragma clang optimize on
1852void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1853 PragmaIntroducerKind Introducer,
1854 Token &FirstToken) {
1855 Token Tok;
1856 PP.Lex(Tok);
1857 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001858 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001859 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001860 return;
1861 }
1862 if (Tok.isNot(tok::identifier)) {
1863 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1864 << PP.getSpelling(Tok);
1865 return;
1866 }
1867 const IdentifierInfo *II = Tok.getIdentifierInfo();
1868 // The only accepted values are 'on' or 'off'.
1869 bool IsOn = false;
1870 if (II->isStr("on")) {
1871 IsOn = true;
1872 } else if (!II->isStr("off")) {
1873 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1874 << PP.getSpelling(Tok);
1875 return;
1876 }
1877 PP.Lex(Tok);
1878
1879 if (Tok.isNot(tok::eod)) {
1880 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1881 << PP.getSpelling(Tok);
1882 return;
1883 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001884
1885 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1886}
1887
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001888/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001889static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1890 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001891 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001892 SmallVector<Token, 1> ValueList;
1893 int OpenParens = ValueInParens ? 1 : 0;
1894 // Read constant expression.
1895 while (Tok.isNot(tok::eod)) {
1896 if (Tok.is(tok::l_paren))
1897 OpenParens++;
1898 else if (Tok.is(tok::r_paren)) {
1899 OpenParens--;
1900 if (OpenParens == 0 && ValueInParens)
1901 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001902 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001903
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001904 ValueList.push_back(Tok);
1905 PP.Lex(Tok);
1906 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001907
1908 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001909 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001910 if (Tok.isNot(tok::r_paren)) {
1911 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1912 return true;
1913 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001914 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001915 }
1916
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001917 Token EOFTok;
1918 EOFTok.startToken();
1919 EOFTok.setKind(tok::eof);
1920 EOFTok.setLocation(Tok.getLocation());
1921 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1922
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001923 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001924
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001925 Info.PragmaName = PragmaName;
1926 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001927 return false;
1928}
1929
Eli Bendersky06a40422014-06-06 20:31:48 +00001930/// \brief Handle the \#pragma clang loop directive.
1931/// #pragma clang 'loop' loop-hints
1932///
1933/// loop-hints:
1934/// loop-hint loop-hints[opt]
1935///
1936/// loop-hint:
1937/// 'vectorize' '(' loop-hint-keyword ')'
1938/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001939/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001940/// 'vectorize_width' '(' loop-hint-value ')'
1941/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001942/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001943///
1944/// loop-hint-keyword:
1945/// 'enable'
1946/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00001947/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00001948///
Mark Heffernan450c2382014-07-23 17:31:31 +00001949/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00001950/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00001951/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00001952/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00001953///
Eli Bendersky06a40422014-06-06 20:31:48 +00001954/// loop-hint-value:
1955/// constant-expression
1956///
1957/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1958/// try vectorizing the instructions of the loop it precedes. Specifying
1959/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1960/// interleaving multiple iterations of the loop it precedes. The width of the
1961/// vector instructions is specified by vectorize_width() and the number of
1962/// interleaved loop iterations is specified by interleave_count(). Specifying a
1963/// value of 1 effectively disables vectorization/interleaving, even if it is
1964/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1965/// only works on inner loops.
1966///
Eli Bendersky86483b32014-06-11 17:56:26 +00001967/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00001968/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
1969/// completely if the trip count is known at compile time and unroll partially
1970/// if the trip count is not known. Specifying unroll(full) is similar to
1971/// unroll(enable) but will unroll the loop only if the trip count is known at
1972/// compile time. Specifying unroll(disable) disables unrolling for the
1973/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
1974/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001975void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1976 PragmaIntroducerKind Introducer,
1977 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001978 // Incoming token is "loop" from "#pragma clang loop".
1979 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001980 SmallVector<Token, 1> TokenList;
1981
1982 // Lex the optimization option and verify it is an identifier.
1983 PP.Lex(Tok);
1984 if (Tok.isNot(tok::identifier)) {
1985 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1986 << /*MissingOption=*/true << "";
1987 return;
1988 }
1989
1990 while (Tok.is(tok::identifier)) {
1991 Token Option = Tok;
1992 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1993
Eli Bendersky86483b32014-06-11 17:56:26 +00001994 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001995 .Case("vectorize", true)
1996 .Case("interleave", true)
1997 .Case("unroll", true)
1998 .Case("vectorize_width", true)
1999 .Case("interleave_count", true)
2000 .Case("unroll_count", true)
2001 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002002 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002003 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2004 << /*MissingOption=*/false << OptionInfo;
2005 return;
2006 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002007 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002008
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002009 // Read '('
2010 if (Tok.isNot(tok::l_paren)) {
2011 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002012 return;
2013 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002014 PP.Lex(Tok);
2015
2016 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2017 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2018 *Info))
2019 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002020
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002021 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002022 Token LoopHintTok;
2023 LoopHintTok.startToken();
2024 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002025 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002026 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002027 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2028 TokenList.push_back(LoopHintTok);
2029 }
2030
2031 if (Tok.isNot(tok::eod)) {
2032 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2033 << "clang loop";
2034 return;
2035 }
2036
David Blaikie2eabcc92016-02-09 18:52:09 +00002037 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2038 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002039
David Blaikie2eabcc92016-02-09 18:52:09 +00002040 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2041 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002042}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002043
2044/// \brief Handle the loop unroll optimization pragmas.
2045/// #pragma unroll
2046/// #pragma unroll unroll-hint-value
2047/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002048/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002049///
2050/// unroll-hint-value:
2051/// constant-expression
2052///
Mark Heffernanc888e412014-07-24 18:09:38 +00002053/// Loop unrolling hints can be specified with '#pragma unroll' or
2054/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2055/// contained in parentheses. With no argument the directive instructs llvm to
2056/// try to unroll the loop completely. A positive integer argument can be
2057/// specified to indicate the number of times the loop should be unrolled. To
2058/// maximize compatibility with other compilers the unroll count argument can be
2059/// specified with or without parentheses. Specifying, '#pragma nounroll'
2060/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002061void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2062 PragmaIntroducerKind Introducer,
2063 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002064 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2065 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002066 Token PragmaName = Tok;
2067 PP.Lex(Tok);
2068 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2069 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002070 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002071 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002072 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00002073 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2074 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2075 << "nounroll";
2076 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002077 } else {
2078 // Unroll pragma with an argument: "#pragma unroll N" or
2079 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002080 // Read '(' if it exists.
2081 bool ValueInParens = Tok.is(tok::l_paren);
2082 if (ValueInParens)
2083 PP.Lex(Tok);
2084
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002085 Token Option;
2086 Option.startToken();
2087 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002088 return;
2089
2090 // In CUDA, the argument to '#pragma unroll' should not be contained in
2091 // parentheses.
2092 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002093 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002094 diag::warn_pragma_unroll_cuda_value_in_parens);
2095
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002096 if (Tok.isNot(tok::eod)) {
2097 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2098 << "unroll";
2099 return;
2100 }
2101 }
2102
2103 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002104 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002105 TokenArray[0].startToken();
2106 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2107 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002108 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002109 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002110 PP.EnterTokenStream(std::move(TokenArray), 1,
2111 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002112}