blob: bc79e3d296630c7b3c5381d1508b8fee15cd7943 [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;
Eli Bendersky06a40422014-06-06 20:31:48 +0000723};
724
Tyler Nowickicab7ca32014-07-30 20:54:33 +0000725bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000726 assert(Tok.is(tok::annot_pragma_loop_hint));
727 PragmaLoopHintInfo *Info =
728 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
Tyler Nowickicab7ca32014-07-30 20:54:33 +0000729 ConsumeToken(); // The annotation token.
Eli Bendersky06a40422014-06-06 20:31:48 +0000730
Tyler Nowickicab7ca32014-07-30 20:54:33 +0000731 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
732 Hint.PragmaNameLoc = IdentifierLoc::create(
733 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000734
Tyler Nowickicab7ca32014-07-30 20:54:33 +0000735 IdentifierInfo *OptionInfo = Info->Option.getIdentifierInfo();
736 Hint.OptionLoc = IdentifierLoc::create(
737 Actions.Context, Info->Option.getLocation(), OptionInfo);
738
739 // Return a valid hint if pragma unroll or nounroll were specified
740 // without an argument.
741 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
742 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
743 if (!Info->HasValue && (PragmaUnroll || PragmaNoUnroll)) {
744 Hint.Range = Info->PragmaName.getLocation();
745 return true;
746 }
747
748 // If no option is specified the argument is assumed to be numeric.
749 bool StateOption = false;
750 if (OptionInfo)
751 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
752 .Case("vectorize", true)
753 .Case("interleave", true)
754 .Case("unroll", true)
755 .Default(false);
756
757 // Validate the argument.
758 if (StateOption) {
759 bool OptionUnroll = OptionInfo->isStr("unroll");
760 SourceLocation StateLoc = Info->Value.getLocation();
761 IdentifierInfo *StateInfo = Info->Value.getIdentifierInfo();
762 if (!StateInfo || ((OptionUnroll ? !StateInfo->isStr("full")
763 : !StateInfo->isStr("enable")) &&
764 !StateInfo->isStr("disable"))) {
765 Diag(StateLoc, diag::err_pragma_invalid_keyword)
766 << /*MissingArgument=*/false << /*FullKeyword=*/OptionUnroll;
767 return false;
768 }
769 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
770 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000771 // FIXME: We should allow non-type template parameters for the loop hint
772 // value. See bug report #19610
773 if (Info->Value.is(tok::numeric_constant))
774 Hint.ValueExpr = Actions.ActOnNumericConstant(Info->Value).get();
Tyler Nowickicab7ca32014-07-30 20:54:33 +0000775 else {
776 Diag(Info->Value.getLocation(), diag::err_pragma_loop_numeric_value);
777 return false;
778 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000779 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000780
Tyler Nowickicab7ca32014-07-30 20:54:33 +0000781 Hint.Range =
782 SourceRange(Info->PragmaName.getLocation(), Info->Value.getLocation());
783 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000784}
785
786// #pragma GCC visibility comes in two variants:
787// 'push' '(' [visibility] ')'
788// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000789void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
790 PragmaIntroducerKind Introducer,
791 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000792 SourceLocation VisLoc = VisTok.getLocation();
793
794 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000795 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000796
797 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
798
Eli Friedman570024a2010-08-05 06:57:20 +0000799 const IdentifierInfo *VisType;
800 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000801 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000802 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000803 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000804 if (Tok.isNot(tok::l_paren)) {
805 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
806 << "visibility";
807 return;
808 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000809 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000810 VisType = Tok.getIdentifierInfo();
811 if (!VisType) {
812 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
813 << "visibility";
814 return;
815 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000816 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000817 if (Tok.isNot(tok::r_paren)) {
818 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
819 << "visibility";
820 return;
821 }
822 } else {
823 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
824 << "visibility";
825 return;
826 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000827 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000828 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000829 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
830 << "visibility";
831 return;
832 }
833
Rafael Espindola273fd772012-01-26 02:02:57 +0000834 Token *Toks = new Token[1];
835 Toks[0].startToken();
836 Toks[0].setKind(tok::annot_pragma_vis);
837 Toks[0].setLocation(VisLoc);
838 Toks[0].setAnnotationValue(
839 const_cast<void*>(static_cast<const void*>(VisType)));
840 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
841 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000842}
843
Daniel Dunbar921b9682008-10-04 19:21:03 +0000844// #pragma pack(...) comes in the following delicious flavors:
845// pack '(' [integer] ')'
846// pack '(' 'show' ')'
847// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000848void PragmaPackHandler::HandlePragma(Preprocessor &PP,
849 PragmaIntroducerKind Introducer,
850 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000851 SourceLocation PackLoc = PackTok.getLocation();
852
853 Token Tok;
854 PP.Lex(Tok);
855 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000856 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000857 return;
858 }
859
John McCallfaf5fb42010-08-26 23:41:50 +0000860 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000861 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000862 Token Alignment;
863 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000864 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000865 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000866 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000867 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000868
869 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000870
871 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
872 // the push/pop stack.
873 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000874 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000875 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000876 } else if (Tok.is(tok::identifier)) {
877 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000878 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000879 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000880 PP.Lex(Tok);
881 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000882 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000883 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000884 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000885 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000886 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000887 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000888 return;
Mike Stump11289f42009-09-09 15:08:12 +0000889 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000890 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000891
Daniel Dunbar921b9682008-10-04 19:21:03 +0000892 if (Tok.is(tok::comma)) {
893 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000894
Daniel Dunbar921b9682008-10-04 19:21:03 +0000895 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000896 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000897
898 PP.Lex(Tok);
899 } else if (Tok.is(tok::identifier)) {
900 Name = Tok.getIdentifierInfo();
901 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000902
Daniel Dunbar921b9682008-10-04 19:21:03 +0000903 if (Tok.is(tok::comma)) {
904 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000905
Daniel Dunbar921b9682008-10-04 19:21:03 +0000906 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000907 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000908 return;
909 }
Mike Stump11289f42009-09-09 15:08:12 +0000910
Eli Friedman68be1642012-10-04 02:36:51 +0000911 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000912
913 PP.Lex(Tok);
914 }
915 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000916 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000917 return;
918 }
919 }
920 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000921 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +0000922 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
923 // the push/pop stack.
924 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
925 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000926 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000927
928 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000929 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000930 return;
931 }
932
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000933 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000934 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000935 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000936 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
937 return;
938 }
939
Daniel Dunbar340cf242012-02-29 01:38:22 +0000940 PragmaPackInfo *Info =
941 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
942 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
943 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +0000944 Info->Kind = Kind;
945 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000946 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000947 Info->LParenLoc = LParenLoc;
948 Info->RParenLoc = RParenLoc;
949
Daniel Dunbar340cf242012-02-29 01:38:22 +0000950 Token *Toks =
951 (Token*) PP.getPreprocessorAllocator().Allocate(
952 sizeof(Token) * 1, llvm::alignOf<Token>());
953 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +0000954 Toks[0].startToken();
955 Toks[0].setKind(tok::annot_pragma_pack);
956 Toks[0].setLocation(PackLoc);
957 Toks[0].setAnnotationValue(static_cast<void*>(Info));
958 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +0000959 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000960}
961
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000962// #pragma ms_struct on
963// #pragma ms_struct off
964void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
965 PragmaIntroducerKind Introducer,
966 Token &MSStructTok) {
967 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
968
969 Token Tok;
970 PP.Lex(Tok);
971 if (Tok.isNot(tok::identifier)) {
972 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
973 return;
974 }
975 const IdentifierInfo *II = Tok.getIdentifierInfo();
976 if (II->isStr("on")) {
977 Kind = Sema::PMSST_ON;
978 PP.Lex(Tok);
979 }
980 else if (II->isStr("off") || II->isStr("reset"))
981 PP.Lex(Tok);
982 else {
983 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
984 return;
985 }
986
987 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +0000988 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
989 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000990 return;
991 }
Eli Friedman68be1642012-10-04 02:36:51 +0000992
993 Token *Toks =
994 (Token*) PP.getPreprocessorAllocator().Allocate(
995 sizeof(Token) * 1, llvm::alignOf<Token>());
996 new (Toks) Token();
997 Toks[0].startToken();
998 Toks[0].setKind(tok::annot_pragma_msstruct);
999 Toks[0].setLocation(MSStructTok.getLocation());
1000 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1001 static_cast<uintptr_t>(Kind)));
1002 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1003 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001004}
1005
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001006// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1007// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001008static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001009 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001010 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001011
1012 if (IsOptions) {
1013 PP.Lex(Tok);
1014 if (Tok.isNot(tok::identifier) ||
1015 !Tok.getIdentifierInfo()->isStr("align")) {
1016 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1017 return;
1018 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001019 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001020
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001021 PP.Lex(Tok);
1022 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001023 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1024 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001025 return;
1026 }
1027
1028 PP.Lex(Tok);
1029 if (Tok.isNot(tok::identifier)) {
1030 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001031 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001032 return;
1033 }
1034
John McCallfaf5fb42010-08-26 23:41:50 +00001035 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001036 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001037 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001038 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001039 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001040 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001041 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001042 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001043 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001044 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001045 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001046 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001047 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001048 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001049 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001050 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1051 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001052 return;
1053 }
1054
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001055 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001056 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001057 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001058 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001059 return;
1060 }
1061
Eli Friedman68be1642012-10-04 02:36:51 +00001062 Token *Toks =
1063 (Token*) PP.getPreprocessorAllocator().Allocate(
1064 sizeof(Token) * 1, llvm::alignOf<Token>());
1065 new (Toks) Token();
1066 Toks[0].startToken();
1067 Toks[0].setKind(tok::annot_pragma_align);
1068 Toks[0].setLocation(FirstTok.getLocation());
1069 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1070 static_cast<uintptr_t>(Kind)));
1071 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1072 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001073}
1074
Douglas Gregorc7d65762010-09-09 22:45:38 +00001075void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1076 PragmaIntroducerKind Introducer,
1077 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001078 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001079}
1080
Douglas Gregorc7d65762010-09-09 22:45:38 +00001081void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1082 PragmaIntroducerKind Introducer,
1083 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001084 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001085}
1086
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001087// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001088void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1089 PragmaIntroducerKind Introducer,
1090 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001091 // FIXME: Should we be expanding macros here? My guess is no.
1092 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001093
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001094 // Lex the left '('.
1095 Token Tok;
1096 PP.Lex(Tok);
1097 if (Tok.isNot(tok::l_paren)) {
1098 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1099 return;
1100 }
Mike Stump11289f42009-09-09 15:08:12 +00001101
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001102 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001103 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001104 SourceLocation RParenLoc;
1105 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001106
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001107 while (true) {
1108 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001109
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001110 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001111 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001112 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001113 LexID = false;
1114 continue;
1115 }
1116
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001117 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001118 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1119 return;
1120 }
Mike Stump11289f42009-09-09 15:08:12 +00001121
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001122 // We are execting a ')' or a ','.
1123 if (Tok.is(tok::comma)) {
1124 LexID = true;
1125 continue;
1126 }
Mike Stump11289f42009-09-09 15:08:12 +00001127
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001128 if (Tok.is(tok::r_paren)) {
1129 RParenLoc = Tok.getLocation();
1130 break;
1131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001133 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001134 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001135 return;
1136 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001137
1138 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001139 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001140 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1141 "unused";
1142 return;
1143 }
1144
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001145 // Verify that we have a location for the right parenthesis.
1146 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001147 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001148
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001149 // For each identifier token, insert into the token stream a
1150 // annot_pragma_unused token followed by the identifier token.
1151 // This allows us to cache a "#pragma unused" that occurs inside an inline
1152 // C++ member function.
1153
Daniel Dunbar340cf242012-02-29 01:38:22 +00001154 Token *Toks =
1155 (Token*) PP.getPreprocessorAllocator().Allocate(
1156 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001157 for (unsigned i=0; i != Identifiers.size(); i++) {
1158 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1159 pragmaUnusedTok.startToken();
1160 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1161 pragmaUnusedTok.setLocation(UnusedLoc);
1162 idTok = Identifiers[i];
1163 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001164 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1165 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001166}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001167
1168// #pragma weak identifier
1169// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001170void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1171 PragmaIntroducerKind Introducer,
1172 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001173 SourceLocation WeakLoc = WeakTok.getLocation();
1174
1175 Token Tok;
1176 PP.Lex(Tok);
1177 if (Tok.isNot(tok::identifier)) {
1178 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1179 return;
1180 }
1181
Eli Friedman68be1642012-10-04 02:36:51 +00001182 Token WeakName = Tok;
1183 bool HasAlias = false;
1184 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001185
1186 PP.Lex(Tok);
1187 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001188 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001189 PP.Lex(Tok);
1190 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001191 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001192 << "weak";
1193 return;
1194 }
Eli Friedman68be1642012-10-04 02:36:51 +00001195 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001196 PP.Lex(Tok);
1197 }
1198
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001199 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001200 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1201 return;
1202 }
1203
Eli Friedman68be1642012-10-04 02:36:51 +00001204 if (HasAlias) {
1205 Token *Toks =
1206 (Token*) PP.getPreprocessorAllocator().Allocate(
1207 sizeof(Token) * 3, llvm::alignOf<Token>());
1208 Token &pragmaUnusedTok = Toks[0];
1209 pragmaUnusedTok.startToken();
1210 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1211 pragmaUnusedTok.setLocation(WeakLoc);
1212 Toks[1] = WeakName;
1213 Toks[2] = AliasName;
1214 PP.EnterTokenStream(Toks, 3,
1215 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001216 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001217 Token *Toks =
1218 (Token*) PP.getPreprocessorAllocator().Allocate(
1219 sizeof(Token) * 2, llvm::alignOf<Token>());
1220 Token &pragmaUnusedTok = Toks[0];
1221 pragmaUnusedTok.startToken();
1222 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1223 pragmaUnusedTok.setLocation(WeakLoc);
1224 Toks[1] = WeakName;
1225 PP.EnterTokenStream(Toks, 2,
1226 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001227 }
1228}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001229
David Chisnall0867d9c2012-02-18 16:12:34 +00001230// #pragma redefine_extname identifier identifier
1231void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1232 PragmaIntroducerKind Introducer,
1233 Token &RedefToken) {
1234 SourceLocation RedefLoc = RedefToken.getLocation();
1235
1236 Token Tok;
1237 PP.Lex(Tok);
1238 if (Tok.isNot(tok::identifier)) {
1239 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1240 "redefine_extname";
1241 return;
1242 }
1243
Eli Friedman68be1642012-10-04 02:36:51 +00001244 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001245 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001246
David Chisnall0867d9c2012-02-18 16:12:34 +00001247 if (Tok.isNot(tok::identifier)) {
1248 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1249 << "redefine_extname";
1250 return;
1251 }
Eli Friedman68be1642012-10-04 02:36:51 +00001252
1253 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001254 PP.Lex(Tok);
1255
1256 if (Tok.isNot(tok::eod)) {
1257 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1258 "redefine_extname";
1259 return;
1260 }
1261
Eli Friedman68be1642012-10-04 02:36:51 +00001262 Token *Toks =
1263 (Token*) PP.getPreprocessorAllocator().Allocate(
1264 sizeof(Token) * 3, llvm::alignOf<Token>());
1265 Token &pragmaRedefTok = Toks[0];
1266 pragmaRedefTok.startToken();
1267 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1268 pragmaRedefTok.setLocation(RedefLoc);
1269 Toks[1] = RedefName;
1270 Toks[2] = AliasName;
1271 PP.EnterTokenStream(Toks, 3,
1272 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001273}
1274
1275
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001276void
1277PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1278 PragmaIntroducerKind Introducer,
1279 Token &Tok) {
1280 tok::OnOffSwitch OOS;
1281 if (PP.LexOnOffSwitch(OOS))
1282 return;
1283
Eli Friedman68be1642012-10-04 02:36:51 +00001284 Token *Toks =
1285 (Token*) PP.getPreprocessorAllocator().Allocate(
1286 sizeof(Token) * 1, llvm::alignOf<Token>());
1287 new (Toks) Token();
1288 Toks[0].startToken();
1289 Toks[0].setKind(tok::annot_pragma_fp_contract);
1290 Toks[0].setLocation(Tok.getLocation());
1291 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1292 static_cast<uintptr_t>(OOS)));
1293 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1294 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001295}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001296
1297void
1298PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1299 PragmaIntroducerKind Introducer,
1300 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001301 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001302 if (Tok.isNot(tok::identifier)) {
1303 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1304 "OPENCL";
1305 return;
1306 }
1307 IdentifierInfo *ename = Tok.getIdentifierInfo();
1308 SourceLocation NameLoc = Tok.getLocation();
1309
1310 PP.Lex(Tok);
1311 if (Tok.isNot(tok::colon)) {
1312 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1313 return;
1314 }
1315
1316 PP.Lex(Tok);
1317 if (Tok.isNot(tok::identifier)) {
1318 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1319 return;
1320 }
1321 IdentifierInfo *op = Tok.getIdentifierInfo();
1322
1323 unsigned state;
1324 if (op->isStr("enable")) {
1325 state = 1;
1326 } else if (op->isStr("disable")) {
1327 state = 0;
1328 } else {
1329 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1330 return;
1331 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001332 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001333
Eli Friedman68be1642012-10-04 02:36:51 +00001334 PP.Lex(Tok);
1335 if (Tok.isNot(tok::eod)) {
1336 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1337 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001338 return;
1339 }
Eli Friedman68be1642012-10-04 02:36:51 +00001340
1341 OpenCLExtData data(ename, state);
1342 Token *Toks =
1343 (Token*) PP.getPreprocessorAllocator().Allocate(
1344 sizeof(Token) * 1, llvm::alignOf<Token>());
1345 new (Toks) Token();
1346 Toks[0].startToken();
1347 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1348 Toks[0].setLocation(NameLoc);
1349 Toks[0].setAnnotationValue(data.getOpaqueValue());
1350 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1351 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001352
1353 if (PP.getPPCallbacks())
1354 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1355 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001356}
1357
Alexey Bataeva769e072013-03-22 06:34:35 +00001358/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1359///
1360void
1361PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1362 PragmaIntroducerKind Introducer,
1363 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001364 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1365 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001366 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001367 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1368 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001369 }
1370 PP.DiscardUntilEndOfDirective();
1371}
1372
1373/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1374///
1375void
1376PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1377 PragmaIntroducerKind Introducer,
1378 Token &FirstTok) {
1379 SmallVector<Token, 16> Pragma;
1380 Token Tok;
1381 Tok.startToken();
1382 Tok.setKind(tok::annot_pragma_openmp);
1383 Tok.setLocation(FirstTok.getLocation());
1384
1385 while (Tok.isNot(tok::eod)) {
1386 Pragma.push_back(Tok);
1387 PP.Lex(Tok);
1388 }
1389 SourceLocation EodLoc = Tok.getLocation();
1390 Tok.startToken();
1391 Tok.setKind(tok::annot_pragma_openmp_end);
1392 Tok.setLocation(EodLoc);
1393 Pragma.push_back(Tok);
1394
1395 Token *Toks = new Token[Pragma.size()];
1396 std::copy(Pragma.begin(), Pragma.end(), Toks);
1397 PP.EnterTokenStream(Toks, Pragma.size(),
1398 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1399}
Reid Kleckner002562a2013-05-06 21:02:12 +00001400
David Majnemer4bb09802014-02-10 19:50:15 +00001401/// \brief Handle '#pragma pointers_to_members'
1402// The grammar for this pragma is as follows:
1403//
1404// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1405//
1406// #pragma pointers_to_members '(' 'best_case' ')'
1407// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1408// #pragma pointers_to_members '(' inheritance-model ')'
1409void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1410 PragmaIntroducerKind Introducer,
1411 Token &Tok) {
1412 SourceLocation PointersToMembersLoc = Tok.getLocation();
1413 PP.Lex(Tok);
1414 if (Tok.isNot(tok::l_paren)) {
1415 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1416 << "pointers_to_members";
1417 return;
1418 }
1419 PP.Lex(Tok);
1420 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1421 if (!Arg) {
1422 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1423 << "pointers_to_members";
1424 return;
1425 }
1426 PP.Lex(Tok);
1427
David Majnemer86c318f2014-02-11 21:05:00 +00001428 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001429 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001430 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001431 } else {
1432 if (Arg->isStr("full_generality")) {
1433 if (Tok.is(tok::comma)) {
1434 PP.Lex(Tok);
1435
1436 Arg = Tok.getIdentifierInfo();
1437 if (!Arg) {
1438 PP.Diag(Tok.getLocation(),
1439 diag::err_pragma_pointers_to_members_unknown_kind)
1440 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1441 return;
1442 }
1443 PP.Lex(Tok);
1444 } else if (Tok.is(tok::r_paren)) {
1445 // #pragma pointers_to_members(full_generality) implicitly specifies
1446 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001447 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001448 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001449 } else {
1450 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1451 << "full_generality";
1452 return;
1453 }
1454 }
1455
1456 if (Arg) {
1457 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001458 RepresentationMethod =
1459 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001460 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001461 RepresentationMethod =
1462 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001463 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001464 RepresentationMethod =
1465 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001466 } else {
1467 PP.Diag(Tok.getLocation(),
1468 diag::err_pragma_pointers_to_members_unknown_kind)
1469 << Arg << /*HasPointerDeclaration*/ 1;
1470 return;
1471 }
1472 }
1473 }
1474
1475 if (Tok.isNot(tok::r_paren)) {
1476 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1477 << (Arg ? Arg->getName() : "full_generality");
1478 return;
1479 }
1480
1481 PP.Lex(Tok);
1482 if (Tok.isNot(tok::eod)) {
1483 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1484 << "pointers_to_members";
1485 return;
1486 }
1487
1488 Token AnnotTok;
1489 AnnotTok.startToken();
1490 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1491 AnnotTok.setLocation(PointersToMembersLoc);
1492 AnnotTok.setAnnotationValue(
1493 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1494 PP.EnterToken(AnnotTok);
1495}
1496
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001497/// \brief Handle '#pragma vtordisp'
1498// The grammar for this pragma is as follows:
1499//
1500// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1501//
1502// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1503// #pragma vtordisp '(' 'pop' ')'
1504// #pragma vtordisp '(' ')'
1505void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1506 PragmaIntroducerKind Introducer,
1507 Token &Tok) {
1508 SourceLocation VtorDispLoc = Tok.getLocation();
1509 PP.Lex(Tok);
1510 if (Tok.isNot(tok::l_paren)) {
1511 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1512 return;
1513 }
1514 PP.Lex(Tok);
1515
1516 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1517 const IdentifierInfo *II = Tok.getIdentifierInfo();
1518 if (II) {
1519 if (II->isStr("push")) {
1520 // #pragma vtordisp(push, mode)
1521 PP.Lex(Tok);
1522 if (Tok.isNot(tok::comma)) {
1523 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1524 return;
1525 }
1526 PP.Lex(Tok);
1527 Kind = Sema::PVDK_Push;
1528 // not push, could be on/off
1529 } else if (II->isStr("pop")) {
1530 // #pragma vtordisp(pop)
1531 PP.Lex(Tok);
1532 Kind = Sema::PVDK_Pop;
1533 }
1534 // not push or pop, could be on/off
1535 } else {
1536 if (Tok.is(tok::r_paren)) {
1537 // #pragma vtordisp()
1538 Kind = Sema::PVDK_Reset;
1539 }
1540 }
1541
1542
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001543 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001544 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1545 const IdentifierInfo *II = Tok.getIdentifierInfo();
1546 if (II && II->isStr("off")) {
1547 PP.Lex(Tok);
1548 Value = 0;
1549 } else if (II && II->isStr("on")) {
1550 PP.Lex(Tok);
1551 Value = 1;
1552 } else if (Tok.is(tok::numeric_constant) &&
1553 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1554 if (Value > 2) {
1555 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1556 << 0 << 2 << "vtordisp";
1557 return;
1558 }
1559 } else {
1560 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1561 << "vtordisp";
1562 return;
1563 }
1564 }
1565
1566 // Finish the pragma: ')' $
1567 if (Tok.isNot(tok::r_paren)) {
1568 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1569 return;
1570 }
1571 PP.Lex(Tok);
1572 if (Tok.isNot(tok::eod)) {
1573 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1574 << "vtordisp";
1575 return;
1576 }
1577
1578 // Enter the annotation.
1579 Token AnnotTok;
1580 AnnotTok.startToken();
1581 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1582 AnnotTok.setLocation(VtorDispLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001583 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1584 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001585 PP.EnterToken(AnnotTok);
1586}
1587
Warren Huntc3b18962014-04-08 22:30:47 +00001588/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1589/// an annotation token.
1590void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1591 PragmaIntroducerKind Introducer,
1592 Token &Tok) {
1593 Token EoF, AnnotTok;
1594 EoF.startToken();
1595 EoF.setKind(tok::eof);
1596 AnnotTok.startToken();
1597 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1598 AnnotTok.setLocation(Tok.getLocation());
1599 SmallVector<Token, 8> TokenVector;
1600 // Suck up all of the tokens before the eod.
1601 for (; Tok.isNot(tok::eod); PP.Lex(Tok))
1602 TokenVector.push_back(Tok);
1603 // Add a sentinal EoF token to the end of the list.
1604 TokenVector.push_back(EoF);
1605 // We must allocate this array with new because EnterTokenStream is going to
1606 // delete it later.
1607 Token *TokenArray = new Token[TokenVector.size()];
1608 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1609 auto Value = new (PP.getPreprocessorAllocator())
1610 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1611 AnnotTok.setAnnotationValue(Value);
1612 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001613}
1614
Aaron Ballman5d041be2013-06-04 02:07:14 +00001615/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1616///
1617/// The syntax is:
1618/// \code
1619/// #pragma detect_mismatch("name", "value")
1620/// \endcode
1621/// Where 'name' and 'value' are quoted strings. The values are embedded in
1622/// the object file and passed along to the linker. If the linker detects a
1623/// mismatch in the object file's values for the given name, a LNK2038 error
1624/// is emitted. See MSDN for more details.
1625void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1626 PragmaIntroducerKind Introducer,
1627 Token &Tok) {
1628 SourceLocation CommentLoc = Tok.getLocation();
1629 PP.Lex(Tok);
1630 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001631 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001632 return;
1633 }
1634
1635 // Read the name to embed, which must be a string literal.
1636 std::string NameString;
1637 if (!PP.LexStringLiteral(Tok, NameString,
1638 "pragma detect_mismatch",
1639 /*MacroExpansion=*/true))
1640 return;
1641
1642 // Read the comma followed by a second string literal.
1643 std::string ValueString;
1644 if (Tok.isNot(tok::comma)) {
1645 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1646 return;
1647 }
1648
1649 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1650 /*MacroExpansion=*/true))
1651 return;
1652
1653 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001654 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001655 return;
1656 }
1657 PP.Lex(Tok); // Eat the r_paren.
1658
1659 if (Tok.isNot(tok::eod)) {
1660 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1661 return;
1662 }
1663
Reid Kleckner71966c92014-02-20 23:37:45 +00001664 // If the pragma is lexically sound, notify any interested PPCallbacks.
1665 if (PP.getPPCallbacks())
1666 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1667 ValueString);
1668
Aaron Ballman5d041be2013-06-04 02:07:14 +00001669 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1670}
1671
Reid Kleckner002562a2013-05-06 21:02:12 +00001672/// \brief Handle the microsoft \#pragma comment extension.
1673///
1674/// The syntax is:
1675/// \code
1676/// #pragma comment(linker, "foo")
1677/// \endcode
1678/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1679/// "foo" is a string, which is fully macro expanded, and permits string
1680/// concatenation, embedded escape characters etc. See MSDN for more details.
1681void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1682 PragmaIntroducerKind Introducer,
1683 Token &Tok) {
1684 SourceLocation CommentLoc = Tok.getLocation();
1685 PP.Lex(Tok);
1686 if (Tok.isNot(tok::l_paren)) {
1687 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1688 return;
1689 }
1690
1691 // Read the identifier.
1692 PP.Lex(Tok);
1693 if (Tok.isNot(tok::identifier)) {
1694 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1695 return;
1696 }
1697
1698 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001699 IdentifierInfo *II = Tok.getIdentifierInfo();
1700 Sema::PragmaMSCommentKind Kind =
1701 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1702 .Case("linker", Sema::PCK_Linker)
1703 .Case("lib", Sema::PCK_Lib)
1704 .Case("compiler", Sema::PCK_Compiler)
1705 .Case("exestr", Sema::PCK_ExeStr)
1706 .Case("user", Sema::PCK_User)
1707 .Default(Sema::PCK_Unknown);
1708 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001709 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1710 return;
1711 }
1712
1713 // Read the optional string if present.
1714 PP.Lex(Tok);
1715 std::string ArgumentString;
1716 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1717 "pragma comment",
1718 /*MacroExpansion=*/true))
1719 return;
1720
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001721 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001722 // FIXME: If the kind is "compiler" warn if the string is present (it is
1723 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001724 // The MSDN docs say that "lib" and "linker" require a string and have a short
1725 // whitelist of linker options they support, but in practice MSVC doesn't
1726 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001727
1728 if (Tok.isNot(tok::r_paren)) {
1729 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1730 return;
1731 }
1732 PP.Lex(Tok); // eat the r_paren.
1733
1734 if (Tok.isNot(tok::eod)) {
1735 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1736 return;
1737 }
1738
Reid Kleckner71966c92014-02-20 23:37:45 +00001739 // If the pragma is lexically sound, notify any interested PPCallbacks.
1740 if (PP.getPPCallbacks())
1741 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1742
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001743 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001744}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001745
1746// #pragma clang optimize off
1747// #pragma clang optimize on
1748void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1749 PragmaIntroducerKind Introducer,
1750 Token &FirstToken) {
1751 Token Tok;
1752 PP.Lex(Tok);
1753 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001754 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001755 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001756 return;
1757 }
1758 if (Tok.isNot(tok::identifier)) {
1759 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1760 << PP.getSpelling(Tok);
1761 return;
1762 }
1763 const IdentifierInfo *II = Tok.getIdentifierInfo();
1764 // The only accepted values are 'on' or 'off'.
1765 bool IsOn = false;
1766 if (II->isStr("on")) {
1767 IsOn = true;
1768 } else if (!II->isStr("off")) {
1769 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1770 << PP.getSpelling(Tok);
1771 return;
1772 }
1773 PP.Lex(Tok);
1774
1775 if (Tok.isNot(tok::eod)) {
1776 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1777 << PP.getSpelling(Tok);
1778 return;
1779 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001780
1781 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1782}
1783
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001784/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowickicab7ca32014-07-30 20:54:33 +00001785static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1786 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001787 PragmaLoopHintInfo &Info) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001788 if (ValueInParens) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001789 if (Tok.is(tok::r_paren)) {
1790 // Nothing between the parentheses.
1791 std::string PragmaString;
1792 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1793 PragmaString = "clang loop ";
1794 PragmaString += Option.getIdentifierInfo()->getName();
1795 } else {
1796 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1797 "Unexpected pragma name");
1798 PragmaString = "unroll";
1799 }
Mark Heffernan450c2382014-07-23 17:31:31 +00001800 // Don't try to emit what the pragma is expecting with the diagnostic
1801 // because the logic is non-trivial and we give expected values in sema
1802 // diagnostics if an invalid argument is given. Here, just note that the
1803 // pragma is missing an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001804 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001805 << PragmaString << /*Expected=*/false;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001806 return true;
1807 }
1808 }
1809
1810 // FIXME: Value should be stored and parsed as a constant expression.
1811 Token Value = Tok;
Tyler Nowickicab7ca32014-07-30 20:54:33 +00001812 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001813
1814 if (ValueInParens) {
Tyler Nowickicab7ca32014-07-30 20:54:33 +00001815 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001816 if (Tok.isNot(tok::r_paren)) {
1817 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1818 return true;
1819 }
Tyler Nowickicab7ca32014-07-30 20:54:33 +00001820 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001821 }
1822
1823 Info.PragmaName = PragmaName;
1824 Info.Option = Option;
1825 Info.Value = Value;
1826 Info.HasValue = true;
1827 return false;
1828}
1829
Eli Bendersky06a40422014-06-06 20:31:48 +00001830/// \brief Handle the \#pragma clang loop directive.
1831/// #pragma clang 'loop' loop-hints
1832///
1833/// loop-hints:
1834/// loop-hint loop-hints[opt]
1835///
1836/// loop-hint:
1837/// 'vectorize' '(' loop-hint-keyword ')'
1838/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001839/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001840/// 'vectorize_width' '(' loop-hint-value ')'
1841/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001842/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001843///
1844/// loop-hint-keyword:
1845/// 'enable'
1846/// 'disable'
1847///
Mark Heffernan450c2382014-07-23 17:31:31 +00001848/// unroll-hint-keyword:
1849/// 'full'
1850/// 'disable'
1851///
Eli Bendersky06a40422014-06-06 20:31:48 +00001852/// loop-hint-value:
1853/// constant-expression
1854///
1855/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1856/// try vectorizing the instructions of the loop it precedes. Specifying
1857/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1858/// interleaving multiple iterations of the loop it precedes. The width of the
1859/// vector instructions is specified by vectorize_width() and the number of
1860/// interleaved loop iterations is specified by interleave_count(). Specifying a
1861/// value of 1 effectively disables vectorization/interleaving, even if it is
1862/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1863/// only works on inner loops.
1864///
Eli Bendersky86483b32014-06-11 17:56:26 +00001865/// The unroll and unroll_count directives control the concatenation
Mark Heffernan450c2382014-07-23 17:31:31 +00001866/// unroller. Specifying unroll(full) instructs llvm to try to
Eli Bendersky86483b32014-06-11 17:56:26 +00001867/// unroll the loop completely, and unroll(disable) disables unrolling
1868/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1869/// try to unroll the loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001870void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1871 PragmaIntroducerKind Introducer,
1872 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001873 // Incoming token is "loop" from "#pragma clang loop".
1874 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001875 SmallVector<Token, 1> TokenList;
1876
1877 // Lex the optimization option and verify it is an identifier.
1878 PP.Lex(Tok);
1879 if (Tok.isNot(tok::identifier)) {
1880 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1881 << /*MissingOption=*/true << "";
1882 return;
1883 }
1884
1885 while (Tok.is(tok::identifier)) {
1886 Token Option = Tok;
1887 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1888
Eli Bendersky86483b32014-06-11 17:56:26 +00001889 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001890 .Case("vectorize", true)
1891 .Case("interleave", true)
1892 .Case("unroll", true)
1893 .Case("vectorize_width", true)
1894 .Case("interleave_count", true)
1895 .Case("unroll_count", true)
1896 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00001897 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001898 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1899 << /*MissingOption=*/false << OptionInfo;
1900 return;
1901 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001902 PP.Lex(Tok);
Eli Bendersky06a40422014-06-06 20:31:48 +00001903
Tyler Nowickicab7ca32014-07-30 20:54:33 +00001904 // Read '('
1905 if (Tok.isNot(tok::l_paren)) {
1906 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001907 return;
1908 }
Tyler Nowickicab7ca32014-07-30 20:54:33 +00001909 PP.Lex(Tok);
1910
1911 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1912 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
1913 *Info))
1914 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001915
1916 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00001917 Token LoopHintTok;
1918 LoopHintTok.startToken();
1919 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001920 LoopHintTok.setLocation(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00001921 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
1922 TokenList.push_back(LoopHintTok);
1923 }
1924
1925 if (Tok.isNot(tok::eod)) {
1926 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1927 << "clang loop";
1928 return;
1929 }
1930
1931 Token *TokenArray = new Token[TokenList.size()];
1932 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
1933
1934 PP.EnterTokenStream(TokenArray, TokenList.size(),
1935 /*DisableMacroExpansion=*/false,
1936 /*OwnsTokens=*/true);
1937}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001938
1939/// \brief Handle the loop unroll optimization pragmas.
1940/// #pragma unroll
1941/// #pragma unroll unroll-hint-value
1942/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00001943/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001944///
1945/// unroll-hint-value:
1946/// constant-expression
1947///
Mark Heffernanc888e412014-07-24 18:09:38 +00001948/// Loop unrolling hints can be specified with '#pragma unroll' or
1949/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
1950/// contained in parentheses. With no argument the directive instructs llvm to
1951/// try to unroll the loop completely. A positive integer argument can be
1952/// specified to indicate the number of times the loop should be unrolled. To
1953/// maximize compatibility with other compilers the unroll count argument can be
1954/// specified with or without parentheses. Specifying, '#pragma nounroll'
1955/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001956void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
1957 PragmaIntroducerKind Introducer,
1958 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00001959 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
1960 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001961 Token PragmaName = Tok;
1962 PP.Lex(Tok);
1963 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1964 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00001965 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001966 Info->PragmaName = PragmaName;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001967 Info->HasValue = false;
Mark Heffernanc888e412014-07-24 18:09:38 +00001968 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
1969 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1970 << "nounroll";
1971 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001972 } else {
1973 // Unroll pragma with an argument: "#pragma unroll N" or
1974 // "#pragma unroll(N)".
Tyler Nowickicab7ca32014-07-30 20:54:33 +00001975 // Read '(' if it exists.
1976 bool ValueInParens = Tok.is(tok::l_paren);
1977 if (ValueInParens)
1978 PP.Lex(Tok);
1979
1980 if (ParseLoopHintValue(PP, Tok, PragmaName, Token(), ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001981 return;
1982
1983 // In CUDA, the argument to '#pragma unroll' should not be contained in
1984 // parentheses.
1985 if (PP.getLangOpts().CUDA && ValueInParens)
1986 PP.Diag(Info->Value.getLocation(),
1987 diag::warn_pragma_unroll_cuda_value_in_parens);
1988
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001989 if (Tok.isNot(tok::eod)) {
1990 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1991 << "unroll";
1992 return;
1993 }
1994 }
1995
1996 // Generate the hint token.
1997 Token *TokenArray = new Token[1];
1998 TokenArray[0].startToken();
1999 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2000 TokenArray[0].setLocation(PragmaName.getLocation());
2001 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2002 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2003 /*OwnsTokens=*/true);
2004}