blob: f63ab3a744fd26227d97d52affa43ee4892f4922 [file] [log] [blame]
Daniel Dunbar921b9682008-10-04 19:21:03 +00001//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar921b9682008-10-04 19:21:03 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the language specific #pragma handlers.
10//
11//===----------------------------------------------------------------------===//
12
Hans Wennborg899ded92014-10-16 20:52:46 +000013#include "clang/AST/ASTContext.h"
Nico Weber66220292016-03-02 17:28:48 +000014#include "clang/Basic/PragmaKinds.h"
David Majnemerad2986e2014-08-14 06:35:08 +000015#include "clang/Basic/TargetInfo.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000016#include "clang/Lex/Preprocessor.h"
Richard Trieu0614cff2018-11-28 04:36:31 +000017#include "clang/Parse/LoopHint.h"
Eli Bendersky06a40422014-06-06 20:31:48 +000018#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/Scope.h"
22#include "llvm/ADT/StringSwitch.h"
23using namespace clang;
Daniel Dunbar921b9682008-10-04 19:21:03 +000024
Reid Kleckner5b086462014-02-20 22:52:09 +000025namespace {
26
27struct PragmaAlignHandler : public PragmaHandler {
28 explicit PragmaAlignHandler() : PragmaHandler("align") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000029 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000030 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000031};
32
33struct PragmaGCCVisibilityHandler : public PragmaHandler {
34 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000035 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000036 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000037};
38
39struct PragmaOptionsHandler : public PragmaHandler {
40 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000041 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000042 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000043};
44
45struct PragmaPackHandler : public PragmaHandler {
46 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000047 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000048 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000049};
50
Javed Absar2a67c9e2017-06-05 10:11:57 +000051struct PragmaClangSectionHandler : public PragmaHandler {
52 explicit PragmaClangSectionHandler(Sema &S)
53 : PragmaHandler("section"), Actions(S) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000054 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Javed Absar2a67c9e2017-06-05 10:11:57 +000055 Token &FirstToken) override;
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000056
Javed Absar2a67c9e2017-06-05 10:11:57 +000057private:
58 Sema &Actions;
59};
60
Reid Kleckner5b086462014-02-20 22:52:09 +000061struct PragmaMSStructHandler : public PragmaHandler {
62 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000063 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000064 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000065};
66
67struct PragmaUnusedHandler : public PragmaHandler {
68 PragmaUnusedHandler() : PragmaHandler("unused") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000069 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000070 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000071};
72
73struct PragmaWeakHandler : public PragmaHandler {
74 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000075 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000076 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000077};
78
79struct PragmaRedefineExtnameHandler : public PragmaHandler {
80 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000081 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000082 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000083};
84
85struct PragmaOpenCLExtensionHandler : public PragmaHandler {
86 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000087 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000088 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000089};
90
91
92struct PragmaFPContractHandler : public PragmaHandler {
93 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +000094 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +000095 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
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000104 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Steven Wub96a3a42018-01-05 22:45:03 +0000105 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)));
Ilya Biryukov929af672019-05-17 09:32:05 +0000121 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
122 /*IsReinject=*/false);
Steven Wub96a3a42018-01-05 22:45:03 +0000123 }
124};
125
126/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
127struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
128 PragmaSTDC_CX_LIMITED_RANGEHandler() : PragmaHandler("CX_LIMITED_RANGE") {}
129
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000130 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Steven Wub96a3a42018-01-05 22:45:03 +0000131 Token &Tok) override {
132 tok::OnOffSwitch OOS;
133 PP.LexOnOffSwitch(OOS);
134 }
135};
136
137/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
138struct PragmaSTDC_UnknownHandler : public PragmaHandler {
139 PragmaSTDC_UnknownHandler() = default;
140
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000141 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Steven Wub96a3a42018-01-05 22:45:03 +0000142 Token &UnknownTok) override {
143 // C99 6.10.6p2, unknown forms are not allowed.
144 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
145 }
146};
147
Adam Nemet60d32642017-04-04 21:18:36 +0000148struct PragmaFPHandler : public PragmaHandler {
149 PragmaFPHandler() : PragmaHandler("fp") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000150 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Adam Nemet60d32642017-04-04 21:18:36 +0000151 Token &FirstToken) override;
152};
153
Reid Kleckner5b086462014-02-20 22:52:09 +0000154struct PragmaNoOpenMPHandler : public PragmaHandler {
155 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000156 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +0000157 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000158};
159
160struct PragmaOpenMPHandler : public PragmaHandler {
161 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000162 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +0000163 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000164};
165
166/// PragmaCommentHandler - "\#pragma comment ...".
167struct PragmaCommentHandler : public PragmaHandler {
168 PragmaCommentHandler(Sema &Actions)
169 : PragmaHandler("comment"), Actions(Actions) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000170 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +0000171 Token &FirstToken) override;
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000172
Reid Kleckner5b086462014-02-20 22:52:09 +0000173private:
174 Sema &Actions;
175};
176
177struct PragmaDetectMismatchHandler : public PragmaHandler {
178 PragmaDetectMismatchHandler(Sema &Actions)
179 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000180 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +0000181 Token &FirstToken) override;
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000182
Reid Kleckner5b086462014-02-20 22:52:09 +0000183private:
184 Sema &Actions;
185};
186
187struct PragmaMSPointersToMembers : public PragmaHandler {
188 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000189 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +0000190 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000191};
192
193struct PragmaMSVtorDisp : public PragmaHandler {
194 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000195 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Craig Topper2b07f022014-03-12 05:09:18 +0000196 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000197};
198
Warren Huntc3b18962014-04-08 22:30:47 +0000199struct PragmaMSPragma : public PragmaHandler {
200 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000201 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000202 Token &FirstToken) override;
203};
204
Dario Domizioli13a0a382014-05-23 12:13:25 +0000205/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
206struct PragmaOptimizeHandler : public PragmaHandler {
207 PragmaOptimizeHandler(Sema &S)
208 : PragmaHandler("optimize"), Actions(S) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000209 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Dario Domizioli13a0a382014-05-23 12:13:25 +0000210 Token &FirstToken) override;
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000211
Dario Domizioli13a0a382014-05-23 12:13:25 +0000212private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000213 Sema &Actions;
214};
215
216struct PragmaLoopHintHandler : public PragmaHandler {
217 PragmaLoopHintHandler() : PragmaHandler("loop") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000218 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Eli Bendersky06a40422014-06-06 20:31:48 +0000219 Token &FirstToken) override;
220};
221
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000222struct PragmaUnrollHintHandler : public PragmaHandler {
223 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000224 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000225 Token &FirstToken) override;
226};
227
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000228struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
229 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
230};
231
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000232struct PragmaMSIntrinsicHandler : public PragmaHandler {
233 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000234 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000235 Token &FirstToken) override;
236};
237
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000238struct PragmaMSOptimizeHandler : public PragmaHandler {
239 PragmaMSOptimizeHandler() : PragmaHandler("optimize") {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000240 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000241 Token &FirstToken) override;
242};
243
Justin Lebar67a78a62016-10-08 22:15:58 +0000244struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
245 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
246 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000247 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Justin Lebar67a78a62016-10-08 22:15:58 +0000248 Token &FirstToken) override;
249
250private:
251 Sema &Actions;
252};
253
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000254/// PragmaAttributeHandler - "\#pragma clang attribute ...".
255struct PragmaAttributeHandler : public PragmaHandler {
256 PragmaAttributeHandler(AttributeFactory &AttrFactory)
257 : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {}
Joel E. Dennyddde0ec2019-05-21 23:51:38 +0000258 void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000259 Token &FirstToken) override;
260
261 /// A pool of attributes that were parsed in \#pragma clang attribute.
262 ParsedAttributes AttributesForPragmaAttribute;
263};
264
Eli Bendersky06a40422014-06-06 20:31:48 +0000265} // end namespace
266
267void Parser::initializePragmaHandlers() {
David Blaikiee0cfc042018-11-15 03:04:23 +0000268 AlignHandler = llvm::make_unique<PragmaAlignHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000269 PP.AddPragmaHandler(AlignHandler.get());
270
David Blaikiee0cfc042018-11-15 03:04:23 +0000271 GCCVisibilityHandler = llvm::make_unique<PragmaGCCVisibilityHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000272 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
273
David Blaikiee0cfc042018-11-15 03:04:23 +0000274 OptionsHandler = llvm::make_unique<PragmaOptionsHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000275 PP.AddPragmaHandler(OptionsHandler.get());
276
David Blaikiee0cfc042018-11-15 03:04:23 +0000277 PackHandler = llvm::make_unique<PragmaPackHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000278 PP.AddPragmaHandler(PackHandler.get());
279
David Blaikiee0cfc042018-11-15 03:04:23 +0000280 MSStructHandler = llvm::make_unique<PragmaMSStructHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000281 PP.AddPragmaHandler(MSStructHandler.get());
282
David Blaikiee0cfc042018-11-15 03:04:23 +0000283 UnusedHandler = llvm::make_unique<PragmaUnusedHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000284 PP.AddPragmaHandler(UnusedHandler.get());
285
David Blaikiee0cfc042018-11-15 03:04:23 +0000286 WeakHandler = llvm::make_unique<PragmaWeakHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000287 PP.AddPragmaHandler(WeakHandler.get());
288
David Blaikiee0cfc042018-11-15 03:04:23 +0000289 RedefineExtnameHandler = llvm::make_unique<PragmaRedefineExtnameHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000290 PP.AddPragmaHandler(RedefineExtnameHandler.get());
291
David Blaikiee0cfc042018-11-15 03:04:23 +0000292 FPContractHandler = llvm::make_unique<PragmaFPContractHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000293 PP.AddPragmaHandler("STDC", FPContractHandler.get());
294
David Blaikiee0cfc042018-11-15 03:04:23 +0000295 STDCFENVHandler = llvm::make_unique<PragmaSTDC_FENV_ACCESSHandler>();
Steven Wub96a3a42018-01-05 22:45:03 +0000296 PP.AddPragmaHandler("STDC", STDCFENVHandler.get());
297
David Blaikiee0cfc042018-11-15 03:04:23 +0000298 STDCCXLIMITHandler = llvm::make_unique<PragmaSTDC_CX_LIMITED_RANGEHandler>();
Steven Wub96a3a42018-01-05 22:45:03 +0000299 PP.AddPragmaHandler("STDC", STDCCXLIMITHandler.get());
300
David Blaikiee0cfc042018-11-15 03:04:23 +0000301 STDCUnknownHandler = llvm::make_unique<PragmaSTDC_UnknownHandler>();
Steven Wub96a3a42018-01-05 22:45:03 +0000302 PP.AddPragmaHandler("STDC", STDCUnknownHandler.get());
303
David Blaikiee0cfc042018-11-15 03:04:23 +0000304 PCSectionHandler = llvm::make_unique<PragmaClangSectionHandler>(Actions);
Javed Absar2a67c9e2017-06-05 10:11:57 +0000305 PP.AddPragmaHandler("clang", PCSectionHandler.get());
306
Reid Kleckner5b086462014-02-20 22:52:09 +0000307 if (getLangOpts().OpenCL) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000308 OpenCLExtensionHandler = llvm::make_unique<PragmaOpenCLExtensionHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000309 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
310
311 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
312 }
313 if (getLangOpts().OpenMP)
David Blaikiee0cfc042018-11-15 03:04:23 +0000314 OpenMPHandler = llvm::make_unique<PragmaOpenMPHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000315 else
David Blaikiee0cfc042018-11-15 03:04:23 +0000316 OpenMPHandler = llvm::make_unique<PragmaNoOpenMPHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000317 PP.AddPragmaHandler(OpenMPHandler.get());
318
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000319 if (getLangOpts().MicrosoftExt ||
320 getTargetInfo().getTriple().isOSBinFormatELF()) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000321 MSCommentHandler = llvm::make_unique<PragmaCommentHandler>(Actions);
Reid Kleckner5b086462014-02-20 22:52:09 +0000322 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000323 }
324
325 if (getLangOpts().MicrosoftExt) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000326 MSDetectMismatchHandler =
327 llvm::make_unique<PragmaDetectMismatchHandler>(Actions);
Reid Kleckner5b086462014-02-20 22:52:09 +0000328 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000329 MSPointersToMembers = llvm::make_unique<PragmaMSPointersToMembers>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000330 PP.AddPragmaHandler(MSPointersToMembers.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000331 MSVtorDisp = llvm::make_unique<PragmaMSVtorDisp>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000332 PP.AddPragmaHandler(MSVtorDisp.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000333 MSInitSeg = llvm::make_unique<PragmaMSPragma>("init_seg");
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000334 PP.AddPragmaHandler(MSInitSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000335 MSDataSeg = llvm::make_unique<PragmaMSPragma>("data_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000336 PP.AddPragmaHandler(MSDataSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000337 MSBSSSeg = llvm::make_unique<PragmaMSPragma>("bss_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000338 PP.AddPragmaHandler(MSBSSSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000339 MSConstSeg = llvm::make_unique<PragmaMSPragma>("const_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000340 PP.AddPragmaHandler(MSConstSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000341 MSCodeSeg = llvm::make_unique<PragmaMSPragma>("code_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000342 PP.AddPragmaHandler(MSCodeSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000343 MSSection = llvm::make_unique<PragmaMSPragma>("section");
Warren Huntc3b18962014-04-08 22:30:47 +0000344 PP.AddPragmaHandler(MSSection.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000345 MSRuntimeChecks = llvm::make_unique<PragmaMSRuntimeChecksHandler>();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000346 PP.AddPragmaHandler(MSRuntimeChecks.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000347 MSIntrinsic = llvm::make_unique<PragmaMSIntrinsicHandler>();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000348 PP.AddPragmaHandler(MSIntrinsic.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000349 MSOptimize = llvm::make_unique<PragmaMSOptimizeHandler>();
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000350 PP.AddPragmaHandler(MSOptimize.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000351 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000352
Justin Lebar67a78a62016-10-08 22:15:58 +0000353 if (getLangOpts().CUDA) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000354 CUDAForceHostDeviceHandler =
355 llvm::make_unique<PragmaForceCUDAHostDeviceHandler>(Actions);
Justin Lebar67a78a62016-10-08 22:15:58 +0000356 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
357 }
358
David Blaikiee0cfc042018-11-15 03:04:23 +0000359 OptimizeHandler = llvm::make_unique<PragmaOptimizeHandler>(Actions);
Eli Bendersky06a40422014-06-06 20:31:48 +0000360 PP.AddPragmaHandler("clang", OptimizeHandler.get());
361
David Blaikiee0cfc042018-11-15 03:04:23 +0000362 LoopHintHandler = llvm::make_unique<PragmaLoopHintHandler>();
Eli Bendersky06a40422014-06-06 20:31:48 +0000363 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000364
David Blaikiee0cfc042018-11-15 03:04:23 +0000365 UnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("unroll");
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000366 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000367
David Blaikiee0cfc042018-11-15 03:04:23 +0000368 NoUnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("nounroll");
Mark Heffernanc888e412014-07-24 18:09:38 +0000369 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Adam Nemet60d32642017-04-04 21:18:36 +0000370
David Blaikiee0cfc042018-11-15 03:04:23 +0000371 UnrollAndJamHintHandler =
372 llvm::make_unique<PragmaUnrollHintHandler>("unroll_and_jam");
David Greenc8e39242018-08-01 14:36:12 +0000373 PP.AddPragmaHandler(UnrollAndJamHintHandler.get());
374
David Blaikiee0cfc042018-11-15 03:04:23 +0000375 NoUnrollAndJamHintHandler =
376 llvm::make_unique<PragmaUnrollHintHandler>("nounroll_and_jam");
David Greenc8e39242018-08-01 14:36:12 +0000377 PP.AddPragmaHandler(NoUnrollAndJamHintHandler.get());
378
David Blaikiee0cfc042018-11-15 03:04:23 +0000379 FPHandler = llvm::make_unique<PragmaFPHandler>();
Adam Nemet60d32642017-04-04 21:18:36 +0000380 PP.AddPragmaHandler("clang", FPHandler.get());
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000381
David Blaikiee0cfc042018-11-15 03:04:23 +0000382 AttributePragmaHandler =
383 llvm::make_unique<PragmaAttributeHandler>(AttrFactory);
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000384 PP.AddPragmaHandler("clang", AttributePragmaHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000385}
386
387void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000388 // Remove the pragma handlers we installed.
389 PP.RemovePragmaHandler(AlignHandler.get());
390 AlignHandler.reset();
391 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
392 GCCVisibilityHandler.reset();
393 PP.RemovePragmaHandler(OptionsHandler.get());
394 OptionsHandler.reset();
395 PP.RemovePragmaHandler(PackHandler.get());
396 PackHandler.reset();
397 PP.RemovePragmaHandler(MSStructHandler.get());
398 MSStructHandler.reset();
399 PP.RemovePragmaHandler(UnusedHandler.get());
400 UnusedHandler.reset();
401 PP.RemovePragmaHandler(WeakHandler.get());
402 WeakHandler.reset();
403 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
404 RedefineExtnameHandler.reset();
405
406 if (getLangOpts().OpenCL) {
407 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
408 OpenCLExtensionHandler.reset();
409 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
410 }
411 PP.RemovePragmaHandler(OpenMPHandler.get());
412 OpenMPHandler.reset();
413
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000414 if (getLangOpts().MicrosoftExt ||
415 getTargetInfo().getTriple().isOSBinFormatELF()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000416 PP.RemovePragmaHandler(MSCommentHandler.get());
417 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000418 }
419
Javed Absar2a67c9e2017-06-05 10:11:57 +0000420 PP.RemovePragmaHandler("clang", PCSectionHandler.get());
421 PCSectionHandler.reset();
422
Yunzhong Gao99efc032015-03-23 20:41:42 +0000423 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000424 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
425 MSDetectMismatchHandler.reset();
426 PP.RemovePragmaHandler(MSPointersToMembers.get());
427 MSPointersToMembers.reset();
428 PP.RemovePragmaHandler(MSVtorDisp.get());
429 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000430 PP.RemovePragmaHandler(MSInitSeg.get());
431 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000432 PP.RemovePragmaHandler(MSDataSeg.get());
433 MSDataSeg.reset();
434 PP.RemovePragmaHandler(MSBSSSeg.get());
435 MSBSSSeg.reset();
436 PP.RemovePragmaHandler(MSConstSeg.get());
437 MSConstSeg.reset();
438 PP.RemovePragmaHandler(MSCodeSeg.get());
439 MSCodeSeg.reset();
440 PP.RemovePragmaHandler(MSSection.get());
441 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000442 PP.RemovePragmaHandler(MSRuntimeChecks.get());
443 MSRuntimeChecks.reset();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000444 PP.RemovePragmaHandler(MSIntrinsic.get());
445 MSIntrinsic.reset();
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000446 PP.RemovePragmaHandler(MSOptimize.get());
447 MSOptimize.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000448 }
449
Justin Lebar67a78a62016-10-08 22:15:58 +0000450 if (getLangOpts().CUDA) {
451 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
452 CUDAForceHostDeviceHandler.reset();
453 }
454
Reid Kleckner5b086462014-02-20 22:52:09 +0000455 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
456 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000457
Steven Wub96a3a42018-01-05 22:45:03 +0000458 PP.RemovePragmaHandler("STDC", STDCFENVHandler.get());
459 STDCFENVHandler.reset();
460
461 PP.RemovePragmaHandler("STDC", STDCCXLIMITHandler.get());
462 STDCCXLIMITHandler.reset();
463
464 PP.RemovePragmaHandler("STDC", STDCUnknownHandler.get());
465 STDCUnknownHandler.reset();
466
Eli Bendersky06a40422014-06-06 20:31:48 +0000467 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
468 OptimizeHandler.reset();
469
470 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
471 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000472
473 PP.RemovePragmaHandler(UnrollHintHandler.get());
474 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000475
476 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
477 NoUnrollHintHandler.reset();
Adam Nemet60d32642017-04-04 21:18:36 +0000478
David Greenc8e39242018-08-01 14:36:12 +0000479 PP.RemovePragmaHandler(UnrollAndJamHintHandler.get());
480 UnrollAndJamHintHandler.reset();
481
482 PP.RemovePragmaHandler(NoUnrollAndJamHintHandler.get());
483 NoUnrollAndJamHintHandler.reset();
484
Adam Nemet60d32642017-04-04 21:18:36 +0000485 PP.RemovePragmaHandler("clang", FPHandler.get());
486 FPHandler.reset();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000487
488 PP.RemovePragmaHandler("clang", AttributePragmaHandler.get());
489 AttributePragmaHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000490}
491
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000492/// Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000493///
494/// Each annot_pragma_unused is followed by the argument token so e.g.
495/// "#pragma unused(x,y)" becomes:
496/// annot_pragma_unused 'x' annot_pragma_unused 'y'
497void Parser::HandlePragmaUnused() {
498 assert(Tok.is(tok::annot_pragma_unused));
Richard Smithaf3b3252017-05-18 19:21:48 +0000499 SourceLocation UnusedLoc = ConsumeAnnotationToken();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000500 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
501 ConsumeToken(); // The argument token.
502}
Eli Friedman570024a2010-08-05 06:57:20 +0000503
Rafael Espindola273fd772012-01-26 02:02:57 +0000504void Parser::HandlePragmaVisibility() {
505 assert(Tok.is(tok::annot_pragma_vis));
506 const IdentifierInfo *VisType =
507 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
Richard Smithaf3b3252017-05-18 19:21:48 +0000508 SourceLocation VisLoc = ConsumeAnnotationToken();
Rafael Espindola273fd772012-01-26 02:02:57 +0000509 Actions.ActOnPragmaVisibility(VisType, VisLoc);
510}
511
Benjamin Kramere003ca22015-10-28 13:54:16 +0000512namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000513struct PragmaPackInfo {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000514 Sema::PragmaMsStackAction Action;
515 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000516 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000517};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000518} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000519
520void Parser::HandlePragmaPack() {
521 assert(Tok.is(tok::annot_pragma_pack));
522 PragmaPackInfo *Info =
523 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
Alex Lorenz45b40142017-07-28 14:41:21 +0000524 SourceLocation PragmaLoc = Tok.getLocation();
Eli Friedman68be1642012-10-04 02:36:51 +0000525 ExprResult Alignment;
526 if (Info->Alignment.is(tok::numeric_constant)) {
527 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
Alex Lorenz45b40142017-07-28 14:41:21 +0000528 if (Alignment.isInvalid()) {
529 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000530 return;
Alex Lorenz45b40142017-07-28 14:41:21 +0000531 }
Eli Friedman68be1642012-10-04 02:36:51 +0000532 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000533 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
534 Alignment.get());
Alex Lorenz45b40142017-07-28 14:41:21 +0000535 // Consume the token after processing the pragma to enable pragma-specific
536 // #include warnings.
537 ConsumeAnnotationToken();
Eli Friedmanec52f922012-02-23 23:47:16 +0000538}
539
Eli Friedman68be1642012-10-04 02:36:51 +0000540void Parser::HandlePragmaMSStruct() {
541 assert(Tok.is(tok::annot_pragma_msstruct));
Nico Weber779355f2016-03-02 23:22:00 +0000542 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
543 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Eli Friedman68be1642012-10-04 02:36:51 +0000544 Actions.ActOnPragmaMSStruct(Kind);
Richard Smithaf3b3252017-05-18 19:21:48 +0000545 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000546}
547
548void Parser::HandlePragmaAlign() {
549 assert(Tok.is(tok::annot_pragma_align));
550 Sema::PragmaOptionsAlignKind Kind =
551 static_cast<Sema::PragmaOptionsAlignKind>(
552 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Alex Lorenz692821a2018-02-08 21:20:43 +0000553 Actions.ActOnPragmaOptionsAlign(Kind, Tok.getLocation());
554 // Consume the token after processing the pragma to enable pragma-specific
555 // #include warnings.
556 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000557}
558
Richard Smithba3a4f92016-01-12 21:59:26 +0000559void Parser::HandlePragmaDump() {
560 assert(Tok.is(tok::annot_pragma_dump));
561 IdentifierInfo *II =
562 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
563 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
Richard Smithaf3b3252017-05-18 19:21:48 +0000564 ConsumeAnnotationToken();
Richard Smithba3a4f92016-01-12 21:59:26 +0000565}
566
Eli Friedman68be1642012-10-04 02:36:51 +0000567void Parser::HandlePragmaWeak() {
568 assert(Tok.is(tok::annot_pragma_weak));
Richard Smithaf3b3252017-05-18 19:21:48 +0000569 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000570 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
571 Tok.getLocation());
572 ConsumeToken(); // The weak name.
573}
574
575void Parser::HandlePragmaWeakAlias() {
576 assert(Tok.is(tok::annot_pragma_weakalias));
Richard Smithaf3b3252017-05-18 19:21:48 +0000577 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000578 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
579 SourceLocation WeakNameLoc = Tok.getLocation();
580 ConsumeToken();
581 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
582 SourceLocation AliasNameLoc = Tok.getLocation();
583 ConsumeToken();
584 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
585 WeakNameLoc, AliasNameLoc);
586
587}
588
589void Parser::HandlePragmaRedefineExtname() {
590 assert(Tok.is(tok::annot_pragma_redefine_extname));
Richard Smithaf3b3252017-05-18 19:21:48 +0000591 SourceLocation RedefLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000592 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
593 SourceLocation RedefNameLoc = Tok.getLocation();
594 ConsumeToken();
595 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
596 SourceLocation AliasNameLoc = Tok.getLocation();
597 ConsumeToken();
598 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
599 RedefNameLoc, AliasNameLoc);
600}
601
602void Parser::HandlePragmaFPContract() {
603 assert(Tok.is(tok::annot_pragma_fp_contract));
604 tok::OnOffSwitch OOS =
605 static_cast<tok::OnOffSwitch>(
606 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Adam Nemet60d32642017-04-04 21:18:36 +0000607
608 LangOptions::FPContractModeKind FPC;
609 switch (OOS) {
610 case tok::OOS_ON:
611 FPC = LangOptions::FPC_On;
612 break;
613 case tok::OOS_OFF:
614 FPC = LangOptions::FPC_Off;
615 break;
616 case tok::OOS_DEFAULT:
617 FPC = getLangOpts().getDefaultFPContractMode();
618 break;
619 }
620
621 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +0000622 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000623}
624
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000625void Parser::HandlePragmaFEnvAccess() {
626 assert(Tok.is(tok::annot_pragma_fenv_access));
627 tok::OnOffSwitch OOS =
628 static_cast<tok::OnOffSwitch>(
629 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
630
631 LangOptions::FEnvAccessModeKind FPC;
632 switch (OOS) {
633 case tok::OOS_ON:
634 FPC = LangOptions::FEA_On;
635 break;
636 case tok::OOS_OFF:
637 FPC = LangOptions::FEA_Off;
638 break;
639 case tok::OOS_DEFAULT: // FIXME: Add this cli option when it makes sense.
640 FPC = LangOptions::FEA_Off;
641 break;
642 }
643
644 Actions.ActOnPragmaFEnvAccess(FPC);
645 ConsumeAnnotationToken();
646}
647
648
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000649StmtResult Parser::HandlePragmaCaptured()
650{
651 assert(Tok.is(tok::annot_pragma_captured));
Richard Smithaf3b3252017-05-18 19:21:48 +0000652 ConsumeAnnotationToken();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000653
654 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000655 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000656 return StmtError();
657 }
658
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000659 SourceLocation Loc = Tok.getLocation();
660
Momchil Velikov57c681f2017-08-10 15:43:06 +0000661 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope |
662 Scope::CompoundStmtScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000663 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
664 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000665
666 StmtResult R = ParseCompoundStatement();
667 CapturedRegionScope.Exit();
668
669 if (R.isInvalid()) {
670 Actions.ActOnCapturedRegionError();
671 return StmtError();
672 }
673
674 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000675}
676
Eli Friedman68be1642012-10-04 02:36:51 +0000677namespace {
Yaxun Liu5b746652016-12-18 05:18:55 +0000678 enum OpenCLExtState : char {
679 Disable, Enable, Begin, End
680 };
681 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
Eli Friedman68be1642012-10-04 02:36:51 +0000682}
683
684void Parser::HandlePragmaOpenCLExtension() {
685 assert(Tok.is(tok::annot_pragma_opencl_extension));
Yaxun Liu5b746652016-12-18 05:18:55 +0000686 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
687 auto State = Data->second;
688 auto Ident = Data->first;
Eli Friedman68be1642012-10-04 02:36:51 +0000689 SourceLocation NameLoc = Tok.getLocation();
Richard Smithaf3b3252017-05-18 19:21:48 +0000690 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000691
Yaxun Liu5b746652016-12-18 05:18:55 +0000692 auto &Opt = Actions.getOpenCLOptions();
693 auto Name = Ident->getName();
Eli Friedman68be1642012-10-04 02:36:51 +0000694 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
695 // overriding all previously issued extension directives, but only if the
696 // behavior is set to disable."
Yaxun Liu5b746652016-12-18 05:18:55 +0000697 if (Name == "all") {
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000698 if (State == Disable) {
Yaxun Liu5b746652016-12-18 05:18:55 +0000699 Opt.disableAll();
Anastasia Stulovae88e2b92019-02-07 17:32:37 +0000700 Opt.enableSupportedCore(getLangOpts());
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000701 } else {
Yaxun Liu5b746652016-12-18 05:18:55 +0000702 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000703 }
Yaxun Liu5b746652016-12-18 05:18:55 +0000704 } else if (State == Begin) {
Anastasia Stulovae88e2b92019-02-07 17:32:37 +0000705 if (!Opt.isKnown(Name) || !Opt.isSupported(Name, getLangOpts())) {
Yaxun Liu5b746652016-12-18 05:18:55 +0000706 Opt.support(Name);
707 }
708 Actions.setCurrentOpenCLExtension(Name);
709 } else if (State == End) {
710 if (Name != Actions.getCurrentOpenCLExtension())
711 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
712 Actions.setCurrentOpenCLExtension("");
713 } else if (!Opt.isKnown(Name))
714 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
Anastasia Stulovae88e2b92019-02-07 17:32:37 +0000715 else if (Opt.isSupportedExtension(Name, getLangOpts()))
Yaxun Liu5b746652016-12-18 05:18:55 +0000716 Opt.enable(Name, State == Enable);
Anastasia Stulovae88e2b92019-02-07 17:32:37 +0000717 else if (Opt.isSupportedCore(Name, getLangOpts()))
Yaxun Liu5b746652016-12-18 05:18:55 +0000718 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
719 else
720 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
Eli Friedman68be1642012-10-04 02:36:51 +0000721}
722
David Majnemer4bb09802014-02-10 19:50:15 +0000723void Parser::HandlePragmaMSPointersToMembers() {
724 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000725 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
726 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000727 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Richard Smithaf3b3252017-05-18 19:21:48 +0000728 SourceLocation PragmaLoc = ConsumeAnnotationToken();
David Majnemer4bb09802014-02-10 19:50:15 +0000729 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
730}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000731
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000732void Parser::HandlePragmaMSVtorDisp() {
733 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
734 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
Denis Zobnin2290dac2016-04-29 11:27:00 +0000735 Sema::PragmaMsStackAction Action =
736 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000737 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
Richard Smithaf3b3252017-05-18 19:21:48 +0000738 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Denis Zobnin2290dac2016-04-29 11:27:00 +0000739 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000740}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000741
Warren Huntc3b18962014-04-08 22:30:47 +0000742void Parser::HandlePragmaMSPragma() {
743 assert(Tok.is(tok::annot_pragma_ms_pragma));
744 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000745 auto TheTokens =
746 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
Ilya Biryukov929af672019-05-17 09:32:05 +0000747 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true,
748 /*IsReinject=*/true);
Richard Smithaf3b3252017-05-18 19:21:48 +0000749 SourceLocation PragmaLocation = ConsumeAnnotationToken();
Warren Huntc3b18962014-04-08 22:30:47 +0000750 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000751 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000752 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000753
Warren Huntc3b18962014-04-08 22:30:47 +0000754 // Figure out which #pragma we're dealing with. The switch has no default
755 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000756 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000757 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
758 .Case("data_seg", &Parser::HandlePragmaMSSegment)
759 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
760 .Case("const_seg", &Parser::HandlePragmaMSSegment)
761 .Case("code_seg", &Parser::HandlePragmaMSSegment)
762 .Case("section", &Parser::HandlePragmaMSSection)
763 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000764
765 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
766 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
767 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000768 while (Tok.isNot(tok::eof))
769 PP.Lex(Tok);
770 PP.Lex(Tok);
771 }
772}
773
Reid Kleckner722b1df2014-07-18 00:13:16 +0000774bool Parser::HandlePragmaMSSection(StringRef PragmaName,
775 SourceLocation PragmaLocation) {
776 if (Tok.isNot(tok::l_paren)) {
777 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
778 return false;
779 }
Warren Huntc3b18962014-04-08 22:30:47 +0000780 PP.Lex(Tok); // (
781 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000782 if (Tok.isNot(tok::string_literal)) {
783 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
784 << PragmaName;
785 return false;
786 }
787 ExprResult StringResult = ParseStringLiteralExpression();
788 if (StringResult.isInvalid())
789 return false; // Already diagnosed.
790 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
791 if (SegmentName->getCharByteWidth() != 1) {
792 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
793 << PragmaName;
794 return false;
795 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000796 int SectionFlags = ASTContext::PSF_Read;
797 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000798 while (Tok.is(tok::comma)) {
799 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000800 // Ignore "long" and "short".
801 // They are undocumented, but widely used, section attributes which appear
802 // to do nothing.
803 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
804 PP.Lex(Tok); // long/short
805 continue;
806 }
807
Reid Kleckner722b1df2014-07-18 00:13:16 +0000808 if (!Tok.isAnyIdentifier()) {
809 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
810 << PragmaName;
811 return false;
812 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000813 ASTContext::PragmaSectionFlag Flag =
814 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000815 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000816 .Case("read", ASTContext::PSF_Read)
817 .Case("write", ASTContext::PSF_Write)
818 .Case("execute", ASTContext::PSF_Execute)
819 .Case("shared", ASTContext::PSF_Invalid)
820 .Case("nopage", ASTContext::PSF_Invalid)
821 .Case("nocache", ASTContext::PSF_Invalid)
822 .Case("discard", ASTContext::PSF_Invalid)
823 .Case("remove", ASTContext::PSF_Invalid)
824 .Default(ASTContext::PSF_None);
825 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
826 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000827 ? diag::warn_pragma_invalid_specific_action
828 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000829 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000830 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000831 }
832 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000833 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000834 PP.Lex(Tok); // Identifier
835 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000836 // If no section attributes are specified, the section will be marked as
837 // read/write.
838 if (SectionFlagsAreDefault)
839 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000840 if (Tok.isNot(tok::r_paren)) {
841 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
842 return false;
843 }
Warren Huntc3b18962014-04-08 22:30:47 +0000844 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000845 if (Tok.isNot(tok::eof)) {
846 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
847 << PragmaName;
848 return false;
849 }
Warren Huntc3b18962014-04-08 22:30:47 +0000850 PP.Lex(Tok); // eof
851 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000852 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000853}
854
Reid Kleckner722b1df2014-07-18 00:13:16 +0000855bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
856 SourceLocation PragmaLocation) {
857 if (Tok.isNot(tok::l_paren)) {
858 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
859 return false;
860 }
Warren Huntc3b18962014-04-08 22:30:47 +0000861 PP.Lex(Tok); // (
862 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000863 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000864 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000865 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000866 if (PushPop == "push")
867 Action = Sema::PSK_Push;
868 else if (PushPop == "pop")
869 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000870 else {
871 PP.Diag(PragmaLocation,
872 diag::warn_pragma_expected_section_push_pop_or_name)
873 << PragmaName;
874 return false;
875 }
Warren Huntc3b18962014-04-08 22:30:47 +0000876 if (Action != Sema::PSK_Reset) {
877 PP.Lex(Tok); // push | pop
878 if (Tok.is(tok::comma)) {
879 PP.Lex(Tok); // ,
880 // If we've got a comma, we either need a label or a string.
881 if (Tok.isAnyIdentifier()) {
882 SlotLabel = Tok.getIdentifierInfo()->getName();
883 PP.Lex(Tok); // identifier
884 if (Tok.is(tok::comma))
885 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000886 else if (Tok.isNot(tok::r_paren)) {
887 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
888 << PragmaName;
889 return false;
890 }
Warren Huntc3b18962014-04-08 22:30:47 +0000891 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000892 } else if (Tok.isNot(tok::r_paren)) {
893 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
894 return false;
895 }
Warren Huntc3b18962014-04-08 22:30:47 +0000896 }
897 }
898 // Grab the string literal for our section name.
899 StringLiteral *SegmentName = nullptr;
900 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000901 if (Tok.isNot(tok::string_literal)) {
902 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000903 diag::warn_pragma_expected_section_name :
904 diag::warn_pragma_expected_section_label_or_name :
905 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000906 PP.Diag(PragmaLocation, DiagID) << PragmaName;
907 return false;
908 }
909 ExprResult StringResult = ParseStringLiteralExpression();
910 if (StringResult.isInvalid())
911 return false; // Already diagnosed.
912 SegmentName = cast<StringLiteral>(StringResult.get());
913 if (SegmentName->getCharByteWidth() != 1) {
914 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
915 << PragmaName;
916 return false;
917 }
Warren Huntc3b18962014-04-08 22:30:47 +0000918 // Setting section "" has no effect
919 if (SegmentName->getLength())
920 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
921 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000922 if (Tok.isNot(tok::r_paren)) {
923 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
924 return false;
925 }
Warren Huntc3b18962014-04-08 22:30:47 +0000926 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000927 if (Tok.isNot(tok::eof)) {
928 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
929 << PragmaName;
930 return false;
931 }
Warren Huntc3b18962014-04-08 22:30:47 +0000932 PP.Lex(Tok); // eof
933 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
934 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000935 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000936}
937
Reid Kleckner1a711b12014-07-22 00:53:05 +0000938// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000939bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
940 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000941 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
942 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
943 return false;
944 }
945
Reid Kleckner1a711b12014-07-22 00:53:05 +0000946 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
947 PragmaName))
948 return false;
949
950 // Parse either the known section names or the string section name.
951 StringLiteral *SegmentName = nullptr;
952 if (Tok.isAnyIdentifier()) {
953 auto *II = Tok.getIdentifierInfo();
954 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
955 .Case("compiler", "\".CRT$XCC\"")
956 .Case("lib", "\".CRT$XCL\"")
957 .Case("user", "\".CRT$XCU\"")
958 .Default("");
959
960 if (!Section.empty()) {
961 // Pretend the user wrote the appropriate string literal here.
962 Token Toks[1];
963 Toks[0].startToken();
964 Toks[0].setKind(tok::string_literal);
965 Toks[0].setLocation(Tok.getLocation());
966 Toks[0].setLiteralData(Section.data());
967 Toks[0].setLength(Section.size());
968 SegmentName =
969 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
970 PP.Lex(Tok);
971 }
972 } else if (Tok.is(tok::string_literal)) {
973 ExprResult StringResult = ParseStringLiteralExpression();
974 if (StringResult.isInvalid())
975 return false;
976 SegmentName = cast<StringLiteral>(StringResult.get());
977 if (SegmentName->getCharByteWidth() != 1) {
978 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
979 << PragmaName;
980 return false;
981 }
982 // FIXME: Add support for the '[, func-name]' part of the pragma.
983 }
984
985 if (!SegmentName) {
986 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
987 return false;
988 }
989
990 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
991 PragmaName) ||
992 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
993 PragmaName))
994 return false;
995
996 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
997 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000998}
999
Benjamin Kramere003ca22015-10-28 13:54:16 +00001000namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +00001001struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001002 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +00001003 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00001004 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +00001005};
Benjamin Kramere003ca22015-10-28 13:54:16 +00001006} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +00001007
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001008static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
1009 std::string PragmaString;
1010 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1011 PragmaString = "clang loop ";
1012 PragmaString += Option.getIdentifierInfo()->getName();
David Greenc8e39242018-08-01 14:36:12 +00001013 } else if (PragmaName.getIdentifierInfo()->getName() == "unroll_and_jam") {
1014 PragmaString = "unroll_and_jam";
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001015 } else {
1016 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1017 "Unexpected pragma name");
1018 PragmaString = "unroll";
1019 }
1020 return PragmaString;
1021}
1022
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001023bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001024 assert(Tok.is(tok::annot_pragma_loop_hint));
1025 PragmaLoopHintInfo *Info =
1026 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
1027
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001028 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
1029 Hint.PragmaNameLoc = IdentifierLoc::create(
1030 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +00001031
Aaron Ballmanef940aa2014-07-31 21:24:32 +00001032 // It is possible that the loop hint has no option identifier, such as
1033 // #pragma unroll(4).
1034 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
1035 ? Info->Option.getIdentifierInfo()
1036 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001037 Hint.OptionLoc = IdentifierLoc::create(
1038 Actions.Context, Info->Option.getLocation(), OptionInfo);
1039
David Blaikie2eabcc92016-02-09 18:52:09 +00001040 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001041
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001042 // Return a valid hint if pragma unroll or nounroll were specified
1043 // without an argument.
1044 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
1045 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Greenc8e39242018-08-01 14:36:12 +00001046 bool PragmaUnrollAndJam = PragmaNameInfo->getName() == "unroll_and_jam";
1047 bool PragmaNoUnrollAndJam = PragmaNameInfo->getName() == "nounroll_and_jam";
1048 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll || PragmaUnrollAndJam ||
1049 PragmaNoUnrollAndJam)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001050 ConsumeAnnotationToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001051 Hint.Range = Info->PragmaName.getLocation();
1052 return true;
1053 }
1054
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001055 // The constant expression is always followed by an eof token, which increases
1056 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +00001057 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001058 "PragmaLoopHintInfo::Toks must contain at least one token.");
1059
1060 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +00001061 bool OptionUnroll = false;
David Greenc8e39242018-08-01 14:36:12 +00001062 bool OptionUnrollAndJam = false;
Adam Nemet2de463e2016-06-14 12:04:26 +00001063 bool OptionDistribute = false;
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001064 bool OptionPipelineDisabled = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001065 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +00001066 if (OptionInfo) { // Pragma Unroll does not specify an option.
1067 OptionUnroll = OptionInfo->isStr("unroll");
David Greenc8e39242018-08-01 14:36:12 +00001068 OptionUnrollAndJam = OptionInfo->isStr("unroll_and_jam");
Adam Nemet2de463e2016-06-14 12:04:26 +00001069 OptionDistribute = OptionInfo->isStr("distribute");
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001070 OptionPipelineDisabled = OptionInfo->isStr("pipeline");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001071 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
1072 .Case("vectorize", true)
1073 .Case("interleave", true)
Sjoerd Meijera48f58c2019-07-25 07:33:13 +00001074 .Case("vectorize_predicate", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00001075 .Default(false) ||
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001076 OptionUnroll || OptionUnrollAndJam || OptionDistribute ||
1077 OptionPipelineDisabled;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001078 }
1079
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001080 bool AssumeSafetyArg = !OptionUnroll && !OptionUnrollAndJam &&
1081 !OptionDistribute && !OptionPipelineDisabled;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001082 // Verify loop hint has an argument.
1083 if (Toks[0].is(tok::eof)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001084 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001085 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
David Greenc8e39242018-08-01 14:36:12 +00001086 << /*StateArgument=*/StateOption
1087 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
Adam Nemet2de463e2016-06-14 12:04:26 +00001088 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001089 return false;
1090 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001091
1092 // Validate the argument.
1093 if (StateOption) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001094 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001095 SourceLocation StateLoc = Toks[0].getLocation();
1096 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Adam Nemet50de4e82016-04-19 22:17:45 +00001097
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001098 bool Valid = StateInfo &&
1099 llvm::StringSwitch<bool>(StateInfo->getName())
1100 .Case("disable", true)
1101 .Case("enable", !OptionPipelineDisabled)
1102 .Case("full", OptionUnroll || OptionUnrollAndJam)
1103 .Case("assume_safety", AssumeSafetyArg)
1104 .Default(false);
Adam Nemet50de4e82016-04-19 22:17:45 +00001105 if (!Valid) {
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001106 if (OptionPipelineDisabled) {
1107 Diag(Toks[0].getLocation(), diag::err_pragma_pipeline_invalid_keyword);
1108 } else {
1109 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
1110 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
1111 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
1112 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001113 return false;
1114 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001115 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001116 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1117 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001118 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
1119 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001120 // Enter constant expression including eof terminator into token stream.
Ilya Biryukov929af672019-05-17 09:32:05 +00001121 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false,
1122 /*IsReinject=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001123 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001124
1125 ExprResult R = ParseConstantExpression();
1126
1127 // Tokens following an error in an ill-formed constant expression will
1128 // remain in the token stream and must be removed.
1129 if (Tok.isNot(tok::eof)) {
1130 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1131 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1132 while (Tok.isNot(tok::eof))
1133 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001134 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001135
1136 ConsumeToken(); // Consume the constant expression eof terminator.
1137
1138 if (R.isInvalid() ||
1139 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
1140 return false;
1141
1142 // Argument is a constant expression with an integer type.
1143 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001144 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001145
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001146 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +00001147 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001148 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +00001149}
1150
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001151namespace {
1152struct PragmaAttributeInfo {
Erik Pilkington7d180942018-10-29 17:38:42 +00001153 enum ActionType { Push, Pop, Attribute };
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001154 ParsedAttributes &Attributes;
1155 ActionType Action;
Erik Pilkington0876cae2018-12-20 22:32:04 +00001156 const IdentifierInfo *Namespace = nullptr;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001157 ArrayRef<Token> Tokens;
1158
1159 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1160};
1161
1162#include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1163
1164} // end anonymous namespace
1165
1166static StringRef getIdentifier(const Token &Tok) {
1167 if (Tok.is(tok::identifier))
1168 return Tok.getIdentifierInfo()->getName();
1169 const char *S = tok::getKeywordSpelling(Tok.getKind());
1170 if (!S)
1171 return "";
1172 return S;
1173}
1174
1175static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1176 using namespace attr;
1177 switch (Rule) {
1178#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1179 case Value: \
1180 return IsAbstract;
1181#include "clang/Basic/AttrSubMatchRulesList.inc"
1182 }
1183 llvm_unreachable("Invalid attribute subject match rule");
1184 return false;
1185}
1186
1187static void diagnoseExpectedAttributeSubjectSubRule(
1188 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1189 SourceLocation SubRuleLoc) {
1190 auto Diagnostic =
1191 PRef.Diag(SubRuleLoc,
1192 diag::err_pragma_attribute_expected_subject_sub_identifier)
1193 << PrimaryRuleName;
1194 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1195 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1196 else
1197 Diagnostic << /*SubRulesSupported=*/0;
1198}
1199
1200static void diagnoseUnknownAttributeSubjectSubRule(
1201 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1202 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1203
1204 auto Diagnostic =
1205 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1206 << SubRuleName << PrimaryRuleName;
1207 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1208 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1209 else
1210 Diagnostic << /*SubRulesSupported=*/0;
1211}
1212
1213bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1214 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1215 SourceLocation &LastMatchRuleEndLoc) {
1216 bool IsAny = false;
1217 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1218 if (getIdentifier(Tok) == "any") {
1219 AnyLoc = ConsumeToken();
1220 IsAny = true;
1221 if (AnyParens.expectAndConsume())
1222 return true;
1223 }
1224
1225 do {
1226 // Parse the subject matcher rule.
1227 StringRef Name = getIdentifier(Tok);
1228 if (Name.empty()) {
1229 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1230 return true;
1231 }
1232 std::pair<Optional<attr::SubjectMatchRule>,
1233 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1234 Rule = isAttributeSubjectMatchRule(Name);
1235 if (!Rule.first) {
1236 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1237 return true;
1238 }
1239 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1240 SourceLocation RuleLoc = ConsumeToken();
1241
1242 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1243 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1244 if (Parens.expectAndConsume())
1245 return true;
1246 } else if (Parens.consumeOpen()) {
1247 if (!SubjectMatchRules
1248 .insert(
1249 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1250 .second)
1251 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1252 << Name
1253 << FixItHint::CreateRemoval(SourceRange(
1254 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1255 LastMatchRuleEndLoc = RuleLoc;
1256 continue;
1257 }
1258
1259 // Parse the sub-rules.
1260 StringRef SubRuleName = getIdentifier(Tok);
1261 if (SubRuleName.empty()) {
1262 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1263 Tok.getLocation());
1264 return true;
1265 }
1266 attr::SubjectMatchRule SubRule;
1267 if (SubRuleName == "unless") {
1268 SourceLocation SubRuleLoc = ConsumeToken();
1269 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1270 if (Parens.expectAndConsume())
1271 return true;
1272 SubRuleName = getIdentifier(Tok);
1273 if (SubRuleName.empty()) {
1274 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1275 SubRuleLoc);
1276 return true;
1277 }
1278 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1279 if (!SubRuleOrNone) {
1280 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1281 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1282 SubRuleUnlessName, SubRuleLoc);
1283 return true;
1284 }
1285 SubRule = *SubRuleOrNone;
1286 ConsumeToken();
1287 if (Parens.consumeClose())
1288 return true;
1289 } else {
1290 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1291 if (!SubRuleOrNone) {
1292 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1293 SubRuleName, Tok.getLocation());
1294 return true;
1295 }
1296 SubRule = *SubRuleOrNone;
1297 ConsumeToken();
1298 }
1299 SourceLocation RuleEndLoc = Tok.getLocation();
1300 LastMatchRuleEndLoc = RuleEndLoc;
1301 if (Parens.consumeClose())
1302 return true;
1303 if (!SubjectMatchRules
1304 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1305 .second) {
1306 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1307 << attr::getSubjectMatchRuleSpelling(SubRule)
1308 << FixItHint::CreateRemoval(SourceRange(
1309 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1310 continue;
1311 }
1312 } while (IsAny && TryConsumeToken(tok::comma));
1313
1314 if (IsAny)
1315 if (AnyParens.consumeClose())
1316 return true;
1317
1318 return false;
1319}
1320
1321namespace {
1322
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001323/// Describes the stage at which attribute subject rule parsing was interrupted.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001324enum class MissingAttributeSubjectRulesRecoveryPoint {
1325 Comma,
1326 ApplyTo,
1327 Equals,
1328 Any,
1329 None,
1330};
1331
1332MissingAttributeSubjectRulesRecoveryPoint
1333getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1334 if (const auto *II = Tok.getIdentifierInfo()) {
1335 if (II->isStr("apply_to"))
1336 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1337 if (II->isStr("any"))
1338 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1339 }
1340 if (Tok.is(tok::equal))
1341 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1342 return MissingAttributeSubjectRulesRecoveryPoint::None;
1343}
1344
1345/// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1346/// suggests the possible attribute subject rules in a fix-it together with
1347/// any other missing tokens.
1348DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
Erich Keanee891aa92018-07-13 15:07:47 +00001349 unsigned DiagID, ParsedAttr &Attribute,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001350 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1351 SourceLocation Loc = PRef.getEndOfPreviousToken();
1352 if (Loc.isInvalid())
1353 Loc = PRef.getCurToken().getLocation();
1354 auto Diagnostic = PRef.Diag(Loc, DiagID);
1355 std::string FixIt;
1356 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1357 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1358 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1359 FixIt = ", ";
1360 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1361 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1362 FixIt += "apply_to";
1363 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1364 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1365 FixIt += " = ";
1366 SourceRange FixItRange(Loc);
1367 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1368 // Gather the subject match rules that are supported by the attribute.
1369 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1370 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1371 if (SubjectMatchRuleSet.empty()) {
1372 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1373 // placeholders will be supported by the fix-its.
1374 return Diagnostic;
1375 }
1376 FixIt += "any(";
1377 bool NeedsComma = false;
1378 for (const auto &I : SubjectMatchRuleSet) {
1379 // Ensure that the missing rule is reported in the fix-it only when it's
1380 // supported in the current language mode.
1381 if (!I.second)
1382 continue;
1383 if (NeedsComma)
1384 FixIt += ", ";
1385 else
1386 NeedsComma = true;
1387 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1388 }
1389 FixIt += ")";
1390 // Check if we need to remove the range
1391 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1392 FixItRange.setEnd(PRef.getCurToken().getLocation());
1393 }
1394 if (FixItRange.getBegin() == FixItRange.getEnd())
1395 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1396 else
1397 Diagnostic << FixItHint::CreateReplacement(
1398 CharSourceRange::getCharRange(FixItRange), FixIt);
1399 return Diagnostic;
1400}
1401
1402} // end anonymous namespace
1403
1404void Parser::HandlePragmaAttribute() {
1405 assert(Tok.is(tok::annot_pragma_attribute) &&
1406 "Expected #pragma attribute annotation token");
1407 SourceLocation PragmaLoc = Tok.getLocation();
1408 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1409 if (Info->Action == PragmaAttributeInfo::Pop) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001410 ConsumeAnnotationToken();
Erik Pilkington0876cae2018-12-20 22:32:04 +00001411 Actions.ActOnPragmaAttributePop(PragmaLoc, Info->Namespace);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001412 return;
1413 }
1414 // Parse the actual attribute with its arguments.
Erik Pilkington7d180942018-10-29 17:38:42 +00001415 assert((Info->Action == PragmaAttributeInfo::Push ||
1416 Info->Action == PragmaAttributeInfo::Attribute) &&
Erik Pilkingtonb287a012018-10-29 03:24:16 +00001417 "Unexpected #pragma attribute command");
Erik Pilkington7d180942018-10-29 17:38:42 +00001418
1419 if (Info->Action == PragmaAttributeInfo::Push && Info->Tokens.empty()) {
1420 ConsumeAnnotationToken();
Erik Pilkington0876cae2018-12-20 22:32:04 +00001421 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace);
Erik Pilkington7d180942018-10-29 17:38:42 +00001422 return;
1423 }
1424
Ilya Biryukov929af672019-05-17 09:32:05 +00001425 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false,
1426 /*IsReinject=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001427 ConsumeAnnotationToken();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001428
1429 ParsedAttributes &Attrs = Info->Attributes;
1430 Attrs.clearListOnly();
1431
1432 auto SkipToEnd = [this]() {
1433 SkipUntil(tok::eof, StopBeforeMatch);
1434 ConsumeToken();
1435 };
1436
1437 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1438 // Parse the CXX11 style attribute.
1439 ParseCXX11AttributeSpecifier(Attrs);
1440 } else if (Tok.is(tok::kw___attribute)) {
1441 ConsumeToken();
1442 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1443 "attribute"))
1444 return SkipToEnd();
1445 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1446 return SkipToEnd();
1447
1448 if (Tok.isNot(tok::identifier)) {
1449 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1450 SkipToEnd();
1451 return;
1452 }
1453 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1454 SourceLocation AttrNameLoc = ConsumeToken();
1455
1456 if (Tok.isNot(tok::l_paren))
1457 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +00001458 ParsedAttr::AS_GNU);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001459 else
1460 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1461 /*ScopeName=*/nullptr,
Erich Keanee891aa92018-07-13 15:07:47 +00001462 /*ScopeLoc=*/SourceLocation(), ParsedAttr::AS_GNU,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001463 /*Declarator=*/nullptr);
1464
1465 if (ExpectAndConsume(tok::r_paren))
1466 return SkipToEnd();
1467 if (ExpectAndConsume(tok::r_paren))
1468 return SkipToEnd();
1469 } else if (Tok.is(tok::kw___declspec)) {
1470 ParseMicrosoftDeclSpecs(Attrs);
1471 } else {
1472 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1473 if (Tok.getIdentifierInfo()) {
1474 // If we suspect that this is an attribute suggest the use of
1475 // '__attribute__'.
Erich Keanee891aa92018-07-13 15:07:47 +00001476 if (ParsedAttr::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1477 ParsedAttr::AS_GNU) !=
1478 ParsedAttr::UnknownAttribute) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001479 SourceLocation InsertStartLoc = Tok.getLocation();
1480 ConsumeToken();
1481 if (Tok.is(tok::l_paren)) {
1482 ConsumeAnyToken();
1483 SkipUntil(tok::r_paren, StopBeforeMatch);
1484 if (Tok.isNot(tok::r_paren))
1485 return SkipToEnd();
1486 }
1487 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1488 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1489 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1490 }
1491 }
1492 SkipToEnd();
1493 return;
1494 }
1495
Erich Keanec480f302018-07-12 21:09:05 +00001496 if (Attrs.empty() || Attrs.begin()->isInvalid()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001497 SkipToEnd();
1498 return;
1499 }
1500
1501 // Ensure that we don't have more than one attribute.
Erich Keanec480f302018-07-12 21:09:05 +00001502 if (Attrs.size() > 1) {
1503 SourceLocation Loc = Attrs[1].getLoc();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001504 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1505 SkipToEnd();
1506 return;
1507 }
1508
Erich Keanee891aa92018-07-13 15:07:47 +00001509 ParsedAttr &Attribute = *Attrs.begin();
Erich Keanec480f302018-07-12 21:09:05 +00001510 if (!Attribute.isSupportedByPragmaAttribute()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001511 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
Erich Keanec480f302018-07-12 21:09:05 +00001512 << Attribute.getName();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001513 SkipToEnd();
1514 return;
1515 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001516
1517 // Parse the subject-list.
1518 if (!TryConsumeToken(tok::comma)) {
1519 createExpectedAttributeSubjectRulesTokenDiagnostic(
1520 diag::err_expected, Attribute,
1521 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1522 << tok::comma;
1523 SkipToEnd();
1524 return;
1525 }
1526
1527 if (Tok.isNot(tok::identifier)) {
1528 createExpectedAttributeSubjectRulesTokenDiagnostic(
1529 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1530 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1531 SkipToEnd();
1532 return;
1533 }
1534 const IdentifierInfo *II = Tok.getIdentifierInfo();
1535 if (!II->isStr("apply_to")) {
1536 createExpectedAttributeSubjectRulesTokenDiagnostic(
1537 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1538 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1539 SkipToEnd();
1540 return;
1541 }
1542 ConsumeToken();
1543
1544 if (!TryConsumeToken(tok::equal)) {
1545 createExpectedAttributeSubjectRulesTokenDiagnostic(
1546 diag::err_expected, Attribute,
1547 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1548 << tok::equal;
1549 SkipToEnd();
1550 return;
1551 }
1552
1553 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1554 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1555 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1556 LastMatchRuleEndLoc)) {
1557 SkipToEnd();
1558 return;
1559 }
1560
1561 // Tokens following an ill-formed attribute will remain in the token stream
1562 // and must be removed.
1563 if (Tok.isNot(tok::eof)) {
1564 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1565 SkipToEnd();
1566 return;
1567 }
1568
1569 // Consume the eof terminator token.
1570 ConsumeToken();
1571
Erik Pilkington7d180942018-10-29 17:38:42 +00001572 // Handle a mixed push/attribute by desurging to a push, then an attribute.
1573 if (Info->Action == PragmaAttributeInfo::Push)
Erik Pilkington0876cae2018-12-20 22:32:04 +00001574 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace);
Erik Pilkington7d180942018-10-29 17:38:42 +00001575
1576 Actions.ActOnPragmaAttributeAttribute(Attribute, PragmaLoc,
1577 std::move(SubjectMatchRules));
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001578}
1579
Eli Bendersky06a40422014-06-06 20:31:48 +00001580// #pragma GCC visibility comes in two variants:
1581// 'push' '(' [visibility] ')'
1582// 'pop'
Fangrui Song6907ce22018-07-30 19:24:48 +00001583void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001584 PragmaIntroducer Introducer,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001585 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +00001586 SourceLocation VisLoc = VisTok.getLocation();
1587
1588 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001589 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001590
1591 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1592
Eli Friedman570024a2010-08-05 06:57:20 +00001593 const IdentifierInfo *VisType;
1594 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +00001595 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +00001596 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001597 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001598 if (Tok.isNot(tok::l_paren)) {
1599 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1600 << "visibility";
1601 return;
1602 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001603 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001604 VisType = Tok.getIdentifierInfo();
1605 if (!VisType) {
1606 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1607 << "visibility";
1608 return;
1609 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001610 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001611 if (Tok.isNot(tok::r_paren)) {
1612 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1613 << "visibility";
1614 return;
1615 }
1616 } else {
1617 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1618 << "visibility";
1619 return;
1620 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001621 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001622 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001623 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +00001624 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1625 << "visibility";
1626 return;
1627 }
1628
David Blaikie2eabcc92016-02-09 18:52:09 +00001629 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +00001630 Toks[0].startToken();
1631 Toks[0].setKind(tok::annot_pragma_vis);
1632 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001633 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +00001634 Toks[0].setAnnotationValue(
Ilya Biryukov929af672019-05-17 09:32:05 +00001635 const_cast<void *>(static_cast<const void *>(VisType)));
1636 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true,
1637 /*IsReinject=*/false);
Eli Friedman570024a2010-08-05 06:57:20 +00001638}
1639
Daniel Dunbar921b9682008-10-04 19:21:03 +00001640// #pragma pack(...) comes in the following delicious flavors:
1641// pack '(' [integer] ')'
1642// pack '(' 'show' ')'
1643// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Fangrui Song6907ce22018-07-30 19:24:48 +00001644void PragmaPackHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001645 PragmaIntroducer Introducer,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001646 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +00001647 SourceLocation PackLoc = PackTok.getLocation();
1648
1649 Token Tok;
1650 PP.Lex(Tok);
1651 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001652 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001653 return;
1654 }
1655
Denis Zobnin10c4f452016-04-29 18:17:40 +00001656 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1657 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001658 Token Alignment;
1659 Alignment.startToken();
Mike Stump11289f42009-09-09 15:08:12 +00001660 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001661 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001662 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001663
1664 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +00001665
1666 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1667 // the push/pop stack.
1668 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
Denis Zobnin10c4f452016-04-29 18:17:40 +00001669 Action =
1670 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001671 } else if (Tok.is(tok::identifier)) {
1672 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +00001673 if (II->isStr("show")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001674 Action = Sema::PSK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001675 PP.Lex(Tok);
1676 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001677 if (II->isStr("push")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001678 Action = Sema::PSK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +00001679 } else if (II->isStr("pop")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001680 Action = Sema::PSK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001681 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001682 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001683 return;
Mike Stump11289f42009-09-09 15:08:12 +00001684 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001685 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001686
Daniel Dunbar921b9682008-10-04 19:21:03 +00001687 if (Tok.is(tok::comma)) {
1688 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001689
Daniel Dunbar921b9682008-10-04 19:21:03 +00001690 if (Tok.is(tok::numeric_constant)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001691 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001692 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001693
1694 PP.Lex(Tok);
1695 } else if (Tok.is(tok::identifier)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001696 SlotLabel = Tok.getIdentifierInfo()->getName();
Daniel Dunbar921b9682008-10-04 19:21:03 +00001697 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001698
Daniel Dunbar921b9682008-10-04 19:21:03 +00001699 if (Tok.is(tok::comma)) {
1700 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001701
Daniel Dunbar921b9682008-10-04 19:21:03 +00001702 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001703 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001704 return;
1705 }
Mike Stump11289f42009-09-09 15:08:12 +00001706
Denis Zobnin10c4f452016-04-29 18:17:40 +00001707 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001708 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001709
1710 PP.Lex(Tok);
1711 }
1712 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001713 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001714 return;
1715 }
1716 }
1717 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001718 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001719 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1720 // the push/pop stack.
1721 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
Denis Zobnin10c4f452016-04-29 18:17:40 +00001722 Action = Sema::PSK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001723 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001724
1725 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001726 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001727 return;
1728 }
1729
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001730 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001731 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001732 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001733 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1734 return;
1735 }
1736
David Blaikie2eabcc92016-02-09 18:52:09 +00001737 PragmaPackInfo *Info =
1738 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Denis Zobnin10c4f452016-04-29 18:17:40 +00001739 Info->Action = Action;
1740 Info->SlotLabel = SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001741 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001742
David Blaikie2eabcc92016-02-09 18:52:09 +00001743 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1744 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001745 Toks[0].startToken();
1746 Toks[0].setKind(tok::annot_pragma_pack);
1747 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001748 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001749 Toks[0].setAnnotationValue(static_cast<void*>(Info));
Ilya Biryukov929af672019-05-17 09:32:05 +00001750 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1751 /*IsReinject=*/false);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001752}
1753
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001754// #pragma ms_struct on
1755// #pragma ms_struct off
Fangrui Song6907ce22018-07-30 19:24:48 +00001756void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001757 PragmaIntroducer Introducer,
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001758 Token &MSStructTok) {
Nico Weber779355f2016-03-02 23:22:00 +00001759 PragmaMSStructKind Kind = PMSST_OFF;
1760
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001761 Token Tok;
1762 PP.Lex(Tok);
1763 if (Tok.isNot(tok::identifier)) {
1764 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1765 return;
1766 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001767 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001768 const IdentifierInfo *II = Tok.getIdentifierInfo();
1769 if (II->isStr("on")) {
Nico Weber779355f2016-03-02 23:22:00 +00001770 Kind = PMSST_ON;
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001771 PP.Lex(Tok);
1772 }
1773 else if (II->isStr("off") || II->isStr("reset"))
1774 PP.Lex(Tok);
1775 else {
1776 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1777 return;
1778 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001779
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001780 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001781 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1782 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001783 return;
1784 }
Eli Friedman68be1642012-10-04 02:36:51 +00001785
David Blaikie2eabcc92016-02-09 18:52:09 +00001786 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1787 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001788 Toks[0].startToken();
1789 Toks[0].setKind(tok::annot_pragma_msstruct);
1790 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001791 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001792 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1793 static_cast<uintptr_t>(Kind)));
Ilya Biryukov929af672019-05-17 09:32:05 +00001794 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1795 /*IsReinject=*/false);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001796}
1797
Javed Absar2a67c9e2017-06-05 10:11:57 +00001798// #pragma clang section bss="abc" data="" rodata="def" text=""
1799void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001800 PragmaIntroducer Introducer,
1801 Token &FirstToken) {
Javed Absar2a67c9e2017-06-05 10:11:57 +00001802
1803 Token Tok;
1804 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1805
1806 PP.Lex(Tok); // eat 'section'
1807 while (Tok.isNot(tok::eod)) {
1808 if (Tok.isNot(tok::identifier)) {
1809 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1810 return;
1811 }
1812
1813 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1814 if (SecType->isStr("bss"))
1815 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1816 else if (SecType->isStr("data"))
1817 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1818 else if (SecType->isStr("rodata"))
1819 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1820 else if (SecType->isStr("text"))
1821 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1822 else {
1823 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1824 return;
1825 }
1826
1827 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1828 if (Tok.isNot(tok::equal)) {
1829 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1830 return;
1831 }
1832
1833 std::string SecName;
1834 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1835 return;
1836
1837 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1838 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1839 Sema::PragmaClangSectionAction::PCSA_Clear),
1840 SecKind, SecName);
1841 }
1842}
1843
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001844// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1845// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001846static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001847 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001848 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001849
1850 if (IsOptions) {
1851 PP.Lex(Tok);
1852 if (Tok.isNot(tok::identifier) ||
1853 !Tok.getIdentifierInfo()->isStr("align")) {
1854 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1855 return;
1856 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001857 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001858
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001859 PP.Lex(Tok);
1860 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001861 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1862 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001863 return;
1864 }
1865
1866 PP.Lex(Tok);
1867 if (Tok.isNot(tok::identifier)) {
1868 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001869 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001870 return;
1871 }
1872
John McCallfaf5fb42010-08-26 23:41:50 +00001873 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001874 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001875 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001876 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001877 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001878 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001879 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001880 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001881 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001882 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001883 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001884 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001885 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001886 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001887 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001888 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1889 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001890 return;
1891 }
1892
David Majnemera8f2f1d2015-03-19 00:10:23 +00001893 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001894 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001895 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001896 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001897 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001898 return;
1899 }
1900
David Blaikie2eabcc92016-02-09 18:52:09 +00001901 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1902 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001903 Toks[0].startToken();
1904 Toks[0].setKind(tok::annot_pragma_align);
1905 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001906 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001907 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1908 static_cast<uintptr_t>(Kind)));
Ilya Biryukov929af672019-05-17 09:32:05 +00001909 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
1910 /*IsReinject=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001911}
1912
Fangrui Song6907ce22018-07-30 19:24:48 +00001913void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001914 PragmaIntroducer Introducer,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001915 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001916 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001917}
1918
Fangrui Song6907ce22018-07-30 19:24:48 +00001919void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001920 PragmaIntroducer Introducer,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001921 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001922 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001923}
1924
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001925// #pragma unused(identifier)
Fangrui Song6907ce22018-07-30 19:24:48 +00001926void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00001927 PragmaIntroducer Introducer,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001928 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001929 // FIXME: Should we be expanding macros here? My guess is no.
1930 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001931
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001932 // Lex the left '('.
1933 Token Tok;
1934 PP.Lex(Tok);
1935 if (Tok.isNot(tok::l_paren)) {
1936 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1937 return;
1938 }
Mike Stump11289f42009-09-09 15:08:12 +00001939
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001940 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001941 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001942 SourceLocation RParenLoc;
1943 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001944
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001945 while (true) {
1946 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001947
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001948 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001949 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001950 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001951 LexID = false;
1952 continue;
1953 }
1954
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001955 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001956 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1957 return;
1958 }
Mike Stump11289f42009-09-09 15:08:12 +00001959
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001960 // We are execting a ')' or a ','.
1961 if (Tok.is(tok::comma)) {
1962 LexID = true;
1963 continue;
1964 }
Mike Stump11289f42009-09-09 15:08:12 +00001965
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001966 if (Tok.is(tok::r_paren)) {
1967 RParenLoc = Tok.getLocation();
1968 break;
1969 }
Mike Stump11289f42009-09-09 15:08:12 +00001970
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001971 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001972 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001973 return;
1974 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001975
1976 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001977 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001978 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1979 "unused";
1980 return;
1981 }
1982
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001983 // Verify that we have a location for the right parenthesis.
1984 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001985 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001986
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001987 // For each identifier token, insert into the token stream a
1988 // annot_pragma_unused token followed by the identifier token.
1989 // This allows us to cache a "#pragma unused" that occurs inside an inline
1990 // C++ member function.
1991
David Blaikie2eabcc92016-02-09 18:52:09 +00001992 MutableArrayRef<Token> Toks(
1993 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1994 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001995 for (unsigned i=0; i != Identifiers.size(); i++) {
1996 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1997 pragmaUnusedTok.startToken();
1998 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1999 pragmaUnusedTok.setLocation(UnusedLoc);
2000 idTok = Identifiers[i];
2001 }
Ilya Biryukov929af672019-05-17 09:32:05 +00002002 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
2003 /*IsReinject=*/false);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00002004}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002005
2006// #pragma weak identifier
2007// #pragma weak identifier '=' identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00002008void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002009 PragmaIntroducer Introducer,
Douglas Gregorc7d65762010-09-09 22:45:38 +00002010 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002011 SourceLocation WeakLoc = WeakTok.getLocation();
2012
2013 Token Tok;
2014 PP.Lex(Tok);
2015 if (Tok.isNot(tok::identifier)) {
2016 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
2017 return;
2018 }
2019
Eli Friedman68be1642012-10-04 02:36:51 +00002020 Token WeakName = Tok;
2021 bool HasAlias = false;
2022 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002023
2024 PP.Lex(Tok);
2025 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00002026 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002027 PP.Lex(Tok);
2028 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00002029 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002030 << "weak";
2031 return;
2032 }
Eli Friedman68be1642012-10-04 02:36:51 +00002033 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002034 PP.Lex(Tok);
2035 }
2036
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002037 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002038 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
2039 return;
2040 }
2041
Eli Friedman68be1642012-10-04 02:36:51 +00002042 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00002043 MutableArrayRef<Token> Toks(
2044 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002045 Token &pragmaUnusedTok = Toks[0];
2046 pragmaUnusedTok.startToken();
2047 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
2048 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002049 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002050 Toks[1] = WeakName;
2051 Toks[2] = AliasName;
Ilya Biryukov929af672019-05-17 09:32:05 +00002052 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
2053 /*IsReinject=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002054 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00002055 MutableArrayRef<Token> Toks(
2056 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00002057 Token &pragmaUnusedTok = Toks[0];
2058 pragmaUnusedTok.startToken();
2059 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
2060 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002061 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00002062 Toks[1] = WeakName;
Ilya Biryukov929af672019-05-17 09:32:05 +00002063 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
2064 /*IsReinject=*/false);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002065 }
2066}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002067
David Chisnall0867d9c2012-02-18 16:12:34 +00002068// #pragma redefine_extname identifier identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00002069void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002070 PragmaIntroducer Introducer,
David Chisnall0867d9c2012-02-18 16:12:34 +00002071 Token &RedefToken) {
2072 SourceLocation RedefLoc = RedefToken.getLocation();
2073
2074 Token Tok;
2075 PP.Lex(Tok);
2076 if (Tok.isNot(tok::identifier)) {
2077 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2078 "redefine_extname";
2079 return;
2080 }
2081
Eli Friedman68be1642012-10-04 02:36:51 +00002082 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002083 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00002084
David Chisnall0867d9c2012-02-18 16:12:34 +00002085 if (Tok.isNot(tok::identifier)) {
2086 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2087 << "redefine_extname";
2088 return;
2089 }
Eli Friedman68be1642012-10-04 02:36:51 +00002090
2091 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002092 PP.Lex(Tok);
2093
2094 if (Tok.isNot(tok::eod)) {
2095 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2096 "redefine_extname";
2097 return;
2098 }
2099
David Blaikie2eabcc92016-02-09 18:52:09 +00002100 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
2101 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002102 Token &pragmaRedefTok = Toks[0];
2103 pragmaRedefTok.startToken();
2104 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
2105 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002106 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002107 Toks[1] = RedefName;
2108 Toks[2] = AliasName;
Ilya Biryukov929af672019-05-17 09:32:05 +00002109 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
2110 /*IsReinject=*/false);
David Chisnall0867d9c2012-02-18 16:12:34 +00002111}
2112
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002113void PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
2114 PragmaIntroducer Introducer,
2115 Token &Tok) {
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002116 tok::OnOffSwitch OOS;
2117 if (PP.LexOnOffSwitch(OOS))
2118 return;
2119
David Blaikie2eabcc92016-02-09 18:52:09 +00002120 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2121 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002122 Toks[0].startToken();
2123 Toks[0].setKind(tok::annot_pragma_fp_contract);
2124 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002125 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002126 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
2127 static_cast<uintptr_t>(OOS)));
Ilya Biryukov929af672019-05-17 09:32:05 +00002128 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
2129 /*IsReinject=*/false);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002130}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002131
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002132void PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
2133 PragmaIntroducer Introducer,
2134 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00002135 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002136 if (Tok.isNot(tok::identifier)) {
2137 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2138 "OPENCL";
2139 return;
2140 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002141 IdentifierInfo *Ext = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002142 SourceLocation NameLoc = Tok.getLocation();
2143
2144 PP.Lex(Tok);
2145 if (Tok.isNot(tok::colon)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002146 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002147 return;
2148 }
2149
2150 PP.Lex(Tok);
2151 if (Tok.isNot(tok::identifier)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002152 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002153 return;
2154 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002155 IdentifierInfo *Pred = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002156
Yaxun Liu5b746652016-12-18 05:18:55 +00002157 OpenCLExtState State;
2158 if (Pred->isStr("enable")) {
2159 State = Enable;
2160 } else if (Pred->isStr("disable")) {
2161 State = Disable;
2162 } else if (Pred->isStr("begin"))
2163 State = Begin;
2164 else if (Pred->isStr("end"))
2165 State = End;
2166 else {
2167 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
2168 << Ext->isStr("all");
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002169 return;
2170 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002171 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002172
Eli Friedman68be1642012-10-04 02:36:51 +00002173 PP.Lex(Tok);
2174 if (Tok.isNot(tok::eod)) {
2175 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2176 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002177 return;
2178 }
Eli Friedman68be1642012-10-04 02:36:51 +00002179
Yaxun Liu5b746652016-12-18 05:18:55 +00002180 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2181 Info->first = Ext;
2182 Info->second = State;
David Blaikie2eabcc92016-02-09 18:52:09 +00002183 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2184 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002185 Toks[0].startToken();
2186 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2187 Toks[0].setLocation(NameLoc);
Yaxun Liu5b746652016-12-18 05:18:55 +00002188 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Majnemera8f2f1d2015-03-19 00:10:23 +00002189 Toks[0].setAnnotationEndLoc(StateLoc);
Ilya Biryukov929af672019-05-17 09:32:05 +00002190 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true,
2191 /*IsReinject=*/false);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002192
2193 if (PP.getPPCallbacks())
Fangrui Song6907ce22018-07-30 19:24:48 +00002194 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
Yaxun Liu5b746652016-12-18 05:18:55 +00002195 StateLoc, State);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002196}
2197
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002198/// Handle '#pragma omp ...' when OpenMP is disabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002199///
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002200void PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2201 PragmaIntroducer Introducer,
2202 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00002203 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2204 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002205 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00002206 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2207 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00002208 }
2209 PP.DiscardUntilEndOfDirective();
2210}
2211
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002212/// Handle '#pragma omp ...' when OpenMP is enabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002213///
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002214void PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2215 PragmaIntroducer Introducer,
2216 Token &FirstTok) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002217 SmallVector<Token, 16> Pragma;
2218 Token Tok;
2219 Tok.startToken();
2220 Tok.setKind(tok::annot_pragma_openmp);
Joel E. Denny91f80662019-05-28 19:27:19 +00002221 Tok.setLocation(Introducer.Loc);
Alexey Bataeva769e072013-03-22 06:34:35 +00002222
Alexey Bataev96dae812018-02-16 18:36:44 +00002223 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002224 Pragma.push_back(Tok);
2225 PP.Lex(Tok);
Alexey Bataev96dae812018-02-16 18:36:44 +00002226 if (Tok.is(tok::annot_pragma_openmp)) {
2227 PP.Diag(Tok, diag::err_omp_unexpected_directive) << 0;
2228 unsigned InnerPragmaCnt = 1;
2229 while (InnerPragmaCnt != 0) {
2230 PP.Lex(Tok);
2231 if (Tok.is(tok::annot_pragma_openmp))
2232 ++InnerPragmaCnt;
2233 else if (Tok.is(tok::annot_pragma_openmp_end))
2234 --InnerPragmaCnt;
2235 }
2236 PP.Lex(Tok);
2237 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002238 }
2239 SourceLocation EodLoc = Tok.getLocation();
2240 Tok.startToken();
2241 Tok.setKind(tok::annot_pragma_openmp_end);
2242 Tok.setLocation(EodLoc);
2243 Pragma.push_back(Tok);
2244
David Blaikie2eabcc92016-02-09 18:52:09 +00002245 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2246 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2247 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
Ilya Biryukov929af672019-05-17 09:32:05 +00002248 /*DisableMacroExpansion=*/false, /*IsReinject=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00002249}
Reid Kleckner002562a2013-05-06 21:02:12 +00002250
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002251/// Handle '#pragma pointers_to_members'
David Majnemer4bb09802014-02-10 19:50:15 +00002252// The grammar for this pragma is as follows:
2253//
2254// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2255//
2256// #pragma pointers_to_members '(' 'best_case' ')'
2257// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2258// #pragma pointers_to_members '(' inheritance-model ')'
2259void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002260 PragmaIntroducer Introducer,
David Majnemer4bb09802014-02-10 19:50:15 +00002261 Token &Tok) {
2262 SourceLocation PointersToMembersLoc = Tok.getLocation();
2263 PP.Lex(Tok);
2264 if (Tok.isNot(tok::l_paren)) {
2265 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2266 << "pointers_to_members";
2267 return;
2268 }
2269 PP.Lex(Tok);
2270 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2271 if (!Arg) {
2272 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2273 << "pointers_to_members";
2274 return;
2275 }
2276 PP.Lex(Tok);
2277
David Majnemer86c318f2014-02-11 21:05:00 +00002278 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00002279 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002280 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00002281 } else {
2282 if (Arg->isStr("full_generality")) {
2283 if (Tok.is(tok::comma)) {
2284 PP.Lex(Tok);
2285
2286 Arg = Tok.getIdentifierInfo();
2287 if (!Arg) {
2288 PP.Diag(Tok.getLocation(),
2289 diag::err_pragma_pointers_to_members_unknown_kind)
2290 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2291 return;
2292 }
2293 PP.Lex(Tok);
2294 } else if (Tok.is(tok::r_paren)) {
2295 // #pragma pointers_to_members(full_generality) implicitly specifies
2296 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00002297 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00002298 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002299 } else {
2300 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2301 << "full_generality";
2302 return;
2303 }
2304 }
2305
2306 if (Arg) {
2307 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002308 RepresentationMethod =
2309 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002310 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002311 RepresentationMethod =
2312 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002313 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002314 RepresentationMethod =
2315 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002316 } else {
2317 PP.Diag(Tok.getLocation(),
2318 diag::err_pragma_pointers_to_members_unknown_kind)
2319 << Arg << /*HasPointerDeclaration*/ 1;
2320 return;
2321 }
2322 }
2323 }
2324
2325 if (Tok.isNot(tok::r_paren)) {
2326 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2327 << (Arg ? Arg->getName() : "full_generality");
2328 return;
2329 }
2330
David Majnemera8f2f1d2015-03-19 00:10:23 +00002331 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00002332 PP.Lex(Tok);
2333 if (Tok.isNot(tok::eod)) {
2334 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2335 << "pointers_to_members";
2336 return;
2337 }
2338
2339 Token AnnotTok;
2340 AnnotTok.startToken();
2341 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2342 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002343 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00002344 AnnotTok.setAnnotationValue(
2345 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
Ilya Biryukov929af672019-05-17 09:32:05 +00002346 PP.EnterToken(AnnotTok, /*IsReinject=*/true);
David Majnemer4bb09802014-02-10 19:50:15 +00002347}
2348
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002349/// Handle '#pragma vtordisp'
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002350// The grammar for this pragma is as follows:
2351//
2352// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2353//
2354// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2355// #pragma vtordisp '(' 'pop' ')'
2356// #pragma vtordisp '(' ')'
2357void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002358 PragmaIntroducer Introducer, Token &Tok) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002359 SourceLocation VtorDispLoc = Tok.getLocation();
2360 PP.Lex(Tok);
2361 if (Tok.isNot(tok::l_paren)) {
2362 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2363 return;
2364 }
2365 PP.Lex(Tok);
2366
Denis Zobnin2290dac2016-04-29 11:27:00 +00002367 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002368 const IdentifierInfo *II = Tok.getIdentifierInfo();
2369 if (II) {
2370 if (II->isStr("push")) {
2371 // #pragma vtordisp(push, mode)
2372 PP.Lex(Tok);
2373 if (Tok.isNot(tok::comma)) {
2374 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2375 return;
2376 }
2377 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002378 Action = Sema::PSK_Push_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002379 // not push, could be on/off
2380 } else if (II->isStr("pop")) {
2381 // #pragma vtordisp(pop)
2382 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002383 Action = Sema::PSK_Pop;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002384 }
2385 // not push or pop, could be on/off
2386 } else {
2387 if (Tok.is(tok::r_paren)) {
2388 // #pragma vtordisp()
Denis Zobnin2290dac2016-04-29 11:27:00 +00002389 Action = Sema::PSK_Reset;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002390 }
2391 }
2392
2393
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002394 uint64_t Value = 0;
Denis Zobnin2290dac2016-04-29 11:27:00 +00002395 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002396 const IdentifierInfo *II = Tok.getIdentifierInfo();
2397 if (II && II->isStr("off")) {
2398 PP.Lex(Tok);
2399 Value = 0;
2400 } else if (II && II->isStr("on")) {
2401 PP.Lex(Tok);
2402 Value = 1;
2403 } else if (Tok.is(tok::numeric_constant) &&
2404 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2405 if (Value > 2) {
2406 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2407 << 0 << 2 << "vtordisp";
2408 return;
2409 }
2410 } else {
2411 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2412 << "vtordisp";
2413 return;
2414 }
2415 }
2416
2417 // Finish the pragma: ')' $
2418 if (Tok.isNot(tok::r_paren)) {
2419 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2420 return;
2421 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00002422 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002423 PP.Lex(Tok);
2424 if (Tok.isNot(tok::eod)) {
2425 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2426 << "vtordisp";
2427 return;
2428 }
2429
2430 // Enter the annotation.
2431 Token AnnotTok;
2432 AnnotTok.startToken();
2433 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2434 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002435 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002436 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
Denis Zobnin2290dac2016-04-29 11:27:00 +00002437 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
Ilya Biryukov929af672019-05-17 09:32:05 +00002438 PP.EnterToken(AnnotTok, /*IsReinject=*/false);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002439}
2440
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002441/// Handle all MS pragmas. Simply forwards the tokens after inserting
Warren Huntc3b18962014-04-08 22:30:47 +00002442/// an annotation token.
2443void PragmaMSPragma::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002444 PragmaIntroducer Introducer, Token &Tok) {
Warren Huntc3b18962014-04-08 22:30:47 +00002445 Token EoF, AnnotTok;
2446 EoF.startToken();
2447 EoF.setKind(tok::eof);
2448 AnnotTok.startToken();
2449 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2450 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002451 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00002452 SmallVector<Token, 8> TokenVector;
2453 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00002454 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00002455 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002456 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2457 }
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002458 // Add a sentinel EoF token to the end of the list.
Warren Huntc3b18962014-04-08 22:30:47 +00002459 TokenVector.push_back(EoF);
2460 // We must allocate this array with new because EnterTokenStream is going to
2461 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00002462 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2463 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00002464 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00002465 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2466 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00002467 AnnotTok.setAnnotationValue(Value);
Ilya Biryukov929af672019-05-17 09:32:05 +00002468 PP.EnterToken(AnnotTok, /*IsReinject*/ false);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00002469}
2470
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002471/// Handle the Microsoft \#pragma detect_mismatch extension.
Aaron Ballman5d041be2013-06-04 02:07:14 +00002472///
2473/// The syntax is:
2474/// \code
2475/// #pragma detect_mismatch("name", "value")
2476/// \endcode
2477/// Where 'name' and 'value' are quoted strings. The values are embedded in
2478/// the object file and passed along to the linker. If the linker detects a
2479/// mismatch in the object file's values for the given name, a LNK2038 error
2480/// is emitted. See MSDN for more details.
2481void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002482 PragmaIntroducer Introducer,
Aaron Ballman5d041be2013-06-04 02:07:14 +00002483 Token &Tok) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002484 SourceLocation DetectMismatchLoc = Tok.getLocation();
Aaron Ballman5d041be2013-06-04 02:07:14 +00002485 PP.Lex(Tok);
2486 if (Tok.isNot(tok::l_paren)) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002487 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002488 return;
2489 }
2490
2491 // Read the name to embed, which must be a string literal.
2492 std::string NameString;
2493 if (!PP.LexStringLiteral(Tok, NameString,
2494 "pragma detect_mismatch",
Rui Ueyama49a3ad22019-07-16 04:46:31 +00002495 /*AllowMacroExpansion=*/true))
Aaron Ballman5d041be2013-06-04 02:07:14 +00002496 return;
2497
2498 // Read the comma followed by a second string literal.
2499 std::string ValueString;
2500 if (Tok.isNot(tok::comma)) {
2501 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2502 return;
2503 }
2504
2505 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
Rui Ueyama49a3ad22019-07-16 04:46:31 +00002506 /*AllowMacroExpansion=*/true))
Aaron Ballman5d041be2013-06-04 02:07:14 +00002507 return;
2508
2509 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002510 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002511 return;
2512 }
2513 PP.Lex(Tok); // Eat the r_paren.
2514
2515 if (Tok.isNot(tok::eod)) {
2516 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2517 return;
2518 }
2519
Reid Kleckner71966c92014-02-20 23:37:45 +00002520 // If the pragma is lexically sound, notify any interested PPCallbacks.
2521 if (PP.getPPCallbacks())
Nico Webercbbaeb12016-03-02 19:28:54 +00002522 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
Reid Kleckner71966c92014-02-20 23:37:45 +00002523 ValueString);
2524
Nico Webercbbaeb12016-03-02 19:28:54 +00002525 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
Aaron Ballman5d041be2013-06-04 02:07:14 +00002526}
2527
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002528/// Handle the microsoft \#pragma comment extension.
Reid Kleckner002562a2013-05-06 21:02:12 +00002529///
2530/// The syntax is:
2531/// \code
2532/// #pragma comment(linker, "foo")
2533/// \endcode
2534/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2535/// "foo" is a string, which is fully macro expanded, and permits string
2536/// concatenation, embedded escape characters etc. See MSDN for more details.
2537void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002538 PragmaIntroducer Introducer,
Reid Kleckner002562a2013-05-06 21:02:12 +00002539 Token &Tok) {
2540 SourceLocation CommentLoc = Tok.getLocation();
2541 PP.Lex(Tok);
2542 if (Tok.isNot(tok::l_paren)) {
2543 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2544 return;
2545 }
2546
2547 // Read the identifier.
2548 PP.Lex(Tok);
2549 if (Tok.isNot(tok::identifier)) {
2550 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2551 return;
2552 }
2553
2554 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002555 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00002556 PragmaMSCommentKind Kind =
2557 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2558 .Case("linker", PCK_Linker)
2559 .Case("lib", PCK_Lib)
2560 .Case("compiler", PCK_Compiler)
2561 .Case("exestr", PCK_ExeStr)
2562 .Case("user", PCK_User)
2563 .Default(PCK_Unknown);
2564 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00002565 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2566 return;
2567 }
2568
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +00002569 if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
2570 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2571 << II->getName();
2572 return;
2573 }
2574
Yunzhong Gao99efc032015-03-23 20:41:42 +00002575 // On PS4, issue a warning about any pragma comments other than
2576 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00002577 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00002578 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2579 << II->getName();
2580 return;
2581 }
2582
Reid Kleckner002562a2013-05-06 21:02:12 +00002583 // Read the optional string if present.
2584 PP.Lex(Tok);
2585 std::string ArgumentString;
2586 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2587 "pragma comment",
Rui Ueyama49a3ad22019-07-16 04:46:31 +00002588 /*AllowMacroExpansion=*/true))
Reid Kleckner002562a2013-05-06 21:02:12 +00002589 return;
2590
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002591 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00002592 // FIXME: If the kind is "compiler" warn if the string is present (it is
2593 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002594 // The MSDN docs say that "lib" and "linker" require a string and have a short
2595 // whitelist of linker options they support, but in practice MSVC doesn't
2596 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00002597
2598 if (Tok.isNot(tok::r_paren)) {
2599 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2600 return;
2601 }
2602 PP.Lex(Tok); // eat the r_paren.
2603
2604 if (Tok.isNot(tok::eod)) {
2605 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2606 return;
2607 }
2608
Reid Kleckner71966c92014-02-20 23:37:45 +00002609 // If the pragma is lexically sound, notify any interested PPCallbacks.
2610 if (PP.getPPCallbacks())
2611 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2612
Nico Weber66220292016-03-02 17:28:48 +00002613 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00002614}
Dario Domizioli13a0a382014-05-23 12:13:25 +00002615
2616// #pragma clang optimize off
2617// #pragma clang optimize on
Fangrui Song6907ce22018-07-30 19:24:48 +00002618void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002619 PragmaIntroducer Introducer,
2620 Token &FirstToken) {
Dario Domizioli13a0a382014-05-23 12:13:25 +00002621 Token Tok;
2622 PP.Lex(Tok);
2623 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002624 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00002625 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00002626 return;
2627 }
2628 if (Tok.isNot(tok::identifier)) {
2629 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2630 << PP.getSpelling(Tok);
2631 return;
2632 }
2633 const IdentifierInfo *II = Tok.getIdentifierInfo();
2634 // The only accepted values are 'on' or 'off'.
2635 bool IsOn = false;
2636 if (II->isStr("on")) {
2637 IsOn = true;
2638 } else if (!II->isStr("off")) {
2639 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2640 << PP.getSpelling(Tok);
2641 return;
2642 }
2643 PP.Lex(Tok);
Fangrui Song6907ce22018-07-30 19:24:48 +00002644
Dario Domizioli13a0a382014-05-23 12:13:25 +00002645 if (Tok.isNot(tok::eod)) {
2646 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2647 << PP.getSpelling(Tok);
2648 return;
2649 }
Eli Bendersky06a40422014-06-06 20:31:48 +00002650
2651 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2652}
2653
Adam Nemet60d32642017-04-04 21:18:36 +00002654namespace {
2655/// Used as the annotation value for tok::annot_pragma_fp.
2656struct TokFPAnnotValue {
2657 enum FlagKinds { Contract };
2658 enum FlagValues { On, Off, Fast };
2659
2660 FlagKinds FlagKind;
2661 FlagValues FlagValue;
2662};
2663} // end anonymous namespace
2664
2665void PragmaFPHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002666 PragmaIntroducer Introducer, Token &Tok) {
Adam Nemet60d32642017-04-04 21:18:36 +00002667 // fp
2668 Token PragmaName = Tok;
2669 SmallVector<Token, 1> TokenList;
2670
2671 PP.Lex(Tok);
2672 if (Tok.isNot(tok::identifier)) {
2673 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2674 << /*MissingOption=*/true << "";
2675 return;
2676 }
2677
2678 while (Tok.is(tok::identifier)) {
2679 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2680
2681 auto FlagKind =
2682 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2683 OptionInfo->getName())
2684 .Case("contract", TokFPAnnotValue::Contract)
2685 .Default(None);
2686 if (!FlagKind) {
2687 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2688 << /*MissingOption=*/false << OptionInfo;
2689 return;
2690 }
2691 PP.Lex(Tok);
2692
2693 // Read '('
2694 if (Tok.isNot(tok::l_paren)) {
2695 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2696 return;
2697 }
2698 PP.Lex(Tok);
2699
2700 if (Tok.isNot(tok::identifier)) {
2701 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2702 << PP.getSpelling(Tok) << OptionInfo->getName();
2703 return;
2704 }
2705 const IdentifierInfo *II = Tok.getIdentifierInfo();
2706
2707 auto FlagValue =
2708 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2709 II->getName())
2710 .Case("on", TokFPAnnotValue::On)
2711 .Case("off", TokFPAnnotValue::Off)
2712 .Case("fast", TokFPAnnotValue::Fast)
2713 .Default(llvm::None);
2714
2715 if (!FlagValue) {
2716 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2717 << PP.getSpelling(Tok) << OptionInfo->getName();
2718 return;
2719 }
2720 PP.Lex(Tok);
2721
2722 // Read ')'
2723 if (Tok.isNot(tok::r_paren)) {
2724 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2725 return;
2726 }
2727 PP.Lex(Tok);
2728
2729 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2730 TokFPAnnotValue{*FlagKind, *FlagValue};
2731 // Generate the loop hint token.
2732 Token FPTok;
2733 FPTok.startToken();
2734 FPTok.setKind(tok::annot_pragma_fp);
2735 FPTok.setLocation(PragmaName.getLocation());
2736 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2737 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2738 TokenList.push_back(FPTok);
2739 }
2740
2741 if (Tok.isNot(tok::eod)) {
2742 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2743 << "clang fp";
2744 return;
2745 }
2746
2747 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2748 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2749
2750 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
Ilya Biryukov929af672019-05-17 09:32:05 +00002751 /*DisableMacroExpansion=*/false, /*IsReinject=*/false);
Adam Nemet60d32642017-04-04 21:18:36 +00002752}
2753
2754void Parser::HandlePragmaFP() {
2755 assert(Tok.is(tok::annot_pragma_fp));
2756 auto *AnnotValue =
2757 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2758
2759 LangOptions::FPContractModeKind FPC;
2760 switch (AnnotValue->FlagValue) {
2761 case TokFPAnnotValue::On:
2762 FPC = LangOptions::FPC_On;
2763 break;
2764 case TokFPAnnotValue::Fast:
2765 FPC = LangOptions::FPC_Fast;
2766 break;
2767 case TokFPAnnotValue::Off:
2768 FPC = LangOptions::FPC_Off;
2769 break;
2770 }
2771
2772 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +00002773 ConsumeAnnotationToken();
Adam Nemet60d32642017-04-04 21:18:36 +00002774}
2775
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002776/// Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002777static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2778 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002779 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002780 SmallVector<Token, 1> ValueList;
2781 int OpenParens = ValueInParens ? 1 : 0;
2782 // Read constant expression.
2783 while (Tok.isNot(tok::eod)) {
2784 if (Tok.is(tok::l_paren))
2785 OpenParens++;
2786 else if (Tok.is(tok::r_paren)) {
2787 OpenParens--;
2788 if (OpenParens == 0 && ValueInParens)
2789 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002790 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002791
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002792 ValueList.push_back(Tok);
2793 PP.Lex(Tok);
2794 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002795
2796 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002797 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002798 if (Tok.isNot(tok::r_paren)) {
2799 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2800 return true;
2801 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002802 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002803 }
2804
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002805 Token EOFTok;
2806 EOFTok.startToken();
2807 EOFTok.setKind(tok::eof);
2808 EOFTok.setLocation(Tok.getLocation());
2809 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2810
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00002811 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002812
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002813 Info.PragmaName = PragmaName;
2814 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002815 return false;
2816}
2817
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002818/// Handle the \#pragma clang loop directive.
Eli Bendersky06a40422014-06-06 20:31:48 +00002819/// #pragma clang 'loop' loop-hints
2820///
2821/// loop-hints:
2822/// loop-hint loop-hints[opt]
2823///
2824/// loop-hint:
2825/// 'vectorize' '(' loop-hint-keyword ')'
2826/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00002827/// 'unroll' '(' unroll-hint-keyword ')'
Sjoerd Meijera48f58c2019-07-25 07:33:13 +00002828/// 'vectorize_predicate' '(' loop-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002829/// 'vectorize_width' '(' loop-hint-value ')'
2830/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00002831/// 'unroll_count' '(' loop-hint-value ')'
Aaron Ballman9bdf5152019-01-04 17:20:00 +00002832/// 'pipeline' '(' disable ')'
2833/// 'pipeline_initiation_interval' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002834///
2835/// loop-hint-keyword:
2836/// 'enable'
2837/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00002838/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00002839///
Mark Heffernan450c2382014-07-23 17:31:31 +00002840/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00002841/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00002842/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00002843/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00002844///
Eli Bendersky06a40422014-06-06 20:31:48 +00002845/// loop-hint-value:
2846/// constant-expression
2847///
2848/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2849/// try vectorizing the instructions of the loop it precedes. Specifying
2850/// interleave(enable) or interleave_count(_value_) instructs llvm to try
2851/// interleaving multiple iterations of the loop it precedes. The width of the
2852/// vector instructions is specified by vectorize_width() and the number of
2853/// interleaved loop iterations is specified by interleave_count(). Specifying a
2854/// value of 1 effectively disables vectorization/interleaving, even if it is
2855/// possible and profitable, and 0 is invalid. The loop vectorizer currently
2856/// only works on inner loops.
2857///
Eli Bendersky86483b32014-06-11 17:56:26 +00002858/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00002859/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2860/// completely if the trip count is known at compile time and unroll partially
2861/// if the trip count is not known. Specifying unroll(full) is similar to
2862/// unroll(enable) but will unroll the loop only if the trip count is known at
2863/// compile time. Specifying unroll(disable) disables unrolling for the
2864/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2865/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00002866void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002867 PragmaIntroducer Introducer,
Eli Bendersky06a40422014-06-06 20:31:48 +00002868 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002869 // Incoming token is "loop" from "#pragma clang loop".
2870 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00002871 SmallVector<Token, 1> TokenList;
2872
2873 // Lex the optimization option and verify it is an identifier.
2874 PP.Lex(Tok);
2875 if (Tok.isNot(tok::identifier)) {
2876 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2877 << /*MissingOption=*/true << "";
2878 return;
2879 }
2880
2881 while (Tok.is(tok::identifier)) {
2882 Token Option = Tok;
2883 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2884
Eli Bendersky86483b32014-06-11 17:56:26 +00002885 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002886 .Case("vectorize", true)
2887 .Case("interleave", true)
2888 .Case("unroll", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00002889 .Case("distribute", true)
Sjoerd Meijera48f58c2019-07-25 07:33:13 +00002890 .Case("vectorize_predicate", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002891 .Case("vectorize_width", true)
2892 .Case("interleave_count", true)
2893 .Case("unroll_count", true)
Aaron Ballman9bdf5152019-01-04 17:20:00 +00002894 .Case("pipeline", true)
2895 .Case("pipeline_initiation_interval", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002896 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002897 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002898 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2899 << /*MissingOption=*/false << OptionInfo;
2900 return;
2901 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002902 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002903
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002904 // Read '('
2905 if (Tok.isNot(tok::l_paren)) {
2906 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002907 return;
2908 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002909 PP.Lex(Tok);
2910
2911 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2912 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2913 *Info))
2914 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002915
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002916 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002917 Token LoopHintTok;
2918 LoopHintTok.startToken();
2919 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002920 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002921 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002922 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2923 TokenList.push_back(LoopHintTok);
2924 }
2925
2926 if (Tok.isNot(tok::eod)) {
2927 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2928 << "clang loop";
2929 return;
2930 }
2931
David Blaikie2eabcc92016-02-09 18:52:09 +00002932 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2933 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002934
David Blaikie2eabcc92016-02-09 18:52:09 +00002935 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
Ilya Biryukov929af672019-05-17 09:32:05 +00002936 /*DisableMacroExpansion=*/false, /*IsReinject=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002937}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002938
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002939/// Handle the loop unroll optimization pragmas.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002940/// #pragma unroll
2941/// #pragma unroll unroll-hint-value
2942/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002943/// #pragma nounroll
David Greenc8e39242018-08-01 14:36:12 +00002944/// #pragma unroll_and_jam
2945/// #pragma unroll_and_jam unroll-hint-value
2946/// #pragma unroll_and_jam '(' unroll-hint-value ')'
2947/// #pragma nounroll_and_jam
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002948///
2949/// unroll-hint-value:
2950/// constant-expression
2951///
Mark Heffernanc888e412014-07-24 18:09:38 +00002952/// Loop unrolling hints can be specified with '#pragma unroll' or
2953/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2954/// contained in parentheses. With no argument the directive instructs llvm to
2955/// try to unroll the loop completely. A positive integer argument can be
2956/// specified to indicate the number of times the loop should be unrolled. To
2957/// maximize compatibility with other compilers the unroll count argument can be
2958/// specified with or without parentheses. Specifying, '#pragma nounroll'
2959/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002960void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00002961 PragmaIntroducer Introducer,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002962 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002963 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2964 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002965 Token PragmaName = Tok;
2966 PP.Lex(Tok);
2967 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2968 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002969 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002970 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002971 Info->Option.startToken();
David Greenc8e39242018-08-01 14:36:12 +00002972 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll" ||
2973 PragmaName.getIdentifierInfo()->getName() == "nounroll_and_jam") {
Mark Heffernanc888e412014-07-24 18:09:38 +00002974 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
David Greenc8e39242018-08-01 14:36:12 +00002975 << PragmaName.getIdentifierInfo()->getName();
Mark Heffernanc888e412014-07-24 18:09:38 +00002976 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002977 } else {
2978 // Unroll pragma with an argument: "#pragma unroll N" or
2979 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002980 // Read '(' if it exists.
2981 bool ValueInParens = Tok.is(tok::l_paren);
2982 if (ValueInParens)
2983 PP.Lex(Tok);
2984
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002985 Token Option;
2986 Option.startToken();
2987 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002988 return;
2989
2990 // In CUDA, the argument to '#pragma unroll' should not be contained in
2991 // parentheses.
2992 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002993 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002994 diag::warn_pragma_unroll_cuda_value_in_parens);
2995
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002996 if (Tok.isNot(tok::eod)) {
2997 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2998 << "unroll";
2999 return;
3000 }
3001 }
3002
3003 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00003004 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00003005 TokenArray[0].startToken();
3006 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
3007 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00003008 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00003009 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00003010 PP.EnterTokenStream(std::move(TokenArray), 1,
Ilya Biryukov929af672019-05-17 09:32:05 +00003011 /*DisableMacroExpansion=*/false, /*IsReinject=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00003012}
Reid Kleckner3f1ec622016-09-07 16:38:32 +00003013
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003014/// Handle the Microsoft \#pragma intrinsic extension.
Reid Kleckner3f1ec622016-09-07 16:38:32 +00003015///
3016/// The syntax is:
3017/// \code
3018/// #pragma intrinsic(memset)
3019/// #pragma intrinsic(strlen, memcpy)
3020/// \endcode
3021///
3022/// Pragma intrisic tells the compiler to use a builtin version of the
3023/// function. Clang does it anyway, so the pragma doesn't really do anything.
3024/// Anyway, we emit a warning if the function specified in \#pragma intrinsic
3025/// isn't an intrinsic in clang and suggest to include intrin.h.
3026void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00003027 PragmaIntroducer Introducer,
Reid Kleckner3f1ec622016-09-07 16:38:32 +00003028 Token &Tok) {
3029 PP.Lex(Tok);
3030
3031 if (Tok.isNot(tok::l_paren)) {
3032 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
3033 << "intrinsic";
3034 return;
3035 }
3036 PP.Lex(Tok);
3037
3038 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
3039
3040 while (Tok.is(tok::identifier)) {
3041 IdentifierInfo *II = Tok.getIdentifierInfo();
3042 if (!II->getBuiltinID())
3043 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
3044 << II << SuggestIntrinH;
3045
3046 PP.Lex(Tok);
3047 if (Tok.isNot(tok::comma))
3048 break;
3049 PP.Lex(Tok);
3050 }
3051
3052 if (Tok.isNot(tok::r_paren)) {
3053 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
3054 << "intrinsic";
3055 return;
3056 }
3057 PP.Lex(Tok);
3058
3059 if (Tok.isNot(tok::eod))
3060 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3061 << "intrinsic";
3062}
Hans Wennborg1bbe00e2018-03-20 08:53:11 +00003063
3064// #pragma optimize("gsty", on|off)
3065void PragmaMSOptimizeHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00003066 PragmaIntroducer Introducer,
Hans Wennborg1bbe00e2018-03-20 08:53:11 +00003067 Token &Tok) {
3068 SourceLocation StartLoc = Tok.getLocation();
3069 PP.Lex(Tok);
3070
3071 if (Tok.isNot(tok::l_paren)) {
3072 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "optimize";
3073 return;
3074 }
3075 PP.Lex(Tok);
3076
3077 if (Tok.isNot(tok::string_literal)) {
3078 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_string) << "optimize";
3079 return;
3080 }
3081 // We could syntax check the string but it's probably not worth the effort.
3082 PP.Lex(Tok);
3083
3084 if (Tok.isNot(tok::comma)) {
3085 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_comma) << "optimize";
3086 return;
3087 }
3088 PP.Lex(Tok);
3089
3090 if (Tok.is(tok::eod) || Tok.is(tok::r_paren)) {
3091 PP.Diag(Tok.getLocation(), diag::warn_pragma_missing_argument)
3092 << "optimize" << /*Expected=*/true << "'on' or 'off'";
3093 return;
3094 }
3095 IdentifierInfo *II = Tok.getIdentifierInfo();
3096 if (!II || (!II->isStr("on") && !II->isStr("off"))) {
3097 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
3098 << PP.getSpelling(Tok) << "optimize" << /*Expected=*/true
3099 << "'on' or 'off'";
3100 return;
3101 }
3102 PP.Lex(Tok);
3103
3104 if (Tok.isNot(tok::r_paren)) {
3105 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "optimize";
3106 return;
3107 }
3108 PP.Lex(Tok);
3109
3110 if (Tok.isNot(tok::eod)) {
3111 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3112 << "optimize";
3113 return;
3114 }
3115 PP.Diag(StartLoc, diag::warn_pragma_optimize);
3116}
3117
Justin Lebar67a78a62016-10-08 22:15:58 +00003118void PragmaForceCUDAHostDeviceHandler::HandlePragma(
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00003119 Preprocessor &PP, PragmaIntroducer Introducer, Token &Tok) {
Justin Lebar67a78a62016-10-08 22:15:58 +00003120 Token FirstTok = Tok;
3121
3122 PP.Lex(Tok);
3123 IdentifierInfo *Info = Tok.getIdentifierInfo();
3124 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
3125 PP.Diag(FirstTok.getLocation(),
3126 diag::warn_pragma_force_cuda_host_device_bad_arg);
3127 return;
3128 }
3129
3130 if (Info->isStr("begin"))
3131 Actions.PushForceCUDAHostDevice();
3132 else if (!Actions.PopForceCUDAHostDevice())
3133 PP.Diag(FirstTok.getLocation(),
3134 diag::err_pragma_cannot_end_force_cuda_host_device);
3135
3136 PP.Lex(Tok);
3137 if (!Tok.is(tok::eod))
3138 PP.Diag(FirstTok.getLocation(),
3139 diag::warn_pragma_force_cuda_host_device_bad_arg);
3140}
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003141
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003142/// Handle the #pragma clang attribute directive.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003143///
3144/// The syntax is:
3145/// \code
Erik Pilkington0876cae2018-12-20 22:32:04 +00003146/// #pragma clang attribute push (attribute, subject-set)
Erik Pilkington7d180942018-10-29 17:38:42 +00003147/// #pragma clang attribute push
3148/// #pragma clang attribute (attribute, subject-set)
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003149/// #pragma clang attribute pop
3150/// \endcode
3151///
Erik Pilkington0876cae2018-12-20 22:32:04 +00003152/// There are also 'namespace' variants of push and pop directives. The bare
3153/// '#pragma clang attribute (attribute, subject-set)' version doesn't require a
3154/// namespace, since it always applies attributes to the most recently pushed
3155/// group, regardless of namespace.
3156/// \code
3157/// #pragma clang attribute namespace.push (attribute, subject-set)
3158/// #pragma clang attribute namespace.push
3159/// #pragma clang attribute namespace.pop
3160/// \endcode
3161///
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003162/// The subject-set clause defines the set of declarations which receive the
3163/// attribute. Its exact syntax is described in the LanguageExtensions document
3164/// in Clang's documentation.
3165///
3166/// This directive instructs the compiler to begin/finish applying the specified
3167/// attribute to the set of attribute-specific declarations in the active range
3168/// of the pragma.
3169void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
Joel E. Dennyddde0ec2019-05-21 23:51:38 +00003170 PragmaIntroducer Introducer,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003171 Token &FirstToken) {
3172 Token Tok;
3173 PP.Lex(Tok);
3174 auto *Info = new (PP.getPreprocessorAllocator())
3175 PragmaAttributeInfo(AttributesForPragmaAttribute);
3176
Erik Pilkington0876cae2018-12-20 22:32:04 +00003177 // Parse the optional namespace followed by a period.
3178 if (Tok.is(tok::identifier)) {
3179 IdentifierInfo *II = Tok.getIdentifierInfo();
3180 if (!II->isStr("push") && !II->isStr("pop")) {
3181 Info->Namespace = II;
3182 PP.Lex(Tok);
3183
3184 if (!Tok.is(tok::period)) {
3185 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_period)
3186 << II;
3187 return;
3188 }
3189 PP.Lex(Tok);
3190 }
3191 }
3192
Erik Pilkington7d180942018-10-29 17:38:42 +00003193 if (!Tok.isOneOf(tok::identifier, tok::l_paren)) {
3194 PP.Diag(Tok.getLocation(),
3195 diag::err_pragma_attribute_expected_push_pop_paren);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003196 return;
3197 }
Erik Pilkington7d180942018-10-29 17:38:42 +00003198
3199 // Determine what action this pragma clang attribute represents.
Erik Pilkington0876cae2018-12-20 22:32:04 +00003200 if (Tok.is(tok::l_paren)) {
3201 if (Info->Namespace) {
3202 PP.Diag(Tok.getLocation(),
3203 diag::err_pragma_attribute_namespace_on_attribute);
3204 PP.Diag(Tok.getLocation(),
3205 diag::note_pragma_attribute_namespace_on_attribute);
3206 return;
3207 }
Erik Pilkington7d180942018-10-29 17:38:42 +00003208 Info->Action = PragmaAttributeInfo::Attribute;
Erik Pilkington0876cae2018-12-20 22:32:04 +00003209 } else {
Erik Pilkington7d180942018-10-29 17:38:42 +00003210 const IdentifierInfo *II = Tok.getIdentifierInfo();
3211 if (II->isStr("push"))
3212 Info->Action = PragmaAttributeInfo::Push;
3213 else if (II->isStr("pop"))
3214 Info->Action = PragmaAttributeInfo::Pop;
3215 else {
3216 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
3217 << PP.getSpelling(Tok);
3218 return;
3219 }
3220
3221 PP.Lex(Tok);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003222 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003223
3224 // Parse the actual attribute.
Erik Pilkington7d180942018-10-29 17:38:42 +00003225 if ((Info->Action == PragmaAttributeInfo::Push && Tok.isNot(tok::eod)) ||
3226 Info->Action == PragmaAttributeInfo::Attribute) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003227 if (Tok.isNot(tok::l_paren)) {
3228 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
3229 return;
3230 }
3231 PP.Lex(Tok);
3232
3233 // Lex the attribute tokens.
3234 SmallVector<Token, 16> AttributeTokens;
3235 int OpenParens = 1;
3236 while (Tok.isNot(tok::eod)) {
3237 if (Tok.is(tok::l_paren))
3238 OpenParens++;
3239 else if (Tok.is(tok::r_paren)) {
3240 OpenParens--;
3241 if (OpenParens == 0)
3242 break;
3243 }
3244
3245 AttributeTokens.push_back(Tok);
3246 PP.Lex(Tok);
3247 }
3248
3249 if (AttributeTokens.empty()) {
3250 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
3251 return;
3252 }
3253 if (Tok.isNot(tok::r_paren)) {
3254 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
3255 return;
3256 }
3257 SourceLocation EndLoc = Tok.getLocation();
3258 PP.Lex(Tok);
3259
3260 // Terminate the attribute for parsing.
3261 Token EOFTok;
3262 EOFTok.startToken();
3263 EOFTok.setKind(tok::eof);
3264 EOFTok.setLocation(EndLoc);
3265 AttributeTokens.push_back(EOFTok);
3266
3267 Info->Tokens =
3268 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
3269 }
3270
3271 if (Tok.isNot(tok::eod))
3272 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3273 << "clang attribute";
3274
3275 // Generate the annotated pragma token.
3276 auto TokenArray = llvm::make_unique<Token[]>(1);
3277 TokenArray[0].startToken();
3278 TokenArray[0].setKind(tok::annot_pragma_attribute);
3279 TokenArray[0].setLocation(FirstToken.getLocation());
3280 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
3281 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
3282 PP.EnterTokenStream(std::move(TokenArray), 1,
Ilya Biryukov929af672019-05-17 09:32:05 +00003283 /*DisableMacroExpansion=*/false, /*IsReinject=*/false);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003284}