blob: 26b363f280c2556e95606a6ffe51c0eceb62418e [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
Hans Wennborg899ded92014-10-16 20:52:46 +000014#include "clang/AST/ASTContext.h"
Nico Weber66220292016-03-02 17:28:48 +000015#include "clang/Basic/PragmaKinds.h"
David Majnemerad2986e2014-08-14 06:35:08 +000016#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/ParseDiagnostic.h"
19#include "clang/Parse/Parser.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000020#include "clang/Parse/RAIIObjectsForParser.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000021#include "clang/Sema/LoopHint.h"
22#include "clang/Sema/Scope.h"
23#include "llvm/ADT/StringSwitch.h"
24using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000025
Reid Kleckner5b086462014-02-20 22:52:09 +000026namespace {
27
28struct PragmaAlignHandler : public PragmaHandler {
29 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Craig Topper2b07f022014-03-12 05:09:18 +000030 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
31 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000032};
33
34struct PragmaGCCVisibilityHandler : public PragmaHandler {
35 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000036 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
37 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000038};
39
40struct PragmaOptionsHandler : public PragmaHandler {
41 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000042 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
43 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000044};
45
46struct PragmaPackHandler : public PragmaHandler {
47 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000048 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
49 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000050};
51
Javed Absar2a67c9e2017-06-05 10:11:57 +000052struct PragmaClangSectionHandler : public PragmaHandler {
53 explicit PragmaClangSectionHandler(Sema &S)
54 : PragmaHandler("section"), Actions(S) {}
55 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
56 Token &FirstToken) override;
57private:
58 Sema &Actions;
59};
60
Reid Kleckner5b086462014-02-20 22:52:09 +000061struct PragmaMSStructHandler : public PragmaHandler {
62 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
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 PragmaUnusedHandler : public PragmaHandler {
68 PragmaUnusedHandler() : PragmaHandler("unused") {}
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 PragmaWeakHandler : public PragmaHandler {
74 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
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
79struct PragmaRedefineExtnameHandler : public PragmaHandler {
80 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000081 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
82 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000083};
84
85struct PragmaOpenCLExtensionHandler : public PragmaHandler {
86 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000087 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
88 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000089};
90
91
92struct PragmaFPContractHandler : public PragmaHandler {
93 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
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
Steven Wub96a3a42018-01-05 22:45:03 +000098// Pragma STDC implementations.
99
100/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
101struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
102 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
103
104 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
105 Token &Tok) override {
106 tok::OnOffSwitch OOS;
107 if (PP.LexOnOffSwitch(OOS))
108 return;
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000109 if (OOS == tok::OOS_ON) {
Steven Wub96a3a42018-01-05 22:45:03 +0000110 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000111 }
112
113 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
114 1);
115 Toks[0].startToken();
116 Toks[0].setKind(tok::annot_pragma_fenv_access);
117 Toks[0].setLocation(Tok.getLocation());
118 Toks[0].setAnnotationEndLoc(Tok.getLocation());
119 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
120 static_cast<uintptr_t>(OOS)));
121 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Steven Wub96a3a42018-01-05 22:45:03 +0000122 }
123};
124
125/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
126struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
127 PragmaSTDC_CX_LIMITED_RANGEHandler() : PragmaHandler("CX_LIMITED_RANGE") {}
128
129 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
130 Token &Tok) override {
131 tok::OnOffSwitch OOS;
132 PP.LexOnOffSwitch(OOS);
133 }
134};
135
136/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
137struct PragmaSTDC_UnknownHandler : public PragmaHandler {
138 PragmaSTDC_UnknownHandler() = default;
139
140 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
141 Token &UnknownTok) override {
142 // C99 6.10.6p2, unknown forms are not allowed.
143 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
144 }
145};
146
Adam Nemet60d32642017-04-04 21:18:36 +0000147struct PragmaFPHandler : public PragmaHandler {
148 PragmaFPHandler() : PragmaHandler("fp") {}
149 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
150 Token &FirstToken) override;
151};
152
Reid Kleckner5b086462014-02-20 22:52:09 +0000153struct PragmaNoOpenMPHandler : public PragmaHandler {
154 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +0000155 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
156 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000157};
158
159struct PragmaOpenMPHandler : public PragmaHandler {
160 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +0000161 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
162 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000163};
164
165/// PragmaCommentHandler - "\#pragma comment ...".
166struct PragmaCommentHandler : public PragmaHandler {
167 PragmaCommentHandler(Sema &Actions)
168 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000169 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
170 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000171private:
172 Sema &Actions;
173};
174
175struct PragmaDetectMismatchHandler : public PragmaHandler {
176 PragmaDetectMismatchHandler(Sema &Actions)
177 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000178 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
179 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000180private:
181 Sema &Actions;
182};
183
184struct PragmaMSPointersToMembers : public PragmaHandler {
185 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000186 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
187 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000188};
189
190struct PragmaMSVtorDisp : public PragmaHandler {
191 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000192 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
193 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000194};
195
Warren Huntc3b18962014-04-08 22:30:47 +0000196struct PragmaMSPragma : public PragmaHandler {
197 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000198 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
199 Token &FirstToken) override;
200};
201
Dario Domizioli13a0a382014-05-23 12:13:25 +0000202/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
203struct PragmaOptimizeHandler : public PragmaHandler {
204 PragmaOptimizeHandler(Sema &S)
205 : PragmaHandler("optimize"), Actions(S) {}
206 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
207 Token &FirstToken) override;
208private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000209 Sema &Actions;
210};
211
212struct PragmaLoopHintHandler : public PragmaHandler {
213 PragmaLoopHintHandler() : PragmaHandler("loop") {}
214 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
215 Token &FirstToken) override;
216};
217
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000218struct PragmaUnrollHintHandler : public PragmaHandler {
219 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
220 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
221 Token &FirstToken) override;
222};
223
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000224struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
225 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
226};
227
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000228struct PragmaMSIntrinsicHandler : public PragmaHandler {
229 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
230 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
231 Token &FirstToken) override;
232};
233
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000234struct PragmaMSOptimizeHandler : public PragmaHandler {
235 PragmaMSOptimizeHandler() : PragmaHandler("optimize") {}
236 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
237 Token &FirstToken) override;
238};
239
Justin Lebar67a78a62016-10-08 22:15:58 +0000240struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
241 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
242 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
243 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
244 Token &FirstToken) override;
245
246private:
247 Sema &Actions;
248};
249
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000250/// PragmaAttributeHandler - "\#pragma clang attribute ...".
251struct PragmaAttributeHandler : public PragmaHandler {
252 PragmaAttributeHandler(AttributeFactory &AttrFactory)
253 : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {}
254 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
255 Token &FirstToken) override;
256
257 /// A pool of attributes that were parsed in \#pragma clang attribute.
258 ParsedAttributes AttributesForPragmaAttribute;
259};
260
Eli Bendersky06a40422014-06-06 20:31:48 +0000261} // end namespace
262
263void Parser::initializePragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000264 AlignHandler.reset(new PragmaAlignHandler());
265 PP.AddPragmaHandler(AlignHandler.get());
266
267 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
268 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
269
270 OptionsHandler.reset(new PragmaOptionsHandler());
271 PP.AddPragmaHandler(OptionsHandler.get());
272
273 PackHandler.reset(new PragmaPackHandler());
274 PP.AddPragmaHandler(PackHandler.get());
275
276 MSStructHandler.reset(new PragmaMSStructHandler());
277 PP.AddPragmaHandler(MSStructHandler.get());
278
279 UnusedHandler.reset(new PragmaUnusedHandler());
280 PP.AddPragmaHandler(UnusedHandler.get());
281
282 WeakHandler.reset(new PragmaWeakHandler());
283 PP.AddPragmaHandler(WeakHandler.get());
284
285 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
286 PP.AddPragmaHandler(RedefineExtnameHandler.get());
287
288 FPContractHandler.reset(new PragmaFPContractHandler());
289 PP.AddPragmaHandler("STDC", FPContractHandler.get());
290
Steven Wub96a3a42018-01-05 22:45:03 +0000291 STDCFENVHandler.reset(new PragmaSTDC_FENV_ACCESSHandler());
292 PP.AddPragmaHandler("STDC", STDCFENVHandler.get());
293
294 STDCCXLIMITHandler.reset(new PragmaSTDC_CX_LIMITED_RANGEHandler());
295 PP.AddPragmaHandler("STDC", STDCCXLIMITHandler.get());
296
297 STDCUnknownHandler.reset(new PragmaSTDC_UnknownHandler());
298 PP.AddPragmaHandler("STDC", STDCUnknownHandler.get());
299
Javed Absar2a67c9e2017-06-05 10:11:57 +0000300 PCSectionHandler.reset(new PragmaClangSectionHandler(Actions));
301 PP.AddPragmaHandler("clang", PCSectionHandler.get());
302
Reid Kleckner5b086462014-02-20 22:52:09 +0000303 if (getLangOpts().OpenCL) {
304 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
305 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
306
307 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
308 }
309 if (getLangOpts().OpenMP)
310 OpenMPHandler.reset(new PragmaOpenMPHandler());
311 else
312 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
313 PP.AddPragmaHandler(OpenMPHandler.get());
314
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000315 if (getLangOpts().MicrosoftExt ||
316 getTargetInfo().getTriple().isOSBinFormatELF()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000317 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
318 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000319 }
320
321 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000322 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
323 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
324 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
325 PP.AddPragmaHandler(MSPointersToMembers.get());
326 MSVtorDisp.reset(new PragmaMSVtorDisp());
327 PP.AddPragmaHandler(MSVtorDisp.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000328 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000329 PP.AddPragmaHandler(MSInitSeg.get());
Warren Huntc3b18962014-04-08 22:30:47 +0000330 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
331 PP.AddPragmaHandler(MSDataSeg.get());
332 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
333 PP.AddPragmaHandler(MSBSSSeg.get());
334 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
335 PP.AddPragmaHandler(MSConstSeg.get());
336 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
337 PP.AddPragmaHandler(MSCodeSeg.get());
338 MSSection.reset(new PragmaMSPragma("section"));
339 PP.AddPragmaHandler(MSSection.get());
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000340 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
341 PP.AddPragmaHandler(MSRuntimeChecks.get());
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000342 MSIntrinsic.reset(new PragmaMSIntrinsicHandler());
343 PP.AddPragmaHandler(MSIntrinsic.get());
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000344 MSOptimize.reset(new PragmaMSOptimizeHandler());
345 PP.AddPragmaHandler(MSOptimize.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000346 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000347
Justin Lebar67a78a62016-10-08 22:15:58 +0000348 if (getLangOpts().CUDA) {
349 CUDAForceHostDeviceHandler.reset(
350 new PragmaForceCUDAHostDeviceHandler(Actions));
351 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
352 }
353
Eli Bendersky06a40422014-06-06 20:31:48 +0000354 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
355 PP.AddPragmaHandler("clang", OptimizeHandler.get());
356
357 LoopHintHandler.reset(new PragmaLoopHintHandler());
358 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000359
360 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
361 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000362
363 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
364 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Adam Nemet60d32642017-04-04 21:18:36 +0000365
David Greenc8e39242018-08-01 14:36:12 +0000366 UnrollAndJamHintHandler.reset(new PragmaUnrollHintHandler("unroll_and_jam"));
367 PP.AddPragmaHandler(UnrollAndJamHintHandler.get());
368
369 NoUnrollAndJamHintHandler.reset(
370 new PragmaUnrollHintHandler("nounroll_and_jam"));
371 PP.AddPragmaHandler(NoUnrollAndJamHintHandler.get());
372
Adam Nemet60d32642017-04-04 21:18:36 +0000373 FPHandler.reset(new PragmaFPHandler());
374 PP.AddPragmaHandler("clang", FPHandler.get());
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000375
376 AttributePragmaHandler.reset(new PragmaAttributeHandler(AttrFactory));
377 PP.AddPragmaHandler("clang", AttributePragmaHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000378}
379
380void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000381 // Remove the pragma handlers we installed.
382 PP.RemovePragmaHandler(AlignHandler.get());
383 AlignHandler.reset();
384 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
385 GCCVisibilityHandler.reset();
386 PP.RemovePragmaHandler(OptionsHandler.get());
387 OptionsHandler.reset();
388 PP.RemovePragmaHandler(PackHandler.get());
389 PackHandler.reset();
390 PP.RemovePragmaHandler(MSStructHandler.get());
391 MSStructHandler.reset();
392 PP.RemovePragmaHandler(UnusedHandler.get());
393 UnusedHandler.reset();
394 PP.RemovePragmaHandler(WeakHandler.get());
395 WeakHandler.reset();
396 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
397 RedefineExtnameHandler.reset();
398
399 if (getLangOpts().OpenCL) {
400 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
401 OpenCLExtensionHandler.reset();
402 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
403 }
404 PP.RemovePragmaHandler(OpenMPHandler.get());
405 OpenMPHandler.reset();
406
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000407 if (getLangOpts().MicrosoftExt ||
408 getTargetInfo().getTriple().isOSBinFormatELF()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000409 PP.RemovePragmaHandler(MSCommentHandler.get());
410 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000411 }
412
Javed Absar2a67c9e2017-06-05 10:11:57 +0000413 PP.RemovePragmaHandler("clang", PCSectionHandler.get());
414 PCSectionHandler.reset();
415
Yunzhong Gao99efc032015-03-23 20:41:42 +0000416 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000417 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
418 MSDetectMismatchHandler.reset();
419 PP.RemovePragmaHandler(MSPointersToMembers.get());
420 MSPointersToMembers.reset();
421 PP.RemovePragmaHandler(MSVtorDisp.get());
422 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000423 PP.RemovePragmaHandler(MSInitSeg.get());
424 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000425 PP.RemovePragmaHandler(MSDataSeg.get());
426 MSDataSeg.reset();
427 PP.RemovePragmaHandler(MSBSSSeg.get());
428 MSBSSSeg.reset();
429 PP.RemovePragmaHandler(MSConstSeg.get());
430 MSConstSeg.reset();
431 PP.RemovePragmaHandler(MSCodeSeg.get());
432 MSCodeSeg.reset();
433 PP.RemovePragmaHandler(MSSection.get());
434 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000435 PP.RemovePragmaHandler(MSRuntimeChecks.get());
436 MSRuntimeChecks.reset();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000437 PP.RemovePragmaHandler(MSIntrinsic.get());
438 MSIntrinsic.reset();
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000439 PP.RemovePragmaHandler(MSOptimize.get());
440 MSOptimize.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000441 }
442
Justin Lebar67a78a62016-10-08 22:15:58 +0000443 if (getLangOpts().CUDA) {
444 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
445 CUDAForceHostDeviceHandler.reset();
446 }
447
Reid Kleckner5b086462014-02-20 22:52:09 +0000448 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
449 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000450
Steven Wub96a3a42018-01-05 22:45:03 +0000451 PP.RemovePragmaHandler("STDC", STDCFENVHandler.get());
452 STDCFENVHandler.reset();
453
454 PP.RemovePragmaHandler("STDC", STDCCXLIMITHandler.get());
455 STDCCXLIMITHandler.reset();
456
457 PP.RemovePragmaHandler("STDC", STDCUnknownHandler.get());
458 STDCUnknownHandler.reset();
459
Eli Bendersky06a40422014-06-06 20:31:48 +0000460 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
461 OptimizeHandler.reset();
462
463 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
464 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000465
466 PP.RemovePragmaHandler(UnrollHintHandler.get());
467 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000468
469 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
470 NoUnrollHintHandler.reset();
Adam Nemet60d32642017-04-04 21:18:36 +0000471
David Greenc8e39242018-08-01 14:36:12 +0000472 PP.RemovePragmaHandler(UnrollAndJamHintHandler.get());
473 UnrollAndJamHintHandler.reset();
474
475 PP.RemovePragmaHandler(NoUnrollAndJamHintHandler.get());
476 NoUnrollAndJamHintHandler.reset();
477
Adam Nemet60d32642017-04-04 21:18:36 +0000478 PP.RemovePragmaHandler("clang", FPHandler.get());
479 FPHandler.reset();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000480
481 PP.RemovePragmaHandler("clang", AttributePragmaHandler.get());
482 AttributePragmaHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000483}
484
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000485/// Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000486///
487/// Each annot_pragma_unused is followed by the argument token so e.g.
488/// "#pragma unused(x,y)" becomes:
489/// annot_pragma_unused 'x' annot_pragma_unused 'y'
490void Parser::HandlePragmaUnused() {
491 assert(Tok.is(tok::annot_pragma_unused));
Richard Smithaf3b3252017-05-18 19:21:48 +0000492 SourceLocation UnusedLoc = ConsumeAnnotationToken();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000493 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
494 ConsumeToken(); // The argument token.
495}
Eli Friedman570024a2010-08-05 06:57:20 +0000496
Rafael Espindola273fd772012-01-26 02:02:57 +0000497void Parser::HandlePragmaVisibility() {
498 assert(Tok.is(tok::annot_pragma_vis));
499 const IdentifierInfo *VisType =
500 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
Richard Smithaf3b3252017-05-18 19:21:48 +0000501 SourceLocation VisLoc = ConsumeAnnotationToken();
Rafael Espindola273fd772012-01-26 02:02:57 +0000502 Actions.ActOnPragmaVisibility(VisType, VisLoc);
503}
504
Benjamin Kramere003ca22015-10-28 13:54:16 +0000505namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000506struct PragmaPackInfo {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000507 Sema::PragmaMsStackAction Action;
508 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000509 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000510};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000511} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000512
513void Parser::HandlePragmaPack() {
514 assert(Tok.is(tok::annot_pragma_pack));
515 PragmaPackInfo *Info =
516 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
Alex Lorenz45b40142017-07-28 14:41:21 +0000517 SourceLocation PragmaLoc = Tok.getLocation();
Eli Friedman68be1642012-10-04 02:36:51 +0000518 ExprResult Alignment;
519 if (Info->Alignment.is(tok::numeric_constant)) {
520 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
Alex Lorenz45b40142017-07-28 14:41:21 +0000521 if (Alignment.isInvalid()) {
522 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000523 return;
Alex Lorenz45b40142017-07-28 14:41:21 +0000524 }
Eli Friedman68be1642012-10-04 02:36:51 +0000525 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000526 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
527 Alignment.get());
Alex Lorenz45b40142017-07-28 14:41:21 +0000528 // Consume the token after processing the pragma to enable pragma-specific
529 // #include warnings.
530 ConsumeAnnotationToken();
Eli Friedmanec52f922012-02-23 23:47:16 +0000531}
532
Eli Friedman68be1642012-10-04 02:36:51 +0000533void Parser::HandlePragmaMSStruct() {
534 assert(Tok.is(tok::annot_pragma_msstruct));
Nico Weber779355f2016-03-02 23:22:00 +0000535 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
536 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Eli Friedman68be1642012-10-04 02:36:51 +0000537 Actions.ActOnPragmaMSStruct(Kind);
Richard Smithaf3b3252017-05-18 19:21:48 +0000538 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000539}
540
541void Parser::HandlePragmaAlign() {
542 assert(Tok.is(tok::annot_pragma_align));
543 Sema::PragmaOptionsAlignKind Kind =
544 static_cast<Sema::PragmaOptionsAlignKind>(
545 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Alex Lorenz692821a2018-02-08 21:20:43 +0000546 Actions.ActOnPragmaOptionsAlign(Kind, Tok.getLocation());
547 // Consume the token after processing the pragma to enable pragma-specific
548 // #include warnings.
549 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000550}
551
Richard Smithba3a4f92016-01-12 21:59:26 +0000552void Parser::HandlePragmaDump() {
553 assert(Tok.is(tok::annot_pragma_dump));
554 IdentifierInfo *II =
555 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
556 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
Richard Smithaf3b3252017-05-18 19:21:48 +0000557 ConsumeAnnotationToken();
Richard Smithba3a4f92016-01-12 21:59:26 +0000558}
559
Eli Friedman68be1642012-10-04 02:36:51 +0000560void Parser::HandlePragmaWeak() {
561 assert(Tok.is(tok::annot_pragma_weak));
Richard Smithaf3b3252017-05-18 19:21:48 +0000562 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000563 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
564 Tok.getLocation());
565 ConsumeToken(); // The weak name.
566}
567
568void Parser::HandlePragmaWeakAlias() {
569 assert(Tok.is(tok::annot_pragma_weakalias));
Richard Smithaf3b3252017-05-18 19:21:48 +0000570 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000571 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
572 SourceLocation WeakNameLoc = Tok.getLocation();
573 ConsumeToken();
574 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
575 SourceLocation AliasNameLoc = Tok.getLocation();
576 ConsumeToken();
577 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
578 WeakNameLoc, AliasNameLoc);
579
580}
581
582void Parser::HandlePragmaRedefineExtname() {
583 assert(Tok.is(tok::annot_pragma_redefine_extname));
Richard Smithaf3b3252017-05-18 19:21:48 +0000584 SourceLocation RedefLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000585 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
586 SourceLocation RedefNameLoc = Tok.getLocation();
587 ConsumeToken();
588 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
589 SourceLocation AliasNameLoc = Tok.getLocation();
590 ConsumeToken();
591 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
592 RedefNameLoc, AliasNameLoc);
593}
594
595void Parser::HandlePragmaFPContract() {
596 assert(Tok.is(tok::annot_pragma_fp_contract));
597 tok::OnOffSwitch OOS =
598 static_cast<tok::OnOffSwitch>(
599 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Adam Nemet60d32642017-04-04 21:18:36 +0000600
601 LangOptions::FPContractModeKind FPC;
602 switch (OOS) {
603 case tok::OOS_ON:
604 FPC = LangOptions::FPC_On;
605 break;
606 case tok::OOS_OFF:
607 FPC = LangOptions::FPC_Off;
608 break;
609 case tok::OOS_DEFAULT:
610 FPC = getLangOpts().getDefaultFPContractMode();
611 break;
612 }
613
614 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +0000615 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000616}
617
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000618void Parser::HandlePragmaFEnvAccess() {
619 assert(Tok.is(tok::annot_pragma_fenv_access));
620 tok::OnOffSwitch OOS =
621 static_cast<tok::OnOffSwitch>(
622 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
623
624 LangOptions::FEnvAccessModeKind FPC;
625 switch (OOS) {
626 case tok::OOS_ON:
627 FPC = LangOptions::FEA_On;
628 break;
629 case tok::OOS_OFF:
630 FPC = LangOptions::FEA_Off;
631 break;
632 case tok::OOS_DEFAULT: // FIXME: Add this cli option when it makes sense.
633 FPC = LangOptions::FEA_Off;
634 break;
635 }
636
637 Actions.ActOnPragmaFEnvAccess(FPC);
638 ConsumeAnnotationToken();
639}
640
641
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000642StmtResult Parser::HandlePragmaCaptured()
643{
644 assert(Tok.is(tok::annot_pragma_captured));
Richard Smithaf3b3252017-05-18 19:21:48 +0000645 ConsumeAnnotationToken();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000646
647 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000648 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000649 return StmtError();
650 }
651
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000652 SourceLocation Loc = Tok.getLocation();
653
Momchil Velikov57c681f2017-08-10 15:43:06 +0000654 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope |
655 Scope::CompoundStmtScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000656 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
657 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000658
659 StmtResult R = ParseCompoundStatement();
660 CapturedRegionScope.Exit();
661
662 if (R.isInvalid()) {
663 Actions.ActOnCapturedRegionError();
664 return StmtError();
665 }
666
667 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000668}
669
Eli Friedman68be1642012-10-04 02:36:51 +0000670namespace {
Yaxun Liu5b746652016-12-18 05:18:55 +0000671 enum OpenCLExtState : char {
672 Disable, Enable, Begin, End
673 };
674 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
Eli Friedman68be1642012-10-04 02:36:51 +0000675}
676
677void Parser::HandlePragmaOpenCLExtension() {
678 assert(Tok.is(tok::annot_pragma_opencl_extension));
Yaxun Liu5b746652016-12-18 05:18:55 +0000679 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
680 auto State = Data->second;
681 auto Ident = Data->first;
Eli Friedman68be1642012-10-04 02:36:51 +0000682 SourceLocation NameLoc = Tok.getLocation();
Richard Smithaf3b3252017-05-18 19:21:48 +0000683 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000684
Yaxun Liu5b746652016-12-18 05:18:55 +0000685 auto &Opt = Actions.getOpenCLOptions();
686 auto Name = Ident->getName();
Eli Friedman68be1642012-10-04 02:36:51 +0000687 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
688 // overriding all previously issued extension directives, but only if the
689 // behavior is set to disable."
Yaxun Liu5b746652016-12-18 05:18:55 +0000690 if (Name == "all") {
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000691 if (State == Disable) {
Yaxun Liu5b746652016-12-18 05:18:55 +0000692 Opt.disableAll();
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000693 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
694 } else {
Yaxun Liu5b746652016-12-18 05:18:55 +0000695 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000696 }
Yaxun Liu5b746652016-12-18 05:18:55 +0000697 } else if (State == Begin) {
698 if (!Opt.isKnown(Name) ||
699 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
700 Opt.support(Name);
701 }
702 Actions.setCurrentOpenCLExtension(Name);
703 } else if (State == End) {
704 if (Name != Actions.getCurrentOpenCLExtension())
705 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
706 Actions.setCurrentOpenCLExtension("");
707 } else if (!Opt.isKnown(Name))
708 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
709 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
710 Opt.enable(Name, State == Enable);
711 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
712 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
713 else
714 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
Eli Friedman68be1642012-10-04 02:36:51 +0000715}
716
David Majnemer4bb09802014-02-10 19:50:15 +0000717void Parser::HandlePragmaMSPointersToMembers() {
718 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000719 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
720 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000721 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Richard Smithaf3b3252017-05-18 19:21:48 +0000722 SourceLocation PragmaLoc = ConsumeAnnotationToken();
David Majnemer4bb09802014-02-10 19:50:15 +0000723 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
724}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000725
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000726void Parser::HandlePragmaMSVtorDisp() {
727 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
728 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
Denis Zobnin2290dac2016-04-29 11:27:00 +0000729 Sema::PragmaMsStackAction Action =
730 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000731 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
Richard Smithaf3b3252017-05-18 19:21:48 +0000732 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Denis Zobnin2290dac2016-04-29 11:27:00 +0000733 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000734}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000735
Warren Huntc3b18962014-04-08 22:30:47 +0000736void Parser::HandlePragmaMSPragma() {
737 assert(Tok.is(tok::annot_pragma_ms_pragma));
738 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000739 auto TheTokens =
740 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
741 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Richard Smithaf3b3252017-05-18 19:21:48 +0000742 SourceLocation PragmaLocation = ConsumeAnnotationToken();
Warren Huntc3b18962014-04-08 22:30:47 +0000743 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000744 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000745 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000746
Warren Huntc3b18962014-04-08 22:30:47 +0000747 // Figure out which #pragma we're dealing with. The switch has no default
748 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000749 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000750 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
751 .Case("data_seg", &Parser::HandlePragmaMSSegment)
752 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
753 .Case("const_seg", &Parser::HandlePragmaMSSegment)
754 .Case("code_seg", &Parser::HandlePragmaMSSegment)
755 .Case("section", &Parser::HandlePragmaMSSection)
756 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000757
758 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
759 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
760 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000761 while (Tok.isNot(tok::eof))
762 PP.Lex(Tok);
763 PP.Lex(Tok);
764 }
765}
766
Reid Kleckner722b1df2014-07-18 00:13:16 +0000767bool Parser::HandlePragmaMSSection(StringRef PragmaName,
768 SourceLocation PragmaLocation) {
769 if (Tok.isNot(tok::l_paren)) {
770 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
771 return false;
772 }
Warren Huntc3b18962014-04-08 22:30:47 +0000773 PP.Lex(Tok); // (
774 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000775 if (Tok.isNot(tok::string_literal)) {
776 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
777 << PragmaName;
778 return false;
779 }
780 ExprResult StringResult = ParseStringLiteralExpression();
781 if (StringResult.isInvalid())
782 return false; // Already diagnosed.
783 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
784 if (SegmentName->getCharByteWidth() != 1) {
785 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
786 << PragmaName;
787 return false;
788 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000789 int SectionFlags = ASTContext::PSF_Read;
790 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000791 while (Tok.is(tok::comma)) {
792 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000793 // Ignore "long" and "short".
794 // They are undocumented, but widely used, section attributes which appear
795 // to do nothing.
796 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
797 PP.Lex(Tok); // long/short
798 continue;
799 }
800
Reid Kleckner722b1df2014-07-18 00:13:16 +0000801 if (!Tok.isAnyIdentifier()) {
802 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
803 << PragmaName;
804 return false;
805 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000806 ASTContext::PragmaSectionFlag Flag =
807 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000808 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000809 .Case("read", ASTContext::PSF_Read)
810 .Case("write", ASTContext::PSF_Write)
811 .Case("execute", ASTContext::PSF_Execute)
812 .Case("shared", ASTContext::PSF_Invalid)
813 .Case("nopage", ASTContext::PSF_Invalid)
814 .Case("nocache", ASTContext::PSF_Invalid)
815 .Case("discard", ASTContext::PSF_Invalid)
816 .Case("remove", ASTContext::PSF_Invalid)
817 .Default(ASTContext::PSF_None);
818 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
819 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000820 ? diag::warn_pragma_invalid_specific_action
821 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000822 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000823 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000824 }
825 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000826 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000827 PP.Lex(Tok); // Identifier
828 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000829 // If no section attributes are specified, the section will be marked as
830 // read/write.
831 if (SectionFlagsAreDefault)
832 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000833 if (Tok.isNot(tok::r_paren)) {
834 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
835 return false;
836 }
Warren Huntc3b18962014-04-08 22:30:47 +0000837 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000838 if (Tok.isNot(tok::eof)) {
839 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
840 << PragmaName;
841 return false;
842 }
Warren Huntc3b18962014-04-08 22:30:47 +0000843 PP.Lex(Tok); // eof
844 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000845 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000846}
847
Reid Kleckner722b1df2014-07-18 00:13:16 +0000848bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
849 SourceLocation PragmaLocation) {
850 if (Tok.isNot(tok::l_paren)) {
851 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
852 return false;
853 }
Warren Huntc3b18962014-04-08 22:30:47 +0000854 PP.Lex(Tok); // (
855 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000856 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000857 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000858 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000859 if (PushPop == "push")
860 Action = Sema::PSK_Push;
861 else if (PushPop == "pop")
862 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000863 else {
864 PP.Diag(PragmaLocation,
865 diag::warn_pragma_expected_section_push_pop_or_name)
866 << PragmaName;
867 return false;
868 }
Warren Huntc3b18962014-04-08 22:30:47 +0000869 if (Action != Sema::PSK_Reset) {
870 PP.Lex(Tok); // push | pop
871 if (Tok.is(tok::comma)) {
872 PP.Lex(Tok); // ,
873 // If we've got a comma, we either need a label or a string.
874 if (Tok.isAnyIdentifier()) {
875 SlotLabel = Tok.getIdentifierInfo()->getName();
876 PP.Lex(Tok); // identifier
877 if (Tok.is(tok::comma))
878 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000879 else if (Tok.isNot(tok::r_paren)) {
880 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
881 << PragmaName;
882 return false;
883 }
Warren Huntc3b18962014-04-08 22:30:47 +0000884 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000885 } else if (Tok.isNot(tok::r_paren)) {
886 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
887 return false;
888 }
Warren Huntc3b18962014-04-08 22:30:47 +0000889 }
890 }
891 // Grab the string literal for our section name.
892 StringLiteral *SegmentName = nullptr;
893 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000894 if (Tok.isNot(tok::string_literal)) {
895 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000896 diag::warn_pragma_expected_section_name :
897 diag::warn_pragma_expected_section_label_or_name :
898 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000899 PP.Diag(PragmaLocation, DiagID) << PragmaName;
900 return false;
901 }
902 ExprResult StringResult = ParseStringLiteralExpression();
903 if (StringResult.isInvalid())
904 return false; // Already diagnosed.
905 SegmentName = cast<StringLiteral>(StringResult.get());
906 if (SegmentName->getCharByteWidth() != 1) {
907 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
908 << PragmaName;
909 return false;
910 }
Warren Huntc3b18962014-04-08 22:30:47 +0000911 // Setting section "" has no effect
912 if (SegmentName->getLength())
913 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
914 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000915 if (Tok.isNot(tok::r_paren)) {
916 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
917 return false;
918 }
Warren Huntc3b18962014-04-08 22:30:47 +0000919 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000920 if (Tok.isNot(tok::eof)) {
921 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
922 << PragmaName;
923 return false;
924 }
Warren Huntc3b18962014-04-08 22:30:47 +0000925 PP.Lex(Tok); // eof
926 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
927 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000928 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000929}
930
Reid Kleckner1a711b12014-07-22 00:53:05 +0000931// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000932bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
933 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000934 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
935 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
936 return false;
937 }
938
Reid Kleckner1a711b12014-07-22 00:53:05 +0000939 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
940 PragmaName))
941 return false;
942
943 // Parse either the known section names or the string section name.
944 StringLiteral *SegmentName = nullptr;
945 if (Tok.isAnyIdentifier()) {
946 auto *II = Tok.getIdentifierInfo();
947 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
948 .Case("compiler", "\".CRT$XCC\"")
949 .Case("lib", "\".CRT$XCL\"")
950 .Case("user", "\".CRT$XCU\"")
951 .Default("");
952
953 if (!Section.empty()) {
954 // Pretend the user wrote the appropriate string literal here.
955 Token Toks[1];
956 Toks[0].startToken();
957 Toks[0].setKind(tok::string_literal);
958 Toks[0].setLocation(Tok.getLocation());
959 Toks[0].setLiteralData(Section.data());
960 Toks[0].setLength(Section.size());
961 SegmentName =
962 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
963 PP.Lex(Tok);
964 }
965 } else if (Tok.is(tok::string_literal)) {
966 ExprResult StringResult = ParseStringLiteralExpression();
967 if (StringResult.isInvalid())
968 return false;
969 SegmentName = cast<StringLiteral>(StringResult.get());
970 if (SegmentName->getCharByteWidth() != 1) {
971 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
972 << PragmaName;
973 return false;
974 }
975 // FIXME: Add support for the '[, func-name]' part of the pragma.
976 }
977
978 if (!SegmentName) {
979 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
980 return false;
981 }
982
983 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
984 PragmaName) ||
985 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
986 PragmaName))
987 return false;
988
989 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
990 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000991}
992
Benjamin Kramere003ca22015-10-28 13:54:16 +0000993namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000994struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000995 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000996 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000997 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +0000998};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000999} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +00001000
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001001static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
1002 std::string PragmaString;
1003 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1004 PragmaString = "clang loop ";
1005 PragmaString += Option.getIdentifierInfo()->getName();
David Greenc8e39242018-08-01 14:36:12 +00001006 } else if (PragmaName.getIdentifierInfo()->getName() == "unroll_and_jam") {
1007 PragmaString = "unroll_and_jam";
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001008 } else {
1009 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1010 "Unexpected pragma name");
1011 PragmaString = "unroll";
1012 }
1013 return PragmaString;
1014}
1015
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001016bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001017 assert(Tok.is(tok::annot_pragma_loop_hint));
1018 PragmaLoopHintInfo *Info =
1019 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
1020
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001021 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
1022 Hint.PragmaNameLoc = IdentifierLoc::create(
1023 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +00001024
Aaron Ballmanef940aa2014-07-31 21:24:32 +00001025 // It is possible that the loop hint has no option identifier, such as
1026 // #pragma unroll(4).
1027 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
1028 ? Info->Option.getIdentifierInfo()
1029 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001030 Hint.OptionLoc = IdentifierLoc::create(
1031 Actions.Context, Info->Option.getLocation(), OptionInfo);
1032
David Blaikie2eabcc92016-02-09 18:52:09 +00001033 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001034
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001035 // Return a valid hint if pragma unroll or nounroll were specified
1036 // without an argument.
1037 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
1038 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Greenc8e39242018-08-01 14:36:12 +00001039 bool PragmaUnrollAndJam = PragmaNameInfo->getName() == "unroll_and_jam";
1040 bool PragmaNoUnrollAndJam = PragmaNameInfo->getName() == "nounroll_and_jam";
1041 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll || PragmaUnrollAndJam ||
1042 PragmaNoUnrollAndJam)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001043 ConsumeAnnotationToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001044 Hint.Range = Info->PragmaName.getLocation();
1045 return true;
1046 }
1047
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001048 // The constant expression is always followed by an eof token, which increases
1049 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +00001050 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001051 "PragmaLoopHintInfo::Toks must contain at least one token.");
1052
1053 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +00001054 bool OptionUnroll = false;
David Greenc8e39242018-08-01 14:36:12 +00001055 bool OptionUnrollAndJam = false;
Adam Nemet2de463e2016-06-14 12:04:26 +00001056 bool OptionDistribute = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001057 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +00001058 if (OptionInfo) { // Pragma Unroll does not specify an option.
1059 OptionUnroll = OptionInfo->isStr("unroll");
David Greenc8e39242018-08-01 14:36:12 +00001060 OptionUnrollAndJam = OptionInfo->isStr("unroll_and_jam");
Adam Nemet2de463e2016-06-14 12:04:26 +00001061 OptionDistribute = OptionInfo->isStr("distribute");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001062 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
1063 .Case("vectorize", true)
1064 .Case("interleave", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00001065 .Default(false) ||
David Greenc8e39242018-08-01 14:36:12 +00001066 OptionUnroll || OptionUnrollAndJam || OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001067 }
1068
David Greenc8e39242018-08-01 14:36:12 +00001069 bool AssumeSafetyArg =
1070 !OptionUnroll && !OptionUnrollAndJam && !OptionDistribute;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001071 // Verify loop hint has an argument.
1072 if (Toks[0].is(tok::eof)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001073 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001074 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
David Greenc8e39242018-08-01 14:36:12 +00001075 << /*StateArgument=*/StateOption
1076 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
Adam Nemet2de463e2016-06-14 12:04:26 +00001077 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001078 return false;
1079 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001080
1081 // Validate the argument.
1082 if (StateOption) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001083 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001084 SourceLocation StateLoc = Toks[0].getLocation();
1085 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Adam Nemet50de4e82016-04-19 22:17:45 +00001086
David Greenc8e39242018-08-01 14:36:12 +00001087 bool Valid =
1088 StateInfo && llvm::StringSwitch<bool>(StateInfo->getName())
1089 .Cases("enable", "disable", true)
1090 .Case("full", OptionUnroll || OptionUnrollAndJam)
1091 .Case("assume_safety", AssumeSafetyArg)
1092 .Default(false);
Adam Nemet50de4e82016-04-19 22:17:45 +00001093 if (!Valid) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001094 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
David Greenc8e39242018-08-01 14:36:12 +00001095 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
Adam Nemet2de463e2016-06-14 12:04:26 +00001096 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001097 return false;
1098 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001099 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001100 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1101 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001102 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
1103 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001104 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +00001105 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001106 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001107
1108 ExprResult R = ParseConstantExpression();
1109
1110 // Tokens following an error in an ill-formed constant expression will
1111 // remain in the token stream and must be removed.
1112 if (Tok.isNot(tok::eof)) {
1113 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1114 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1115 while (Tok.isNot(tok::eof))
1116 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001117 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001118
1119 ConsumeToken(); // Consume the constant expression eof terminator.
1120
1121 if (R.isInvalid() ||
1122 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
1123 return false;
1124
1125 // Argument is a constant expression with an integer type.
1126 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001127 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001128
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001129 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +00001130 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001131 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +00001132}
1133
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001134namespace {
1135struct PragmaAttributeInfo {
Erik Pilkingtonb287a012018-10-29 03:24:16 +00001136 enum ActionType { Push, Pop };
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001137 ParsedAttributes &Attributes;
1138 ActionType Action;
1139 ArrayRef<Token> Tokens;
1140
1141 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1142};
1143
1144#include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1145
1146} // end anonymous namespace
1147
1148static StringRef getIdentifier(const Token &Tok) {
1149 if (Tok.is(tok::identifier))
1150 return Tok.getIdentifierInfo()->getName();
1151 const char *S = tok::getKeywordSpelling(Tok.getKind());
1152 if (!S)
1153 return "";
1154 return S;
1155}
1156
1157static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1158 using namespace attr;
1159 switch (Rule) {
1160#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1161 case Value: \
1162 return IsAbstract;
1163#include "clang/Basic/AttrSubMatchRulesList.inc"
1164 }
1165 llvm_unreachable("Invalid attribute subject match rule");
1166 return false;
1167}
1168
1169static void diagnoseExpectedAttributeSubjectSubRule(
1170 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1171 SourceLocation SubRuleLoc) {
1172 auto Diagnostic =
1173 PRef.Diag(SubRuleLoc,
1174 diag::err_pragma_attribute_expected_subject_sub_identifier)
1175 << PrimaryRuleName;
1176 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1177 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1178 else
1179 Diagnostic << /*SubRulesSupported=*/0;
1180}
1181
1182static void diagnoseUnknownAttributeSubjectSubRule(
1183 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1184 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1185
1186 auto Diagnostic =
1187 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1188 << SubRuleName << PrimaryRuleName;
1189 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1190 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1191 else
1192 Diagnostic << /*SubRulesSupported=*/0;
1193}
1194
1195bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1196 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1197 SourceLocation &LastMatchRuleEndLoc) {
1198 bool IsAny = false;
1199 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1200 if (getIdentifier(Tok) == "any") {
1201 AnyLoc = ConsumeToken();
1202 IsAny = true;
1203 if (AnyParens.expectAndConsume())
1204 return true;
1205 }
1206
1207 do {
1208 // Parse the subject matcher rule.
1209 StringRef Name = getIdentifier(Tok);
1210 if (Name.empty()) {
1211 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1212 return true;
1213 }
1214 std::pair<Optional<attr::SubjectMatchRule>,
1215 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1216 Rule = isAttributeSubjectMatchRule(Name);
1217 if (!Rule.first) {
1218 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1219 return true;
1220 }
1221 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1222 SourceLocation RuleLoc = ConsumeToken();
1223
1224 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1225 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1226 if (Parens.expectAndConsume())
1227 return true;
1228 } else if (Parens.consumeOpen()) {
1229 if (!SubjectMatchRules
1230 .insert(
1231 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1232 .second)
1233 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1234 << Name
1235 << FixItHint::CreateRemoval(SourceRange(
1236 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1237 LastMatchRuleEndLoc = RuleLoc;
1238 continue;
1239 }
1240
1241 // Parse the sub-rules.
1242 StringRef SubRuleName = getIdentifier(Tok);
1243 if (SubRuleName.empty()) {
1244 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1245 Tok.getLocation());
1246 return true;
1247 }
1248 attr::SubjectMatchRule SubRule;
1249 if (SubRuleName == "unless") {
1250 SourceLocation SubRuleLoc = ConsumeToken();
1251 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1252 if (Parens.expectAndConsume())
1253 return true;
1254 SubRuleName = getIdentifier(Tok);
1255 if (SubRuleName.empty()) {
1256 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1257 SubRuleLoc);
1258 return true;
1259 }
1260 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1261 if (!SubRuleOrNone) {
1262 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1263 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1264 SubRuleUnlessName, SubRuleLoc);
1265 return true;
1266 }
1267 SubRule = *SubRuleOrNone;
1268 ConsumeToken();
1269 if (Parens.consumeClose())
1270 return true;
1271 } else {
1272 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1273 if (!SubRuleOrNone) {
1274 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1275 SubRuleName, Tok.getLocation());
1276 return true;
1277 }
1278 SubRule = *SubRuleOrNone;
1279 ConsumeToken();
1280 }
1281 SourceLocation RuleEndLoc = Tok.getLocation();
1282 LastMatchRuleEndLoc = RuleEndLoc;
1283 if (Parens.consumeClose())
1284 return true;
1285 if (!SubjectMatchRules
1286 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1287 .second) {
1288 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1289 << attr::getSubjectMatchRuleSpelling(SubRule)
1290 << FixItHint::CreateRemoval(SourceRange(
1291 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1292 continue;
1293 }
1294 } while (IsAny && TryConsumeToken(tok::comma));
1295
1296 if (IsAny)
1297 if (AnyParens.consumeClose())
1298 return true;
1299
1300 return false;
1301}
1302
1303namespace {
1304
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001305/// Describes the stage at which attribute subject rule parsing was interrupted.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001306enum class MissingAttributeSubjectRulesRecoveryPoint {
1307 Comma,
1308 ApplyTo,
1309 Equals,
1310 Any,
1311 None,
1312};
1313
1314MissingAttributeSubjectRulesRecoveryPoint
1315getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1316 if (const auto *II = Tok.getIdentifierInfo()) {
1317 if (II->isStr("apply_to"))
1318 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1319 if (II->isStr("any"))
1320 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1321 }
1322 if (Tok.is(tok::equal))
1323 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1324 return MissingAttributeSubjectRulesRecoveryPoint::None;
1325}
1326
1327/// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1328/// suggests the possible attribute subject rules in a fix-it together with
1329/// any other missing tokens.
1330DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
Erich Keanee891aa92018-07-13 15:07:47 +00001331 unsigned DiagID, ParsedAttr &Attribute,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001332 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1333 SourceLocation Loc = PRef.getEndOfPreviousToken();
1334 if (Loc.isInvalid())
1335 Loc = PRef.getCurToken().getLocation();
1336 auto Diagnostic = PRef.Diag(Loc, DiagID);
1337 std::string FixIt;
1338 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1339 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1340 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1341 FixIt = ", ";
1342 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1343 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1344 FixIt += "apply_to";
1345 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1346 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1347 FixIt += " = ";
1348 SourceRange FixItRange(Loc);
1349 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1350 // Gather the subject match rules that are supported by the attribute.
1351 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1352 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1353 if (SubjectMatchRuleSet.empty()) {
1354 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1355 // placeholders will be supported by the fix-its.
1356 return Diagnostic;
1357 }
1358 FixIt += "any(";
1359 bool NeedsComma = false;
1360 for (const auto &I : SubjectMatchRuleSet) {
1361 // Ensure that the missing rule is reported in the fix-it only when it's
1362 // supported in the current language mode.
1363 if (!I.second)
1364 continue;
1365 if (NeedsComma)
1366 FixIt += ", ";
1367 else
1368 NeedsComma = true;
1369 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1370 }
1371 FixIt += ")";
1372 // Check if we need to remove the range
1373 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1374 FixItRange.setEnd(PRef.getCurToken().getLocation());
1375 }
1376 if (FixItRange.getBegin() == FixItRange.getEnd())
1377 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1378 else
1379 Diagnostic << FixItHint::CreateReplacement(
1380 CharSourceRange::getCharRange(FixItRange), FixIt);
1381 return Diagnostic;
1382}
1383
1384} // end anonymous namespace
1385
1386void Parser::HandlePragmaAttribute() {
1387 assert(Tok.is(tok::annot_pragma_attribute) &&
1388 "Expected #pragma attribute annotation token");
1389 SourceLocation PragmaLoc = Tok.getLocation();
1390 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1391 if (Info->Action == PragmaAttributeInfo::Pop) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001392 ConsumeAnnotationToken();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001393 Actions.ActOnPragmaAttributePop(PragmaLoc);
1394 return;
1395 }
1396 // Parse the actual attribute with its arguments.
Erik Pilkingtonb287a012018-10-29 03:24:16 +00001397 assert(Info->Action == PragmaAttributeInfo::Push &&
1398 "Unexpected #pragma attribute command");
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001399 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001400 ConsumeAnnotationToken();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001401
1402 ParsedAttributes &Attrs = Info->Attributes;
1403 Attrs.clearListOnly();
1404
1405 auto SkipToEnd = [this]() {
1406 SkipUntil(tok::eof, StopBeforeMatch);
1407 ConsumeToken();
1408 };
1409
1410 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1411 // Parse the CXX11 style attribute.
1412 ParseCXX11AttributeSpecifier(Attrs);
1413 } else if (Tok.is(tok::kw___attribute)) {
1414 ConsumeToken();
1415 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1416 "attribute"))
1417 return SkipToEnd();
1418 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1419 return SkipToEnd();
1420
1421 if (Tok.isNot(tok::identifier)) {
1422 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1423 SkipToEnd();
1424 return;
1425 }
1426 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1427 SourceLocation AttrNameLoc = ConsumeToken();
1428
1429 if (Tok.isNot(tok::l_paren))
1430 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +00001431 ParsedAttr::AS_GNU);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001432 else
1433 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1434 /*ScopeName=*/nullptr,
Erich Keanee891aa92018-07-13 15:07:47 +00001435 /*ScopeLoc=*/SourceLocation(), ParsedAttr::AS_GNU,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001436 /*Declarator=*/nullptr);
1437
1438 if (ExpectAndConsume(tok::r_paren))
1439 return SkipToEnd();
1440 if (ExpectAndConsume(tok::r_paren))
1441 return SkipToEnd();
1442 } else if (Tok.is(tok::kw___declspec)) {
1443 ParseMicrosoftDeclSpecs(Attrs);
1444 } else {
1445 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1446 if (Tok.getIdentifierInfo()) {
1447 // If we suspect that this is an attribute suggest the use of
1448 // '__attribute__'.
Erich Keanee891aa92018-07-13 15:07:47 +00001449 if (ParsedAttr::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1450 ParsedAttr::AS_GNU) !=
1451 ParsedAttr::UnknownAttribute) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001452 SourceLocation InsertStartLoc = Tok.getLocation();
1453 ConsumeToken();
1454 if (Tok.is(tok::l_paren)) {
1455 ConsumeAnyToken();
1456 SkipUntil(tok::r_paren, StopBeforeMatch);
1457 if (Tok.isNot(tok::r_paren))
1458 return SkipToEnd();
1459 }
1460 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1461 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1462 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1463 }
1464 }
1465 SkipToEnd();
1466 return;
1467 }
1468
Erich Keanec480f302018-07-12 21:09:05 +00001469 if (Attrs.empty() || Attrs.begin()->isInvalid()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001470 SkipToEnd();
1471 return;
1472 }
1473
1474 // Ensure that we don't have more than one attribute.
Erich Keanec480f302018-07-12 21:09:05 +00001475 if (Attrs.size() > 1) {
1476 SourceLocation Loc = Attrs[1].getLoc();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001477 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1478 SkipToEnd();
1479 return;
1480 }
1481
Erich Keanee891aa92018-07-13 15:07:47 +00001482 ParsedAttr &Attribute = *Attrs.begin();
Erich Keanec480f302018-07-12 21:09:05 +00001483 if (!Attribute.isSupportedByPragmaAttribute()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001484 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
Erich Keanec480f302018-07-12 21:09:05 +00001485 << Attribute.getName();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001486 SkipToEnd();
1487 return;
1488 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001489
1490 // Parse the subject-list.
1491 if (!TryConsumeToken(tok::comma)) {
1492 createExpectedAttributeSubjectRulesTokenDiagnostic(
1493 diag::err_expected, Attribute,
1494 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1495 << tok::comma;
1496 SkipToEnd();
1497 return;
1498 }
1499
1500 if (Tok.isNot(tok::identifier)) {
1501 createExpectedAttributeSubjectRulesTokenDiagnostic(
1502 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1503 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1504 SkipToEnd();
1505 return;
1506 }
1507 const IdentifierInfo *II = Tok.getIdentifierInfo();
1508 if (!II->isStr("apply_to")) {
1509 createExpectedAttributeSubjectRulesTokenDiagnostic(
1510 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1511 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1512 SkipToEnd();
1513 return;
1514 }
1515 ConsumeToken();
1516
1517 if (!TryConsumeToken(tok::equal)) {
1518 createExpectedAttributeSubjectRulesTokenDiagnostic(
1519 diag::err_expected, Attribute,
1520 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1521 << tok::equal;
1522 SkipToEnd();
1523 return;
1524 }
1525
1526 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1527 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1528 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1529 LastMatchRuleEndLoc)) {
1530 SkipToEnd();
1531 return;
1532 }
1533
1534 // Tokens following an ill-formed attribute will remain in the token stream
1535 // and must be removed.
1536 if (Tok.isNot(tok::eof)) {
1537 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1538 SkipToEnd();
1539 return;
1540 }
1541
1542 // Consume the eof terminator token.
1543 ConsumeToken();
1544
Erik Pilkingtonb287a012018-10-29 03:24:16 +00001545 Actions.ActOnPragmaAttributePush(Attribute, PragmaLoc,
1546 std::move(SubjectMatchRules));
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001547}
1548
Eli Bendersky06a40422014-06-06 20:31:48 +00001549// #pragma GCC visibility comes in two variants:
1550// 'push' '(' [visibility] ')'
1551// 'pop'
Fangrui Song6907ce22018-07-30 19:24:48 +00001552void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001553 PragmaIntroducerKind Introducer,
1554 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +00001555 SourceLocation VisLoc = VisTok.getLocation();
1556
1557 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001558 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001559
1560 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1561
Eli Friedman570024a2010-08-05 06:57:20 +00001562 const IdentifierInfo *VisType;
1563 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +00001564 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +00001565 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001566 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001567 if (Tok.isNot(tok::l_paren)) {
1568 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1569 << "visibility";
1570 return;
1571 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001572 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001573 VisType = Tok.getIdentifierInfo();
1574 if (!VisType) {
1575 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1576 << "visibility";
1577 return;
1578 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001579 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001580 if (Tok.isNot(tok::r_paren)) {
1581 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1582 << "visibility";
1583 return;
1584 }
1585 } else {
1586 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1587 << "visibility";
1588 return;
1589 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001590 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001591 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001592 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +00001593 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1594 << "visibility";
1595 return;
1596 }
1597
David Blaikie2eabcc92016-02-09 18:52:09 +00001598 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +00001599 Toks[0].startToken();
1600 Toks[0].setKind(tok::annot_pragma_vis);
1601 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001602 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +00001603 Toks[0].setAnnotationValue(
1604 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001605 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +00001606}
1607
Daniel Dunbar921b9682008-10-04 19:21:03 +00001608// #pragma pack(...) comes in the following delicious flavors:
1609// pack '(' [integer] ')'
1610// pack '(' 'show' ')'
1611// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Fangrui Song6907ce22018-07-30 19:24:48 +00001612void PragmaPackHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001613 PragmaIntroducerKind Introducer,
1614 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +00001615 SourceLocation PackLoc = PackTok.getLocation();
1616
1617 Token Tok;
1618 PP.Lex(Tok);
1619 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001620 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001621 return;
1622 }
1623
Denis Zobnin10c4f452016-04-29 18:17:40 +00001624 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1625 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001626 Token Alignment;
1627 Alignment.startToken();
Mike Stump11289f42009-09-09 15:08:12 +00001628 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001629 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001630 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001631
1632 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +00001633
1634 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1635 // the push/pop stack.
1636 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
Denis Zobnin10c4f452016-04-29 18:17:40 +00001637 Action =
1638 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001639 } else if (Tok.is(tok::identifier)) {
1640 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +00001641 if (II->isStr("show")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001642 Action = Sema::PSK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001643 PP.Lex(Tok);
1644 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001645 if (II->isStr("push")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001646 Action = Sema::PSK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +00001647 } else if (II->isStr("pop")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001648 Action = Sema::PSK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001649 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001650 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001651 return;
Mike Stump11289f42009-09-09 15:08:12 +00001652 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001653 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001654
Daniel Dunbar921b9682008-10-04 19:21:03 +00001655 if (Tok.is(tok::comma)) {
1656 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001657
Daniel Dunbar921b9682008-10-04 19:21:03 +00001658 if (Tok.is(tok::numeric_constant)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001659 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001660 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001661
1662 PP.Lex(Tok);
1663 } else if (Tok.is(tok::identifier)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001664 SlotLabel = Tok.getIdentifierInfo()->getName();
Daniel Dunbar921b9682008-10-04 19:21:03 +00001665 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001666
Daniel Dunbar921b9682008-10-04 19:21:03 +00001667 if (Tok.is(tok::comma)) {
1668 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001669
Daniel Dunbar921b9682008-10-04 19:21:03 +00001670 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001671 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001672 return;
1673 }
Mike Stump11289f42009-09-09 15:08:12 +00001674
Denis Zobnin10c4f452016-04-29 18:17:40 +00001675 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001676 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001677
1678 PP.Lex(Tok);
1679 }
1680 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001681 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001682 return;
1683 }
1684 }
1685 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001686 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001687 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1688 // the push/pop stack.
1689 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
Denis Zobnin10c4f452016-04-29 18:17:40 +00001690 Action = Sema::PSK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001691 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001692
1693 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001694 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001695 return;
1696 }
1697
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001698 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001699 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001700 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001701 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1702 return;
1703 }
1704
David Blaikie2eabcc92016-02-09 18:52:09 +00001705 PragmaPackInfo *Info =
1706 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Denis Zobnin10c4f452016-04-29 18:17:40 +00001707 Info->Action = Action;
1708 Info->SlotLabel = SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001709 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001710
David Blaikie2eabcc92016-02-09 18:52:09 +00001711 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1712 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001713 Toks[0].startToken();
1714 Toks[0].setKind(tok::annot_pragma_pack);
1715 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001716 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001717 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001718 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001719}
1720
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001721// #pragma ms_struct on
1722// #pragma ms_struct off
Fangrui Song6907ce22018-07-30 19:24:48 +00001723void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001724 PragmaIntroducerKind Introducer,
1725 Token &MSStructTok) {
Nico Weber779355f2016-03-02 23:22:00 +00001726 PragmaMSStructKind Kind = PMSST_OFF;
1727
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001728 Token Tok;
1729 PP.Lex(Tok);
1730 if (Tok.isNot(tok::identifier)) {
1731 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1732 return;
1733 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001734 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001735 const IdentifierInfo *II = Tok.getIdentifierInfo();
1736 if (II->isStr("on")) {
Nico Weber779355f2016-03-02 23:22:00 +00001737 Kind = PMSST_ON;
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001738 PP.Lex(Tok);
1739 }
1740 else if (II->isStr("off") || II->isStr("reset"))
1741 PP.Lex(Tok);
1742 else {
1743 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1744 return;
1745 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001746
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001747 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001748 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1749 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001750 return;
1751 }
Eli Friedman68be1642012-10-04 02:36:51 +00001752
David Blaikie2eabcc92016-02-09 18:52:09 +00001753 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1754 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001755 Toks[0].startToken();
1756 Toks[0].setKind(tok::annot_pragma_msstruct);
1757 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001758 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001759 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1760 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001761 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001762}
1763
Javed Absar2a67c9e2017-06-05 10:11:57 +00001764// #pragma clang section bss="abc" data="" rodata="def" text=""
1765void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
1766 PragmaIntroducerKind Introducer, Token &FirstToken) {
1767
1768 Token Tok;
1769 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1770
1771 PP.Lex(Tok); // eat 'section'
1772 while (Tok.isNot(tok::eod)) {
1773 if (Tok.isNot(tok::identifier)) {
1774 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1775 return;
1776 }
1777
1778 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1779 if (SecType->isStr("bss"))
1780 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1781 else if (SecType->isStr("data"))
1782 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1783 else if (SecType->isStr("rodata"))
1784 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1785 else if (SecType->isStr("text"))
1786 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1787 else {
1788 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1789 return;
1790 }
1791
1792 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1793 if (Tok.isNot(tok::equal)) {
1794 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1795 return;
1796 }
1797
1798 std::string SecName;
1799 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1800 return;
1801
1802 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1803 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1804 Sema::PragmaClangSectionAction::PCSA_Clear),
1805 SecKind, SecName);
1806 }
1807}
1808
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001809// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1810// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001811static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001812 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001813 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001814
1815 if (IsOptions) {
1816 PP.Lex(Tok);
1817 if (Tok.isNot(tok::identifier) ||
1818 !Tok.getIdentifierInfo()->isStr("align")) {
1819 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1820 return;
1821 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001822 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001823
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001824 PP.Lex(Tok);
1825 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001826 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1827 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001828 return;
1829 }
1830
1831 PP.Lex(Tok);
1832 if (Tok.isNot(tok::identifier)) {
1833 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001834 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001835 return;
1836 }
1837
John McCallfaf5fb42010-08-26 23:41:50 +00001838 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001839 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001840 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001841 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001842 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001843 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001844 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001845 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001846 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001847 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001848 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001849 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001850 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001851 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001852 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001853 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1854 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001855 return;
1856 }
1857
David Majnemera8f2f1d2015-03-19 00:10:23 +00001858 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001859 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001860 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001861 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001862 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001863 return;
1864 }
1865
David Blaikie2eabcc92016-02-09 18:52:09 +00001866 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1867 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001868 Toks[0].startToken();
1869 Toks[0].setKind(tok::annot_pragma_align);
1870 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001871 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001872 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1873 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001874 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001875}
1876
Fangrui Song6907ce22018-07-30 19:24:48 +00001877void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001878 PragmaIntroducerKind Introducer,
1879 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001880 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001881}
1882
Fangrui Song6907ce22018-07-30 19:24:48 +00001883void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001884 PragmaIntroducerKind Introducer,
1885 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001886 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001887}
1888
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001889// #pragma unused(identifier)
Fangrui Song6907ce22018-07-30 19:24:48 +00001890void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001891 PragmaIntroducerKind Introducer,
1892 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001893 // FIXME: Should we be expanding macros here? My guess is no.
1894 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001895
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001896 // Lex the left '('.
1897 Token Tok;
1898 PP.Lex(Tok);
1899 if (Tok.isNot(tok::l_paren)) {
1900 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1901 return;
1902 }
Mike Stump11289f42009-09-09 15:08:12 +00001903
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001904 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001905 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001906 SourceLocation RParenLoc;
1907 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001908
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001909 while (true) {
1910 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001911
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001912 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001913 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001914 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001915 LexID = false;
1916 continue;
1917 }
1918
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001919 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001920 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1921 return;
1922 }
Mike Stump11289f42009-09-09 15:08:12 +00001923
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001924 // We are execting a ')' or a ','.
1925 if (Tok.is(tok::comma)) {
1926 LexID = true;
1927 continue;
1928 }
Mike Stump11289f42009-09-09 15:08:12 +00001929
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001930 if (Tok.is(tok::r_paren)) {
1931 RParenLoc = Tok.getLocation();
1932 break;
1933 }
Mike Stump11289f42009-09-09 15:08:12 +00001934
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001935 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001936 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001937 return;
1938 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001939
1940 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001941 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001942 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1943 "unused";
1944 return;
1945 }
1946
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001947 // Verify that we have a location for the right parenthesis.
1948 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001949 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001950
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001951 // For each identifier token, insert into the token stream a
1952 // annot_pragma_unused token followed by the identifier token.
1953 // This allows us to cache a "#pragma unused" that occurs inside an inline
1954 // C++ member function.
1955
David Blaikie2eabcc92016-02-09 18:52:09 +00001956 MutableArrayRef<Token> Toks(
1957 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1958 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001959 for (unsigned i=0; i != Identifiers.size(); i++) {
1960 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1961 pragmaUnusedTok.startToken();
1962 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1963 pragmaUnusedTok.setLocation(UnusedLoc);
1964 idTok = Identifiers[i];
1965 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001966 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001967}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001968
1969// #pragma weak identifier
1970// #pragma weak identifier '=' identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00001971void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001972 PragmaIntroducerKind Introducer,
1973 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001974 SourceLocation WeakLoc = WeakTok.getLocation();
1975
1976 Token Tok;
1977 PP.Lex(Tok);
1978 if (Tok.isNot(tok::identifier)) {
1979 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1980 return;
1981 }
1982
Eli Friedman68be1642012-10-04 02:36:51 +00001983 Token WeakName = Tok;
1984 bool HasAlias = false;
1985 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001986
1987 PP.Lex(Tok);
1988 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001989 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001990 PP.Lex(Tok);
1991 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00001992 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001993 << "weak";
1994 return;
1995 }
Eli Friedman68be1642012-10-04 02:36:51 +00001996 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001997 PP.Lex(Tok);
1998 }
1999
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002000 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002001 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
2002 return;
2003 }
2004
Eli Friedman68be1642012-10-04 02:36:51 +00002005 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00002006 MutableArrayRef<Token> Toks(
2007 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002008 Token &pragmaUnusedTok = Toks[0];
2009 pragmaUnusedTok.startToken();
2010 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
2011 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002012 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002013 Toks[1] = WeakName;
2014 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002015 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002016 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00002017 MutableArrayRef<Token> Toks(
2018 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00002019 Token &pragmaUnusedTok = Toks[0];
2020 pragmaUnusedTok.startToken();
2021 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
2022 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002023 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00002024 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002025 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002026 }
2027}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002028
David Chisnall0867d9c2012-02-18 16:12:34 +00002029// #pragma redefine_extname identifier identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00002030void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
David Chisnall0867d9c2012-02-18 16:12:34 +00002031 PragmaIntroducerKind Introducer,
2032 Token &RedefToken) {
2033 SourceLocation RedefLoc = RedefToken.getLocation();
2034
2035 Token Tok;
2036 PP.Lex(Tok);
2037 if (Tok.isNot(tok::identifier)) {
2038 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2039 "redefine_extname";
2040 return;
2041 }
2042
Eli Friedman68be1642012-10-04 02:36:51 +00002043 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002044 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00002045
David Chisnall0867d9c2012-02-18 16:12:34 +00002046 if (Tok.isNot(tok::identifier)) {
2047 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2048 << "redefine_extname";
2049 return;
2050 }
Eli Friedman68be1642012-10-04 02:36:51 +00002051
2052 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002053 PP.Lex(Tok);
2054
2055 if (Tok.isNot(tok::eod)) {
2056 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2057 "redefine_extname";
2058 return;
2059 }
2060
David Blaikie2eabcc92016-02-09 18:52:09 +00002061 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
2062 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002063 Token &pragmaRedefTok = Toks[0];
2064 pragmaRedefTok.startToken();
2065 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
2066 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002067 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002068 Toks[1] = RedefName;
2069 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002070 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
David Chisnall0867d9c2012-02-18 16:12:34 +00002071}
2072
2073
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002074void
Fangrui Song6907ce22018-07-30 19:24:48 +00002075PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002076 PragmaIntroducerKind Introducer,
2077 Token &Tok) {
2078 tok::OnOffSwitch OOS;
2079 if (PP.LexOnOffSwitch(OOS))
2080 return;
2081
David Blaikie2eabcc92016-02-09 18:52:09 +00002082 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2083 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002084 Toks[0].startToken();
2085 Toks[0].setKind(tok::annot_pragma_fp_contract);
2086 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002087 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002088 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
2089 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00002090 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002091}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002092
Fangrui Song6907ce22018-07-30 19:24:48 +00002093void
2094PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002095 PragmaIntroducerKind Introducer,
2096 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00002097 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002098 if (Tok.isNot(tok::identifier)) {
2099 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2100 "OPENCL";
2101 return;
2102 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002103 IdentifierInfo *Ext = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002104 SourceLocation NameLoc = Tok.getLocation();
2105
2106 PP.Lex(Tok);
2107 if (Tok.isNot(tok::colon)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002108 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002109 return;
2110 }
2111
2112 PP.Lex(Tok);
2113 if (Tok.isNot(tok::identifier)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002114 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002115 return;
2116 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002117 IdentifierInfo *Pred = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002118
Yaxun Liu5b746652016-12-18 05:18:55 +00002119 OpenCLExtState State;
2120 if (Pred->isStr("enable")) {
2121 State = Enable;
2122 } else if (Pred->isStr("disable")) {
2123 State = Disable;
2124 } else if (Pred->isStr("begin"))
2125 State = Begin;
2126 else if (Pred->isStr("end"))
2127 State = End;
2128 else {
2129 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
2130 << Ext->isStr("all");
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002131 return;
2132 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002133 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002134
Eli Friedman68be1642012-10-04 02:36:51 +00002135 PP.Lex(Tok);
2136 if (Tok.isNot(tok::eod)) {
2137 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2138 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002139 return;
2140 }
Eli Friedman68be1642012-10-04 02:36:51 +00002141
Yaxun Liu5b746652016-12-18 05:18:55 +00002142 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2143 Info->first = Ext;
2144 Info->second = State;
David Blaikie2eabcc92016-02-09 18:52:09 +00002145 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2146 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002147 Toks[0].startToken();
2148 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2149 Toks[0].setLocation(NameLoc);
Yaxun Liu5b746652016-12-18 05:18:55 +00002150 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Majnemera8f2f1d2015-03-19 00:10:23 +00002151 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00002152 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002153
2154 if (PP.getPPCallbacks())
Fangrui Song6907ce22018-07-30 19:24:48 +00002155 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
Yaxun Liu5b746652016-12-18 05:18:55 +00002156 StateLoc, State);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002157}
2158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002159/// Handle '#pragma omp ...' when OpenMP is disabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002160///
2161void
2162PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2163 PragmaIntroducerKind Introducer,
2164 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00002165 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2166 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002167 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00002168 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2169 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00002170 }
2171 PP.DiscardUntilEndOfDirective();
2172}
2173
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002174/// Handle '#pragma omp ...' when OpenMP is enabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002175///
2176void
2177PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2178 PragmaIntroducerKind Introducer,
2179 Token &FirstTok) {
2180 SmallVector<Token, 16> Pragma;
2181 Token Tok;
2182 Tok.startToken();
2183 Tok.setKind(tok::annot_pragma_openmp);
2184 Tok.setLocation(FirstTok.getLocation());
2185
Alexey Bataev96dae812018-02-16 18:36:44 +00002186 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002187 Pragma.push_back(Tok);
2188 PP.Lex(Tok);
Alexey Bataev96dae812018-02-16 18:36:44 +00002189 if (Tok.is(tok::annot_pragma_openmp)) {
2190 PP.Diag(Tok, diag::err_omp_unexpected_directive) << 0;
2191 unsigned InnerPragmaCnt = 1;
2192 while (InnerPragmaCnt != 0) {
2193 PP.Lex(Tok);
2194 if (Tok.is(tok::annot_pragma_openmp))
2195 ++InnerPragmaCnt;
2196 else if (Tok.is(tok::annot_pragma_openmp_end))
2197 --InnerPragmaCnt;
2198 }
2199 PP.Lex(Tok);
2200 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002201 }
2202 SourceLocation EodLoc = Tok.getLocation();
2203 Tok.startToken();
2204 Tok.setKind(tok::annot_pragma_openmp_end);
2205 Tok.setLocation(EodLoc);
2206 Pragma.push_back(Tok);
2207
David Blaikie2eabcc92016-02-09 18:52:09 +00002208 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2209 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2210 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
2211 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00002212}
Reid Kleckner002562a2013-05-06 21:02:12 +00002213
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002214/// Handle '#pragma pointers_to_members'
David Majnemer4bb09802014-02-10 19:50:15 +00002215// The grammar for this pragma is as follows:
2216//
2217// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2218//
2219// #pragma pointers_to_members '(' 'best_case' ')'
2220// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2221// #pragma pointers_to_members '(' inheritance-model ')'
2222void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
2223 PragmaIntroducerKind Introducer,
2224 Token &Tok) {
2225 SourceLocation PointersToMembersLoc = Tok.getLocation();
2226 PP.Lex(Tok);
2227 if (Tok.isNot(tok::l_paren)) {
2228 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2229 << "pointers_to_members";
2230 return;
2231 }
2232 PP.Lex(Tok);
2233 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2234 if (!Arg) {
2235 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2236 << "pointers_to_members";
2237 return;
2238 }
2239 PP.Lex(Tok);
2240
David Majnemer86c318f2014-02-11 21:05:00 +00002241 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00002242 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002243 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00002244 } else {
2245 if (Arg->isStr("full_generality")) {
2246 if (Tok.is(tok::comma)) {
2247 PP.Lex(Tok);
2248
2249 Arg = Tok.getIdentifierInfo();
2250 if (!Arg) {
2251 PP.Diag(Tok.getLocation(),
2252 diag::err_pragma_pointers_to_members_unknown_kind)
2253 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2254 return;
2255 }
2256 PP.Lex(Tok);
2257 } else if (Tok.is(tok::r_paren)) {
2258 // #pragma pointers_to_members(full_generality) implicitly specifies
2259 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00002260 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00002261 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002262 } else {
2263 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2264 << "full_generality";
2265 return;
2266 }
2267 }
2268
2269 if (Arg) {
2270 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002271 RepresentationMethod =
2272 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002273 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002274 RepresentationMethod =
2275 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002276 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002277 RepresentationMethod =
2278 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002279 } else {
2280 PP.Diag(Tok.getLocation(),
2281 diag::err_pragma_pointers_to_members_unknown_kind)
2282 << Arg << /*HasPointerDeclaration*/ 1;
2283 return;
2284 }
2285 }
2286 }
2287
2288 if (Tok.isNot(tok::r_paren)) {
2289 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2290 << (Arg ? Arg->getName() : "full_generality");
2291 return;
2292 }
2293
David Majnemera8f2f1d2015-03-19 00:10:23 +00002294 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00002295 PP.Lex(Tok);
2296 if (Tok.isNot(tok::eod)) {
2297 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2298 << "pointers_to_members";
2299 return;
2300 }
2301
2302 Token AnnotTok;
2303 AnnotTok.startToken();
2304 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2305 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002306 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00002307 AnnotTok.setAnnotationValue(
2308 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
2309 PP.EnterToken(AnnotTok);
2310}
2311
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002312/// Handle '#pragma vtordisp'
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002313// The grammar for this pragma is as follows:
2314//
2315// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2316//
2317// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2318// #pragma vtordisp '(' 'pop' ')'
2319// #pragma vtordisp '(' ')'
2320void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
2321 PragmaIntroducerKind Introducer,
2322 Token &Tok) {
2323 SourceLocation VtorDispLoc = Tok.getLocation();
2324 PP.Lex(Tok);
2325 if (Tok.isNot(tok::l_paren)) {
2326 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2327 return;
2328 }
2329 PP.Lex(Tok);
2330
Denis Zobnin2290dac2016-04-29 11:27:00 +00002331 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002332 const IdentifierInfo *II = Tok.getIdentifierInfo();
2333 if (II) {
2334 if (II->isStr("push")) {
2335 // #pragma vtordisp(push, mode)
2336 PP.Lex(Tok);
2337 if (Tok.isNot(tok::comma)) {
2338 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2339 return;
2340 }
2341 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002342 Action = Sema::PSK_Push_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002343 // not push, could be on/off
2344 } else if (II->isStr("pop")) {
2345 // #pragma vtordisp(pop)
2346 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002347 Action = Sema::PSK_Pop;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002348 }
2349 // not push or pop, could be on/off
2350 } else {
2351 if (Tok.is(tok::r_paren)) {
2352 // #pragma vtordisp()
Denis Zobnin2290dac2016-04-29 11:27:00 +00002353 Action = Sema::PSK_Reset;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002354 }
2355 }
2356
2357
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002358 uint64_t Value = 0;
Denis Zobnin2290dac2016-04-29 11:27:00 +00002359 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002360 const IdentifierInfo *II = Tok.getIdentifierInfo();
2361 if (II && II->isStr("off")) {
2362 PP.Lex(Tok);
2363 Value = 0;
2364 } else if (II && II->isStr("on")) {
2365 PP.Lex(Tok);
2366 Value = 1;
2367 } else if (Tok.is(tok::numeric_constant) &&
2368 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2369 if (Value > 2) {
2370 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2371 << 0 << 2 << "vtordisp";
2372 return;
2373 }
2374 } else {
2375 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2376 << "vtordisp";
2377 return;
2378 }
2379 }
2380
2381 // Finish the pragma: ')' $
2382 if (Tok.isNot(tok::r_paren)) {
2383 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2384 return;
2385 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00002386 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002387 PP.Lex(Tok);
2388 if (Tok.isNot(tok::eod)) {
2389 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2390 << "vtordisp";
2391 return;
2392 }
2393
2394 // Enter the annotation.
2395 Token AnnotTok;
2396 AnnotTok.startToken();
2397 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2398 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002399 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002400 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
Denis Zobnin2290dac2016-04-29 11:27:00 +00002401 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002402 PP.EnterToken(AnnotTok);
2403}
2404
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002405/// Handle all MS pragmas. Simply forwards the tokens after inserting
Warren Huntc3b18962014-04-08 22:30:47 +00002406/// an annotation token.
2407void PragmaMSPragma::HandlePragma(Preprocessor &PP,
2408 PragmaIntroducerKind Introducer,
2409 Token &Tok) {
2410 Token EoF, AnnotTok;
2411 EoF.startToken();
2412 EoF.setKind(tok::eof);
2413 AnnotTok.startToken();
2414 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2415 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002416 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00002417 SmallVector<Token, 8> TokenVector;
2418 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00002419 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00002420 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002421 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2422 }
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002423 // Add a sentinel EoF token to the end of the list.
Warren Huntc3b18962014-04-08 22:30:47 +00002424 TokenVector.push_back(EoF);
2425 // We must allocate this array with new because EnterTokenStream is going to
2426 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00002427 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2428 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00002429 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00002430 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2431 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00002432 AnnotTok.setAnnotationValue(Value);
2433 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00002434}
2435
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002436/// Handle the Microsoft \#pragma detect_mismatch extension.
Aaron Ballman5d041be2013-06-04 02:07:14 +00002437///
2438/// The syntax is:
2439/// \code
2440/// #pragma detect_mismatch("name", "value")
2441/// \endcode
2442/// Where 'name' and 'value' are quoted strings. The values are embedded in
2443/// the object file and passed along to the linker. If the linker detects a
2444/// mismatch in the object file's values for the given name, a LNK2038 error
2445/// is emitted. See MSDN for more details.
2446void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
2447 PragmaIntroducerKind Introducer,
2448 Token &Tok) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002449 SourceLocation DetectMismatchLoc = Tok.getLocation();
Aaron Ballman5d041be2013-06-04 02:07:14 +00002450 PP.Lex(Tok);
2451 if (Tok.isNot(tok::l_paren)) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002452 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002453 return;
2454 }
2455
2456 // Read the name to embed, which must be a string literal.
2457 std::string NameString;
2458 if (!PP.LexStringLiteral(Tok, NameString,
2459 "pragma detect_mismatch",
2460 /*MacroExpansion=*/true))
2461 return;
2462
2463 // Read the comma followed by a second string literal.
2464 std::string ValueString;
2465 if (Tok.isNot(tok::comma)) {
2466 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2467 return;
2468 }
2469
2470 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
2471 /*MacroExpansion=*/true))
2472 return;
2473
2474 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002475 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002476 return;
2477 }
2478 PP.Lex(Tok); // Eat the r_paren.
2479
2480 if (Tok.isNot(tok::eod)) {
2481 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2482 return;
2483 }
2484
Reid Kleckner71966c92014-02-20 23:37:45 +00002485 // If the pragma is lexically sound, notify any interested PPCallbacks.
2486 if (PP.getPPCallbacks())
Nico Webercbbaeb12016-03-02 19:28:54 +00002487 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
Reid Kleckner71966c92014-02-20 23:37:45 +00002488 ValueString);
2489
Nico Webercbbaeb12016-03-02 19:28:54 +00002490 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
Aaron Ballman5d041be2013-06-04 02:07:14 +00002491}
2492
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002493/// Handle the microsoft \#pragma comment extension.
Reid Kleckner002562a2013-05-06 21:02:12 +00002494///
2495/// The syntax is:
2496/// \code
2497/// #pragma comment(linker, "foo")
2498/// \endcode
2499/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2500/// "foo" is a string, which is fully macro expanded, and permits string
2501/// concatenation, embedded escape characters etc. See MSDN for more details.
2502void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
2503 PragmaIntroducerKind Introducer,
2504 Token &Tok) {
2505 SourceLocation CommentLoc = Tok.getLocation();
2506 PP.Lex(Tok);
2507 if (Tok.isNot(tok::l_paren)) {
2508 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2509 return;
2510 }
2511
2512 // Read the identifier.
2513 PP.Lex(Tok);
2514 if (Tok.isNot(tok::identifier)) {
2515 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2516 return;
2517 }
2518
2519 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002520 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00002521 PragmaMSCommentKind Kind =
2522 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2523 .Case("linker", PCK_Linker)
2524 .Case("lib", PCK_Lib)
2525 .Case("compiler", PCK_Compiler)
2526 .Case("exestr", PCK_ExeStr)
2527 .Case("user", PCK_User)
2528 .Default(PCK_Unknown);
2529 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00002530 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2531 return;
2532 }
2533
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +00002534 if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
2535 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2536 << II->getName();
2537 return;
2538 }
2539
Yunzhong Gao99efc032015-03-23 20:41:42 +00002540 // On PS4, issue a warning about any pragma comments other than
2541 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00002542 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00002543 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2544 << II->getName();
2545 return;
2546 }
2547
Reid Kleckner002562a2013-05-06 21:02:12 +00002548 // Read the optional string if present.
2549 PP.Lex(Tok);
2550 std::string ArgumentString;
2551 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2552 "pragma comment",
2553 /*MacroExpansion=*/true))
2554 return;
2555
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002556 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00002557 // FIXME: If the kind is "compiler" warn if the string is present (it is
2558 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002559 // The MSDN docs say that "lib" and "linker" require a string and have a short
2560 // whitelist of linker options they support, but in practice MSVC doesn't
2561 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00002562
2563 if (Tok.isNot(tok::r_paren)) {
2564 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2565 return;
2566 }
2567 PP.Lex(Tok); // eat the r_paren.
2568
2569 if (Tok.isNot(tok::eod)) {
2570 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2571 return;
2572 }
2573
Reid Kleckner71966c92014-02-20 23:37:45 +00002574 // If the pragma is lexically sound, notify any interested PPCallbacks.
2575 if (PP.getPPCallbacks())
2576 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2577
Nico Weber66220292016-03-02 17:28:48 +00002578 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00002579}
Dario Domizioli13a0a382014-05-23 12:13:25 +00002580
2581// #pragma clang optimize off
2582// #pragma clang optimize on
Fangrui Song6907ce22018-07-30 19:24:48 +00002583void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
Dario Domizioli13a0a382014-05-23 12:13:25 +00002584 PragmaIntroducerKind Introducer,
2585 Token &FirstToken) {
2586 Token Tok;
2587 PP.Lex(Tok);
2588 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002589 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00002590 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00002591 return;
2592 }
2593 if (Tok.isNot(tok::identifier)) {
2594 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2595 << PP.getSpelling(Tok);
2596 return;
2597 }
2598 const IdentifierInfo *II = Tok.getIdentifierInfo();
2599 // The only accepted values are 'on' or 'off'.
2600 bool IsOn = false;
2601 if (II->isStr("on")) {
2602 IsOn = true;
2603 } else if (!II->isStr("off")) {
2604 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2605 << PP.getSpelling(Tok);
2606 return;
2607 }
2608 PP.Lex(Tok);
Fangrui Song6907ce22018-07-30 19:24:48 +00002609
Dario Domizioli13a0a382014-05-23 12:13:25 +00002610 if (Tok.isNot(tok::eod)) {
2611 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2612 << PP.getSpelling(Tok);
2613 return;
2614 }
Eli Bendersky06a40422014-06-06 20:31:48 +00002615
2616 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2617}
2618
Adam Nemet60d32642017-04-04 21:18:36 +00002619namespace {
2620/// Used as the annotation value for tok::annot_pragma_fp.
2621struct TokFPAnnotValue {
2622 enum FlagKinds { Contract };
2623 enum FlagValues { On, Off, Fast };
2624
2625 FlagKinds FlagKind;
2626 FlagValues FlagValue;
2627};
2628} // end anonymous namespace
2629
2630void PragmaFPHandler::HandlePragma(Preprocessor &PP,
2631 PragmaIntroducerKind Introducer,
2632 Token &Tok) {
2633 // fp
2634 Token PragmaName = Tok;
2635 SmallVector<Token, 1> TokenList;
2636
2637 PP.Lex(Tok);
2638 if (Tok.isNot(tok::identifier)) {
2639 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2640 << /*MissingOption=*/true << "";
2641 return;
2642 }
2643
2644 while (Tok.is(tok::identifier)) {
2645 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2646
2647 auto FlagKind =
2648 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2649 OptionInfo->getName())
2650 .Case("contract", TokFPAnnotValue::Contract)
2651 .Default(None);
2652 if (!FlagKind) {
2653 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2654 << /*MissingOption=*/false << OptionInfo;
2655 return;
2656 }
2657 PP.Lex(Tok);
2658
2659 // Read '('
2660 if (Tok.isNot(tok::l_paren)) {
2661 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2662 return;
2663 }
2664 PP.Lex(Tok);
2665
2666 if (Tok.isNot(tok::identifier)) {
2667 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2668 << PP.getSpelling(Tok) << OptionInfo->getName();
2669 return;
2670 }
2671 const IdentifierInfo *II = Tok.getIdentifierInfo();
2672
2673 auto FlagValue =
2674 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2675 II->getName())
2676 .Case("on", TokFPAnnotValue::On)
2677 .Case("off", TokFPAnnotValue::Off)
2678 .Case("fast", TokFPAnnotValue::Fast)
2679 .Default(llvm::None);
2680
2681 if (!FlagValue) {
2682 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2683 << PP.getSpelling(Tok) << OptionInfo->getName();
2684 return;
2685 }
2686 PP.Lex(Tok);
2687
2688 // Read ')'
2689 if (Tok.isNot(tok::r_paren)) {
2690 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2691 return;
2692 }
2693 PP.Lex(Tok);
2694
2695 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2696 TokFPAnnotValue{*FlagKind, *FlagValue};
2697 // Generate the loop hint token.
2698 Token FPTok;
2699 FPTok.startToken();
2700 FPTok.setKind(tok::annot_pragma_fp);
2701 FPTok.setLocation(PragmaName.getLocation());
2702 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2703 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2704 TokenList.push_back(FPTok);
2705 }
2706
2707 if (Tok.isNot(tok::eod)) {
2708 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2709 << "clang fp";
2710 return;
2711 }
2712
2713 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2714 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2715
2716 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2717 /*DisableMacroExpansion=*/false);
2718}
2719
2720void Parser::HandlePragmaFP() {
2721 assert(Tok.is(tok::annot_pragma_fp));
2722 auto *AnnotValue =
2723 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2724
2725 LangOptions::FPContractModeKind FPC;
2726 switch (AnnotValue->FlagValue) {
2727 case TokFPAnnotValue::On:
2728 FPC = LangOptions::FPC_On;
2729 break;
2730 case TokFPAnnotValue::Fast:
2731 FPC = LangOptions::FPC_Fast;
2732 break;
2733 case TokFPAnnotValue::Off:
2734 FPC = LangOptions::FPC_Off;
2735 break;
2736 }
2737
2738 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +00002739 ConsumeAnnotationToken();
Adam Nemet60d32642017-04-04 21:18:36 +00002740}
2741
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002742/// Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002743static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2744 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002745 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002746 SmallVector<Token, 1> ValueList;
2747 int OpenParens = ValueInParens ? 1 : 0;
2748 // Read constant expression.
2749 while (Tok.isNot(tok::eod)) {
2750 if (Tok.is(tok::l_paren))
2751 OpenParens++;
2752 else if (Tok.is(tok::r_paren)) {
2753 OpenParens--;
2754 if (OpenParens == 0 && ValueInParens)
2755 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002756 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002757
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002758 ValueList.push_back(Tok);
2759 PP.Lex(Tok);
2760 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002761
2762 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002763 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002764 if (Tok.isNot(tok::r_paren)) {
2765 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2766 return true;
2767 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002768 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002769 }
2770
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002771 Token EOFTok;
2772 EOFTok.startToken();
2773 EOFTok.setKind(tok::eof);
2774 EOFTok.setLocation(Tok.getLocation());
2775 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2776
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00002777 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002778
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002779 Info.PragmaName = PragmaName;
2780 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002781 return false;
2782}
2783
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002784/// Handle the \#pragma clang loop directive.
Eli Bendersky06a40422014-06-06 20:31:48 +00002785/// #pragma clang 'loop' loop-hints
2786///
2787/// loop-hints:
2788/// loop-hint loop-hints[opt]
2789///
2790/// loop-hint:
2791/// 'vectorize' '(' loop-hint-keyword ')'
2792/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00002793/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002794/// 'vectorize_width' '(' loop-hint-value ')'
2795/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00002796/// 'unroll_count' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002797///
2798/// loop-hint-keyword:
2799/// 'enable'
2800/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00002801/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00002802///
Mark Heffernan450c2382014-07-23 17:31:31 +00002803/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00002804/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00002805/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00002806/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00002807///
Eli Bendersky06a40422014-06-06 20:31:48 +00002808/// loop-hint-value:
2809/// constant-expression
2810///
2811/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2812/// try vectorizing the instructions of the loop it precedes. Specifying
2813/// interleave(enable) or interleave_count(_value_) instructs llvm to try
2814/// interleaving multiple iterations of the loop it precedes. The width of the
2815/// vector instructions is specified by vectorize_width() and the number of
2816/// interleaved loop iterations is specified by interleave_count(). Specifying a
2817/// value of 1 effectively disables vectorization/interleaving, even if it is
2818/// possible and profitable, and 0 is invalid. The loop vectorizer currently
2819/// only works on inner loops.
2820///
Eli Bendersky86483b32014-06-11 17:56:26 +00002821/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00002822/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2823/// completely if the trip count is known at compile time and unroll partially
2824/// if the trip count is not known. Specifying unroll(full) is similar to
2825/// unroll(enable) but will unroll the loop only if the trip count is known at
2826/// compile time. Specifying unroll(disable) disables unrolling for the
2827/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2828/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00002829void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2830 PragmaIntroducerKind Introducer,
2831 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002832 // Incoming token is "loop" from "#pragma clang loop".
2833 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00002834 SmallVector<Token, 1> TokenList;
2835
2836 // Lex the optimization option and verify it is an identifier.
2837 PP.Lex(Tok);
2838 if (Tok.isNot(tok::identifier)) {
2839 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2840 << /*MissingOption=*/true << "";
2841 return;
2842 }
2843
2844 while (Tok.is(tok::identifier)) {
2845 Token Option = Tok;
2846 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2847
Eli Bendersky86483b32014-06-11 17:56:26 +00002848 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002849 .Case("vectorize", true)
2850 .Case("interleave", true)
2851 .Case("unroll", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00002852 .Case("distribute", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002853 .Case("vectorize_width", true)
2854 .Case("interleave_count", true)
2855 .Case("unroll_count", true)
2856 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002857 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002858 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2859 << /*MissingOption=*/false << OptionInfo;
2860 return;
2861 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002862 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002863
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002864 // Read '('
2865 if (Tok.isNot(tok::l_paren)) {
2866 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002867 return;
2868 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002869 PP.Lex(Tok);
2870
2871 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2872 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2873 *Info))
2874 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002875
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002876 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002877 Token LoopHintTok;
2878 LoopHintTok.startToken();
2879 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002880 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002881 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002882 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2883 TokenList.push_back(LoopHintTok);
2884 }
2885
2886 if (Tok.isNot(tok::eod)) {
2887 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2888 << "clang loop";
2889 return;
2890 }
2891
David Blaikie2eabcc92016-02-09 18:52:09 +00002892 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2893 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002894
David Blaikie2eabcc92016-02-09 18:52:09 +00002895 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2896 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002897}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002898
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002899/// Handle the loop unroll optimization pragmas.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002900/// #pragma unroll
2901/// #pragma unroll unroll-hint-value
2902/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002903/// #pragma nounroll
David Greenc8e39242018-08-01 14:36:12 +00002904/// #pragma unroll_and_jam
2905/// #pragma unroll_and_jam unroll-hint-value
2906/// #pragma unroll_and_jam '(' unroll-hint-value ')'
2907/// #pragma nounroll_and_jam
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002908///
2909/// unroll-hint-value:
2910/// constant-expression
2911///
Mark Heffernanc888e412014-07-24 18:09:38 +00002912/// Loop unrolling hints can be specified with '#pragma unroll' or
2913/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2914/// contained in parentheses. With no argument the directive instructs llvm to
2915/// try to unroll the loop completely. A positive integer argument can be
2916/// specified to indicate the number of times the loop should be unrolled. To
2917/// maximize compatibility with other compilers the unroll count argument can be
2918/// specified with or without parentheses. Specifying, '#pragma nounroll'
2919/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002920void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2921 PragmaIntroducerKind Introducer,
2922 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002923 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2924 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002925 Token PragmaName = Tok;
2926 PP.Lex(Tok);
2927 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2928 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002929 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002930 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002931 Info->Option.startToken();
David Greenc8e39242018-08-01 14:36:12 +00002932 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll" ||
2933 PragmaName.getIdentifierInfo()->getName() == "nounroll_and_jam") {
Mark Heffernanc888e412014-07-24 18:09:38 +00002934 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
David Greenc8e39242018-08-01 14:36:12 +00002935 << PragmaName.getIdentifierInfo()->getName();
Mark Heffernanc888e412014-07-24 18:09:38 +00002936 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002937 } else {
2938 // Unroll pragma with an argument: "#pragma unroll N" or
2939 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002940 // Read '(' if it exists.
2941 bool ValueInParens = Tok.is(tok::l_paren);
2942 if (ValueInParens)
2943 PP.Lex(Tok);
2944
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002945 Token Option;
2946 Option.startToken();
2947 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002948 return;
2949
2950 // In CUDA, the argument to '#pragma unroll' should not be contained in
2951 // parentheses.
2952 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002953 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002954 diag::warn_pragma_unroll_cuda_value_in_parens);
2955
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002956 if (Tok.isNot(tok::eod)) {
2957 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2958 << "unroll";
2959 return;
2960 }
2961 }
2962
2963 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002964 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002965 TokenArray[0].startToken();
2966 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2967 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002968 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002969 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002970 PP.EnterTokenStream(std::move(TokenArray), 1,
2971 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002972}
Reid Kleckner3f1ec622016-09-07 16:38:32 +00002973
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002974/// Handle the Microsoft \#pragma intrinsic extension.
Reid Kleckner3f1ec622016-09-07 16:38:32 +00002975///
2976/// The syntax is:
2977/// \code
2978/// #pragma intrinsic(memset)
2979/// #pragma intrinsic(strlen, memcpy)
2980/// \endcode
2981///
2982/// Pragma intrisic tells the compiler to use a builtin version of the
2983/// function. Clang does it anyway, so the pragma doesn't really do anything.
2984/// Anyway, we emit a warning if the function specified in \#pragma intrinsic
2985/// isn't an intrinsic in clang and suggest to include intrin.h.
2986void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
2987 PragmaIntroducerKind Introducer,
2988 Token &Tok) {
2989 PP.Lex(Tok);
2990
2991 if (Tok.isNot(tok::l_paren)) {
2992 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
2993 << "intrinsic";
2994 return;
2995 }
2996 PP.Lex(Tok);
2997
2998 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
2999
3000 while (Tok.is(tok::identifier)) {
3001 IdentifierInfo *II = Tok.getIdentifierInfo();
3002 if (!II->getBuiltinID())
3003 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
3004 << II << SuggestIntrinH;
3005
3006 PP.Lex(Tok);
3007 if (Tok.isNot(tok::comma))
3008 break;
3009 PP.Lex(Tok);
3010 }
3011
3012 if (Tok.isNot(tok::r_paren)) {
3013 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
3014 << "intrinsic";
3015 return;
3016 }
3017 PP.Lex(Tok);
3018
3019 if (Tok.isNot(tok::eod))
3020 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3021 << "intrinsic";
3022}
Hans Wennborg1bbe00e2018-03-20 08:53:11 +00003023
3024// #pragma optimize("gsty", on|off)
3025void PragmaMSOptimizeHandler::HandlePragma(Preprocessor &PP,
3026 PragmaIntroducerKind Introducer,
3027 Token &Tok) {
3028 SourceLocation StartLoc = Tok.getLocation();
3029 PP.Lex(Tok);
3030
3031 if (Tok.isNot(tok::l_paren)) {
3032 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "optimize";
3033 return;
3034 }
3035 PP.Lex(Tok);
3036
3037 if (Tok.isNot(tok::string_literal)) {
3038 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_string) << "optimize";
3039 return;
3040 }
3041 // We could syntax check the string but it's probably not worth the effort.
3042 PP.Lex(Tok);
3043
3044 if (Tok.isNot(tok::comma)) {
3045 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_comma) << "optimize";
3046 return;
3047 }
3048 PP.Lex(Tok);
3049
3050 if (Tok.is(tok::eod) || Tok.is(tok::r_paren)) {
3051 PP.Diag(Tok.getLocation(), diag::warn_pragma_missing_argument)
3052 << "optimize" << /*Expected=*/true << "'on' or 'off'";
3053 return;
3054 }
3055 IdentifierInfo *II = Tok.getIdentifierInfo();
3056 if (!II || (!II->isStr("on") && !II->isStr("off"))) {
3057 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
3058 << PP.getSpelling(Tok) << "optimize" << /*Expected=*/true
3059 << "'on' or 'off'";
3060 return;
3061 }
3062 PP.Lex(Tok);
3063
3064 if (Tok.isNot(tok::r_paren)) {
3065 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "optimize";
3066 return;
3067 }
3068 PP.Lex(Tok);
3069
3070 if (Tok.isNot(tok::eod)) {
3071 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3072 << "optimize";
3073 return;
3074 }
3075 PP.Diag(StartLoc, diag::warn_pragma_optimize);
3076}
3077
Justin Lebar67a78a62016-10-08 22:15:58 +00003078void PragmaForceCUDAHostDeviceHandler::HandlePragma(
3079 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
3080 Token FirstTok = Tok;
3081
3082 PP.Lex(Tok);
3083 IdentifierInfo *Info = Tok.getIdentifierInfo();
3084 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
3085 PP.Diag(FirstTok.getLocation(),
3086 diag::warn_pragma_force_cuda_host_device_bad_arg);
3087 return;
3088 }
3089
3090 if (Info->isStr("begin"))
3091 Actions.PushForceCUDAHostDevice();
3092 else if (!Actions.PopForceCUDAHostDevice())
3093 PP.Diag(FirstTok.getLocation(),
3094 diag::err_pragma_cannot_end_force_cuda_host_device);
3095
3096 PP.Lex(Tok);
3097 if (!Tok.is(tok::eod))
3098 PP.Diag(FirstTok.getLocation(),
3099 diag::warn_pragma_force_cuda_host_device_bad_arg);
3100}
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003101
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003102/// Handle the #pragma clang attribute directive.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003103///
3104/// The syntax is:
3105/// \code
3106/// #pragma clang attribute push(attribute, subject-set)
3107/// #pragma clang attribute pop
3108/// \endcode
3109///
3110/// The subject-set clause defines the set of declarations which receive the
3111/// attribute. Its exact syntax is described in the LanguageExtensions document
3112/// in Clang's documentation.
3113///
3114/// This directive instructs the compiler to begin/finish applying the specified
3115/// attribute to the set of attribute-specific declarations in the active range
3116/// of the pragma.
3117void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
3118 PragmaIntroducerKind Introducer,
3119 Token &FirstToken) {
3120 Token Tok;
3121 PP.Lex(Tok);
3122 auto *Info = new (PP.getPreprocessorAllocator())
3123 PragmaAttributeInfo(AttributesForPragmaAttribute);
3124
Erik Pilkingtonb287a012018-10-29 03:24:16 +00003125 // Parse the 'push' or 'pop'.
3126 if (Tok.isNot(tok::identifier)) {
3127 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_push_pop);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003128 return;
3129 }
Erik Pilkingtonb287a012018-10-29 03:24:16 +00003130 const auto *II = Tok.getIdentifierInfo();
3131 if (II->isStr("push"))
3132 Info->Action = PragmaAttributeInfo::Push;
3133 else if (II->isStr("pop"))
3134 Info->Action = PragmaAttributeInfo::Pop;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003135 else {
Erik Pilkingtonb287a012018-10-29 03:24:16 +00003136 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
3137 << PP.getSpelling(Tok);
3138 return;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003139 }
Erik Pilkingtonb287a012018-10-29 03:24:16 +00003140 PP.Lex(Tok);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003141
3142 // Parse the actual attribute.
Erik Pilkingtonb287a012018-10-29 03:24:16 +00003143 if (Info->Action == PragmaAttributeInfo::Push) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003144 if (Tok.isNot(tok::l_paren)) {
3145 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
3146 return;
3147 }
3148 PP.Lex(Tok);
3149
3150 // Lex the attribute tokens.
3151 SmallVector<Token, 16> AttributeTokens;
3152 int OpenParens = 1;
3153 while (Tok.isNot(tok::eod)) {
3154 if (Tok.is(tok::l_paren))
3155 OpenParens++;
3156 else if (Tok.is(tok::r_paren)) {
3157 OpenParens--;
3158 if (OpenParens == 0)
3159 break;
3160 }
3161
3162 AttributeTokens.push_back(Tok);
3163 PP.Lex(Tok);
3164 }
3165
3166 if (AttributeTokens.empty()) {
3167 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
3168 return;
3169 }
3170 if (Tok.isNot(tok::r_paren)) {
3171 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
3172 return;
3173 }
3174 SourceLocation EndLoc = Tok.getLocation();
3175 PP.Lex(Tok);
3176
3177 // Terminate the attribute for parsing.
3178 Token EOFTok;
3179 EOFTok.startToken();
3180 EOFTok.setKind(tok::eof);
3181 EOFTok.setLocation(EndLoc);
3182 AttributeTokens.push_back(EOFTok);
3183
3184 Info->Tokens =
3185 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
3186 }
3187
3188 if (Tok.isNot(tok::eod))
3189 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3190 << "clang attribute";
3191
3192 // Generate the annotated pragma token.
3193 auto TokenArray = llvm::make_unique<Token[]>(1);
3194 TokenArray[0].startToken();
3195 TokenArray[0].setKind(tok::annot_pragma_attribute);
3196 TokenArray[0].setLocation(FirstToken.getLocation());
3197 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
3198 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
3199 PP.EnterTokenStream(std::move(TokenArray), 1,
3200 /*DisableMacroExpansion=*/false);
3201}