blob: ecdef48f0e228fd2f31ebef7e106770f5e0b05d8 [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
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000151struct PragmaUnrollHintHandler : public PragmaHandler {
152 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
153 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
154 Token &FirstToken) override;
155};
156
Eli Bendersky06a40422014-06-06 20:31:48 +0000157} // end namespace
158
159void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000160 AlignHandler.reset(new PragmaAlignHandler());
161 PP.AddPragmaHandler(AlignHandler.get());
162
163 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
164 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
165
166 OptionsHandler.reset(new PragmaOptionsHandler());
167 PP.AddPragmaHandler(OptionsHandler.get());
168
169 PackHandler.reset(new PragmaPackHandler());
170 PP.AddPragmaHandler(PackHandler.get());
171
172 MSStructHandler.reset(new PragmaMSStructHandler());
173 PP.AddPragmaHandler(MSStructHandler.get());
174
175 UnusedHandler.reset(new PragmaUnusedHandler());
176 PP.AddPragmaHandler(UnusedHandler.get());
177
178 WeakHandler.reset(new PragmaWeakHandler());
179 PP.AddPragmaHandler(WeakHandler.get());
180
181 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
182 PP.AddPragmaHandler(RedefineExtnameHandler.get());
183
184 FPContractHandler.reset(new PragmaFPContractHandler());
185 PP.AddPragmaHandler("STDC", FPContractHandler.get());
186
187 if (getLangOpts().OpenCL) {
188 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
189 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
190
191 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
192 }
193 if (getLangOpts().OpenMP)
194 OpenMPHandler.reset(new PragmaOpenMPHandler());
195 else
196 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
197 PP.AddPragmaHandler(OpenMPHandler.get());
198
199 if (getLangOpts().MicrosoftExt) {
200 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
201 PP.AddPragmaHandler(MSCommentHandler.get());
202 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
203 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
204 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
205 PP.AddPragmaHandler(MSPointersToMembers.get());
206 MSVtorDisp.reset(new PragmaMSVtorDisp());
207 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000208 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000209 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000210 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
211 PP.AddPragmaHandler(MSDataSeg.get());
212 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
213 PP.AddPragmaHandler(MSBSSSeg.get());
214 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
215 PP.AddPragmaHandler(MSConstSeg.get());
216 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
217 PP.AddPragmaHandler(MSCodeSeg.get());
218 MSSection.reset(new PragmaMSPragma("section"));
219 PP.AddPragmaHandler(MSSection.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000220 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000221
222 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
223 PP.AddPragmaHandler("clang", OptimizeHandler.get());
224
225 LoopHintHandler.reset(new PragmaLoopHintHandler());
226 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000227
228 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
229 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000230
231 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
232 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000233}
234
235void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000236 // Remove the pragma handlers we installed.
237 PP.RemovePragmaHandler(AlignHandler.get());
238 AlignHandler.reset();
239 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
240 GCCVisibilityHandler.reset();
241 PP.RemovePragmaHandler(OptionsHandler.get());
242 OptionsHandler.reset();
243 PP.RemovePragmaHandler(PackHandler.get());
244 PackHandler.reset();
245 PP.RemovePragmaHandler(MSStructHandler.get());
246 MSStructHandler.reset();
247 PP.RemovePragmaHandler(UnusedHandler.get());
248 UnusedHandler.reset();
249 PP.RemovePragmaHandler(WeakHandler.get());
250 WeakHandler.reset();
251 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
252 RedefineExtnameHandler.reset();
253
254 if (getLangOpts().OpenCL) {
255 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
256 OpenCLExtensionHandler.reset();
257 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
258 }
259 PP.RemovePragmaHandler(OpenMPHandler.get());
260 OpenMPHandler.reset();
261
262 if (getLangOpts().MicrosoftExt) {
263 PP.RemovePragmaHandler(MSCommentHandler.get());
264 MSCommentHandler.reset();
265 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
266 MSDetectMismatchHandler.reset();
267 PP.RemovePragmaHandler(MSPointersToMembers.get());
268 MSPointersToMembers.reset();
269 PP.RemovePragmaHandler(MSVtorDisp.get());
270 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000271 PP.RemovePragmaHandler(MSInitSeg.get());
272 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000273 PP.RemovePragmaHandler(MSDataSeg.get());
274 MSDataSeg.reset();
275 PP.RemovePragmaHandler(MSBSSSeg.get());
276 MSBSSSeg.reset();
277 PP.RemovePragmaHandler(MSConstSeg.get());
278 MSConstSeg.reset();
279 PP.RemovePragmaHandler(MSCodeSeg.get());
280 MSCodeSeg.reset();
281 PP.RemovePragmaHandler(MSSection.get());
282 MSSection.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000283 }
284
285 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
286 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000287
288 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
289 OptimizeHandler.reset();
290
291 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
292 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000293
294 PP.RemovePragmaHandler(UnrollHintHandler.get());
295 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000296
297 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
298 NoUnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000299}
300
301/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000302///
303/// Each annot_pragma_unused is followed by the argument token so e.g.
304/// "#pragma unused(x,y)" becomes:
305/// annot_pragma_unused 'x' annot_pragma_unused 'y'
306void Parser::HandlePragmaUnused() {
307 assert(Tok.is(tok::annot_pragma_unused));
308 SourceLocation UnusedLoc = ConsumeToken();
309 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
310 ConsumeToken(); // The argument token.
311}
Eli Friedman570024a2010-08-05 06:57:20 +0000312
Rafael Espindola273fd772012-01-26 02:02:57 +0000313void Parser::HandlePragmaVisibility() {
314 assert(Tok.is(tok::annot_pragma_vis));
315 const IdentifierInfo *VisType =
316 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
317 SourceLocation VisLoc = ConsumeToken();
318 Actions.ActOnPragmaVisibility(VisType, VisLoc);
319}
320
Eli Friedmanec52f922012-02-23 23:47:16 +0000321struct PragmaPackInfo {
322 Sema::PragmaPackKind Kind;
323 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000324 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000325 SourceLocation LParenLoc;
326 SourceLocation RParenLoc;
327};
328
329void Parser::HandlePragmaPack() {
330 assert(Tok.is(tok::annot_pragma_pack));
331 PragmaPackInfo *Info =
332 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
333 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000334 ExprResult Alignment;
335 if (Info->Alignment.is(tok::numeric_constant)) {
336 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
337 if (Alignment.isInvalid())
338 return;
339 }
340 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000341 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000342}
343
Eli Friedman68be1642012-10-04 02:36:51 +0000344void Parser::HandlePragmaMSStruct() {
345 assert(Tok.is(tok::annot_pragma_msstruct));
346 Sema::PragmaMSStructKind Kind =
347 static_cast<Sema::PragmaMSStructKind>(
348 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
349 Actions.ActOnPragmaMSStruct(Kind);
350 ConsumeToken(); // The annotation token.
351}
352
353void Parser::HandlePragmaAlign() {
354 assert(Tok.is(tok::annot_pragma_align));
355 Sema::PragmaOptionsAlignKind Kind =
356 static_cast<Sema::PragmaOptionsAlignKind>(
357 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
358 SourceLocation PragmaLoc = ConsumeToken();
359 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
360}
361
362void Parser::HandlePragmaWeak() {
363 assert(Tok.is(tok::annot_pragma_weak));
364 SourceLocation PragmaLoc = ConsumeToken();
365 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
366 Tok.getLocation());
367 ConsumeToken(); // The weak name.
368}
369
370void Parser::HandlePragmaWeakAlias() {
371 assert(Tok.is(tok::annot_pragma_weakalias));
372 SourceLocation PragmaLoc = ConsumeToken();
373 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
374 SourceLocation WeakNameLoc = Tok.getLocation();
375 ConsumeToken();
376 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
377 SourceLocation AliasNameLoc = Tok.getLocation();
378 ConsumeToken();
379 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
380 WeakNameLoc, AliasNameLoc);
381
382}
383
384void Parser::HandlePragmaRedefineExtname() {
385 assert(Tok.is(tok::annot_pragma_redefine_extname));
386 SourceLocation RedefLoc = ConsumeToken();
387 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
388 SourceLocation RedefNameLoc = Tok.getLocation();
389 ConsumeToken();
390 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
391 SourceLocation AliasNameLoc = Tok.getLocation();
392 ConsumeToken();
393 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
394 RedefNameLoc, AliasNameLoc);
395}
396
397void Parser::HandlePragmaFPContract() {
398 assert(Tok.is(tok::annot_pragma_fp_contract));
399 tok::OnOffSwitch OOS =
400 static_cast<tok::OnOffSwitch>(
401 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
402 Actions.ActOnPragmaFPContract(OOS);
403 ConsumeToken(); // The annotation token.
404}
405
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000406StmtResult Parser::HandlePragmaCaptured()
407{
408 assert(Tok.is(tok::annot_pragma_captured));
409 ConsumeToken();
410
411 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000412 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000413 return StmtError();
414 }
415
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000416 SourceLocation Loc = Tok.getLocation();
417
418 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000419 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
420 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000421
422 StmtResult R = ParseCompoundStatement();
423 CapturedRegionScope.Exit();
424
425 if (R.isInvalid()) {
426 Actions.ActOnCapturedRegionError();
427 return StmtError();
428 }
429
430 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000431}
432
Eli Friedman68be1642012-10-04 02:36:51 +0000433namespace {
434 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
435}
436
437void Parser::HandlePragmaOpenCLExtension() {
438 assert(Tok.is(tok::annot_pragma_opencl_extension));
439 OpenCLExtData data =
440 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
441 unsigned state = data.getInt();
442 IdentifierInfo *ename = data.getPointer();
443 SourceLocation NameLoc = Tok.getLocation();
444 ConsumeToken(); // The annotation token.
445
446 OpenCLOptions &f = Actions.getOpenCLOptions();
447 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
448 // overriding all previously issued extension directives, but only if the
449 // behavior is set to disable."
450 if (state == 0 && ename->isStr("all")) {
451#define OPENCLEXT(nm) f.nm = 0;
452#include "clang/Basic/OpenCLExtensions.def"
453 }
454#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
455#include "clang/Basic/OpenCLExtensions.def"
456 else {
457 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
458 return;
459 }
460}
461
David Majnemer4bb09802014-02-10 19:50:15 +0000462void Parser::HandlePragmaMSPointersToMembers() {
463 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000464 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
465 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000466 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
467 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
468 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
469}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000470
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000471void Parser::HandlePragmaMSVtorDisp() {
472 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
473 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
474 Sema::PragmaVtorDispKind Kind =
475 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
476 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
477 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
478 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
479}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000480
Warren Huntc3b18962014-04-08 22:30:47 +0000481void Parser::HandlePragmaMSPragma() {
482 assert(Tok.is(tok::annot_pragma_ms_pragma));
483 // Grab the tokens out of the annotation and enter them into the stream.
484 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
485 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
486 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
487 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000488 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000489 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000490
Warren Huntc3b18962014-04-08 22:30:47 +0000491 // Figure out which #pragma we're dealing with. The switch has no default
492 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000493 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000494 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
495 .Case("data_seg", &Parser::HandlePragmaMSSegment)
496 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
497 .Case("const_seg", &Parser::HandlePragmaMSSegment)
498 .Case("code_seg", &Parser::HandlePragmaMSSegment)
499 .Case("section", &Parser::HandlePragmaMSSection)
500 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000501
502 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
503 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
504 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000505 while (Tok.isNot(tok::eof))
506 PP.Lex(Tok);
507 PP.Lex(Tok);
508 }
509}
510
Reid Kleckner722b1df2014-07-18 00:13:16 +0000511bool Parser::HandlePragmaMSSection(StringRef PragmaName,
512 SourceLocation PragmaLocation) {
513 if (Tok.isNot(tok::l_paren)) {
514 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
515 return false;
516 }
Warren Huntc3b18962014-04-08 22:30:47 +0000517 PP.Lex(Tok); // (
518 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000519 if (Tok.isNot(tok::string_literal)) {
520 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
521 << PragmaName;
522 return false;
523 }
524 ExprResult StringResult = ParseStringLiteralExpression();
525 if (StringResult.isInvalid())
526 return false; // Already diagnosed.
527 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
528 if (SegmentName->getCharByteWidth() != 1) {
529 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
530 << PragmaName;
531 return false;
532 }
Warren Huntc3b18962014-04-08 22:30:47 +0000533 int SectionFlags = 0;
534 while (Tok.is(tok::comma)) {
535 PP.Lex(Tok); // ,
Reid Kleckner722b1df2014-07-18 00:13:16 +0000536 if (!Tok.isAnyIdentifier()) {
537 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
538 << PragmaName;
539 return false;
540 }
Warren Huntc3b18962014-04-08 22:30:47 +0000541 Sema::PragmaSectionFlag Flag =
542 llvm::StringSwitch<Sema::PragmaSectionFlag>(
543 Tok.getIdentifierInfo()->getName())
544 .Case("read", Sema::PSF_Read)
545 .Case("write", Sema::PSF_Write)
546 .Case("execute", Sema::PSF_Execute)
547 .Case("shared", Sema::PSF_Invalid)
548 .Case("nopage", Sema::PSF_Invalid)
549 .Case("nocache", Sema::PSF_Invalid)
550 .Case("discard", Sema::PSF_Invalid)
551 .Case("remove", Sema::PSF_Invalid)
552 .Default(Sema::PSF_None);
553 if (Flag == Sema::PSF_None || Flag == Sema::PSF_Invalid) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000554 PP.Diag(PragmaLocation, Flag == Sema::PSF_None
555 ? diag::warn_pragma_invalid_specific_action
556 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000557 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000558 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000559 }
560 SectionFlags |= Flag;
561 PP.Lex(Tok); // Identifier
562 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000563 if (Tok.isNot(tok::r_paren)) {
564 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
565 return false;
566 }
Warren Huntc3b18962014-04-08 22:30:47 +0000567 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000568 if (Tok.isNot(tok::eof)) {
569 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
570 << PragmaName;
571 return false;
572 }
Warren Huntc3b18962014-04-08 22:30:47 +0000573 PP.Lex(Tok); // eof
574 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000575 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000576}
577
Reid Kleckner722b1df2014-07-18 00:13:16 +0000578bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
579 SourceLocation PragmaLocation) {
580 if (Tok.isNot(tok::l_paren)) {
581 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
582 return false;
583 }
Warren Huntc3b18962014-04-08 22:30:47 +0000584 PP.Lex(Tok); // (
585 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000586 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000587 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000588 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000589 if (PushPop == "push")
590 Action = Sema::PSK_Push;
591 else if (PushPop == "pop")
592 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000593 else {
594 PP.Diag(PragmaLocation,
595 diag::warn_pragma_expected_section_push_pop_or_name)
596 << PragmaName;
597 return false;
598 }
Warren Huntc3b18962014-04-08 22:30:47 +0000599 if (Action != Sema::PSK_Reset) {
600 PP.Lex(Tok); // push | pop
601 if (Tok.is(tok::comma)) {
602 PP.Lex(Tok); // ,
603 // If we've got a comma, we either need a label or a string.
604 if (Tok.isAnyIdentifier()) {
605 SlotLabel = Tok.getIdentifierInfo()->getName();
606 PP.Lex(Tok); // identifier
607 if (Tok.is(tok::comma))
608 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000609 else if (Tok.isNot(tok::r_paren)) {
610 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
611 << PragmaName;
612 return false;
613 }
Warren Huntc3b18962014-04-08 22:30:47 +0000614 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000615 } else if (Tok.isNot(tok::r_paren)) {
616 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
617 return false;
618 }
Warren Huntc3b18962014-04-08 22:30:47 +0000619 }
620 }
621 // Grab the string literal for our section name.
622 StringLiteral *SegmentName = nullptr;
623 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000624 if (Tok.isNot(tok::string_literal)) {
625 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000626 diag::warn_pragma_expected_section_name :
627 diag::warn_pragma_expected_section_label_or_name :
628 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000629 PP.Diag(PragmaLocation, DiagID) << PragmaName;
630 return false;
631 }
632 ExprResult StringResult = ParseStringLiteralExpression();
633 if (StringResult.isInvalid())
634 return false; // Already diagnosed.
635 SegmentName = cast<StringLiteral>(StringResult.get());
636 if (SegmentName->getCharByteWidth() != 1) {
637 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
638 << PragmaName;
639 return false;
640 }
Warren Huntc3b18962014-04-08 22:30:47 +0000641 // Setting section "" has no effect
642 if (SegmentName->getLength())
643 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
644 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000645 if (Tok.isNot(tok::r_paren)) {
646 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
647 return false;
648 }
Warren Huntc3b18962014-04-08 22:30:47 +0000649 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000650 if (Tok.isNot(tok::eof)) {
651 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
652 << PragmaName;
653 return false;
654 }
Warren Huntc3b18962014-04-08 22:30:47 +0000655 PP.Lex(Tok); // eof
656 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
657 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000658 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000659}
660
Reid Kleckner1a711b12014-07-22 00:53:05 +0000661// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000662bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
663 SourceLocation PragmaLocation) {
Reid Kleckner1a711b12014-07-22 00:53:05 +0000664 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
665 PragmaName))
666 return false;
667
668 // Parse either the known section names or the string section name.
669 StringLiteral *SegmentName = nullptr;
670 if (Tok.isAnyIdentifier()) {
671 auto *II = Tok.getIdentifierInfo();
672 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
673 .Case("compiler", "\".CRT$XCC\"")
674 .Case("lib", "\".CRT$XCL\"")
675 .Case("user", "\".CRT$XCU\"")
676 .Default("");
677
678 if (!Section.empty()) {
679 // Pretend the user wrote the appropriate string literal here.
680 Token Toks[1];
681 Toks[0].startToken();
682 Toks[0].setKind(tok::string_literal);
683 Toks[0].setLocation(Tok.getLocation());
684 Toks[0].setLiteralData(Section.data());
685 Toks[0].setLength(Section.size());
686 SegmentName =
687 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
688 PP.Lex(Tok);
689 }
690 } else if (Tok.is(tok::string_literal)) {
691 ExprResult StringResult = ParseStringLiteralExpression();
692 if (StringResult.isInvalid())
693 return false;
694 SegmentName = cast<StringLiteral>(StringResult.get());
695 if (SegmentName->getCharByteWidth() != 1) {
696 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
697 << PragmaName;
698 return false;
699 }
700 // FIXME: Add support for the '[, func-name]' part of the pragma.
701 }
702
703 if (!SegmentName) {
704 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
705 return false;
706 }
707
708 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
709 PragmaName) ||
710 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
711 PragmaName))
712 return false;
713
714 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
715 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000716}
717
718struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000719 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000720 Token Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000721 Token Value;
722 bool HasValue;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000723 PragmaLoopHintInfo() : HasValue(false) {}
Eli Bendersky06a40422014-06-06 20:31:48 +0000724};
725
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000726bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000727 assert(Tok.is(tok::annot_pragma_loop_hint));
728 PragmaLoopHintInfo *Info =
729 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000730 ConsumeToken(); // The annotation token.
Eli Bendersky06a40422014-06-06 20:31:48 +0000731
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000732 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
733 Hint.PragmaNameLoc = IdentifierLoc::create(
734 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000735
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000736 // It is possible that the loop hint has no option identifier, such as
737 // #pragma unroll(4).
738 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
739 ? Info->Option.getIdentifierInfo()
740 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000741 Hint.OptionLoc = IdentifierLoc::create(
742 Actions.Context, Info->Option.getLocation(), OptionInfo);
743
744 // Return a valid hint if pragma unroll or nounroll were specified
745 // without an argument.
746 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
747 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
748 if (!Info->HasValue && (PragmaUnroll || PragmaNoUnroll)) {
749 Hint.Range = Info->PragmaName.getLocation();
750 return true;
751 }
752
753 // If no option is specified the argument is assumed to be numeric.
754 bool StateOption = false;
755 if (OptionInfo)
756 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
757 .Case("vectorize", true)
758 .Case("interleave", true)
759 .Case("unroll", true)
760 .Default(false);
761
762 // Validate the argument.
763 if (StateOption) {
764 bool OptionUnroll = OptionInfo->isStr("unroll");
765 SourceLocation StateLoc = Info->Value.getLocation();
766 IdentifierInfo *StateInfo = Info->Value.getIdentifierInfo();
767 if (!StateInfo || ((OptionUnroll ? !StateInfo->isStr("full")
768 : !StateInfo->isStr("enable")) &&
769 !StateInfo->isStr("disable"))) {
770 Diag(StateLoc, diag::err_pragma_invalid_keyword)
771 << /*MissingArgument=*/false << /*FullKeyword=*/OptionUnroll;
772 return false;
773 }
774 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
775 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000776 // FIXME: We should allow non-type template parameters for the loop hint
777 // value. See bug report #19610
778 if (Info->Value.is(tok::numeric_constant))
779 Hint.ValueExpr = Actions.ActOnNumericConstant(Info->Value).get();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000780 else {
781 Diag(Info->Value.getLocation(), diag::err_pragma_loop_numeric_value);
782 return false;
783 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000784 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000785
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000786 Hint.Range =
787 SourceRange(Info->PragmaName.getLocation(), Info->Value.getLocation());
788 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000789}
790
791// #pragma GCC visibility comes in two variants:
792// 'push' '(' [visibility] ')'
793// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000794void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
795 PragmaIntroducerKind Introducer,
796 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000797 SourceLocation VisLoc = VisTok.getLocation();
798
799 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000800 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000801
802 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
803
Eli Friedman570024a2010-08-05 06:57:20 +0000804 const IdentifierInfo *VisType;
805 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000806 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000807 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000808 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000809 if (Tok.isNot(tok::l_paren)) {
810 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
811 << "visibility";
812 return;
813 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000814 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000815 VisType = Tok.getIdentifierInfo();
816 if (!VisType) {
817 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
818 << "visibility";
819 return;
820 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000821 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000822 if (Tok.isNot(tok::r_paren)) {
823 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
824 << "visibility";
825 return;
826 }
827 } else {
828 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
829 << "visibility";
830 return;
831 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000832 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000833 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000834 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
835 << "visibility";
836 return;
837 }
838
Rafael Espindola273fd772012-01-26 02:02:57 +0000839 Token *Toks = new Token[1];
840 Toks[0].startToken();
841 Toks[0].setKind(tok::annot_pragma_vis);
842 Toks[0].setLocation(VisLoc);
843 Toks[0].setAnnotationValue(
844 const_cast<void*>(static_cast<const void*>(VisType)));
845 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
846 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000847}
848
Daniel Dunbar921b9682008-10-04 19:21:03 +0000849// #pragma pack(...) comes in the following delicious flavors:
850// pack '(' [integer] ')'
851// pack '(' 'show' ')'
852// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000853void PragmaPackHandler::HandlePragma(Preprocessor &PP,
854 PragmaIntroducerKind Introducer,
855 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000856 SourceLocation PackLoc = PackTok.getLocation();
857
858 Token Tok;
859 PP.Lex(Tok);
860 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000861 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000862 return;
863 }
864
John McCallfaf5fb42010-08-26 23:41:50 +0000865 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000866 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000867 Token Alignment;
868 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000869 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000870 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000871 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000872 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000873
874 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000875
876 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
877 // the push/pop stack.
878 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000879 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000880 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000881 } else if (Tok.is(tok::identifier)) {
882 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000883 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000884 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000885 PP.Lex(Tok);
886 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000887 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000888 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000889 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000890 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000891 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000892 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000893 return;
Mike Stump11289f42009-09-09 15:08:12 +0000894 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000895 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000896
Daniel Dunbar921b9682008-10-04 19:21:03 +0000897 if (Tok.is(tok::comma)) {
898 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000899
Daniel Dunbar921b9682008-10-04 19:21:03 +0000900 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000901 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000902
903 PP.Lex(Tok);
904 } else if (Tok.is(tok::identifier)) {
905 Name = Tok.getIdentifierInfo();
906 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000907
Daniel Dunbar921b9682008-10-04 19:21:03 +0000908 if (Tok.is(tok::comma)) {
909 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000910
Daniel Dunbar921b9682008-10-04 19:21:03 +0000911 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000912 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000913 return;
914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
Eli Friedman68be1642012-10-04 02:36:51 +0000916 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000917
918 PP.Lex(Tok);
919 }
920 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000921 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000922 return;
923 }
924 }
925 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000926 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +0000927 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
928 // the push/pop stack.
929 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
930 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000931 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000932
933 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000934 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000935 return;
936 }
937
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000938 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000939 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000940 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000941 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
942 return;
943 }
944
Daniel Dunbar340cf242012-02-29 01:38:22 +0000945 PragmaPackInfo *Info =
946 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
947 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
948 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +0000949 Info->Kind = Kind;
950 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000951 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000952 Info->LParenLoc = LParenLoc;
953 Info->RParenLoc = RParenLoc;
954
Daniel Dunbar340cf242012-02-29 01:38:22 +0000955 Token *Toks =
956 (Token*) PP.getPreprocessorAllocator().Allocate(
957 sizeof(Token) * 1, llvm::alignOf<Token>());
958 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +0000959 Toks[0].startToken();
960 Toks[0].setKind(tok::annot_pragma_pack);
961 Toks[0].setLocation(PackLoc);
962 Toks[0].setAnnotationValue(static_cast<void*>(Info));
963 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +0000964 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000965}
966
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000967// #pragma ms_struct on
968// #pragma ms_struct off
969void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
970 PragmaIntroducerKind Introducer,
971 Token &MSStructTok) {
972 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
973
974 Token Tok;
975 PP.Lex(Tok);
976 if (Tok.isNot(tok::identifier)) {
977 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
978 return;
979 }
980 const IdentifierInfo *II = Tok.getIdentifierInfo();
981 if (II->isStr("on")) {
982 Kind = Sema::PMSST_ON;
983 PP.Lex(Tok);
984 }
985 else if (II->isStr("off") || II->isStr("reset"))
986 PP.Lex(Tok);
987 else {
988 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
989 return;
990 }
991
992 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +0000993 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
994 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000995 return;
996 }
Eli Friedman68be1642012-10-04 02:36:51 +0000997
998 Token *Toks =
999 (Token*) PP.getPreprocessorAllocator().Allocate(
1000 sizeof(Token) * 1, llvm::alignOf<Token>());
1001 new (Toks) Token();
1002 Toks[0].startToken();
1003 Toks[0].setKind(tok::annot_pragma_msstruct);
1004 Toks[0].setLocation(MSStructTok.getLocation());
1005 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1006 static_cast<uintptr_t>(Kind)));
1007 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1008 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001009}
1010
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001011// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1012// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001013static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001014 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001015 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001016
1017 if (IsOptions) {
1018 PP.Lex(Tok);
1019 if (Tok.isNot(tok::identifier) ||
1020 !Tok.getIdentifierInfo()->isStr("align")) {
1021 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1022 return;
1023 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001024 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001025
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001026 PP.Lex(Tok);
1027 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001028 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1029 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001030 return;
1031 }
1032
1033 PP.Lex(Tok);
1034 if (Tok.isNot(tok::identifier)) {
1035 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001036 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001037 return;
1038 }
1039
John McCallfaf5fb42010-08-26 23:41:50 +00001040 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001041 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001042 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001043 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001044 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001045 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001046 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001047 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001048 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001049 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001050 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001051 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001052 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001053 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001054 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001055 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1056 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001057 return;
1058 }
1059
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001060 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001061 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001062 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001063 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001064 return;
1065 }
1066
Eli Friedman68be1642012-10-04 02:36:51 +00001067 Token *Toks =
1068 (Token*) PP.getPreprocessorAllocator().Allocate(
1069 sizeof(Token) * 1, llvm::alignOf<Token>());
1070 new (Toks) Token();
1071 Toks[0].startToken();
1072 Toks[0].setKind(tok::annot_pragma_align);
1073 Toks[0].setLocation(FirstTok.getLocation());
1074 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1075 static_cast<uintptr_t>(Kind)));
1076 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1077 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001078}
1079
Douglas Gregorc7d65762010-09-09 22:45:38 +00001080void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1081 PragmaIntroducerKind Introducer,
1082 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001083 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001084}
1085
Douglas Gregorc7d65762010-09-09 22:45:38 +00001086void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1087 PragmaIntroducerKind Introducer,
1088 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001089 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001090}
1091
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001092// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001093void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1094 PragmaIntroducerKind Introducer,
1095 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001096 // FIXME: Should we be expanding macros here? My guess is no.
1097 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001098
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001099 // Lex the left '('.
1100 Token Tok;
1101 PP.Lex(Tok);
1102 if (Tok.isNot(tok::l_paren)) {
1103 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1104 return;
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001107 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001108 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001109 SourceLocation RParenLoc;
1110 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001111
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001112 while (true) {
1113 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001114
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001115 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001116 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001117 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001118 LexID = false;
1119 continue;
1120 }
1121
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001122 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001123 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1124 return;
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001127 // We are execting a ')' or a ','.
1128 if (Tok.is(tok::comma)) {
1129 LexID = true;
1130 continue;
1131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001133 if (Tok.is(tok::r_paren)) {
1134 RParenLoc = Tok.getLocation();
1135 break;
1136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001138 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001139 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001140 return;
1141 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001142
1143 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001144 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001145 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1146 "unused";
1147 return;
1148 }
1149
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001150 // Verify that we have a location for the right parenthesis.
1151 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001152 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001153
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001154 // For each identifier token, insert into the token stream a
1155 // annot_pragma_unused token followed by the identifier token.
1156 // This allows us to cache a "#pragma unused" that occurs inside an inline
1157 // C++ member function.
1158
Daniel Dunbar340cf242012-02-29 01:38:22 +00001159 Token *Toks =
1160 (Token*) PP.getPreprocessorAllocator().Allocate(
1161 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001162 for (unsigned i=0; i != Identifiers.size(); i++) {
1163 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1164 pragmaUnusedTok.startToken();
1165 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1166 pragmaUnusedTok.setLocation(UnusedLoc);
1167 idTok = Identifiers[i];
1168 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001169 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1170 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001171}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001172
1173// #pragma weak identifier
1174// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001175void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1176 PragmaIntroducerKind Introducer,
1177 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001178 SourceLocation WeakLoc = WeakTok.getLocation();
1179
1180 Token Tok;
1181 PP.Lex(Tok);
1182 if (Tok.isNot(tok::identifier)) {
1183 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1184 return;
1185 }
1186
Eli Friedman68be1642012-10-04 02:36:51 +00001187 Token WeakName = Tok;
1188 bool HasAlias = false;
1189 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001190
1191 PP.Lex(Tok);
1192 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001193 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001194 PP.Lex(Tok);
1195 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001196 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001197 << "weak";
1198 return;
1199 }
Eli Friedman68be1642012-10-04 02:36:51 +00001200 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001201 PP.Lex(Tok);
1202 }
1203
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001204 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001205 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1206 return;
1207 }
1208
Eli Friedman68be1642012-10-04 02:36:51 +00001209 if (HasAlias) {
1210 Token *Toks =
1211 (Token*) PP.getPreprocessorAllocator().Allocate(
1212 sizeof(Token) * 3, llvm::alignOf<Token>());
1213 Token &pragmaUnusedTok = Toks[0];
1214 pragmaUnusedTok.startToken();
1215 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1216 pragmaUnusedTok.setLocation(WeakLoc);
1217 Toks[1] = WeakName;
1218 Toks[2] = AliasName;
1219 PP.EnterTokenStream(Toks, 3,
1220 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001221 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001222 Token *Toks =
1223 (Token*) PP.getPreprocessorAllocator().Allocate(
1224 sizeof(Token) * 2, llvm::alignOf<Token>());
1225 Token &pragmaUnusedTok = Toks[0];
1226 pragmaUnusedTok.startToken();
1227 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1228 pragmaUnusedTok.setLocation(WeakLoc);
1229 Toks[1] = WeakName;
1230 PP.EnterTokenStream(Toks, 2,
1231 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001232 }
1233}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001234
David Chisnall0867d9c2012-02-18 16:12:34 +00001235// #pragma redefine_extname identifier identifier
1236void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1237 PragmaIntroducerKind Introducer,
1238 Token &RedefToken) {
1239 SourceLocation RedefLoc = RedefToken.getLocation();
1240
1241 Token Tok;
1242 PP.Lex(Tok);
1243 if (Tok.isNot(tok::identifier)) {
1244 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1245 "redefine_extname";
1246 return;
1247 }
1248
Eli Friedman68be1642012-10-04 02:36:51 +00001249 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001250 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001251
David Chisnall0867d9c2012-02-18 16:12:34 +00001252 if (Tok.isNot(tok::identifier)) {
1253 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1254 << "redefine_extname";
1255 return;
1256 }
Eli Friedman68be1642012-10-04 02:36:51 +00001257
1258 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001259 PP.Lex(Tok);
1260
1261 if (Tok.isNot(tok::eod)) {
1262 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1263 "redefine_extname";
1264 return;
1265 }
1266
Eli Friedman68be1642012-10-04 02:36:51 +00001267 Token *Toks =
1268 (Token*) PP.getPreprocessorAllocator().Allocate(
1269 sizeof(Token) * 3, llvm::alignOf<Token>());
1270 Token &pragmaRedefTok = Toks[0];
1271 pragmaRedefTok.startToken();
1272 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1273 pragmaRedefTok.setLocation(RedefLoc);
1274 Toks[1] = RedefName;
1275 Toks[2] = AliasName;
1276 PP.EnterTokenStream(Toks, 3,
1277 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001278}
1279
1280
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001281void
1282PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1283 PragmaIntroducerKind Introducer,
1284 Token &Tok) {
1285 tok::OnOffSwitch OOS;
1286 if (PP.LexOnOffSwitch(OOS))
1287 return;
1288
Eli Friedman68be1642012-10-04 02:36:51 +00001289 Token *Toks =
1290 (Token*) PP.getPreprocessorAllocator().Allocate(
1291 sizeof(Token) * 1, llvm::alignOf<Token>());
1292 new (Toks) Token();
1293 Toks[0].startToken();
1294 Toks[0].setKind(tok::annot_pragma_fp_contract);
1295 Toks[0].setLocation(Tok.getLocation());
1296 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1297 static_cast<uintptr_t>(OOS)));
1298 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1299 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001300}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001301
1302void
1303PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1304 PragmaIntroducerKind Introducer,
1305 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001306 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001307 if (Tok.isNot(tok::identifier)) {
1308 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1309 "OPENCL";
1310 return;
1311 }
1312 IdentifierInfo *ename = Tok.getIdentifierInfo();
1313 SourceLocation NameLoc = Tok.getLocation();
1314
1315 PP.Lex(Tok);
1316 if (Tok.isNot(tok::colon)) {
1317 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1318 return;
1319 }
1320
1321 PP.Lex(Tok);
1322 if (Tok.isNot(tok::identifier)) {
1323 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1324 return;
1325 }
1326 IdentifierInfo *op = Tok.getIdentifierInfo();
1327
1328 unsigned state;
1329 if (op->isStr("enable")) {
1330 state = 1;
1331 } else if (op->isStr("disable")) {
1332 state = 0;
1333 } else {
1334 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1335 return;
1336 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001337 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001338
Eli Friedman68be1642012-10-04 02:36:51 +00001339 PP.Lex(Tok);
1340 if (Tok.isNot(tok::eod)) {
1341 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1342 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001343 return;
1344 }
Eli Friedman68be1642012-10-04 02:36:51 +00001345
1346 OpenCLExtData data(ename, state);
1347 Token *Toks =
1348 (Token*) PP.getPreprocessorAllocator().Allocate(
1349 sizeof(Token) * 1, llvm::alignOf<Token>());
1350 new (Toks) Token();
1351 Toks[0].startToken();
1352 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1353 Toks[0].setLocation(NameLoc);
1354 Toks[0].setAnnotationValue(data.getOpaqueValue());
1355 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1356 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001357
1358 if (PP.getPPCallbacks())
1359 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1360 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001361}
1362
Alexey Bataeva769e072013-03-22 06:34:35 +00001363/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1364///
1365void
1366PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1367 PragmaIntroducerKind Introducer,
1368 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001369 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1370 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001371 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001372 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1373 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001374 }
1375 PP.DiscardUntilEndOfDirective();
1376}
1377
1378/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1379///
1380void
1381PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1382 PragmaIntroducerKind Introducer,
1383 Token &FirstTok) {
1384 SmallVector<Token, 16> Pragma;
1385 Token Tok;
1386 Tok.startToken();
1387 Tok.setKind(tok::annot_pragma_openmp);
1388 Tok.setLocation(FirstTok.getLocation());
1389
1390 while (Tok.isNot(tok::eod)) {
1391 Pragma.push_back(Tok);
1392 PP.Lex(Tok);
1393 }
1394 SourceLocation EodLoc = Tok.getLocation();
1395 Tok.startToken();
1396 Tok.setKind(tok::annot_pragma_openmp_end);
1397 Tok.setLocation(EodLoc);
1398 Pragma.push_back(Tok);
1399
1400 Token *Toks = new Token[Pragma.size()];
1401 std::copy(Pragma.begin(), Pragma.end(), Toks);
1402 PP.EnterTokenStream(Toks, Pragma.size(),
1403 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1404}
Reid Kleckner002562a2013-05-06 21:02:12 +00001405
David Majnemer4bb09802014-02-10 19:50:15 +00001406/// \brief Handle '#pragma pointers_to_members'
1407// The grammar for this pragma is as follows:
1408//
1409// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1410//
1411// #pragma pointers_to_members '(' 'best_case' ')'
1412// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1413// #pragma pointers_to_members '(' inheritance-model ')'
1414void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1415 PragmaIntroducerKind Introducer,
1416 Token &Tok) {
1417 SourceLocation PointersToMembersLoc = Tok.getLocation();
1418 PP.Lex(Tok);
1419 if (Tok.isNot(tok::l_paren)) {
1420 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1421 << "pointers_to_members";
1422 return;
1423 }
1424 PP.Lex(Tok);
1425 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1426 if (!Arg) {
1427 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1428 << "pointers_to_members";
1429 return;
1430 }
1431 PP.Lex(Tok);
1432
David Majnemer86c318f2014-02-11 21:05:00 +00001433 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001434 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001435 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001436 } else {
1437 if (Arg->isStr("full_generality")) {
1438 if (Tok.is(tok::comma)) {
1439 PP.Lex(Tok);
1440
1441 Arg = Tok.getIdentifierInfo();
1442 if (!Arg) {
1443 PP.Diag(Tok.getLocation(),
1444 diag::err_pragma_pointers_to_members_unknown_kind)
1445 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1446 return;
1447 }
1448 PP.Lex(Tok);
1449 } else if (Tok.is(tok::r_paren)) {
1450 // #pragma pointers_to_members(full_generality) implicitly specifies
1451 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001452 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001453 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001454 } else {
1455 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1456 << "full_generality";
1457 return;
1458 }
1459 }
1460
1461 if (Arg) {
1462 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001463 RepresentationMethod =
1464 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001465 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001466 RepresentationMethod =
1467 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001468 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001469 RepresentationMethod =
1470 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001471 } else {
1472 PP.Diag(Tok.getLocation(),
1473 diag::err_pragma_pointers_to_members_unknown_kind)
1474 << Arg << /*HasPointerDeclaration*/ 1;
1475 return;
1476 }
1477 }
1478 }
1479
1480 if (Tok.isNot(tok::r_paren)) {
1481 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1482 << (Arg ? Arg->getName() : "full_generality");
1483 return;
1484 }
1485
1486 PP.Lex(Tok);
1487 if (Tok.isNot(tok::eod)) {
1488 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1489 << "pointers_to_members";
1490 return;
1491 }
1492
1493 Token AnnotTok;
1494 AnnotTok.startToken();
1495 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1496 AnnotTok.setLocation(PointersToMembersLoc);
1497 AnnotTok.setAnnotationValue(
1498 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1499 PP.EnterToken(AnnotTok);
1500}
1501
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001502/// \brief Handle '#pragma vtordisp'
1503// The grammar for this pragma is as follows:
1504//
1505// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1506//
1507// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1508// #pragma vtordisp '(' 'pop' ')'
1509// #pragma vtordisp '(' ')'
1510void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1511 PragmaIntroducerKind Introducer,
1512 Token &Tok) {
1513 SourceLocation VtorDispLoc = Tok.getLocation();
1514 PP.Lex(Tok);
1515 if (Tok.isNot(tok::l_paren)) {
1516 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1517 return;
1518 }
1519 PP.Lex(Tok);
1520
1521 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1522 const IdentifierInfo *II = Tok.getIdentifierInfo();
1523 if (II) {
1524 if (II->isStr("push")) {
1525 // #pragma vtordisp(push, mode)
1526 PP.Lex(Tok);
1527 if (Tok.isNot(tok::comma)) {
1528 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1529 return;
1530 }
1531 PP.Lex(Tok);
1532 Kind = Sema::PVDK_Push;
1533 // not push, could be on/off
1534 } else if (II->isStr("pop")) {
1535 // #pragma vtordisp(pop)
1536 PP.Lex(Tok);
1537 Kind = Sema::PVDK_Pop;
1538 }
1539 // not push or pop, could be on/off
1540 } else {
1541 if (Tok.is(tok::r_paren)) {
1542 // #pragma vtordisp()
1543 Kind = Sema::PVDK_Reset;
1544 }
1545 }
1546
1547
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001548 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001549 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1550 const IdentifierInfo *II = Tok.getIdentifierInfo();
1551 if (II && II->isStr("off")) {
1552 PP.Lex(Tok);
1553 Value = 0;
1554 } else if (II && II->isStr("on")) {
1555 PP.Lex(Tok);
1556 Value = 1;
1557 } else if (Tok.is(tok::numeric_constant) &&
1558 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1559 if (Value > 2) {
1560 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1561 << 0 << 2 << "vtordisp";
1562 return;
1563 }
1564 } else {
1565 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1566 << "vtordisp";
1567 return;
1568 }
1569 }
1570
1571 // Finish the pragma: ')' $
1572 if (Tok.isNot(tok::r_paren)) {
1573 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1574 return;
1575 }
1576 PP.Lex(Tok);
1577 if (Tok.isNot(tok::eod)) {
1578 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1579 << "vtordisp";
1580 return;
1581 }
1582
1583 // Enter the annotation.
1584 Token AnnotTok;
1585 AnnotTok.startToken();
1586 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1587 AnnotTok.setLocation(VtorDispLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001588 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1589 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001590 PP.EnterToken(AnnotTok);
1591}
1592
Warren Huntc3b18962014-04-08 22:30:47 +00001593/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1594/// an annotation token.
1595void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1596 PragmaIntroducerKind Introducer,
1597 Token &Tok) {
1598 Token EoF, AnnotTok;
1599 EoF.startToken();
1600 EoF.setKind(tok::eof);
1601 AnnotTok.startToken();
1602 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1603 AnnotTok.setLocation(Tok.getLocation());
1604 SmallVector<Token, 8> TokenVector;
1605 // Suck up all of the tokens before the eod.
1606 for (; Tok.isNot(tok::eod); PP.Lex(Tok))
1607 TokenVector.push_back(Tok);
1608 // Add a sentinal EoF token to the end of the list.
1609 TokenVector.push_back(EoF);
1610 // We must allocate this array with new because EnterTokenStream is going to
1611 // delete it later.
1612 Token *TokenArray = new Token[TokenVector.size()];
1613 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1614 auto Value = new (PP.getPreprocessorAllocator())
1615 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1616 AnnotTok.setAnnotationValue(Value);
1617 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001618}
1619
Aaron Ballman5d041be2013-06-04 02:07:14 +00001620/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1621///
1622/// The syntax is:
1623/// \code
1624/// #pragma detect_mismatch("name", "value")
1625/// \endcode
1626/// Where 'name' and 'value' are quoted strings. The values are embedded in
1627/// the object file and passed along to the linker. If the linker detects a
1628/// mismatch in the object file's values for the given name, a LNK2038 error
1629/// is emitted. See MSDN for more details.
1630void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1631 PragmaIntroducerKind Introducer,
1632 Token &Tok) {
1633 SourceLocation CommentLoc = Tok.getLocation();
1634 PP.Lex(Tok);
1635 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001636 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001637 return;
1638 }
1639
1640 // Read the name to embed, which must be a string literal.
1641 std::string NameString;
1642 if (!PP.LexStringLiteral(Tok, NameString,
1643 "pragma detect_mismatch",
1644 /*MacroExpansion=*/true))
1645 return;
1646
1647 // Read the comma followed by a second string literal.
1648 std::string ValueString;
1649 if (Tok.isNot(tok::comma)) {
1650 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1651 return;
1652 }
1653
1654 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1655 /*MacroExpansion=*/true))
1656 return;
1657
1658 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001659 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001660 return;
1661 }
1662 PP.Lex(Tok); // Eat the r_paren.
1663
1664 if (Tok.isNot(tok::eod)) {
1665 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1666 return;
1667 }
1668
Reid Kleckner71966c92014-02-20 23:37:45 +00001669 // If the pragma is lexically sound, notify any interested PPCallbacks.
1670 if (PP.getPPCallbacks())
1671 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1672 ValueString);
1673
Aaron Ballman5d041be2013-06-04 02:07:14 +00001674 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1675}
1676
Reid Kleckner002562a2013-05-06 21:02:12 +00001677/// \brief Handle the microsoft \#pragma comment extension.
1678///
1679/// The syntax is:
1680/// \code
1681/// #pragma comment(linker, "foo")
1682/// \endcode
1683/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1684/// "foo" is a string, which is fully macro expanded, and permits string
1685/// concatenation, embedded escape characters etc. See MSDN for more details.
1686void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1687 PragmaIntroducerKind Introducer,
1688 Token &Tok) {
1689 SourceLocation CommentLoc = Tok.getLocation();
1690 PP.Lex(Tok);
1691 if (Tok.isNot(tok::l_paren)) {
1692 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1693 return;
1694 }
1695
1696 // Read the identifier.
1697 PP.Lex(Tok);
1698 if (Tok.isNot(tok::identifier)) {
1699 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1700 return;
1701 }
1702
1703 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001704 IdentifierInfo *II = Tok.getIdentifierInfo();
1705 Sema::PragmaMSCommentKind Kind =
1706 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1707 .Case("linker", Sema::PCK_Linker)
1708 .Case("lib", Sema::PCK_Lib)
1709 .Case("compiler", Sema::PCK_Compiler)
1710 .Case("exestr", Sema::PCK_ExeStr)
1711 .Case("user", Sema::PCK_User)
1712 .Default(Sema::PCK_Unknown);
1713 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001714 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1715 return;
1716 }
1717
1718 // Read the optional string if present.
1719 PP.Lex(Tok);
1720 std::string ArgumentString;
1721 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1722 "pragma comment",
1723 /*MacroExpansion=*/true))
1724 return;
1725
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001726 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001727 // FIXME: If the kind is "compiler" warn if the string is present (it is
1728 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001729 // The MSDN docs say that "lib" and "linker" require a string and have a short
1730 // whitelist of linker options they support, but in practice MSVC doesn't
1731 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001732
1733 if (Tok.isNot(tok::r_paren)) {
1734 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1735 return;
1736 }
1737 PP.Lex(Tok); // eat the r_paren.
1738
1739 if (Tok.isNot(tok::eod)) {
1740 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1741 return;
1742 }
1743
Reid Kleckner71966c92014-02-20 23:37:45 +00001744 // If the pragma is lexically sound, notify any interested PPCallbacks.
1745 if (PP.getPPCallbacks())
1746 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1747
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001748 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001749}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001750
1751// #pragma clang optimize off
1752// #pragma clang optimize on
1753void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1754 PragmaIntroducerKind Introducer,
1755 Token &FirstToken) {
1756 Token Tok;
1757 PP.Lex(Tok);
1758 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001759 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001760 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001761 return;
1762 }
1763 if (Tok.isNot(tok::identifier)) {
1764 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1765 << PP.getSpelling(Tok);
1766 return;
1767 }
1768 const IdentifierInfo *II = Tok.getIdentifierInfo();
1769 // The only accepted values are 'on' or 'off'.
1770 bool IsOn = false;
1771 if (II->isStr("on")) {
1772 IsOn = true;
1773 } else if (!II->isStr("off")) {
1774 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1775 << PP.getSpelling(Tok);
1776 return;
1777 }
1778 PP.Lex(Tok);
1779
1780 if (Tok.isNot(tok::eod)) {
1781 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1782 << PP.getSpelling(Tok);
1783 return;
1784 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001785
1786 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1787}
1788
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001789/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001790static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1791 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001792 PragmaLoopHintInfo &Info) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001793 if (ValueInParens) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001794 if (Tok.is(tok::r_paren)) {
1795 // Nothing between the parentheses.
1796 std::string PragmaString;
1797 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1798 PragmaString = "clang loop ";
1799 PragmaString += Option.getIdentifierInfo()->getName();
1800 } else {
1801 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1802 "Unexpected pragma name");
1803 PragmaString = "unroll";
1804 }
Mark Heffernan450c2382014-07-23 17:31:31 +00001805 // Don't try to emit what the pragma is expecting with the diagnostic
1806 // because the logic is non-trivial and we give expected values in sema
1807 // diagnostics if an invalid argument is given. Here, just note that the
1808 // pragma is missing an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001809 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001810 << PragmaString << /*Expected=*/false;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001811 return true;
1812 }
1813 }
1814
1815 // FIXME: Value should be stored and parsed as a constant expression.
1816 Token Value = Tok;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001817 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001818
1819 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001820 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001821 if (Tok.isNot(tok::r_paren)) {
1822 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1823 return true;
1824 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001825 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001826 }
1827
1828 Info.PragmaName = PragmaName;
1829 Info.Option = Option;
1830 Info.Value = Value;
1831 Info.HasValue = true;
1832 return false;
1833}
1834
Eli Bendersky06a40422014-06-06 20:31:48 +00001835/// \brief Handle the \#pragma clang loop directive.
1836/// #pragma clang 'loop' loop-hints
1837///
1838/// loop-hints:
1839/// loop-hint loop-hints[opt]
1840///
1841/// loop-hint:
1842/// 'vectorize' '(' loop-hint-keyword ')'
1843/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001844/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001845/// 'vectorize_width' '(' loop-hint-value ')'
1846/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001847/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001848///
1849/// loop-hint-keyword:
1850/// 'enable'
1851/// 'disable'
1852///
Mark Heffernan450c2382014-07-23 17:31:31 +00001853/// unroll-hint-keyword:
1854/// 'full'
1855/// 'disable'
1856///
Eli Bendersky06a40422014-06-06 20:31:48 +00001857/// loop-hint-value:
1858/// constant-expression
1859///
1860/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1861/// try vectorizing the instructions of the loop it precedes. Specifying
1862/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1863/// interleaving multiple iterations of the loop it precedes. The width of the
1864/// vector instructions is specified by vectorize_width() and the number of
1865/// interleaved loop iterations is specified by interleave_count(). Specifying a
1866/// value of 1 effectively disables vectorization/interleaving, even if it is
1867/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1868/// only works on inner loops.
1869///
Eli Bendersky86483b32014-06-11 17:56:26 +00001870/// The unroll and unroll_count directives control the concatenation
Mark Heffernan450c2382014-07-23 17:31:31 +00001871/// unroller. Specifying unroll(full) instructs llvm to try to
Eli Bendersky86483b32014-06-11 17:56:26 +00001872/// unroll the loop completely, and unroll(disable) disables unrolling
1873/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1874/// try to unroll the loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001875void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1876 PragmaIntroducerKind Introducer,
1877 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001878 // Incoming token is "loop" from "#pragma clang loop".
1879 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001880 SmallVector<Token, 1> TokenList;
1881
1882 // Lex the optimization option and verify it is an identifier.
1883 PP.Lex(Tok);
1884 if (Tok.isNot(tok::identifier)) {
1885 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1886 << /*MissingOption=*/true << "";
1887 return;
1888 }
1889
1890 while (Tok.is(tok::identifier)) {
1891 Token Option = Tok;
1892 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1893
Eli Bendersky86483b32014-06-11 17:56:26 +00001894 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001895 .Case("vectorize", true)
1896 .Case("interleave", true)
1897 .Case("unroll", true)
1898 .Case("vectorize_width", true)
1899 .Case("interleave_count", true)
1900 .Case("unroll_count", true)
1901 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00001902 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001903 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1904 << /*MissingOption=*/false << OptionInfo;
1905 return;
1906 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00001907 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001908
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001909 // Read '('
1910 if (Tok.isNot(tok::l_paren)) {
1911 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00001912 return;
1913 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001914 PP.Lex(Tok);
1915
1916 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1917 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
1918 *Info))
1919 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00001920
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001921 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00001922 Token LoopHintTok;
1923 LoopHintTok.startToken();
1924 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001925 LoopHintTok.setLocation(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00001926 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
1927 TokenList.push_back(LoopHintTok);
1928 }
1929
1930 if (Tok.isNot(tok::eod)) {
1931 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1932 << "clang loop";
1933 return;
1934 }
1935
1936 Token *TokenArray = new Token[TokenList.size()];
1937 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
1938
1939 PP.EnterTokenStream(TokenArray, TokenList.size(),
1940 /*DisableMacroExpansion=*/false,
1941 /*OwnsTokens=*/true);
1942}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001943
1944/// \brief Handle the loop unroll optimization pragmas.
1945/// #pragma unroll
1946/// #pragma unroll unroll-hint-value
1947/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00001948/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001949///
1950/// unroll-hint-value:
1951/// constant-expression
1952///
Mark Heffernanc888e412014-07-24 18:09:38 +00001953/// Loop unrolling hints can be specified with '#pragma unroll' or
1954/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
1955/// contained in parentheses. With no argument the directive instructs llvm to
1956/// try to unroll the loop completely. A positive integer argument can be
1957/// specified to indicate the number of times the loop should be unrolled. To
1958/// maximize compatibility with other compilers the unroll count argument can be
1959/// specified with or without parentheses. Specifying, '#pragma nounroll'
1960/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001961void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
1962 PragmaIntroducerKind Introducer,
1963 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00001964 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
1965 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001966 Token PragmaName = Tok;
1967 PP.Lex(Tok);
1968 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1969 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00001970 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001971 Info->PragmaName = PragmaName;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001972 Info->HasValue = false;
Aaron Ballmand6807da2014-08-01 12:41:37 +00001973 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00001974 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
1975 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1976 << "nounroll";
1977 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001978 } else {
1979 // Unroll pragma with an argument: "#pragma unroll N" or
1980 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001981 // Read '(' if it exists.
1982 bool ValueInParens = Tok.is(tok::l_paren);
1983 if (ValueInParens)
1984 PP.Lex(Tok);
1985
Aaron Ballmand0b090d2014-08-01 12:20:20 +00001986 Token Option;
1987 Option.startToken();
1988 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001989 return;
1990
1991 // In CUDA, the argument to '#pragma unroll' should not be contained in
1992 // parentheses.
1993 if (PP.getLangOpts().CUDA && ValueInParens)
1994 PP.Diag(Info->Value.getLocation(),
1995 diag::warn_pragma_unroll_cuda_value_in_parens);
1996
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001997 if (Tok.isNot(tok::eod)) {
1998 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1999 << "unroll";
2000 return;
2001 }
2002 }
2003
2004 // Generate the hint token.
2005 Token *TokenArray = new Token[1];
2006 TokenArray[0].startToken();
2007 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2008 TokenArray[0].setLocation(PragmaName.getLocation());
2009 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2010 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2011 /*OwnsTokens=*/true);
2012}