blob: 5f89f01598d0ca832169dbb6bfa6f11f020b561c [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
201 if (getLangOpts().MicrosoftExt) {
202 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
203 PP.AddPragmaHandler(MSCommentHandler.get());
204 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
205 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
206 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
207 PP.AddPragmaHandler(MSPointersToMembers.get());
208 MSVtorDisp.reset(new PragmaMSVtorDisp());
209 PP.AddPragmaHandler(MSVtorDisp.get());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700210 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
211 PP.AddPragmaHandler(MSInitSeg.get());
212 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
213 PP.AddPragmaHandler(MSDataSeg.get());
214 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
215 PP.AddPragmaHandler(MSBSSSeg.get());
216 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
217 PP.AddPragmaHandler(MSConstSeg.get());
218 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
219 PP.AddPragmaHandler(MSCodeSeg.get());
220 MSSection.reset(new PragmaMSPragma("section"));
221 PP.AddPragmaHandler(MSSection.get());
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700222 } else if (getTargetInfo().getTriple().isPS4()) {
223 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
224 PP.AddPragmaHandler(MSCommentHandler.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
267 if (getLangOpts().MicrosoftExt) {
268 PP.RemovePragmaHandler(MSCommentHandler.get());
269 MSCommentHandler.reset();
270 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
271 MSDetectMismatchHandler.reset();
272 PP.RemovePragmaHandler(MSPointersToMembers.get());
273 MSPointersToMembers.reset();
274 PP.RemovePragmaHandler(MSVtorDisp.get());
275 MSVtorDisp.reset();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700276 PP.RemovePragmaHandler(MSInitSeg.get());
277 MSInitSeg.reset();
278 PP.RemovePragmaHandler(MSDataSeg.get());
279 MSDataSeg.reset();
280 PP.RemovePragmaHandler(MSBSSSeg.get());
281 MSBSSSeg.reset();
282 PP.RemovePragmaHandler(MSConstSeg.get());
283 MSConstSeg.reset();
284 PP.RemovePragmaHandler(MSCodeSeg.get());
285 MSCodeSeg.reset();
286 PP.RemovePragmaHandler(MSSection.get());
287 MSSection.reset();
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700288 } else if (getTargetInfo().getTriple().isPS4()) {
289 PP.RemovePragmaHandler(MSCommentHandler.get());
290 MSCommentHandler.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 }
Joerg Sonnenbergere23af2a2011-07-20 01:03:50 +0000909 PP.LexUnexpandedToken(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +0000910 if (Tok.isNot(tok::eod)) {
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000911 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
912 << "visibility";
913 return;
914 }
915
Rafael Espindola426fc942012-01-26 02:02:57 +0000916 Token *Toks = new Token[1];
917 Toks[0].startToken();
918 Toks[0].setKind(tok::annot_pragma_vis);
919 Toks[0].setLocation(VisLoc);
920 Toks[0].setAnnotationValue(
921 const_cast<void*>(static_cast<const void*>(VisType)));
922 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
923 /*OwnsTokens=*/true);
Eli Friedmanaa8b0d12010-08-05 06:57:20 +0000924}
925
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000926// #pragma pack(...) comes in the following delicious flavors:
927// pack '(' [integer] ')'
928// pack '(' 'show' ')'
929// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregor80c60f72010-09-09 22:45:38 +0000930void PragmaPackHandler::HandlePragma(Preprocessor &PP,
931 PragmaIntroducerKind Introducer,
932 Token &PackTok) {
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000933 SourceLocation PackLoc = PackTok.getLocation();
934
935 Token Tok;
936 PP.Lex(Tok);
937 if (Tok.isNot(tok::l_paren)) {
Ted Kremenek4726d032009-03-23 22:28:25 +0000938 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000939 return;
940 }
941
John McCallf312b1e2010-08-26 23:41:50 +0000942 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700943 IdentifierInfo *Name = nullptr;
Eli Friedman9595c7e2012-10-04 02:36:51 +0000944 Token Alignment;
945 Alignment.startToken();
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000946 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000947 PP.Lex(Tok);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000948 if (Tok.is(tok::numeric_constant)) {
Eli Friedman9595c7e2012-10-04 02:36:51 +0000949 Alignment = Tok;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000950
951 PP.Lex(Tok);
Eli Friedman19bda3a2011-11-02 01:53:16 +0000952
953 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
954 // the push/pop stack.
955 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikie4e4d0842012-03-11 07:00:24 +0000956 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman19bda3a2011-11-02 01:53:16 +0000957 Kind = Sema::PPK_Push;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000958 } else if (Tok.is(tok::identifier)) {
959 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner08631c52008-11-23 21:45:46 +0000960 if (II->isStr("show")) {
John McCallf312b1e2010-08-26 23:41:50 +0000961 Kind = Sema::PPK_Show;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000962 PP.Lex(Tok);
963 } else {
Chris Lattner08631c52008-11-23 21:45:46 +0000964 if (II->isStr("push")) {
John McCallf312b1e2010-08-26 23:41:50 +0000965 Kind = Sema::PPK_Push;
Chris Lattner08631c52008-11-23 21:45:46 +0000966 } else if (II->isStr("pop")) {
John McCallf312b1e2010-08-26 23:41:50 +0000967 Kind = Sema::PPK_Pop;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000968 } else {
Stephen Hines651f13c2014-04-23 16:59:28 -0700969 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000970 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000971 }
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000972 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000974 if (Tok.is(tok::comma)) {
975 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000977 if (Tok.is(tok::numeric_constant)) {
Eli Friedman9595c7e2012-10-04 02:36:51 +0000978 Alignment = Tok;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000979
980 PP.Lex(Tok);
981 } else if (Tok.is(tok::identifier)) {
982 Name = Tok.getIdentifierInfo();
983 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000985 if (Tok.is(tok::comma)) {
986 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000988 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattner08631c52008-11-23 21:45:46 +0000989 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000990 return;
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Eli Friedman9595c7e2012-10-04 02:36:51 +0000993 Alignment = Tok;
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000994
995 PP.Lex(Tok);
996 }
997 } else {
Chris Lattner08631c52008-11-23 21:45:46 +0000998 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +0000999 return;
1000 }
1001 }
1002 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001003 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman19bda3a2011-11-02 01:53:16 +00001004 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1005 // the push/pop stack.
1006 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1007 Kind = Sema::PPK_Pop;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001008 }
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +00001009
1010 if (Tok.isNot(tok::r_paren)) {
Ted Kremenek4726d032009-03-23 22:28:25 +00001011 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +00001012 return;
1013 }
1014
Daniel Dunbar861800c2010-05-26 23:29:06 +00001015 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedman99914792009-06-05 00:49:58 +00001016 PP.Lex(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +00001017 if (Tok.isNot(tok::eod)) {
Eli Friedman99914792009-06-05 00:49:58 +00001018 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1019 return;
1020 }
1021
Daniel Dunbarb0939552012-02-29 01:38:22 +00001022 PragmaPackInfo *Info =
1023 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
1024 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
1025 new (Info) PragmaPackInfo();
Eli Friedmanaa5ab262012-02-23 23:47:16 +00001026 Info->Kind = Kind;
1027 Info->Name = Name;
Eli Friedman9595c7e2012-10-04 02:36:51 +00001028 Info->Alignment = Alignment;
Eli Friedmanaa5ab262012-02-23 23:47:16 +00001029 Info->LParenLoc = LParenLoc;
1030 Info->RParenLoc = RParenLoc;
1031
Daniel Dunbarb0939552012-02-29 01:38:22 +00001032 Token *Toks =
1033 (Token*) PP.getPreprocessorAllocator().Allocate(
1034 sizeof(Token) * 1, llvm::alignOf<Token>());
1035 new (Toks) Token();
Eli Friedmanaa5ab262012-02-23 23:47:16 +00001036 Toks[0].startToken();
1037 Toks[0].setKind(tok::annot_pragma_pack);
1038 Toks[0].setLocation(PackLoc);
1039 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1040 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbarb0939552012-02-29 01:38:22 +00001041 /*OwnsTokens=*/false);
Daniel Dunbarfcdd8fe2008-10-04 19:21:03 +00001042}
1043
Fariborz Jahanian62c92582011-04-25 18:49:15 +00001044// #pragma ms_struct on
1045// #pragma ms_struct off
1046void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1047 PragmaIntroducerKind Introducer,
1048 Token &MSStructTok) {
1049 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
1050
1051 Token Tok;
1052 PP.Lex(Tok);
1053 if (Tok.isNot(tok::identifier)) {
1054 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1055 return;
1056 }
1057 const IdentifierInfo *II = Tok.getIdentifierInfo();
1058 if (II->isStr("on")) {
1059 Kind = Sema::PMSST_ON;
1060 PP.Lex(Tok);
1061 }
1062 else if (II->isStr("off") || II->isStr("reset"))
1063 PP.Lex(Tok);
1064 else {
1065 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1066 return;
1067 }
1068
1069 if (Tok.isNot(tok::eod)) {
Daniel Dunbarb0939552012-02-29 01:38:22 +00001070 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1071 << "ms_struct";
Fariborz Jahanian62c92582011-04-25 18:49:15 +00001072 return;
1073 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001074
1075 Token *Toks =
1076 (Token*) PP.getPreprocessorAllocator().Allocate(
1077 sizeof(Token) * 1, llvm::alignOf<Token>());
1078 new (Toks) Token();
1079 Toks[0].startToken();
1080 Toks[0].setKind(tok::annot_pragma_msstruct);
1081 Toks[0].setLocation(MSStructTok.getLocation());
1082 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1083 static_cast<uintptr_t>(Kind)));
1084 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1085 /*OwnsTokens=*/false);
Fariborz Jahanian62c92582011-04-25 18:49:15 +00001086}
1087
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001088// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1089// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman9595c7e2012-10-04 02:36:51 +00001090static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001091 bool IsOptions) {
Daniel Dunbar861800c2010-05-26 23:29:06 +00001092 Token Tok;
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001093
1094 if (IsOptions) {
1095 PP.Lex(Tok);
1096 if (Tok.isNot(tok::identifier) ||
1097 !Tok.getIdentifierInfo()->isStr("align")) {
1098 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1099 return;
1100 }
Daniel Dunbar861800c2010-05-26 23:29:06 +00001101 }
Daniel Dunbar638e7cf2010-05-27 18:42:09 +00001102
Daniel Dunbar861800c2010-05-26 23:29:06 +00001103 PP.Lex(Tok);
1104 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001105 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1106 << IsOptions;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001107 return;
1108 }
1109
1110 PP.Lex(Tok);
1111 if (Tok.isNot(tok::identifier)) {
1112 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001113 << (IsOptions ? "options" : "align");
Daniel Dunbar861800c2010-05-26 23:29:06 +00001114 return;
1115 }
1116
John McCallf312b1e2010-08-26 23:41:50 +00001117 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001118 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar638e7cf2010-05-27 18:42:09 +00001119 if (II->isStr("native"))
John McCallf312b1e2010-08-26 23:41:50 +00001120 Kind = Sema::POAK_Native;
Daniel Dunbar638e7cf2010-05-27 18:42:09 +00001121 else if (II->isStr("natural"))
John McCallf312b1e2010-08-26 23:41:50 +00001122 Kind = Sema::POAK_Natural;
Daniel Dunbar6f739142010-05-27 18:42:17 +00001123 else if (II->isStr("packed"))
John McCallf312b1e2010-08-26 23:41:50 +00001124 Kind = Sema::POAK_Packed;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001125 else if (II->isStr("power"))
John McCallf312b1e2010-08-26 23:41:50 +00001126 Kind = Sema::POAK_Power;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001127 else if (II->isStr("mac68k"))
John McCallf312b1e2010-08-26 23:41:50 +00001128 Kind = Sema::POAK_Mac68k;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001129 else if (II->isStr("reset"))
John McCallf312b1e2010-08-26 23:41:50 +00001130 Kind = Sema::POAK_Reset;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001131 else {
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001132 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1133 << IsOptions;
Daniel Dunbar861800c2010-05-26 23:29:06 +00001134 return;
1135 }
1136
Daniel Dunbar861800c2010-05-26 23:29:06 +00001137 PP.Lex(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +00001138 if (Tok.isNot(tok::eod)) {
Daniel Dunbar861800c2010-05-26 23:29:06 +00001139 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001140 << (IsOptions ? "options" : "align");
Daniel Dunbar861800c2010-05-26 23:29:06 +00001141 return;
1142 }
1143
Eli Friedman9595c7e2012-10-04 02:36:51 +00001144 Token *Toks =
1145 (Token*) PP.getPreprocessorAllocator().Allocate(
1146 sizeof(Token) * 1, llvm::alignOf<Token>());
1147 new (Toks) Token();
1148 Toks[0].startToken();
1149 Toks[0].setKind(tok::annot_pragma_align);
1150 Toks[0].setLocation(FirstTok.getLocation());
1151 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1152 static_cast<uintptr_t>(Kind)));
1153 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1154 /*OwnsTokens=*/false);
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001155}
1156
Douglas Gregor80c60f72010-09-09 22:45:38 +00001157void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1158 PragmaIntroducerKind Introducer,
1159 Token &AlignTok) {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001160 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcbb98ed2010-07-31 19:17:07 +00001161}
1162
Douglas Gregor80c60f72010-09-09 22:45:38 +00001163void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1164 PragmaIntroducerKind Introducer,
1165 Token &OptionsTok) {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001166 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar861800c2010-05-26 23:29:06 +00001167}
1168
Ted Kremenek4726d032009-03-23 22:28:25 +00001169// #pragma unused(identifier)
Douglas Gregor80c60f72010-09-09 22:45:38 +00001170void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1171 PragmaIntroducerKind Introducer,
1172 Token &UnusedTok) {
Ted Kremenek4726d032009-03-23 22:28:25 +00001173 // FIXME: Should we be expanding macros here? My guess is no.
1174 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Ted Kremenek4726d032009-03-23 22:28:25 +00001176 // Lex the left '('.
1177 Token Tok;
1178 PP.Lex(Tok);
1179 if (Tok.isNot(tok::l_paren)) {
1180 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1181 return;
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Ted Kremenek4726d032009-03-23 22:28:25 +00001184 // Lex the declaration reference(s).
Chris Lattner5f9e2722011-07-23 10:55:15 +00001185 SmallVector<Token, 5> Identifiers;
Ted Kremenek4726d032009-03-23 22:28:25 +00001186 SourceLocation RParenLoc;
1187 bool LexID = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Ted Kremenek4726d032009-03-23 22:28:25 +00001189 while (true) {
1190 PP.Lex(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001191
Ted Kremenek4726d032009-03-23 22:28:25 +00001192 if (LexID) {
Mike Stump1eb44332009-09-09 15:08:12 +00001193 if (Tok.is(tok::identifier)) {
Ted Kremenek7a02a372009-08-03 23:24:57 +00001194 Identifiers.push_back(Tok);
Ted Kremenek4726d032009-03-23 22:28:25 +00001195 LexID = false;
1196 continue;
1197 }
1198
Ted Kremenek7a02a372009-08-03 23:24:57 +00001199 // Illegal token!
Ted Kremenek4726d032009-03-23 22:28:25 +00001200 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1201 return;
1202 }
Mike Stump1eb44332009-09-09 15:08:12 +00001203
Ted Kremenek4726d032009-03-23 22:28:25 +00001204 // We are execting a ')' or a ','.
1205 if (Tok.is(tok::comma)) {
1206 LexID = true;
1207 continue;
1208 }
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Ted Kremenek4726d032009-03-23 22:28:25 +00001210 if (Tok.is(tok::r_paren)) {
1211 RParenLoc = Tok.getLocation();
1212 break;
1213 }
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Ted Kremenek7a02a372009-08-03 23:24:57 +00001215 // Illegal token!
Stephen Hines651f13c2014-04-23 16:59:28 -07001216 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenek4726d032009-03-23 22:28:25 +00001217 return;
1218 }
Eli Friedman99914792009-06-05 00:49:58 +00001219
1220 PP.Lex(Tok);
Peter Collingbourne84021552011-02-28 02:37:51 +00001221 if (Tok.isNot(tok::eod)) {
Eli Friedman99914792009-06-05 00:49:58 +00001222 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1223 "unused";
1224 return;
1225 }
1226
Ted Kremenek4726d032009-03-23 22:28:25 +00001227 // Verify that we have a location for the right parenthesis.
1228 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenek7a02a372009-08-03 23:24:57 +00001229 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenek4726d032009-03-23 22:28:25 +00001230
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +00001231 // For each identifier token, insert into the token stream a
1232 // annot_pragma_unused token followed by the identifier token.
1233 // This allows us to cache a "#pragma unused" that occurs inside an inline
1234 // C++ member function.
1235
Daniel Dunbarb0939552012-02-29 01:38:22 +00001236 Token *Toks =
1237 (Token*) PP.getPreprocessorAllocator().Allocate(
1238 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisb918d0f2011-01-17 18:58:44 +00001239 for (unsigned i=0; i != Identifiers.size(); i++) {
1240 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1241 pragmaUnusedTok.startToken();
1242 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1243 pragmaUnusedTok.setLocation(UnusedLoc);
1244 idTok = Identifiers[i];
1245 }
Daniel Dunbarb0939552012-02-29 01:38:22 +00001246 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1247 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenek4726d032009-03-23 22:28:25 +00001248}
Eli Friedman99914792009-06-05 00:49:58 +00001249
1250// #pragma weak identifier
1251// #pragma weak identifier '=' identifier
Douglas Gregor80c60f72010-09-09 22:45:38 +00001252void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1253 PragmaIntroducerKind Introducer,
1254 Token &WeakTok) {
Eli Friedman99914792009-06-05 00:49:58 +00001255 SourceLocation WeakLoc = WeakTok.getLocation();
1256
1257 Token Tok;
1258 PP.Lex(Tok);
1259 if (Tok.isNot(tok::identifier)) {
1260 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1261 return;
1262 }
1263
Eli Friedman9595c7e2012-10-04 02:36:51 +00001264 Token WeakName = Tok;
1265 bool HasAlias = false;
1266 Token AliasName;
Eli Friedman99914792009-06-05 00:49:58 +00001267
1268 PP.Lex(Tok);
1269 if (Tok.is(tok::equal)) {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001270 HasAlias = true;
Eli Friedman99914792009-06-05 00:49:58 +00001271 PP.Lex(Tok);
1272 if (Tok.isNot(tok::identifier)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001273 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedman99914792009-06-05 00:49:58 +00001274 << "weak";
1275 return;
1276 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001277 AliasName = Tok;
Eli Friedman99914792009-06-05 00:49:58 +00001278 PP.Lex(Tok);
1279 }
1280
Peter Collingbourne84021552011-02-28 02:37:51 +00001281 if (Tok.isNot(tok::eod)) {
Eli Friedman99914792009-06-05 00:49:58 +00001282 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1283 return;
1284 }
1285
Eli Friedman9595c7e2012-10-04 02:36:51 +00001286 if (HasAlias) {
1287 Token *Toks =
1288 (Token*) PP.getPreprocessorAllocator().Allocate(
1289 sizeof(Token) * 3, llvm::alignOf<Token>());
1290 Token &pragmaUnusedTok = Toks[0];
1291 pragmaUnusedTok.startToken();
1292 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1293 pragmaUnusedTok.setLocation(WeakLoc);
1294 Toks[1] = WeakName;
1295 Toks[2] = AliasName;
1296 PP.EnterTokenStream(Toks, 3,
1297 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedman99914792009-06-05 00:49:58 +00001298 } else {
Eli Friedman9595c7e2012-10-04 02:36:51 +00001299 Token *Toks =
1300 (Token*) PP.getPreprocessorAllocator().Allocate(
1301 sizeof(Token) * 2, llvm::alignOf<Token>());
1302 Token &pragmaUnusedTok = Toks[0];
1303 pragmaUnusedTok.startToken();
1304 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1305 pragmaUnusedTok.setLocation(WeakLoc);
1306 Toks[1] = WeakName;
1307 PP.EnterTokenStream(Toks, 2,
1308 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedman99914792009-06-05 00:49:58 +00001309 }
1310}
Peter Collingbourne321b8172011-02-14 01:42:35 +00001311
David Chisnall5f3c1632012-02-18 16:12:34 +00001312// #pragma redefine_extname identifier identifier
1313void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1314 PragmaIntroducerKind Introducer,
1315 Token &RedefToken) {
1316 SourceLocation RedefLoc = RedefToken.getLocation();
1317
1318 Token Tok;
1319 PP.Lex(Tok);
1320 if (Tok.isNot(tok::identifier)) {
1321 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1322 "redefine_extname";
1323 return;
1324 }
1325
Eli Friedman9595c7e2012-10-04 02:36:51 +00001326 Token RedefName = Tok;
David Chisnall5f3c1632012-02-18 16:12:34 +00001327 PP.Lex(Tok);
Eli Friedman9595c7e2012-10-04 02:36:51 +00001328
David Chisnall5f3c1632012-02-18 16:12:34 +00001329 if (Tok.isNot(tok::identifier)) {
1330 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1331 << "redefine_extname";
1332 return;
1333 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001334
1335 Token AliasName = Tok;
David Chisnall5f3c1632012-02-18 16:12:34 +00001336 PP.Lex(Tok);
1337
1338 if (Tok.isNot(tok::eod)) {
1339 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1340 "redefine_extname";
1341 return;
1342 }
1343
Eli Friedman9595c7e2012-10-04 02:36:51 +00001344 Token *Toks =
1345 (Token*) PP.getPreprocessorAllocator().Allocate(
1346 sizeof(Token) * 3, llvm::alignOf<Token>());
1347 Token &pragmaRedefTok = Toks[0];
1348 pragmaRedefTok.startToken();
1349 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1350 pragmaRedefTok.setLocation(RedefLoc);
1351 Toks[1] = RedefName;
1352 Toks[2] = AliasName;
1353 PP.EnterTokenStream(Toks, 3,
1354 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall5f3c1632012-02-18 16:12:34 +00001355}
1356
1357
Peter Collingbourne321b8172011-02-14 01:42:35 +00001358void
1359PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1360 PragmaIntroducerKind Introducer,
1361 Token &Tok) {
1362 tok::OnOffSwitch OOS;
1363 if (PP.LexOnOffSwitch(OOS))
1364 return;
1365
Eli Friedman9595c7e2012-10-04 02:36:51 +00001366 Token *Toks =
1367 (Token*) PP.getPreprocessorAllocator().Allocate(
1368 sizeof(Token) * 1, llvm::alignOf<Token>());
1369 new (Toks) Token();
1370 Toks[0].startToken();
1371 Toks[0].setKind(tok::annot_pragma_fp_contract);
1372 Toks[0].setLocation(Tok.getLocation());
1373 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1374 static_cast<uintptr_t>(OOS)));
1375 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1376 /*OwnsTokens=*/false);
Peter Collingbourne321b8172011-02-14 01:42:35 +00001377}
Peter Collingbournef315fa82011-02-14 01:42:53 +00001378
1379void
1380PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1381 PragmaIntroducerKind Introducer,
1382 Token &Tok) {
Tanya Lattnerb38b6a72011-04-14 23:35:31 +00001383 PP.LexUnexpandedToken(Tok);
Peter Collingbournef315fa82011-02-14 01:42:53 +00001384 if (Tok.isNot(tok::identifier)) {
1385 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1386 "OPENCL";
1387 return;
1388 }
1389 IdentifierInfo *ename = Tok.getIdentifierInfo();
1390 SourceLocation NameLoc = Tok.getLocation();
1391
1392 PP.Lex(Tok);
1393 if (Tok.isNot(tok::colon)) {
1394 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1395 return;
1396 }
1397
1398 PP.Lex(Tok);
1399 if (Tok.isNot(tok::identifier)) {
1400 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1401 return;
1402 }
1403 IdentifierInfo *op = Tok.getIdentifierInfo();
1404
1405 unsigned state;
1406 if (op->isStr("enable")) {
1407 state = 1;
1408 } else if (op->isStr("disable")) {
1409 state = 0;
1410 } else {
1411 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1412 return;
1413 }
Pekka Jaaskelainena0950e82013-10-12 09:29:48 +00001414 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbournef315fa82011-02-14 01:42:53 +00001415
Eli Friedman9595c7e2012-10-04 02:36:51 +00001416 PP.Lex(Tok);
1417 if (Tok.isNot(tok::eod)) {
1418 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1419 "OPENCL EXTENSION";
Peter Collingbournef315fa82011-02-14 01:42:53 +00001420 return;
1421 }
Eli Friedman9595c7e2012-10-04 02:36:51 +00001422
1423 OpenCLExtData data(ename, state);
1424 Token *Toks =
1425 (Token*) PP.getPreprocessorAllocator().Allocate(
1426 sizeof(Token) * 1, llvm::alignOf<Token>());
1427 new (Toks) Token();
1428 Toks[0].startToken();
1429 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1430 Toks[0].setLocation(NameLoc);
1431 Toks[0].setAnnotationValue(data.getOpaqueValue());
1432 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1433 /*OwnsTokens=*/false);
Pekka Jaaskelainena0950e82013-10-12 09:29:48 +00001434
1435 if (PP.getPPCallbacks())
1436 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1437 StateLoc, state);
Peter Collingbournef315fa82011-02-14 01:42:53 +00001438}
1439
Alexey Bataevc6400582013-03-22 06:34:35 +00001440/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1441///
1442void
1443PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1444 PragmaIntroducerKind Introducer,
1445 Token &FirstTok) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001446 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1447 FirstTok.getLocation())) {
Alexey Bataevc6400582013-03-22 06:34:35 +00001448 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001449 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1450 diag::Severity::Ignored, SourceLocation());
Alexey Bataevc6400582013-03-22 06:34:35 +00001451 }
1452 PP.DiscardUntilEndOfDirective();
1453}
1454
1455/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1456///
1457void
1458PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1459 PragmaIntroducerKind Introducer,
1460 Token &FirstTok) {
1461 SmallVector<Token, 16> Pragma;
1462 Token Tok;
1463 Tok.startToken();
1464 Tok.setKind(tok::annot_pragma_openmp);
1465 Tok.setLocation(FirstTok.getLocation());
1466
1467 while (Tok.isNot(tok::eod)) {
1468 Pragma.push_back(Tok);
1469 PP.Lex(Tok);
1470 }
1471 SourceLocation EodLoc = Tok.getLocation();
1472 Tok.startToken();
1473 Tok.setKind(tok::annot_pragma_openmp_end);
1474 Tok.setLocation(EodLoc);
1475 Pragma.push_back(Tok);
1476
1477 Token *Toks = new Token[Pragma.size()];
1478 std::copy(Pragma.begin(), Pragma.end(), Toks);
1479 PP.EnterTokenStream(Toks, Pragma.size(),
1480 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1481}
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001482
Stephen Hines651f13c2014-04-23 16:59:28 -07001483/// \brief Handle '#pragma pointers_to_members'
1484// The grammar for this pragma is as follows:
1485//
1486// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1487//
1488// #pragma pointers_to_members '(' 'best_case' ')'
1489// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1490// #pragma pointers_to_members '(' inheritance-model ')'
1491void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1492 PragmaIntroducerKind Introducer,
1493 Token &Tok) {
1494 SourceLocation PointersToMembersLoc = Tok.getLocation();
1495 PP.Lex(Tok);
1496 if (Tok.isNot(tok::l_paren)) {
1497 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1498 << "pointers_to_members";
1499 return;
1500 }
1501 PP.Lex(Tok);
1502 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1503 if (!Arg) {
1504 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1505 << "pointers_to_members";
1506 return;
1507 }
1508 PP.Lex(Tok);
1509
1510 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
1511 if (Arg->isStr("best_case")) {
1512 RepresentationMethod = LangOptions::PPTMK_BestCase;
1513 } else {
1514 if (Arg->isStr("full_generality")) {
1515 if (Tok.is(tok::comma)) {
1516 PP.Lex(Tok);
1517
1518 Arg = Tok.getIdentifierInfo();
1519 if (!Arg) {
1520 PP.Diag(Tok.getLocation(),
1521 diag::err_pragma_pointers_to_members_unknown_kind)
1522 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1523 return;
1524 }
1525 PP.Lex(Tok);
1526 } else if (Tok.is(tok::r_paren)) {
1527 // #pragma pointers_to_members(full_generality) implicitly specifies
1528 // virtual_inheritance.
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001529 Arg = nullptr;
Stephen Hines651f13c2014-04-23 16:59:28 -07001530 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
1531 } else {
1532 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1533 << "full_generality";
1534 return;
1535 }
1536 }
1537
1538 if (Arg) {
1539 if (Arg->isStr("single_inheritance")) {
1540 RepresentationMethod =
1541 LangOptions::PPTMK_FullGeneralitySingleInheritance;
1542 } else if (Arg->isStr("multiple_inheritance")) {
1543 RepresentationMethod =
1544 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
1545 } else if (Arg->isStr("virtual_inheritance")) {
1546 RepresentationMethod =
1547 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
1548 } else {
1549 PP.Diag(Tok.getLocation(),
1550 diag::err_pragma_pointers_to_members_unknown_kind)
1551 << Arg << /*HasPointerDeclaration*/ 1;
1552 return;
1553 }
1554 }
1555 }
1556
1557 if (Tok.isNot(tok::r_paren)) {
1558 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1559 << (Arg ? Arg->getName() : "full_generality");
1560 return;
1561 }
1562
1563 PP.Lex(Tok);
1564 if (Tok.isNot(tok::eod)) {
1565 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1566 << "pointers_to_members";
1567 return;
1568 }
1569
1570 Token AnnotTok;
1571 AnnotTok.startToken();
1572 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1573 AnnotTok.setLocation(PointersToMembersLoc);
1574 AnnotTok.setAnnotationValue(
1575 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1576 PP.EnterToken(AnnotTok);
1577}
1578
1579/// \brief Handle '#pragma vtordisp'
1580// The grammar for this pragma is as follows:
1581//
1582// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1583//
1584// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1585// #pragma vtordisp '(' 'pop' ')'
1586// #pragma vtordisp '(' ')'
1587void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1588 PragmaIntroducerKind Introducer,
1589 Token &Tok) {
1590 SourceLocation VtorDispLoc = Tok.getLocation();
1591 PP.Lex(Tok);
1592 if (Tok.isNot(tok::l_paren)) {
1593 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1594 return;
1595 }
1596 PP.Lex(Tok);
1597
1598 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1599 const IdentifierInfo *II = Tok.getIdentifierInfo();
1600 if (II) {
1601 if (II->isStr("push")) {
1602 // #pragma vtordisp(push, mode)
1603 PP.Lex(Tok);
1604 if (Tok.isNot(tok::comma)) {
1605 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1606 return;
1607 }
1608 PP.Lex(Tok);
1609 Kind = Sema::PVDK_Push;
1610 // not push, could be on/off
1611 } else if (II->isStr("pop")) {
1612 // #pragma vtordisp(pop)
1613 PP.Lex(Tok);
1614 Kind = Sema::PVDK_Pop;
1615 }
1616 // not push or pop, could be on/off
1617 } else {
1618 if (Tok.is(tok::r_paren)) {
1619 // #pragma vtordisp()
1620 Kind = Sema::PVDK_Reset;
1621 }
1622 }
1623
1624
1625 uint64_t Value = 0;
1626 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1627 const IdentifierInfo *II = Tok.getIdentifierInfo();
1628 if (II && II->isStr("off")) {
1629 PP.Lex(Tok);
1630 Value = 0;
1631 } else if (II && II->isStr("on")) {
1632 PP.Lex(Tok);
1633 Value = 1;
1634 } else if (Tok.is(tok::numeric_constant) &&
1635 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1636 if (Value > 2) {
1637 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1638 << 0 << 2 << "vtordisp";
1639 return;
1640 }
1641 } else {
1642 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1643 << "vtordisp";
1644 return;
1645 }
1646 }
1647
1648 // Finish the pragma: ')' $
1649 if (Tok.isNot(tok::r_paren)) {
1650 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1651 return;
1652 }
1653 PP.Lex(Tok);
1654 if (Tok.isNot(tok::eod)) {
1655 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1656 << "vtordisp";
1657 return;
1658 }
1659
1660 // Enter the annotation.
1661 Token AnnotTok;
1662 AnnotTok.startToken();
1663 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1664 AnnotTok.setLocation(VtorDispLoc);
1665 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1666 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
1667 PP.EnterToken(AnnotTok);
1668}
1669
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001670/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1671/// an annotation token.
1672void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1673 PragmaIntroducerKind Introducer,
1674 Token &Tok) {
1675 Token EoF, AnnotTok;
1676 EoF.startToken();
1677 EoF.setKind(tok::eof);
1678 AnnotTok.startToken();
1679 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1680 AnnotTok.setLocation(Tok.getLocation());
1681 SmallVector<Token, 8> TokenVector;
1682 // Suck up all of the tokens before the eod.
1683 for (; Tok.isNot(tok::eod); PP.Lex(Tok))
1684 TokenVector.push_back(Tok);
1685 // Add a sentinal EoF token to the end of the list.
1686 TokenVector.push_back(EoF);
1687 // We must allocate this array with new because EnterTokenStream is going to
1688 // delete it later.
1689 Token *TokenArray = new Token[TokenVector.size()];
1690 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1691 auto Value = new (PP.getPreprocessorAllocator())
1692 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1693 AnnotTok.setAnnotationValue(Value);
1694 PP.EnterToken(AnnotTok);
1695}
1696
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001697/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1698///
1699/// The syntax is:
1700/// \code
1701/// #pragma detect_mismatch("name", "value")
1702/// \endcode
1703/// Where 'name' and 'value' are quoted strings. The values are embedded in
1704/// the object file and passed along to the linker. If the linker detects a
1705/// mismatch in the object file's values for the given name, a LNK2038 error
1706/// is emitted. See MSDN for more details.
1707void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1708 PragmaIntroducerKind Introducer,
1709 Token &Tok) {
1710 SourceLocation CommentLoc = Tok.getLocation();
1711 PP.Lex(Tok);
1712 if (Tok.isNot(tok::l_paren)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001713 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001714 return;
1715 }
1716
1717 // Read the name to embed, which must be a string literal.
1718 std::string NameString;
1719 if (!PP.LexStringLiteral(Tok, NameString,
1720 "pragma detect_mismatch",
1721 /*MacroExpansion=*/true))
1722 return;
1723
1724 // Read the comma followed by a second string literal.
1725 std::string ValueString;
1726 if (Tok.isNot(tok::comma)) {
1727 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1728 return;
1729 }
1730
1731 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1732 /*MacroExpansion=*/true))
1733 return;
1734
1735 if (Tok.isNot(tok::r_paren)) {
Stephen Hines651f13c2014-04-23 16:59:28 -07001736 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballmana7ff62f2013-06-04 02:07:14 +00001737 return;
1738 }
1739 PP.Lex(Tok); // Eat the r_paren.
1740
1741 if (Tok.isNot(tok::eod)) {
1742 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1743 return;
1744 }
1745
1746 // If the pragma is lexically sound, notify any interested PPCallbacks.
1747 if (PP.getPPCallbacks())
1748 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1749 ValueString);
1750
1751 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1752}
1753
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001754/// \brief Handle the microsoft \#pragma comment extension.
1755///
1756/// The syntax is:
1757/// \code
1758/// #pragma comment(linker, "foo")
1759/// \endcode
1760/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1761/// "foo" is a string, which is fully macro expanded, and permits string
1762/// concatenation, embedded escape characters etc. See MSDN for more details.
1763void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1764 PragmaIntroducerKind Introducer,
1765 Token &Tok) {
1766 SourceLocation CommentLoc = Tok.getLocation();
1767 PP.Lex(Tok);
1768 if (Tok.isNot(tok::l_paren)) {
1769 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1770 return;
1771 }
1772
1773 // Read the identifier.
1774 PP.Lex(Tok);
1775 if (Tok.isNot(tok::identifier)) {
1776 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1777 return;
1778 }
1779
1780 // Verify that this is one of the 5 whitelisted options.
Reid Kleckner3190ca92013-05-08 13:44:39 +00001781 IdentifierInfo *II = Tok.getIdentifierInfo();
1782 Sema::PragmaMSCommentKind Kind =
1783 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1784 .Case("linker", Sema::PCK_Linker)
1785 .Case("lib", Sema::PCK_Lib)
1786 .Case("compiler", Sema::PCK_Compiler)
1787 .Case("exestr", Sema::PCK_ExeStr)
1788 .Case("user", Sema::PCK_User)
1789 .Default(Sema::PCK_Unknown);
1790 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001791 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1792 return;
1793 }
1794
1795 // Read the optional string if present.
1796 PP.Lex(Tok);
1797 std::string ArgumentString;
1798 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1799 "pragma comment",
1800 /*MacroExpansion=*/true))
1801 return;
1802
Reid Kleckner3190ca92013-05-08 13:44:39 +00001803 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001804 // FIXME: If the kind is "compiler" warn if the string is present (it is
1805 // ignored).
Reid Kleckner3190ca92013-05-08 13:44:39 +00001806 // The MSDN docs say that "lib" and "linker" require a string and have a short
1807 // whitelist of linker options they support, but in practice MSVC doesn't
1808 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001809
1810 if (Tok.isNot(tok::r_paren)) {
1811 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1812 return;
1813 }
1814 PP.Lex(Tok); // eat the r_paren.
1815
1816 if (Tok.isNot(tok::eod)) {
1817 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1818 return;
1819 }
1820
1821 // If the pragma is lexically sound, notify any interested PPCallbacks.
1822 if (PP.getPPCallbacks())
1823 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
Reid Kleckner3190ca92013-05-08 13:44:39 +00001824
1825 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner7adf79a2013-05-06 21:02:12 +00001826}
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001827
1828// #pragma clang optimize off
1829// #pragma clang optimize on
1830void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1831 PragmaIntroducerKind Introducer,
1832 Token &FirstToken) {
1833 Token Tok;
1834 PP.Lex(Tok);
1835 if (Tok.is(tok::eod)) {
Stephen Hines176edba2014-12-01 14:53:08 -08001836 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
1837 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Stephen Hines6bcf27b2014-05-29 04:14:42 -07001838 return;
1839 }
1840 if (Tok.isNot(tok::identifier)) {
1841 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1842 << PP.getSpelling(Tok);
1843 return;
1844 }
1845 const IdentifierInfo *II = Tok.getIdentifierInfo();
1846 // The only accepted values are 'on' or 'off'.
1847 bool IsOn = false;
1848 if (II->isStr("on")) {
1849 IsOn = true;
1850 } else if (!II->isStr("off")) {
1851 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1852 << PP.getSpelling(Tok);
1853 return;
1854 }
1855 PP.Lex(Tok);
1856
1857 if (Tok.isNot(tok::eod)) {
1858 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1859 << PP.getSpelling(Tok);
1860 return;
1861 }
1862
1863 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1864}
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001865
Stephen Hines176edba2014-12-01 14:53:08 -08001866/// \brief Parses loop or unroll pragma hint value and fills in Info.
1867static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1868 Token Option, bool ValueInParens,
1869 PragmaLoopHintInfo &Info) {
1870 SmallVector<Token, 1> ValueList;
1871 int OpenParens = ValueInParens ? 1 : 0;
1872 // Read constant expression.
1873 while (Tok.isNot(tok::eod)) {
1874 if (Tok.is(tok::l_paren))
1875 OpenParens++;
1876 else if (Tok.is(tok::r_paren)) {
1877 OpenParens--;
1878 if (OpenParens == 0 && ValueInParens)
1879 break;
1880 }
1881
1882 ValueList.push_back(Tok);
1883 PP.Lex(Tok);
1884 }
1885
1886 if (ValueInParens) {
1887 // Read ')'
1888 if (Tok.isNot(tok::r_paren)) {
1889 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1890 return true;
1891 }
1892 PP.Lex(Tok);
1893 }
1894
1895 Token EOFTok;
1896 EOFTok.startToken();
1897 EOFTok.setKind(tok::eof);
1898 EOFTok.setLocation(Tok.getLocation());
1899 ValueList.push_back(EOFTok); // Terminates expression for parsing.
1900
1901 Token *TokenArray = (Token *)PP.getPreprocessorAllocator().Allocate(
1902 ValueList.size() * sizeof(Token), llvm::alignOf<Token>());
1903 std::copy(ValueList.begin(), ValueList.end(), TokenArray);
1904 Info.Toks = TokenArray;
1905 Info.TokSize = ValueList.size();
1906
1907 Info.PragmaName = PragmaName;
1908 Info.Option = Option;
1909 return false;
1910}
1911
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001912/// \brief Handle the \#pragma clang loop directive.
1913/// #pragma clang 'loop' loop-hints
1914///
1915/// loop-hints:
1916/// loop-hint loop-hints[opt]
1917///
1918/// loop-hint:
1919/// 'vectorize' '(' loop-hint-keyword ')'
1920/// 'interleave' '(' loop-hint-keyword ')'
Stephen Hines176edba2014-12-01 14:53:08 -08001921/// 'unroll' '(' unroll-hint-keyword ')'
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001922/// 'vectorize_width' '(' loop-hint-value ')'
1923/// 'interleave_count' '(' loop-hint-value ')'
1924/// 'unroll_count' '(' loop-hint-value ')'
1925///
1926/// loop-hint-keyword:
1927/// 'enable'
1928/// 'disable'
1929///
Stephen Hines176edba2014-12-01 14:53:08 -08001930/// unroll-hint-keyword:
1931/// 'full'
1932/// 'disable'
1933///
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001934/// loop-hint-value:
1935/// constant-expression
1936///
1937/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1938/// try vectorizing the instructions of the loop it precedes. Specifying
1939/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1940/// interleaving multiple iterations of the loop it precedes. The width of the
1941/// vector instructions is specified by vectorize_width() and the number of
1942/// interleaved loop iterations is specified by interleave_count(). Specifying a
1943/// value of 1 effectively disables vectorization/interleaving, even if it is
1944/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1945/// only works on inner loops.
1946///
1947/// The unroll and unroll_count directives control the concatenation
Stephen Hines176edba2014-12-01 14:53:08 -08001948/// unroller. Specifying unroll(full) instructs llvm to try to
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001949/// unroll the loop completely, and unroll(disable) disables unrolling
1950/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1951/// try to unroll the loop the number of times indicated by the value.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001952void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1953 PragmaIntroducerKind Introducer,
1954 Token &Tok) {
Stephen Hines176edba2014-12-01 14:53:08 -08001955 // Incoming token is "loop" from "#pragma clang loop".
1956 Token PragmaName = Tok;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001957 SmallVector<Token, 1> TokenList;
1958
1959 // Lex the optimization option and verify it is an identifier.
1960 PP.Lex(Tok);
1961 if (Tok.isNot(tok::identifier)) {
1962 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1963 << /*MissingOption=*/true << "";
1964 return;
1965 }
1966
1967 while (Tok.is(tok::identifier)) {
1968 Token Option = Tok;
1969 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1970
1971 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Stephen Hines176edba2014-12-01 14:53:08 -08001972 .Case("vectorize", true)
1973 .Case("interleave", true)
1974 .Case("unroll", true)
1975 .Case("vectorize_width", true)
1976 .Case("interleave_count", true)
1977 .Case("unroll_count", true)
1978 .Default(false);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001979 if (!OptionValid) {
1980 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1981 << /*MissingOption=*/false << OptionInfo;
1982 return;
1983 }
Stephen Hines176edba2014-12-01 14:53:08 -08001984 PP.Lex(Tok);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001985
1986 // Read '('
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001987 if (Tok.isNot(tok::l_paren)) {
1988 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
1989 return;
1990 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001991 PP.Lex(Tok);
1992
1993 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
Stephen Hines176edba2014-12-01 14:53:08 -08001994 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
1995 *Info))
1996 return;
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001997
Stephen Hines176edba2014-12-01 14:53:08 -08001998 // Generate the loop hint token.
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001999 Token LoopHintTok;
2000 LoopHintTok.startToken();
2001 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Stephen Hines176edba2014-12-01 14:53:08 -08002002 LoopHintTok.setLocation(PragmaName.getLocation());
Stephen Hinesc568f1e2014-07-21 00:47:37 -07002003 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2004 TokenList.push_back(LoopHintTok);
2005 }
2006
2007 if (Tok.isNot(tok::eod)) {
2008 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2009 << "clang loop";
2010 return;
2011 }
2012
2013 Token *TokenArray = new Token[TokenList.size()];
2014 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
2015
2016 PP.EnterTokenStream(TokenArray, TokenList.size(),
2017 /*DisableMacroExpansion=*/false,
2018 /*OwnsTokens=*/true);
2019}
Stephen Hines176edba2014-12-01 14:53:08 -08002020
2021/// \brief Handle the loop unroll optimization pragmas.
2022/// #pragma unroll
2023/// #pragma unroll unroll-hint-value
2024/// #pragma unroll '(' unroll-hint-value ')'
2025/// #pragma nounroll
2026///
2027/// unroll-hint-value:
2028/// constant-expression
2029///
2030/// Loop unrolling hints can be specified with '#pragma unroll' or
2031/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2032/// contained in parentheses. With no argument the directive instructs llvm to
2033/// try to unroll the loop completely. A positive integer argument can be
2034/// specified to indicate the number of times the loop should be unrolled. To
2035/// maximize compatibility with other compilers the unroll count argument can be
2036/// specified with or without parentheses. Specifying, '#pragma nounroll'
2037/// disables unrolling of the loop.
2038void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2039 PragmaIntroducerKind Introducer,
2040 Token &Tok) {
2041 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2042 // "#pragma nounroll".
2043 Token PragmaName = Tok;
2044 PP.Lex(Tok);
2045 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2046 if (Tok.is(tok::eod)) {
2047 // nounroll or unroll pragma without an argument.
2048 Info->PragmaName = PragmaName;
2049 Info->Option.startToken();
2050 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2051 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2052 << "nounroll";
2053 return;
2054 } else {
2055 // Unroll pragma with an argument: "#pragma unroll N" or
2056 // "#pragma unroll(N)".
2057 // Read '(' if it exists.
2058 bool ValueInParens = Tok.is(tok::l_paren);
2059 if (ValueInParens)
2060 PP.Lex(Tok);
2061
2062 Token Option;
2063 Option.startToken();
2064 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
2065 return;
2066
2067 // In CUDA, the argument to '#pragma unroll' should not be contained in
2068 // parentheses.
2069 if (PP.getLangOpts().CUDA && ValueInParens)
2070 PP.Diag(Info->Toks[0].getLocation(),
2071 diag::warn_pragma_unroll_cuda_value_in_parens);
2072
2073 if (Tok.isNot(tok::eod)) {
2074 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2075 << "unroll";
2076 return;
2077 }
2078 }
2079
2080 // Generate the hint token.
2081 Token *TokenArray = new Token[1];
2082 TokenArray[0].startToken();
2083 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2084 TokenArray[0].setLocation(PragmaName.getLocation());
2085 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2086 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2087 /*OwnsTokens=*/true);
2088}