blob: 64066c1ec29d90b8f256333be53baee244cb318c [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"
David Majnemerad2986e2014-08-14 06:35:08 +000015#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000016#include "clang/Lex/Preprocessor.h"
17#include "clang/Parse/ParseDiagnostic.h"
18#include "clang/Parse/Parser.h"
19#include "clang/Sema/LoopHint.h"
20#include "clang/Sema/Scope.h"
21#include "llvm/ADT/StringSwitch.h"
22using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000023
Reid Kleckner5b086462014-02-20 22:52:09 +000024namespace {
25
26struct PragmaAlignHandler : public PragmaHandler {
27 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Craig Topper2b07f022014-03-12 05:09:18 +000028 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
29 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000030};
31
32struct PragmaGCCVisibilityHandler : public PragmaHandler {
33 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000034 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
35 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000036};
37
38struct PragmaOptionsHandler : public PragmaHandler {
39 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000040 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
41 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000042};
43
44struct PragmaPackHandler : public PragmaHandler {
45 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000046 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
47 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000048};
49
50struct PragmaMSStructHandler : public PragmaHandler {
51 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000052 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
53 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000054};
55
56struct PragmaUnusedHandler : public PragmaHandler {
57 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000058 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
59 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000060};
61
62struct PragmaWeakHandler : public PragmaHandler {
63 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000064 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
65 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000066};
67
68struct PragmaRedefineExtnameHandler : public PragmaHandler {
69 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000070 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
71 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000072};
73
74struct PragmaOpenCLExtensionHandler : public PragmaHandler {
75 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000076 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
77 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000078};
79
80
81struct PragmaFPContractHandler : public PragmaHandler {
82 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000083 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
84 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000085};
86
87struct PragmaNoOpenMPHandler : public PragmaHandler {
88 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000089 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
90 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000091};
92
93struct PragmaOpenMPHandler : public PragmaHandler {
94 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +000095 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
96 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000097};
98
99/// PragmaCommentHandler - "\#pragma comment ...".
100struct PragmaCommentHandler : public PragmaHandler {
101 PragmaCommentHandler(Sema &Actions)
102 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000103 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
104 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000105private:
106 Sema &Actions;
107};
108
109struct PragmaDetectMismatchHandler : public PragmaHandler {
110 PragmaDetectMismatchHandler(Sema &Actions)
111 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000112 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
113 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000114private:
115 Sema &Actions;
116};
117
118struct PragmaMSPointersToMembers : public PragmaHandler {
119 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000120 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
121 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000122};
123
124struct PragmaMSVtorDisp : public PragmaHandler {
125 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000126 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
127 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000128};
129
Warren Huntc3b18962014-04-08 22:30:47 +0000130struct PragmaMSPragma : public PragmaHandler {
131 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000132 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
133 Token &FirstToken) override;
134};
135
Dario Domizioli13a0a382014-05-23 12:13:25 +0000136/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
137struct PragmaOptimizeHandler : public PragmaHandler {
138 PragmaOptimizeHandler(Sema &S)
139 : PragmaHandler("optimize"), Actions(S) {}
140 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
141 Token &FirstToken) override;
142private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000143 Sema &Actions;
144};
145
146struct PragmaLoopHintHandler : public PragmaHandler {
147 PragmaLoopHintHandler() : PragmaHandler("loop") {}
148 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
149 Token &FirstToken) override;
150};
151
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000152struct PragmaUnrollHintHandler : public PragmaHandler {
153 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
154 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
155 Token &FirstToken) override;
156};
157
Eli Bendersky06a40422014-06-06 20:31:48 +0000158} // end namespace
159
160void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000161 AlignHandler.reset(new PragmaAlignHandler());
162 PP.AddPragmaHandler(AlignHandler.get());
163
164 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
165 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
166
167 OptionsHandler.reset(new PragmaOptionsHandler());
168 PP.AddPragmaHandler(OptionsHandler.get());
169
170 PackHandler.reset(new PragmaPackHandler());
171 PP.AddPragmaHandler(PackHandler.get());
172
173 MSStructHandler.reset(new PragmaMSStructHandler());
174 PP.AddPragmaHandler(MSStructHandler.get());
175
176 UnusedHandler.reset(new PragmaUnusedHandler());
177 PP.AddPragmaHandler(UnusedHandler.get());
178
179 WeakHandler.reset(new PragmaWeakHandler());
180 PP.AddPragmaHandler(WeakHandler.get());
181
182 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
183 PP.AddPragmaHandler(RedefineExtnameHandler.get());
184
185 FPContractHandler.reset(new PragmaFPContractHandler());
186 PP.AddPragmaHandler("STDC", FPContractHandler.get());
187
188 if (getLangOpts().OpenCL) {
189 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
190 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
191
192 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
193 }
194 if (getLangOpts().OpenMP)
195 OpenMPHandler.reset(new PragmaOpenMPHandler());
196 else
197 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
198 PP.AddPragmaHandler(OpenMPHandler.get());
199
200 if (getLangOpts().MicrosoftExt) {
201 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
202 PP.AddPragmaHandler(MSCommentHandler.get());
203 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
204 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
205 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
206 PP.AddPragmaHandler(MSPointersToMembers.get());
207 MSVtorDisp.reset(new PragmaMSVtorDisp());
208 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000209 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000210 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000211 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
212 PP.AddPragmaHandler(MSDataSeg.get());
213 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
214 PP.AddPragmaHandler(MSBSSSeg.get());
215 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
216 PP.AddPragmaHandler(MSConstSeg.get());
217 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
218 PP.AddPragmaHandler(MSCodeSeg.get());
219 MSSection.reset(new PragmaMSPragma("section"));
220 PP.AddPragmaHandler(MSSection.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000221 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000222
223 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
224 PP.AddPragmaHandler("clang", OptimizeHandler.get());
225
226 LoopHintHandler.reset(new PragmaLoopHintHandler());
227 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000228
229 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
230 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000231
232 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
233 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000234}
235
236void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000237 // Remove the pragma handlers we installed.
238 PP.RemovePragmaHandler(AlignHandler.get());
239 AlignHandler.reset();
240 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
241 GCCVisibilityHandler.reset();
242 PP.RemovePragmaHandler(OptionsHandler.get());
243 OptionsHandler.reset();
244 PP.RemovePragmaHandler(PackHandler.get());
245 PackHandler.reset();
246 PP.RemovePragmaHandler(MSStructHandler.get());
247 MSStructHandler.reset();
248 PP.RemovePragmaHandler(UnusedHandler.get());
249 UnusedHandler.reset();
250 PP.RemovePragmaHandler(WeakHandler.get());
251 WeakHandler.reset();
252 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
253 RedefineExtnameHandler.reset();
254
255 if (getLangOpts().OpenCL) {
256 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
257 OpenCLExtensionHandler.reset();
258 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
259 }
260 PP.RemovePragmaHandler(OpenMPHandler.get());
261 OpenMPHandler.reset();
262
263 if (getLangOpts().MicrosoftExt) {
264 PP.RemovePragmaHandler(MSCommentHandler.get());
265 MSCommentHandler.reset();
266 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
267 MSDetectMismatchHandler.reset();
268 PP.RemovePragmaHandler(MSPointersToMembers.get());
269 MSPointersToMembers.reset();
270 PP.RemovePragmaHandler(MSVtorDisp.get());
271 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000272 PP.RemovePragmaHandler(MSInitSeg.get());
273 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000274 PP.RemovePragmaHandler(MSDataSeg.get());
275 MSDataSeg.reset();
276 PP.RemovePragmaHandler(MSBSSSeg.get());
277 MSBSSSeg.reset();
278 PP.RemovePragmaHandler(MSConstSeg.get());
279 MSConstSeg.reset();
280 PP.RemovePragmaHandler(MSCodeSeg.get());
281 MSCodeSeg.reset();
282 PP.RemovePragmaHandler(MSSection.get());
283 MSSection.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000284 }
285
286 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
287 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000288
289 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
290 OptimizeHandler.reset();
291
292 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
293 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000294
295 PP.RemovePragmaHandler(UnrollHintHandler.get());
296 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000297
298 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
299 NoUnrollHintHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000300}
301
302/// \brief Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000303///
304/// Each annot_pragma_unused is followed by the argument token so e.g.
305/// "#pragma unused(x,y)" becomes:
306/// annot_pragma_unused 'x' annot_pragma_unused 'y'
307void Parser::HandlePragmaUnused() {
308 assert(Tok.is(tok::annot_pragma_unused));
309 SourceLocation UnusedLoc = ConsumeToken();
310 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
311 ConsumeToken(); // The argument token.
312}
Eli Friedman570024a2010-08-05 06:57:20 +0000313
Rafael Espindola273fd772012-01-26 02:02:57 +0000314void Parser::HandlePragmaVisibility() {
315 assert(Tok.is(tok::annot_pragma_vis));
316 const IdentifierInfo *VisType =
317 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
318 SourceLocation VisLoc = ConsumeToken();
319 Actions.ActOnPragmaVisibility(VisType, VisLoc);
320}
321
Eli Friedmanec52f922012-02-23 23:47:16 +0000322struct PragmaPackInfo {
323 Sema::PragmaPackKind Kind;
324 IdentifierInfo *Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000325 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000326 SourceLocation LParenLoc;
327 SourceLocation RParenLoc;
328};
329
330void Parser::HandlePragmaPack() {
331 assert(Tok.is(tok::annot_pragma_pack));
332 PragmaPackInfo *Info =
333 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
334 SourceLocation PragmaLoc = ConsumeToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000335 ExprResult Alignment;
336 if (Info->Alignment.is(tok::numeric_constant)) {
337 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
338 if (Alignment.isInvalid())
339 return;
340 }
341 Actions.ActOnPragmaPack(Info->Kind, Info->Name, Alignment.get(), PragmaLoc,
Eli Friedmanec52f922012-02-23 23:47:16 +0000342 Info->LParenLoc, Info->RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +0000343}
344
Eli Friedman68be1642012-10-04 02:36:51 +0000345void Parser::HandlePragmaMSStruct() {
346 assert(Tok.is(tok::annot_pragma_msstruct));
347 Sema::PragmaMSStructKind Kind =
348 static_cast<Sema::PragmaMSStructKind>(
349 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
350 Actions.ActOnPragmaMSStruct(Kind);
351 ConsumeToken(); // The annotation token.
352}
353
354void Parser::HandlePragmaAlign() {
355 assert(Tok.is(tok::annot_pragma_align));
356 Sema::PragmaOptionsAlignKind Kind =
357 static_cast<Sema::PragmaOptionsAlignKind>(
358 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
359 SourceLocation PragmaLoc = ConsumeToken();
360 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
361}
362
363void Parser::HandlePragmaWeak() {
364 assert(Tok.is(tok::annot_pragma_weak));
365 SourceLocation PragmaLoc = ConsumeToken();
366 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
367 Tok.getLocation());
368 ConsumeToken(); // The weak name.
369}
370
371void Parser::HandlePragmaWeakAlias() {
372 assert(Tok.is(tok::annot_pragma_weakalias));
373 SourceLocation PragmaLoc = ConsumeToken();
374 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
375 SourceLocation WeakNameLoc = Tok.getLocation();
376 ConsumeToken();
377 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
378 SourceLocation AliasNameLoc = Tok.getLocation();
379 ConsumeToken();
380 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
381 WeakNameLoc, AliasNameLoc);
382
383}
384
385void Parser::HandlePragmaRedefineExtname() {
386 assert(Tok.is(tok::annot_pragma_redefine_extname));
387 SourceLocation RedefLoc = ConsumeToken();
388 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
389 SourceLocation RedefNameLoc = Tok.getLocation();
390 ConsumeToken();
391 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
392 SourceLocation AliasNameLoc = Tok.getLocation();
393 ConsumeToken();
394 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
395 RedefNameLoc, AliasNameLoc);
396}
397
398void Parser::HandlePragmaFPContract() {
399 assert(Tok.is(tok::annot_pragma_fp_contract));
400 tok::OnOffSwitch OOS =
401 static_cast<tok::OnOffSwitch>(
402 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
403 Actions.ActOnPragmaFPContract(OOS);
404 ConsumeToken(); // The annotation token.
405}
406
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000407StmtResult Parser::HandlePragmaCaptured()
408{
409 assert(Tok.is(tok::annot_pragma_captured));
410 ConsumeToken();
411
412 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000413 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000414 return StmtError();
415 }
416
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000417 SourceLocation Loc = Tok.getLocation();
418
419 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000420 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
421 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000422
423 StmtResult R = ParseCompoundStatement();
424 CapturedRegionScope.Exit();
425
426 if (R.isInvalid()) {
427 Actions.ActOnCapturedRegionError();
428 return StmtError();
429 }
430
431 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000432}
433
Eli Friedman68be1642012-10-04 02:36:51 +0000434namespace {
435 typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
436}
437
438void Parser::HandlePragmaOpenCLExtension() {
439 assert(Tok.is(tok::annot_pragma_opencl_extension));
440 OpenCLExtData data =
441 OpenCLExtData::getFromOpaqueValue(Tok.getAnnotationValue());
442 unsigned state = data.getInt();
443 IdentifierInfo *ename = data.getPointer();
444 SourceLocation NameLoc = Tok.getLocation();
445 ConsumeToken(); // The annotation token.
446
447 OpenCLOptions &f = Actions.getOpenCLOptions();
448 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
449 // overriding all previously issued extension directives, but only if the
450 // behavior is set to disable."
451 if (state == 0 && ename->isStr("all")) {
452#define OPENCLEXT(nm) f.nm = 0;
453#include "clang/Basic/OpenCLExtensions.def"
454 }
455#define OPENCLEXT(nm) else if (ename->isStr(#nm)) { f.nm = state; }
456#include "clang/Basic/OpenCLExtensions.def"
457 else {
458 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << ename;
459 return;
460 }
461}
462
David Majnemer4bb09802014-02-10 19:50:15 +0000463void Parser::HandlePragmaMSPointersToMembers() {
464 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000465 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
466 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000467 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
468 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
469 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
470}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000471
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000472void Parser::HandlePragmaMSVtorDisp() {
473 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
474 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
475 Sema::PragmaVtorDispKind Kind =
476 static_cast<Sema::PragmaVtorDispKind>((Value >> 16) & 0xFFFF);
477 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
478 SourceLocation PragmaLoc = ConsumeToken(); // The annotation token.
479 Actions.ActOnPragmaMSVtorDisp(Kind, PragmaLoc, Mode);
480}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000481
Warren Huntc3b18962014-04-08 22:30:47 +0000482void Parser::HandlePragmaMSPragma() {
483 assert(Tok.is(tok::annot_pragma_ms_pragma));
484 // Grab the tokens out of the annotation and enter them into the stream.
485 auto TheTokens = (std::pair<Token*, size_t> *)Tok.getAnnotationValue();
486 PP.EnterTokenStream(TheTokens->first, TheTokens->second, true, true);
487 SourceLocation PragmaLocation = ConsumeToken(); // The annotation token.
488 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000489 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000490 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000491
Warren Huntc3b18962014-04-08 22:30:47 +0000492 // Figure out which #pragma we're dealing with. The switch has no default
493 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000494 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000495 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
496 .Case("data_seg", &Parser::HandlePragmaMSSegment)
497 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
498 .Case("const_seg", &Parser::HandlePragmaMSSegment)
499 .Case("code_seg", &Parser::HandlePragmaMSSegment)
500 .Case("section", &Parser::HandlePragmaMSSection)
501 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000502
503 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
504 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
505 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000506 while (Tok.isNot(tok::eof))
507 PP.Lex(Tok);
508 PP.Lex(Tok);
509 }
510}
511
Reid Kleckner722b1df2014-07-18 00:13:16 +0000512bool Parser::HandlePragmaMSSection(StringRef PragmaName,
513 SourceLocation PragmaLocation) {
514 if (Tok.isNot(tok::l_paren)) {
515 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
516 return false;
517 }
Warren Huntc3b18962014-04-08 22:30:47 +0000518 PP.Lex(Tok); // (
519 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000520 if (Tok.isNot(tok::string_literal)) {
521 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
522 << PragmaName;
523 return false;
524 }
525 ExprResult StringResult = ParseStringLiteralExpression();
526 if (StringResult.isInvalid())
527 return false; // Already diagnosed.
528 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
529 if (SegmentName->getCharByteWidth() != 1) {
530 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
531 << PragmaName;
532 return false;
533 }
Warren Huntc3b18962014-04-08 22:30:47 +0000534 int SectionFlags = 0;
535 while (Tok.is(tok::comma)) {
536 PP.Lex(Tok); // ,
Reid Kleckner722b1df2014-07-18 00:13:16 +0000537 if (!Tok.isAnyIdentifier()) {
538 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
539 << PragmaName;
540 return false;
541 }
Warren Huntc3b18962014-04-08 22:30:47 +0000542 Sema::PragmaSectionFlag Flag =
543 llvm::StringSwitch<Sema::PragmaSectionFlag>(
544 Tok.getIdentifierInfo()->getName())
545 .Case("read", Sema::PSF_Read)
546 .Case("write", Sema::PSF_Write)
547 .Case("execute", Sema::PSF_Execute)
548 .Case("shared", Sema::PSF_Invalid)
549 .Case("nopage", Sema::PSF_Invalid)
550 .Case("nocache", Sema::PSF_Invalid)
551 .Case("discard", Sema::PSF_Invalid)
552 .Case("remove", Sema::PSF_Invalid)
553 .Default(Sema::PSF_None);
554 if (Flag == Sema::PSF_None || Flag == Sema::PSF_Invalid) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000555 PP.Diag(PragmaLocation, Flag == Sema::PSF_None
556 ? diag::warn_pragma_invalid_specific_action
557 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000558 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000559 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000560 }
561 SectionFlags |= Flag;
562 PP.Lex(Tok); // Identifier
563 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000564 if (Tok.isNot(tok::r_paren)) {
565 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
566 return false;
567 }
Warren Huntc3b18962014-04-08 22:30:47 +0000568 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000569 if (Tok.isNot(tok::eof)) {
570 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
571 << PragmaName;
572 return false;
573 }
Warren Huntc3b18962014-04-08 22:30:47 +0000574 PP.Lex(Tok); // eof
575 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000576 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000577}
578
Reid Kleckner722b1df2014-07-18 00:13:16 +0000579bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
580 SourceLocation PragmaLocation) {
581 if (Tok.isNot(tok::l_paren)) {
582 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
583 return false;
584 }
Warren Huntc3b18962014-04-08 22:30:47 +0000585 PP.Lex(Tok); // (
586 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000587 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000588 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000589 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000590 if (PushPop == "push")
591 Action = Sema::PSK_Push;
592 else if (PushPop == "pop")
593 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000594 else {
595 PP.Diag(PragmaLocation,
596 diag::warn_pragma_expected_section_push_pop_or_name)
597 << PragmaName;
598 return false;
599 }
Warren Huntc3b18962014-04-08 22:30:47 +0000600 if (Action != Sema::PSK_Reset) {
601 PP.Lex(Tok); // push | pop
602 if (Tok.is(tok::comma)) {
603 PP.Lex(Tok); // ,
604 // If we've got a comma, we either need a label or a string.
605 if (Tok.isAnyIdentifier()) {
606 SlotLabel = Tok.getIdentifierInfo()->getName();
607 PP.Lex(Tok); // identifier
608 if (Tok.is(tok::comma))
609 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000610 else if (Tok.isNot(tok::r_paren)) {
611 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
612 << PragmaName;
613 return false;
614 }
Warren Huntc3b18962014-04-08 22:30:47 +0000615 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000616 } else if (Tok.isNot(tok::r_paren)) {
617 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
618 return false;
619 }
Warren Huntc3b18962014-04-08 22:30:47 +0000620 }
621 }
622 // Grab the string literal for our section name.
623 StringLiteral *SegmentName = nullptr;
624 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000625 if (Tok.isNot(tok::string_literal)) {
626 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000627 diag::warn_pragma_expected_section_name :
628 diag::warn_pragma_expected_section_label_or_name :
629 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000630 PP.Diag(PragmaLocation, DiagID) << PragmaName;
631 return false;
632 }
633 ExprResult StringResult = ParseStringLiteralExpression();
634 if (StringResult.isInvalid())
635 return false; // Already diagnosed.
636 SegmentName = cast<StringLiteral>(StringResult.get());
637 if (SegmentName->getCharByteWidth() != 1) {
638 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
639 << PragmaName;
640 return false;
641 }
Warren Huntc3b18962014-04-08 22:30:47 +0000642 // Setting section "" has no effect
643 if (SegmentName->getLength())
644 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
645 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000646 if (Tok.isNot(tok::r_paren)) {
647 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
648 return false;
649 }
Warren Huntc3b18962014-04-08 22:30:47 +0000650 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000651 if (Tok.isNot(tok::eof)) {
652 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
653 << PragmaName;
654 return false;
655 }
Warren Huntc3b18962014-04-08 22:30:47 +0000656 PP.Lex(Tok); // eof
657 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
658 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000659 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000660}
661
Reid Kleckner1a711b12014-07-22 00:53:05 +0000662// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000663bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
664 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000665 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
666 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
667 return false;
668 }
669
Reid Kleckner1a711b12014-07-22 00:53:05 +0000670 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
671 PragmaName))
672 return false;
673
674 // Parse either the known section names or the string section name.
675 StringLiteral *SegmentName = nullptr;
676 if (Tok.isAnyIdentifier()) {
677 auto *II = Tok.getIdentifierInfo();
678 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
679 .Case("compiler", "\".CRT$XCC\"")
680 .Case("lib", "\".CRT$XCL\"")
681 .Case("user", "\".CRT$XCU\"")
682 .Default("");
683
684 if (!Section.empty()) {
685 // Pretend the user wrote the appropriate string literal here.
686 Token Toks[1];
687 Toks[0].startToken();
688 Toks[0].setKind(tok::string_literal);
689 Toks[0].setLocation(Tok.getLocation());
690 Toks[0].setLiteralData(Section.data());
691 Toks[0].setLength(Section.size());
692 SegmentName =
693 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
694 PP.Lex(Tok);
695 }
696 } else if (Tok.is(tok::string_literal)) {
697 ExprResult StringResult = ParseStringLiteralExpression();
698 if (StringResult.isInvalid())
699 return false;
700 SegmentName = cast<StringLiteral>(StringResult.get());
701 if (SegmentName->getCharByteWidth() != 1) {
702 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
703 << PragmaName;
704 return false;
705 }
706 // FIXME: Add support for the '[, func-name]' part of the pragma.
707 }
708
709 if (!SegmentName) {
710 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
711 return false;
712 }
713
714 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
715 PragmaName) ||
716 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
717 PragmaName))
718 return false;
719
720 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
721 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000722}
723
724struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000725 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000726 Token Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000727 Token Value;
728 bool HasValue;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000729 PragmaLoopHintInfo() : HasValue(false) {}
Eli Bendersky06a40422014-06-06 20:31:48 +0000730};
731
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000732bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +0000733 assert(Tok.is(tok::annot_pragma_loop_hint));
734 PragmaLoopHintInfo *Info =
735 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000736 ConsumeToken(); // The annotation token.
Eli Bendersky06a40422014-06-06 20:31:48 +0000737
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000738 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
739 Hint.PragmaNameLoc = IdentifierLoc::create(
740 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +0000741
Aaron Ballmanef940aa2014-07-31 21:24:32 +0000742 // It is possible that the loop hint has no option identifier, such as
743 // #pragma unroll(4).
744 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
745 ? Info->Option.getIdentifierInfo()
746 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000747 Hint.OptionLoc = IdentifierLoc::create(
748 Actions.Context, Info->Option.getLocation(), OptionInfo);
749
750 // Return a valid hint if pragma unroll or nounroll were specified
751 // without an argument.
752 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
753 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
754 if (!Info->HasValue && (PragmaUnroll || PragmaNoUnroll)) {
755 Hint.Range = Info->PragmaName.getLocation();
756 return true;
757 }
758
759 // If no option is specified the argument is assumed to be numeric.
760 bool StateOption = false;
761 if (OptionInfo)
762 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
763 .Case("vectorize", true)
764 .Case("interleave", true)
765 .Case("unroll", true)
766 .Default(false);
767
768 // Validate the argument.
769 if (StateOption) {
770 bool OptionUnroll = OptionInfo->isStr("unroll");
771 SourceLocation StateLoc = Info->Value.getLocation();
772 IdentifierInfo *StateInfo = Info->Value.getIdentifierInfo();
773 if (!StateInfo || ((OptionUnroll ? !StateInfo->isStr("full")
774 : !StateInfo->isStr("enable")) &&
775 !StateInfo->isStr("disable"))) {
776 Diag(StateLoc, diag::err_pragma_invalid_keyword)
777 << /*MissingArgument=*/false << /*FullKeyword=*/OptionUnroll;
778 return false;
779 }
780 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
781 } else {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000782 // FIXME: We should allow non-type template parameters for the loop hint
783 // value. See bug report #19610
784 if (Info->Value.is(tok::numeric_constant))
785 Hint.ValueExpr = Actions.ActOnNumericConstant(Info->Value).get();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000786 else {
787 Diag(Info->Value.getLocation(), diag::err_pragma_loop_numeric_value);
788 return false;
789 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000790 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000791
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +0000792 Hint.Range =
793 SourceRange(Info->PragmaName.getLocation(), Info->Value.getLocation());
794 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000795}
796
797// #pragma GCC visibility comes in two variants:
798// 'push' '(' [visibility] ')'
799// 'pop'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000800void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
801 PragmaIntroducerKind Introducer,
802 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +0000803 SourceLocation VisLoc = VisTok.getLocation();
804
805 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000806 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000807
808 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
809
Eli Friedman570024a2010-08-05 06:57:20 +0000810 const IdentifierInfo *VisType;
811 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +0000812 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +0000813 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000814 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000815 if (Tok.isNot(tok::l_paren)) {
816 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
817 << "visibility";
818 return;
819 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000820 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000821 VisType = Tok.getIdentifierInfo();
822 if (!VisType) {
823 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
824 << "visibility";
825 return;
826 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000827 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +0000828 if (Tok.isNot(tok::r_paren)) {
829 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
830 << "visibility";
831 return;
832 }
833 } else {
834 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
835 << "visibility";
836 return;
837 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +0000838 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000839 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +0000840 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
841 << "visibility";
842 return;
843 }
844
Rafael Espindola273fd772012-01-26 02:02:57 +0000845 Token *Toks = new Token[1];
846 Toks[0].startToken();
847 Toks[0].setKind(tok::annot_pragma_vis);
848 Toks[0].setLocation(VisLoc);
849 Toks[0].setAnnotationValue(
850 const_cast<void*>(static_cast<const void*>(VisType)));
851 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
852 /*OwnsTokens=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +0000853}
854
Daniel Dunbar921b9682008-10-04 19:21:03 +0000855// #pragma pack(...) comes in the following delicious flavors:
856// pack '(' [integer] ')'
857// pack '(' 'show' ')'
858// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Douglas Gregorc7d65762010-09-09 22:45:38 +0000859void PragmaPackHandler::HandlePragma(Preprocessor &PP,
860 PragmaIntroducerKind Introducer,
861 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +0000862 SourceLocation PackLoc = PackTok.getLocation();
863
864 Token Tok;
865 PP.Lex(Tok);
866 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000867 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000868 return;
869 }
870
John McCallfaf5fb42010-08-26 23:41:50 +0000871 Sema::PragmaPackKind Kind = Sema::PPK_Default;
Craig Topper161e4db2014-05-21 06:02:52 +0000872 IdentifierInfo *Name = nullptr;
Eli Friedman68be1642012-10-04 02:36:51 +0000873 Token Alignment;
874 Alignment.startToken();
Daniel Dunbar921b9682008-10-04 19:21:03 +0000875 SourceLocation LParenLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000876 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000877 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000878 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000879
880 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +0000881
882 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
883 // the push/pop stack.
884 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000885 if (PP.getLangOpts().ApplePragmaPack)
Eli Friedman055c9702011-11-02 01:53:16 +0000886 Kind = Sema::PPK_Push;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000887 } else if (Tok.is(tok::identifier)) {
888 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +0000889 if (II->isStr("show")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000890 Kind = Sema::PPK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000891 PP.Lex(Tok);
892 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000893 if (II->isStr("push")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000894 Kind = Sema::PPK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +0000895 } else if (II->isStr("pop")) {
John McCallfaf5fb42010-08-26 23:41:50 +0000896 Kind = Sema::PPK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000897 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000898 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000899 return;
Mike Stump11289f42009-09-09 15:08:12 +0000900 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000901 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000902
Daniel Dunbar921b9682008-10-04 19:21:03 +0000903 if (Tok.is(tok::comma)) {
904 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000905
Daniel Dunbar921b9682008-10-04 19:21:03 +0000906 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +0000907 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000908
909 PP.Lex(Tok);
910 } else if (Tok.is(tok::identifier)) {
911 Name = Tok.getIdentifierInfo();
912 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000913
Daniel Dunbar921b9682008-10-04 19:21:03 +0000914 if (Tok.is(tok::comma)) {
915 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +0000916
Daniel Dunbar921b9682008-10-04 19:21:03 +0000917 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000918 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000919 return;
920 }
Mike Stump11289f42009-09-09 15:08:12 +0000921
Eli Friedman68be1642012-10-04 02:36:51 +0000922 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +0000923
924 PP.Lex(Tok);
925 }
926 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000927 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000928 return;
929 }
930 }
931 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000932 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +0000933 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
934 // the push/pop stack.
935 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
936 Kind = Sema::PPK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000937 }
Daniel Dunbar921b9682008-10-04 19:21:03 +0000938
939 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +0000940 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +0000941 return;
942 }
943
Daniel Dunbar75c9be72010-05-26 23:29:06 +0000944 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000945 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +0000946 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +0000947 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
948 return;
949 }
950
Daniel Dunbar340cf242012-02-29 01:38:22 +0000951 PragmaPackInfo *Info =
952 (PragmaPackInfo*) PP.getPreprocessorAllocator().Allocate(
953 sizeof(PragmaPackInfo), llvm::alignOf<PragmaPackInfo>());
954 new (Info) PragmaPackInfo();
Eli Friedmanec52f922012-02-23 23:47:16 +0000955 Info->Kind = Kind;
956 Info->Name = Name;
Eli Friedman68be1642012-10-04 02:36:51 +0000957 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000958 Info->LParenLoc = LParenLoc;
959 Info->RParenLoc = RParenLoc;
960
Daniel Dunbar340cf242012-02-29 01:38:22 +0000961 Token *Toks =
962 (Token*) PP.getPreprocessorAllocator().Allocate(
963 sizeof(Token) * 1, llvm::alignOf<Token>());
964 new (Toks) Token();
Eli Friedmanec52f922012-02-23 23:47:16 +0000965 Toks[0].startToken();
966 Toks[0].setKind(tok::annot_pragma_pack);
967 Toks[0].setLocation(PackLoc);
968 Toks[0].setAnnotationValue(static_cast<void*>(Info));
969 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
Daniel Dunbar340cf242012-02-29 01:38:22 +0000970 /*OwnsTokens=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +0000971}
972
Fariborz Jahanian743dda42011-04-25 18:49:15 +0000973// #pragma ms_struct on
974// #pragma ms_struct off
975void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
976 PragmaIntroducerKind Introducer,
977 Token &MSStructTok) {
978 Sema::PragmaMSStructKind Kind = Sema::PMSST_OFF;
979
980 Token Tok;
981 PP.Lex(Tok);
982 if (Tok.isNot(tok::identifier)) {
983 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
984 return;
985 }
986 const IdentifierInfo *II = Tok.getIdentifierInfo();
987 if (II->isStr("on")) {
988 Kind = Sema::PMSST_ON;
989 PP.Lex(Tok);
990 }
991 else if (II->isStr("off") || II->isStr("reset"))
992 PP.Lex(Tok);
993 else {
994 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
995 return;
996 }
997
998 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +0000999 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1000 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001001 return;
1002 }
Eli Friedman68be1642012-10-04 02:36:51 +00001003
1004 Token *Toks =
1005 (Token*) PP.getPreprocessorAllocator().Allocate(
1006 sizeof(Token) * 1, llvm::alignOf<Token>());
1007 new (Toks) Token();
1008 Toks[0].startToken();
1009 Toks[0].setKind(tok::annot_pragma_msstruct);
1010 Toks[0].setLocation(MSStructTok.getLocation());
1011 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1012 static_cast<uintptr_t>(Kind)));
1013 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1014 /*OwnsTokens=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001015}
1016
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001017// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1018// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001019static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001020 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001021 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001022
1023 if (IsOptions) {
1024 PP.Lex(Tok);
1025 if (Tok.isNot(tok::identifier) ||
1026 !Tok.getIdentifierInfo()->isStr("align")) {
1027 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1028 return;
1029 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001030 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001031
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001032 PP.Lex(Tok);
1033 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001034 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1035 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001036 return;
1037 }
1038
1039 PP.Lex(Tok);
1040 if (Tok.isNot(tok::identifier)) {
1041 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001042 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001043 return;
1044 }
1045
John McCallfaf5fb42010-08-26 23:41:50 +00001046 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001047 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001048 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001049 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001050 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001051 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001052 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001053 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001054 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001055 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001056 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001057 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001058 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001059 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001060 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001061 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1062 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001063 return;
1064 }
1065
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001066 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001067 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001068 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001069 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001070 return;
1071 }
1072
Eli Friedman68be1642012-10-04 02:36:51 +00001073 Token *Toks =
1074 (Token*) PP.getPreprocessorAllocator().Allocate(
1075 sizeof(Token) * 1, llvm::alignOf<Token>());
1076 new (Toks) Token();
1077 Toks[0].startToken();
1078 Toks[0].setKind(tok::annot_pragma_align);
1079 Toks[0].setLocation(FirstTok.getLocation());
1080 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1081 static_cast<uintptr_t>(Kind)));
1082 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1083 /*OwnsTokens=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001084}
1085
Douglas Gregorc7d65762010-09-09 22:45:38 +00001086void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1087 PragmaIntroducerKind Introducer,
1088 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001089 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001090}
1091
Douglas Gregorc7d65762010-09-09 22:45:38 +00001092void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1093 PragmaIntroducerKind Introducer,
1094 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001095 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001096}
1097
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001098// #pragma unused(identifier)
Douglas Gregorc7d65762010-09-09 22:45:38 +00001099void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1100 PragmaIntroducerKind Introducer,
1101 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001102 // FIXME: Should we be expanding macros here? My guess is no.
1103 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001104
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001105 // Lex the left '('.
1106 Token Tok;
1107 PP.Lex(Tok);
1108 if (Tok.isNot(tok::l_paren)) {
1109 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1110 return;
1111 }
Mike Stump11289f42009-09-09 15:08:12 +00001112
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001113 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001114 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001115 SourceLocation RParenLoc;
1116 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001117
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001118 while (true) {
1119 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001120
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001121 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001122 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001123 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001124 LexID = false;
1125 continue;
1126 }
1127
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001128 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001129 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1130 return;
1131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001133 // We are execting a ')' or a ','.
1134 if (Tok.is(tok::comma)) {
1135 LexID = true;
1136 continue;
1137 }
Mike Stump11289f42009-09-09 15:08:12 +00001138
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001139 if (Tok.is(tok::r_paren)) {
1140 RParenLoc = Tok.getLocation();
1141 break;
1142 }
Mike Stump11289f42009-09-09 15:08:12 +00001143
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001144 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001145 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001146 return;
1147 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001148
1149 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001150 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001151 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1152 "unused";
1153 return;
1154 }
1155
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001156 // Verify that we have a location for the right parenthesis.
1157 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001158 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001159
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001160 // For each identifier token, insert into the token stream a
1161 // annot_pragma_unused token followed by the identifier token.
1162 // This allows us to cache a "#pragma unused" that occurs inside an inline
1163 // C++ member function.
1164
Daniel Dunbar340cf242012-02-29 01:38:22 +00001165 Token *Toks =
1166 (Token*) PP.getPreprocessorAllocator().Allocate(
1167 sizeof(Token) * 2 * Identifiers.size(), llvm::alignOf<Token>());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001168 for (unsigned i=0; i != Identifiers.size(); i++) {
1169 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1170 pragmaUnusedTok.startToken();
1171 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1172 pragmaUnusedTok.setLocation(UnusedLoc);
1173 idTok = Identifiers[i];
1174 }
Daniel Dunbar340cf242012-02-29 01:38:22 +00001175 PP.EnterTokenStream(Toks, 2*Identifiers.size(),
1176 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001177}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001178
1179// #pragma weak identifier
1180// #pragma weak identifier '=' identifier
Douglas Gregorc7d65762010-09-09 22:45:38 +00001181void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1182 PragmaIntroducerKind Introducer,
1183 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001184 SourceLocation WeakLoc = WeakTok.getLocation();
1185
1186 Token Tok;
1187 PP.Lex(Tok);
1188 if (Tok.isNot(tok::identifier)) {
1189 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1190 return;
1191 }
1192
Eli Friedman68be1642012-10-04 02:36:51 +00001193 Token WeakName = Tok;
1194 bool HasAlias = false;
1195 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001196
1197 PP.Lex(Tok);
1198 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001199 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001200 PP.Lex(Tok);
1201 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001202 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001203 << "weak";
1204 return;
1205 }
Eli Friedman68be1642012-10-04 02:36:51 +00001206 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001207 PP.Lex(Tok);
1208 }
1209
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001210 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001211 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1212 return;
1213 }
1214
Eli Friedman68be1642012-10-04 02:36:51 +00001215 if (HasAlias) {
1216 Token *Toks =
1217 (Token*) PP.getPreprocessorAllocator().Allocate(
1218 sizeof(Token) * 3, llvm::alignOf<Token>());
1219 Token &pragmaUnusedTok = Toks[0];
1220 pragmaUnusedTok.startToken();
1221 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1222 pragmaUnusedTok.setLocation(WeakLoc);
1223 Toks[1] = WeakName;
1224 Toks[2] = AliasName;
1225 PP.EnterTokenStream(Toks, 3,
1226 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001227 } else {
Eli Friedman68be1642012-10-04 02:36:51 +00001228 Token *Toks =
1229 (Token*) PP.getPreprocessorAllocator().Allocate(
1230 sizeof(Token) * 2, llvm::alignOf<Token>());
1231 Token &pragmaUnusedTok = Toks[0];
1232 pragmaUnusedTok.startToken();
1233 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1234 pragmaUnusedTok.setLocation(WeakLoc);
1235 Toks[1] = WeakName;
1236 PP.EnterTokenStream(Toks, 2,
1237 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001238 }
1239}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001240
David Chisnall0867d9c2012-02-18 16:12:34 +00001241// #pragma redefine_extname identifier identifier
1242void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1243 PragmaIntroducerKind Introducer,
1244 Token &RedefToken) {
1245 SourceLocation RedefLoc = RedefToken.getLocation();
1246
1247 Token Tok;
1248 PP.Lex(Tok);
1249 if (Tok.isNot(tok::identifier)) {
1250 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1251 "redefine_extname";
1252 return;
1253 }
1254
Eli Friedman68be1642012-10-04 02:36:51 +00001255 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001256 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00001257
David Chisnall0867d9c2012-02-18 16:12:34 +00001258 if (Tok.isNot(tok::identifier)) {
1259 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1260 << "redefine_extname";
1261 return;
1262 }
Eli Friedman68be1642012-10-04 02:36:51 +00001263
1264 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00001265 PP.Lex(Tok);
1266
1267 if (Tok.isNot(tok::eod)) {
1268 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1269 "redefine_extname";
1270 return;
1271 }
1272
Eli Friedman68be1642012-10-04 02:36:51 +00001273 Token *Toks =
1274 (Token*) PP.getPreprocessorAllocator().Allocate(
1275 sizeof(Token) * 3, llvm::alignOf<Token>());
1276 Token &pragmaRedefTok = Toks[0];
1277 pragmaRedefTok.startToken();
1278 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1279 pragmaRedefTok.setLocation(RedefLoc);
1280 Toks[1] = RedefName;
1281 Toks[2] = AliasName;
1282 PP.EnterTokenStream(Toks, 3,
1283 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00001284}
1285
1286
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001287void
1288PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1289 PragmaIntroducerKind Introducer,
1290 Token &Tok) {
1291 tok::OnOffSwitch OOS;
1292 if (PP.LexOnOffSwitch(OOS))
1293 return;
1294
Eli Friedman68be1642012-10-04 02:36:51 +00001295 Token *Toks =
1296 (Token*) PP.getPreprocessorAllocator().Allocate(
1297 sizeof(Token) * 1, llvm::alignOf<Token>());
1298 new (Toks) Token();
1299 Toks[0].startToken();
1300 Toks[0].setKind(tok::annot_pragma_fp_contract);
1301 Toks[0].setLocation(Tok.getLocation());
1302 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1303 static_cast<uintptr_t>(OOS)));
1304 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1305 /*OwnsTokens=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00001306}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001307
1308void
1309PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1310 PragmaIntroducerKind Introducer,
1311 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00001312 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001313 if (Tok.isNot(tok::identifier)) {
1314 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1315 "OPENCL";
1316 return;
1317 }
1318 IdentifierInfo *ename = Tok.getIdentifierInfo();
1319 SourceLocation NameLoc = Tok.getLocation();
1320
1321 PP.Lex(Tok);
1322 if (Tok.isNot(tok::colon)) {
1323 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << ename;
1324 return;
1325 }
1326
1327 PP.Lex(Tok);
1328 if (Tok.isNot(tok::identifier)) {
1329 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1330 return;
1331 }
1332 IdentifierInfo *op = Tok.getIdentifierInfo();
1333
1334 unsigned state;
1335 if (op->isStr("enable")) {
1336 state = 1;
1337 } else if (op->isStr("disable")) {
1338 state = 0;
1339 } else {
1340 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_enable_disable);
1341 return;
1342 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001343 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001344
Eli Friedman68be1642012-10-04 02:36:51 +00001345 PP.Lex(Tok);
1346 if (Tok.isNot(tok::eod)) {
1347 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1348 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001349 return;
1350 }
Eli Friedman68be1642012-10-04 02:36:51 +00001351
1352 OpenCLExtData data(ename, state);
1353 Token *Toks =
1354 (Token*) PP.getPreprocessorAllocator().Allocate(
1355 sizeof(Token) * 1, llvm::alignOf<Token>());
1356 new (Toks) Token();
1357 Toks[0].startToken();
1358 Toks[0].setKind(tok::annot_pragma_opencl_extension);
1359 Toks[0].setLocation(NameLoc);
1360 Toks[0].setAnnotationValue(data.getOpaqueValue());
1361 PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
1362 /*OwnsTokens=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00001363
1364 if (PP.getPPCallbacks())
1365 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, ename,
1366 StateLoc, state);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001367}
1368
Alexey Bataeva769e072013-03-22 06:34:35 +00001369/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
1370///
1371void
1372PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
1373 PragmaIntroducerKind Introducer,
1374 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001375 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
1376 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001377 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00001378 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
1379 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00001380 }
1381 PP.DiscardUntilEndOfDirective();
1382}
1383
1384/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
1385///
1386void
1387PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
1388 PragmaIntroducerKind Introducer,
1389 Token &FirstTok) {
1390 SmallVector<Token, 16> Pragma;
1391 Token Tok;
1392 Tok.startToken();
1393 Tok.setKind(tok::annot_pragma_openmp);
1394 Tok.setLocation(FirstTok.getLocation());
1395
1396 while (Tok.isNot(tok::eod)) {
1397 Pragma.push_back(Tok);
1398 PP.Lex(Tok);
1399 }
1400 SourceLocation EodLoc = Tok.getLocation();
1401 Tok.startToken();
1402 Tok.setKind(tok::annot_pragma_openmp_end);
1403 Tok.setLocation(EodLoc);
1404 Pragma.push_back(Tok);
1405
1406 Token *Toks = new Token[Pragma.size()];
1407 std::copy(Pragma.begin(), Pragma.end(), Toks);
1408 PP.EnterTokenStream(Toks, Pragma.size(),
1409 /*DisableMacroExpansion=*/true, /*OwnsTokens=*/true);
1410}
Reid Kleckner002562a2013-05-06 21:02:12 +00001411
David Majnemer4bb09802014-02-10 19:50:15 +00001412/// \brief Handle '#pragma pointers_to_members'
1413// The grammar for this pragma is as follows:
1414//
1415// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
1416//
1417// #pragma pointers_to_members '(' 'best_case' ')'
1418// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
1419// #pragma pointers_to_members '(' inheritance-model ')'
1420void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
1421 PragmaIntroducerKind Introducer,
1422 Token &Tok) {
1423 SourceLocation PointersToMembersLoc = Tok.getLocation();
1424 PP.Lex(Tok);
1425 if (Tok.isNot(tok::l_paren)) {
1426 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
1427 << "pointers_to_members";
1428 return;
1429 }
1430 PP.Lex(Tok);
1431 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
1432 if (!Arg) {
1433 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1434 << "pointers_to_members";
1435 return;
1436 }
1437 PP.Lex(Tok);
1438
David Majnemer86c318f2014-02-11 21:05:00 +00001439 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00001440 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001441 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00001442 } else {
1443 if (Arg->isStr("full_generality")) {
1444 if (Tok.is(tok::comma)) {
1445 PP.Lex(Tok);
1446
1447 Arg = Tok.getIdentifierInfo();
1448 if (!Arg) {
1449 PP.Diag(Tok.getLocation(),
1450 diag::err_pragma_pointers_to_members_unknown_kind)
1451 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
1452 return;
1453 }
1454 PP.Lex(Tok);
1455 } else if (Tok.is(tok::r_paren)) {
1456 // #pragma pointers_to_members(full_generality) implicitly specifies
1457 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00001458 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00001459 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001460 } else {
1461 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
1462 << "full_generality";
1463 return;
1464 }
1465 }
1466
1467 if (Arg) {
1468 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001469 RepresentationMethod =
1470 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001471 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001472 RepresentationMethod =
1473 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001474 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00001475 RepresentationMethod =
1476 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00001477 } else {
1478 PP.Diag(Tok.getLocation(),
1479 diag::err_pragma_pointers_to_members_unknown_kind)
1480 << Arg << /*HasPointerDeclaration*/ 1;
1481 return;
1482 }
1483 }
1484 }
1485
1486 if (Tok.isNot(tok::r_paren)) {
1487 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
1488 << (Arg ? Arg->getName() : "full_generality");
1489 return;
1490 }
1491
1492 PP.Lex(Tok);
1493 if (Tok.isNot(tok::eod)) {
1494 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1495 << "pointers_to_members";
1496 return;
1497 }
1498
1499 Token AnnotTok;
1500 AnnotTok.startToken();
1501 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
1502 AnnotTok.setLocation(PointersToMembersLoc);
1503 AnnotTok.setAnnotationValue(
1504 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
1505 PP.EnterToken(AnnotTok);
1506}
1507
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001508/// \brief Handle '#pragma vtordisp'
1509// The grammar for this pragma is as follows:
1510//
1511// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
1512//
1513// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
1514// #pragma vtordisp '(' 'pop' ')'
1515// #pragma vtordisp '(' ')'
1516void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
1517 PragmaIntroducerKind Introducer,
1518 Token &Tok) {
1519 SourceLocation VtorDispLoc = Tok.getLocation();
1520 PP.Lex(Tok);
1521 if (Tok.isNot(tok::l_paren)) {
1522 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
1523 return;
1524 }
1525 PP.Lex(Tok);
1526
1527 Sema::PragmaVtorDispKind Kind = Sema::PVDK_Set;
1528 const IdentifierInfo *II = Tok.getIdentifierInfo();
1529 if (II) {
1530 if (II->isStr("push")) {
1531 // #pragma vtordisp(push, mode)
1532 PP.Lex(Tok);
1533 if (Tok.isNot(tok::comma)) {
1534 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
1535 return;
1536 }
1537 PP.Lex(Tok);
1538 Kind = Sema::PVDK_Push;
1539 // not push, could be on/off
1540 } else if (II->isStr("pop")) {
1541 // #pragma vtordisp(pop)
1542 PP.Lex(Tok);
1543 Kind = Sema::PVDK_Pop;
1544 }
1545 // not push or pop, could be on/off
1546 } else {
1547 if (Tok.is(tok::r_paren)) {
1548 // #pragma vtordisp()
1549 Kind = Sema::PVDK_Reset;
1550 }
1551 }
1552
1553
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001554 uint64_t Value = 0;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001555 if (Kind == Sema::PVDK_Push || Kind == Sema::PVDK_Set) {
1556 const IdentifierInfo *II = Tok.getIdentifierInfo();
1557 if (II && II->isStr("off")) {
1558 PP.Lex(Tok);
1559 Value = 0;
1560 } else if (II && II->isStr("on")) {
1561 PP.Lex(Tok);
1562 Value = 1;
1563 } else if (Tok.is(tok::numeric_constant) &&
1564 PP.parseSimpleIntegerLiteral(Tok, Value)) {
1565 if (Value > 2) {
1566 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
1567 << 0 << 2 << "vtordisp";
1568 return;
1569 }
1570 } else {
1571 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
1572 << "vtordisp";
1573 return;
1574 }
1575 }
1576
1577 // Finish the pragma: ')' $
1578 if (Tok.isNot(tok::r_paren)) {
1579 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
1580 return;
1581 }
1582 PP.Lex(Tok);
1583 if (Tok.isNot(tok::eod)) {
1584 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1585 << "vtordisp";
1586 return;
1587 }
1588
1589 // Enter the annotation.
1590 Token AnnotTok;
1591 AnnotTok.startToken();
1592 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
1593 AnnotTok.setLocation(VtorDispLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00001594 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
1595 static_cast<uintptr_t>((Kind << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001596 PP.EnterToken(AnnotTok);
1597}
1598
Warren Huntc3b18962014-04-08 22:30:47 +00001599/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
1600/// an annotation token.
1601void PragmaMSPragma::HandlePragma(Preprocessor &PP,
1602 PragmaIntroducerKind Introducer,
1603 Token &Tok) {
1604 Token EoF, AnnotTok;
1605 EoF.startToken();
1606 EoF.setKind(tok::eof);
1607 AnnotTok.startToken();
1608 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
1609 AnnotTok.setLocation(Tok.getLocation());
1610 SmallVector<Token, 8> TokenVector;
1611 // Suck up all of the tokens before the eod.
1612 for (; Tok.isNot(tok::eod); PP.Lex(Tok))
1613 TokenVector.push_back(Tok);
1614 // Add a sentinal EoF token to the end of the list.
1615 TokenVector.push_back(EoF);
1616 // We must allocate this array with new because EnterTokenStream is going to
1617 // delete it later.
1618 Token *TokenArray = new Token[TokenVector.size()];
1619 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray);
1620 auto Value = new (PP.getPreprocessorAllocator())
1621 std::pair<Token*, size_t>(std::make_pair(TokenArray, TokenVector.size()));
1622 AnnotTok.setAnnotationValue(Value);
1623 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00001624}
1625
Aaron Ballman5d041be2013-06-04 02:07:14 +00001626/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
1627///
1628/// The syntax is:
1629/// \code
1630/// #pragma detect_mismatch("name", "value")
1631/// \endcode
1632/// Where 'name' and 'value' are quoted strings. The values are embedded in
1633/// the object file and passed along to the linker. If the linker detects a
1634/// mismatch in the object file's values for the given name, a LNK2038 error
1635/// is emitted. See MSDN for more details.
1636void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
1637 PragmaIntroducerKind Introducer,
1638 Token &Tok) {
1639 SourceLocation CommentLoc = Tok.getLocation();
1640 PP.Lex(Tok);
1641 if (Tok.isNot(tok::l_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001642 PP.Diag(CommentLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001643 return;
1644 }
1645
1646 // Read the name to embed, which must be a string literal.
1647 std::string NameString;
1648 if (!PP.LexStringLiteral(Tok, NameString,
1649 "pragma detect_mismatch",
1650 /*MacroExpansion=*/true))
1651 return;
1652
1653 // Read the comma followed by a second string literal.
1654 std::string ValueString;
1655 if (Tok.isNot(tok::comma)) {
1656 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1657 return;
1658 }
1659
1660 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
1661 /*MacroExpansion=*/true))
1662 return;
1663
1664 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001665 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00001666 return;
1667 }
1668 PP.Lex(Tok); // Eat the r_paren.
1669
1670 if (Tok.isNot(tok::eod)) {
1671 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
1672 return;
1673 }
1674
Reid Kleckner71966c92014-02-20 23:37:45 +00001675 // If the pragma is lexically sound, notify any interested PPCallbacks.
1676 if (PP.getPPCallbacks())
1677 PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
1678 ValueString);
1679
Aaron Ballman5d041be2013-06-04 02:07:14 +00001680 Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
1681}
1682
Reid Kleckner002562a2013-05-06 21:02:12 +00001683/// \brief Handle the microsoft \#pragma comment extension.
1684///
1685/// The syntax is:
1686/// \code
1687/// #pragma comment(linker, "foo")
1688/// \endcode
1689/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
1690/// "foo" is a string, which is fully macro expanded, and permits string
1691/// concatenation, embedded escape characters etc. See MSDN for more details.
1692void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
1693 PragmaIntroducerKind Introducer,
1694 Token &Tok) {
1695 SourceLocation CommentLoc = Tok.getLocation();
1696 PP.Lex(Tok);
1697 if (Tok.isNot(tok::l_paren)) {
1698 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1699 return;
1700 }
1701
1702 // Read the identifier.
1703 PP.Lex(Tok);
1704 if (Tok.isNot(tok::identifier)) {
1705 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
1706 return;
1707 }
1708
1709 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001710 IdentifierInfo *II = Tok.getIdentifierInfo();
1711 Sema::PragmaMSCommentKind Kind =
1712 llvm::StringSwitch<Sema::PragmaMSCommentKind>(II->getName())
1713 .Case("linker", Sema::PCK_Linker)
1714 .Case("lib", Sema::PCK_Lib)
1715 .Case("compiler", Sema::PCK_Compiler)
1716 .Case("exestr", Sema::PCK_ExeStr)
1717 .Case("user", Sema::PCK_User)
1718 .Default(Sema::PCK_Unknown);
1719 if (Kind == Sema::PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00001720 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
1721 return;
1722 }
1723
1724 // Read the optional string if present.
1725 PP.Lex(Tok);
1726 std::string ArgumentString;
1727 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
1728 "pragma comment",
1729 /*MacroExpansion=*/true))
1730 return;
1731
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001732 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00001733 // FIXME: If the kind is "compiler" warn if the string is present (it is
1734 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001735 // The MSDN docs say that "lib" and "linker" require a string and have a short
1736 // whitelist of linker options they support, but in practice MSVC doesn't
1737 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00001738
1739 if (Tok.isNot(tok::r_paren)) {
1740 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1741 return;
1742 }
1743 PP.Lex(Tok); // eat the r_paren.
1744
1745 if (Tok.isNot(tok::eod)) {
1746 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
1747 return;
1748 }
1749
Reid Kleckner71966c92014-02-20 23:37:45 +00001750 // If the pragma is lexically sound, notify any interested PPCallbacks.
1751 if (PP.getPPCallbacks())
1752 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
1753
Reid Klecknere43f0fe2013-05-08 13:44:39 +00001754 Actions.ActOnPragmaMSComment(Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00001755}
Dario Domizioli13a0a382014-05-23 12:13:25 +00001756
1757// #pragma clang optimize off
1758// #pragma clang optimize on
1759void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
1760 PragmaIntroducerKind Introducer,
1761 Token &FirstToken) {
1762 Token Tok;
1763 PP.Lex(Tok);
1764 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001765 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001766 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00001767 return;
1768 }
1769 if (Tok.isNot(tok::identifier)) {
1770 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1771 << PP.getSpelling(Tok);
1772 return;
1773 }
1774 const IdentifierInfo *II = Tok.getIdentifierInfo();
1775 // The only accepted values are 'on' or 'off'.
1776 bool IsOn = false;
1777 if (II->isStr("on")) {
1778 IsOn = true;
1779 } else if (!II->isStr("off")) {
1780 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
1781 << PP.getSpelling(Tok);
1782 return;
1783 }
1784 PP.Lex(Tok);
1785
1786 if (Tok.isNot(tok::eod)) {
1787 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
1788 << PP.getSpelling(Tok);
1789 return;
1790 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001791
1792 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
1793}
1794
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001795/// \brief Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001796static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
1797 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001798 PragmaLoopHintInfo &Info) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001799 if (ValueInParens) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001800 if (Tok.is(tok::r_paren)) {
1801 // Nothing between the parentheses.
1802 std::string PragmaString;
1803 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1804 PragmaString = "clang loop ";
1805 PragmaString += Option.getIdentifierInfo()->getName();
1806 } else {
1807 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1808 "Unexpected pragma name");
1809 PragmaString = "unroll";
1810 }
Mark Heffernan450c2382014-07-23 17:31:31 +00001811 // Don't try to emit what the pragma is expecting with the diagnostic
1812 // because the logic is non-trivial and we give expected values in sema
1813 // diagnostics if an invalid argument is given. Here, just note that the
1814 // pragma is missing an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001815 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00001816 << PragmaString << /*Expected=*/false;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001817 return true;
1818 }
1819 }
1820
1821 // FIXME: Value should be stored and parsed as a constant expression.
1822 Token Value = Tok;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001823 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001824
1825 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001826 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001827 if (Tok.isNot(tok::r_paren)) {
1828 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
1829 return true;
1830 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001831 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001832 }
1833
1834 Info.PragmaName = PragmaName;
1835 Info.Option = Option;
1836 Info.Value = Value;
1837 Info.HasValue = true;
1838 return false;
1839}
1840
Eli Bendersky06a40422014-06-06 20:31:48 +00001841/// \brief Handle the \#pragma clang loop directive.
1842/// #pragma clang 'loop' loop-hints
1843///
1844/// loop-hints:
1845/// loop-hint loop-hints[opt]
1846///
1847/// loop-hint:
1848/// 'vectorize' '(' loop-hint-keyword ')'
1849/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00001850/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001851/// 'vectorize_width' '(' loop-hint-value ')'
1852/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00001853/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00001854///
1855/// loop-hint-keyword:
1856/// 'enable'
1857/// 'disable'
1858///
Mark Heffernan450c2382014-07-23 17:31:31 +00001859/// unroll-hint-keyword:
1860/// 'full'
1861/// 'disable'
1862///
Eli Bendersky06a40422014-06-06 20:31:48 +00001863/// loop-hint-value:
1864/// constant-expression
1865///
1866/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
1867/// try vectorizing the instructions of the loop it precedes. Specifying
1868/// interleave(enable) or interleave_count(_value_) instructs llvm to try
1869/// interleaving multiple iterations of the loop it precedes. The width of the
1870/// vector instructions is specified by vectorize_width() and the number of
1871/// interleaved loop iterations is specified by interleave_count(). Specifying a
1872/// value of 1 effectively disables vectorization/interleaving, even if it is
1873/// possible and profitable, and 0 is invalid. The loop vectorizer currently
1874/// only works on inner loops.
1875///
Eli Bendersky86483b32014-06-11 17:56:26 +00001876/// The unroll and unroll_count directives control the concatenation
Mark Heffernan450c2382014-07-23 17:31:31 +00001877/// unroller. Specifying unroll(full) instructs llvm to try to
Eli Bendersky86483b32014-06-11 17:56:26 +00001878/// unroll the loop completely, and unroll(disable) disables unrolling
1879/// for the loop. Specifying unroll_count(_value_) instructs llvm to
1880/// try to unroll the loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00001881void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
1882 PragmaIntroducerKind Introducer,
1883 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001884 // Incoming token is "loop" from "#pragma clang loop".
1885 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00001886 SmallVector<Token, 1> TokenList;
1887
1888 // Lex the optimization option and verify it is an identifier.
1889 PP.Lex(Tok);
1890 if (Tok.isNot(tok::identifier)) {
1891 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1892 << /*MissingOption=*/true << "";
1893 return;
1894 }
1895
1896 while (Tok.is(tok::identifier)) {
1897 Token Option = Tok;
1898 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
1899
Eli Bendersky86483b32014-06-11 17:56:26 +00001900 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001901 .Case("vectorize", true)
1902 .Case("interleave", true)
1903 .Case("unroll", true)
1904 .Case("vectorize_width", true)
1905 .Case("interleave_count", true)
1906 .Case("unroll_count", true)
1907 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00001908 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001909 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
1910 << /*MissingOption=*/false << OptionInfo;
1911 return;
1912 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00001913 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001914
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001915 // Read '('
1916 if (Tok.isNot(tok::l_paren)) {
1917 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00001918 return;
1919 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001920 PP.Lex(Tok);
1921
1922 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1923 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
1924 *Info))
1925 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00001926
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001927 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00001928 Token LoopHintTok;
1929 LoopHintTok.startToken();
1930 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001931 LoopHintTok.setLocation(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00001932 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
1933 TokenList.push_back(LoopHintTok);
1934 }
1935
1936 if (Tok.isNot(tok::eod)) {
1937 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1938 << "clang loop";
1939 return;
1940 }
1941
1942 Token *TokenArray = new Token[TokenList.size()];
1943 std::copy(TokenList.begin(), TokenList.end(), TokenArray);
1944
1945 PP.EnterTokenStream(TokenArray, TokenList.size(),
1946 /*DisableMacroExpansion=*/false,
1947 /*OwnsTokens=*/true);
1948}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001949
1950/// \brief Handle the loop unroll optimization pragmas.
1951/// #pragma unroll
1952/// #pragma unroll unroll-hint-value
1953/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00001954/// #pragma nounroll
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001955///
1956/// unroll-hint-value:
1957/// constant-expression
1958///
Mark Heffernanc888e412014-07-24 18:09:38 +00001959/// Loop unrolling hints can be specified with '#pragma unroll' or
1960/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
1961/// contained in parentheses. With no argument the directive instructs llvm to
1962/// try to unroll the loop completely. A positive integer argument can be
1963/// specified to indicate the number of times the loop should be unrolled. To
1964/// maximize compatibility with other compilers the unroll count argument can be
1965/// specified with or without parentheses. Specifying, '#pragma nounroll'
1966/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001967void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
1968 PragmaIntroducerKind Introducer,
1969 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00001970 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
1971 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001972 Token PragmaName = Tok;
1973 PP.Lex(Tok);
1974 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1975 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00001976 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001977 Info->PragmaName = PragmaName;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001978 Info->HasValue = false;
Aaron Ballmand6807da2014-08-01 12:41:37 +00001979 Info->Option.startToken();
Mark Heffernanc888e412014-07-24 18:09:38 +00001980 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
1981 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1982 << "nounroll";
1983 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001984 } else {
1985 // Unroll pragma with an argument: "#pragma unroll N" or
1986 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001987 // Read '(' if it exists.
1988 bool ValueInParens = Tok.is(tok::l_paren);
1989 if (ValueInParens)
1990 PP.Lex(Tok);
1991
Aaron Ballmand0b090d2014-08-01 12:20:20 +00001992 Token Option;
1993 Option.startToken();
1994 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001995 return;
1996
1997 // In CUDA, the argument to '#pragma unroll' should not be contained in
1998 // parentheses.
1999 if (PP.getLangOpts().CUDA && ValueInParens)
2000 PP.Diag(Info->Value.getLocation(),
2001 diag::warn_pragma_unroll_cuda_value_in_parens);
2002
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002003 if (Tok.isNot(tok::eod)) {
2004 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2005 << "unroll";
2006 return;
2007 }
2008 }
2009
2010 // Generate the hint token.
2011 Token *TokenArray = new Token[1];
2012 TokenArray[0].startToken();
2013 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2014 TokenArray[0].setLocation(PragmaName.getLocation());
2015 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2016 PP.EnterTokenStream(TokenArray, 1, /*DisableMacroExpansion=*/false,
2017 /*OwnsTokens=*/true);
2018}