blob: 1bdf5b001df0b77b5677ae1f13b54df39701ba7f [file] [log] [blame]
Daniel Dunbar921b9682008-10-04 19:21:03 +00001//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the language specific #pragma handlers.
11//
12//===----------------------------------------------------------------------===//
13
Warren Huntc3b18962014-04-08 22:30:47 +000014#include "RAIIObjectsForParser.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000015#include "clang/Lex/Preprocessor.h"
16#include "clang/Parse/ParseDiagnostic.h"
17#include "clang/Parse/Parser.h"
18#include "clang/Sema/LoopHint.h"
19#include "clang/Sema/Scope.h"
20#include "llvm/ADT/StringSwitch.h"
21using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000022
Reid Kleckner5b086462014-02-20 22:52:09 +000023namespace {
24
25struct PragmaAlignHandler : public PragmaHandler {
26 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Craig Topper2b07f022014-03-12 05:09:18 +000027 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
28 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000029};
30
31struct PragmaGCCVisibilityHandler : public PragmaHandler {
32 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000033 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
34 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000035};
36
37struct PragmaOptionsHandler : public PragmaHandler {
38 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000039 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
40 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000041};
42
43struct PragmaPackHandler : public PragmaHandler {
44 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000045 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
46 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000047};
48
49struct PragmaMSStructHandler : public PragmaHandler {
50 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000051 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
52 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000053};
54
55struct PragmaUnusedHandler : public PragmaHandler {
56 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000057 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
58 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000059};
60
61struct PragmaWeakHandler : public PragmaHandler {
62 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000063 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
64 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000065};
66
67struct PragmaRedefineExtnameHandler : public PragmaHandler {
68 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000069 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
70 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000071};
72
73struct PragmaOpenCLExtensionHandler : public PragmaHandler {
74 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000075 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
76 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000077};
78
79
80struct PragmaFPContractHandler : public PragmaHandler {
81 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000082 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
83 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000084};
85
86struct PragmaNoOpenMPHandler : public PragmaHandler {
87 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000088 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
89 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000090};
91
92struct PragmaOpenMPHandler : public PragmaHandler {
93 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000094 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
95 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000096};
97
98/// PragmaCommentHandler - "\#pragma comment ...".
99struct PragmaCommentHandler : public PragmaHandler {
100 PragmaCommentHandler(Sema &Actions)
101 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000102 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
103 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000104private:
105 Sema &Actions;
106};
107
108struct PragmaDetectMismatchHandler : public PragmaHandler {
109 PragmaDetectMismatchHandler(Sema &Actions)
110 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000111 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
112 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000113private:
114 Sema &Actions;
115};
116
117struct PragmaMSPointersToMembers : public PragmaHandler {
118 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000119 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
120 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000121};
122
123struct PragmaMSVtorDisp : public PragmaHandler {
124 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000125 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
126 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000127};
128
Warren Huntc3b18962014-04-08 22:30:47 +0000129struct PragmaMSPragma : public PragmaHandler {
130 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000131 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
132 Token &FirstToken) override;
133};
134
Dario Domizioli13a0a382014-05-23 12:13:25 +0000135/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
136struct PragmaOptimizeHandler : public PragmaHandler {
137 PragmaOptimizeHandler(Sema &S)
138 : PragmaHandler("optimize"), Actions(S) {}
139 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
140 Token &FirstToken) override;
141private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000142 Sema &Actions;
143};
144
145struct PragmaLoopHintHandler : public PragmaHandler {
146 PragmaLoopHintHandler() : PragmaHandler("loop") {}
147 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
148 Token &FirstToken) override;
149};
150
151} // end namespace
152
153void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000154 AlignHandler.reset(new PragmaAlignHandler());
155 PP.AddPragmaHandler(AlignHandler.get());
156
157 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
158 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
159
160 OptionsHandler.reset(new PragmaOptionsHandler());
161 PP.AddPragmaHandler(OptionsHandler.get());
162
163 PackHandler.reset(new PragmaPackHandler());
164 PP.AddPragmaHandler(PackHandler.get());
165
166 MSStructHandler.reset(new PragmaMSStructHandler());
167 PP.AddPragmaHandler(MSStructHandler.get());
168
169 UnusedHandler.reset(new PragmaUnusedHandler());
170 PP.AddPragmaHandler(UnusedHandler.get());
171
172 WeakHandler.reset(new PragmaWeakHandler());
173 PP.AddPragmaHandler(WeakHandler.get());
174
175 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
176 PP.AddPragmaHandler(RedefineExtnameHandler.get());
177
178 FPContractHandler.reset(new PragmaFPContractHandler());
179 PP.AddPragmaHandler("STDC", FPContractHandler.get());
180
181 if (getLangOpts().OpenCL) {
182 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
183 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
184
185 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
186 }
187 if (getLangOpts().OpenMP)
188 OpenMPHandler.reset(new PragmaOpenMPHandler());
189 else
190 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
191 PP.AddPragmaHandler(OpenMPHandler.get());
192
193 if (getLangOpts().MicrosoftExt) {
194 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
195 PP.AddPragmaHandler(MSCommentHandler.get());
196 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
197 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
198 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
199 PP.AddPragmaHandler(MSPointersToMembers.get());
200 MSVtorDisp.reset(new PragmaMSVtorDisp());
201 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000202 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000203 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000204 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
205 PP.AddPragmaHandler(MSDataSeg.get());
206 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
207 PP.AddPragmaHandler(MSBSSSeg.get());
208 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
209 PP.AddPragmaHandler(MSConstSeg.get());
210 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
211 PP.AddPragmaHandler(MSCodeSeg.get());
212 MSSection.reset(new PragmaMSPragma("section"));
213 PP.AddPragmaHandler(MSSection.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000214 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000215
216 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
217 PP.AddPragmaHandler("clang", OptimizeHandler.get());
218
219 LoopHintHandler.reset(new PragmaLoopHintHandler());
220 PP.AddPragmaHandler("clang", LoopHintHandler.get());
221}
222
223void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000224 // Remove the pragma handlers we installed.
225 PP.RemovePragmaHandler(AlignHandler.get());
226 AlignHandler.reset();
227 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
228 GCCVisibilityHandler.reset();
229 PP.RemovePragmaHandler(OptionsHandler.get());
230 OptionsHandler.reset();
231 PP.RemovePragmaHandler(PackHandler.get());
232 PackHandler.reset();
233 PP.RemovePragmaHandler(MSStructHandler.get());
234 MSStructHandler.reset();
235 PP.RemovePragmaHandler(UnusedHandler.get());
236 UnusedHandler.reset();
237 PP.RemovePragmaHandler(WeakHandler.get());
238 WeakHandler.reset();
239 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
240 RedefineExtnameHandler.reset();
241
242 if (getLangOpts().OpenCL) {
243 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
244 OpenCLExtensionHandler.reset();
245 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
246 }
247 PP.RemovePragmaHandler(OpenMPHandler.get());
248 OpenMPHandler.reset();
249
250 if (getLangOpts().MicrosoftExt) {
251 PP.RemovePragmaHandler(MSCommentHandler.get());
252 MSCommentHandler.reset();
253 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
254 MSDetectMismatchHandler.reset();
255 PP.RemovePragmaHandler(MSPointersToMembers.get());
256 MSPointersToMembers.reset();
257 PP.RemovePragmaHandler(MSVtorDisp.get());
258 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000259 PP.RemovePragmaHandler(MSInitSeg.get());
260 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000261 PP.RemovePragmaHandler(MSDataSeg.get());
262 MSDataSeg.reset();
263 PP.RemovePragmaHandler(MSBSSSeg.get());
264 MSBSSSeg.reset();
265 PP.RemovePragmaHandler(MSConstSeg.get());
266 MSConstSeg.reset();
267 PP.RemovePragmaHandler(MSCodeSeg.get());
268 MSCodeSeg.reset();
269 PP.RemovePragmaHandler(MSSection.get());
270 MSSection.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000271 }
272
273 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
274 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000275
276 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
277 OptimizeHandler.reset();
278
279 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
280 LoopHintHandler.reset();
281}
282
283/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000284///
285/// Each annot_pragma_unused is followed by the argument token so e.g.
286/// "#pragma unused(x,y)" becomes:
287/// annot_pragma_unused 'x' annot_pragma_unused 'y'
288void Parser::HandlePragmaUnused() {
289 assert(Tok.is(tok::annot_pragma_unused));
290 SourceLocation UnusedLoc = ConsumeToken();
291 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
292 ConsumeToken(); // The argument token.
293}
Eli Friedman570024a2010-08-05 06:57:20 +0000294
Rafael Espindola273fd772012-01-26 02:02:57 +0000295void Parser::HandlePragmaVisibility() {
296 assert(Tok.is(tok::annot_pragma_vis));
297 const IdentifierInfo *VisType =
298 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
299 SourceLocation VisLoc = ConsumeToken();
300 Actions.ActOnPragmaVisibility(VisType, VisLoc);
301}
302
Eli Friedmanec52f922012-02-23 23:47:16 +0000303struct PragmaPackInfo {
304 Sema::PragmaPackKind Kind;
305 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000306 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000307 SourceLocation LParenLoc;
308 SourceLocation RParenLoc;
309};
310
311void Parser::HandlePragmaPack() {
312 assert(Tok.is(tok::annot_pragma_pack));
313 PragmaPackInfo *Info =
314 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
315 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000316 ExprResult Alignment;
317 if (Info->Alignment.is(tok::numeric_constant)) {
318 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
319 if (Alignment.isInvalid())
320 return;
321 }
322 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000323 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000324}
325
Eli Friedman68be1642012-10-04 02:36:51 +0000326void Parser::HandlePragmaMSStruct() {
327 assert(Tok.is(tok::annot_pragma_msstruct));
328 Sema::PragmaMSStructKind Kind =
329 static_cast<Sema::PragmaMSStructKind>(
330 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
331 Actions.ActOnPragmaMSStruct(Kind);
332 ConsumeToken(); // The annotation token.
333}
334
335void Parser::HandlePragmaAlign() {
336 assert(Tok.is(tok::annot_pragma_align));
337 Sema::PragmaOptionsAlignKind Kind =
338 static_cast<Sema::PragmaOptionsAlignKind>(
339 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
340 SourceLocation PragmaLoc = ConsumeToken();
341 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
342}
343
344void Parser::HandlePragmaWeak() {
345 assert(Tok.is(tok::annot_pragma_weak));
346 SourceLocation PragmaLoc = ConsumeToken();
347 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
348 Tok.getLocation());
349 ConsumeToken(); // The weak name.
350}
351
352void Parser::HandlePragmaWeakAlias() {
353 assert(Tok.is(tok::annot_pragma_weakalias));
354 SourceLocation PragmaLoc = ConsumeToken();
355 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
356 SourceLocation WeakNameLoc = Tok.getLocation();
357 ConsumeToken();
358 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
359 SourceLocation AliasNameLoc = Tok.getLocation();
360 ConsumeToken();
361 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
362 WeakNameLoc, AliasNameLoc);
363
364}
365
366void Parser::HandlePragmaRedefineExtname() {
367 assert(Tok.is(tok::annot_pragma_redefine_extname));
368 SourceLocation RedefLoc = ConsumeToken();
369 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
370 SourceLocation RedefNameLoc = Tok.getLocation();
371 ConsumeToken();
372 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
373 SourceLocation AliasNameLoc = Tok.getLocation();
374 ConsumeToken();
375 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
376 RedefNameLoc, AliasNameLoc);
377}
378
379void Parser::HandlePragmaFPContract() {
380 assert(Tok.is(tok::annot_pragma_fp_contract));
381 tok::OnOffSwitch OOS =
382 static_cast<tok::OnOffSwitch>(
383 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
384 Actions.ActOnPragmaFPContract(OOS);
385 ConsumeToken(); // The annotation token.
386}
387
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000388StmtResult Parser::HandlePragmaCaptured()
389{
390 assert(Tok.is(tok::annot_pragma_captured));
391 ConsumeToken();
392
393 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000394 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000395 return StmtError();
396 }
397
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000398 SourceLocation Loc = Tok.getLocation();
399
400 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000401 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
402 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000403
404 StmtResult R = ParseCompoundStatement();
405 CapturedRegionScope.Exit();
406
407 if (R.isInvalid()) {
408 Actions.ActOnCapturedRegionError();
409 return StmtError();
410 }
411
412 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000413}
414
Eli Friedman68be1642012-10-04 02:36:51 +0000415namespace {
416 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
417}
418
419void Parser::HandlePragmaOpenCLExtension() {
420 assert(Tok.is(tok::annot_pragma_opencl_extension));
421 OpenCLExtData data =
422 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
423 unsigned state = data.getInt();
424 IdentifierInfo *ename = data.getPointer();
425 SourceLocation NameLoc = Tok.getLocation();
426 ConsumeToken(); // The annotation token.
427
428 OpenCLOptions &f = Actions.getOpenCLOptions();
429 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
430 // overriding all previously issued extension directives, but only if the
431 // behavior is set to disable."
432 if (state == 0 && ename->isStr("all")) {
433#define OPENCLEXT(nm) f.nm = 0;
434#include "clang/Basic/OpenCLExtensions.def"
435 }
436#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
437#include "clang/Basic/OpenCLExtensions.def"
438 else {
439 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
440 return;
441 }
442}
443
David Majnemer4bb09802014-02-10 19:50:15 +0000444void Parser::HandlePragmaMSPointersToMembers() {
445 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000446 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
447 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000448 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
449 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
450 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
451}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000452
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000453void Parser::HandlePragmaMSVtorDisp() {
454 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
455 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
456 Sema::PragmaVtorDispKind Kind =
457 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
458 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
459 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
460 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
461}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000462
Warren Huntc3b18962014-04-08 22:30:47 +0000463void Parser::HandlePragmaMSPragma() {
464 assert(Tok.is(tok::annot_pragma_ms_pragma));
465 // Grab the tokens out of the annotation and enter them into the stream.
466 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
467 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
468 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
469 assert(Tok.isAnyIdentifier());
470 llvm::StringRef PragmaName = Tok.getIdentifierInfo()->getName();
471 PP.Lex(Tok); // pragma kind
472 // Figure out which #pragma we're dealing with. The switch has no default
473 // because lex shouldn't emit the annotation token for unrecognized pragmas.
474 typedef unsigned (Parser::*PragmaHandler)(llvm::StringRef, SourceLocation);
475 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
476 .Case("data_seg", &Parser::HandlePragmaMSSegment)
477 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
478 .Case("const_seg", &Parser::HandlePragmaMSSegment)
479 .Case("code_seg", &Parser::HandlePragmaMSSegment)
480 .Case("section", &Parser::HandlePragmaMSSection)
481 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
482 if (auto DiagID = (this->*Handler)(PragmaName, PragmaLocation)) {
483 PP.Diag(PragmaLocation, DiagID) << PragmaName;
484 while (Tok.isNot(tok::eof))
485 PP.Lex(Tok);
486 PP.Lex(Tok);
487 }
488}
489
490unsigned Parser::HandlePragmaMSSection(llvm::StringRef PragmaName,
491 SourceLocation PragmaLocation) {
492 if (Tok.isNot(tok::l_paren))
493 return diag::warn_pragma_expected_lparen;
494 PP.Lex(Tok); // (
495 // Parsing code for pragma section
496 if (Tok.isNot(tok::string_literal))
497 return diag::warn_pragma_expected_section_name;
498 StringLiteral *SegmentName =
499 cast<StringLiteral>(ParseStringLiteralExpression().get());
500 int SectionFlags = 0;
501 while (Tok.is(tok::comma)) {
502 PP.Lex(Tok); // ,
503 if (!Tok.isAnyIdentifier())
504 return diag::warn_pragma_expected_action_or_r_paren;
505 Sema::PragmaSectionFlag Flag =
506 llvm::StringSwitch<Sema::PragmaSectionFlag>(
507 Tok.getIdentifierInfo()->getName())
508 .Case("read", Sema::PSF_Read)
509 .Case("write", Sema::PSF_Write)
510 .Case("execute", Sema::PSF_Execute)
511 .Case("shared", Sema::PSF_Invalid)
512 .Case("nopage", Sema::PSF_Invalid)
513 .Case("nocache", Sema::PSF_Invalid)
514 .Case("discard", Sema::PSF_Invalid)
515 .Case("remove", Sema::PSF_Invalid)
516 .Default(Sema::PSF_None);
517 if (Flag == Sema::PSF_None || Flag == Sema::PSF_Invalid) {
518 PP.Diag(PragmaLocation, Flag == Sema::PSF_None ?
519 diag::warn_pragma_invalid_specific_action :
520 diag::warn_pragma_unsupported_action)
521 << PragmaName << Tok.getIdentifierInfo()->getName();
522 while (Tok.isNot(tok::eof))
523 PP.Lex(Tok);
524 PP.Lex(Tok);
525 return 0;
526 }
527 SectionFlags |= Flag;
528 PP.Lex(Tok); // Identifier
529 }
530 if (Tok.isNot(tok::r_paren))
531 return diag::warn_pragma_expected_rparen;
532 PP.Lex(Tok); // )
533 if (Tok.isNot(tok::eof))
534 return diag::warn_pragma_extra_tokens_at_eol;
535 PP.Lex(Tok); // eof
536 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
537 return 0;
538}
539
540unsigned Parser::HandlePragmaMSSegment(llvm::StringRef PragmaName,
541 SourceLocation PragmaLocation) {
542 if (Tok.isNot(tok::l_paren))
543 return diag::warn_pragma_expected_lparen;
544 PP.Lex(Tok); // (
545 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
546 llvm::StringRef SlotLabel;
547 if (Tok.isAnyIdentifier()) {
548 llvm::StringRef PushPop = Tok.getIdentifierInfo()->getName();
549 if (PushPop == "push")
550 Action = Sema::PSK_Push;
551 else if (PushPop == "pop")
552 Action = Sema::PSK_Pop;
553 else
554 return diag::warn_pragma_expected_section_push_pop_or_name;
555 if (Action != Sema::PSK_Reset) {
556 PP.Lex(Tok); // push | pop
557 if (Tok.is(tok::comma)) {
558 PP.Lex(Tok); // ,
559 // If we've got a comma, we either need a label or a string.
560 if (Tok.isAnyIdentifier()) {
561 SlotLabel = Tok.getIdentifierInfo()->getName();
562 PP.Lex(Tok); // identifier
563 if (Tok.is(tok::comma))
564 PP.Lex(Tok);
565 else if (Tok.isNot(tok::r_paren))
566 return diag::warn_pragma_expected_punc;
567 }
568 } else if (Tok.isNot(tok::r_paren))
569 return diag::warn_pragma_expected_punc;
570 }
571 }
572 // Grab the string literal for our section name.
573 StringLiteral *SegmentName = nullptr;
574 if (Tok.isNot(tok::r_paren)) {
575 if (Tok.isNot(tok::string_literal))
576 return Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
577 diag::warn_pragma_expected_section_name :
578 diag::warn_pragma_expected_section_label_or_name :
579 diag::warn_pragma_expected_section_push_pop_or_name;
580 SegmentName = cast<StringLiteral>(ParseStringLiteralExpression().get());
581 // Setting section "" has no effect
582 if (SegmentName->getLength())
583 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
584 }
585 if (Tok.isNot(tok::r_paren))
586 return diag::warn_pragma_expected_rparen;
587 PP.Lex(Tok); // )
588 if (Tok.isNot(tok::eof))
589 return diag::warn_pragma_extra_tokens_at_eol;
590 PP.Lex(Tok); // eof
591 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
592 SegmentName, PragmaName);
593 return 0;
594}
595
596unsigned Parser::HandlePragmaMSInitSeg(llvm::StringRef PragmaName,
597 SourceLocation PragmaLocation) {
598 return PP.getDiagnostics().getCustomDiagID(
Eli Bendersky06a40422014-06-06 20:31:48 +0000599 DiagnosticsEngine::Error, "'#pragma %0' not implemented.");
600}
601
602struct PragmaLoopHintInfo {
603 Token Loop;
604 Token Value;
605 Token Option;
606};
607
608LoopHint Parser::HandlePragmaLoopHint() {
609 assert(Tok.is(tok::annot_pragma_loop_hint));
610 PragmaLoopHintInfo *Info =
611 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
612
613 LoopHint Hint;
614 Hint.LoopLoc =
615 IdentifierLoc::create(Actions.Context, Info->Loop.getLocation(),
616 Info->Loop.getIdentifierInfo());
617 Hint.OptionLoc =
618 IdentifierLoc::create(Actions.Context, Info->Option.getLocation(),
619 Info->Option.getIdentifierInfo());
620 Hint.ValueLoc =
621 IdentifierLoc::create(Actions.Context, Info->Value.getLocation(),
622 Info->Value.getIdentifierInfo());
623 Hint.Range =
624 SourceRange(Info->Option.getLocation(), Info->Value.getLocation());
625
Tyler Nowickif54bda62014-06-13 17:15:27 +0000626 // FIXME: We should allow non-type template parameters for the loop hint
627 // value. See bug report #19610
Eli Bendersky06a40422014-06-06 20:31:48 +0000628 if (Info->Value.is(tok::numeric_constant))
629 Hint.ValueExpr = Actions.ActOnNumericConstant(Info->Value).get();
630 else
631 Hint.ValueExpr = nullptr;
632
633 return Hint;
634}
635
636// #pragma GCC visibility comes in two variants:
637// 'push' '(' [visibility] ')'
638// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000639void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
640 PragmaIntroducerKind Introducer,
641 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000642 SourceLocation VisLoc = VisTok.getLocation();
643
644 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000645 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000646
647 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
648
Eli Friedman570024a2010-08-05 06:57:20 +0000649 const IdentifierInfo *VisType;
650 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000651 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000652 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000653 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000654 if (Tok.isNot(tok::l_paren)) {
655 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
656 << "visibility";
657 return;
658 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000659 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000660 VisType = Tok.getIdentifierInfo();
661 if (!VisType) {
662 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
663 << "visibility";
664 return;
665 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000666 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000667 if (Tok.isNot(tok::r_paren)) {
668 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
669 << "visibility";
670 return;
671 }
672 } else {
673 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
674 << "visibility";
675 return;
676 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000677 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000678 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000679 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
680 << "visibility";
681 return;
682 }
683
Rafael Espindola273fd772012-01-26 02:02:57 +0000684 Token *Toks = new Token[1];
685 Toks[0].startToken();
686 Toks[0].setKind(tok::annot_pragma_vis);
687 Toks[0].setLocation(VisLoc);
688 Toks[0].setAnnotationValue(
689 const_cast<void*>(static_cast<const void*>(VisType)));
690 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
691 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000692}
693
Daniel Dunbar921b9682008-10-04 19:21:03 +0000694// #pragma pack(...) comes in the following delicious flavors:
695// pack '(' [integer] ')'
696// pack '(' 'show' ')'
697// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000698void PragmaPackHandler::HandlePragma(Preprocessor &PP,
699 PragmaIntroducerKind Introducer,
700 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000701 SourceLocation PackLoc = PackTok.getLocation();
702
703 Token Tok;
704 PP.Lex(Tok);
705 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000706 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000707 return;
708 }
709
John McCallfaf5fb42010-08-26 23:41:50 +0000710 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000711 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000712 Token Alignment;
713 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000714 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000715 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000716 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000717 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000718
719 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000720
721 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
722 // the push/pop stack.
723 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000724 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000725 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000726 } else if (Tok.is(tok::identifier)) {
727 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000728 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000729 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000730 PP.Lex(Tok);
731 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000732 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000733 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000734 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000735 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000736 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000737 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000738 return;
Mike Stump11289f42009-09-09 15:08:12 +0000739 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000740 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000741
Daniel Dunbar921b9682008-10-04 19:21:03 +0000742 if (Tok.is(tok::comma)) {
743 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000744
Daniel Dunbar921b9682008-10-04 19:21:03 +0000745 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000746 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000747
748 PP.Lex(Tok);
749 } else if (Tok.is(tok::identifier)) {
750 Name = Tok.getIdentifierInfo();
751 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000752
Daniel Dunbar921b9682008-10-04 19:21:03 +0000753 if (Tok.is(tok::comma)) {
754 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000755
Daniel Dunbar921b9682008-10-04 19:21:03 +0000756 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000757 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000758 return;
759 }
Mike Stump11289f42009-09-09 15:08:12 +0000760
Eli Friedman68be1642012-10-04 02:36:51 +0000761 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000762
763 PP.Lex(Tok);
764 }
765 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000766 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000767 return;
768 }
769 }
770 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000771 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +0000772 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
773 // the push/pop stack.
774 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
775 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000776 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000777
778 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000779 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000780 return;
781 }
782
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000783 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000784 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000785 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000786 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
787 return;
788 }
789
Daniel Dunbar340cf242012-02-29 01:38:22 +0000790 PragmaPackInfo *Info =
791 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
792 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
793 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +0000794 Info->Kind = Kind;
795 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000796 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000797 Info->LParenLoc = LParenLoc;
798 Info->RParenLoc = RParenLoc;
799
Daniel Dunbar340cf242012-02-29 01:38:22 +0000800 Token *Toks =
801 (Token*) PP.getPreprocessorAllocator().Allocate(
802 sizeof(Token) * 1, llvm::alignOf<Token>());
803 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +0000804 Toks[0].startToken();
805 Toks[0].setKind(tok::annot_pragma_pack);
806 Toks[0].setLocation(PackLoc);
807 Toks[0].setAnnotationValue(static_cast<void*>(Info));
808 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +0000809 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000810}
811
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000812// #pragma ms_struct on
813// #pragma ms_struct off
814void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
815 PragmaIntroducerKind Introducer,
816 Token &MSStructTok) {
817 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
818
819 Token Tok;
820 PP.Lex(Tok);
821 if (Tok.isNot(tok::identifier)) {
822 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
823 return;
824 }
825 const IdentifierInfo *II = Tok.getIdentifierInfo();
826 if (II->isStr("on")) {
827 Kind = Sema::PMSST_ON;
828 PP.Lex(Tok);
829 }
830 else if (II->isStr("off") || II->isStr("reset"))
831 PP.Lex(Tok);
832 else {
833 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
834 return;
835 }
836
837 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +0000838 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
839 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000840 return;
841 }
Eli Friedman68be1642012-10-04 02:36:51 +0000842
843 Token *Toks =
844 (Token*) PP.getPreprocessorAllocator().Allocate(
845 sizeof(Token) * 1, llvm::alignOf<Token>());
846 new (Toks) Token();
847 Toks[0].startToken();
848 Toks[0].setKind(tok::annot_pragma_msstruct);
849 Toks[0].setLocation(MSStructTok.getLocation());
850 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
851 static_cast<uintptr_t>(Kind)));
852 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
853 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000854}
855
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000856// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
857// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +0000858static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000859 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000860 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000861
862 if (IsOptions) {
863 PP.Lex(Tok);
864 if (Tok.isNot(tok::identifier) ||
865 !Tok.getIdentifierInfo()->isStr("align")) {
866 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
867 return;
868 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000869 }
Daniel Dunbar663e8092010-05-27 18:42:09 +0000870
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000871 PP.Lex(Tok);
872 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000873 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
874 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000875 return;
876 }
877
878 PP.Lex(Tok);
879 if (Tok.isNot(tok::identifier)) {
880 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000881 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000882 return;
883 }
884
John McCallfaf5fb42010-08-26 23:41:50 +0000885 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000886 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +0000887 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +0000888 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +0000889 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +0000890 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +0000891 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +0000892 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000893 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +0000894 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000895 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +0000896 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000897 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +0000898 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000899 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000900 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
901 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000902 return;
903 }
904
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000905 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000906 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000907 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000908 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000909 return;
910 }
911
Eli Friedman68be1642012-10-04 02:36:51 +0000912 Token *Toks =
913 (Token*) PP.getPreprocessorAllocator().Allocate(
914 sizeof(Token) * 1, llvm::alignOf<Token>());
915 new (Toks) Token();
916 Toks[0].startToken();
917 Toks[0].setKind(tok::annot_pragma_align);
918 Toks[0].setLocation(FirstTok.getLocation());
919 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
920 static_cast<uintptr_t>(Kind)));
921 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
922 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000923}
924
Douglas Gregorc7d65762010-09-09 22:45:38 +0000925void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
926 PragmaIntroducerKind Introducer,
927 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +0000928 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000929}
930
Douglas Gregorc7d65762010-09-09 22:45:38 +0000931void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
932 PragmaIntroducerKind Introducer,
933 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +0000934 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000935}
936
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000937// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +0000938void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
939 PragmaIntroducerKind Introducer,
940 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000941 // FIXME: Should we be expanding macros here? My guess is no.
942 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000943
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000944 // Lex the left '('.
945 Token Tok;
946 PP.Lex(Tok);
947 if (Tok.isNot(tok::l_paren)) {
948 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
949 return;
950 }
Mike Stump11289f42009-09-09 15:08:12 +0000951
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000952 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000953 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000954 SourceLocation RParenLoc;
955 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +0000956
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000957 while (true) {
958 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000959
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000960 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +0000961 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +0000962 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000963 LexID = false;
964 continue;
965 }
966
Ted Kremenekfb50bf52009-08-03 23:24:57 +0000967 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000968 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
969 return;
970 }
Mike Stump11289f42009-09-09 15:08:12 +0000971
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000972 // We are execting a ')' or a ','.
973 if (Tok.is(tok::comma)) {
974 LexID = true;
975 continue;
976 }
Mike Stump11289f42009-09-09 15:08:12 +0000977
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000978 if (Tok.is(tok::r_paren)) {
979 RParenLoc = Tok.getLocation();
980 break;
981 }
Mike Stump11289f42009-09-09 15:08:12 +0000982
Ted Kremenekfb50bf52009-08-03 23:24:57 +0000983 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +0000984 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000985 return;
986 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000987
988 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000989 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000990 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
991 "unused";
992 return;
993 }
994
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000995 // Verify that we have a location for the right parenthesis.
996 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +0000997 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000998
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000999 // For each identifier token, insert into the token stream a
1000 // annot_pragma_unused token followed by the identifier token.
1001 // This allows us to cache a "#pragma unused" that occurs inside an inline
1002 // C++ member function.
1003
Daniel Dunbar340cf242012-02-29 01:38:22 +00001004 Token *Toks =
1005 (Token*) PP.getPreprocessorAllocator().Allocate(
1006 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001007 for (unsigned i=0; i != Identifiers.size(); i++) {
1008 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1009 pragmaUnusedTok.startToken();
1010 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1011 pragmaUnusedTok.setLocation(UnusedLoc);
1012 idTok = Identifiers[i];
1013 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001014 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1015 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001016}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001017
1018// #pragma weak identifier
1019// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001020void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1021 PragmaIntroducerKind Introducer,
1022 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001023 SourceLocation WeakLoc = WeakTok.getLocation();
1024
1025 Token Tok;
1026 PP.Lex(Tok);
1027 if (Tok.isNot(tok::identifier)) {
1028 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1029 return;
1030 }
1031
Eli Friedman68be1642012-10-04 02:36:51 +00001032 Token WeakName = Tok;
1033 bool HasAlias = false;
1034 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001035
1036 PP.Lex(Tok);
1037 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001038 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001039 PP.Lex(Tok);
1040 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001041 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001042 << "weak";
1043 return;
1044 }
Eli Friedman68be1642012-10-04 02:36:51 +00001045 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001046 PP.Lex(Tok);
1047 }
1048
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001049 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001050 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1051 return;
1052 }
1053
Eli Friedman68be1642012-10-04 02:36:51 +00001054 if (HasAlias) {
1055 Token *Toks =
1056 (Token*) PP.getPreprocessorAllocator().Allocate(
1057 sizeof(Token) * 3, llvm::alignOf<Token>());
1058 Token &pragmaUnusedTok = Toks[0];
1059 pragmaUnusedTok.startToken();
1060 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1061 pragmaUnusedTok.setLocation(WeakLoc);
1062 Toks[1] = WeakName;
1063 Toks[2] = AliasName;
1064 PP.EnterTokenStream(Toks, 3,
1065 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001066 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001067 Token *Toks =
1068 (Token*) PP.getPreprocessorAllocator().Allocate(
1069 sizeof(Token) * 2, llvm::alignOf<Token>());
1070 Token &pragmaUnusedTok = Toks[0];
1071 pragmaUnusedTok.startToken();
1072 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1073 pragmaUnusedTok.setLocation(WeakLoc);
1074 Toks[1] = WeakName;
1075 PP.EnterTokenStream(Toks, 2,
1076 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001077 }
1078}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001079
David Chisnall0867d9c2012-02-18 16:12:34 +00001080// #pragma redefine_extname identifier identifier
1081void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1082 PragmaIntroducerKind Introducer,
1083 Token &RedefToken) {
1084 SourceLocation RedefLoc = RedefToken.getLocation();
1085
1086 Token Tok;
1087 PP.Lex(Tok);
1088 if (Tok.isNot(tok::identifier)) {
1089 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1090 "redefine_extname";
1091 return;
1092 }
1093
Eli Friedman68be1642012-10-04 02:36:51 +00001094 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001095 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001096
David Chisnall0867d9c2012-02-18 16:12:34 +00001097 if (Tok.isNot(tok::identifier)) {
1098 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1099 << "redefine_extname";
1100 return;
1101 }
Eli Friedman68be1642012-10-04 02:36:51 +00001102
1103 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001104 PP.Lex(Tok);
1105
1106 if (Tok.isNot(tok::eod)) {
1107 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1108 "redefine_extname";
1109 return;
1110 }
1111
Eli Friedman68be1642012-10-04 02:36:51 +00001112 Token *Toks =
1113 (Token*) PP.getPreprocessorAllocator().Allocate(
1114 sizeof(Token) * 3, llvm::alignOf<Token>());
1115 Token &pragmaRedefTok = Toks[0];
1116 pragmaRedefTok.startToken();
1117 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1118 pragmaRedefTok.setLocation(RedefLoc);
1119 Toks[1] = RedefName;
1120 Toks[2] = AliasName;
1121 PP.EnterTokenStream(Toks, 3,
1122 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001123}
1124
1125
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001126void
1127PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1128 PragmaIntroducerKind Introducer,
1129 Token &Tok) {
1130 tok::OnOffSwitch OOS;
1131 if (PP.LexOnOffSwitch(OOS))
1132 return;
1133
Eli Friedman68be1642012-10-04 02:36:51 +00001134 Token *Toks =
1135 (Token*) PP.getPreprocessorAllocator().Allocate(
1136 sizeof(Token) * 1, llvm::alignOf<Token>());
1137 new (Toks) Token();
1138 Toks[0].startToken();
1139 Toks[0].setKind(tok::annot_pragma_fp_contract);
1140 Toks[0].setLocation(Tok.getLocation());
1141 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1142 static_cast<uintptr_t>(OOS)));
1143 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1144 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001145}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001146
1147void
1148PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1149 PragmaIntroducerKind Introducer,
1150 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001151 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001152 if (Tok.isNot(tok::identifier)) {
1153 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1154 "OPENCL";
1155 return;
1156 }
1157 IdentifierInfo *ename = Tok.getIdentifierInfo();
1158 SourceLocation NameLoc = Tok.getLocation();
1159
1160 PP.Lex(Tok);
1161 if (Tok.isNot(tok::colon)) {
1162 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1163 return;
1164 }
1165
1166 PP.Lex(Tok);
1167 if (Tok.isNot(tok::identifier)) {
1168 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1169 return;
1170 }
1171 IdentifierInfo *op = Tok.getIdentifierInfo();
1172
1173 unsigned state;
1174 if (op->isStr("enable")) {
1175 state = 1;
1176 } else if (op->isStr("disable")) {
1177 state = 0;
1178 } else {
1179 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1180 return;
1181 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001182 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001183
Eli Friedman68be1642012-10-04 02:36:51 +00001184 PP.Lex(Tok);
1185 if (Tok.isNot(tok::eod)) {
1186 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1187 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001188 return;
1189 }
Eli Friedman68be1642012-10-04 02:36:51 +00001190
1191 OpenCLExtData data(ename, state);
1192 Token *Toks =
1193 (Token*) PP.getPreprocessorAllocator().Allocate(
1194 sizeof(Token) * 1, llvm::alignOf<Token>());
1195 new (Toks) Token();
1196 Toks[0].startToken();
1197 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1198 Toks[0].setLocation(NameLoc);
1199 Toks[0].setAnnotationValue(data.getOpaqueValue());
1200 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1201 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001202
1203 if (PP.getPPCallbacks())
1204 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1205 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001206}
1207
Alexey Bataeva769e072013-03-22 06:34:35 +00001208/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1209///
1210void
1211PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1212 PragmaIntroducerKind Introducer,
1213 Token &FirstTok) {
1214 if (PP.getDiagnostics().getDiagnosticLevel(diag::warn_pragma_omp_ignored,
1215 FirstTok.getLocation()) !=
1216 DiagnosticsEngine::Ignored) {
1217 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001218 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1219 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001220 }
1221 PP.DiscardUntilEndOfDirective();
1222}
1223
1224/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1225///
1226void
1227PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1228 PragmaIntroducerKind Introducer,
1229 Token &FirstTok) {
1230 SmallVector<Token, 16> Pragma;
1231 Token Tok;
1232 Tok.startToken();
1233 Tok.setKind(tok::annot_pragma_openmp);
1234 Tok.setLocation(FirstTok.getLocation());
1235
1236 while (Tok.isNot(tok::eod)) {
1237 Pragma.push_back(Tok);
1238 PP.Lex(Tok);
1239 }
1240 SourceLocation EodLoc = Tok.getLocation();
1241 Tok.startToken();
1242 Tok.setKind(tok::annot_pragma_openmp_end);
1243 Tok.setLocation(EodLoc);
1244 Pragma.push_back(Tok);
1245
1246 Token *Toks = new Token[Pragma.size()];
1247 std::copy(Pragma.begin(), Pragma.end(), Toks);
1248 PP.EnterTokenStream(Toks, Pragma.size(),
1249 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1250}
Reid Kleckner002562a2013-05-06 21:02:12 +00001251
David Majnemer4bb09802014-02-10 19:50:15 +00001252/// \brief Handle '#pragma pointers_to_members'
1253// The grammar for this pragma is as follows:
1254//
1255// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1256//
1257// #pragma pointers_to_members '(' 'best_case' ')'
1258// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1259// #pragma pointers_to_members '(' inheritance-model ')'
1260void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1261 PragmaIntroducerKind Introducer,
1262 Token &Tok) {
1263 SourceLocation PointersToMembersLoc = Tok.getLocation();
1264 PP.Lex(Tok);
1265 if (Tok.isNot(tok::l_paren)) {
1266 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1267 << "pointers_to_members";
1268 return;
1269 }
1270 PP.Lex(Tok);
1271 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1272 if (!Arg) {
1273 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1274 << "pointers_to_members";
1275 return;
1276 }
1277 PP.Lex(Tok);
1278
David Majnemer86c318f2014-02-11 21:05:00 +00001279 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001280 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001281 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001282 } else {
1283 if (Arg->isStr("full_generality")) {
1284 if (Tok.is(tok::comma)) {
1285 PP.Lex(Tok);
1286
1287 Arg = Tok.getIdentifierInfo();
1288 if (!Arg) {
1289 PP.Diag(Tok.getLocation(),
1290 diag::err_pragma_pointers_to_members_unknown_kind)
1291 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1292 return;
1293 }
1294 PP.Lex(Tok);
1295 } else if (Tok.is(tok::r_paren)) {
1296 // #pragma pointers_to_members(full_generality) implicitly specifies
1297 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001298 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001299 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001300 } else {
1301 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1302 << "full_generality";
1303 return;
1304 }
1305 }
1306
1307 if (Arg) {
1308 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001309 RepresentationMethod =
1310 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001311 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001312 RepresentationMethod =
1313 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001314 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001315 RepresentationMethod =
1316 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001317 } else {
1318 PP.Diag(Tok.getLocation(),
1319 diag::err_pragma_pointers_to_members_unknown_kind)
1320 << Arg << /*HasPointerDeclaration*/ 1;
1321 return;
1322 }
1323 }
1324 }
1325
1326 if (Tok.isNot(tok::r_paren)) {
1327 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1328 << (Arg ? Arg->getName() : "full_generality");
1329 return;
1330 }
1331
1332 PP.Lex(Tok);
1333 if (Tok.isNot(tok::eod)) {
1334 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1335 << "pointers_to_members";
1336 return;
1337 }
1338
1339 Token AnnotTok;
1340 AnnotTok.startToken();
1341 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1342 AnnotTok.setLocation(PointersToMembersLoc);
1343 AnnotTok.setAnnotationValue(
1344 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1345 PP.EnterToken(AnnotTok);
1346}
1347
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001348/// \brief Handle '#pragma vtordisp'
1349// The grammar for this pragma is as follows:
1350//
1351// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1352//
1353// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1354// #pragma vtordisp '(' 'pop' ')'
1355// #pragma vtordisp '(' ')'
1356void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1357 PragmaIntroducerKind Introducer,
1358 Token &Tok) {
1359 SourceLocation VtorDispLoc = Tok.getLocation();
1360 PP.Lex(Tok);
1361 if (Tok.isNot(tok::l_paren)) {
1362 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1363 return;
1364 }
1365 PP.Lex(Tok);
1366
1367 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1368 const IdentifierInfo *II = Tok.getIdentifierInfo();
1369 if (II) {
1370 if (II->isStr("push")) {
1371 // #pragma vtordisp(push, mode)
1372 PP.Lex(Tok);
1373 if (Tok.isNot(tok::comma)) {
1374 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1375 return;
1376 }
1377 PP.Lex(Tok);
1378 Kind = Sema::PVDK_Push;
1379 // not push, could be on/off
1380 } else if (II->isStr("pop")) {
1381 // #pragma vtordisp(pop)
1382 PP.Lex(Tok);
1383 Kind = Sema::PVDK_Pop;
1384 }
1385 // not push or pop, could be on/off
1386 } else {
1387 if (Tok.is(tok::r_paren)) {
1388 // #pragma vtordisp()
1389 Kind = Sema::PVDK_Reset;
1390 }
1391 }
1392
1393
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001394 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001395 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1396 const IdentifierInfo *II = Tok.getIdentifierInfo();
1397 if (II && II->isStr("off")) {
1398 PP.Lex(Tok);
1399 Value = 0;
1400 } else if (II && II->isStr("on")) {
1401 PP.Lex(Tok);
1402 Value = 1;
1403 } else if (Tok.is(tok::numeric_constant) &&
1404 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1405 if (Value > 2) {
1406 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1407 << 0 << 2 << "vtordisp";
1408 return;
1409 }
1410 } else {
1411 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1412 << "vtordisp";
1413 return;
1414 }
1415 }
1416
1417 // Finish the pragma: ')' $
1418 if (Tok.isNot(tok::r_paren)) {
1419 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1420 return;
1421 }
1422 PP.Lex(Tok);
1423 if (Tok.isNot(tok::eod)) {
1424 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1425 << "vtordisp";
1426 return;
1427 }
1428
1429 // Enter the annotation.
1430 Token AnnotTok;
1431 AnnotTok.startToken();
1432 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1433 AnnotTok.setLocation(VtorDispLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001434 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1435 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001436 PP.EnterToken(AnnotTok);
1437}
1438
Warren Huntc3b18962014-04-08 22:30:47 +00001439/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1440/// an annotation token.
1441void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1442 PragmaIntroducerKind Introducer,
1443 Token &Tok) {
1444 Token EoF, AnnotTok;
1445 EoF.startToken();
1446 EoF.setKind(tok::eof);
1447 AnnotTok.startToken();
1448 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1449 AnnotTok.setLocation(Tok.getLocation());
1450 SmallVector<Token, 8> TokenVector;
1451 // Suck up all of the tokens before the eod.
1452 for (; Tok.isNot(tok::eod); PP.Lex(Tok))
1453 TokenVector.push_back(Tok);
1454 // Add a sentinal EoF token to the end of the list.
1455 TokenVector.push_back(EoF);
1456 // We must allocate this array with new because EnterTokenStream is going to
1457 // delete it later.
1458 Token *TokenArray = new Token[TokenVector.size()];
1459 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1460 auto Value = new (PP.getPreprocessorAllocator())
1461 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1462 AnnotTok.setAnnotationValue(Value);
1463 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001464}
1465
Aaron Ballman5d041be2013-06-04 02:07:14 +00001466/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1467///
1468/// The syntax is:
1469/// \code
1470/// #pragma detect_mismatch("name", "value")
1471/// \endcode
1472/// Where 'name' and 'value' are quoted strings. The values are embedded in
1473/// the object file and passed along to the linker. If the linker detects a
1474/// mismatch in the object file's values for the given name, a LNK2038 error
1475/// is emitted. See MSDN for more details.
1476void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1477 PragmaIntroducerKind Introducer,
1478 Token &Tok) {
1479 SourceLocation CommentLoc = Tok.getLocation();
1480 PP.Lex(Tok);
1481 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001482 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001483 return;
1484 }
1485
1486 // Read the name to embed, which must be a string literal.
1487 std::string NameString;
1488 if (!PP.LexStringLiteral(Tok, NameString,
1489 "pragma detect_mismatch",
1490 /*MacroExpansion=*/true))
1491 return;
1492
1493 // Read the comma followed by a second string literal.
1494 std::string ValueString;
1495 if (Tok.isNot(tok::comma)) {
1496 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1497 return;
1498 }
1499
1500 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1501 /*MacroExpansion=*/true))
1502 return;
1503
1504 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001505 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001506 return;
1507 }
1508 PP.Lex(Tok); // Eat the r_paren.
1509
1510 if (Tok.isNot(tok::eod)) {
1511 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1512 return;
1513 }
1514
Reid Kleckner71966c92014-02-20 23:37:45 +00001515 // If the pragma is lexically sound, notify any interested PPCallbacks.
1516 if (PP.getPPCallbacks())
1517 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1518 ValueString);
1519
Aaron Ballman5d041be2013-06-04 02:07:14 +00001520 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1521}
1522
Reid Kleckner002562a2013-05-06 21:02:12 +00001523/// \brief Handle the microsoft \#pragma comment extension.
1524///
1525/// The syntax is:
1526/// \code
1527/// #pragma comment(linker, "foo")
1528/// \endcode
1529/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1530/// "foo" is a string, which is fully macro expanded, and permits string
1531/// concatenation, embedded escape characters etc. See MSDN for more details.
1532void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1533 PragmaIntroducerKind Introducer,
1534 Token &Tok) {
1535 SourceLocation CommentLoc = Tok.getLocation();
1536 PP.Lex(Tok);
1537 if (Tok.isNot(tok::l_paren)) {
1538 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1539 return;
1540 }
1541
1542 // Read the identifier.
1543 PP.Lex(Tok);
1544 if (Tok.isNot(tok::identifier)) {
1545 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1546 return;
1547 }
1548
1549 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001550 IdentifierInfo *II = Tok.getIdentifierInfo();
1551 Sema::PragmaMSCommentKind Kind =
1552 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1553 .Case("linker", Sema::PCK_Linker)
1554 .Case("lib", Sema::PCK_Lib)
1555 .Case("compiler", Sema::PCK_Compiler)
1556 .Case("exestr", Sema::PCK_ExeStr)
1557 .Case("user", Sema::PCK_User)
1558 .Default(Sema::PCK_Unknown);
1559 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001560 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1561 return;
1562 }
1563
1564 // Read the optional string if present.
1565 PP.Lex(Tok);
1566 std::string ArgumentString;
1567 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1568 "pragma comment",
1569 /*MacroExpansion=*/true))
1570 return;
1571
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001572 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001573 // FIXME: If the kind is "compiler" warn if the string is present (it is
1574 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001575 // The MSDN docs say that "lib" and "linker" require a string and have a short
1576 // whitelist of linker options they support, but in practice MSVC doesn't
1577 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001578
1579 if (Tok.isNot(tok::r_paren)) {
1580 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1581 return;
1582 }
1583 PP.Lex(Tok); // eat the r_paren.
1584
1585 if (Tok.isNot(tok::eod)) {
1586 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1587 return;
1588 }
1589
Reid Kleckner71966c92014-02-20 23:37:45 +00001590 // If the pragma is lexically sound, notify any interested PPCallbacks.
1591 if (PP.getPPCallbacks())
1592 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1593
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001594 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001595}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001596
1597// #pragma clang optimize off
1598// #pragma clang optimize on
1599void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1600 PragmaIntroducerKind Introducer,
1601 Token &FirstToken) {
1602 Token Tok;
1603 PP.Lex(Tok);
1604 if (Tok.is(tok::eod)) {
1605 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_missing_argument);
1606 return;
1607 }
1608 if (Tok.isNot(tok::identifier)) {
1609 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1610 << PP.getSpelling(Tok);
1611 return;
1612 }
1613 const IdentifierInfo *II = Tok.getIdentifierInfo();
1614 // The only accepted values are 'on' or 'off'.
1615 bool IsOn = false;
1616 if (II->isStr("on")) {
1617 IsOn = true;
1618 } else if (!II->isStr("off")) {
1619 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1620 << PP.getSpelling(Tok);
1621 return;
1622 }
1623 PP.Lex(Tok);
1624
1625 if (Tok.isNot(tok::eod)) {
1626 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1627 << PP.getSpelling(Tok);
1628 return;
1629 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001630
1631 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1632}
1633
1634/// \brief Handle the \#pragma clang loop directive.
1635/// #pragma clang 'loop' loop-hints
1636///
1637/// loop-hints:
1638/// loop-hint loop-hints[opt]
1639///
1640/// loop-hint:
1641/// 'vectorize' '(' loop-hint-keyword ')'
1642/// 'interleave' '(' loop-hint-keyword ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001643/// 'unroll' '(' loop-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001644/// 'vectorize_width' '(' loop-hint-value ')'
1645/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001646/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001647///
1648/// loop-hint-keyword:
1649/// 'enable'
1650/// 'disable'
1651///
1652/// loop-hint-value:
1653/// constant-expression
1654///
1655/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1656/// try vectorizing the instructions of the loop it precedes. Specifying
1657/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1658/// interleaving multiple iterations of the loop it precedes. The width of the
1659/// vector instructions is specified by vectorize_width() and the number of
1660/// interleaved loop iterations is specified by interleave_count(). Specifying a
1661/// value of 1 effectively disables vectorization/interleaving, even if it is
1662/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1663/// only works on inner loops.
1664///
Eli Bendersky86483b32014-06-11 17:56:26 +00001665/// The unroll and unroll_count directives control the concatenation
1666/// unroller. Specifying unroll(enable) instructs llvm to try to
1667/// unroll the loop completely, and unroll(disable) disables unrolling
1668/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1669/// try to unroll the loop the number of times indicated by the value.
1670/// If unroll(enable) and unroll_count are both specified only
1671/// unroll_count takes effect.
Eli Bendersky06a40422014-06-06 20:31:48 +00001672void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1673 PragmaIntroducerKind Introducer,
1674 Token &Tok) {
1675 Token Loop = Tok;
1676 SmallVector<Token, 1> TokenList;
1677
1678 // Lex the optimization option and verify it is an identifier.
1679 PP.Lex(Tok);
1680 if (Tok.isNot(tok::identifier)) {
1681 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1682 << /*MissingOption=*/true << "";
1683 return;
1684 }
1685
1686 while (Tok.is(tok::identifier)) {
1687 Token Option = Tok;
1688 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1689
Eli Bendersky86483b32014-06-11 17:56:26 +00001690 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
1691 .Case("vectorize", true)
1692 .Case("interleave", true)
1693 .Case("unroll", true)
1694 .Case("vectorize_width", true)
1695 .Case("interleave_count", true)
1696 .Case("unroll_count", true)
1697 .Default(false);
1698 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001699 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1700 << /*MissingOption=*/false << OptionInfo;
1701 return;
1702 }
1703
1704 // Read '('
1705 PP.Lex(Tok);
1706 if (Tok.isNot(tok::l_paren)) {
1707 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
1708 return;
1709 }
1710
1711 // FIXME: All tokens between '(' and ')' should be stored and parsed as a
1712 // constant expression.
1713 PP.Lex(Tok);
1714 Token Value;
1715 if (Tok.is(tok::identifier) || Tok.is(tok::numeric_constant))
1716 Value = Tok;
1717
1718 // Read ')'
1719 PP.Lex(Tok);
1720 if (Tok.isNot(tok::r_paren)) {
1721 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1722 return;
1723 }
1724
1725 // Get next optimization option.
1726 PP.Lex(Tok);
1727
1728 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1729 Info->Loop = Loop;
1730 Info->Option = Option;
1731 Info->Value = Value;
1732
1733 // Generate the vectorization hint token.
1734 Token LoopHintTok;
1735 LoopHintTok.startToken();
1736 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
1737 LoopHintTok.setLocation(Loop.getLocation());
1738 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
1739 TokenList.push_back(LoopHintTok);
1740 }
1741
1742 if (Tok.isNot(tok::eod)) {
1743 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1744 << "clang loop";
1745 return;
1746 }
1747
1748 Token *TokenArray = new Token[TokenList.size()];
1749 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
1750
1751 PP.EnterTokenStream(TokenArray, TokenList.size(),
1752 /*DisableMacroExpansion=*/false,
1753 /*OwnsTokens=*/true);
1754}