blob: a7b5f6ebddb3b958927ef58dc80f7273af833e59 [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());
Eli Bendersky06a40422014-06-06 20:31:48 +0000230}
231
232void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000233 // Remove the pragma handlers we installed.
234 PP.RemovePragmaHandler(AlignHandler.get());
235 AlignHandler.reset();
236 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
237 GCCVisibilityHandler.reset();
238 PP.RemovePragmaHandler(OptionsHandler.get());
239 OptionsHandler.reset();
240 PP.RemovePragmaHandler(PackHandler.get());
241 PackHandler.reset();
242 PP.RemovePragmaHandler(MSStructHandler.get());
243 MSStructHandler.reset();
244 PP.RemovePragmaHandler(UnusedHandler.get());
245 UnusedHandler.reset();
246 PP.RemovePragmaHandler(WeakHandler.get());
247 WeakHandler.reset();
248 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
249 RedefineExtnameHandler.reset();
250
251 if (getLangOpts().OpenCL) {
252 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
253 OpenCLExtensionHandler.reset();
254 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
255 }
256 PP.RemovePragmaHandler(OpenMPHandler.get());
257 OpenMPHandler.reset();
258
259 if (getLangOpts().MicrosoftExt) {
260 PP.RemovePragmaHandler(MSCommentHandler.get());
261 MSCommentHandler.reset();
262 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
263 MSDetectMismatchHandler.reset();
264 PP.RemovePragmaHandler(MSPointersToMembers.get());
265 MSPointersToMembers.reset();
266 PP.RemovePragmaHandler(MSVtorDisp.get());
267 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000268 PP.RemovePragmaHandler(MSInitSeg.get());
269 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000270 PP.RemovePragmaHandler(MSDataSeg.get());
271 MSDataSeg.reset();
272 PP.RemovePragmaHandler(MSBSSSeg.get());
273 MSBSSSeg.reset();
274 PP.RemovePragmaHandler(MSConstSeg.get());
275 MSConstSeg.reset();
276 PP.RemovePragmaHandler(MSCodeSeg.get());
277 MSCodeSeg.reset();
278 PP.RemovePragmaHandler(MSSection.get());
279 MSSection.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000280 }
281
282 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
283 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000284
285 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
286 OptimizeHandler.reset();
287
288 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
289 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000290
291 PP.RemovePragmaHandler(UnrollHintHandler.get());
292 UnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000293}
294
295/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000296///
297/// Each annot_pragma_unused is followed by the argument token so e.g.
298/// "#pragma unused(x,y)" becomes:
299/// annot_pragma_unused 'x' annot_pragma_unused 'y'
300void Parser::HandlePragmaUnused() {
301 assert(Tok.is(tok::annot_pragma_unused));
302 SourceLocation UnusedLoc = ConsumeToken();
303 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
304 ConsumeToken(); // The argument token.
305}
Eli Friedman570024a2010-08-05 06:57:20 +0000306
Rafael Espindola273fd772012-01-26 02:02:57 +0000307void Parser::HandlePragmaVisibility() {
308 assert(Tok.is(tok::annot_pragma_vis));
309 const IdentifierInfo *VisType =
310 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
311 SourceLocation VisLoc = ConsumeToken();
312 Actions.ActOnPragmaVisibility(VisType, VisLoc);
313}
314
Eli Friedmanec52f922012-02-23 23:47:16 +0000315struct PragmaPackInfo {
316 Sema::PragmaPackKind Kind;
317 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000318 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000319 SourceLocation LParenLoc;
320 SourceLocation RParenLoc;
321};
322
323void Parser::HandlePragmaPack() {
324 assert(Tok.is(tok::annot_pragma_pack));
325 PragmaPackInfo *Info =
326 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
327 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000328 ExprResult Alignment;
329 if (Info->Alignment.is(tok::numeric_constant)) {
330 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
331 if (Alignment.isInvalid())
332 return;
333 }
334 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000335 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000336}
337
Eli Friedman68be1642012-10-04 02:36:51 +0000338void Parser::HandlePragmaMSStruct() {
339 assert(Tok.is(tok::annot_pragma_msstruct));
340 Sema::PragmaMSStructKind Kind =
341 static_cast<Sema::PragmaMSStructKind>(
342 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
343 Actions.ActOnPragmaMSStruct(Kind);
344 ConsumeToken(); // The annotation token.
345}
346
347void Parser::HandlePragmaAlign() {
348 assert(Tok.is(tok::annot_pragma_align));
349 Sema::PragmaOptionsAlignKind Kind =
350 static_cast<Sema::PragmaOptionsAlignKind>(
351 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
352 SourceLocation PragmaLoc = ConsumeToken();
353 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
354}
355
356void Parser::HandlePragmaWeak() {
357 assert(Tok.is(tok::annot_pragma_weak));
358 SourceLocation PragmaLoc = ConsumeToken();
359 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
360 Tok.getLocation());
361 ConsumeToken(); // The weak name.
362}
363
364void Parser::HandlePragmaWeakAlias() {
365 assert(Tok.is(tok::annot_pragma_weakalias));
366 SourceLocation PragmaLoc = ConsumeToken();
367 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
368 SourceLocation WeakNameLoc = Tok.getLocation();
369 ConsumeToken();
370 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
371 SourceLocation AliasNameLoc = Tok.getLocation();
372 ConsumeToken();
373 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
374 WeakNameLoc, AliasNameLoc);
375
376}
377
378void Parser::HandlePragmaRedefineExtname() {
379 assert(Tok.is(tok::annot_pragma_redefine_extname));
380 SourceLocation RedefLoc = ConsumeToken();
381 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
382 SourceLocation RedefNameLoc = Tok.getLocation();
383 ConsumeToken();
384 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
385 SourceLocation AliasNameLoc = Tok.getLocation();
386 ConsumeToken();
387 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
388 RedefNameLoc, AliasNameLoc);
389}
390
391void Parser::HandlePragmaFPContract() {
392 assert(Tok.is(tok::annot_pragma_fp_contract));
393 tok::OnOffSwitch OOS =
394 static_cast<tok::OnOffSwitch>(
395 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
396 Actions.ActOnPragmaFPContract(OOS);
397 ConsumeToken(); // The annotation token.
398}
399
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000400StmtResult Parser::HandlePragmaCaptured()
401{
402 assert(Tok.is(tok::annot_pragma_captured));
403 ConsumeToken();
404
405 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000406 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000407 return StmtError();
408 }
409
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000410 SourceLocation Loc = Tok.getLocation();
411
412 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000413 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
414 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000415
416 StmtResult R = ParseCompoundStatement();
417 CapturedRegionScope.Exit();
418
419 if (R.isInvalid()) {
420 Actions.ActOnCapturedRegionError();
421 return StmtError();
422 }
423
424 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000425}
426
Eli Friedman68be1642012-10-04 02:36:51 +0000427namespace {
428 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
429}
430
431void Parser::HandlePragmaOpenCLExtension() {
432 assert(Tok.is(tok::annot_pragma_opencl_extension));
433 OpenCLExtData data =
434 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
435 unsigned state = data.getInt();
436 IdentifierInfo *ename = data.getPointer();
437 SourceLocation NameLoc = Tok.getLocation();
438 ConsumeToken(); // The annotation token.
439
440 OpenCLOptions &f = Actions.getOpenCLOptions();
441 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
442 // overriding all previously issued extension directives, but only if the
443 // behavior is set to disable."
444 if (state == 0 && ename->isStr("all")) {
445#define OPENCLEXT(nm) f.nm = 0;
446#include "clang/Basic/OpenCLExtensions.def"
447 }
448#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
449#include "clang/Basic/OpenCLExtensions.def"
450 else {
451 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
452 return;
453 }
454}
455
David Majnemer4bb09802014-02-10 19:50:15 +0000456void Parser::HandlePragmaMSPointersToMembers() {
457 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000458 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
459 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000460 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
461 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
462 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
463}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000464
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000465void Parser::HandlePragmaMSVtorDisp() {
466 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
467 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
468 Sema::PragmaVtorDispKind Kind =
469 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
470 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
471 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
472 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
473}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000474
Warren Huntc3b18962014-04-08 22:30:47 +0000475void Parser::HandlePragmaMSPragma() {
476 assert(Tok.is(tok::annot_pragma_ms_pragma));
477 // Grab the tokens out of the annotation and enter them into the stream.
478 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
479 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
480 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
481 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000482 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000483 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000484
Warren Huntc3b18962014-04-08 22:30:47 +0000485 // Figure out which #pragma we're dealing with. The switch has no default
486 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000487 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000488 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
489 .Case("data_seg", &Parser::HandlePragmaMSSegment)
490 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
491 .Case("const_seg", &Parser::HandlePragmaMSSegment)
492 .Case("code_seg", &Parser::HandlePragmaMSSegment)
493 .Case("section", &Parser::HandlePragmaMSSection)
494 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000495
496 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
497 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
498 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000499 while (Tok.isNot(tok::eof))
500 PP.Lex(Tok);
501 PP.Lex(Tok);
502 }
503}
504
Reid Kleckner722b1df2014-07-18 00:13:16 +0000505bool Parser::HandlePragmaMSSection(StringRef PragmaName,
506 SourceLocation PragmaLocation) {
507 if (Tok.isNot(tok::l_paren)) {
508 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
509 return false;
510 }
Warren Huntc3b18962014-04-08 22:30:47 +0000511 PP.Lex(Tok); // (
512 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000513 if (Tok.isNot(tok::string_literal)) {
514 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
515 << PragmaName;
516 return false;
517 }
518 ExprResult StringResult = ParseStringLiteralExpression();
519 if (StringResult.isInvalid())
520 return false; // Already diagnosed.
521 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
522 if (SegmentName->getCharByteWidth() != 1) {
523 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
524 << PragmaName;
525 return false;
526 }
Warren Huntc3b18962014-04-08 22:30:47 +0000527 int SectionFlags = 0;
528 while (Tok.is(tok::comma)) {
529 PP.Lex(Tok); // ,
Reid Kleckner722b1df2014-07-18 00:13:16 +0000530 if (!Tok.isAnyIdentifier()) {
531 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
532 << PragmaName;
533 return false;
534 }
Warren Huntc3b18962014-04-08 22:30:47 +0000535 Sema::PragmaSectionFlag Flag =
536 llvm::StringSwitch<Sema::PragmaSectionFlag>(
537 Tok.getIdentifierInfo()->getName())
538 .Case("read", Sema::PSF_Read)
539 .Case("write", Sema::PSF_Write)
540 .Case("execute", Sema::PSF_Execute)
541 .Case("shared", Sema::PSF_Invalid)
542 .Case("nopage", Sema::PSF_Invalid)
543 .Case("nocache", Sema::PSF_Invalid)
544 .Case("discard", Sema::PSF_Invalid)
545 .Case("remove", Sema::PSF_Invalid)
546 .Default(Sema::PSF_None);
547 if (Flag == Sema::PSF_None || Flag == Sema::PSF_Invalid) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000548 PP.Diag(PragmaLocation, Flag == Sema::PSF_None
549 ? diag::warn_pragma_invalid_specific_action
550 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000551 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000552 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000553 }
554 SectionFlags |= Flag;
555 PP.Lex(Tok); // Identifier
556 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000557 if (Tok.isNot(tok::r_paren)) {
558 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
559 return false;
560 }
Warren Huntc3b18962014-04-08 22:30:47 +0000561 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000562 if (Tok.isNot(tok::eof)) {
563 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
564 << PragmaName;
565 return false;
566 }
Warren Huntc3b18962014-04-08 22:30:47 +0000567 PP.Lex(Tok); // eof
568 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000569 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000570}
571
Reid Kleckner722b1df2014-07-18 00:13:16 +0000572bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
573 SourceLocation PragmaLocation) {
574 if (Tok.isNot(tok::l_paren)) {
575 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
576 return false;
577 }
Warren Huntc3b18962014-04-08 22:30:47 +0000578 PP.Lex(Tok); // (
579 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000580 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000581 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000582 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000583 if (PushPop == "push")
584 Action = Sema::PSK_Push;
585 else if (PushPop == "pop")
586 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000587 else {
588 PP.Diag(PragmaLocation,
589 diag::warn_pragma_expected_section_push_pop_or_name)
590 << PragmaName;
591 return false;
592 }
Warren Huntc3b18962014-04-08 22:30:47 +0000593 if (Action != Sema::PSK_Reset) {
594 PP.Lex(Tok); // push | pop
595 if (Tok.is(tok::comma)) {
596 PP.Lex(Tok); // ,
597 // If we've got a comma, we either need a label or a string.
598 if (Tok.isAnyIdentifier()) {
599 SlotLabel = Tok.getIdentifierInfo()->getName();
600 PP.Lex(Tok); // identifier
601 if (Tok.is(tok::comma))
602 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000603 else if (Tok.isNot(tok::r_paren)) {
604 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
605 << PragmaName;
606 return false;
607 }
Warren Huntc3b18962014-04-08 22:30:47 +0000608 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000609 } else if (Tok.isNot(tok::r_paren)) {
610 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
611 return false;
612 }
Warren Huntc3b18962014-04-08 22:30:47 +0000613 }
614 }
615 // Grab the string literal for our section name.
616 StringLiteral *SegmentName = nullptr;
617 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000618 if (Tok.isNot(tok::string_literal)) {
619 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000620 diag::warn_pragma_expected_section_name :
621 diag::warn_pragma_expected_section_label_or_name :
622 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000623 PP.Diag(PragmaLocation, DiagID) << PragmaName;
624 return false;
625 }
626 ExprResult StringResult = ParseStringLiteralExpression();
627 if (StringResult.isInvalid())
628 return false; // Already diagnosed.
629 SegmentName = cast<StringLiteral>(StringResult.get());
630 if (SegmentName->getCharByteWidth() != 1) {
631 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
632 << PragmaName;
633 return false;
634 }
Warren Huntc3b18962014-04-08 22:30:47 +0000635 // Setting section "" has no effect
636 if (SegmentName->getLength())
637 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
638 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000639 if (Tok.isNot(tok::r_paren)) {
640 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
641 return false;
642 }
Warren Huntc3b18962014-04-08 22:30:47 +0000643 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000644 if (Tok.isNot(tok::eof)) {
645 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
646 << PragmaName;
647 return false;
648 }
Warren Huntc3b18962014-04-08 22:30:47 +0000649 PP.Lex(Tok); // eof
650 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
651 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000652 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000653}
654
Reid Kleckner722b1df2014-07-18 00:13:16 +0000655bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
656 SourceLocation PragmaLocation) {
657 PP.Diag(PragmaLocation,
658 PP.getDiagnostics().getCustomDiagID(
659 DiagnosticsEngine::Error, "'#pragma init_seg' not implemented."));
660 return false;
Eli Bendersky06a40422014-06-06 20:31:48 +0000661}
662
663struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000664 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000665 Token Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000666 Token Value;
667 bool HasValue;
Eli Bendersky06a40422014-06-06 20:31:48 +0000668};
669
670LoopHint Parser::HandlePragmaLoopHint() {
671 assert(Tok.is(tok::annot_pragma_loop_hint));
672 PragmaLoopHintInfo *Info =
673 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
674
675 LoopHint Hint;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000676 Hint.PragmaNameLoc =
677 IdentifierLoc::create(Actions.Context, Info->PragmaName.getLocation(),
678 Info->PragmaName.getIdentifierInfo());
Eli Bendersky06a40422014-06-06 20:31:48 +0000679 Hint.OptionLoc =
680 IdentifierLoc::create(Actions.Context, Info->Option.getLocation(),
681 Info->Option.getIdentifierInfo());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000682 if (Info->HasValue) {
683 Hint.Range =
684 SourceRange(Info->Option.getLocation(), Info->Value.getLocation());
685 Hint.ValueLoc =
686 IdentifierLoc::create(Actions.Context, Info->Value.getLocation(),
687 Info->Value.getIdentifierInfo());
Eli Bendersky06a40422014-06-06 20:31:48 +0000688
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000689 // FIXME: We should allow non-type template parameters for the loop hint
690 // value. See bug report #19610
691 if (Info->Value.is(tok::numeric_constant))
692 Hint.ValueExpr = Actions.ActOnNumericConstant(Info->Value).get();
693 else
694 Hint.ValueExpr = nullptr;
695 } else {
696 Hint.Range = SourceRange(Info->PragmaName.getLocation());
697 Hint.ValueLoc = nullptr;
Eli Bendersky06a40422014-06-06 20:31:48 +0000698 Hint.ValueExpr = nullptr;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000699 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000700
701 return Hint;
702}
703
704// #pragma GCC visibility comes in two variants:
705// 'push' '(' [visibility] ')'
706// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000707void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
708 PragmaIntroducerKind Introducer,
709 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000710 SourceLocation VisLoc = VisTok.getLocation();
711
712 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000713 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000714
715 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
716
Eli Friedman570024a2010-08-05 06:57:20 +0000717 const IdentifierInfo *VisType;
718 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000719 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000720 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000721 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000722 if (Tok.isNot(tok::l_paren)) {
723 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
724 << "visibility";
725 return;
726 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000727 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000728 VisType = Tok.getIdentifierInfo();
729 if (!VisType) {
730 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
731 << "visibility";
732 return;
733 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000734 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000735 if (Tok.isNot(tok::r_paren)) {
736 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
737 << "visibility";
738 return;
739 }
740 } else {
741 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
742 << "visibility";
743 return;
744 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000745 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000746 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000747 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
748 << "visibility";
749 return;
750 }
751
Rafael Espindola273fd772012-01-26 02:02:57 +0000752 Token *Toks = new Token[1];
753 Toks[0].startToken();
754 Toks[0].setKind(tok::annot_pragma_vis);
755 Toks[0].setLocation(VisLoc);
756 Toks[0].setAnnotationValue(
757 const_cast<void*>(static_cast<const void*>(VisType)));
758 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
759 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000760}
761
Daniel Dunbar921b9682008-10-04 19:21:03 +0000762// #pragma pack(...) comes in the following delicious flavors:
763// pack '(' [integer] ')'
764// pack '(' 'show' ')'
765// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000766void PragmaPackHandler::HandlePragma(Preprocessor &PP,
767 PragmaIntroducerKind Introducer,
768 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000769 SourceLocation PackLoc = PackTok.getLocation();
770
771 Token Tok;
772 PP.Lex(Tok);
773 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000774 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000775 return;
776 }
777
John McCallfaf5fb42010-08-26 23:41:50 +0000778 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000779 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000780 Token Alignment;
781 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000782 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000783 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000784 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000785 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000786
787 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000788
789 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
790 // the push/pop stack.
791 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000792 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000793 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000794 } else if (Tok.is(tok::identifier)) {
795 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000796 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000797 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000798 PP.Lex(Tok);
799 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000800 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000801 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000802 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000803 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000804 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000805 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000806 return;
Mike Stump11289f42009-09-09 15:08:12 +0000807 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000808 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000809
Daniel Dunbar921b9682008-10-04 19:21:03 +0000810 if (Tok.is(tok::comma)) {
811 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000812
Daniel Dunbar921b9682008-10-04 19:21:03 +0000813 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000814 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000815
816 PP.Lex(Tok);
817 } else if (Tok.is(tok::identifier)) {
818 Name = Tok.getIdentifierInfo();
819 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000820
Daniel Dunbar921b9682008-10-04 19:21:03 +0000821 if (Tok.is(tok::comma)) {
822 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000823
Daniel Dunbar921b9682008-10-04 19:21:03 +0000824 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000825 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000826 return;
827 }
Mike Stump11289f42009-09-09 15:08:12 +0000828
Eli Friedman68be1642012-10-04 02:36:51 +0000829 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000830
831 PP.Lex(Tok);
832 }
833 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000834 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000835 return;
836 }
837 }
838 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000839 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +0000840 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
841 // the push/pop stack.
842 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
843 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000844 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000845
846 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000847 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000848 return;
849 }
850
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000851 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000852 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000853 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000854 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
855 return;
856 }
857
Daniel Dunbar340cf242012-02-29 01:38:22 +0000858 PragmaPackInfo *Info =
859 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
860 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
861 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +0000862 Info->Kind = Kind;
863 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000864 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000865 Info->LParenLoc = LParenLoc;
866 Info->RParenLoc = RParenLoc;
867
Daniel Dunbar340cf242012-02-29 01:38:22 +0000868 Token *Toks =
869 (Token*) PP.getPreprocessorAllocator().Allocate(
870 sizeof(Token) * 1, llvm::alignOf<Token>());
871 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +0000872 Toks[0].startToken();
873 Toks[0].setKind(tok::annot_pragma_pack);
874 Toks[0].setLocation(PackLoc);
875 Toks[0].setAnnotationValue(static_cast<void*>(Info));
876 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +0000877 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000878}
879
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000880// #pragma ms_struct on
881// #pragma ms_struct off
882void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
883 PragmaIntroducerKind Introducer,
884 Token &MSStructTok) {
885 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
886
887 Token Tok;
888 PP.Lex(Tok);
889 if (Tok.isNot(tok::identifier)) {
890 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
891 return;
892 }
893 const IdentifierInfo *II = Tok.getIdentifierInfo();
894 if (II->isStr("on")) {
895 Kind = Sema::PMSST_ON;
896 PP.Lex(Tok);
897 }
898 else if (II->isStr("off") || II->isStr("reset"))
899 PP.Lex(Tok);
900 else {
901 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
902 return;
903 }
904
905 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +0000906 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
907 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000908 return;
909 }
Eli Friedman68be1642012-10-04 02:36:51 +0000910
911 Token *Toks =
912 (Token*) PP.getPreprocessorAllocator().Allocate(
913 sizeof(Token) * 1, llvm::alignOf<Token>());
914 new (Toks) Token();
915 Toks[0].startToken();
916 Toks[0].setKind(tok::annot_pragma_msstruct);
917 Toks[0].setLocation(MSStructTok.getLocation());
918 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
919 static_cast<uintptr_t>(Kind)));
920 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
921 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000922}
923
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000924// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
925// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +0000926static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000927 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000928 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000929
930 if (IsOptions) {
931 PP.Lex(Tok);
932 if (Tok.isNot(tok::identifier) ||
933 !Tok.getIdentifierInfo()->isStr("align")) {
934 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
935 return;
936 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000937 }
Daniel Dunbar663e8092010-05-27 18:42:09 +0000938
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000939 PP.Lex(Tok);
940 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000941 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
942 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000943 return;
944 }
945
946 PP.Lex(Tok);
947 if (Tok.isNot(tok::identifier)) {
948 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000949 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000950 return;
951 }
952
John McCallfaf5fb42010-08-26 23:41:50 +0000953 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000954 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +0000955 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +0000956 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +0000957 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +0000958 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +0000959 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +0000960 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000961 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +0000962 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000963 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +0000964 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000965 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +0000966 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000967 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000968 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
969 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000970 return;
971 }
972
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000973 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000974 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000975 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000976 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000977 return;
978 }
979
Eli Friedman68be1642012-10-04 02:36:51 +0000980 Token *Toks =
981 (Token*) PP.getPreprocessorAllocator().Allocate(
982 sizeof(Token) * 1, llvm::alignOf<Token>());
983 new (Toks) Token();
984 Toks[0].startToken();
985 Toks[0].setKind(tok::annot_pragma_align);
986 Toks[0].setLocation(FirstTok.getLocation());
987 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
988 static_cast<uintptr_t>(Kind)));
989 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
990 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000991}
992
Douglas Gregorc7d65762010-09-09 22:45:38 +0000993void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
994 PragmaIntroducerKind Introducer,
995 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +0000996 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000997}
998
Douglas Gregorc7d65762010-09-09 22:45:38 +0000999void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1000 PragmaIntroducerKind Introducer,
1001 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001002 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001003}
1004
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001005// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001006void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1007 PragmaIntroducerKind Introducer,
1008 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001009 // FIXME: Should we be expanding macros here? My guess is no.
1010 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001011
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001012 // Lex the left '('.
1013 Token Tok;
1014 PP.Lex(Tok);
1015 if (Tok.isNot(tok::l_paren)) {
1016 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1017 return;
1018 }
Mike Stump11289f42009-09-09 15:08:12 +00001019
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001020 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001021 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001022 SourceLocation RParenLoc;
1023 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001024
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001025 while (true) {
1026 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001027
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001028 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001029 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001030 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001031 LexID = false;
1032 continue;
1033 }
1034
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001035 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001036 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1037 return;
1038 }
Mike Stump11289f42009-09-09 15:08:12 +00001039
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001040 // We are execting a ')' or a ','.
1041 if (Tok.is(tok::comma)) {
1042 LexID = true;
1043 continue;
1044 }
Mike Stump11289f42009-09-09 15:08:12 +00001045
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001046 if (Tok.is(tok::r_paren)) {
1047 RParenLoc = Tok.getLocation();
1048 break;
1049 }
Mike Stump11289f42009-09-09 15:08:12 +00001050
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001051 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001052 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001053 return;
1054 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001055
1056 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001057 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001058 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1059 "unused";
1060 return;
1061 }
1062
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001063 // Verify that we have a location for the right parenthesis.
1064 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001065 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001066
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001067 // For each identifier token, insert into the token stream a
1068 // annot_pragma_unused token followed by the identifier token.
1069 // This allows us to cache a "#pragma unused" that occurs inside an inline
1070 // C++ member function.
1071
Daniel Dunbar340cf242012-02-29 01:38:22 +00001072 Token *Toks =
1073 (Token*) PP.getPreprocessorAllocator().Allocate(
1074 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001075 for (unsigned i=0; i != Identifiers.size(); i++) {
1076 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1077 pragmaUnusedTok.startToken();
1078 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1079 pragmaUnusedTok.setLocation(UnusedLoc);
1080 idTok = Identifiers[i];
1081 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001082 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1083 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001084}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001085
1086// #pragma weak identifier
1087// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001088void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1089 PragmaIntroducerKind Introducer,
1090 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001091 SourceLocation WeakLoc = WeakTok.getLocation();
1092
1093 Token Tok;
1094 PP.Lex(Tok);
1095 if (Tok.isNot(tok::identifier)) {
1096 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1097 return;
1098 }
1099
Eli Friedman68be1642012-10-04 02:36:51 +00001100 Token WeakName = Tok;
1101 bool HasAlias = false;
1102 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001103
1104 PP.Lex(Tok);
1105 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001106 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001107 PP.Lex(Tok);
1108 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001109 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001110 << "weak";
1111 return;
1112 }
Eli Friedman68be1642012-10-04 02:36:51 +00001113 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001114 PP.Lex(Tok);
1115 }
1116
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001117 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001118 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1119 return;
1120 }
1121
Eli Friedman68be1642012-10-04 02:36:51 +00001122 if (HasAlias) {
1123 Token *Toks =
1124 (Token*) PP.getPreprocessorAllocator().Allocate(
1125 sizeof(Token) * 3, llvm::alignOf<Token>());
1126 Token &pragmaUnusedTok = Toks[0];
1127 pragmaUnusedTok.startToken();
1128 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1129 pragmaUnusedTok.setLocation(WeakLoc);
1130 Toks[1] = WeakName;
1131 Toks[2] = AliasName;
1132 PP.EnterTokenStream(Toks, 3,
1133 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001134 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001135 Token *Toks =
1136 (Token*) PP.getPreprocessorAllocator().Allocate(
1137 sizeof(Token) * 2, llvm::alignOf<Token>());
1138 Token &pragmaUnusedTok = Toks[0];
1139 pragmaUnusedTok.startToken();
1140 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1141 pragmaUnusedTok.setLocation(WeakLoc);
1142 Toks[1] = WeakName;
1143 PP.EnterTokenStream(Toks, 2,
1144 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001145 }
1146}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001147
David Chisnall0867d9c2012-02-18 16:12:34 +00001148// #pragma redefine_extname identifier identifier
1149void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1150 PragmaIntroducerKind Introducer,
1151 Token &RedefToken) {
1152 SourceLocation RedefLoc = RedefToken.getLocation();
1153
1154 Token Tok;
1155 PP.Lex(Tok);
1156 if (Tok.isNot(tok::identifier)) {
1157 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1158 "redefine_extname";
1159 return;
1160 }
1161
Eli Friedman68be1642012-10-04 02:36:51 +00001162 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001163 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001164
David Chisnall0867d9c2012-02-18 16:12:34 +00001165 if (Tok.isNot(tok::identifier)) {
1166 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1167 << "redefine_extname";
1168 return;
1169 }
Eli Friedman68be1642012-10-04 02:36:51 +00001170
1171 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001172 PP.Lex(Tok);
1173
1174 if (Tok.isNot(tok::eod)) {
1175 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1176 "redefine_extname";
1177 return;
1178 }
1179
Eli Friedman68be1642012-10-04 02:36:51 +00001180 Token *Toks =
1181 (Token*) PP.getPreprocessorAllocator().Allocate(
1182 sizeof(Token) * 3, llvm::alignOf<Token>());
1183 Token &pragmaRedefTok = Toks[0];
1184 pragmaRedefTok.startToken();
1185 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1186 pragmaRedefTok.setLocation(RedefLoc);
1187 Toks[1] = RedefName;
1188 Toks[2] = AliasName;
1189 PP.EnterTokenStream(Toks, 3,
1190 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001191}
1192
1193
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001194void
1195PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1196 PragmaIntroducerKind Introducer,
1197 Token &Tok) {
1198 tok::OnOffSwitch OOS;
1199 if (PP.LexOnOffSwitch(OOS))
1200 return;
1201
Eli Friedman68be1642012-10-04 02:36:51 +00001202 Token *Toks =
1203 (Token*) PP.getPreprocessorAllocator().Allocate(
1204 sizeof(Token) * 1, llvm::alignOf<Token>());
1205 new (Toks) Token();
1206 Toks[0].startToken();
1207 Toks[0].setKind(tok::annot_pragma_fp_contract);
1208 Toks[0].setLocation(Tok.getLocation());
1209 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1210 static_cast<uintptr_t>(OOS)));
1211 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1212 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001213}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001214
1215void
1216PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1217 PragmaIntroducerKind Introducer,
1218 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001219 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001220 if (Tok.isNot(tok::identifier)) {
1221 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1222 "OPENCL";
1223 return;
1224 }
1225 IdentifierInfo *ename = Tok.getIdentifierInfo();
1226 SourceLocation NameLoc = Tok.getLocation();
1227
1228 PP.Lex(Tok);
1229 if (Tok.isNot(tok::colon)) {
1230 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1231 return;
1232 }
1233
1234 PP.Lex(Tok);
1235 if (Tok.isNot(tok::identifier)) {
1236 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1237 return;
1238 }
1239 IdentifierInfo *op = Tok.getIdentifierInfo();
1240
1241 unsigned state;
1242 if (op->isStr("enable")) {
1243 state = 1;
1244 } else if (op->isStr("disable")) {
1245 state = 0;
1246 } else {
1247 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1248 return;
1249 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001250 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001251
Eli Friedman68be1642012-10-04 02:36:51 +00001252 PP.Lex(Tok);
1253 if (Tok.isNot(tok::eod)) {
1254 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1255 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001256 return;
1257 }
Eli Friedman68be1642012-10-04 02:36:51 +00001258
1259 OpenCLExtData data(ename, state);
1260 Token *Toks =
1261 (Token*) PP.getPreprocessorAllocator().Allocate(
1262 sizeof(Token) * 1, llvm::alignOf<Token>());
1263 new (Toks) Token();
1264 Toks[0].startToken();
1265 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1266 Toks[0].setLocation(NameLoc);
1267 Toks[0].setAnnotationValue(data.getOpaqueValue());
1268 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1269 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001270
1271 if (PP.getPPCallbacks())
1272 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1273 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001274}
1275
Alexey Bataeva769e072013-03-22 06:34:35 +00001276/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1277///
1278void
1279PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1280 PragmaIntroducerKind Introducer,
1281 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001282 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1283 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001284 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001285 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1286 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001287 }
1288 PP.DiscardUntilEndOfDirective();
1289}
1290
1291/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1292///
1293void
1294PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1295 PragmaIntroducerKind Introducer,
1296 Token &FirstTok) {
1297 SmallVector<Token, 16> Pragma;
1298 Token Tok;
1299 Tok.startToken();
1300 Tok.setKind(tok::annot_pragma_openmp);
1301 Tok.setLocation(FirstTok.getLocation());
1302
1303 while (Tok.isNot(tok::eod)) {
1304 Pragma.push_back(Tok);
1305 PP.Lex(Tok);
1306 }
1307 SourceLocation EodLoc = Tok.getLocation();
1308 Tok.startToken();
1309 Tok.setKind(tok::annot_pragma_openmp_end);
1310 Tok.setLocation(EodLoc);
1311 Pragma.push_back(Tok);
1312
1313 Token *Toks = new Token[Pragma.size()];
1314 std::copy(Pragma.begin(), Pragma.end(), Toks);
1315 PP.EnterTokenStream(Toks, Pragma.size(),
1316 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1317}
Reid Kleckner002562a2013-05-06 21:02:12 +00001318
David Majnemer4bb09802014-02-10 19:50:15 +00001319/// \brief Handle '#pragma pointers_to_members'
1320// The grammar for this pragma is as follows:
1321//
1322// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1323//
1324// #pragma pointers_to_members '(' 'best_case' ')'
1325// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1326// #pragma pointers_to_members '(' inheritance-model ')'
1327void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1328 PragmaIntroducerKind Introducer,
1329 Token &Tok) {
1330 SourceLocation PointersToMembersLoc = Tok.getLocation();
1331 PP.Lex(Tok);
1332 if (Tok.isNot(tok::l_paren)) {
1333 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1334 << "pointers_to_members";
1335 return;
1336 }
1337 PP.Lex(Tok);
1338 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1339 if (!Arg) {
1340 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1341 << "pointers_to_members";
1342 return;
1343 }
1344 PP.Lex(Tok);
1345
David Majnemer86c318f2014-02-11 21:05:00 +00001346 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001347 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001348 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001349 } else {
1350 if (Arg->isStr("full_generality")) {
1351 if (Tok.is(tok::comma)) {
1352 PP.Lex(Tok);
1353
1354 Arg = Tok.getIdentifierInfo();
1355 if (!Arg) {
1356 PP.Diag(Tok.getLocation(),
1357 diag::err_pragma_pointers_to_members_unknown_kind)
1358 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1359 return;
1360 }
1361 PP.Lex(Tok);
1362 } else if (Tok.is(tok::r_paren)) {
1363 // #pragma pointers_to_members(full_generality) implicitly specifies
1364 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001365 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001366 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001367 } else {
1368 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1369 << "full_generality";
1370 return;
1371 }
1372 }
1373
1374 if (Arg) {
1375 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001376 RepresentationMethod =
1377 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001378 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001379 RepresentationMethod =
1380 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001381 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001382 RepresentationMethod =
1383 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001384 } else {
1385 PP.Diag(Tok.getLocation(),
1386 diag::err_pragma_pointers_to_members_unknown_kind)
1387 << Arg << /*HasPointerDeclaration*/ 1;
1388 return;
1389 }
1390 }
1391 }
1392
1393 if (Tok.isNot(tok::r_paren)) {
1394 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1395 << (Arg ? Arg->getName() : "full_generality");
1396 return;
1397 }
1398
1399 PP.Lex(Tok);
1400 if (Tok.isNot(tok::eod)) {
1401 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1402 << "pointers_to_members";
1403 return;
1404 }
1405
1406 Token AnnotTok;
1407 AnnotTok.startToken();
1408 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1409 AnnotTok.setLocation(PointersToMembersLoc);
1410 AnnotTok.setAnnotationValue(
1411 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1412 PP.EnterToken(AnnotTok);
1413}
1414
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001415/// \brief Handle '#pragma vtordisp'
1416// The grammar for this pragma is as follows:
1417//
1418// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1419//
1420// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1421// #pragma vtordisp '(' 'pop' ')'
1422// #pragma vtordisp '(' ')'
1423void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1424 PragmaIntroducerKind Introducer,
1425 Token &Tok) {
1426 SourceLocation VtorDispLoc = Tok.getLocation();
1427 PP.Lex(Tok);
1428 if (Tok.isNot(tok::l_paren)) {
1429 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1430 return;
1431 }
1432 PP.Lex(Tok);
1433
1434 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1435 const IdentifierInfo *II = Tok.getIdentifierInfo();
1436 if (II) {
1437 if (II->isStr("push")) {
1438 // #pragma vtordisp(push, mode)
1439 PP.Lex(Tok);
1440 if (Tok.isNot(tok::comma)) {
1441 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1442 return;
1443 }
1444 PP.Lex(Tok);
1445 Kind = Sema::PVDK_Push;
1446 // not push, could be on/off
1447 } else if (II->isStr("pop")) {
1448 // #pragma vtordisp(pop)
1449 PP.Lex(Tok);
1450 Kind = Sema::PVDK_Pop;
1451 }
1452 // not push or pop, could be on/off
1453 } else {
1454 if (Tok.is(tok::r_paren)) {
1455 // #pragma vtordisp()
1456 Kind = Sema::PVDK_Reset;
1457 }
1458 }
1459
1460
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001461 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001462 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1463 const IdentifierInfo *II = Tok.getIdentifierInfo();
1464 if (II && II->isStr("off")) {
1465 PP.Lex(Tok);
1466 Value = 0;
1467 } else if (II && II->isStr("on")) {
1468 PP.Lex(Tok);
1469 Value = 1;
1470 } else if (Tok.is(tok::numeric_constant) &&
1471 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1472 if (Value > 2) {
1473 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1474 << 0 << 2 << "vtordisp";
1475 return;
1476 }
1477 } else {
1478 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1479 << "vtordisp";
1480 return;
1481 }
1482 }
1483
1484 // Finish the pragma: ')' $
1485 if (Tok.isNot(tok::r_paren)) {
1486 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1487 return;
1488 }
1489 PP.Lex(Tok);
1490 if (Tok.isNot(tok::eod)) {
1491 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1492 << "vtordisp";
1493 return;
1494 }
1495
1496 // Enter the annotation.
1497 Token AnnotTok;
1498 AnnotTok.startToken();
1499 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1500 AnnotTok.setLocation(VtorDispLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001501 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1502 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001503 PP.EnterToken(AnnotTok);
1504}
1505
Warren Huntc3b18962014-04-08 22:30:47 +00001506/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1507/// an annotation token.
1508void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1509 PragmaIntroducerKind Introducer,
1510 Token &Tok) {
1511 Token EoF, AnnotTok;
1512 EoF.startToken();
1513 EoF.setKind(tok::eof);
1514 AnnotTok.startToken();
1515 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1516 AnnotTok.setLocation(Tok.getLocation());
1517 SmallVector<Token, 8> TokenVector;
1518 // Suck up all of the tokens before the eod.
1519 for (; Tok.isNot(tok::eod); PP.Lex(Tok))
1520 TokenVector.push_back(Tok);
1521 // Add a sentinal EoF token to the end of the list.
1522 TokenVector.push_back(EoF);
1523 // We must allocate this array with new because EnterTokenStream is going to
1524 // delete it later.
1525 Token *TokenArray = new Token[TokenVector.size()];
1526 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1527 auto Value = new (PP.getPreprocessorAllocator())
1528 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1529 AnnotTok.setAnnotationValue(Value);
1530 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001531}
1532
Aaron Ballman5d041be2013-06-04 02:07:14 +00001533/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1534///
1535/// The syntax is:
1536/// \code
1537/// #pragma detect_mismatch("name", "value")
1538/// \endcode
1539/// Where 'name' and 'value' are quoted strings. The values are embedded in
1540/// the object file and passed along to the linker. If the linker detects a
1541/// mismatch in the object file's values for the given name, a LNK2038 error
1542/// is emitted. See MSDN for more details.
1543void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1544 PragmaIntroducerKind Introducer,
1545 Token &Tok) {
1546 SourceLocation CommentLoc = Tok.getLocation();
1547 PP.Lex(Tok);
1548 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001549 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001550 return;
1551 }
1552
1553 // Read the name to embed, which must be a string literal.
1554 std::string NameString;
1555 if (!PP.LexStringLiteral(Tok, NameString,
1556 "pragma detect_mismatch",
1557 /*MacroExpansion=*/true))
1558 return;
1559
1560 // Read the comma followed by a second string literal.
1561 std::string ValueString;
1562 if (Tok.isNot(tok::comma)) {
1563 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1564 return;
1565 }
1566
1567 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1568 /*MacroExpansion=*/true))
1569 return;
1570
1571 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001572 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001573 return;
1574 }
1575 PP.Lex(Tok); // Eat the r_paren.
1576
1577 if (Tok.isNot(tok::eod)) {
1578 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1579 return;
1580 }
1581
Reid Kleckner71966c92014-02-20 23:37:45 +00001582 // If the pragma is lexically sound, notify any interested PPCallbacks.
1583 if (PP.getPPCallbacks())
1584 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1585 ValueString);
1586
Aaron Ballman5d041be2013-06-04 02:07:14 +00001587 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1588}
1589
Reid Kleckner002562a2013-05-06 21:02:12 +00001590/// \brief Handle the microsoft \#pragma comment extension.
1591///
1592/// The syntax is:
1593/// \code
1594/// #pragma comment(linker, "foo")
1595/// \endcode
1596/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1597/// "foo" is a string, which is fully macro expanded, and permits string
1598/// concatenation, embedded escape characters etc. See MSDN for more details.
1599void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1600 PragmaIntroducerKind Introducer,
1601 Token &Tok) {
1602 SourceLocation CommentLoc = Tok.getLocation();
1603 PP.Lex(Tok);
1604 if (Tok.isNot(tok::l_paren)) {
1605 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1606 return;
1607 }
1608
1609 // Read the identifier.
1610 PP.Lex(Tok);
1611 if (Tok.isNot(tok::identifier)) {
1612 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1613 return;
1614 }
1615
1616 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001617 IdentifierInfo *II = Tok.getIdentifierInfo();
1618 Sema::PragmaMSCommentKind Kind =
1619 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1620 .Case("linker", Sema::PCK_Linker)
1621 .Case("lib", Sema::PCK_Lib)
1622 .Case("compiler", Sema::PCK_Compiler)
1623 .Case("exestr", Sema::PCK_ExeStr)
1624 .Case("user", Sema::PCK_User)
1625 .Default(Sema::PCK_Unknown);
1626 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001627 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1628 return;
1629 }
1630
1631 // Read the optional string if present.
1632 PP.Lex(Tok);
1633 std::string ArgumentString;
1634 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1635 "pragma comment",
1636 /*MacroExpansion=*/true))
1637 return;
1638
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001639 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001640 // FIXME: If the kind is "compiler" warn if the string is present (it is
1641 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001642 // The MSDN docs say that "lib" and "linker" require a string and have a short
1643 // whitelist of linker options they support, but in practice MSVC doesn't
1644 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001645
1646 if (Tok.isNot(tok::r_paren)) {
1647 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1648 return;
1649 }
1650 PP.Lex(Tok); // eat the r_paren.
1651
1652 if (Tok.isNot(tok::eod)) {
1653 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1654 return;
1655 }
1656
Reid Kleckner71966c92014-02-20 23:37:45 +00001657 // If the pragma is lexically sound, notify any interested PPCallbacks.
1658 if (PP.getPPCallbacks())
1659 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1660
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001661 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001662}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001663
1664// #pragma clang optimize off
1665// #pragma clang optimize on
1666void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1667 PragmaIntroducerKind Introducer,
1668 Token &FirstToken) {
1669 Token Tok;
1670 PP.Lex(Tok);
1671 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001672 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
1673 << "clang optimize"
1674 << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001675 return;
1676 }
1677 if (Tok.isNot(tok::identifier)) {
1678 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1679 << PP.getSpelling(Tok);
1680 return;
1681 }
1682 const IdentifierInfo *II = Tok.getIdentifierInfo();
1683 // The only accepted values are 'on' or 'off'.
1684 bool IsOn = false;
1685 if (II->isStr("on")) {
1686 IsOn = true;
1687 } else if (!II->isStr("off")) {
1688 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1689 << PP.getSpelling(Tok);
1690 return;
1691 }
1692 PP.Lex(Tok);
1693
1694 if (Tok.isNot(tok::eod)) {
1695 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1696 << PP.getSpelling(Tok);
1697 return;
1698 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001699
1700 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1701}
1702
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001703/// \brief Parses loop or unroll pragma hint value and fills in Info.
1704static bool ParseLoopHintValue(Preprocessor &PP, Token Tok, Token &PragmaName,
1705 Token &Option, bool &ValueInParens,
1706 PragmaLoopHintInfo &Info) {
1707 ValueInParens = Tok.is(tok::l_paren);
1708 if (ValueInParens) {
1709 PP.Lex(Tok);
1710 if (Tok.is(tok::r_paren)) {
1711 // Nothing between the parentheses.
1712 std::string PragmaString;
1713 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1714 PragmaString = "clang loop ";
1715 PragmaString += Option.getIdentifierInfo()->getName();
1716 } else {
1717 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1718 "Unexpected pragma name");
1719 PragmaString = "unroll";
1720 }
1721 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
1722 << PragmaString << "a positive integer value";
1723 return true;
1724 }
1725 }
1726
1727 // FIXME: Value should be stored and parsed as a constant expression.
1728 Token Value = Tok;
1729
1730 if (ValueInParens) {
1731 PP.Lex(Tok);
1732 if (Tok.isNot(tok::r_paren)) {
1733 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1734 return true;
1735 }
1736 }
1737
1738 Info.PragmaName = PragmaName;
1739 Info.Option = Option;
1740 Info.Value = Value;
1741 Info.HasValue = true;
1742 return false;
1743}
1744
Eli Bendersky06a40422014-06-06 20:31:48 +00001745/// \brief Handle the \#pragma clang loop directive.
1746/// #pragma clang 'loop' loop-hints
1747///
1748/// loop-hints:
1749/// loop-hint loop-hints[opt]
1750///
1751/// loop-hint:
1752/// 'vectorize' '(' loop-hint-keyword ')'
1753/// 'interleave' '(' loop-hint-keyword ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001754/// 'unroll' '(' loop-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001755/// 'vectorize_width' '(' loop-hint-value ')'
1756/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001757/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001758///
1759/// loop-hint-keyword:
1760/// 'enable'
1761/// 'disable'
1762///
1763/// loop-hint-value:
1764/// constant-expression
1765///
1766/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1767/// try vectorizing the instructions of the loop it precedes. Specifying
1768/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1769/// interleaving multiple iterations of the loop it precedes. The width of the
1770/// vector instructions is specified by vectorize_width() and the number of
1771/// interleaved loop iterations is specified by interleave_count(). Specifying a
1772/// value of 1 effectively disables vectorization/interleaving, even if it is
1773/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1774/// only works on inner loops.
1775///
Eli Bendersky86483b32014-06-11 17:56:26 +00001776/// The unroll and unroll_count directives control the concatenation
1777/// unroller. Specifying unroll(enable) instructs llvm to try to
1778/// unroll the loop completely, and unroll(disable) disables unrolling
1779/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1780/// try to unroll the loop the number of times indicated by the value.
1781/// If unroll(enable) and unroll_count are both specified only
1782/// unroll_count takes effect.
Eli Bendersky06a40422014-06-06 20:31:48 +00001783void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1784 PragmaIntroducerKind Introducer,
1785 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001786 // Incoming token is "loop" from "#pragma clang loop".
1787 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001788 SmallVector<Token, 1> TokenList;
1789
1790 // Lex the optimization option and verify it is an identifier.
1791 PP.Lex(Tok);
1792 if (Tok.isNot(tok::identifier)) {
1793 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1794 << /*MissingOption=*/true << "";
1795 return;
1796 }
1797
1798 while (Tok.is(tok::identifier)) {
1799 Token Option = Tok;
1800 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1801
Eli Bendersky86483b32014-06-11 17:56:26 +00001802 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001803 .Case("vectorize", true)
1804 .Case("interleave", true)
1805 .Case("unroll", true)
1806 .Case("vectorize_width", true)
1807 .Case("interleave_count", true)
1808 .Case("unroll_count", true)
1809 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00001810 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001811 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1812 << /*MissingOption=*/false << OptionInfo;
1813 return;
1814 }
1815
Eli Bendersky06a40422014-06-06 20:31:48 +00001816 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001817 PP.Lex(Tok);
1818 bool ValueInParens;
1819 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
1820 return;
Eli Bendersky06a40422014-06-06 20:31:48 +00001821
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001822 if (!ValueInParens) {
1823 PP.Diag(Info->Value.getLocation(), diag::err_expected) << tok::l_paren;
1824 return;
1825 }
1826
1827 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00001828 Token LoopHintTok;
1829 LoopHintTok.startToken();
1830 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001831 LoopHintTok.setLocation(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00001832 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
1833 TokenList.push_back(LoopHintTok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001834
1835 // Get next optimization option.
1836 PP.Lex(Tok);
Eli Bendersky06a40422014-06-06 20:31:48 +00001837 }
1838
1839 if (Tok.isNot(tok::eod)) {
1840 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1841 << "clang loop";
1842 return;
1843 }
1844
1845 Token *TokenArray = new Token[TokenList.size()];
1846 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
1847
1848 PP.EnterTokenStream(TokenArray, TokenList.size(),
1849 /*DisableMacroExpansion=*/false,
1850 /*OwnsTokens=*/true);
1851}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001852
1853/// \brief Handle the loop unroll optimization pragmas.
1854/// #pragma unroll
1855/// #pragma unroll unroll-hint-value
1856/// #pragma unroll '(' unroll-hint-value ')'
1857///
1858/// unroll-hint-value:
1859/// constant-expression
1860///
1861/// Loop unrolling hints are specified with '#pragma unroll'. '#pragma unroll'
1862/// can take a numeric argument optionally contained in parentheses. With no
1863/// argument the directive instructs llvm to try to unroll the loop
1864/// completely. A positive integer argument can be specified to indicate the
1865/// number of times the loop should be unrolled. To maximize compatibility with
1866/// other compilers the unroll count argument can be specified with or without
1867/// parentheses.
1868void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
1869 PragmaIntroducerKind Introducer,
1870 Token &Tok) {
1871 // Incoming token is "unroll" of "#pragma unroll".
1872 Token PragmaName = Tok;
1873 PP.Lex(Tok);
1874 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1875 if (Tok.is(tok::eod)) {
1876 // Unroll pragma without an argument.
1877 Info->PragmaName = PragmaName;
1878 Info->Option = PragmaName;
1879 Info->HasValue = false;
1880 } else {
1881 // Unroll pragma with an argument: "#pragma unroll N" or
1882 // "#pragma unroll(N)".
1883 bool ValueInParens;
1884 if (ParseLoopHintValue(PP, Tok, PragmaName, PragmaName, ValueInParens,
1885 *Info))
1886 return;
1887
1888 // In CUDA, the argument to '#pragma unroll' should not be contained in
1889 // parentheses.
1890 if (PP.getLangOpts().CUDA && ValueInParens)
1891 PP.Diag(Info->Value.getLocation(),
1892 diag::warn_pragma_unroll_cuda_value_in_parens);
1893
1894 PP.Lex(Tok);
1895 if (Tok.isNot(tok::eod)) {
1896 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1897 << "unroll";
1898 return;
1899 }
1900 }
1901
1902 // Generate the hint token.
1903 Token *TokenArray = new Token[1];
1904 TokenArray[0].startToken();
1905 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
1906 TokenArray[0].setLocation(PragmaName.getLocation());
1907 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
1908 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
1909 /*OwnsTokens=*/true);
1910}