blob: 96484b496d15be3784e0a26654b1646bdc4635d5 [file] [log] [blame]
Daniel Dunbarfcdd8fe2008-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
Stephen Hines6bcf27b2014-05-29 04:14:42 -070014#include "RAIIObjectsForParser.h"
Stephen Hines176edba2014-12-01 14:53:08 -080015#include "clang/AST/ASTContext.h"
16#include "clang/Basic/TargetInfo.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000017#include "clang/Lex/Preprocessor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000018#include "clang/Parse/ParseDiagnostic.h"
Ted Kremenek4726d032009-03-23 22:28:25 +000019#include "clang/Parse/Parser.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070020#include "clang/Sema/LoopHint.h"
Tareq A. Siraj6afcf882013-04-16 19:37:38 +000021#include "clang/Sema/Scope.h"
Reid Kleckner7adf79a2013-05-06 21:02:12 +000022#include "llvm/ADT/StringSwitch.h"
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +000023using namespace clang;
24
Stephen Hines651f13c2014-04-23 16:59:28 -070025namespace {
26
27struct PragmaAlignHandler : public PragmaHandler {
28 explicit PragmaAlignHandler() : PragmaHandler("align") {}
29 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
30 Token &FirstToken) override;
31};
32
33struct PragmaGCCVisibilityHandler : public PragmaHandler {
34 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
35 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
36 Token &FirstToken) override;
37};
38
39struct PragmaOptionsHandler : public PragmaHandler {
40 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
41 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
42 Token &FirstToken) override;
43};
44
45struct PragmaPackHandler : public PragmaHandler {
46 explicit PragmaPackHandler() : PragmaHandler("pack") {}
47 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
48 Token &FirstToken) override;
49};
50
51struct PragmaMSStructHandler : public PragmaHandler {
52 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
53 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
54 Token &FirstToken) override;
55};
56
57struct PragmaUnusedHandler : public PragmaHandler {
58 PragmaUnusedHandler() : PragmaHandler("unused") {}
59 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
60 Token &FirstToken) override;
61};
62
63struct PragmaWeakHandler : public PragmaHandler {
64 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
65 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
66 Token &FirstToken) override;
67};
68
69struct PragmaRedefineExtnameHandler : public PragmaHandler {
70 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
71 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
72 Token &FirstToken) override;
73};
74
75struct PragmaOpenCLExtensionHandler : public PragmaHandler {
76 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
77 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
78 Token &FirstToken) override;
79};
80
81
82struct PragmaFPContractHandler : public PragmaHandler {
83 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
84 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
85 Token &FirstToken) override;
86};
87
88struct PragmaNoOpenMPHandler : public PragmaHandler {
89 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
90 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
91 Token &FirstToken) override;
92};
93
94struct PragmaOpenMPHandler : public PragmaHandler {
95 PragmaOpenMPHandler() : PragmaHandler("omp") { }
96 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
97 Token &FirstToken) override;
98};
99
100/// PragmaCommentHandler - "\#pragma comment ...".
101struct PragmaCommentHandler : public PragmaHandler {
102 PragmaCommentHandler(Sema &Actions)
103 : PragmaHandler("comment"), Actions(Actions) {}
104 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
105 Token &FirstToken) override;
106private:
107 Sema &Actions;
108};
109
110struct PragmaDetectMismatchHandler : public PragmaHandler {
111 PragmaDetectMismatchHandler(Sema &Actions)
112 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
113 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
114 Token &FirstToken) override;
115private:
116 Sema &Actions;
117};
118
119struct PragmaMSPointersToMembers : public PragmaHandler {
120 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
121 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
122 Token &FirstToken) override;
123};
124
125struct PragmaMSVtorDisp : public PragmaHandler {
126 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
127 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
128 Token &FirstToken) override;
129};
130
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700131struct PragmaMSPragma : public PragmaHandler {
132 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
133 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
134 Token &FirstToken) override;
135};
136
137/// 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:
144 Sema &Actions;
145};
146
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700147struct PragmaLoopHintHandler : public PragmaHandler {
148 PragmaLoopHintHandler() : PragmaHandler("loop") {}
149 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
150 Token &FirstToken) override;
151};
152
Stephen Hines176edba2014-12-01 14:53:08 -0800153struct PragmaUnrollHintHandler : public PragmaHandler {
154 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
155 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
156 Token &FirstToken) override;
157};
158
Stephen Hines651f13c2014-04-23 16:59:28 -0700159} // end namespace
160
161void Parser::initializePragmaHandlers() {
162 AlignHandler.reset(new PragmaAlignHandler());
163 PP.AddPragmaHandler(AlignHandler.get());
164
165 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
166 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
167
168 OptionsHandler.reset(new PragmaOptionsHandler());
169 PP.AddPragmaHandler(OptionsHandler.get());
170
171 PackHandler.reset(new PragmaPackHandler());
172 PP.AddPragmaHandler(PackHandler.get());
173
174 MSStructHandler.reset(new PragmaMSStructHandler());
175 PP.AddPragmaHandler(MSStructHandler.get());
176
177 UnusedHandler.reset(new PragmaUnusedHandler());
178 PP.AddPragmaHandler(UnusedHandler.get());
179
180 WeakHandler.reset(new PragmaWeakHandler());
181 PP.AddPragmaHandler(WeakHandler.get());
182
183 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
184 PP.AddPragmaHandler(RedefineExtnameHandler.get());
185
186 FPContractHandler.reset(new PragmaFPContractHandler());
187 PP.AddPragmaHandler("STDC", FPContractHandler.get());
188
189 if (getLangOpts().OpenCL) {
190 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
191 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
192
193 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
194 }
195 if (getLangOpts().OpenMP)
196 OpenMPHandler.reset(new PragmaOpenMPHandler());
197 else
198 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
199 PP.AddPragmaHandler(OpenMPHandler.get());
200
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700201 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700202 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
203 PP.AddPragmaHandler(MSCommentHandler.get());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700204 }
205
206 if (getLangOpts().MicrosoftExt) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700207 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
208 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
209 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
210 PP.AddPragmaHandler(MSPointersToMembers.get());
211 MSVtorDisp.reset(new PragmaMSVtorDisp());
212 PP.AddPragmaHandler(MSVtorDisp.get());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700213 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
214 PP.AddPragmaHandler(MSInitSeg.get());
215 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
216 PP.AddPragmaHandler(MSDataSeg.get());
217 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
218 PP.AddPragmaHandler(MSBSSSeg.get());
219 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
220 PP.AddPragmaHandler(MSConstSeg.get());
221 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
222 PP.AddPragmaHandler(MSCodeSeg.get());
223 MSSection.reset(new PragmaMSPragma("section"));
224 PP.AddPragmaHandler(MSSection.get());
Stephen Hines651f13c2014-04-23 16:59:28 -0700225 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700226
227 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
228 PP.AddPragmaHandler("clang", OptimizeHandler.get());
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700229
230 LoopHintHandler.reset(new PragmaLoopHintHandler());
231 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Stephen Hines176edba2014-12-01 14:53:08 -0800232
233 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
234 PP.AddPragmaHandler(UnrollHintHandler.get());
235
236 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
237 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Stephen Hines651f13c2014-04-23 16:59:28 -0700238}
239
240void Parser::resetPragmaHandlers() {
241 // Remove the pragma handlers we installed.
242 PP.RemovePragmaHandler(AlignHandler.get());
243 AlignHandler.reset();
244 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
245 GCCVisibilityHandler.reset();
246 PP.RemovePragmaHandler(OptionsHandler.get());
247 OptionsHandler.reset();
248 PP.RemovePragmaHandler(PackHandler.get());
249 PackHandler.reset();
250 PP.RemovePragmaHandler(MSStructHandler.get());
251 MSStructHandler.reset();
252 PP.RemovePragmaHandler(UnusedHandler.get());
253 UnusedHandler.reset();
254 PP.RemovePragmaHandler(WeakHandler.get());
255 WeakHandler.reset();
256 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
257 RedefineExtnameHandler.reset();
258
259 if (getLangOpts().OpenCL) {
260 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
261 OpenCLExtensionHandler.reset();
262 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
263 }
264 PP.RemovePragmaHandler(OpenMPHandler.get());
265 OpenMPHandler.reset();
266
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700267 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700268 PP.RemovePragmaHandler(MSCommentHandler.get());
269 MSCommentHandler.reset();
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700270 }
271
272 if (getLangOpts().MicrosoftExt) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700273 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
274 MSDetectMismatchHandler.reset();
275 PP.RemovePragmaHandler(MSPointersToMembers.get());
276 MSPointersToMembers.reset();
277 PP.RemovePragmaHandler(MSVtorDisp.get());
278 MSVtorDisp.reset();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700279 PP.RemovePragmaHandler(MSInitSeg.get());
280 MSInitSeg.reset();
281 PP.RemovePragmaHandler(MSDataSeg.get());
282 MSDataSeg.reset();
283 PP.RemovePragmaHandler(MSBSSSeg.get());
284 MSBSSSeg.reset();
285 PP.RemovePragmaHandler(MSConstSeg.get());
286 MSConstSeg.reset();
287 PP.RemovePragmaHandler(MSCodeSeg.get());
288 MSCodeSeg.reset();
289 PP.RemovePragmaHandler(MSSection.get());
290 MSSection.reset();
Stephen Hines651f13c2014-04-23 16:59:28 -0700291 }
292
293 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
294 FPContractHandler.reset();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700295
296 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
297 OptimizeHandler.reset();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700298
299 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
300 LoopHintHandler.reset();
Stephen Hines176edba2014-12-01 14:53:08 -0800301
302 PP.RemovePragmaHandler(UnrollHintHandler.get());
303 UnrollHintHandler.reset();
304
305 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
306 NoUnrollHintHandler.reset();
Stephen Hines651f13c2014-04-23 16:59:28 -0700307}
308
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +0000309/// \brief Handle the annotation token produced for #pragma unused(...)
310///
311/// Each annot_pragma_unused is followed by the argument token so e.g.
312/// "#pragma unused(x,y)" becomes:
313/// annot_pragma_unused 'x' annot_pragma_unused 'y'
314void Parser::HandlePragmaUnused() {
315 assert(Tok.is(tok::annot_pragma_unused));
316 SourceLocation UnusedLoc = ConsumeToken();
317 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
318 ConsumeToken(); // The argument token.
319}
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000320
Rafael Espindola426fc942012-01-26 02:02:57 +0000321void Parser::HandlePragmaVisibility() {
322 assert(Tok.is(tok::annot_pragma_vis));
323 const IdentifierInfo *VisType =
324 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
325 SourceLocation VisLoc = ConsumeToken();
326 Actions.ActOnPragmaVisibility(VisType, VisLoc);
327}
328
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000329struct PragmaPackInfo {
330 Sema::PragmaPackKind Kind;
331 IdentifierInfo *Name;
Eli Friedman9595c7e2012-10-04 02:36:51 +0000332 Token Alignment;
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000333 SourceLocation LParenLoc;
334 SourceLocation RParenLoc;
335};
336
337void Parser::HandlePragmaPack() {
338 assert(Tok.is(tok::annot_pragma_pack));
339 PragmaPackInfo *Info =
340 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
341 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman9595c7e2012-10-04 02:36:51 +0000342 ExprResult Alignment;
343 if (Info->Alignment.is(tok::numeric_constant)) {
344 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
345 if (Alignment.isInvalid())
346 return;
347 }
348 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000349 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanaa5ab262012-02-23 23:47:16 +0000350}
351
Eli Friedman9595c7e2012-10-04 02:36:51 +0000352void Parser::HandlePragmaMSStruct() {
353 assert(Tok.is(tok::annot_pragma_msstruct));
354 Sema::PragmaMSStructKind Kind =
355 static_cast<Sema::PragmaMSStructKind>(
356 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
357 Actions.ActOnPragmaMSStruct(Kind);
358 ConsumeToken(); // The annotation token.
359}
360
361void Parser::HandlePragmaAlign() {
362 assert(Tok.is(tok::annot_pragma_align));
363 Sema::PragmaOptionsAlignKind Kind =
364 static_cast<Sema::PragmaOptionsAlignKind>(
365 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
366 SourceLocation PragmaLoc = ConsumeToken();
367 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
368}
369
370void Parser::HandlePragmaWeak() {
371 assert(Tok.is(tok::annot_pragma_weak));
372 SourceLocation PragmaLoc = ConsumeToken();
373 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
374 Tok.getLocation());
375 ConsumeToken(); // The weak name.
376}
377
378void Parser::HandlePragmaWeakAlias() {
379 assert(Tok.is(tok::annot_pragma_weakalias));
380 SourceLocation PragmaLoc = ConsumeToken();
381 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
382 SourceLocation WeakNameLoc = Tok.getLocation();
383 ConsumeToken();
384 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
385 SourceLocation AliasNameLoc = Tok.getLocation();
386 ConsumeToken();
387 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
388 WeakNameLoc, AliasNameLoc);
389
390}
391
392void Parser::HandlePragmaRedefineExtname() {
393 assert(Tok.is(tok::annot_pragma_redefine_extname));
394 SourceLocation RedefLoc = ConsumeToken();
395 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
396 SourceLocation RedefNameLoc = Tok.getLocation();
397 ConsumeToken();
398 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
399 SourceLocation AliasNameLoc = Tok.getLocation();
400 ConsumeToken();
401 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
402 RedefNameLoc, AliasNameLoc);
403}
404
405void Parser::HandlePragmaFPContract() {
406 assert(Tok.is(tok::annot_pragma_fp_contract));
407 tok::OnOffSwitch OOS =
408 static_cast<tok::OnOffSwitch>(
409 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
410 Actions.ActOnPragmaFPContract(OOS);
411 ConsumeToken(); // The annotation token.
412}
413
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000414StmtResult Parser::HandlePragmaCaptured()
415{
416 assert(Tok.is(tok::annot_pragma_captured));
417 ConsumeToken();
418
419 if (Tok.isNot(tok::l_brace)) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700420 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000421 return StmtError();
422 }
423
Tareq A. Siraj6afcf882013-04-16 19:37:38 +0000424 SourceLocation Loc = Tok.getLocation();
425
426 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir8c045ac2013-05-03 19:00:33 +0000427 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
428 /*NumParams=*/1);
Tareq A. Siraj6afcf882013-04-16 19:37:38 +0000429
430 StmtResult R = ParseCompoundStatement();
431 CapturedRegionScope.Exit();
432
433 if (R.isInvalid()) {
434 Actions.ActOnCapturedRegionError();
435 return StmtError();
436 }
437
438 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000439}
440
Eli Friedman9595c7e2012-10-04 02:36:51 +0000441namespace {
442 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
443}
444
445void Parser::HandlePragmaOpenCLExtension() {
446 assert(Tok.is(tok::annot_pragma_opencl_extension));
447 OpenCLExtData data =
448 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
449 unsigned state = data.getInt();
450 IdentifierInfo *ename = data.getPointer();
451 SourceLocation NameLoc = Tok.getLocation();
452 ConsumeToken(); // The annotation token.
453
454 OpenCLOptions &f = Actions.getOpenCLOptions();
455 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
456 // overriding all previously issued extension directives, but only if the
457 // behavior is set to disable."
458 if (state == 0 && ename->isStr("all")) {
459#define OPENCLEXT(nm) f.nm = 0;
460#include "clang/Basic/OpenCLExtensions.def"
461 }
462#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
463#include "clang/Basic/OpenCLExtensions.def"
464 else {
465 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
466 return;
467 }
468}
469
Stephen Hines651f13c2014-04-23 16:59:28 -0700470void Parser::HandlePragmaMSPointersToMembers() {
471 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
472 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
473 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
474 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
475 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
476 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
477}
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000478
Stephen Hines651f13c2014-04-23 16:59:28 -0700479void Parser::HandlePragmaMSVtorDisp() {
480 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
481 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
482 Sema::PragmaVtorDispKind Kind =
483 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
484 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
485 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
486 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
487}
Tareq A. Siraj85192c72013-04-16 18:41:26 +0000488
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700489void Parser::HandlePragmaMSPragma() {
490 assert(Tok.is(tok::annot_pragma_ms_pragma));
491 // Grab the tokens out of the annotation and enter them into the stream.
492 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
493 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
494 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
495 assert(Tok.isAnyIdentifier());
Stephen Hines176edba2014-12-01 14:53:08 -0800496 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700497 PP.Lex(Tok); // pragma kind
Stephen Hines176edba2014-12-01 14:53:08 -0800498
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700499 // Figure out which #pragma we're dealing with. The switch has no default
500 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Stephen Hines176edba2014-12-01 14:53:08 -0800501 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700502 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
503 .Case("data_seg", &Parser::HandlePragmaMSSegment)
504 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
505 .Case("const_seg", &Parser::HandlePragmaMSSegment)
506 .Case("code_seg", &Parser::HandlePragmaMSSegment)
507 .Case("section", &Parser::HandlePragmaMSSection)
508 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Stephen Hines176edba2014-12-01 14:53:08 -0800509
510 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
511 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
512 // until eof (really end of line) to prevent follow-on errors.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700513 while (Tok.isNot(tok::eof))
514 PP.Lex(Tok);
515 PP.Lex(Tok);
516 }
517}
518
Stephen Hines176edba2014-12-01 14:53:08 -0800519bool Parser::HandlePragmaMSSection(StringRef PragmaName,
520 SourceLocation PragmaLocation) {
521 if (Tok.isNot(tok::l_paren)) {
522 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
523 return false;
524 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700525 PP.Lex(Tok); // (
526 // Parsing code for pragma section
Stephen Hines176edba2014-12-01 14:53:08 -0800527 if (Tok.isNot(tok::string_literal)) {
528 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
529 << PragmaName;
530 return false;
531 }
532 ExprResult StringResult = ParseStringLiteralExpression();
533 if (StringResult.isInvalid())
534 return false; // Already diagnosed.
535 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
536 if (SegmentName->getCharByteWidth() != 1) {
537 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
538 << PragmaName;
539 return false;
540 }
541 int SectionFlags = ASTContext::PSF_Read;
542 bool SectionFlagsAreDefault = true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700543 while (Tok.is(tok::comma)) {
544 PP.Lex(Tok); // ,
Stephen Hines176edba2014-12-01 14:53:08 -0800545 // Ignore "long" and "short".
546 // They are undocumented, but widely used, section attributes which appear
547 // to do nothing.
548 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
549 PP.Lex(Tok); // long/short
550 continue;
551 }
552
553 if (!Tok.isAnyIdentifier()) {
554 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
555 << PragmaName;
556 return false;
557 }
558 ASTContext::PragmaSectionFlag Flag =
559 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700560 Tok.getIdentifierInfo()->getName())
Stephen Hines176edba2014-12-01 14:53:08 -0800561 .Case("read", ASTContext::PSF_Read)
562 .Case("write", ASTContext::PSF_Write)
563 .Case("execute", ASTContext::PSF_Execute)
564 .Case("shared", ASTContext::PSF_Invalid)
565 .Case("nopage", ASTContext::PSF_Invalid)
566 .Case("nocache", ASTContext::PSF_Invalid)
567 .Case("discard", ASTContext::PSF_Invalid)
568 .Case("remove", ASTContext::PSF_Invalid)
569 .Default(ASTContext::PSF_None);
570 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
571 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
572 ? diag::warn_pragma_invalid_specific_action
573 : diag::warn_pragma_unsupported_action)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700574 << PragmaName << Tok.getIdentifierInfo()->getName();
Stephen Hines176edba2014-12-01 14:53:08 -0800575 return false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700576 }
577 SectionFlags |= Flag;
Stephen Hines176edba2014-12-01 14:53:08 -0800578 SectionFlagsAreDefault = false;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700579 PP.Lex(Tok); // Identifier
580 }
Stephen Hines176edba2014-12-01 14:53:08 -0800581 // If no section attributes are specified, the section will be marked as
582 // read/write.
583 if (SectionFlagsAreDefault)
584 SectionFlags |= ASTContext::PSF_Write;
585 if (Tok.isNot(tok::r_paren)) {
586 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
587 return false;
588 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700589 PP.Lex(Tok); // )
Stephen Hines176edba2014-12-01 14:53:08 -0800590 if (Tok.isNot(tok::eof)) {
591 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
592 << PragmaName;
593 return false;
594 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700595 PP.Lex(Tok); // eof
596 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Stephen Hines176edba2014-12-01 14:53:08 -0800597 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700598}
599
Stephen Hines176edba2014-12-01 14:53:08 -0800600bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
601 SourceLocation PragmaLocation) {
602 if (Tok.isNot(tok::l_paren)) {
603 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
604 return false;
605 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700606 PP.Lex(Tok); // (
607 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Stephen Hines176edba2014-12-01 14:53:08 -0800608 StringRef SlotLabel;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700609 if (Tok.isAnyIdentifier()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800610 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700611 if (PushPop == "push")
612 Action = Sema::PSK_Push;
613 else if (PushPop == "pop")
614 Action = Sema::PSK_Pop;
Stephen Hines176edba2014-12-01 14:53:08 -0800615 else {
616 PP.Diag(PragmaLocation,
617 diag::warn_pragma_expected_section_push_pop_or_name)
618 << PragmaName;
619 return false;
620 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700621 if (Action != Sema::PSK_Reset) {
622 PP.Lex(Tok); // push | pop
623 if (Tok.is(tok::comma)) {
624 PP.Lex(Tok); // ,
625 // If we've got a comma, we either need a label or a string.
626 if (Tok.isAnyIdentifier()) {
627 SlotLabel = Tok.getIdentifierInfo()->getName();
628 PP.Lex(Tok); // identifier
629 if (Tok.is(tok::comma))
630 PP.Lex(Tok);
Stephen Hines176edba2014-12-01 14:53:08 -0800631 else if (Tok.isNot(tok::r_paren)) {
632 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
633 << PragmaName;
634 return false;
635 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700636 }
Stephen Hines176edba2014-12-01 14:53:08 -0800637 } else if (Tok.isNot(tok::r_paren)) {
638 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
639 return false;
640 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700641 }
642 }
643 // Grab the string literal for our section name.
644 StringLiteral *SegmentName = nullptr;
645 if (Tok.isNot(tok::r_paren)) {
Stephen Hines176edba2014-12-01 14:53:08 -0800646 if (Tok.isNot(tok::string_literal)) {
647 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700648 diag::warn_pragma_expected_section_name :
649 diag::warn_pragma_expected_section_label_or_name :
650 diag::warn_pragma_expected_section_push_pop_or_name;
Stephen Hines176edba2014-12-01 14:53:08 -0800651 PP.Diag(PragmaLocation, DiagID) << PragmaName;
652 return false;
653 }
654 ExprResult StringResult = ParseStringLiteralExpression();
655 if (StringResult.isInvalid())
656 return false; // Already diagnosed.
657 SegmentName = cast<StringLiteral>(StringResult.get());
658 if (SegmentName->getCharByteWidth() != 1) {
659 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
660 << PragmaName;
661 return false;
662 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700663 // Setting section "" has no effect
664 if (SegmentName->getLength())
665 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
666 }
Stephen Hines176edba2014-12-01 14:53:08 -0800667 if (Tok.isNot(tok::r_paren)) {
668 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
669 return false;
670 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700671 PP.Lex(Tok); // )
Stephen Hines176edba2014-12-01 14:53:08 -0800672 if (Tok.isNot(tok::eof)) {
673 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
674 << PragmaName;
675 return false;
676 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700677 PP.Lex(Tok); // eof
678 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
679 SegmentName, PragmaName);
Stephen Hines176edba2014-12-01 14:53:08 -0800680 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700681}
682
Stephen Hines176edba2014-12-01 14:53:08 -0800683// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
684bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
685 SourceLocation PragmaLocation) {
686 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
687 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
688 return false;
689 }
690
691 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
692 PragmaName))
693 return false;
694
695 // Parse either the known section names or the string section name.
696 StringLiteral *SegmentName = nullptr;
697 if (Tok.isAnyIdentifier()) {
698 auto *II = Tok.getIdentifierInfo();
699 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
700 .Case("compiler", "\".CRT$XCC\"")
701 .Case("lib", "\".CRT$XCL\"")
702 .Case("user", "\".CRT$XCU\"")
703 .Default("");
704
705 if (!Section.empty()) {
706 // Pretend the user wrote the appropriate string literal here.
707 Token Toks[1];
708 Toks[0].startToken();
709 Toks[0].setKind(tok::string_literal);
710 Toks[0].setLocation(Tok.getLocation());
711 Toks[0].setLiteralData(Section.data());
712 Toks[0].setLength(Section.size());
713 SegmentName =
714 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
715 PP.Lex(Tok);
716 }
717 } else if (Tok.is(tok::string_literal)) {
718 ExprResult StringResult = ParseStringLiteralExpression();
719 if (StringResult.isInvalid())
720 return false;
721 SegmentName = cast<StringLiteral>(StringResult.get());
722 if (SegmentName->getCharByteWidth() != 1) {
723 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
724 << PragmaName;
725 return false;
726 }
727 // FIXME: Add support for the '[, func-name]' part of the pragma.
728 }
729
730 if (!SegmentName) {
731 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
732 return false;
733 }
734
735 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
736 PragmaName) ||
737 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
738 PragmaName))
739 return false;
740
741 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
742 return true;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700743}
744
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700745struct PragmaLoopHintInfo {
Stephen Hines176edba2014-12-01 14:53:08 -0800746 Token PragmaName;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700747 Token Option;
Stephen Hines176edba2014-12-01 14:53:08 -0800748 Token *Toks;
749 size_t TokSize;
750 PragmaLoopHintInfo() : Toks(nullptr), TokSize(0) {}
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700751};
752
Stephen Hines176edba2014-12-01 14:53:08 -0800753static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
754 std::string PragmaString;
755 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
756 PragmaString = "clang loop ";
757 PragmaString += Option.getIdentifierInfo()->getName();
758 } else {
759 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
760 "Unexpected pragma name");
761 PragmaString = "unroll";
762 }
763 return PragmaString;
764}
765
766bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700767 assert(Tok.is(tok::annot_pragma_loop_hint));
768 PragmaLoopHintInfo *Info =
769 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
770
Stephen Hines176edba2014-12-01 14:53:08 -0800771 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
772 Hint.PragmaNameLoc = IdentifierLoc::create(
773 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700774
Stephen Hines176edba2014-12-01 14:53:08 -0800775 // It is possible that the loop hint has no option identifier, such as
776 // #pragma unroll(4).
777 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
778 ? Info->Option.getIdentifierInfo()
779 : nullptr;
780 Hint.OptionLoc = IdentifierLoc::create(
781 Actions.Context, Info->Option.getLocation(), OptionInfo);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700782
Stephen Hines176edba2014-12-01 14:53:08 -0800783 Token *Toks = Info->Toks;
784 size_t TokSize = Info->TokSize;
785
786 // Return a valid hint if pragma unroll or nounroll were specified
787 // without an argument.
788 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
789 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
790 if (TokSize == 0 && (PragmaUnroll || PragmaNoUnroll)) {
791 ConsumeToken(); // The annotation token.
792 Hint.Range = Info->PragmaName.getLocation();
793 return true;
794 }
795
796 // The constant expression is always followed by an eof token, which increases
797 // the TokSize by 1.
798 assert(TokSize > 0 &&
799 "PragmaLoopHintInfo::Toks must contain at least one token.");
800
801 // If no option is specified the argument is assumed to be a constant expr.
802 bool StateOption = false;
803 if (OptionInfo) { // Pragma unroll does not specify an option.
804 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
805 .Case("vectorize", true)
806 .Case("interleave", true)
807 .Case("unroll", true)
808 .Default(false);
809 }
810
811 // Verify loop hint has an argument.
812 if (Toks[0].is(tok::eof)) {
813 ConsumeToken(); // The annotation token.
814 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
815 << /*StateArgument=*/StateOption << /*FullKeyword=*/PragmaUnroll;
816 return false;
817 }
818
819 // Validate the argument.
820 if (StateOption) {
821 ConsumeToken(); // The annotation token.
822 bool OptionUnroll = OptionInfo->isStr("unroll");
823 SourceLocation StateLoc = Toks[0].getLocation();
824 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
825 if (!StateInfo || ((OptionUnroll ? !StateInfo->isStr("full")
826 : !StateInfo->isStr("enable")) &&
827 !StateInfo->isStr("disable"))) {
828 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
829 << /*FullKeyword=*/OptionUnroll;
830 return false;
831 }
832 if (TokSize > 2)
833 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
834 << PragmaLoopHintString(Info->PragmaName, Info->Option);
835 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
836 } else {
837 // Enter constant expression including eof terminator into token stream.
838 PP.EnterTokenStream(Toks, TokSize, /*DisableMacroExpansion=*/false,
839 /*OwnsTokens=*/false);
840 ConsumeToken(); // The annotation token.
841
842 ExprResult R = ParseConstantExpression();
843
844 // Tokens following an error in an ill-formed constant expression will
845 // remain in the token stream and must be removed.
846 if (Tok.isNot(tok::eof)) {
847 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
848 << PragmaLoopHintString(Info->PragmaName, Info->Option);
849 while (Tok.isNot(tok::eof))
850 ConsumeAnyToken();
851 }
852
853 ConsumeToken(); // Consume the constant expression eof terminator.
854
855 if (R.isInvalid() ||
856 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
857 return false;
858
859 // Argument is a constant expression with an integer type.
860 Hint.ValueExpr = R.get();
861 }
862
863 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
864 Info->Toks[TokSize - 1].getLocation());
865 return true;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700866}
867
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000868// #pragma GCC visibility comes in two variants:
869// 'push' '(' [visibility] ')'
870// 'pop'
Douglas Gregor80c60f72010-09-09 22:45:38 +0000871void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
872 PragmaIntroducerKind Introducer,
873 Token &VisTok) {
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000874 SourceLocation VisLoc = VisTok.getLocation();
875
876 Token Tok;
Joerg Sonnenbergere23af2a2011-07-20 01:03:50 +0000877 PP.LexUnexpandedToken(Tok);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000878
879 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
880
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000881 const IdentifierInfo *VisType;
882 if (PushPop && PushPop->isStr("pop")) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700883 VisType = nullptr;
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000884 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenbergere23af2a2011-07-20 01:03:50 +0000885 PP.LexUnexpandedToken(Tok);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000886 if (Tok.isNot(tok::l_paren)) {
887 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
888 << "visibility";
889 return;
890 }
Joerg Sonnenbergere23af2a2011-07-20 01:03:50 +0000891 PP.LexUnexpandedToken(Tok);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000892 VisType = Tok.getIdentifierInfo();
893 if (!VisType) {
894 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
895 << "visibility";
896 return;
897 }
Joerg Sonnenbergere23af2a2011-07-20 01:03:50 +0000898 PP.LexUnexpandedToken(Tok);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000899 if (Tok.isNot(tok::r_paren)) {
900 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
901 << "visibility";
902 return;
903 }
904 } else {
905 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
906 << "visibility";
907 return;
908 }
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700909 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenbergere23af2a2011-07-20 01:03:50 +0000910 PP.LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000911 if (Tok.isNot(tok::eod)) {
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000912 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
913 << "visibility";
914 return;
915 }
916
Rafael Espindola426fc942012-01-26 02:02:57 +0000917 Token *Toks = new Token[1];
918 Toks[0].startToken();
919 Toks[0].setKind(tok::annot_pragma_vis);
920 Toks[0].setLocation(VisLoc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -0700921 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola426fc942012-01-26 02:02:57 +0000922 Toks[0].setAnnotationValue(
923 const_cast<void*>(static_cast<const void*>(VisType)));
924 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
925 /*OwnsTokens=*/true);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000926}
927
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000928// #pragma pack(...) comes in the following delicious flavors:
929// pack '(' [integer] ')'
930// pack '(' 'show' ')'
931// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregor80c60f72010-09-09 22:45:38 +0000932void PragmaPackHandler::HandlePragma(Preprocessor &PP,
933 PragmaIntroducerKind Introducer,
934 Token &PackTok) {
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000935 SourceLocation PackLoc = PackTok.getLocation();
936
937 Token Tok;
938 PP.Lex(Tok);
939 if (Tok.isNot(tok::l_paren)) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000940 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000941 return;
942 }
943
John McCallf312b1e2010-08-26 23:41:50 +0000944 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700945 IdentifierInfo *Name = nullptr;
Eli Friedman9595c7e2012-10-04 02:36:51 +0000946 Token Alignment;
947 Alignment.startToken();
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000948 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000949 PP.Lex(Tok);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000950 if (Tok.is(tok::numeric_constant)) {
Eli Friedman9595c7e2012-10-04 02:36:51 +0000951 Alignment = Tok;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000952
953 PP.Lex(Tok);
Eli Friedman19bda3a2011-11-02 01:53:16 +0000954
955 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
956 // the push/pop stack.
957 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikie4e4d0842012-03-11 07:00:24 +0000958 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman19bda3a2011-11-02 01:53:16 +0000959 Kind = Sema::PPK_Push;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000960 } else if (Tok.is(tok::identifier)) {
961 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner08631c52008-11-23 21:45:46 +0000962 if (II->isStr("show")) {
John McCallf312b1e2010-08-26 23:41:50 +0000963 Kind = Sema::PPK_Show;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000964 PP.Lex(Tok);
965 } else {
Chris Lattner08631c52008-11-23 21:45:46 +0000966 if (II->isStr("push")) {
John McCallf312b1e2010-08-26 23:41:50 +0000967 Kind = Sema::PPK_Push;
Chris Lattner08631c52008-11-23 21:45:46 +0000968 } else if (II->isStr("pop")) {
John McCallf312b1e2010-08-26 23:41:50 +0000969 Kind = Sema::PPK_Pop;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000970 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -0700971 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000972 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000973 }
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000974 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000976 if (Tok.is(tok::comma)) {
977 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000979 if (Tok.is(tok::numeric_constant)) {
Eli Friedman9595c7e2012-10-04 02:36:51 +0000980 Alignment = Tok;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000981
982 PP.Lex(Tok);
983 } else if (Tok.is(tok::identifier)) {
984 Name = Tok.getIdentifierInfo();
985 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000987 if (Tok.is(tok::comma)) {
988 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000989
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000990 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattner08631c52008-11-23 21:45:46 +0000991 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000992 return;
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Eli Friedman9595c7e2012-10-04 02:36:51 +0000995 Alignment = Tok;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000996
997 PP.Lex(Tok);
998 }
999 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00001000 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +00001001 return;
1002 }
1003 }
1004 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001005 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman19bda3a2011-11-02 01:53:16 +00001006 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1007 // the push/pop stack.
1008 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1009 Kind = Sema::PPK_Pop;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001010 }
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +00001011
1012 if (Tok.isNot(tok::r_paren)) {
Ted Kremenek4726d032009-03-23 22:28:25 +00001013 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +00001014 return;
1015 }
1016
Daniel Dunbar861800c2010-05-26 23:29:06 +00001017 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedman99914792009-06-05 00:49:58 +00001018 PP.Lex(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +00001019 if (Tok.isNot(tok::eod)) {
Eli Friedman99914792009-06-05 00:49:58 +00001020 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1021 return;
1022 }
1023
Daniel Dunbarb0939552012-02-29 01:38:22 +00001024 PragmaPackInfo *Info =
1025 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
1026 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
1027 new (Info) PragmaPackInfo();
Eli Friedmanaa5ab262012-02-23 23:47:16 +00001028 Info->Kind = Kind;
1029 Info->Name = Name;
Eli Friedman9595c7e2012-10-04 02:36:51 +00001030 Info->Alignment = Alignment;
Eli Friedmanaa5ab262012-02-23 23:47:16 +00001031 Info->LParenLoc = LParenLoc;
1032 Info->RParenLoc = RParenLoc;
1033
Daniel Dunbarb0939552012-02-29 01:38:22 +00001034 Token *Toks =
1035 (Token*) PP.getPreprocessorAllocator().Allocate(
1036 sizeof(Token) * 1, llvm::alignOf<Token>());
1037 new (Toks) Token();
Eli Friedmanaa5ab262012-02-23 23:47:16 +00001038 Toks[0].startToken();
1039 Toks[0].setKind(tok::annot_pragma_pack);
1040 Toks[0].setLocation(PackLoc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001041 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanaa5ab262012-02-23 23:47:16 +00001042 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1043 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbarb0939552012-02-29 01:38:22 +00001044 /*OwnsTokens=*/false);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +00001045}
1046
Fariborz Jahanian62c92582011-04-25 18:49:15 +00001047// #pragma ms_struct on
1048// #pragma ms_struct off
1049void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1050 PragmaIntroducerKind Introducer,
1051 Token &MSStructTok) {
1052 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
1053
1054 Token Tok;
1055 PP.Lex(Tok);
1056 if (Tok.isNot(tok::identifier)) {
1057 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1058 return;
1059 }
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001060 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian62c92582011-04-25 18:49:15 +00001061 const IdentifierInfo *II = Tok.getIdentifierInfo();
1062 if (II->isStr("on")) {
1063 Kind = Sema::PMSST_ON;
1064 PP.Lex(Tok);
1065 }
1066 else if (II->isStr("off") || II->isStr("reset"))
1067 PP.Lex(Tok);
1068 else {
1069 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1070 return;
1071 }
1072
1073 if (Tok.isNot(tok::eod)) {
Daniel Dunbarb0939552012-02-29 01:38:22 +00001074 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1075 << "ms_struct";
Fariborz Jahanian62c92582011-04-25 18:49:15 +00001076 return;
1077 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001078
1079 Token *Toks =
1080 (Token*) PP.getPreprocessorAllocator().Allocate(
1081 sizeof(Token) * 1, llvm::alignOf<Token>());
1082 new (Toks) Token();
1083 Toks[0].startToken();
1084 Toks[0].setKind(tok::annot_pragma_msstruct);
1085 Toks[0].setLocation(MSStructTok.getLocation());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001086 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman9595c7e2012-10-04 02:36:51 +00001087 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1088 static_cast<uintptr_t>(Kind)));
1089 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1090 /*OwnsTokens=*/false);
Fariborz Jahanian62c92582011-04-25 18:49:15 +00001091}
1092
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001093// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1094// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman9595c7e2012-10-04 02:36:51 +00001095static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001096 bool IsOptions) {
Daniel Dunbar861800c2010-05-26 23:29:06 +00001097 Token Tok;
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001098
1099 if (IsOptions) {
1100 PP.Lex(Tok);
1101 if (Tok.isNot(tok::identifier) ||
1102 !Tok.getIdentifierInfo()->isStr("align")) {
1103 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1104 return;
1105 }
Daniel Dunbar861800c2010-05-26 23:29:06 +00001106 }
Daniel Dunbar638e7cf2010-05-27 18:42:09 +00001107
Daniel Dunbar861800c2010-05-26 23:29:06 +00001108 PP.Lex(Tok);
1109 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001110 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1111 << IsOptions;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001112 return;
1113 }
1114
1115 PP.Lex(Tok);
1116 if (Tok.isNot(tok::identifier)) {
1117 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001118 << (IsOptions ? "options" : "align");
Daniel Dunbar861800c2010-05-26 23:29:06 +00001119 return;
1120 }
1121
John McCallf312b1e2010-08-26 23:41:50 +00001122 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001123 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar638e7cf2010-05-27 18:42:09 +00001124 if (II->isStr("native"))
John McCallf312b1e2010-08-26 23:41:50 +00001125 Kind = Sema::POAK_Native;
Daniel Dunbar638e7cf2010-05-27 18:42:09 +00001126 else if (II->isStr("natural"))
John McCallf312b1e2010-08-26 23:41:50 +00001127 Kind = Sema::POAK_Natural;
Daniel Dunbar6f739142010-05-27 18:42:17 +00001128 else if (II->isStr("packed"))
John McCallf312b1e2010-08-26 23:41:50 +00001129 Kind = Sema::POAK_Packed;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001130 else if (II->isStr("power"))
John McCallf312b1e2010-08-26 23:41:50 +00001131 Kind = Sema::POAK_Power;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001132 else if (II->isStr("mac68k"))
John McCallf312b1e2010-08-26 23:41:50 +00001133 Kind = Sema::POAK_Mac68k;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001134 else if (II->isStr("reset"))
John McCallf312b1e2010-08-26 23:41:50 +00001135 Kind = Sema::POAK_Reset;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001136 else {
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001137 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1138 << IsOptions;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001139 return;
1140 }
1141
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001142 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar861800c2010-05-26 23:29:06 +00001143 PP.Lex(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +00001144 if (Tok.isNot(tok::eod)) {
Daniel Dunbar861800c2010-05-26 23:29:06 +00001145 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001146 << (IsOptions ? "options" : "align");
Daniel Dunbar861800c2010-05-26 23:29:06 +00001147 return;
1148 }
1149
Eli Friedman9595c7e2012-10-04 02:36:51 +00001150 Token *Toks =
1151 (Token*) PP.getPreprocessorAllocator().Allocate(
1152 sizeof(Token) * 1, llvm::alignOf<Token>());
1153 new (Toks) Token();
1154 Toks[0].startToken();
1155 Toks[0].setKind(tok::annot_pragma_align);
1156 Toks[0].setLocation(FirstTok.getLocation());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001157 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman9595c7e2012-10-04 02:36:51 +00001158 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1159 static_cast<uintptr_t>(Kind)));
1160 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1161 /*OwnsTokens=*/false);
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001162}
1163
Douglas Gregor80c60f72010-09-09 22:45:38 +00001164void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1165 PragmaIntroducerKind Introducer,
1166 Token &AlignTok) {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001167 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001168}
1169
Douglas Gregor80c60f72010-09-09 22:45:38 +00001170void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1171 PragmaIntroducerKind Introducer,
1172 Token &OptionsTok) {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001173 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar861800c2010-05-26 23:29:06 +00001174}
1175
Ted Kremenek4726d032009-03-23 22:28:25 +00001176// #pragma unused(identifier)
Douglas Gregor80c60f72010-09-09 22:45:38 +00001177void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1178 PragmaIntroducerKind Introducer,
1179 Token &UnusedTok) {
Ted Kremenek4726d032009-03-23 22:28:25 +00001180 // FIXME: Should we be expanding macros here? My guess is no.
1181 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Ted Kremenek4726d032009-03-23 22:28:25 +00001183 // Lex the left '('.
1184 Token Tok;
1185 PP.Lex(Tok);
1186 if (Tok.isNot(tok::l_paren)) {
1187 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1188 return;
1189 }
Mike Stump1eb44332009-09-09 15:08:12 +00001190
Ted Kremenek4726d032009-03-23 22:28:25 +00001191 // Lex the declaration reference(s).
Chris Lattner5f9e2722011-07-23 10:55:15 +00001192 SmallVector<Token, 5> Identifiers;
Ted Kremenek4726d032009-03-23 22:28:25 +00001193 SourceLocation RParenLoc;
1194 bool LexID = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001195
Ted Kremenek4726d032009-03-23 22:28:25 +00001196 while (true) {
1197 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001198
Ted Kremenek4726d032009-03-23 22:28:25 +00001199 if (LexID) {
Mike Stump1eb44332009-09-09 15:08:12 +00001200 if (Tok.is(tok::identifier)) {
Ted Kremenek7a02a372009-08-03 23:24:57 +00001201 Identifiers.push_back(Tok);
Ted Kremenek4726d032009-03-23 22:28:25 +00001202 LexID = false;
1203 continue;
1204 }
1205
Ted Kremenek7a02a372009-08-03 23:24:57 +00001206 // Illegal token!
Ted Kremenek4726d032009-03-23 22:28:25 +00001207 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1208 return;
1209 }
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Ted Kremenek4726d032009-03-23 22:28:25 +00001211 // We are execting a ')' or a ','.
1212 if (Tok.is(tok::comma)) {
1213 LexID = true;
1214 continue;
1215 }
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Ted Kremenek4726d032009-03-23 22:28:25 +00001217 if (Tok.is(tok::r_paren)) {
1218 RParenLoc = Tok.getLocation();
1219 break;
1220 }
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Ted Kremenek7a02a372009-08-03 23:24:57 +00001222 // Illegal token!
Stephen Hines651f13c2014-04-23 16:59:28 -07001223 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenek4726d032009-03-23 22:28:25 +00001224 return;
1225 }
Eli Friedman99914792009-06-05 00:49:58 +00001226
1227 PP.Lex(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +00001228 if (Tok.isNot(tok::eod)) {
Eli Friedman99914792009-06-05 00:49:58 +00001229 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1230 "unused";
1231 return;
1232 }
1233
Ted Kremenek4726d032009-03-23 22:28:25 +00001234 // Verify that we have a location for the right parenthesis.
1235 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenek7a02a372009-08-03 23:24:57 +00001236 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenek4726d032009-03-23 22:28:25 +00001237
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +00001238 // For each identifier token, insert into the token stream a
1239 // annot_pragma_unused token followed by the identifier token.
1240 // This allows us to cache a "#pragma unused" that occurs inside an inline
1241 // C++ member function.
1242
Daniel Dunbarb0939552012-02-29 01:38:22 +00001243 Token *Toks =
1244 (Token*) PP.getPreprocessorAllocator().Allocate(
1245 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +00001246 for (unsigned i=0; i != Identifiers.size(); i++) {
1247 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1248 pragmaUnusedTok.startToken();
1249 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1250 pragmaUnusedTok.setLocation(UnusedLoc);
1251 idTok = Identifiers[i];
1252 }
Daniel Dunbarb0939552012-02-29 01:38:22 +00001253 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1254 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenek4726d032009-03-23 22:28:25 +00001255}
Eli Friedman99914792009-06-05 00:49:58 +00001256
1257// #pragma weak identifier
1258// #pragma weak identifier '=' identifier
Douglas Gregor80c60f72010-09-09 22:45:38 +00001259void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1260 PragmaIntroducerKind Introducer,
1261 Token &WeakTok) {
Eli Friedman99914792009-06-05 00:49:58 +00001262 SourceLocation WeakLoc = WeakTok.getLocation();
1263
1264 Token Tok;
1265 PP.Lex(Tok);
1266 if (Tok.isNot(tok::identifier)) {
1267 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1268 return;
1269 }
1270
Eli Friedman9595c7e2012-10-04 02:36:51 +00001271 Token WeakName = Tok;
1272 bool HasAlias = false;
1273 Token AliasName;
Eli Friedman99914792009-06-05 00:49:58 +00001274
1275 PP.Lex(Tok);
1276 if (Tok.is(tok::equal)) {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001277 HasAlias = true;
Eli Friedman99914792009-06-05 00:49:58 +00001278 PP.Lex(Tok);
1279 if (Tok.isNot(tok::identifier)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001280 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedman99914792009-06-05 00:49:58 +00001281 << "weak";
1282 return;
1283 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001284 AliasName = Tok;
Eli Friedman99914792009-06-05 00:49:58 +00001285 PP.Lex(Tok);
1286 }
1287
Peter Collingbourne84021552011-02-28 02:37:51 +00001288 if (Tok.isNot(tok::eod)) {
Eli Friedman99914792009-06-05 00:49:58 +00001289 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1290 return;
1291 }
1292
Eli Friedman9595c7e2012-10-04 02:36:51 +00001293 if (HasAlias) {
1294 Token *Toks =
1295 (Token*) PP.getPreprocessorAllocator().Allocate(
1296 sizeof(Token) * 3, llvm::alignOf<Token>());
1297 Token &pragmaUnusedTok = Toks[0];
1298 pragmaUnusedTok.startToken();
1299 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1300 pragmaUnusedTok.setLocation(WeakLoc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001301 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman9595c7e2012-10-04 02:36:51 +00001302 Toks[1] = WeakName;
1303 Toks[2] = AliasName;
1304 PP.EnterTokenStream(Toks, 3,
1305 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedman99914792009-06-05 00:49:58 +00001306 } else {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001307 Token *Toks =
1308 (Token*) PP.getPreprocessorAllocator().Allocate(
1309 sizeof(Token) * 2, llvm::alignOf<Token>());
1310 Token &pragmaUnusedTok = Toks[0];
1311 pragmaUnusedTok.startToken();
1312 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1313 pragmaUnusedTok.setLocation(WeakLoc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001314 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman9595c7e2012-10-04 02:36:51 +00001315 Toks[1] = WeakName;
1316 PP.EnterTokenStream(Toks, 2,
1317 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedman99914792009-06-05 00:49:58 +00001318 }
1319}
Peter Collingbourne321b8172011-02-14 01:42:35 +00001320
David Chisnall5f3c1632012-02-18 16:12:34 +00001321// #pragma redefine_extname identifier identifier
1322void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1323 PragmaIntroducerKind Introducer,
1324 Token &RedefToken) {
1325 SourceLocation RedefLoc = RedefToken.getLocation();
1326
1327 Token Tok;
1328 PP.Lex(Tok);
1329 if (Tok.isNot(tok::identifier)) {
1330 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1331 "redefine_extname";
1332 return;
1333 }
1334
Eli Friedman9595c7e2012-10-04 02:36:51 +00001335 Token RedefName = Tok;
David Chisnall5f3c1632012-02-18 16:12:34 +00001336 PP.Lex(Tok);
Eli Friedman9595c7e2012-10-04 02:36:51 +00001337
David Chisnall5f3c1632012-02-18 16:12:34 +00001338 if (Tok.isNot(tok::identifier)) {
1339 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1340 << "redefine_extname";
1341 return;
1342 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001343
1344 Token AliasName = Tok;
David Chisnall5f3c1632012-02-18 16:12:34 +00001345 PP.Lex(Tok);
1346
1347 if (Tok.isNot(tok::eod)) {
1348 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1349 "redefine_extname";
1350 return;
1351 }
1352
Eli Friedman9595c7e2012-10-04 02:36:51 +00001353 Token *Toks =
1354 (Token*) PP.getPreprocessorAllocator().Allocate(
1355 sizeof(Token) * 3, llvm::alignOf<Token>());
1356 Token &pragmaRedefTok = Toks[0];
1357 pragmaRedefTok.startToken();
1358 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1359 pragmaRedefTok.setLocation(RedefLoc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001360 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman9595c7e2012-10-04 02:36:51 +00001361 Toks[1] = RedefName;
1362 Toks[2] = AliasName;
1363 PP.EnterTokenStream(Toks, 3,
1364 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall5f3c1632012-02-18 16:12:34 +00001365}
1366
1367
Peter Collingbourne321b8172011-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
Eli Friedman9595c7e2012-10-04 02:36:51 +00001376 Token *Toks =
1377 (Token*) PP.getPreprocessorAllocator().Allocate(
1378 sizeof(Token) * 1, llvm::alignOf<Token>());
1379 new (Toks) Token();
1380 Toks[0].startToken();
1381 Toks[0].setKind(tok::annot_pragma_fp_contract);
1382 Toks[0].setLocation(Tok.getLocation());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001383 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman9595c7e2012-10-04 02:36:51 +00001384 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1385 static_cast<uintptr_t>(OOS)));
1386 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1387 /*OwnsTokens=*/false);
Peter Collingbourne321b8172011-02-14 01:42:35 +00001388}
Peter Collingbournef315fa82011-02-14 01:42:53 +00001389
1390void
1391PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1392 PragmaIntroducerKind Introducer,
1393 Token &Tok) {
Tanya Lattnerb38b6a72011-04-14 23:35:31 +00001394 PP.LexUnexpandedToken(Tok);
Peter Collingbournef315fa82011-02-14 01:42:53 +00001395 if (Tok.isNot(tok::identifier)) {
1396 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1397 "OPENCL";
1398 return;
1399 }
1400 IdentifierInfo *ename = Tok.getIdentifierInfo();
1401 SourceLocation NameLoc = Tok.getLocation();
1402
1403 PP.Lex(Tok);
1404 if (Tok.isNot(tok::colon)) {
1405 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1406 return;
1407 }
1408
1409 PP.Lex(Tok);
1410 if (Tok.isNot(tok::identifier)) {
1411 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1412 return;
1413 }
1414 IdentifierInfo *op = Tok.getIdentifierInfo();
1415
1416 unsigned state;
1417 if (op->isStr("enable")) {
1418 state = 1;
1419 } else if (op->isStr("disable")) {
1420 state = 0;
1421 } else {
1422 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1423 return;
1424 }
Pekka Jaaskelainena0950e82013-10-12 09:29:48 +00001425 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbournef315fa82011-02-14 01:42:53 +00001426
Eli Friedman9595c7e2012-10-04 02:36:51 +00001427 PP.Lex(Tok);
1428 if (Tok.isNot(tok::eod)) {
1429 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1430 "OPENCL EXTENSION";
Peter Collingbournef315fa82011-02-14 01:42:53 +00001431 return;
1432 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001433
1434 OpenCLExtData data(ename, state);
1435 Token *Toks =
1436 (Token*) PP.getPreprocessorAllocator().Allocate(
1437 sizeof(Token) * 1, llvm::alignOf<Token>());
1438 new (Toks) Token();
1439 Toks[0].startToken();
1440 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1441 Toks[0].setLocation(NameLoc);
1442 Toks[0].setAnnotationValue(data.getOpaqueValue());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001443 Toks[0].setAnnotationEndLoc(StateLoc);
Eli Friedman9595c7e2012-10-04 02:36:51 +00001444 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1445 /*OwnsTokens=*/false);
Pekka Jaaskelainena0950e82013-10-12 09:29:48 +00001446
1447 if (PP.getPPCallbacks())
1448 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1449 StateLoc, state);
Peter Collingbournef315fa82011-02-14 01:42:53 +00001450}
1451
Alexey Bataevc6400582013-03-22 06:34:35 +00001452/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1453///
1454void
1455PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1456 PragmaIntroducerKind Introducer,
1457 Token &FirstTok) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001458 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1459 FirstTok.getLocation())) {
Alexey Bataevc6400582013-03-22 06:34:35 +00001460 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001461 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1462 diag::Severity::Ignored, SourceLocation());
Alexey Bataevc6400582013-03-22 06:34:35 +00001463 }
1464 PP.DiscardUntilEndOfDirective();
1465}
1466
1467/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1468///
1469void
1470PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1471 PragmaIntroducerKind Introducer,
1472 Token &FirstTok) {
1473 SmallVector<Token, 16> Pragma;
1474 Token Tok;
1475 Tok.startToken();
1476 Tok.setKind(tok::annot_pragma_openmp);
1477 Tok.setLocation(FirstTok.getLocation());
1478
1479 while (Tok.isNot(tok::eod)) {
1480 Pragma.push_back(Tok);
1481 PP.Lex(Tok);
1482 }
1483 SourceLocation EodLoc = Tok.getLocation();
1484 Tok.startToken();
1485 Tok.setKind(tok::annot_pragma_openmp_end);
1486 Tok.setLocation(EodLoc);
1487 Pragma.push_back(Tok);
1488
1489 Token *Toks = new Token[Pragma.size()];
1490 std::copy(Pragma.begin(), Pragma.end(), Toks);
1491 PP.EnterTokenStream(Toks, Pragma.size(),
1492 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1493}
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001494
Stephen Hines651f13c2014-04-23 16:59:28 -07001495/// \brief Handle '#pragma pointers_to_members'
1496// The grammar for this pragma is as follows:
1497//
1498// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1499//
1500// #pragma pointers_to_members '(' 'best_case' ')'
1501// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1502// #pragma pointers_to_members '(' inheritance-model ')'
1503void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1504 PragmaIntroducerKind Introducer,
1505 Token &Tok) {
1506 SourceLocation PointersToMembersLoc = Tok.getLocation();
1507 PP.Lex(Tok);
1508 if (Tok.isNot(tok::l_paren)) {
1509 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1510 << "pointers_to_members";
1511 return;
1512 }
1513 PP.Lex(Tok);
1514 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1515 if (!Arg) {
1516 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1517 << "pointers_to_members";
1518 return;
1519 }
1520 PP.Lex(Tok);
1521
1522 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
1523 if (Arg->isStr("best_case")) {
1524 RepresentationMethod = LangOptions::PPTMK_BestCase;
1525 } else {
1526 if (Arg->isStr("full_generality")) {
1527 if (Tok.is(tok::comma)) {
1528 PP.Lex(Tok);
1529
1530 Arg = Tok.getIdentifierInfo();
1531 if (!Arg) {
1532 PP.Diag(Tok.getLocation(),
1533 diag::err_pragma_pointers_to_members_unknown_kind)
1534 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1535 return;
1536 }
1537 PP.Lex(Tok);
1538 } else if (Tok.is(tok::r_paren)) {
1539 // #pragma pointers_to_members(full_generality) implicitly specifies
1540 // virtual_inheritance.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001541 Arg = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001542 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
1543 } else {
1544 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1545 << "full_generality";
1546 return;
1547 }
1548 }
1549
1550 if (Arg) {
1551 if (Arg->isStr("single_inheritance")) {
1552 RepresentationMethod =
1553 LangOptions::PPTMK_FullGeneralitySingleInheritance;
1554 } else if (Arg->isStr("multiple_inheritance")) {
1555 RepresentationMethod =
1556 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
1557 } else if (Arg->isStr("virtual_inheritance")) {
1558 RepresentationMethod =
1559 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
1560 } else {
1561 PP.Diag(Tok.getLocation(),
1562 diag::err_pragma_pointers_to_members_unknown_kind)
1563 << Arg << /*HasPointerDeclaration*/ 1;
1564 return;
1565 }
1566 }
1567 }
1568
1569 if (Tok.isNot(tok::r_paren)) {
1570 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1571 << (Arg ? Arg->getName() : "full_generality");
1572 return;
1573 }
1574
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001575 SourceLocation EndLoc = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -07001576 PP.Lex(Tok);
1577 if (Tok.isNot(tok::eod)) {
1578 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1579 << "pointers_to_members";
1580 return;
1581 }
1582
1583 Token AnnotTok;
1584 AnnotTok.startToken();
1585 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1586 AnnotTok.setLocation(PointersToMembersLoc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001587 AnnotTok.setAnnotationEndLoc(EndLoc);
Stephen Hines651f13c2014-04-23 16:59:28 -07001588 AnnotTok.setAnnotationValue(
1589 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1590 PP.EnterToken(AnnotTok);
1591}
1592
1593/// \brief Handle '#pragma vtordisp'
1594// The grammar for this pragma is as follows:
1595//
1596// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1597//
1598// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1599// #pragma vtordisp '(' 'pop' ')'
1600// #pragma vtordisp '(' ')'
1601void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1602 PragmaIntroducerKind Introducer,
1603 Token &Tok) {
1604 SourceLocation VtorDispLoc = Tok.getLocation();
1605 PP.Lex(Tok);
1606 if (Tok.isNot(tok::l_paren)) {
1607 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1608 return;
1609 }
1610 PP.Lex(Tok);
1611
1612 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1613 const IdentifierInfo *II = Tok.getIdentifierInfo();
1614 if (II) {
1615 if (II->isStr("push")) {
1616 // #pragma vtordisp(push, mode)
1617 PP.Lex(Tok);
1618 if (Tok.isNot(tok::comma)) {
1619 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1620 return;
1621 }
1622 PP.Lex(Tok);
1623 Kind = Sema::PVDK_Push;
1624 // not push, could be on/off
1625 } else if (II->isStr("pop")) {
1626 // #pragma vtordisp(pop)
1627 PP.Lex(Tok);
1628 Kind = Sema::PVDK_Pop;
1629 }
1630 // not push or pop, could be on/off
1631 } else {
1632 if (Tok.is(tok::r_paren)) {
1633 // #pragma vtordisp()
1634 Kind = Sema::PVDK_Reset;
1635 }
1636 }
1637
1638
1639 uint64_t Value = 0;
1640 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1641 const IdentifierInfo *II = Tok.getIdentifierInfo();
1642 if (II && II->isStr("off")) {
1643 PP.Lex(Tok);
1644 Value = 0;
1645 } else if (II && II->isStr("on")) {
1646 PP.Lex(Tok);
1647 Value = 1;
1648 } else if (Tok.is(tok::numeric_constant) &&
1649 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1650 if (Value > 2) {
1651 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1652 << 0 << 2 << "vtordisp";
1653 return;
1654 }
1655 } else {
1656 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1657 << "vtordisp";
1658 return;
1659 }
1660 }
1661
1662 // Finish the pragma: ')' $
1663 if (Tok.isNot(tok::r_paren)) {
1664 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1665 return;
1666 }
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001667 SourceLocation EndLoc = Tok.getLocation();
Stephen Hines651f13c2014-04-23 16:59:28 -07001668 PP.Lex(Tok);
1669 if (Tok.isNot(tok::eod)) {
1670 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1671 << "vtordisp";
1672 return;
1673 }
1674
1675 // Enter the annotation.
1676 Token AnnotTok;
1677 AnnotTok.startToken();
1678 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1679 AnnotTok.setLocation(VtorDispLoc);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001680 AnnotTok.setAnnotationEndLoc(EndLoc);
Stephen Hines651f13c2014-04-23 16:59:28 -07001681 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1682 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
1683 PP.EnterToken(AnnotTok);
1684}
1685
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001686/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1687/// an annotation token.
1688void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1689 PragmaIntroducerKind Introducer,
1690 Token &Tok) {
1691 Token EoF, AnnotTok;
1692 EoF.startToken();
1693 EoF.setKind(tok::eof);
1694 AnnotTok.startToken();
1695 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1696 AnnotTok.setLocation(Tok.getLocation());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001697 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001698 SmallVector<Token, 8> TokenVector;
1699 // Suck up all of the tokens before the eod.
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001700 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001701 TokenVector.push_back(Tok);
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001702 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
1703 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001704 // Add a sentinal EoF token to the end of the list.
1705 TokenVector.push_back(EoF);
1706 // We must allocate this array with new because EnterTokenStream is going to
1707 // delete it later.
1708 Token *TokenArray = new Token[TokenVector.size()];
1709 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1710 auto Value = new (PP.getPreprocessorAllocator())
1711 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1712 AnnotTok.setAnnotationValue(Value);
1713 PP.EnterToken(AnnotTok);
1714}
1715
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001716/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1717///
1718/// The syntax is:
1719/// \code
1720/// #pragma detect_mismatch("name", "value")
1721/// \endcode
1722/// Where 'name' and 'value' are quoted strings. The values are embedded in
1723/// the object file and passed along to the linker. If the linker detects a
1724/// mismatch in the object file's values for the given name, a LNK2038 error
1725/// is emitted. See MSDN for more details.
1726void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1727 PragmaIntroducerKind Introducer,
1728 Token &Tok) {
1729 SourceLocation CommentLoc = Tok.getLocation();
1730 PP.Lex(Tok);
1731 if (Tok.isNot(tok::l_paren)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001732 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001733 return;
1734 }
1735
1736 // Read the name to embed, which must be a string literal.
1737 std::string NameString;
1738 if (!PP.LexStringLiteral(Tok, NameString,
1739 "pragma detect_mismatch",
1740 /*MacroExpansion=*/true))
1741 return;
1742
1743 // Read the comma followed by a second string literal.
1744 std::string ValueString;
1745 if (Tok.isNot(tok::comma)) {
1746 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1747 return;
1748 }
1749
1750 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1751 /*MacroExpansion=*/true))
1752 return;
1753
1754 if (Tok.isNot(tok::r_paren)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001755 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001756 return;
1757 }
1758 PP.Lex(Tok); // Eat the r_paren.
1759
1760 if (Tok.isNot(tok::eod)) {
1761 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1762 return;
1763 }
1764
1765 // If the pragma is lexically sound, notify any interested PPCallbacks.
1766 if (PP.getPPCallbacks())
1767 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1768 ValueString);
1769
1770 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1771}
1772
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001773/// \brief Handle the microsoft \#pragma comment extension.
1774///
1775/// The syntax is:
1776/// \code
1777/// #pragma comment(linker, "foo")
1778/// \endcode
1779/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1780/// "foo" is a string, which is fully macro expanded, and permits string
1781/// concatenation, embedded escape characters etc. See MSDN for more details.
1782void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1783 PragmaIntroducerKind Introducer,
1784 Token &Tok) {
1785 SourceLocation CommentLoc = Tok.getLocation();
1786 PP.Lex(Tok);
1787 if (Tok.isNot(tok::l_paren)) {
1788 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1789 return;
1790 }
1791
1792 // Read the identifier.
1793 PP.Lex(Tok);
1794 if (Tok.isNot(tok::identifier)) {
1795 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1796 return;
1797 }
1798
1799 // Verify that this is one of the 5 whitelisted options.
Reid Kleckner3190ca92013-05-08 13:44:39 +00001800 IdentifierInfo *II = Tok.getIdentifierInfo();
1801 Sema::PragmaMSCommentKind Kind =
1802 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1803 .Case("linker", Sema::PCK_Linker)
1804 .Case("lib", Sema::PCK_Lib)
1805 .Case("compiler", Sema::PCK_Compiler)
1806 .Case("exestr", Sema::PCK_ExeStr)
1807 .Case("user", Sema::PCK_User)
1808 .Default(Sema::PCK_Unknown);
1809 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001810 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1811 return;
1812 }
1813
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07001814 // On PS4, issue a warning about any pragma comments other than
1815 // #pragma comment lib.
1816 if (PP.getTargetInfo().getTriple().isPS4() && Kind != Sema::PCK_Lib) {
1817 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
1818 << II->getName();
1819 return;
1820 }
1821
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001822 // Read the optional string if present.
1823 PP.Lex(Tok);
1824 std::string ArgumentString;
1825 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1826 "pragma comment",
1827 /*MacroExpansion=*/true))
1828 return;
1829
Reid Kleckner3190ca92013-05-08 13:44:39 +00001830 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001831 // FIXME: If the kind is "compiler" warn if the string is present (it is
1832 // ignored).
Reid Kleckner3190ca92013-05-08 13:44:39 +00001833 // The MSDN docs say that "lib" and "linker" require a string and have a short
1834 // whitelist of linker options they support, but in practice MSVC doesn't
1835 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001836
1837 if (Tok.isNot(tok::r_paren)) {
1838 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1839 return;
1840 }
1841 PP.Lex(Tok); // eat the r_paren.
1842
1843 if (Tok.isNot(tok::eod)) {
1844 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1845 return;
1846 }
1847
1848 // If the pragma is lexically sound, notify any interested PPCallbacks.
1849 if (PP.getPPCallbacks())
1850 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001851
1852 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001853}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001854
1855// #pragma clang optimize off
1856// #pragma clang optimize on
1857void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1858 PragmaIntroducerKind Introducer,
1859 Token &FirstToken) {
1860 Token Tok;
1861 PP.Lex(Tok);
1862 if (Tok.is(tok::eod)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001863 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
1864 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001865 return;
1866 }
1867 if (Tok.isNot(tok::identifier)) {
1868 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1869 << PP.getSpelling(Tok);
1870 return;
1871 }
1872 const IdentifierInfo *II = Tok.getIdentifierInfo();
1873 // The only accepted values are 'on' or 'off'.
1874 bool IsOn = false;
1875 if (II->isStr("on")) {
1876 IsOn = true;
1877 } else if (!II->isStr("off")) {
1878 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1879 << PP.getSpelling(Tok);
1880 return;
1881 }
1882 PP.Lex(Tok);
1883
1884 if (Tok.isNot(tok::eod)) {
1885 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1886 << PP.getSpelling(Tok);
1887 return;
1888 }
1889
1890 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1891}
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001892
Stephen Hines176edba2014-12-01 14:53:08 -08001893/// \brief Parses loop or unroll pragma hint value and fills in Info.
1894static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1895 Token Option, bool ValueInParens,
1896 PragmaLoopHintInfo &Info) {
1897 SmallVector<Token, 1> ValueList;
1898 int OpenParens = ValueInParens ? 1 : 0;
1899 // Read constant expression.
1900 while (Tok.isNot(tok::eod)) {
1901 if (Tok.is(tok::l_paren))
1902 OpenParens++;
1903 else if (Tok.is(tok::r_paren)) {
1904 OpenParens--;
1905 if (OpenParens == 0 && ValueInParens)
1906 break;
1907 }
1908
1909 ValueList.push_back(Tok);
1910 PP.Lex(Tok);
1911 }
1912
1913 if (ValueInParens) {
1914 // Read ')'
1915 if (Tok.isNot(tok::r_paren)) {
1916 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1917 return true;
1918 }
1919 PP.Lex(Tok);
1920 }
1921
1922 Token EOFTok;
1923 EOFTok.startToken();
1924 EOFTok.setKind(tok::eof);
1925 EOFTok.setLocation(Tok.getLocation());
1926 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1927
1928 Token *TokenArray = (Token *)PP.getPreprocessorAllocator().Allocate(
1929 ValueList.size() * sizeof(Token), llvm::alignOf<Token>());
1930 std::copy(ValueList.begin(), ValueList.end(), TokenArray);
1931 Info.Toks = TokenArray;
1932 Info.TokSize = ValueList.size();
1933
1934 Info.PragmaName = PragmaName;
1935 Info.Option = Option;
1936 return false;
1937}
1938
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001939/// \brief Handle the \#pragma clang loop directive.
1940/// #pragma clang 'loop' loop-hints
1941///
1942/// loop-hints:
1943/// loop-hint loop-hints[opt]
1944///
1945/// loop-hint:
1946/// 'vectorize' '(' loop-hint-keyword ')'
1947/// 'interleave' '(' loop-hint-keyword ')'
Stephen Hines176edba2014-12-01 14:53:08 -08001948/// 'unroll' '(' unroll-hint-keyword ')'
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001949/// 'vectorize_width' '(' loop-hint-value ')'
1950/// 'interleave_count' '(' loop-hint-value ')'
1951/// 'unroll_count' '(' loop-hint-value ')'
1952///
1953/// loop-hint-keyword:
1954/// 'enable'
1955/// 'disable'
1956///
Stephen Hines176edba2014-12-01 14:53:08 -08001957/// unroll-hint-keyword:
1958/// 'full'
1959/// 'disable'
1960///
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001961/// loop-hint-value:
1962/// constant-expression
1963///
1964/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1965/// try vectorizing the instructions of the loop it precedes. Specifying
1966/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1967/// interleaving multiple iterations of the loop it precedes. The width of the
1968/// vector instructions is specified by vectorize_width() and the number of
1969/// interleaved loop iterations is specified by interleave_count(). Specifying a
1970/// value of 1 effectively disables vectorization/interleaving, even if it is
1971/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1972/// only works on inner loops.
1973///
1974/// The unroll and unroll_count directives control the concatenation
Stephen Hines176edba2014-12-01 14:53:08 -08001975/// unroller. Specifying unroll(full) instructs llvm to try to
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001976/// unroll the loop completely, and unroll(disable) disables unrolling
1977/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1978/// try to unroll the loop the number of times indicated by the value.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001979void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1980 PragmaIntroducerKind Introducer,
1981 Token &Tok) {
Stephen Hines176edba2014-12-01 14:53:08 -08001982 // Incoming token is "loop" from "#pragma clang loop".
1983 Token PragmaName = Tok;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001984 SmallVector<Token, 1> TokenList;
1985
1986 // Lex the optimization option and verify it is an identifier.
1987 PP.Lex(Tok);
1988 if (Tok.isNot(tok::identifier)) {
1989 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1990 << /*MissingOption=*/true << "";
1991 return;
1992 }
1993
1994 while (Tok.is(tok::identifier)) {
1995 Token Option = Tok;
1996 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1997
1998 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Stephen Hines176edba2014-12-01 14:53:08 -08001999 .Case("vectorize", true)
2000 .Case("interleave", true)
2001 .Case("unroll", true)
2002 .Case("vectorize_width", true)
2003 .Case("interleave_count", true)
2004 .Case("unroll_count", true)
2005 .Default(false);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002006 if (!OptionValid) {
2007 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2008 << /*MissingOption=*/false << OptionInfo;
2009 return;
2010 }
Stephen Hines176edba2014-12-01 14:53:08 -08002011 PP.Lex(Tok);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002012
2013 // Read '('
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002014 if (Tok.isNot(tok::l_paren)) {
2015 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2016 return;
2017 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002018 PP.Lex(Tok);
2019
2020 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
Stephen Hines176edba2014-12-01 14:53:08 -08002021 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2022 *Info))
2023 return;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002024
Stephen Hines176edba2014-12-01 14:53:08 -08002025 // Generate the loop hint token.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002026 Token LoopHintTok;
2027 LoopHintTok.startToken();
2028 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Stephen Hines176edba2014-12-01 14:53:08 -08002029 LoopHintTok.setLocation(PragmaName.getLocation());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002030 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002031 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2032 TokenList.push_back(LoopHintTok);
2033 }
2034
2035 if (Tok.isNot(tok::eod)) {
2036 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2037 << "clang loop";
2038 return;
2039 }
2040
2041 Token *TokenArray = new Token[TokenList.size()];
2042 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
2043
2044 PP.EnterTokenStream(TokenArray, TokenList.size(),
2045 /*DisableMacroExpansion=*/false,
2046 /*OwnsTokens=*/true);
2047}
Stephen Hines176edba2014-12-01 14:53:08 -08002048
2049/// \brief Handle the loop unroll optimization pragmas.
2050/// #pragma unroll
2051/// #pragma unroll unroll-hint-value
2052/// #pragma unroll '(' unroll-hint-value ')'
2053/// #pragma nounroll
2054///
2055/// unroll-hint-value:
2056/// constant-expression
2057///
2058/// Loop unrolling hints can be specified with '#pragma unroll' or
2059/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2060/// contained in parentheses. With no argument the directive instructs llvm to
2061/// try to unroll the loop completely. A positive integer argument can be
2062/// specified to indicate the number of times the loop should be unrolled. To
2063/// maximize compatibility with other compilers the unroll count argument can be
2064/// specified with or without parentheses. Specifying, '#pragma nounroll'
2065/// disables unrolling of the loop.
2066void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2067 PragmaIntroducerKind Introducer,
2068 Token &Tok) {
2069 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2070 // "#pragma nounroll".
2071 Token PragmaName = Tok;
2072 PP.Lex(Tok);
2073 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2074 if (Tok.is(tok::eod)) {
2075 // nounroll or unroll pragma without an argument.
2076 Info->PragmaName = PragmaName;
2077 Info->Option.startToken();
2078 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2079 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2080 << "nounroll";
2081 return;
2082 } else {
2083 // Unroll pragma with an argument: "#pragma unroll N" or
2084 // "#pragma unroll(N)".
2085 // Read '(' if it exists.
2086 bool ValueInParens = Tok.is(tok::l_paren);
2087 if (ValueInParens)
2088 PP.Lex(Tok);
2089
2090 Token Option;
2091 Option.startToken();
2092 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
2093 return;
2094
2095 // In CUDA, the argument to '#pragma unroll' should not be contained in
2096 // parentheses.
2097 if (PP.getLangOpts().CUDA && ValueInParens)
2098 PP.Diag(Info->Toks[0].getLocation(),
2099 diag::warn_pragma_unroll_cuda_value_in_parens);
2100
2101 if (Tok.isNot(tok::eod)) {
2102 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2103 << "unroll";
2104 return;
2105 }
2106 }
2107
2108 // Generate the hint token.
2109 Token *TokenArray = new Token[1];
2110 TokenArray[0].startToken();
2111 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2112 TokenArray[0].setLocation(PragmaName.getLocation());
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -07002113 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Stephen Hines176edba2014-12-01 14:53:08 -08002114 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2115 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2116 /*OwnsTokens=*/true);
2117}