blob: 5fb999d749c6cf4f9fa8a3cedce9bb25612a6fe6 [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 Kleckner1a711b12014-07-22 00:53:05 +0000655// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000656bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
657 SourceLocation PragmaLocation) {
Reid Kleckner1a711b12014-07-22 00:53:05 +0000658 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
659 PragmaName))
660 return false;
661
662 // Parse either the known section names or the string section name.
663 StringLiteral *SegmentName = nullptr;
664 if (Tok.isAnyIdentifier()) {
665 auto *II = Tok.getIdentifierInfo();
666 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
667 .Case("compiler", "\".CRT$XCC\"")
668 .Case("lib", "\".CRT$XCL\"")
669 .Case("user", "\".CRT$XCU\"")
670 .Default("");
671
672 if (!Section.empty()) {
673 // Pretend the user wrote the appropriate string literal here.
674 Token Toks[1];
675 Toks[0].startToken();
676 Toks[0].setKind(tok::string_literal);
677 Toks[0].setLocation(Tok.getLocation());
678 Toks[0].setLiteralData(Section.data());
679 Toks[0].setLength(Section.size());
680 SegmentName =
681 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
682 PP.Lex(Tok);
683 }
684 } else if (Tok.is(tok::string_literal)) {
685 ExprResult StringResult = ParseStringLiteralExpression();
686 if (StringResult.isInvalid())
687 return false;
688 SegmentName = cast<StringLiteral>(StringResult.get());
689 if (SegmentName->getCharByteWidth() != 1) {
690 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
691 << PragmaName;
692 return false;
693 }
694 // FIXME: Add support for the '[, func-name]' part of the pragma.
695 }
696
697 if (!SegmentName) {
698 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
699 return false;
700 }
701
702 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
703 PragmaName) ||
704 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
705 PragmaName))
706 return false;
707
708 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
709 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000710}
711
712struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000713 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000714 Token Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000715 Token Value;
716 bool HasValue;
Eli Bendersky06a40422014-06-06 20:31:48 +0000717};
718
719LoopHint Parser::HandlePragmaLoopHint() {
720 assert(Tok.is(tok::annot_pragma_loop_hint));
721 PragmaLoopHintInfo *Info =
722 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
723
724 LoopHint Hint;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000725 Hint.PragmaNameLoc =
726 IdentifierLoc::create(Actions.Context, Info->PragmaName.getLocation(),
727 Info->PragmaName.getIdentifierInfo());
Eli Bendersky06a40422014-06-06 20:31:48 +0000728 Hint.OptionLoc =
729 IdentifierLoc::create(Actions.Context, Info->Option.getLocation(),
730 Info->Option.getIdentifierInfo());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000731 if (Info->HasValue) {
732 Hint.Range =
733 SourceRange(Info->Option.getLocation(), Info->Value.getLocation());
734 Hint.ValueLoc =
735 IdentifierLoc::create(Actions.Context, Info->Value.getLocation(),
736 Info->Value.getIdentifierInfo());
Eli Bendersky06a40422014-06-06 20:31:48 +0000737
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000738 // FIXME: We should allow non-type template parameters for the loop hint
739 // value. See bug report #19610
740 if (Info->Value.is(tok::numeric_constant))
741 Hint.ValueExpr = Actions.ActOnNumericConstant(Info->Value).get();
742 else
743 Hint.ValueExpr = nullptr;
744 } else {
745 Hint.Range = SourceRange(Info->PragmaName.getLocation());
746 Hint.ValueLoc = nullptr;
Eli Bendersky06a40422014-06-06 20:31:48 +0000747 Hint.ValueExpr = nullptr;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000748 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000749
750 return Hint;
751}
752
753// #pragma GCC visibility comes in two variants:
754// 'push' '(' [visibility] ')'
755// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000756void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
757 PragmaIntroducerKind Introducer,
758 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000759 SourceLocation VisLoc = VisTok.getLocation();
760
761 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000762 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000763
764 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
765
Eli Friedman570024a2010-08-05 06:57:20 +0000766 const IdentifierInfo *VisType;
767 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000768 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000769 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000770 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000771 if (Tok.isNot(tok::l_paren)) {
772 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
773 << "visibility";
774 return;
775 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000776 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000777 VisType = Tok.getIdentifierInfo();
778 if (!VisType) {
779 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
780 << "visibility";
781 return;
782 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000783 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000784 if (Tok.isNot(tok::r_paren)) {
785 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
786 << "visibility";
787 return;
788 }
789 } else {
790 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
791 << "visibility";
792 return;
793 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000794 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000795 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000796 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
797 << "visibility";
798 return;
799 }
800
Rafael Espindola273fd772012-01-26 02:02:57 +0000801 Token *Toks = new Token[1];
802 Toks[0].startToken();
803 Toks[0].setKind(tok::annot_pragma_vis);
804 Toks[0].setLocation(VisLoc);
805 Toks[0].setAnnotationValue(
806 const_cast<void*>(static_cast<const void*>(VisType)));
807 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
808 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000809}
810
Daniel Dunbar921b9682008-10-04 19:21:03 +0000811// #pragma pack(...) comes in the following delicious flavors:
812// pack '(' [integer] ')'
813// pack '(' 'show' ')'
814// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000815void PragmaPackHandler::HandlePragma(Preprocessor &PP,
816 PragmaIntroducerKind Introducer,
817 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000818 SourceLocation PackLoc = PackTok.getLocation();
819
820 Token Tok;
821 PP.Lex(Tok);
822 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000823 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000824 return;
825 }
826
John McCallfaf5fb42010-08-26 23:41:50 +0000827 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000828 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000829 Token Alignment;
830 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000831 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000832 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000833 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000834 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000835
836 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000837
838 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
839 // the push/pop stack.
840 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000841 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000842 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000843 } else if (Tok.is(tok::identifier)) {
844 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000845 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000846 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000847 PP.Lex(Tok);
848 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000849 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000850 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000851 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000852 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000853 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000854 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000855 return;
Mike Stump11289f42009-09-09 15:08:12 +0000856 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000857 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000858
Daniel Dunbar921b9682008-10-04 19:21:03 +0000859 if (Tok.is(tok::comma)) {
860 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000861
Daniel Dunbar921b9682008-10-04 19:21:03 +0000862 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000863 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000864
865 PP.Lex(Tok);
866 } else if (Tok.is(tok::identifier)) {
867 Name = Tok.getIdentifierInfo();
868 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000869
Daniel Dunbar921b9682008-10-04 19:21:03 +0000870 if (Tok.is(tok::comma)) {
871 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000872
Daniel Dunbar921b9682008-10-04 19:21:03 +0000873 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000874 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000875 return;
876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
Eli Friedman68be1642012-10-04 02:36:51 +0000878 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000879
880 PP.Lex(Tok);
881 }
882 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000883 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000884 return;
885 }
886 }
887 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000888 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +0000889 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
890 // the push/pop stack.
891 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
892 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000893 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000894
895 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000896 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000897 return;
898 }
899
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000900 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000901 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000902 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000903 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
904 return;
905 }
906
Daniel Dunbar340cf242012-02-29 01:38:22 +0000907 PragmaPackInfo *Info =
908 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
909 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
910 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +0000911 Info->Kind = Kind;
912 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000913 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000914 Info->LParenLoc = LParenLoc;
915 Info->RParenLoc = RParenLoc;
916
Daniel Dunbar340cf242012-02-29 01:38:22 +0000917 Token *Toks =
918 (Token*) PP.getPreprocessorAllocator().Allocate(
919 sizeof(Token) * 1, llvm::alignOf<Token>());
920 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +0000921 Toks[0].startToken();
922 Toks[0].setKind(tok::annot_pragma_pack);
923 Toks[0].setLocation(PackLoc);
924 Toks[0].setAnnotationValue(static_cast<void*>(Info));
925 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +0000926 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000927}
928
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000929// #pragma ms_struct on
930// #pragma ms_struct off
931void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
932 PragmaIntroducerKind Introducer,
933 Token &MSStructTok) {
934 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
935
936 Token Tok;
937 PP.Lex(Tok);
938 if (Tok.isNot(tok::identifier)) {
939 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
940 return;
941 }
942 const IdentifierInfo *II = Tok.getIdentifierInfo();
943 if (II->isStr("on")) {
944 Kind = Sema::PMSST_ON;
945 PP.Lex(Tok);
946 }
947 else if (II->isStr("off") || II->isStr("reset"))
948 PP.Lex(Tok);
949 else {
950 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
951 return;
952 }
953
954 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +0000955 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
956 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000957 return;
958 }
Eli Friedman68be1642012-10-04 02:36:51 +0000959
960 Token *Toks =
961 (Token*) PP.getPreprocessorAllocator().Allocate(
962 sizeof(Token) * 1, llvm::alignOf<Token>());
963 new (Toks) Token();
964 Toks[0].startToken();
965 Toks[0].setKind(tok::annot_pragma_msstruct);
966 Toks[0].setLocation(MSStructTok.getLocation());
967 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
968 static_cast<uintptr_t>(Kind)));
969 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
970 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000971}
972
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000973// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
974// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +0000975static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000976 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000977 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000978
979 if (IsOptions) {
980 PP.Lex(Tok);
981 if (Tok.isNot(tok::identifier) ||
982 !Tok.getIdentifierInfo()->isStr("align")) {
983 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
984 return;
985 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000986 }
Daniel Dunbar663e8092010-05-27 18:42:09 +0000987
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000988 PP.Lex(Tok);
989 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000990 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
991 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000992 return;
993 }
994
995 PP.Lex(Tok);
996 if (Tok.isNot(tok::identifier)) {
997 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +0000998 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000999 return;
1000 }
1001
John McCallfaf5fb42010-08-26 23:41:50 +00001002 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001003 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001004 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001005 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001006 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001007 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001008 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001009 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001010 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001011 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001012 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001013 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001014 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001015 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001016 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001017 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1018 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001019 return;
1020 }
1021
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001022 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001023 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001024 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001025 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001026 return;
1027 }
1028
Eli Friedman68be1642012-10-04 02:36:51 +00001029 Token *Toks =
1030 (Token*) PP.getPreprocessorAllocator().Allocate(
1031 sizeof(Token) * 1, llvm::alignOf<Token>());
1032 new (Toks) Token();
1033 Toks[0].startToken();
1034 Toks[0].setKind(tok::annot_pragma_align);
1035 Toks[0].setLocation(FirstTok.getLocation());
1036 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1037 static_cast<uintptr_t>(Kind)));
1038 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1039 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001040}
1041
Douglas Gregorc7d65762010-09-09 22:45:38 +00001042void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1043 PragmaIntroducerKind Introducer,
1044 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001045 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001046}
1047
Douglas Gregorc7d65762010-09-09 22:45:38 +00001048void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1049 PragmaIntroducerKind Introducer,
1050 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001051 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001052}
1053
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001054// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001055void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1056 PragmaIntroducerKind Introducer,
1057 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001058 // FIXME: Should we be expanding macros here? My guess is no.
1059 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001060
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001061 // Lex the left '('.
1062 Token Tok;
1063 PP.Lex(Tok);
1064 if (Tok.isNot(tok::l_paren)) {
1065 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1066 return;
1067 }
Mike Stump11289f42009-09-09 15:08:12 +00001068
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001069 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001070 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001071 SourceLocation RParenLoc;
1072 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001073
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001074 while (true) {
1075 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001076
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001077 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001078 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001079 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001080 LexID = false;
1081 continue;
1082 }
1083
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001084 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001085 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1086 return;
1087 }
Mike Stump11289f42009-09-09 15:08:12 +00001088
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001089 // We are execting a ')' or a ','.
1090 if (Tok.is(tok::comma)) {
1091 LexID = true;
1092 continue;
1093 }
Mike Stump11289f42009-09-09 15:08:12 +00001094
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001095 if (Tok.is(tok::r_paren)) {
1096 RParenLoc = Tok.getLocation();
1097 break;
1098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001100 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001101 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001102 return;
1103 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001104
1105 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001106 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001107 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1108 "unused";
1109 return;
1110 }
1111
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001112 // Verify that we have a location for the right parenthesis.
1113 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001114 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001115
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001116 // For each identifier token, insert into the token stream a
1117 // annot_pragma_unused token followed by the identifier token.
1118 // This allows us to cache a "#pragma unused" that occurs inside an inline
1119 // C++ member function.
1120
Daniel Dunbar340cf242012-02-29 01:38:22 +00001121 Token *Toks =
1122 (Token*) PP.getPreprocessorAllocator().Allocate(
1123 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001124 for (unsigned i=0; i != Identifiers.size(); i++) {
1125 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1126 pragmaUnusedTok.startToken();
1127 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1128 pragmaUnusedTok.setLocation(UnusedLoc);
1129 idTok = Identifiers[i];
1130 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001131 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1132 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001133}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001134
1135// #pragma weak identifier
1136// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001137void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1138 PragmaIntroducerKind Introducer,
1139 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001140 SourceLocation WeakLoc = WeakTok.getLocation();
1141
1142 Token Tok;
1143 PP.Lex(Tok);
1144 if (Tok.isNot(tok::identifier)) {
1145 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1146 return;
1147 }
1148
Eli Friedman68be1642012-10-04 02:36:51 +00001149 Token WeakName = Tok;
1150 bool HasAlias = false;
1151 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001152
1153 PP.Lex(Tok);
1154 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001155 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001156 PP.Lex(Tok);
1157 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001158 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001159 << "weak";
1160 return;
1161 }
Eli Friedman68be1642012-10-04 02:36:51 +00001162 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001163 PP.Lex(Tok);
1164 }
1165
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001166 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001167 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1168 return;
1169 }
1170
Eli Friedman68be1642012-10-04 02:36:51 +00001171 if (HasAlias) {
1172 Token *Toks =
1173 (Token*) PP.getPreprocessorAllocator().Allocate(
1174 sizeof(Token) * 3, llvm::alignOf<Token>());
1175 Token &pragmaUnusedTok = Toks[0];
1176 pragmaUnusedTok.startToken();
1177 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1178 pragmaUnusedTok.setLocation(WeakLoc);
1179 Toks[1] = WeakName;
1180 Toks[2] = AliasName;
1181 PP.EnterTokenStream(Toks, 3,
1182 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001183 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001184 Token *Toks =
1185 (Token*) PP.getPreprocessorAllocator().Allocate(
1186 sizeof(Token) * 2, llvm::alignOf<Token>());
1187 Token &pragmaUnusedTok = Toks[0];
1188 pragmaUnusedTok.startToken();
1189 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1190 pragmaUnusedTok.setLocation(WeakLoc);
1191 Toks[1] = WeakName;
1192 PP.EnterTokenStream(Toks, 2,
1193 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001194 }
1195}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001196
David Chisnall0867d9c2012-02-18 16:12:34 +00001197// #pragma redefine_extname identifier identifier
1198void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1199 PragmaIntroducerKind Introducer,
1200 Token &RedefToken) {
1201 SourceLocation RedefLoc = RedefToken.getLocation();
1202
1203 Token Tok;
1204 PP.Lex(Tok);
1205 if (Tok.isNot(tok::identifier)) {
1206 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1207 "redefine_extname";
1208 return;
1209 }
1210
Eli Friedman68be1642012-10-04 02:36:51 +00001211 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001212 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001213
David Chisnall0867d9c2012-02-18 16:12:34 +00001214 if (Tok.isNot(tok::identifier)) {
1215 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1216 << "redefine_extname";
1217 return;
1218 }
Eli Friedman68be1642012-10-04 02:36:51 +00001219
1220 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001221 PP.Lex(Tok);
1222
1223 if (Tok.isNot(tok::eod)) {
1224 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1225 "redefine_extname";
1226 return;
1227 }
1228
Eli Friedman68be1642012-10-04 02:36:51 +00001229 Token *Toks =
1230 (Token*) PP.getPreprocessorAllocator().Allocate(
1231 sizeof(Token) * 3, llvm::alignOf<Token>());
1232 Token &pragmaRedefTok = Toks[0];
1233 pragmaRedefTok.startToken();
1234 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1235 pragmaRedefTok.setLocation(RedefLoc);
1236 Toks[1] = RedefName;
1237 Toks[2] = AliasName;
1238 PP.EnterTokenStream(Toks, 3,
1239 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001240}
1241
1242
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001243void
1244PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1245 PragmaIntroducerKind Introducer,
1246 Token &Tok) {
1247 tok::OnOffSwitch OOS;
1248 if (PP.LexOnOffSwitch(OOS))
1249 return;
1250
Eli Friedman68be1642012-10-04 02:36:51 +00001251 Token *Toks =
1252 (Token*) PP.getPreprocessorAllocator().Allocate(
1253 sizeof(Token) * 1, llvm::alignOf<Token>());
1254 new (Toks) Token();
1255 Toks[0].startToken();
1256 Toks[0].setKind(tok::annot_pragma_fp_contract);
1257 Toks[0].setLocation(Tok.getLocation());
1258 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1259 static_cast<uintptr_t>(OOS)));
1260 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1261 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001262}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001263
1264void
1265PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1266 PragmaIntroducerKind Introducer,
1267 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001268 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001269 if (Tok.isNot(tok::identifier)) {
1270 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1271 "OPENCL";
1272 return;
1273 }
1274 IdentifierInfo *ename = Tok.getIdentifierInfo();
1275 SourceLocation NameLoc = Tok.getLocation();
1276
1277 PP.Lex(Tok);
1278 if (Tok.isNot(tok::colon)) {
1279 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1280 return;
1281 }
1282
1283 PP.Lex(Tok);
1284 if (Tok.isNot(tok::identifier)) {
1285 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1286 return;
1287 }
1288 IdentifierInfo *op = Tok.getIdentifierInfo();
1289
1290 unsigned state;
1291 if (op->isStr("enable")) {
1292 state = 1;
1293 } else if (op->isStr("disable")) {
1294 state = 0;
1295 } else {
1296 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1297 return;
1298 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001299 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001300
Eli Friedman68be1642012-10-04 02:36:51 +00001301 PP.Lex(Tok);
1302 if (Tok.isNot(tok::eod)) {
1303 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1304 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001305 return;
1306 }
Eli Friedman68be1642012-10-04 02:36:51 +00001307
1308 OpenCLExtData data(ename, state);
1309 Token *Toks =
1310 (Token*) PP.getPreprocessorAllocator().Allocate(
1311 sizeof(Token) * 1, llvm::alignOf<Token>());
1312 new (Toks) Token();
1313 Toks[0].startToken();
1314 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1315 Toks[0].setLocation(NameLoc);
1316 Toks[0].setAnnotationValue(data.getOpaqueValue());
1317 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1318 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001319
1320 if (PP.getPPCallbacks())
1321 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1322 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001323}
1324
Alexey Bataeva769e072013-03-22 06:34:35 +00001325/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1326///
1327void
1328PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1329 PragmaIntroducerKind Introducer,
1330 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001331 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1332 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001333 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001334 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1335 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001336 }
1337 PP.DiscardUntilEndOfDirective();
1338}
1339
1340/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1341///
1342void
1343PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1344 PragmaIntroducerKind Introducer,
1345 Token &FirstTok) {
1346 SmallVector<Token, 16> Pragma;
1347 Token Tok;
1348 Tok.startToken();
1349 Tok.setKind(tok::annot_pragma_openmp);
1350 Tok.setLocation(FirstTok.getLocation());
1351
1352 while (Tok.isNot(tok::eod)) {
1353 Pragma.push_back(Tok);
1354 PP.Lex(Tok);
1355 }
1356 SourceLocation EodLoc = Tok.getLocation();
1357 Tok.startToken();
1358 Tok.setKind(tok::annot_pragma_openmp_end);
1359 Tok.setLocation(EodLoc);
1360 Pragma.push_back(Tok);
1361
1362 Token *Toks = new Token[Pragma.size()];
1363 std::copy(Pragma.begin(), Pragma.end(), Toks);
1364 PP.EnterTokenStream(Toks, Pragma.size(),
1365 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1366}
Reid Kleckner002562a2013-05-06 21:02:12 +00001367
David Majnemer4bb09802014-02-10 19:50:15 +00001368/// \brief Handle '#pragma pointers_to_members'
1369// The grammar for this pragma is as follows:
1370//
1371// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1372//
1373// #pragma pointers_to_members '(' 'best_case' ')'
1374// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1375// #pragma pointers_to_members '(' inheritance-model ')'
1376void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1377 PragmaIntroducerKind Introducer,
1378 Token &Tok) {
1379 SourceLocation PointersToMembersLoc = Tok.getLocation();
1380 PP.Lex(Tok);
1381 if (Tok.isNot(tok::l_paren)) {
1382 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1383 << "pointers_to_members";
1384 return;
1385 }
1386 PP.Lex(Tok);
1387 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1388 if (!Arg) {
1389 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1390 << "pointers_to_members";
1391 return;
1392 }
1393 PP.Lex(Tok);
1394
David Majnemer86c318f2014-02-11 21:05:00 +00001395 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001396 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001397 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001398 } else {
1399 if (Arg->isStr("full_generality")) {
1400 if (Tok.is(tok::comma)) {
1401 PP.Lex(Tok);
1402
1403 Arg = Tok.getIdentifierInfo();
1404 if (!Arg) {
1405 PP.Diag(Tok.getLocation(),
1406 diag::err_pragma_pointers_to_members_unknown_kind)
1407 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1408 return;
1409 }
1410 PP.Lex(Tok);
1411 } else if (Tok.is(tok::r_paren)) {
1412 // #pragma pointers_to_members(full_generality) implicitly specifies
1413 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001414 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001415 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001416 } else {
1417 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1418 << "full_generality";
1419 return;
1420 }
1421 }
1422
1423 if (Arg) {
1424 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001425 RepresentationMethod =
1426 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001427 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001428 RepresentationMethod =
1429 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001430 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001431 RepresentationMethod =
1432 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001433 } else {
1434 PP.Diag(Tok.getLocation(),
1435 diag::err_pragma_pointers_to_members_unknown_kind)
1436 << Arg << /*HasPointerDeclaration*/ 1;
1437 return;
1438 }
1439 }
1440 }
1441
1442 if (Tok.isNot(tok::r_paren)) {
1443 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1444 << (Arg ? Arg->getName() : "full_generality");
1445 return;
1446 }
1447
1448 PP.Lex(Tok);
1449 if (Tok.isNot(tok::eod)) {
1450 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1451 << "pointers_to_members";
1452 return;
1453 }
1454
1455 Token AnnotTok;
1456 AnnotTok.startToken();
1457 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1458 AnnotTok.setLocation(PointersToMembersLoc);
1459 AnnotTok.setAnnotationValue(
1460 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1461 PP.EnterToken(AnnotTok);
1462}
1463
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001464/// \brief Handle '#pragma vtordisp'
1465// The grammar for this pragma is as follows:
1466//
1467// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1468//
1469// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1470// #pragma vtordisp '(' 'pop' ')'
1471// #pragma vtordisp '(' ')'
1472void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1473 PragmaIntroducerKind Introducer,
1474 Token &Tok) {
1475 SourceLocation VtorDispLoc = Tok.getLocation();
1476 PP.Lex(Tok);
1477 if (Tok.isNot(tok::l_paren)) {
1478 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1479 return;
1480 }
1481 PP.Lex(Tok);
1482
1483 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1484 const IdentifierInfo *II = Tok.getIdentifierInfo();
1485 if (II) {
1486 if (II->isStr("push")) {
1487 // #pragma vtordisp(push, mode)
1488 PP.Lex(Tok);
1489 if (Tok.isNot(tok::comma)) {
1490 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1491 return;
1492 }
1493 PP.Lex(Tok);
1494 Kind = Sema::PVDK_Push;
1495 // not push, could be on/off
1496 } else if (II->isStr("pop")) {
1497 // #pragma vtordisp(pop)
1498 PP.Lex(Tok);
1499 Kind = Sema::PVDK_Pop;
1500 }
1501 // not push or pop, could be on/off
1502 } else {
1503 if (Tok.is(tok::r_paren)) {
1504 // #pragma vtordisp()
1505 Kind = Sema::PVDK_Reset;
1506 }
1507 }
1508
1509
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001510 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001511 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1512 const IdentifierInfo *II = Tok.getIdentifierInfo();
1513 if (II && II->isStr("off")) {
1514 PP.Lex(Tok);
1515 Value = 0;
1516 } else if (II && II->isStr("on")) {
1517 PP.Lex(Tok);
1518 Value = 1;
1519 } else if (Tok.is(tok::numeric_constant) &&
1520 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1521 if (Value > 2) {
1522 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1523 << 0 << 2 << "vtordisp";
1524 return;
1525 }
1526 } else {
1527 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1528 << "vtordisp";
1529 return;
1530 }
1531 }
1532
1533 // Finish the pragma: ')' $
1534 if (Tok.isNot(tok::r_paren)) {
1535 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1536 return;
1537 }
1538 PP.Lex(Tok);
1539 if (Tok.isNot(tok::eod)) {
1540 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1541 << "vtordisp";
1542 return;
1543 }
1544
1545 // Enter the annotation.
1546 Token AnnotTok;
1547 AnnotTok.startToken();
1548 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1549 AnnotTok.setLocation(VtorDispLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001550 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1551 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001552 PP.EnterToken(AnnotTok);
1553}
1554
Warren Huntc3b18962014-04-08 22:30:47 +00001555/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1556/// an annotation token.
1557void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1558 PragmaIntroducerKind Introducer,
1559 Token &Tok) {
1560 Token EoF, AnnotTok;
1561 EoF.startToken();
1562 EoF.setKind(tok::eof);
1563 AnnotTok.startToken();
1564 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1565 AnnotTok.setLocation(Tok.getLocation());
1566 SmallVector<Token, 8> TokenVector;
1567 // Suck up all of the tokens before the eod.
1568 for (; Tok.isNot(tok::eod); PP.Lex(Tok))
1569 TokenVector.push_back(Tok);
1570 // Add a sentinal EoF token to the end of the list.
1571 TokenVector.push_back(EoF);
1572 // We must allocate this array with new because EnterTokenStream is going to
1573 // delete it later.
1574 Token *TokenArray = new Token[TokenVector.size()];
1575 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1576 auto Value = new (PP.getPreprocessorAllocator())
1577 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1578 AnnotTok.setAnnotationValue(Value);
1579 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001580}
1581
Aaron Ballman5d041be2013-06-04 02:07:14 +00001582/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1583///
1584/// The syntax is:
1585/// \code
1586/// #pragma detect_mismatch("name", "value")
1587/// \endcode
1588/// Where 'name' and 'value' are quoted strings. The values are embedded in
1589/// the object file and passed along to the linker. If the linker detects a
1590/// mismatch in the object file's values for the given name, a LNK2038 error
1591/// is emitted. See MSDN for more details.
1592void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1593 PragmaIntroducerKind Introducer,
1594 Token &Tok) {
1595 SourceLocation CommentLoc = Tok.getLocation();
1596 PP.Lex(Tok);
1597 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001598 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001599 return;
1600 }
1601
1602 // Read the name to embed, which must be a string literal.
1603 std::string NameString;
1604 if (!PP.LexStringLiteral(Tok, NameString,
1605 "pragma detect_mismatch",
1606 /*MacroExpansion=*/true))
1607 return;
1608
1609 // Read the comma followed by a second string literal.
1610 std::string ValueString;
1611 if (Tok.isNot(tok::comma)) {
1612 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1613 return;
1614 }
1615
1616 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1617 /*MacroExpansion=*/true))
1618 return;
1619
1620 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001621 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001622 return;
1623 }
1624 PP.Lex(Tok); // Eat the r_paren.
1625
1626 if (Tok.isNot(tok::eod)) {
1627 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1628 return;
1629 }
1630
Reid Kleckner71966c92014-02-20 23:37:45 +00001631 // If the pragma is lexically sound, notify any interested PPCallbacks.
1632 if (PP.getPPCallbacks())
1633 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1634 ValueString);
1635
Aaron Ballman5d041be2013-06-04 02:07:14 +00001636 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1637}
1638
Reid Kleckner002562a2013-05-06 21:02:12 +00001639/// \brief Handle the microsoft \#pragma comment extension.
1640///
1641/// The syntax is:
1642/// \code
1643/// #pragma comment(linker, "foo")
1644/// \endcode
1645/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1646/// "foo" is a string, which is fully macro expanded, and permits string
1647/// concatenation, embedded escape characters etc. See MSDN for more details.
1648void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1649 PragmaIntroducerKind Introducer,
1650 Token &Tok) {
1651 SourceLocation CommentLoc = Tok.getLocation();
1652 PP.Lex(Tok);
1653 if (Tok.isNot(tok::l_paren)) {
1654 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1655 return;
1656 }
1657
1658 // Read the identifier.
1659 PP.Lex(Tok);
1660 if (Tok.isNot(tok::identifier)) {
1661 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1662 return;
1663 }
1664
1665 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001666 IdentifierInfo *II = Tok.getIdentifierInfo();
1667 Sema::PragmaMSCommentKind Kind =
1668 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1669 .Case("linker", Sema::PCK_Linker)
1670 .Case("lib", Sema::PCK_Lib)
1671 .Case("compiler", Sema::PCK_Compiler)
1672 .Case("exestr", Sema::PCK_ExeStr)
1673 .Case("user", Sema::PCK_User)
1674 .Default(Sema::PCK_Unknown);
1675 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001676 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1677 return;
1678 }
1679
1680 // Read the optional string if present.
1681 PP.Lex(Tok);
1682 std::string ArgumentString;
1683 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1684 "pragma comment",
1685 /*MacroExpansion=*/true))
1686 return;
1687
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001688 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001689 // FIXME: If the kind is "compiler" warn if the string is present (it is
1690 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001691 // The MSDN docs say that "lib" and "linker" require a string and have a short
1692 // whitelist of linker options they support, but in practice MSVC doesn't
1693 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001694
1695 if (Tok.isNot(tok::r_paren)) {
1696 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1697 return;
1698 }
1699 PP.Lex(Tok); // eat the r_paren.
1700
1701 if (Tok.isNot(tok::eod)) {
1702 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1703 return;
1704 }
1705
Reid Kleckner71966c92014-02-20 23:37:45 +00001706 // If the pragma is lexically sound, notify any interested PPCallbacks.
1707 if (PP.getPPCallbacks())
1708 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1709
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001710 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001711}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001712
1713// #pragma clang optimize off
1714// #pragma clang optimize on
1715void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1716 PragmaIntroducerKind Introducer,
1717 Token &FirstToken) {
1718 Token Tok;
1719 PP.Lex(Tok);
1720 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001721 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001722 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001723 return;
1724 }
1725 if (Tok.isNot(tok::identifier)) {
1726 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1727 << PP.getSpelling(Tok);
1728 return;
1729 }
1730 const IdentifierInfo *II = Tok.getIdentifierInfo();
1731 // The only accepted values are 'on' or 'off'.
1732 bool IsOn = false;
1733 if (II->isStr("on")) {
1734 IsOn = true;
1735 } else if (!II->isStr("off")) {
1736 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1737 << PP.getSpelling(Tok);
1738 return;
1739 }
1740 PP.Lex(Tok);
1741
1742 if (Tok.isNot(tok::eod)) {
1743 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1744 << PP.getSpelling(Tok);
1745 return;
1746 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001747
1748 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1749}
1750
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001751/// \brief Parses loop or unroll pragma hint value and fills in Info.
1752static bool ParseLoopHintValue(Preprocessor &PP, Token Tok, Token &PragmaName,
1753 Token &Option, bool &ValueInParens,
1754 PragmaLoopHintInfo &Info) {
1755 ValueInParens = Tok.is(tok::l_paren);
1756 if (ValueInParens) {
1757 PP.Lex(Tok);
1758 if (Tok.is(tok::r_paren)) {
1759 // Nothing between the parentheses.
1760 std::string PragmaString;
1761 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1762 PragmaString = "clang loop ";
1763 PragmaString += Option.getIdentifierInfo()->getName();
1764 } else {
1765 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1766 "Unexpected pragma name");
1767 PragmaString = "unroll";
1768 }
Mark Heffernan450c2382014-07-23 17:31:31 +00001769 // Don't try to emit what the pragma is expecting with the diagnostic
1770 // because the logic is non-trivial and we give expected values in sema
1771 // diagnostics if an invalid argument is given. Here, just note that the
1772 // pragma is missing an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001773 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001774 << PragmaString << /*Expected=*/false;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001775 return true;
1776 }
1777 }
1778
1779 // FIXME: Value should be stored and parsed as a constant expression.
1780 Token Value = Tok;
1781
1782 if (ValueInParens) {
1783 PP.Lex(Tok);
1784 if (Tok.isNot(tok::r_paren)) {
1785 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1786 return true;
1787 }
1788 }
1789
1790 Info.PragmaName = PragmaName;
1791 Info.Option = Option;
1792 Info.Value = Value;
1793 Info.HasValue = true;
1794 return false;
1795}
1796
Eli Bendersky06a40422014-06-06 20:31:48 +00001797/// \brief Handle the \#pragma clang loop directive.
1798/// #pragma clang 'loop' loop-hints
1799///
1800/// loop-hints:
1801/// loop-hint loop-hints[opt]
1802///
1803/// loop-hint:
1804/// 'vectorize' '(' loop-hint-keyword ')'
1805/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001806/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001807/// 'vectorize_width' '(' loop-hint-value ')'
1808/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001809/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001810///
1811/// loop-hint-keyword:
1812/// 'enable'
1813/// 'disable'
1814///
Mark Heffernan450c2382014-07-23 17:31:31 +00001815/// unroll-hint-keyword:
1816/// 'full'
1817/// 'disable'
1818///
Eli Bendersky06a40422014-06-06 20:31:48 +00001819/// loop-hint-value:
1820/// constant-expression
1821///
1822/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1823/// try vectorizing the instructions of the loop it precedes. Specifying
1824/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1825/// interleaving multiple iterations of the loop it precedes. The width of the
1826/// vector instructions is specified by vectorize_width() and the number of
1827/// interleaved loop iterations is specified by interleave_count(). Specifying a
1828/// value of 1 effectively disables vectorization/interleaving, even if it is
1829/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1830/// only works on inner loops.
1831///
Eli Bendersky86483b32014-06-11 17:56:26 +00001832/// The unroll and unroll_count directives control the concatenation
Mark Heffernan450c2382014-07-23 17:31:31 +00001833/// unroller. Specifying unroll(full) instructs llvm to try to
Eli Bendersky86483b32014-06-11 17:56:26 +00001834/// unroll the loop completely, and unroll(disable) disables unrolling
1835/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1836/// try to unroll the loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001837void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1838 PragmaIntroducerKind Introducer,
1839 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001840 // Incoming token is "loop" from "#pragma clang loop".
1841 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001842 SmallVector<Token, 1> TokenList;
1843
1844 // Lex the optimization option and verify it is an identifier.
1845 PP.Lex(Tok);
1846 if (Tok.isNot(tok::identifier)) {
1847 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1848 << /*MissingOption=*/true << "";
1849 return;
1850 }
1851
1852 while (Tok.is(tok::identifier)) {
1853 Token Option = Tok;
1854 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1855
Eli Bendersky86483b32014-06-11 17:56:26 +00001856 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001857 .Case("vectorize", true)
1858 .Case("interleave", true)
1859 .Case("unroll", true)
1860 .Case("vectorize_width", true)
1861 .Case("interleave_count", true)
1862 .Case("unroll_count", true)
1863 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00001864 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001865 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1866 << /*MissingOption=*/false << OptionInfo;
1867 return;
1868 }
1869
Eli Bendersky06a40422014-06-06 20:31:48 +00001870 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001871 PP.Lex(Tok);
1872 bool ValueInParens;
1873 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
1874 return;
Eli Bendersky06a40422014-06-06 20:31:48 +00001875
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001876 if (!ValueInParens) {
1877 PP.Diag(Info->Value.getLocation(), diag::err_expected) << tok::l_paren;
1878 return;
1879 }
1880
1881 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00001882 Token LoopHintTok;
1883 LoopHintTok.startToken();
1884 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001885 LoopHintTok.setLocation(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00001886 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
1887 TokenList.push_back(LoopHintTok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001888
1889 // Get next optimization option.
1890 PP.Lex(Tok);
Eli Bendersky06a40422014-06-06 20:31:48 +00001891 }
1892
1893 if (Tok.isNot(tok::eod)) {
1894 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1895 << "clang loop";
1896 return;
1897 }
1898
1899 Token *TokenArray = new Token[TokenList.size()];
1900 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
1901
1902 PP.EnterTokenStream(TokenArray, TokenList.size(),
1903 /*DisableMacroExpansion=*/false,
1904 /*OwnsTokens=*/true);
1905}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001906
1907/// \brief Handle the loop unroll optimization pragmas.
1908/// #pragma unroll
1909/// #pragma unroll unroll-hint-value
1910/// #pragma unroll '(' unroll-hint-value ')'
1911///
1912/// unroll-hint-value:
1913/// constant-expression
1914///
1915/// Loop unrolling hints are specified with '#pragma unroll'. '#pragma unroll'
1916/// can take a numeric argument optionally contained in parentheses. With no
1917/// argument the directive instructs llvm to try to unroll the loop
1918/// completely. A positive integer argument can be specified to indicate the
1919/// number of times the loop should be unrolled. To maximize compatibility with
1920/// other compilers the unroll count argument can be specified with or without
1921/// parentheses.
1922void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
1923 PragmaIntroducerKind Introducer,
1924 Token &Tok) {
1925 // Incoming token is "unroll" of "#pragma unroll".
1926 Token PragmaName = Tok;
1927 PP.Lex(Tok);
1928 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1929 if (Tok.is(tok::eod)) {
1930 // Unroll pragma without an argument.
1931 Info->PragmaName = PragmaName;
1932 Info->Option = PragmaName;
1933 Info->HasValue = false;
1934 } else {
1935 // Unroll pragma with an argument: "#pragma unroll N" or
1936 // "#pragma unroll(N)".
1937 bool ValueInParens;
1938 if (ParseLoopHintValue(PP, Tok, PragmaName, PragmaName, ValueInParens,
1939 *Info))
1940 return;
1941
1942 // In CUDA, the argument to '#pragma unroll' should not be contained in
1943 // parentheses.
1944 if (PP.getLangOpts().CUDA && ValueInParens)
1945 PP.Diag(Info->Value.getLocation(),
1946 diag::warn_pragma_unroll_cuda_value_in_parens);
1947
1948 PP.Lex(Tok);
1949 if (Tok.isNot(tok::eod)) {
1950 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1951 << "unroll";
1952 return;
1953 }
1954 }
1955
1956 // Generate the hint token.
1957 Token *TokenArray = new Token[1];
1958 TokenArray[0].startToken();
1959 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
1960 TokenArray[0].setLocation(PragmaName.getLocation());
1961 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
1962 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
1963 /*OwnsTokens=*/true);
1964}