blob: f0ac49f3d96e38358691a433eeed5ac1eed7677e [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") {}
Craig Topper2b07f022014-03-12 05:09:18 +000029 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
30 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000031};
32
33struct PragmaGCCVisibilityHandler : public PragmaHandler {
34 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
Craig Topper2b07f022014-03-12 05:09:18 +000035 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
36 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000037};
38
39struct PragmaOptionsHandler : public PragmaHandler {
40 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
Craig Topper2b07f022014-03-12 05:09:18 +000041 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
42 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000043};
44
45struct PragmaPackHandler : public PragmaHandler {
46 explicit PragmaPackHandler() : PragmaHandler("pack") {}
Craig Topper2b07f022014-03-12 05:09:18 +000047 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
48 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) {}
54 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
55 Token &FirstToken) override;
56private:
57 Sema &Actions;
58};
59
Reid Kleckner5b086462014-02-20 22:52:09 +000060struct PragmaMSStructHandler : public PragmaHandler {
61 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
Craig Topper2b07f022014-03-12 05:09:18 +000062 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
63 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000064};
65
66struct PragmaUnusedHandler : public PragmaHandler {
67 PragmaUnusedHandler() : PragmaHandler("unused") {}
Craig Topper2b07f022014-03-12 05:09:18 +000068 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
69 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000070};
71
72struct PragmaWeakHandler : public PragmaHandler {
73 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
Craig Topper2b07f022014-03-12 05:09:18 +000074 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
75 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000076};
77
78struct PragmaRedefineExtnameHandler : public PragmaHandler {
79 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
Craig Topper2b07f022014-03-12 05:09:18 +000080 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
81 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000082};
83
84struct PragmaOpenCLExtensionHandler : public PragmaHandler {
85 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
Craig Topper2b07f022014-03-12 05:09:18 +000086 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
87 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000088};
89
90
91struct PragmaFPContractHandler : public PragmaHandler {
92 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
Craig Topper2b07f022014-03-12 05:09:18 +000093 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
94 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +000095};
96
Steven Wub96a3a42018-01-05 22:45:03 +000097// Pragma STDC implementations.
98
99/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
100struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
101 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
102
103 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
104 Token &Tok) override {
105 tok::OnOffSwitch OOS;
106 if (PP.LexOnOffSwitch(OOS))
107 return;
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000108 if (OOS == tok::OOS_ON) {
Steven Wub96a3a42018-01-05 22:45:03 +0000109 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000110 }
111
112 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
113 1);
114 Toks[0].startToken();
115 Toks[0].setKind(tok::annot_pragma_fenv_access);
116 Toks[0].setLocation(Tok.getLocation());
117 Toks[0].setAnnotationEndLoc(Tok.getLocation());
118 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
119 static_cast<uintptr_t>(OOS)));
120 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Steven Wub96a3a42018-01-05 22:45:03 +0000121 }
122};
123
124/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
125struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
126 PragmaSTDC_CX_LIMITED_RANGEHandler() : PragmaHandler("CX_LIMITED_RANGE") {}
127
128 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
129 Token &Tok) override {
130 tok::OnOffSwitch OOS;
131 PP.LexOnOffSwitch(OOS);
132 }
133};
134
135/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
136struct PragmaSTDC_UnknownHandler : public PragmaHandler {
137 PragmaSTDC_UnknownHandler() = default;
138
139 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
140 Token &UnknownTok) override {
141 // C99 6.10.6p2, unknown forms are not allowed.
142 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
143 }
144};
145
Adam Nemet60d32642017-04-04 21:18:36 +0000146struct PragmaFPHandler : public PragmaHandler {
147 PragmaFPHandler() : PragmaHandler("fp") {}
148 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
149 Token &FirstToken) override;
150};
151
Reid Kleckner5b086462014-02-20 22:52:09 +0000152struct PragmaNoOpenMPHandler : public PragmaHandler {
153 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +0000154 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
155 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000156};
157
158struct PragmaOpenMPHandler : public PragmaHandler {
159 PragmaOpenMPHandler() : PragmaHandler("omp") { }
Craig Topper2b07f022014-03-12 05:09:18 +0000160 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
161 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000162};
163
164/// PragmaCommentHandler - "\#pragma comment ...".
165struct PragmaCommentHandler : public PragmaHandler {
166 PragmaCommentHandler(Sema &Actions)
167 : PragmaHandler("comment"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000168 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
169 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000170private:
171 Sema &Actions;
172};
173
174struct PragmaDetectMismatchHandler : public PragmaHandler {
175 PragmaDetectMismatchHandler(Sema &Actions)
176 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
Craig Topper2b07f022014-03-12 05:09:18 +0000177 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
178 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000179private:
180 Sema &Actions;
181};
182
183struct PragmaMSPointersToMembers : public PragmaHandler {
184 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000185 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
186 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000187};
188
189struct PragmaMSVtorDisp : public PragmaHandler {
190 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
Craig Topper2b07f022014-03-12 05:09:18 +0000191 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
192 Token &FirstToken) override;
Reid Kleckner5b086462014-02-20 22:52:09 +0000193};
194
Warren Huntc3b18962014-04-08 22:30:47 +0000195struct PragmaMSPragma : public PragmaHandler {
196 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000197 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
198 Token &FirstToken) override;
199};
200
Dario Domizioli13a0a382014-05-23 12:13:25 +0000201/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
202struct PragmaOptimizeHandler : public PragmaHandler {
203 PragmaOptimizeHandler(Sema &S)
204 : PragmaHandler("optimize"), Actions(S) {}
205 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
206 Token &FirstToken) override;
207private:
Eli Bendersky06a40422014-06-06 20:31:48 +0000208 Sema &Actions;
209};
210
211struct PragmaLoopHintHandler : public PragmaHandler {
212 PragmaLoopHintHandler() : PragmaHandler("loop") {}
213 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
214 Token &FirstToken) override;
215};
216
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000217struct PragmaUnrollHintHandler : public PragmaHandler {
218 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
219 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
220 Token &FirstToken) override;
221};
222
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000223struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
224 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
225};
226
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000227struct PragmaMSIntrinsicHandler : public PragmaHandler {
228 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
229 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
230 Token &FirstToken) override;
231};
232
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000233struct PragmaMSOptimizeHandler : public PragmaHandler {
234 PragmaMSOptimizeHandler() : PragmaHandler("optimize") {}
235 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
236 Token &FirstToken) override;
237};
238
Justin Lebar67a78a62016-10-08 22:15:58 +0000239struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
240 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
241 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
242 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
243 Token &FirstToken) override;
244
245private:
246 Sema &Actions;
247};
248
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000249/// PragmaAttributeHandler - "\#pragma clang attribute ...".
250struct PragmaAttributeHandler : public PragmaHandler {
251 PragmaAttributeHandler(AttributeFactory &AttrFactory)
252 : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {}
253 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
254 Token &FirstToken) override;
255
256 /// A pool of attributes that were parsed in \#pragma clang attribute.
257 ParsedAttributes AttributesForPragmaAttribute;
258};
259
Eli Bendersky06a40422014-06-06 20:31:48 +0000260} // end namespace
261
262void Parser::initializePragmaHandlers() {
David Blaikiee0cfc042018-11-15 03:04:23 +0000263 AlignHandler = llvm::make_unique<PragmaAlignHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000264 PP.AddPragmaHandler(AlignHandler.get());
265
David Blaikiee0cfc042018-11-15 03:04:23 +0000266 GCCVisibilityHandler = llvm::make_unique<PragmaGCCVisibilityHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000267 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
268
David Blaikiee0cfc042018-11-15 03:04:23 +0000269 OptionsHandler = llvm::make_unique<PragmaOptionsHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000270 PP.AddPragmaHandler(OptionsHandler.get());
271
David Blaikiee0cfc042018-11-15 03:04:23 +0000272 PackHandler = llvm::make_unique<PragmaPackHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000273 PP.AddPragmaHandler(PackHandler.get());
274
David Blaikiee0cfc042018-11-15 03:04:23 +0000275 MSStructHandler = llvm::make_unique<PragmaMSStructHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000276 PP.AddPragmaHandler(MSStructHandler.get());
277
David Blaikiee0cfc042018-11-15 03:04:23 +0000278 UnusedHandler = llvm::make_unique<PragmaUnusedHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000279 PP.AddPragmaHandler(UnusedHandler.get());
280
David Blaikiee0cfc042018-11-15 03:04:23 +0000281 WeakHandler = llvm::make_unique<PragmaWeakHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000282 PP.AddPragmaHandler(WeakHandler.get());
283
David Blaikiee0cfc042018-11-15 03:04:23 +0000284 RedefineExtnameHandler = llvm::make_unique<PragmaRedefineExtnameHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000285 PP.AddPragmaHandler(RedefineExtnameHandler.get());
286
David Blaikiee0cfc042018-11-15 03:04:23 +0000287 FPContractHandler = llvm::make_unique<PragmaFPContractHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000288 PP.AddPragmaHandler("STDC", FPContractHandler.get());
289
David Blaikiee0cfc042018-11-15 03:04:23 +0000290 STDCFENVHandler = llvm::make_unique<PragmaSTDC_FENV_ACCESSHandler>();
Steven Wub96a3a42018-01-05 22:45:03 +0000291 PP.AddPragmaHandler("STDC", STDCFENVHandler.get());
292
David Blaikiee0cfc042018-11-15 03:04:23 +0000293 STDCCXLIMITHandler = llvm::make_unique<PragmaSTDC_CX_LIMITED_RANGEHandler>();
Steven Wub96a3a42018-01-05 22:45:03 +0000294 PP.AddPragmaHandler("STDC", STDCCXLIMITHandler.get());
295
David Blaikiee0cfc042018-11-15 03:04:23 +0000296 STDCUnknownHandler = llvm::make_unique<PragmaSTDC_UnknownHandler>();
Steven Wub96a3a42018-01-05 22:45:03 +0000297 PP.AddPragmaHandler("STDC", STDCUnknownHandler.get());
298
David Blaikiee0cfc042018-11-15 03:04:23 +0000299 PCSectionHandler = llvm::make_unique<PragmaClangSectionHandler>(Actions);
Javed Absar2a67c9e2017-06-05 10:11:57 +0000300 PP.AddPragmaHandler("clang", PCSectionHandler.get());
301
Reid Kleckner5b086462014-02-20 22:52:09 +0000302 if (getLangOpts().OpenCL) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000303 OpenCLExtensionHandler = llvm::make_unique<PragmaOpenCLExtensionHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000304 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
305
306 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
307 }
308 if (getLangOpts().OpenMP)
David Blaikiee0cfc042018-11-15 03:04:23 +0000309 OpenMPHandler = llvm::make_unique<PragmaOpenMPHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000310 else
David Blaikiee0cfc042018-11-15 03:04:23 +0000311 OpenMPHandler = llvm::make_unique<PragmaNoOpenMPHandler>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000312 PP.AddPragmaHandler(OpenMPHandler.get());
313
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000314 if (getLangOpts().MicrosoftExt ||
315 getTargetInfo().getTriple().isOSBinFormatELF()) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000316 MSCommentHandler = llvm::make_unique<PragmaCommentHandler>(Actions);
Reid Kleckner5b086462014-02-20 22:52:09 +0000317 PP.AddPragmaHandler(MSCommentHandler.get());
Yunzhong Gao99efc032015-03-23 20:41:42 +0000318 }
319
320 if (getLangOpts().MicrosoftExt) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000321 MSDetectMismatchHandler =
322 llvm::make_unique<PragmaDetectMismatchHandler>(Actions);
Reid Kleckner5b086462014-02-20 22:52:09 +0000323 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000324 MSPointersToMembers = llvm::make_unique<PragmaMSPointersToMembers>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000325 PP.AddPragmaHandler(MSPointersToMembers.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000326 MSVtorDisp = llvm::make_unique<PragmaMSVtorDisp>();
Reid Kleckner5b086462014-02-20 22:52:09 +0000327 PP.AddPragmaHandler(MSVtorDisp.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000328 MSInitSeg = llvm::make_unique<PragmaMSPragma>("init_seg");
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000329 PP.AddPragmaHandler(MSInitSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000330 MSDataSeg = llvm::make_unique<PragmaMSPragma>("data_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000331 PP.AddPragmaHandler(MSDataSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000332 MSBSSSeg = llvm::make_unique<PragmaMSPragma>("bss_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000333 PP.AddPragmaHandler(MSBSSSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000334 MSConstSeg = llvm::make_unique<PragmaMSPragma>("const_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000335 PP.AddPragmaHandler(MSConstSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000336 MSCodeSeg = llvm::make_unique<PragmaMSPragma>("code_seg");
Warren Huntc3b18962014-04-08 22:30:47 +0000337 PP.AddPragmaHandler(MSCodeSeg.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000338 MSSection = llvm::make_unique<PragmaMSPragma>("section");
Warren Huntc3b18962014-04-08 22:30:47 +0000339 PP.AddPragmaHandler(MSSection.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000340 MSRuntimeChecks = llvm::make_unique<PragmaMSRuntimeChecksHandler>();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000341 PP.AddPragmaHandler(MSRuntimeChecks.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000342 MSIntrinsic = llvm::make_unique<PragmaMSIntrinsicHandler>();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000343 PP.AddPragmaHandler(MSIntrinsic.get());
David Blaikiee0cfc042018-11-15 03:04:23 +0000344 MSOptimize = llvm::make_unique<PragmaMSOptimizeHandler>();
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000345 PP.AddPragmaHandler(MSOptimize.get());
Reid Kleckner5b086462014-02-20 22:52:09 +0000346 }
Eli Bendersky06a40422014-06-06 20:31:48 +0000347
Justin Lebar67a78a62016-10-08 22:15:58 +0000348 if (getLangOpts().CUDA) {
David Blaikiee0cfc042018-11-15 03:04:23 +0000349 CUDAForceHostDeviceHandler =
350 llvm::make_unique<PragmaForceCUDAHostDeviceHandler>(Actions);
Justin Lebar67a78a62016-10-08 22:15:58 +0000351 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
352 }
353
David Blaikiee0cfc042018-11-15 03:04:23 +0000354 OptimizeHandler = llvm::make_unique<PragmaOptimizeHandler>(Actions);
Eli Bendersky06a40422014-06-06 20:31:48 +0000355 PP.AddPragmaHandler("clang", OptimizeHandler.get());
356
David Blaikiee0cfc042018-11-15 03:04:23 +0000357 LoopHintHandler = llvm::make_unique<PragmaLoopHintHandler>();
Eli Bendersky06a40422014-06-06 20:31:48 +0000358 PP.AddPragmaHandler("clang", LoopHintHandler.get());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000359
David Blaikiee0cfc042018-11-15 03:04:23 +0000360 UnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("unroll");
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000361 PP.AddPragmaHandler(UnrollHintHandler.get());
Mark Heffernanc888e412014-07-24 18:09:38 +0000362
David Blaikiee0cfc042018-11-15 03:04:23 +0000363 NoUnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("nounroll");
Mark Heffernanc888e412014-07-24 18:09:38 +0000364 PP.AddPragmaHandler(NoUnrollHintHandler.get());
Adam Nemet60d32642017-04-04 21:18:36 +0000365
David Blaikiee0cfc042018-11-15 03:04:23 +0000366 UnrollAndJamHintHandler =
367 llvm::make_unique<PragmaUnrollHintHandler>("unroll_and_jam");
David Greenc8e39242018-08-01 14:36:12 +0000368 PP.AddPragmaHandler(UnrollAndJamHintHandler.get());
369
David Blaikiee0cfc042018-11-15 03:04:23 +0000370 NoUnrollAndJamHintHandler =
371 llvm::make_unique<PragmaUnrollHintHandler>("nounroll_and_jam");
David Greenc8e39242018-08-01 14:36:12 +0000372 PP.AddPragmaHandler(NoUnrollAndJamHintHandler.get());
373
David Blaikiee0cfc042018-11-15 03:04:23 +0000374 FPHandler = llvm::make_unique<PragmaFPHandler>();
Adam Nemet60d32642017-04-04 21:18:36 +0000375 PP.AddPragmaHandler("clang", FPHandler.get());
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000376
David Blaikiee0cfc042018-11-15 03:04:23 +0000377 AttributePragmaHandler =
378 llvm::make_unique<PragmaAttributeHandler>(AttrFactory);
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000379 PP.AddPragmaHandler("clang", AttributePragmaHandler.get());
Eli Bendersky06a40422014-06-06 20:31:48 +0000380}
381
382void Parser::resetPragmaHandlers() {
Reid Kleckner5b086462014-02-20 22:52:09 +0000383 // Remove the pragma handlers we installed.
384 PP.RemovePragmaHandler(AlignHandler.get());
385 AlignHandler.reset();
386 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
387 GCCVisibilityHandler.reset();
388 PP.RemovePragmaHandler(OptionsHandler.get());
389 OptionsHandler.reset();
390 PP.RemovePragmaHandler(PackHandler.get());
391 PackHandler.reset();
392 PP.RemovePragmaHandler(MSStructHandler.get());
393 MSStructHandler.reset();
394 PP.RemovePragmaHandler(UnusedHandler.get());
395 UnusedHandler.reset();
396 PP.RemovePragmaHandler(WeakHandler.get());
397 WeakHandler.reset();
398 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
399 RedefineExtnameHandler.reset();
400
401 if (getLangOpts().OpenCL) {
402 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
403 OpenCLExtensionHandler.reset();
404 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
405 }
406 PP.RemovePragmaHandler(OpenMPHandler.get());
407 OpenMPHandler.reset();
408
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +0000409 if (getLangOpts().MicrosoftExt ||
410 getTargetInfo().getTriple().isOSBinFormatELF()) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000411 PP.RemovePragmaHandler(MSCommentHandler.get());
412 MSCommentHandler.reset();
Yunzhong Gao99efc032015-03-23 20:41:42 +0000413 }
414
Javed Absar2a67c9e2017-06-05 10:11:57 +0000415 PP.RemovePragmaHandler("clang", PCSectionHandler.get());
416 PCSectionHandler.reset();
417
Yunzhong Gao99efc032015-03-23 20:41:42 +0000418 if (getLangOpts().MicrosoftExt) {
Reid Kleckner5b086462014-02-20 22:52:09 +0000419 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
420 MSDetectMismatchHandler.reset();
421 PP.RemovePragmaHandler(MSPointersToMembers.get());
422 MSPointersToMembers.reset();
423 PP.RemovePragmaHandler(MSVtorDisp.get());
424 MSVtorDisp.reset();
Reid Klecknerd3923aa2014-04-03 19:04:24 +0000425 PP.RemovePragmaHandler(MSInitSeg.get());
426 MSInitSeg.reset();
Warren Huntc3b18962014-04-08 22:30:47 +0000427 PP.RemovePragmaHandler(MSDataSeg.get());
428 MSDataSeg.reset();
429 PP.RemovePragmaHandler(MSBSSSeg.get());
430 MSBSSSeg.reset();
431 PP.RemovePragmaHandler(MSConstSeg.get());
432 MSConstSeg.reset();
433 PP.RemovePragmaHandler(MSCodeSeg.get());
434 MSCodeSeg.reset();
435 PP.RemovePragmaHandler(MSSection.get());
436 MSSection.reset();
Hans Wennborg7357bbc2015-10-12 20:47:58 +0000437 PP.RemovePragmaHandler(MSRuntimeChecks.get());
438 MSRuntimeChecks.reset();
Reid Kleckner3f1ec622016-09-07 16:38:32 +0000439 PP.RemovePragmaHandler(MSIntrinsic.get());
440 MSIntrinsic.reset();
Hans Wennborg1bbe00e2018-03-20 08:53:11 +0000441 PP.RemovePragmaHandler(MSOptimize.get());
442 MSOptimize.reset();
Reid Kleckner5b086462014-02-20 22:52:09 +0000443 }
444
Justin Lebar67a78a62016-10-08 22:15:58 +0000445 if (getLangOpts().CUDA) {
446 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
447 CUDAForceHostDeviceHandler.reset();
448 }
449
Reid Kleckner5b086462014-02-20 22:52:09 +0000450 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
451 FPContractHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000452
Steven Wub96a3a42018-01-05 22:45:03 +0000453 PP.RemovePragmaHandler("STDC", STDCFENVHandler.get());
454 STDCFENVHandler.reset();
455
456 PP.RemovePragmaHandler("STDC", STDCCXLIMITHandler.get());
457 STDCCXLIMITHandler.reset();
458
459 PP.RemovePragmaHandler("STDC", STDCUnknownHandler.get());
460 STDCUnknownHandler.reset();
461
Eli Bendersky06a40422014-06-06 20:31:48 +0000462 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
463 OptimizeHandler.reset();
464
465 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
466 LoopHintHandler.reset();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000467
468 PP.RemovePragmaHandler(UnrollHintHandler.get());
469 UnrollHintHandler.reset();
Mark Heffernanc888e412014-07-24 18:09:38 +0000470
471 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
472 NoUnrollHintHandler.reset();
Adam Nemet60d32642017-04-04 21:18:36 +0000473
David Greenc8e39242018-08-01 14:36:12 +0000474 PP.RemovePragmaHandler(UnrollAndJamHintHandler.get());
475 UnrollAndJamHintHandler.reset();
476
477 PP.RemovePragmaHandler(NoUnrollAndJamHintHandler.get());
478 NoUnrollAndJamHintHandler.reset();
479
Adam Nemet60d32642017-04-04 21:18:36 +0000480 PP.RemovePragmaHandler("clang", FPHandler.get());
481 FPHandler.reset();
Alex Lorenz9e7bf162017-04-18 14:33:39 +0000482
483 PP.RemovePragmaHandler("clang", AttributePragmaHandler.get());
484 AttributePragmaHandler.reset();
Eli Bendersky06a40422014-06-06 20:31:48 +0000485}
486
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000487/// Handle the annotation token produced for #pragma unused(...)
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000488///
489/// Each annot_pragma_unused is followed by the argument token so e.g.
490/// "#pragma unused(x,y)" becomes:
491/// annot_pragma_unused 'x' annot_pragma_unused 'y'
492void Parser::HandlePragmaUnused() {
493 assert(Tok.is(tok::annot_pragma_unused));
Richard Smithaf3b3252017-05-18 19:21:48 +0000494 SourceLocation UnusedLoc = ConsumeAnnotationToken();
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +0000495 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
496 ConsumeToken(); // The argument token.
497}
Eli Friedman570024a2010-08-05 06:57:20 +0000498
Rafael Espindola273fd772012-01-26 02:02:57 +0000499void Parser::HandlePragmaVisibility() {
500 assert(Tok.is(tok::annot_pragma_vis));
501 const IdentifierInfo *VisType =
502 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
Richard Smithaf3b3252017-05-18 19:21:48 +0000503 SourceLocation VisLoc = ConsumeAnnotationToken();
Rafael Espindola273fd772012-01-26 02:02:57 +0000504 Actions.ActOnPragmaVisibility(VisType, VisLoc);
505}
506
Benjamin Kramere003ca22015-10-28 13:54:16 +0000507namespace {
Eli Friedmanec52f922012-02-23 23:47:16 +0000508struct PragmaPackInfo {
Denis Zobnin10c4f452016-04-29 18:17:40 +0000509 Sema::PragmaMsStackAction Action;
510 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +0000511 Token Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +0000512};
Benjamin Kramere003ca22015-10-28 13:54:16 +0000513} // end anonymous namespace
Eli Friedmanec52f922012-02-23 23:47:16 +0000514
515void Parser::HandlePragmaPack() {
516 assert(Tok.is(tok::annot_pragma_pack));
517 PragmaPackInfo *Info =
518 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
Alex Lorenz45b40142017-07-28 14:41:21 +0000519 SourceLocation PragmaLoc = Tok.getLocation();
Eli Friedman68be1642012-10-04 02:36:51 +0000520 ExprResult Alignment;
521 if (Info->Alignment.is(tok::numeric_constant)) {
522 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
Alex Lorenz45b40142017-07-28 14:41:21 +0000523 if (Alignment.isInvalid()) {
524 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000525 return;
Alex Lorenz45b40142017-07-28 14:41:21 +0000526 }
Eli Friedman68be1642012-10-04 02:36:51 +0000527 }
Denis Zobnin10c4f452016-04-29 18:17:40 +0000528 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
529 Alignment.get());
Alex Lorenz45b40142017-07-28 14:41:21 +0000530 // Consume the token after processing the pragma to enable pragma-specific
531 // #include warnings.
532 ConsumeAnnotationToken();
Eli Friedmanec52f922012-02-23 23:47:16 +0000533}
534
Eli Friedman68be1642012-10-04 02:36:51 +0000535void Parser::HandlePragmaMSStruct() {
536 assert(Tok.is(tok::annot_pragma_msstruct));
Nico Weber779355f2016-03-02 23:22:00 +0000537 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
538 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Eli Friedman68be1642012-10-04 02:36:51 +0000539 Actions.ActOnPragmaMSStruct(Kind);
Richard Smithaf3b3252017-05-18 19:21:48 +0000540 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000541}
542
543void Parser::HandlePragmaAlign() {
544 assert(Tok.is(tok::annot_pragma_align));
545 Sema::PragmaOptionsAlignKind Kind =
546 static_cast<Sema::PragmaOptionsAlignKind>(
547 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Alex Lorenz692821a2018-02-08 21:20:43 +0000548 Actions.ActOnPragmaOptionsAlign(Kind, Tok.getLocation());
549 // Consume the token after processing the pragma to enable pragma-specific
550 // #include warnings.
551 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000552}
553
Richard Smithba3a4f92016-01-12 21:59:26 +0000554void Parser::HandlePragmaDump() {
555 assert(Tok.is(tok::annot_pragma_dump));
556 IdentifierInfo *II =
557 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
558 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
Richard Smithaf3b3252017-05-18 19:21:48 +0000559 ConsumeAnnotationToken();
Richard Smithba3a4f92016-01-12 21:59:26 +0000560}
561
Eli Friedman68be1642012-10-04 02:36:51 +0000562void Parser::HandlePragmaWeak() {
563 assert(Tok.is(tok::annot_pragma_weak));
Richard Smithaf3b3252017-05-18 19:21:48 +0000564 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000565 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
566 Tok.getLocation());
567 ConsumeToken(); // The weak name.
568}
569
570void Parser::HandlePragmaWeakAlias() {
571 assert(Tok.is(tok::annot_pragma_weakalias));
Richard Smithaf3b3252017-05-18 19:21:48 +0000572 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000573 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
574 SourceLocation WeakNameLoc = Tok.getLocation();
575 ConsumeToken();
576 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
577 SourceLocation AliasNameLoc = Tok.getLocation();
578 ConsumeToken();
579 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
580 WeakNameLoc, AliasNameLoc);
581
582}
583
584void Parser::HandlePragmaRedefineExtname() {
585 assert(Tok.is(tok::annot_pragma_redefine_extname));
Richard Smithaf3b3252017-05-18 19:21:48 +0000586 SourceLocation RedefLoc = ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000587 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
588 SourceLocation RedefNameLoc = Tok.getLocation();
589 ConsumeToken();
590 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
591 SourceLocation AliasNameLoc = Tok.getLocation();
592 ConsumeToken();
593 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
594 RedefNameLoc, AliasNameLoc);
595}
596
597void Parser::HandlePragmaFPContract() {
598 assert(Tok.is(tok::annot_pragma_fp_contract));
599 tok::OnOffSwitch OOS =
600 static_cast<tok::OnOffSwitch>(
601 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Adam Nemet60d32642017-04-04 21:18:36 +0000602
603 LangOptions::FPContractModeKind FPC;
604 switch (OOS) {
605 case tok::OOS_ON:
606 FPC = LangOptions::FPC_On;
607 break;
608 case tok::OOS_OFF:
609 FPC = LangOptions::FPC_Off;
610 break;
611 case tok::OOS_DEFAULT:
612 FPC = getLangOpts().getDefaultFPContractMode();
613 break;
614 }
615
616 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +0000617 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000618}
619
Kevin P. Neal2c0bc8b2018-08-14 17:06:56 +0000620void Parser::HandlePragmaFEnvAccess() {
621 assert(Tok.is(tok::annot_pragma_fenv_access));
622 tok::OnOffSwitch OOS =
623 static_cast<tok::OnOffSwitch>(
624 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
625
626 LangOptions::FEnvAccessModeKind FPC;
627 switch (OOS) {
628 case tok::OOS_ON:
629 FPC = LangOptions::FEA_On;
630 break;
631 case tok::OOS_OFF:
632 FPC = LangOptions::FEA_Off;
633 break;
634 case tok::OOS_DEFAULT: // FIXME: Add this cli option when it makes sense.
635 FPC = LangOptions::FEA_Off;
636 break;
637 }
638
639 Actions.ActOnPragmaFEnvAccess(FPC);
640 ConsumeAnnotationToken();
641}
642
643
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000644StmtResult Parser::HandlePragmaCaptured()
645{
646 assert(Tok.is(tok::annot_pragma_captured));
Richard Smithaf3b3252017-05-18 19:21:48 +0000647 ConsumeAnnotationToken();
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000648
649 if (Tok.isNot(tok::l_brace)) {
Alp Tokerec543272013-12-24 09:48:30 +0000650 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000651 return StmtError();
652 }
653
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000654 SourceLocation Loc = Tok.getLocation();
655
Momchil Velikov57c681f2017-08-10 15:43:06 +0000656 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope |
657 Scope::CompoundStmtScope);
Ben Langmuir37943a72013-05-03 19:00:33 +0000658 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
659 /*NumParams=*/1);
Tareq A. Siraj6dfa25a2013-04-16 19:37:38 +0000660
661 StmtResult R = ParseCompoundStatement();
662 CapturedRegionScope.Exit();
663
664 if (R.isInvalid()) {
665 Actions.ActOnCapturedRegionError();
666 return StmtError();
667 }
668
669 return Actions.ActOnCapturedRegionEnd(R.get());
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000670}
671
Eli Friedman68be1642012-10-04 02:36:51 +0000672namespace {
Yaxun Liu5b746652016-12-18 05:18:55 +0000673 enum OpenCLExtState : char {
674 Disable, Enable, Begin, End
675 };
676 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
Eli Friedman68be1642012-10-04 02:36:51 +0000677}
678
679void Parser::HandlePragmaOpenCLExtension() {
680 assert(Tok.is(tok::annot_pragma_opencl_extension));
Yaxun Liu5b746652016-12-18 05:18:55 +0000681 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
682 auto State = Data->second;
683 auto Ident = Data->first;
Eli Friedman68be1642012-10-04 02:36:51 +0000684 SourceLocation NameLoc = Tok.getLocation();
Richard Smithaf3b3252017-05-18 19:21:48 +0000685 ConsumeAnnotationToken();
Eli Friedman68be1642012-10-04 02:36:51 +0000686
Yaxun Liu5b746652016-12-18 05:18:55 +0000687 auto &Opt = Actions.getOpenCLOptions();
688 auto Name = Ident->getName();
Eli Friedman68be1642012-10-04 02:36:51 +0000689 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
690 // overriding all previously issued extension directives, but only if the
691 // behavior is set to disable."
Yaxun Liu5b746652016-12-18 05:18:55 +0000692 if (Name == "all") {
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000693 if (State == Disable) {
Yaxun Liu5b746652016-12-18 05:18:55 +0000694 Opt.disableAll();
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000695 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
696 } else {
Yaxun Liu5b746652016-12-18 05:18:55 +0000697 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
Konstantin Zhuravlyovde70a882017-01-06 16:14:41 +0000698 }
Yaxun Liu5b746652016-12-18 05:18:55 +0000699 } else if (State == Begin) {
700 if (!Opt.isKnown(Name) ||
701 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
702 Opt.support(Name);
703 }
704 Actions.setCurrentOpenCLExtension(Name);
705 } else if (State == End) {
706 if (Name != Actions.getCurrentOpenCLExtension())
707 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
708 Actions.setCurrentOpenCLExtension("");
709 } else if (!Opt.isKnown(Name))
710 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
711 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
712 Opt.enable(Name, State == Enable);
713 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
714 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
715 else
716 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
Eli Friedman68be1642012-10-04 02:36:51 +0000717}
718
David Majnemer4bb09802014-02-10 19:50:15 +0000719void Parser::HandlePragmaMSPointersToMembers() {
720 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
David Majnemer86c318f2014-02-11 21:05:00 +0000721 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
722 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
David Majnemer4bb09802014-02-10 19:50:15 +0000723 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
Richard Smithaf3b3252017-05-18 19:21:48 +0000724 SourceLocation PragmaLoc = ConsumeAnnotationToken();
David Majnemer4bb09802014-02-10 19:50:15 +0000725 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
726}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000727
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000728void Parser::HandlePragmaMSVtorDisp() {
729 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
730 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
Denis Zobnin2290dac2016-04-29 11:27:00 +0000731 Sema::PragmaMsStackAction Action =
732 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000733 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
Richard Smithaf3b3252017-05-18 19:21:48 +0000734 SourceLocation PragmaLoc = ConsumeAnnotationToken();
Denis Zobnin2290dac2016-04-29 11:27:00 +0000735 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
Reid Klecknerc0dca6d2014-02-12 23:50:26 +0000736}
Tareq A. Siraj0de0dd42013-04-16 18:41:26 +0000737
Warren Huntc3b18962014-04-08 22:30:47 +0000738void Parser::HandlePragmaMSPragma() {
739 assert(Tok.is(tok::annot_pragma_ms_pragma));
740 // Grab the tokens out of the annotation and enter them into the stream.
David Blaikie2eabcc92016-02-09 18:52:09 +0000741 auto TheTokens =
742 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
743 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
Richard Smithaf3b3252017-05-18 19:21:48 +0000744 SourceLocation PragmaLocation = ConsumeAnnotationToken();
Warren Huntc3b18962014-04-08 22:30:47 +0000745 assert(Tok.isAnyIdentifier());
Reid Kleckner722b1df2014-07-18 00:13:16 +0000746 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000747 PP.Lex(Tok); // pragma kind
Reid Kleckner722b1df2014-07-18 00:13:16 +0000748
Warren Huntc3b18962014-04-08 22:30:47 +0000749 // Figure out which #pragma we're dealing with. The switch has no default
750 // because lex shouldn't emit the annotation token for unrecognized pragmas.
Reid Kleckner722b1df2014-07-18 00:13:16 +0000751 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
Warren Huntc3b18962014-04-08 22:30:47 +0000752 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
753 .Case("data_seg", &Parser::HandlePragmaMSSegment)
754 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
755 .Case("const_seg", &Parser::HandlePragmaMSSegment)
756 .Case("code_seg", &Parser::HandlePragmaMSSegment)
757 .Case("section", &Parser::HandlePragmaMSSection)
758 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000759
760 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
761 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
762 // until eof (really end of line) to prevent follow-on errors.
Warren Huntc3b18962014-04-08 22:30:47 +0000763 while (Tok.isNot(tok::eof))
764 PP.Lex(Tok);
765 PP.Lex(Tok);
766 }
767}
768
Reid Kleckner722b1df2014-07-18 00:13:16 +0000769bool Parser::HandlePragmaMSSection(StringRef PragmaName,
770 SourceLocation PragmaLocation) {
771 if (Tok.isNot(tok::l_paren)) {
772 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
773 return false;
774 }
Warren Huntc3b18962014-04-08 22:30:47 +0000775 PP.Lex(Tok); // (
776 // Parsing code for pragma section
Reid Kleckner722b1df2014-07-18 00:13:16 +0000777 if (Tok.isNot(tok::string_literal)) {
778 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
779 << PragmaName;
780 return false;
781 }
782 ExprResult StringResult = ParseStringLiteralExpression();
783 if (StringResult.isInvalid())
784 return false; // Already diagnosed.
785 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
786 if (SegmentName->getCharByteWidth() != 1) {
787 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
788 << PragmaName;
789 return false;
790 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000791 int SectionFlags = ASTContext::PSF_Read;
792 bool SectionFlagsAreDefault = true;
Warren Huntc3b18962014-04-08 22:30:47 +0000793 while (Tok.is(tok::comma)) {
794 PP.Lex(Tok); // ,
David Majnemer48c28fa2014-10-22 21:08:43 +0000795 // Ignore "long" and "short".
796 // They are undocumented, but widely used, section attributes which appear
797 // to do nothing.
798 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
799 PP.Lex(Tok); // long/short
800 continue;
801 }
802
Reid Kleckner722b1df2014-07-18 00:13:16 +0000803 if (!Tok.isAnyIdentifier()) {
804 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
805 << PragmaName;
806 return false;
807 }
Hans Wennborg899ded92014-10-16 20:52:46 +0000808 ASTContext::PragmaSectionFlag Flag =
809 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
Warren Huntc3b18962014-04-08 22:30:47 +0000810 Tok.getIdentifierInfo()->getName())
Hans Wennborg899ded92014-10-16 20:52:46 +0000811 .Case("read", ASTContext::PSF_Read)
812 .Case("write", ASTContext::PSF_Write)
813 .Case("execute", ASTContext::PSF_Execute)
814 .Case("shared", ASTContext::PSF_Invalid)
815 .Case("nopage", ASTContext::PSF_Invalid)
816 .Case("nocache", ASTContext::PSF_Invalid)
817 .Case("discard", ASTContext::PSF_Invalid)
818 .Case("remove", ASTContext::PSF_Invalid)
819 .Default(ASTContext::PSF_None);
820 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
821 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
Reid Kleckner722b1df2014-07-18 00:13:16 +0000822 ? diag::warn_pragma_invalid_specific_action
823 : diag::warn_pragma_unsupported_action)
Warren Huntc3b18962014-04-08 22:30:47 +0000824 << PragmaName << Tok.getIdentifierInfo()->getName();
Reid Kleckner722b1df2014-07-18 00:13:16 +0000825 return false;
Warren Huntc3b18962014-04-08 22:30:47 +0000826 }
827 SectionFlags |= Flag;
David Majnemer48c28fa2014-10-22 21:08:43 +0000828 SectionFlagsAreDefault = false;
Warren Huntc3b18962014-04-08 22:30:47 +0000829 PP.Lex(Tok); // Identifier
830 }
David Majnemer48c28fa2014-10-22 21:08:43 +0000831 // If no section attributes are specified, the section will be marked as
832 // read/write.
833 if (SectionFlagsAreDefault)
834 SectionFlags |= ASTContext::PSF_Write;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000835 if (Tok.isNot(tok::r_paren)) {
836 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
837 return false;
838 }
Warren Huntc3b18962014-04-08 22:30:47 +0000839 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000840 if (Tok.isNot(tok::eof)) {
841 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
842 << PragmaName;
843 return false;
844 }
Warren Huntc3b18962014-04-08 22:30:47 +0000845 PP.Lex(Tok); // eof
846 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000847 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000848}
849
Reid Kleckner722b1df2014-07-18 00:13:16 +0000850bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
851 SourceLocation PragmaLocation) {
852 if (Tok.isNot(tok::l_paren)) {
853 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
854 return false;
855 }
Warren Huntc3b18962014-04-08 22:30:47 +0000856 PP.Lex(Tok); // (
857 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000858 StringRef SlotLabel;
Warren Huntc3b18962014-04-08 22:30:47 +0000859 if (Tok.isAnyIdentifier()) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000860 StringRef PushPop = Tok.getIdentifierInfo()->getName();
Warren Huntc3b18962014-04-08 22:30:47 +0000861 if (PushPop == "push")
862 Action = Sema::PSK_Push;
863 else if (PushPop == "pop")
864 Action = Sema::PSK_Pop;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000865 else {
866 PP.Diag(PragmaLocation,
867 diag::warn_pragma_expected_section_push_pop_or_name)
868 << PragmaName;
869 return false;
870 }
Warren Huntc3b18962014-04-08 22:30:47 +0000871 if (Action != Sema::PSK_Reset) {
872 PP.Lex(Tok); // push | pop
873 if (Tok.is(tok::comma)) {
874 PP.Lex(Tok); // ,
875 // If we've got a comma, we either need a label or a string.
876 if (Tok.isAnyIdentifier()) {
877 SlotLabel = Tok.getIdentifierInfo()->getName();
878 PP.Lex(Tok); // identifier
879 if (Tok.is(tok::comma))
880 PP.Lex(Tok);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000881 else if (Tok.isNot(tok::r_paren)) {
882 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
883 << PragmaName;
884 return false;
885 }
Warren Huntc3b18962014-04-08 22:30:47 +0000886 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000887 } else if (Tok.isNot(tok::r_paren)) {
888 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
889 return false;
890 }
Warren Huntc3b18962014-04-08 22:30:47 +0000891 }
892 }
893 // Grab the string literal for our section name.
894 StringLiteral *SegmentName = nullptr;
895 if (Tok.isNot(tok::r_paren)) {
Reid Kleckner722b1df2014-07-18 00:13:16 +0000896 if (Tok.isNot(tok::string_literal)) {
897 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
Warren Huntc3b18962014-04-08 22:30:47 +0000898 diag::warn_pragma_expected_section_name :
899 diag::warn_pragma_expected_section_label_or_name :
900 diag::warn_pragma_expected_section_push_pop_or_name;
Reid Kleckner722b1df2014-07-18 00:13:16 +0000901 PP.Diag(PragmaLocation, DiagID) << PragmaName;
902 return false;
903 }
904 ExprResult StringResult = ParseStringLiteralExpression();
905 if (StringResult.isInvalid())
906 return false; // Already diagnosed.
907 SegmentName = cast<StringLiteral>(StringResult.get());
908 if (SegmentName->getCharByteWidth() != 1) {
909 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
910 << PragmaName;
911 return false;
912 }
Warren Huntc3b18962014-04-08 22:30:47 +0000913 // Setting section "" has no effect
914 if (SegmentName->getLength())
915 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
916 }
Reid Kleckner722b1df2014-07-18 00:13:16 +0000917 if (Tok.isNot(tok::r_paren)) {
918 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
919 return false;
920 }
Warren Huntc3b18962014-04-08 22:30:47 +0000921 PP.Lex(Tok); // )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000922 if (Tok.isNot(tok::eof)) {
923 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
924 << PragmaName;
925 return false;
926 }
Warren Huntc3b18962014-04-08 22:30:47 +0000927 PP.Lex(Tok); // eof
928 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
929 SegmentName, PragmaName);
Reid Kleckner722b1df2014-07-18 00:13:16 +0000930 return true;
Warren Huntc3b18962014-04-08 22:30:47 +0000931}
932
Reid Kleckner1a711b12014-07-22 00:53:05 +0000933// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
Reid Kleckner722b1df2014-07-18 00:13:16 +0000934bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
935 SourceLocation PragmaLocation) {
David Majnemerad2986e2014-08-14 06:35:08 +0000936 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
937 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
938 return false;
939 }
940
Reid Kleckner1a711b12014-07-22 00:53:05 +0000941 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
942 PragmaName))
943 return false;
944
945 // Parse either the known section names or the string section name.
946 StringLiteral *SegmentName = nullptr;
947 if (Tok.isAnyIdentifier()) {
948 auto *II = Tok.getIdentifierInfo();
949 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
950 .Case("compiler", "\".CRT$XCC\"")
951 .Case("lib", "\".CRT$XCL\"")
952 .Case("user", "\".CRT$XCU\"")
953 .Default("");
954
955 if (!Section.empty()) {
956 // Pretend the user wrote the appropriate string literal here.
957 Token Toks[1];
958 Toks[0].startToken();
959 Toks[0].setKind(tok::string_literal);
960 Toks[0].setLocation(Tok.getLocation());
961 Toks[0].setLiteralData(Section.data());
962 Toks[0].setLength(Section.size());
963 SegmentName =
964 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
965 PP.Lex(Tok);
966 }
967 } else if (Tok.is(tok::string_literal)) {
968 ExprResult StringResult = ParseStringLiteralExpression();
969 if (StringResult.isInvalid())
970 return false;
971 SegmentName = cast<StringLiteral>(StringResult.get());
972 if (SegmentName->getCharByteWidth() != 1) {
973 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
974 << PragmaName;
975 return false;
976 }
977 // FIXME: Add support for the '[, func-name]' part of the pragma.
978 }
979
980 if (!SegmentName) {
981 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
982 return false;
983 }
984
985 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
986 PragmaName) ||
987 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
988 PragmaName))
989 return false;
990
991 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
992 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +0000993}
994
Benjamin Kramere003ca22015-10-28 13:54:16 +0000995namespace {
Eli Bendersky06a40422014-06-06 20:31:48 +0000996struct PragmaLoopHintInfo {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +0000997 Token PragmaName;
Eli Bendersky06a40422014-06-06 20:31:48 +0000998 Token Option;
Benjamin Kramerfa7f8552015-08-05 09:39:57 +0000999 ArrayRef<Token> Toks;
Eli Bendersky06a40422014-06-06 20:31:48 +00001000};
Benjamin Kramere003ca22015-10-28 13:54:16 +00001001} // end anonymous namespace
Eli Bendersky06a40422014-06-06 20:31:48 +00001002
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001003static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
1004 std::string PragmaString;
1005 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1006 PragmaString = "clang loop ";
1007 PragmaString += Option.getIdentifierInfo()->getName();
David Greenc8e39242018-08-01 14:36:12 +00001008 } else if (PragmaName.getIdentifierInfo()->getName() == "unroll_and_jam") {
1009 PragmaString = "unroll_and_jam";
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001010 } else {
1011 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1012 "Unexpected pragma name");
1013 PragmaString = "unroll";
1014 }
1015 return PragmaString;
1016}
1017
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001018bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
Eli Bendersky06a40422014-06-06 20:31:48 +00001019 assert(Tok.is(tok::annot_pragma_loop_hint));
1020 PragmaLoopHintInfo *Info =
1021 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
1022
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001023 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
1024 Hint.PragmaNameLoc = IdentifierLoc::create(
1025 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
Eli Bendersky06a40422014-06-06 20:31:48 +00001026
Aaron Ballmanef940aa2014-07-31 21:24:32 +00001027 // It is possible that the loop hint has no option identifier, such as
1028 // #pragma unroll(4).
1029 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
1030 ? Info->Option.getIdentifierInfo()
1031 : nullptr;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001032 Hint.OptionLoc = IdentifierLoc::create(
1033 Actions.Context, Info->Option.getLocation(), OptionInfo);
1034
David Blaikie2eabcc92016-02-09 18:52:09 +00001035 llvm::ArrayRef<Token> Toks = Info->Toks;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001036
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001037 // Return a valid hint if pragma unroll or nounroll were specified
1038 // without an argument.
1039 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
1040 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
David Greenc8e39242018-08-01 14:36:12 +00001041 bool PragmaUnrollAndJam = PragmaNameInfo->getName() == "unroll_and_jam";
1042 bool PragmaNoUnrollAndJam = PragmaNameInfo->getName() == "nounroll_and_jam";
1043 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll || PragmaUnrollAndJam ||
1044 PragmaNoUnrollAndJam)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001045 ConsumeAnnotationToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001046 Hint.Range = Info->PragmaName.getLocation();
1047 return true;
1048 }
1049
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001050 // The constant expression is always followed by an eof token, which increases
1051 // the TokSize by 1.
David Blaikie2eabcc92016-02-09 18:52:09 +00001052 assert(!Toks.empty() &&
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001053 "PragmaLoopHintInfo::Toks must contain at least one token.");
1054
1055 // If no option is specified the argument is assumed to be a constant expr.
Tyler Nowicki24853c12015-06-08 23:13:43 +00001056 bool OptionUnroll = false;
David Greenc8e39242018-08-01 14:36:12 +00001057 bool OptionUnrollAndJam = false;
Adam Nemet2de463e2016-06-14 12:04:26 +00001058 bool OptionDistribute = false;
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001059 bool OptionPipelineDisabled = false;
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001060 bool StateOption = false;
Tyler Nowicki24853c12015-06-08 23:13:43 +00001061 if (OptionInfo) { // Pragma Unroll does not specify an option.
1062 OptionUnroll = OptionInfo->isStr("unroll");
David Greenc8e39242018-08-01 14:36:12 +00001063 OptionUnrollAndJam = OptionInfo->isStr("unroll_and_jam");
Adam Nemet2de463e2016-06-14 12:04:26 +00001064 OptionDistribute = OptionInfo->isStr("distribute");
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001065 OptionPipelineDisabled = OptionInfo->isStr("pipeline");
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001066 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
1067 .Case("vectorize", true)
1068 .Case("interleave", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00001069 .Default(false) ||
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001070 OptionUnroll || OptionUnrollAndJam || OptionDistribute ||
1071 OptionPipelineDisabled;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001072 }
1073
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001074 bool AssumeSafetyArg = !OptionUnroll && !OptionUnrollAndJam &&
1075 !OptionDistribute && !OptionPipelineDisabled;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001076 // Verify loop hint has an argument.
1077 if (Toks[0].is(tok::eof)) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001078 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001079 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
David Greenc8e39242018-08-01 14:36:12 +00001080 << /*StateArgument=*/StateOption
1081 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
Adam Nemet2de463e2016-06-14 12:04:26 +00001082 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001083 return false;
1084 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001085
1086 // Validate the argument.
1087 if (StateOption) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001088 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001089 SourceLocation StateLoc = Toks[0].getLocation();
1090 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
Adam Nemet50de4e82016-04-19 22:17:45 +00001091
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001092 bool Valid = StateInfo &&
1093 llvm::StringSwitch<bool>(StateInfo->getName())
1094 .Case("disable", true)
1095 .Case("enable", !OptionPipelineDisabled)
1096 .Case("full", OptionUnroll || OptionUnrollAndJam)
1097 .Case("assume_safety", AssumeSafetyArg)
1098 .Default(false);
Adam Nemet50de4e82016-04-19 22:17:45 +00001099 if (!Valid) {
Aaron Ballman9bdf5152019-01-04 17:20:00 +00001100 if (OptionPipelineDisabled) {
1101 Diag(Toks[0].getLocation(), diag::err_pragma_pipeline_invalid_keyword);
1102 } else {
1103 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
1104 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
1105 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
1106 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001107 return false;
1108 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001109 if (Toks.size() > 2)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001110 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1111 << PragmaLoopHintString(Info->PragmaName, Info->Option);
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001112 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
1113 } else {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001114 // Enter constant expression including eof terminator into token stream.
David Blaikie2eabcc92016-02-09 18:52:09 +00001115 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001116 ConsumeAnnotationToken();
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001117
1118 ExprResult R = ParseConstantExpression();
1119
1120 // Tokens following an error in an ill-formed constant expression will
1121 // remain in the token stream and must be removed.
1122 if (Tok.isNot(tok::eof)) {
1123 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1124 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1125 while (Tok.isNot(tok::eof))
1126 ConsumeAnyToken();
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001127 }
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001128
1129 ConsumeToken(); // Consume the constant expression eof terminator.
1130
1131 if (R.isInvalid() ||
1132 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
1133 return false;
1134
1135 // Argument is a constant expression with an integer type.
1136 Hint.ValueExpr = R.get();
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00001137 }
Eli Bendersky06a40422014-06-06 20:31:48 +00001138
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001139 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
David Blaikie2eabcc92016-02-09 18:52:09 +00001140 Info->Toks.back().getLocation());
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00001141 return true;
Eli Bendersky06a40422014-06-06 20:31:48 +00001142}
1143
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001144namespace {
1145struct PragmaAttributeInfo {
Erik Pilkington7d180942018-10-29 17:38:42 +00001146 enum ActionType { Push, Pop, Attribute };
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001147 ParsedAttributes &Attributes;
1148 ActionType Action;
Erik Pilkington0876cae2018-12-20 22:32:04 +00001149 const IdentifierInfo *Namespace = nullptr;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001150 ArrayRef<Token> Tokens;
1151
1152 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1153};
1154
1155#include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1156
1157} // end anonymous namespace
1158
1159static StringRef getIdentifier(const Token &Tok) {
1160 if (Tok.is(tok::identifier))
1161 return Tok.getIdentifierInfo()->getName();
1162 const char *S = tok::getKeywordSpelling(Tok.getKind());
1163 if (!S)
1164 return "";
1165 return S;
1166}
1167
1168static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1169 using namespace attr;
1170 switch (Rule) {
1171#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1172 case Value: \
1173 return IsAbstract;
1174#include "clang/Basic/AttrSubMatchRulesList.inc"
1175 }
1176 llvm_unreachable("Invalid attribute subject match rule");
1177 return false;
1178}
1179
1180static void diagnoseExpectedAttributeSubjectSubRule(
1181 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1182 SourceLocation SubRuleLoc) {
1183 auto Diagnostic =
1184 PRef.Diag(SubRuleLoc,
1185 diag::err_pragma_attribute_expected_subject_sub_identifier)
1186 << PrimaryRuleName;
1187 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1188 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1189 else
1190 Diagnostic << /*SubRulesSupported=*/0;
1191}
1192
1193static void diagnoseUnknownAttributeSubjectSubRule(
1194 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1195 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1196
1197 auto Diagnostic =
1198 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1199 << SubRuleName << PrimaryRuleName;
1200 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1201 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1202 else
1203 Diagnostic << /*SubRulesSupported=*/0;
1204}
1205
1206bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1207 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1208 SourceLocation &LastMatchRuleEndLoc) {
1209 bool IsAny = false;
1210 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1211 if (getIdentifier(Tok) == "any") {
1212 AnyLoc = ConsumeToken();
1213 IsAny = true;
1214 if (AnyParens.expectAndConsume())
1215 return true;
1216 }
1217
1218 do {
1219 // Parse the subject matcher rule.
1220 StringRef Name = getIdentifier(Tok);
1221 if (Name.empty()) {
1222 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1223 return true;
1224 }
1225 std::pair<Optional<attr::SubjectMatchRule>,
1226 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1227 Rule = isAttributeSubjectMatchRule(Name);
1228 if (!Rule.first) {
1229 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1230 return true;
1231 }
1232 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1233 SourceLocation RuleLoc = ConsumeToken();
1234
1235 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1236 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1237 if (Parens.expectAndConsume())
1238 return true;
1239 } else if (Parens.consumeOpen()) {
1240 if (!SubjectMatchRules
1241 .insert(
1242 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1243 .second)
1244 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1245 << Name
1246 << FixItHint::CreateRemoval(SourceRange(
1247 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1248 LastMatchRuleEndLoc = RuleLoc;
1249 continue;
1250 }
1251
1252 // Parse the sub-rules.
1253 StringRef SubRuleName = getIdentifier(Tok);
1254 if (SubRuleName.empty()) {
1255 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1256 Tok.getLocation());
1257 return true;
1258 }
1259 attr::SubjectMatchRule SubRule;
1260 if (SubRuleName == "unless") {
1261 SourceLocation SubRuleLoc = ConsumeToken();
1262 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1263 if (Parens.expectAndConsume())
1264 return true;
1265 SubRuleName = getIdentifier(Tok);
1266 if (SubRuleName.empty()) {
1267 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1268 SubRuleLoc);
1269 return true;
1270 }
1271 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1272 if (!SubRuleOrNone) {
1273 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1274 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1275 SubRuleUnlessName, SubRuleLoc);
1276 return true;
1277 }
1278 SubRule = *SubRuleOrNone;
1279 ConsumeToken();
1280 if (Parens.consumeClose())
1281 return true;
1282 } else {
1283 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1284 if (!SubRuleOrNone) {
1285 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1286 SubRuleName, Tok.getLocation());
1287 return true;
1288 }
1289 SubRule = *SubRuleOrNone;
1290 ConsumeToken();
1291 }
1292 SourceLocation RuleEndLoc = Tok.getLocation();
1293 LastMatchRuleEndLoc = RuleEndLoc;
1294 if (Parens.consumeClose())
1295 return true;
1296 if (!SubjectMatchRules
1297 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1298 .second) {
1299 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1300 << attr::getSubjectMatchRuleSpelling(SubRule)
1301 << FixItHint::CreateRemoval(SourceRange(
1302 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1303 continue;
1304 }
1305 } while (IsAny && TryConsumeToken(tok::comma));
1306
1307 if (IsAny)
1308 if (AnyParens.consumeClose())
1309 return true;
1310
1311 return false;
1312}
1313
1314namespace {
1315
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001316/// Describes the stage at which attribute subject rule parsing was interrupted.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001317enum class MissingAttributeSubjectRulesRecoveryPoint {
1318 Comma,
1319 ApplyTo,
1320 Equals,
1321 Any,
1322 None,
1323};
1324
1325MissingAttributeSubjectRulesRecoveryPoint
1326getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1327 if (const auto *II = Tok.getIdentifierInfo()) {
1328 if (II->isStr("apply_to"))
1329 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1330 if (II->isStr("any"))
1331 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1332 }
1333 if (Tok.is(tok::equal))
1334 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1335 return MissingAttributeSubjectRulesRecoveryPoint::None;
1336}
1337
1338/// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1339/// suggests the possible attribute subject rules in a fix-it together with
1340/// any other missing tokens.
1341DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
Erich Keanee891aa92018-07-13 15:07:47 +00001342 unsigned DiagID, ParsedAttr &Attribute,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001343 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1344 SourceLocation Loc = PRef.getEndOfPreviousToken();
1345 if (Loc.isInvalid())
1346 Loc = PRef.getCurToken().getLocation();
1347 auto Diagnostic = PRef.Diag(Loc, DiagID);
1348 std::string FixIt;
1349 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1350 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1351 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1352 FixIt = ", ";
1353 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1354 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1355 FixIt += "apply_to";
1356 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1357 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1358 FixIt += " = ";
1359 SourceRange FixItRange(Loc);
1360 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1361 // Gather the subject match rules that are supported by the attribute.
1362 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1363 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1364 if (SubjectMatchRuleSet.empty()) {
1365 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1366 // placeholders will be supported by the fix-its.
1367 return Diagnostic;
1368 }
1369 FixIt += "any(";
1370 bool NeedsComma = false;
1371 for (const auto &I : SubjectMatchRuleSet) {
1372 // Ensure that the missing rule is reported in the fix-it only when it's
1373 // supported in the current language mode.
1374 if (!I.second)
1375 continue;
1376 if (NeedsComma)
1377 FixIt += ", ";
1378 else
1379 NeedsComma = true;
1380 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1381 }
1382 FixIt += ")";
1383 // Check if we need to remove the range
1384 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1385 FixItRange.setEnd(PRef.getCurToken().getLocation());
1386 }
1387 if (FixItRange.getBegin() == FixItRange.getEnd())
1388 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1389 else
1390 Diagnostic << FixItHint::CreateReplacement(
1391 CharSourceRange::getCharRange(FixItRange), FixIt);
1392 return Diagnostic;
1393}
1394
1395} // end anonymous namespace
1396
1397void Parser::HandlePragmaAttribute() {
1398 assert(Tok.is(tok::annot_pragma_attribute) &&
1399 "Expected #pragma attribute annotation token");
1400 SourceLocation PragmaLoc = Tok.getLocation();
1401 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1402 if (Info->Action == PragmaAttributeInfo::Pop) {
Richard Smithaf3b3252017-05-18 19:21:48 +00001403 ConsumeAnnotationToken();
Erik Pilkington0876cae2018-12-20 22:32:04 +00001404 Actions.ActOnPragmaAttributePop(PragmaLoc, Info->Namespace);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001405 return;
1406 }
1407 // Parse the actual attribute with its arguments.
Erik Pilkington7d180942018-10-29 17:38:42 +00001408 assert((Info->Action == PragmaAttributeInfo::Push ||
1409 Info->Action == PragmaAttributeInfo::Attribute) &&
Erik Pilkingtonb287a012018-10-29 03:24:16 +00001410 "Unexpected #pragma attribute command");
Erik Pilkington7d180942018-10-29 17:38:42 +00001411
1412 if (Info->Action == PragmaAttributeInfo::Push && Info->Tokens.empty()) {
1413 ConsumeAnnotationToken();
Erik Pilkington0876cae2018-12-20 22:32:04 +00001414 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace);
Erik Pilkington7d180942018-10-29 17:38:42 +00001415 return;
1416 }
1417
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001418 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false);
Richard Smithaf3b3252017-05-18 19:21:48 +00001419 ConsumeAnnotationToken();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001420
1421 ParsedAttributes &Attrs = Info->Attributes;
1422 Attrs.clearListOnly();
1423
1424 auto SkipToEnd = [this]() {
1425 SkipUntil(tok::eof, StopBeforeMatch);
1426 ConsumeToken();
1427 };
1428
1429 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1430 // Parse the CXX11 style attribute.
1431 ParseCXX11AttributeSpecifier(Attrs);
1432 } else if (Tok.is(tok::kw___attribute)) {
1433 ConsumeToken();
1434 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1435 "attribute"))
1436 return SkipToEnd();
1437 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1438 return SkipToEnd();
1439
1440 if (Tok.isNot(tok::identifier)) {
1441 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1442 SkipToEnd();
1443 return;
1444 }
1445 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1446 SourceLocation AttrNameLoc = ConsumeToken();
1447
1448 if (Tok.isNot(tok::l_paren))
1449 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Erich Keanee891aa92018-07-13 15:07:47 +00001450 ParsedAttr::AS_GNU);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001451 else
1452 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1453 /*ScopeName=*/nullptr,
Erich Keanee891aa92018-07-13 15:07:47 +00001454 /*ScopeLoc=*/SourceLocation(), ParsedAttr::AS_GNU,
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001455 /*Declarator=*/nullptr);
1456
1457 if (ExpectAndConsume(tok::r_paren))
1458 return SkipToEnd();
1459 if (ExpectAndConsume(tok::r_paren))
1460 return SkipToEnd();
1461 } else if (Tok.is(tok::kw___declspec)) {
1462 ParseMicrosoftDeclSpecs(Attrs);
1463 } else {
1464 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1465 if (Tok.getIdentifierInfo()) {
1466 // If we suspect that this is an attribute suggest the use of
1467 // '__attribute__'.
Erich Keanee891aa92018-07-13 15:07:47 +00001468 if (ParsedAttr::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1469 ParsedAttr::AS_GNU) !=
1470 ParsedAttr::UnknownAttribute) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001471 SourceLocation InsertStartLoc = Tok.getLocation();
1472 ConsumeToken();
1473 if (Tok.is(tok::l_paren)) {
1474 ConsumeAnyToken();
1475 SkipUntil(tok::r_paren, StopBeforeMatch);
1476 if (Tok.isNot(tok::r_paren))
1477 return SkipToEnd();
1478 }
1479 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1480 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1481 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1482 }
1483 }
1484 SkipToEnd();
1485 return;
1486 }
1487
Erich Keanec480f302018-07-12 21:09:05 +00001488 if (Attrs.empty() || Attrs.begin()->isInvalid()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001489 SkipToEnd();
1490 return;
1491 }
1492
1493 // Ensure that we don't have more than one attribute.
Erich Keanec480f302018-07-12 21:09:05 +00001494 if (Attrs.size() > 1) {
1495 SourceLocation Loc = Attrs[1].getLoc();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001496 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1497 SkipToEnd();
1498 return;
1499 }
1500
Erich Keanee891aa92018-07-13 15:07:47 +00001501 ParsedAttr &Attribute = *Attrs.begin();
Erich Keanec480f302018-07-12 21:09:05 +00001502 if (!Attribute.isSupportedByPragmaAttribute()) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001503 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
Erich Keanec480f302018-07-12 21:09:05 +00001504 << Attribute.getName();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001505 SkipToEnd();
1506 return;
1507 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001508
1509 // Parse the subject-list.
1510 if (!TryConsumeToken(tok::comma)) {
1511 createExpectedAttributeSubjectRulesTokenDiagnostic(
1512 diag::err_expected, Attribute,
1513 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1514 << tok::comma;
1515 SkipToEnd();
1516 return;
1517 }
1518
1519 if (Tok.isNot(tok::identifier)) {
1520 createExpectedAttributeSubjectRulesTokenDiagnostic(
1521 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1522 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1523 SkipToEnd();
1524 return;
1525 }
1526 const IdentifierInfo *II = Tok.getIdentifierInfo();
1527 if (!II->isStr("apply_to")) {
1528 createExpectedAttributeSubjectRulesTokenDiagnostic(
1529 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1530 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1531 SkipToEnd();
1532 return;
1533 }
1534 ConsumeToken();
1535
1536 if (!TryConsumeToken(tok::equal)) {
1537 createExpectedAttributeSubjectRulesTokenDiagnostic(
1538 diag::err_expected, Attribute,
1539 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1540 << tok::equal;
1541 SkipToEnd();
1542 return;
1543 }
1544
1545 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1546 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1547 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1548 LastMatchRuleEndLoc)) {
1549 SkipToEnd();
1550 return;
1551 }
1552
1553 // Tokens following an ill-formed attribute will remain in the token stream
1554 // and must be removed.
1555 if (Tok.isNot(tok::eof)) {
1556 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1557 SkipToEnd();
1558 return;
1559 }
1560
1561 // Consume the eof terminator token.
1562 ConsumeToken();
1563
Erik Pilkington7d180942018-10-29 17:38:42 +00001564 // Handle a mixed push/attribute by desurging to a push, then an attribute.
1565 if (Info->Action == PragmaAttributeInfo::Push)
Erik Pilkington0876cae2018-12-20 22:32:04 +00001566 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace);
Erik Pilkington7d180942018-10-29 17:38:42 +00001567
1568 Actions.ActOnPragmaAttributeAttribute(Attribute, PragmaLoc,
1569 std::move(SubjectMatchRules));
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001570}
1571
Eli Bendersky06a40422014-06-06 20:31:48 +00001572// #pragma GCC visibility comes in two variants:
1573// 'push' '(' [visibility] ')'
1574// 'pop'
Fangrui Song6907ce22018-07-30 19:24:48 +00001575void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001576 PragmaIntroducerKind Introducer,
1577 Token &VisTok) {
Eli Friedman570024a2010-08-05 06:57:20 +00001578 SourceLocation VisLoc = VisTok.getLocation();
1579
1580 Token Tok;
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001581 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001582
1583 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1584
Eli Friedman570024a2010-08-05 06:57:20 +00001585 const IdentifierInfo *VisType;
1586 if (PushPop && PushPop->isStr("pop")) {
Craig Topper161e4db2014-05-21 06:02:52 +00001587 VisType = nullptr;
Eli Friedman570024a2010-08-05 06:57:20 +00001588 } else if (PushPop && PushPop->isStr("push")) {
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001589 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001590 if (Tok.isNot(tok::l_paren)) {
1591 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1592 << "visibility";
1593 return;
1594 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001595 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001596 VisType = Tok.getIdentifierInfo();
1597 if (!VisType) {
1598 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1599 << "visibility";
1600 return;
1601 }
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001602 PP.LexUnexpandedToken(Tok);
Eli Friedman570024a2010-08-05 06:57:20 +00001603 if (Tok.isNot(tok::r_paren)) {
1604 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1605 << "visibility";
1606 return;
1607 }
1608 } else {
1609 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1610 << "visibility";
1611 return;
1612 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001613 SourceLocation EndLoc = Tok.getLocation();
Joerg Sonnenberger869f0b72011-07-20 01:03:50 +00001614 PP.LexUnexpandedToken(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001615 if (Tok.isNot(tok::eod)) {
Eli Friedman570024a2010-08-05 06:57:20 +00001616 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1617 << "visibility";
1618 return;
1619 }
1620
David Blaikie2eabcc92016-02-09 18:52:09 +00001621 auto Toks = llvm::make_unique<Token[]>(1);
Rafael Espindola273fd772012-01-26 02:02:57 +00001622 Toks[0].startToken();
1623 Toks[0].setKind(tok::annot_pragma_vis);
1624 Toks[0].setLocation(VisLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001625 Toks[0].setAnnotationEndLoc(EndLoc);
Rafael Espindola273fd772012-01-26 02:02:57 +00001626 Toks[0].setAnnotationValue(
1627 const_cast<void*>(static_cast<const void*>(VisType)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001628 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
Eli Friedman570024a2010-08-05 06:57:20 +00001629}
1630
Daniel Dunbar921b9682008-10-04 19:21:03 +00001631// #pragma pack(...) comes in the following delicious flavors:
1632// pack '(' [integer] ')'
1633// pack '(' 'show' ')'
1634// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
Fangrui Song6907ce22018-07-30 19:24:48 +00001635void PragmaPackHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001636 PragmaIntroducerKind Introducer,
1637 Token &PackTok) {
Daniel Dunbar921b9682008-10-04 19:21:03 +00001638 SourceLocation PackLoc = PackTok.getLocation();
1639
1640 Token Tok;
1641 PP.Lex(Tok);
1642 if (Tok.isNot(tok::l_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001643 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001644 return;
1645 }
1646
Denis Zobnin10c4f452016-04-29 18:17:40 +00001647 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1648 StringRef SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001649 Token Alignment;
1650 Alignment.startToken();
Mike Stump11289f42009-09-09 15:08:12 +00001651 PP.Lex(Tok);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001652 if (Tok.is(tok::numeric_constant)) {
Eli Friedman68be1642012-10-04 02:36:51 +00001653 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001654
1655 PP.Lex(Tok);
Eli Friedman055c9702011-11-02 01:53:16 +00001656
1657 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1658 // the push/pop stack.
1659 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
Denis Zobnin10c4f452016-04-29 18:17:40 +00001660 Action =
1661 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001662 } else if (Tok.is(tok::identifier)) {
1663 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattnere3d20d92008-11-23 21:45:46 +00001664 if (II->isStr("show")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001665 Action = Sema::PSK_Show;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001666 PP.Lex(Tok);
1667 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001668 if (II->isStr("push")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001669 Action = Sema::PSK_Push;
Chris Lattnere3d20d92008-11-23 21:45:46 +00001670 } else if (II->isStr("pop")) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001671 Action = Sema::PSK_Pop;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001672 } else {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00001673 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001674 return;
Mike Stump11289f42009-09-09 15:08:12 +00001675 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001676 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001677
Daniel Dunbar921b9682008-10-04 19:21:03 +00001678 if (Tok.is(tok::comma)) {
1679 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001680
Daniel Dunbar921b9682008-10-04 19:21:03 +00001681 if (Tok.is(tok::numeric_constant)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001682 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001683 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001684
1685 PP.Lex(Tok);
1686 } else if (Tok.is(tok::identifier)) {
Denis Zobnin10c4f452016-04-29 18:17:40 +00001687 SlotLabel = Tok.getIdentifierInfo()->getName();
Daniel Dunbar921b9682008-10-04 19:21:03 +00001688 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001689
Daniel Dunbar921b9682008-10-04 19:21:03 +00001690 if (Tok.is(tok::comma)) {
1691 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001692
Daniel Dunbar921b9682008-10-04 19:21:03 +00001693 if (Tok.isNot(tok::numeric_constant)) {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001694 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001695 return;
1696 }
Mike Stump11289f42009-09-09 15:08:12 +00001697
Denis Zobnin10c4f452016-04-29 18:17:40 +00001698 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
Eli Friedman68be1642012-10-04 02:36:51 +00001699 Alignment = Tok;
Daniel Dunbar921b9682008-10-04 19:21:03 +00001700
1701 PP.Lex(Tok);
1702 }
1703 } else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001704 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001705 return;
1706 }
1707 }
1708 }
David Blaikiebbafb8a2012-03-11 07:00:24 +00001709 } else if (PP.getLangOpts().ApplePragmaPack) {
Eli Friedman055c9702011-11-02 01:53:16 +00001710 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1711 // the push/pop stack.
1712 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
Denis Zobnin10c4f452016-04-29 18:17:40 +00001713 Action = Sema::PSK_Pop;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001714 }
Daniel Dunbar921b9682008-10-04 19:21:03 +00001715
1716 if (Tok.isNot(tok::r_paren)) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001717 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
Daniel Dunbar921b9682008-10-04 19:21:03 +00001718 return;
1719 }
1720
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001721 SourceLocation RParenLoc = Tok.getLocation();
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001722 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001723 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001724 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1725 return;
1726 }
1727
David Blaikie2eabcc92016-02-09 18:52:09 +00001728 PragmaPackInfo *Info =
1729 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
Denis Zobnin10c4f452016-04-29 18:17:40 +00001730 Info->Action = Action;
1731 Info->SlotLabel = SlotLabel;
Eli Friedman68be1642012-10-04 02:36:51 +00001732 Info->Alignment = Alignment;
Eli Friedmanec52f922012-02-23 23:47:16 +00001733
David Blaikie2eabcc92016-02-09 18:52:09 +00001734 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1735 1);
Eli Friedmanec52f922012-02-23 23:47:16 +00001736 Toks[0].startToken();
1737 Toks[0].setKind(tok::annot_pragma_pack);
1738 Toks[0].setLocation(PackLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00001739 Toks[0].setAnnotationEndLoc(RParenLoc);
Eli Friedmanec52f922012-02-23 23:47:16 +00001740 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00001741 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbar921b9682008-10-04 19:21:03 +00001742}
1743
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001744// #pragma ms_struct on
1745// #pragma ms_struct off
Fangrui Song6907ce22018-07-30 19:24:48 +00001746void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001747 PragmaIntroducerKind Introducer,
1748 Token &MSStructTok) {
Nico Weber779355f2016-03-02 23:22:00 +00001749 PragmaMSStructKind Kind = PMSST_OFF;
1750
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001751 Token Tok;
1752 PP.Lex(Tok);
1753 if (Tok.isNot(tok::identifier)) {
1754 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1755 return;
1756 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00001757 SourceLocation EndLoc = Tok.getLocation();
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001758 const IdentifierInfo *II = Tok.getIdentifierInfo();
1759 if (II->isStr("on")) {
Nico Weber779355f2016-03-02 23:22:00 +00001760 Kind = PMSST_ON;
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001761 PP.Lex(Tok);
1762 }
1763 else if (II->isStr("off") || II->isStr("reset"))
1764 PP.Lex(Tok);
1765 else {
1766 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1767 return;
1768 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001769
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001770 if (Tok.isNot(tok::eod)) {
Daniel Dunbar340cf242012-02-29 01:38:22 +00001771 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1772 << "ms_struct";
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001773 return;
1774 }
Eli Friedman68be1642012-10-04 02:36:51 +00001775
David Blaikie2eabcc92016-02-09 18:52:09 +00001776 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1777 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001778 Toks[0].startToken();
1779 Toks[0].setKind(tok::annot_pragma_msstruct);
1780 Toks[0].setLocation(MSStructTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001781 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001782 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1783 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001784 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Fariborz Jahanian743dda42011-04-25 18:49:15 +00001785}
1786
Javed Absar2a67c9e2017-06-05 10:11:57 +00001787// #pragma clang section bss="abc" data="" rodata="def" text=""
1788void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
1789 PragmaIntroducerKind Introducer, Token &FirstToken) {
1790
1791 Token Tok;
1792 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1793
1794 PP.Lex(Tok); // eat 'section'
1795 while (Tok.isNot(tok::eod)) {
1796 if (Tok.isNot(tok::identifier)) {
1797 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1798 return;
1799 }
1800
1801 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1802 if (SecType->isStr("bss"))
1803 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1804 else if (SecType->isStr("data"))
1805 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1806 else if (SecType->isStr("rodata"))
1807 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1808 else if (SecType->isStr("text"))
1809 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1810 else {
1811 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1812 return;
1813 }
1814
1815 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1816 if (Tok.isNot(tok::equal)) {
1817 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1818 return;
1819 }
1820
1821 std::string SecName;
1822 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1823 return;
1824
1825 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1826 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1827 Sema::PragmaClangSectionAction::PCSA_Clear),
1828 SecKind, SecName);
1829 }
1830}
1831
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001832// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1833// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
Eli Friedman68be1642012-10-04 02:36:51 +00001834static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001835 bool IsOptions) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001836 Token Tok;
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001837
1838 if (IsOptions) {
1839 PP.Lex(Tok);
1840 if (Tok.isNot(tok::identifier) ||
1841 !Tok.getIdentifierInfo()->isStr("align")) {
1842 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1843 return;
1844 }
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001845 }
Daniel Dunbar663e8092010-05-27 18:42:09 +00001846
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001847 PP.Lex(Tok);
1848 if (Tok.isNot(tok::equal)) {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001849 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1850 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001851 return;
1852 }
1853
1854 PP.Lex(Tok);
1855 if (Tok.isNot(tok::identifier)) {
1856 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001857 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001858 return;
1859 }
1860
John McCallfaf5fb42010-08-26 23:41:50 +00001861 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001862 const IdentifierInfo *II = Tok.getIdentifierInfo();
Daniel Dunbar663e8092010-05-27 18:42:09 +00001863 if (II->isStr("native"))
John McCallfaf5fb42010-08-26 23:41:50 +00001864 Kind = Sema::POAK_Native;
Daniel Dunbar663e8092010-05-27 18:42:09 +00001865 else if (II->isStr("natural"))
John McCallfaf5fb42010-08-26 23:41:50 +00001866 Kind = Sema::POAK_Natural;
Daniel Dunbar9c84d4a2010-05-27 18:42:17 +00001867 else if (II->isStr("packed"))
John McCallfaf5fb42010-08-26 23:41:50 +00001868 Kind = Sema::POAK_Packed;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001869 else if (II->isStr("power"))
John McCallfaf5fb42010-08-26 23:41:50 +00001870 Kind = Sema::POAK_Power;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001871 else if (II->isStr("mac68k"))
John McCallfaf5fb42010-08-26 23:41:50 +00001872 Kind = Sema::POAK_Mac68k;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001873 else if (II->isStr("reset"))
John McCallfaf5fb42010-08-26 23:41:50 +00001874 Kind = Sema::POAK_Reset;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001875 else {
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001876 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1877 << IsOptions;
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001878 return;
1879 }
1880
David Majnemera8f2f1d2015-03-19 00:10:23 +00001881 SourceLocation EndLoc = Tok.getLocation();
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001882 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001883 if (Tok.isNot(tok::eod)) {
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001884 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001885 << (IsOptions ? "options" : "align");
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001886 return;
1887 }
1888
David Blaikie2eabcc92016-02-09 18:52:09 +00001889 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1890 1);
Eli Friedman68be1642012-10-04 02:36:51 +00001891 Toks[0].startToken();
1892 Toks[0].setKind(tok::annot_pragma_align);
1893 Toks[0].setLocation(FirstTok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00001894 Toks[0].setAnnotationEndLoc(EndLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00001895 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1896 static_cast<uintptr_t>(Kind)));
David Blaikie2eabcc92016-02-09 18:52:09 +00001897 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001898}
1899
Fangrui Song6907ce22018-07-30 19:24:48 +00001900void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001901 PragmaIntroducerKind Introducer,
1902 Token &AlignTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001903 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
Daniel Dunbarcb82acb2010-07-31 19:17:07 +00001904}
1905
Fangrui Song6907ce22018-07-30 19:24:48 +00001906void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001907 PragmaIntroducerKind Introducer,
1908 Token &OptionsTok) {
Eli Friedman68be1642012-10-04 02:36:51 +00001909 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
Daniel Dunbar75c9be72010-05-26 23:29:06 +00001910}
1911
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001912// #pragma unused(identifier)
Fangrui Song6907ce22018-07-30 19:24:48 +00001913void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001914 PragmaIntroducerKind Introducer,
1915 Token &UnusedTok) {
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001916 // FIXME: Should we be expanding macros here? My guess is no.
1917 SourceLocation UnusedLoc = UnusedTok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001918
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001919 // Lex the left '('.
1920 Token Tok;
1921 PP.Lex(Tok);
1922 if (Tok.isNot(tok::l_paren)) {
1923 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1924 return;
1925 }
Mike Stump11289f42009-09-09 15:08:12 +00001926
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001927 // Lex the declaration reference(s).
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001928 SmallVector<Token, 5> Identifiers;
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001929 SourceLocation RParenLoc;
1930 bool LexID = true;
Mike Stump11289f42009-09-09 15:08:12 +00001931
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001932 while (true) {
1933 PP.Lex(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001934
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001935 if (LexID) {
Mike Stump11289f42009-09-09 15:08:12 +00001936 if (Tok.is(tok::identifier)) {
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001937 Identifiers.push_back(Tok);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001938 LexID = false;
1939 continue;
1940 }
1941
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001942 // Illegal token!
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001943 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1944 return;
1945 }
Mike Stump11289f42009-09-09 15:08:12 +00001946
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001947 // We are execting a ')' or a ','.
1948 if (Tok.is(tok::comma)) {
1949 LexID = true;
1950 continue;
1951 }
Mike Stump11289f42009-09-09 15:08:12 +00001952
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001953 if (Tok.is(tok::r_paren)) {
1954 RParenLoc = Tok.getLocation();
1955 break;
1956 }
Mike Stump11289f42009-09-09 15:08:12 +00001957
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001958 // Illegal token!
David Majnemer88969812014-02-10 19:06:37 +00001959 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001960 return;
1961 }
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001962
1963 PP.Lex(Tok);
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00001964 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001965 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1966 "unused";
1967 return;
1968 }
1969
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001970 // Verify that we have a location for the right parenthesis.
1971 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
Ted Kremenekfb50bf52009-08-03 23:24:57 +00001972 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001973
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001974 // For each identifier token, insert into the token stream a
1975 // annot_pragma_unused token followed by the identifier token.
1976 // This allows us to cache a "#pragma unused" that occurs inside an inline
1977 // C++ member function.
1978
David Blaikie2eabcc92016-02-09 18:52:09 +00001979 MutableArrayRef<Token> Toks(
1980 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1981 2 * Identifiers.size());
Argyrios Kyrtzidisee569622011-01-17 18:58:44 +00001982 for (unsigned i=0; i != Identifiers.size(); i++) {
1983 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1984 pragmaUnusedTok.startToken();
1985 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1986 pragmaUnusedTok.setLocation(UnusedLoc);
1987 idTok = Identifiers[i];
1988 }
David Blaikie2eabcc92016-02-09 18:52:09 +00001989 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Ted Kremenekfd14fad2009-03-23 22:28:25 +00001990}
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001991
1992// #pragma weak identifier
1993// #pragma weak identifier '=' identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00001994void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
Douglas Gregorc7d65762010-09-09 22:45:38 +00001995 PragmaIntroducerKind Introducer,
1996 Token &WeakTok) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00001997 SourceLocation WeakLoc = WeakTok.getLocation();
1998
1999 Token Tok;
2000 PP.Lex(Tok);
2001 if (Tok.isNot(tok::identifier)) {
2002 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
2003 return;
2004 }
2005
Eli Friedman68be1642012-10-04 02:36:51 +00002006 Token WeakName = Tok;
2007 bool HasAlias = false;
2008 Token AliasName;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002009
2010 PP.Lex(Tok);
2011 if (Tok.is(tok::equal)) {
Eli Friedman68be1642012-10-04 02:36:51 +00002012 HasAlias = true;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002013 PP.Lex(Tok);
2014 if (Tok.isNot(tok::identifier)) {
Mike Stump11289f42009-09-09 15:08:12 +00002015 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002016 << "weak";
2017 return;
2018 }
Eli Friedman68be1642012-10-04 02:36:51 +00002019 AliasName = Tok;
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002020 PP.Lex(Tok);
2021 }
2022
Peter Collingbourne2f1e36b2011-02-28 02:37:51 +00002023 if (Tok.isNot(tok::eod)) {
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002024 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
2025 return;
2026 }
2027
Eli Friedman68be1642012-10-04 02:36:51 +00002028 if (HasAlias) {
David Blaikie2eabcc92016-02-09 18:52:09 +00002029 MutableArrayRef<Token> Toks(
2030 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002031 Token &pragmaUnusedTok = Toks[0];
2032 pragmaUnusedTok.startToken();
2033 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
2034 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002035 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002036 Toks[1] = WeakName;
2037 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002038 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002039 } else {
David Blaikie2eabcc92016-02-09 18:52:09 +00002040 MutableArrayRef<Token> Toks(
2041 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
Eli Friedman68be1642012-10-04 02:36:51 +00002042 Token &pragmaUnusedTok = Toks[0];
2043 pragmaUnusedTok.startToken();
2044 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
2045 pragmaUnusedTok.setLocation(WeakLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002046 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
Eli Friedman68be1642012-10-04 02:36:51 +00002047 Toks[1] = WeakName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002048 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Eli Friedmanf5867dd2009-06-05 00:49:58 +00002049 }
2050}
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002051
David Chisnall0867d9c2012-02-18 16:12:34 +00002052// #pragma redefine_extname identifier identifier
Fangrui Song6907ce22018-07-30 19:24:48 +00002053void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
David Chisnall0867d9c2012-02-18 16:12:34 +00002054 PragmaIntroducerKind Introducer,
2055 Token &RedefToken) {
2056 SourceLocation RedefLoc = RedefToken.getLocation();
2057
2058 Token Tok;
2059 PP.Lex(Tok);
2060 if (Tok.isNot(tok::identifier)) {
2061 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2062 "redefine_extname";
2063 return;
2064 }
2065
Eli Friedman68be1642012-10-04 02:36:51 +00002066 Token RedefName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002067 PP.Lex(Tok);
Eli Friedman68be1642012-10-04 02:36:51 +00002068
David Chisnall0867d9c2012-02-18 16:12:34 +00002069 if (Tok.isNot(tok::identifier)) {
2070 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2071 << "redefine_extname";
2072 return;
2073 }
Eli Friedman68be1642012-10-04 02:36:51 +00002074
2075 Token AliasName = Tok;
David Chisnall0867d9c2012-02-18 16:12:34 +00002076 PP.Lex(Tok);
2077
2078 if (Tok.isNot(tok::eod)) {
2079 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2080 "redefine_extname";
2081 return;
2082 }
2083
David Blaikie2eabcc92016-02-09 18:52:09 +00002084 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
2085 3);
Eli Friedman68be1642012-10-04 02:36:51 +00002086 Token &pragmaRedefTok = Toks[0];
2087 pragmaRedefTok.startToken();
2088 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
2089 pragmaRedefTok.setLocation(RedefLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002090 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002091 Toks[1] = RedefName;
2092 Toks[2] = AliasName;
David Blaikie2eabcc92016-02-09 18:52:09 +00002093 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
David Chisnall0867d9c2012-02-18 16:12:34 +00002094}
2095
2096
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002097void
Fangrui Song6907ce22018-07-30 19:24:48 +00002098PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002099 PragmaIntroducerKind Introducer,
2100 Token &Tok) {
2101 tok::OnOffSwitch OOS;
2102 if (PP.LexOnOffSwitch(OOS))
2103 return;
2104
David Blaikie2eabcc92016-02-09 18:52:09 +00002105 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2106 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002107 Toks[0].startToken();
2108 Toks[0].setKind(tok::annot_pragma_fp_contract);
2109 Toks[0].setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002110 Toks[0].setAnnotationEndLoc(Tok.getLocation());
Eli Friedman68be1642012-10-04 02:36:51 +00002111 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
2112 static_cast<uintptr_t>(OOS)));
David Blaikie2eabcc92016-02-09 18:52:09 +00002113 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Peter Collingbourne564c0fa2011-02-14 01:42:35 +00002114}
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002115
Fangrui Song6907ce22018-07-30 19:24:48 +00002116void
2117PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002118 PragmaIntroducerKind Introducer,
2119 Token &Tok) {
Tanya Lattneree840b82011-04-14 23:35:31 +00002120 PP.LexUnexpandedToken(Tok);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002121 if (Tok.isNot(tok::identifier)) {
2122 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2123 "OPENCL";
2124 return;
2125 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002126 IdentifierInfo *Ext = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002127 SourceLocation NameLoc = Tok.getLocation();
2128
2129 PP.Lex(Tok);
2130 if (Tok.isNot(tok::colon)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002131 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002132 return;
2133 }
2134
2135 PP.Lex(Tok);
2136 if (Tok.isNot(tok::identifier)) {
Yaxun Liu5b746652016-12-18 05:18:55 +00002137 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002138 return;
2139 }
Yaxun Liu5b746652016-12-18 05:18:55 +00002140 IdentifierInfo *Pred = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002141
Yaxun Liu5b746652016-12-18 05:18:55 +00002142 OpenCLExtState State;
2143 if (Pred->isStr("enable")) {
2144 State = Enable;
2145 } else if (Pred->isStr("disable")) {
2146 State = Disable;
2147 } else if (Pred->isStr("begin"))
2148 State = Begin;
2149 else if (Pred->isStr("end"))
2150 State = End;
2151 else {
2152 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
2153 << Ext->isStr("all");
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002154 return;
2155 }
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002156 SourceLocation StateLoc = Tok.getLocation();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002157
Eli Friedman68be1642012-10-04 02:36:51 +00002158 PP.Lex(Tok);
2159 if (Tok.isNot(tok::eod)) {
2160 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2161 "OPENCL EXTENSION";
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002162 return;
2163 }
Eli Friedman68be1642012-10-04 02:36:51 +00002164
Yaxun Liu5b746652016-12-18 05:18:55 +00002165 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2166 Info->first = Ext;
2167 Info->second = State;
David Blaikie2eabcc92016-02-09 18:52:09 +00002168 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2169 1);
Eli Friedman68be1642012-10-04 02:36:51 +00002170 Toks[0].startToken();
2171 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2172 Toks[0].setLocation(NameLoc);
Yaxun Liu5b746652016-12-18 05:18:55 +00002173 Toks[0].setAnnotationValue(static_cast<void*>(Info));
David Majnemera8f2f1d2015-03-19 00:10:23 +00002174 Toks[0].setAnnotationEndLoc(StateLoc);
David Blaikie2eabcc92016-02-09 18:52:09 +00002175 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
Pekka Jaaskelainen1db1da22013-10-12 09:29:48 +00002176
2177 if (PP.getPPCallbacks())
Fangrui Song6907ce22018-07-30 19:24:48 +00002178 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
Yaxun Liu5b746652016-12-18 05:18:55 +00002179 StateLoc, State);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002180}
2181
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002182/// Handle '#pragma omp ...' when OpenMP is disabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002183///
2184void
2185PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2186 PragmaIntroducerKind Introducer,
2187 Token &FirstTok) {
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00002188 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2189 FirstTok.getLocation())) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002190 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
Alp Tokerd576e002014-06-12 11:13:52 +00002191 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2192 diag::Severity::Ignored, SourceLocation());
Alexey Bataeva769e072013-03-22 06:34:35 +00002193 }
2194 PP.DiscardUntilEndOfDirective();
2195}
2196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002197/// Handle '#pragma omp ...' when OpenMP is enabled.
Alexey Bataeva769e072013-03-22 06:34:35 +00002198///
2199void
2200PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2201 PragmaIntroducerKind Introducer,
2202 Token &FirstTok) {
2203 SmallVector<Token, 16> Pragma;
2204 Token Tok;
2205 Tok.startToken();
2206 Tok.setKind(tok::annot_pragma_openmp);
2207 Tok.setLocation(FirstTok.getLocation());
2208
Alexey Bataev96dae812018-02-16 18:36:44 +00002209 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002210 Pragma.push_back(Tok);
2211 PP.Lex(Tok);
Alexey Bataev96dae812018-02-16 18:36:44 +00002212 if (Tok.is(tok::annot_pragma_openmp)) {
2213 PP.Diag(Tok, diag::err_omp_unexpected_directive) << 0;
2214 unsigned InnerPragmaCnt = 1;
2215 while (InnerPragmaCnt != 0) {
2216 PP.Lex(Tok);
2217 if (Tok.is(tok::annot_pragma_openmp))
2218 ++InnerPragmaCnt;
2219 else if (Tok.is(tok::annot_pragma_openmp_end))
2220 --InnerPragmaCnt;
2221 }
2222 PP.Lex(Tok);
2223 }
Alexey Bataeva769e072013-03-22 06:34:35 +00002224 }
2225 SourceLocation EodLoc = Tok.getLocation();
2226 Tok.startToken();
2227 Tok.setKind(tok::annot_pragma_openmp_end);
2228 Tok.setLocation(EodLoc);
2229 Pragma.push_back(Tok);
2230
David Blaikie2eabcc92016-02-09 18:52:09 +00002231 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2232 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2233 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
2234 /*DisableMacroExpansion=*/false);
Alexey Bataeva769e072013-03-22 06:34:35 +00002235}
Reid Kleckner002562a2013-05-06 21:02:12 +00002236
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002237/// Handle '#pragma pointers_to_members'
David Majnemer4bb09802014-02-10 19:50:15 +00002238// The grammar for this pragma is as follows:
2239//
2240// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2241//
2242// #pragma pointers_to_members '(' 'best_case' ')'
2243// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2244// #pragma pointers_to_members '(' inheritance-model ')'
2245void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
2246 PragmaIntroducerKind Introducer,
2247 Token &Tok) {
2248 SourceLocation PointersToMembersLoc = Tok.getLocation();
2249 PP.Lex(Tok);
2250 if (Tok.isNot(tok::l_paren)) {
2251 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2252 << "pointers_to_members";
2253 return;
2254 }
2255 PP.Lex(Tok);
2256 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2257 if (!Arg) {
2258 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2259 << "pointers_to_members";
2260 return;
2261 }
2262 PP.Lex(Tok);
2263
David Majnemer86c318f2014-02-11 21:05:00 +00002264 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
David Majnemer4bb09802014-02-10 19:50:15 +00002265 if (Arg->isStr("best_case")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002266 RepresentationMethod = LangOptions::PPTMK_BestCase;
David Majnemer4bb09802014-02-10 19:50:15 +00002267 } else {
2268 if (Arg->isStr("full_generality")) {
2269 if (Tok.is(tok::comma)) {
2270 PP.Lex(Tok);
2271
2272 Arg = Tok.getIdentifierInfo();
2273 if (!Arg) {
2274 PP.Diag(Tok.getLocation(),
2275 diag::err_pragma_pointers_to_members_unknown_kind)
2276 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2277 return;
2278 }
2279 PP.Lex(Tok);
2280 } else if (Tok.is(tok::r_paren)) {
2281 // #pragma pointers_to_members(full_generality) implicitly specifies
2282 // virtual_inheritance.
Craig Topper161e4db2014-05-21 06:02:52 +00002283 Arg = nullptr;
David Majnemer86c318f2014-02-11 21:05:00 +00002284 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002285 } else {
2286 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2287 << "full_generality";
2288 return;
2289 }
2290 }
2291
2292 if (Arg) {
2293 if (Arg->isStr("single_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002294 RepresentationMethod =
2295 LangOptions::PPTMK_FullGeneralitySingleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002296 } else if (Arg->isStr("multiple_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002297 RepresentationMethod =
2298 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002299 } else if (Arg->isStr("virtual_inheritance")) {
David Majnemer86c318f2014-02-11 21:05:00 +00002300 RepresentationMethod =
2301 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
David Majnemer4bb09802014-02-10 19:50:15 +00002302 } else {
2303 PP.Diag(Tok.getLocation(),
2304 diag::err_pragma_pointers_to_members_unknown_kind)
2305 << Arg << /*HasPointerDeclaration*/ 1;
2306 return;
2307 }
2308 }
2309 }
2310
2311 if (Tok.isNot(tok::r_paren)) {
2312 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2313 << (Arg ? Arg->getName() : "full_generality");
2314 return;
2315 }
2316
David Majnemera8f2f1d2015-03-19 00:10:23 +00002317 SourceLocation EndLoc = Tok.getLocation();
David Majnemer4bb09802014-02-10 19:50:15 +00002318 PP.Lex(Tok);
2319 if (Tok.isNot(tok::eod)) {
2320 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2321 << "pointers_to_members";
2322 return;
2323 }
2324
2325 Token AnnotTok;
2326 AnnotTok.startToken();
2327 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2328 AnnotTok.setLocation(PointersToMembersLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002329 AnnotTok.setAnnotationEndLoc(EndLoc);
David Majnemer4bb09802014-02-10 19:50:15 +00002330 AnnotTok.setAnnotationValue(
2331 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
2332 PP.EnterToken(AnnotTok);
2333}
2334
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002335/// Handle '#pragma vtordisp'
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002336// The grammar for this pragma is as follows:
2337//
2338// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2339//
2340// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2341// #pragma vtordisp '(' 'pop' ')'
2342// #pragma vtordisp '(' ')'
2343void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
2344 PragmaIntroducerKind Introducer,
2345 Token &Tok) {
2346 SourceLocation VtorDispLoc = Tok.getLocation();
2347 PP.Lex(Tok);
2348 if (Tok.isNot(tok::l_paren)) {
2349 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2350 return;
2351 }
2352 PP.Lex(Tok);
2353
Denis Zobnin2290dac2016-04-29 11:27:00 +00002354 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002355 const IdentifierInfo *II = Tok.getIdentifierInfo();
2356 if (II) {
2357 if (II->isStr("push")) {
2358 // #pragma vtordisp(push, mode)
2359 PP.Lex(Tok);
2360 if (Tok.isNot(tok::comma)) {
2361 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2362 return;
2363 }
2364 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002365 Action = Sema::PSK_Push_Set;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002366 // not push, could be on/off
2367 } else if (II->isStr("pop")) {
2368 // #pragma vtordisp(pop)
2369 PP.Lex(Tok);
Denis Zobnin2290dac2016-04-29 11:27:00 +00002370 Action = Sema::PSK_Pop;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002371 }
2372 // not push or pop, could be on/off
2373 } else {
2374 if (Tok.is(tok::r_paren)) {
2375 // #pragma vtordisp()
Denis Zobnin2290dac2016-04-29 11:27:00 +00002376 Action = Sema::PSK_Reset;
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002377 }
2378 }
2379
2380
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002381 uint64_t Value = 0;
Denis Zobnin2290dac2016-04-29 11:27:00 +00002382 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002383 const IdentifierInfo *II = Tok.getIdentifierInfo();
2384 if (II && II->isStr("off")) {
2385 PP.Lex(Tok);
2386 Value = 0;
2387 } else if (II && II->isStr("on")) {
2388 PP.Lex(Tok);
2389 Value = 1;
2390 } else if (Tok.is(tok::numeric_constant) &&
2391 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2392 if (Value > 2) {
2393 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2394 << 0 << 2 << "vtordisp";
2395 return;
2396 }
2397 } else {
2398 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2399 << "vtordisp";
2400 return;
2401 }
2402 }
2403
2404 // Finish the pragma: ')' $
2405 if (Tok.isNot(tok::r_paren)) {
2406 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2407 return;
2408 }
David Majnemera8f2f1d2015-03-19 00:10:23 +00002409 SourceLocation EndLoc = Tok.getLocation();
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002410 PP.Lex(Tok);
2411 if (Tok.isNot(tok::eod)) {
2412 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2413 << "vtordisp";
2414 return;
2415 }
2416
2417 // Enter the annotation.
2418 Token AnnotTok;
2419 AnnotTok.startToken();
2420 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2421 AnnotTok.setLocation(VtorDispLoc);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002422 AnnotTok.setAnnotationEndLoc(EndLoc);
Reid Klecknera4b3b2d2014-02-13 00:44:34 +00002423 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
Denis Zobnin2290dac2016-04-29 11:27:00 +00002424 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
Reid Klecknerc0dca6d2014-02-12 23:50:26 +00002425 PP.EnterToken(AnnotTok);
2426}
2427
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002428/// Handle all MS pragmas. Simply forwards the tokens after inserting
Warren Huntc3b18962014-04-08 22:30:47 +00002429/// an annotation token.
2430void PragmaMSPragma::HandlePragma(Preprocessor &PP,
2431 PragmaIntroducerKind Introducer,
2432 Token &Tok) {
2433 Token EoF, AnnotTok;
2434 EoF.startToken();
2435 EoF.setKind(tok::eof);
2436 AnnotTok.startToken();
2437 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2438 AnnotTok.setLocation(Tok.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002439 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
Warren Huntc3b18962014-04-08 22:30:47 +00002440 SmallVector<Token, 8> TokenVector;
2441 // Suck up all of the tokens before the eod.
David Majnemera8f2f1d2015-03-19 00:10:23 +00002442 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
Warren Huntc3b18962014-04-08 22:30:47 +00002443 TokenVector.push_back(Tok);
David Majnemera8f2f1d2015-03-19 00:10:23 +00002444 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2445 }
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00002446 // Add a sentinel EoF token to the end of the list.
Warren Huntc3b18962014-04-08 22:30:47 +00002447 TokenVector.push_back(EoF);
2448 // We must allocate this array with new because EnterTokenStream is going to
2449 // delete it later.
David Blaikie2eabcc92016-02-09 18:52:09 +00002450 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2451 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
Warren Huntc3b18962014-04-08 22:30:47 +00002452 auto Value = new (PP.getPreprocessorAllocator())
David Blaikie2eabcc92016-02-09 18:52:09 +00002453 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2454 TokenVector.size());
Warren Huntc3b18962014-04-08 22:30:47 +00002455 AnnotTok.setAnnotationValue(Value);
2456 PP.EnterToken(AnnotTok);
Reid Klecknerd3923aa2014-04-03 19:04:24 +00002457}
2458
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002459/// Handle the Microsoft \#pragma detect_mismatch extension.
Aaron Ballman5d041be2013-06-04 02:07:14 +00002460///
2461/// The syntax is:
2462/// \code
2463/// #pragma detect_mismatch("name", "value")
2464/// \endcode
2465/// Where 'name' and 'value' are quoted strings. The values are embedded in
2466/// the object file and passed along to the linker. If the linker detects a
2467/// mismatch in the object file's values for the given name, a LNK2038 error
2468/// is emitted. See MSDN for more details.
2469void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
2470 PragmaIntroducerKind Introducer,
2471 Token &Tok) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002472 SourceLocation DetectMismatchLoc = Tok.getLocation();
Aaron Ballman5d041be2013-06-04 02:07:14 +00002473 PP.Lex(Tok);
2474 if (Tok.isNot(tok::l_paren)) {
Nico Webercbbaeb12016-03-02 19:28:54 +00002475 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002476 return;
2477 }
2478
2479 // Read the name to embed, which must be a string literal.
2480 std::string NameString;
2481 if (!PP.LexStringLiteral(Tok, NameString,
2482 "pragma detect_mismatch",
2483 /*MacroExpansion=*/true))
2484 return;
2485
2486 // Read the comma followed by a second string literal.
2487 std::string ValueString;
2488 if (Tok.isNot(tok::comma)) {
2489 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2490 return;
2491 }
2492
2493 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
2494 /*MacroExpansion=*/true))
2495 return;
2496
2497 if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00002498 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
Aaron Ballman5d041be2013-06-04 02:07:14 +00002499 return;
2500 }
2501 PP.Lex(Tok); // Eat the r_paren.
2502
2503 if (Tok.isNot(tok::eod)) {
2504 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2505 return;
2506 }
2507
Reid Kleckner71966c92014-02-20 23:37:45 +00002508 // If the pragma is lexically sound, notify any interested PPCallbacks.
2509 if (PP.getPPCallbacks())
Nico Webercbbaeb12016-03-02 19:28:54 +00002510 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
Reid Kleckner71966c92014-02-20 23:37:45 +00002511 ValueString);
2512
Nico Webercbbaeb12016-03-02 19:28:54 +00002513 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
Aaron Ballman5d041be2013-06-04 02:07:14 +00002514}
2515
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002516/// Handle the microsoft \#pragma comment extension.
Reid Kleckner002562a2013-05-06 21:02:12 +00002517///
2518/// The syntax is:
2519/// \code
2520/// #pragma comment(linker, "foo")
2521/// \endcode
2522/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2523/// "foo" is a string, which is fully macro expanded, and permits string
2524/// concatenation, embedded escape characters etc. See MSDN for more details.
2525void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
2526 PragmaIntroducerKind Introducer,
2527 Token &Tok) {
2528 SourceLocation CommentLoc = Tok.getLocation();
2529 PP.Lex(Tok);
2530 if (Tok.isNot(tok::l_paren)) {
2531 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2532 return;
2533 }
2534
2535 // Read the identifier.
2536 PP.Lex(Tok);
2537 if (Tok.isNot(tok::identifier)) {
2538 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2539 return;
2540 }
2541
2542 // Verify that this is one of the 5 whitelisted options.
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002543 IdentifierInfo *II = Tok.getIdentifierInfo();
Nico Weber66220292016-03-02 17:28:48 +00002544 PragmaMSCommentKind Kind =
2545 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2546 .Case("linker", PCK_Linker)
2547 .Case("lib", PCK_Lib)
2548 .Case("compiler", PCK_Compiler)
2549 .Case("exestr", PCK_ExeStr)
2550 .Case("user", PCK_User)
2551 .Default(PCK_Unknown);
2552 if (Kind == PCK_Unknown) {
Reid Kleckner002562a2013-05-06 21:02:12 +00002553 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2554 return;
2555 }
2556
Saleem Abdulrasoolfd4db532018-02-07 01:46:46 +00002557 if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
2558 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2559 << II->getName();
2560 return;
2561 }
2562
Yunzhong Gao99efc032015-03-23 20:41:42 +00002563 // On PS4, issue a warning about any pragma comments other than
2564 // #pragma comment lib.
Nico Weber66220292016-03-02 17:28:48 +00002565 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
Yunzhong Gao99efc032015-03-23 20:41:42 +00002566 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2567 << II->getName();
2568 return;
2569 }
2570
Reid Kleckner002562a2013-05-06 21:02:12 +00002571 // Read the optional string if present.
2572 PP.Lex(Tok);
2573 std::string ArgumentString;
2574 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2575 "pragma comment",
2576 /*MacroExpansion=*/true))
2577 return;
2578
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002579 // FIXME: warn that 'exestr' is deprecated.
Reid Kleckner002562a2013-05-06 21:02:12 +00002580 // FIXME: If the kind is "compiler" warn if the string is present (it is
2581 // ignored).
Reid Klecknere43f0fe2013-05-08 13:44:39 +00002582 // The MSDN docs say that "lib" and "linker" require a string and have a short
2583 // whitelist of linker options they support, but in practice MSVC doesn't
2584 // issue a diagnostic. Therefore neither does clang.
Reid Kleckner002562a2013-05-06 21:02:12 +00002585
2586 if (Tok.isNot(tok::r_paren)) {
2587 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2588 return;
2589 }
2590 PP.Lex(Tok); // eat the r_paren.
2591
2592 if (Tok.isNot(tok::eod)) {
2593 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2594 return;
2595 }
2596
Reid Kleckner71966c92014-02-20 23:37:45 +00002597 // If the pragma is lexically sound, notify any interested PPCallbacks.
2598 if (PP.getPPCallbacks())
2599 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2600
Nico Weber66220292016-03-02 17:28:48 +00002601 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
Reid Kleckner002562a2013-05-06 21:02:12 +00002602}
Dario Domizioli13a0a382014-05-23 12:13:25 +00002603
2604// #pragma clang optimize off
2605// #pragma clang optimize on
Fangrui Song6907ce22018-07-30 19:24:48 +00002606void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
Dario Domizioli13a0a382014-05-23 12:13:25 +00002607 PragmaIntroducerKind Introducer,
2608 Token &FirstToken) {
2609 Token Tok;
2610 PP.Lex(Tok);
2611 if (Tok.is(tok::eod)) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002612 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
Mark Heffernan450c2382014-07-23 17:31:31 +00002613 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
Dario Domizioli13a0a382014-05-23 12:13:25 +00002614 return;
2615 }
2616 if (Tok.isNot(tok::identifier)) {
2617 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2618 << PP.getSpelling(Tok);
2619 return;
2620 }
2621 const IdentifierInfo *II = Tok.getIdentifierInfo();
2622 // The only accepted values are 'on' or 'off'.
2623 bool IsOn = false;
2624 if (II->isStr("on")) {
2625 IsOn = true;
2626 } else if (!II->isStr("off")) {
2627 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2628 << PP.getSpelling(Tok);
2629 return;
2630 }
2631 PP.Lex(Tok);
Fangrui Song6907ce22018-07-30 19:24:48 +00002632
Dario Domizioli13a0a382014-05-23 12:13:25 +00002633 if (Tok.isNot(tok::eod)) {
2634 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2635 << PP.getSpelling(Tok);
2636 return;
2637 }
Eli Bendersky06a40422014-06-06 20:31:48 +00002638
2639 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2640}
2641
Adam Nemet60d32642017-04-04 21:18:36 +00002642namespace {
2643/// Used as the annotation value for tok::annot_pragma_fp.
2644struct TokFPAnnotValue {
2645 enum FlagKinds { Contract };
2646 enum FlagValues { On, Off, Fast };
2647
2648 FlagKinds FlagKind;
2649 FlagValues FlagValue;
2650};
2651} // end anonymous namespace
2652
2653void PragmaFPHandler::HandlePragma(Preprocessor &PP,
2654 PragmaIntroducerKind Introducer,
2655 Token &Tok) {
2656 // fp
2657 Token PragmaName = Tok;
2658 SmallVector<Token, 1> TokenList;
2659
2660 PP.Lex(Tok);
2661 if (Tok.isNot(tok::identifier)) {
2662 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2663 << /*MissingOption=*/true << "";
2664 return;
2665 }
2666
2667 while (Tok.is(tok::identifier)) {
2668 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2669
2670 auto FlagKind =
2671 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2672 OptionInfo->getName())
2673 .Case("contract", TokFPAnnotValue::Contract)
2674 .Default(None);
2675 if (!FlagKind) {
2676 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2677 << /*MissingOption=*/false << OptionInfo;
2678 return;
2679 }
2680 PP.Lex(Tok);
2681
2682 // Read '('
2683 if (Tok.isNot(tok::l_paren)) {
2684 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2685 return;
2686 }
2687 PP.Lex(Tok);
2688
2689 if (Tok.isNot(tok::identifier)) {
2690 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2691 << PP.getSpelling(Tok) << OptionInfo->getName();
2692 return;
2693 }
2694 const IdentifierInfo *II = Tok.getIdentifierInfo();
2695
2696 auto FlagValue =
2697 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2698 II->getName())
2699 .Case("on", TokFPAnnotValue::On)
2700 .Case("off", TokFPAnnotValue::Off)
2701 .Case("fast", TokFPAnnotValue::Fast)
2702 .Default(llvm::None);
2703
2704 if (!FlagValue) {
2705 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2706 << PP.getSpelling(Tok) << OptionInfo->getName();
2707 return;
2708 }
2709 PP.Lex(Tok);
2710
2711 // Read ')'
2712 if (Tok.isNot(tok::r_paren)) {
2713 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2714 return;
2715 }
2716 PP.Lex(Tok);
2717
2718 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2719 TokFPAnnotValue{*FlagKind, *FlagValue};
2720 // Generate the loop hint token.
2721 Token FPTok;
2722 FPTok.startToken();
2723 FPTok.setKind(tok::annot_pragma_fp);
2724 FPTok.setLocation(PragmaName.getLocation());
2725 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2726 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2727 TokenList.push_back(FPTok);
2728 }
2729
2730 if (Tok.isNot(tok::eod)) {
2731 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2732 << "clang fp";
2733 return;
2734 }
2735
2736 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2737 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2738
2739 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2740 /*DisableMacroExpansion=*/false);
2741}
2742
2743void Parser::HandlePragmaFP() {
2744 assert(Tok.is(tok::annot_pragma_fp));
2745 auto *AnnotValue =
2746 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2747
2748 LangOptions::FPContractModeKind FPC;
2749 switch (AnnotValue->FlagValue) {
2750 case TokFPAnnotValue::On:
2751 FPC = LangOptions::FPC_On;
2752 break;
2753 case TokFPAnnotValue::Fast:
2754 FPC = LangOptions::FPC_Fast;
2755 break;
2756 case TokFPAnnotValue::Off:
2757 FPC = LangOptions::FPC_Off;
2758 break;
2759 }
2760
2761 Actions.ActOnPragmaFPContract(FPC);
Richard Smithaf3b3252017-05-18 19:21:48 +00002762 ConsumeAnnotationToken();
Adam Nemet60d32642017-04-04 21:18:36 +00002763}
2764
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002765/// Parses loop or unroll pragma hint value and fills in Info.
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002766static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2767 Token Option, bool ValueInParens,
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002768 PragmaLoopHintInfo &Info) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002769 SmallVector<Token, 1> ValueList;
2770 int OpenParens = ValueInParens ? 1 : 0;
2771 // Read constant expression.
2772 while (Tok.isNot(tok::eod)) {
2773 if (Tok.is(tok::l_paren))
2774 OpenParens++;
2775 else if (Tok.is(tok::r_paren)) {
2776 OpenParens--;
2777 if (OpenParens == 0 && ValueInParens)
2778 break;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002779 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002780
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002781 ValueList.push_back(Tok);
2782 PP.Lex(Tok);
2783 }
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002784
2785 if (ValueInParens) {
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002786 // Read ')'
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002787 if (Tok.isNot(tok::r_paren)) {
2788 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2789 return true;
2790 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002791 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002792 }
2793
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002794 Token EOFTok;
2795 EOFTok.startToken();
2796 EOFTok.setKind(tok::eof);
2797 EOFTok.setLocation(Tok.getLocation());
2798 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2799
Benjamin Kramerfa7f8552015-08-05 09:39:57 +00002800 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002801
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002802 Info.PragmaName = PragmaName;
2803 Info.Option = Option;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002804 return false;
2805}
2806
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002807/// Handle the \#pragma clang loop directive.
Eli Bendersky06a40422014-06-06 20:31:48 +00002808/// #pragma clang 'loop' loop-hints
2809///
2810/// loop-hints:
2811/// loop-hint loop-hints[opt]
2812///
2813/// loop-hint:
2814/// 'vectorize' '(' loop-hint-keyword ')'
2815/// 'interleave' '(' loop-hint-keyword ')'
Mark Heffernan450c2382014-07-23 17:31:31 +00002816/// 'unroll' '(' unroll-hint-keyword ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002817/// 'vectorize_width' '(' loop-hint-value ')'
2818/// 'interleave_count' '(' loop-hint-value ')'
Eli Bendersky86483b32014-06-11 17:56:26 +00002819/// 'unroll_count' '(' loop-hint-value ')'
Aaron Ballman9bdf5152019-01-04 17:20:00 +00002820/// 'pipeline' '(' disable ')'
2821/// 'pipeline_initiation_interval' '(' loop-hint-value ')'
Eli Bendersky06a40422014-06-06 20:31:48 +00002822///
2823/// loop-hint-keyword:
2824/// 'enable'
2825/// 'disable'
Tyler Nowicki9d268e12015-06-11 23:23:17 +00002826/// 'assume_safety'
Eli Bendersky06a40422014-06-06 20:31:48 +00002827///
Mark Heffernan450c2382014-07-23 17:31:31 +00002828/// unroll-hint-keyword:
Mark Heffernan397a98d2015-08-10 17:29:39 +00002829/// 'enable'
Mark Heffernan450c2382014-07-23 17:31:31 +00002830/// 'disable'
Mark Heffernan397a98d2015-08-10 17:29:39 +00002831/// 'full'
Mark Heffernan450c2382014-07-23 17:31:31 +00002832///
Eli Bendersky06a40422014-06-06 20:31:48 +00002833/// loop-hint-value:
2834/// constant-expression
2835///
2836/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2837/// try vectorizing the instructions of the loop it precedes. Specifying
2838/// interleave(enable) or interleave_count(_value_) instructs llvm to try
2839/// interleaving multiple iterations of the loop it precedes. The width of the
2840/// vector instructions is specified by vectorize_width() and the number of
2841/// interleaved loop iterations is specified by interleave_count(). Specifying a
2842/// value of 1 effectively disables vectorization/interleaving, even if it is
2843/// possible and profitable, and 0 is invalid. The loop vectorizer currently
2844/// only works on inner loops.
2845///
Eli Bendersky86483b32014-06-11 17:56:26 +00002846/// The unroll and unroll_count directives control the concatenation
Mark Heffernan397a98d2015-08-10 17:29:39 +00002847/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2848/// completely if the trip count is known at compile time and unroll partially
2849/// if the trip count is not known. Specifying unroll(full) is similar to
2850/// unroll(enable) but will unroll the loop only if the trip count is known at
2851/// compile time. Specifying unroll(disable) disables unrolling for the
2852/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2853/// loop the number of times indicated by the value.
Eli Bendersky06a40422014-06-06 20:31:48 +00002854void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2855 PragmaIntroducerKind Introducer,
2856 Token &Tok) {
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002857 // Incoming token is "loop" from "#pragma clang loop".
2858 Token PragmaName = Tok;
Eli Bendersky06a40422014-06-06 20:31:48 +00002859 SmallVector<Token, 1> TokenList;
2860
2861 // Lex the optimization option and verify it is an identifier.
2862 PP.Lex(Tok);
2863 if (Tok.isNot(tok::identifier)) {
2864 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2865 << /*MissingOption=*/true << "";
2866 return;
2867 }
2868
2869 while (Tok.is(tok::identifier)) {
2870 Token Option = Tok;
2871 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2872
Eli Bendersky86483b32014-06-11 17:56:26 +00002873 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002874 .Case("vectorize", true)
2875 .Case("interleave", true)
2876 .Case("unroll", true)
Adam Nemet2de463e2016-06-14 12:04:26 +00002877 .Case("distribute", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002878 .Case("vectorize_width", true)
2879 .Case("interleave_count", true)
2880 .Case("unroll_count", true)
Aaron Ballman9bdf5152019-01-04 17:20:00 +00002881 .Case("pipeline", true)
2882 .Case("pipeline_initiation_interval", true)
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002883 .Default(false);
Eli Bendersky86483b32014-06-11 17:56:26 +00002884 if (!OptionValid) {
Eli Bendersky06a40422014-06-06 20:31:48 +00002885 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2886 << /*MissingOption=*/false << OptionInfo;
2887 return;
2888 }
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002889 PP.Lex(Tok);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002890
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002891 // Read '('
2892 if (Tok.isNot(tok::l_paren)) {
2893 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002894 return;
2895 }
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002896 PP.Lex(Tok);
2897
2898 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2899 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2900 *Info))
2901 return;
NAKAMURA Takumidb9552f2014-07-31 01:52:33 +00002902
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002903 // Generate the loop hint token.
Eli Bendersky06a40422014-06-06 20:31:48 +00002904 Token LoopHintTok;
2905 LoopHintTok.startToken();
2906 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002907 LoopHintTok.setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002908 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
Eli Bendersky06a40422014-06-06 20:31:48 +00002909 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2910 TokenList.push_back(LoopHintTok);
2911 }
2912
2913 if (Tok.isNot(tok::eod)) {
2914 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2915 << "clang loop";
2916 return;
2917 }
2918
David Blaikie2eabcc92016-02-09 18:52:09 +00002919 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2920 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
Eli Bendersky06a40422014-06-06 20:31:48 +00002921
David Blaikie2eabcc92016-02-09 18:52:09 +00002922 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2923 /*DisableMacroExpansion=*/false);
Eli Bendersky06a40422014-06-06 20:31:48 +00002924}
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002925
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002926/// Handle the loop unroll optimization pragmas.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002927/// #pragma unroll
2928/// #pragma unroll unroll-hint-value
2929/// #pragma unroll '(' unroll-hint-value ')'
Mark Heffernanc888e412014-07-24 18:09:38 +00002930/// #pragma nounroll
David Greenc8e39242018-08-01 14:36:12 +00002931/// #pragma unroll_and_jam
2932/// #pragma unroll_and_jam unroll-hint-value
2933/// #pragma unroll_and_jam '(' unroll-hint-value ')'
2934/// #pragma nounroll_and_jam
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002935///
2936/// unroll-hint-value:
2937/// constant-expression
2938///
Mark Heffernanc888e412014-07-24 18:09:38 +00002939/// Loop unrolling hints can be specified with '#pragma unroll' or
2940/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2941/// contained in parentheses. With no argument the directive instructs llvm to
2942/// try to unroll the loop completely. A positive integer argument can be
2943/// specified to indicate the number of times the loop should be unrolled. To
2944/// maximize compatibility with other compilers the unroll count argument can be
2945/// specified with or without parentheses. Specifying, '#pragma nounroll'
2946/// disables unrolling of the loop.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002947void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2948 PragmaIntroducerKind Introducer,
2949 Token &Tok) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002950 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2951 // "#pragma nounroll".
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002952 Token PragmaName = Tok;
2953 PP.Lex(Tok);
2954 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2955 if (Tok.is(tok::eod)) {
Mark Heffernanc888e412014-07-24 18:09:38 +00002956 // nounroll or unroll pragma without an argument.
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002957 Info->PragmaName = PragmaName;
Aaron Ballmand6807da2014-08-01 12:41:37 +00002958 Info->Option.startToken();
David Greenc8e39242018-08-01 14:36:12 +00002959 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll" ||
2960 PragmaName.getIdentifierInfo()->getName() == "nounroll_and_jam") {
Mark Heffernanc888e412014-07-24 18:09:38 +00002961 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
David Greenc8e39242018-08-01 14:36:12 +00002962 << PragmaName.getIdentifierInfo()->getName();
Mark Heffernanc888e412014-07-24 18:09:38 +00002963 return;
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002964 } else {
2965 // Unroll pragma with an argument: "#pragma unroll N" or
2966 // "#pragma unroll(N)".
Tyler Nowicki0c9b34b2014-07-31 20:15:14 +00002967 // Read '(' if it exists.
2968 bool ValueInParens = Tok.is(tok::l_paren);
2969 if (ValueInParens)
2970 PP.Lex(Tok);
2971
Aaron Ballmand0b090d2014-08-01 12:20:20 +00002972 Token Option;
2973 Option.startToken();
2974 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002975 return;
2976
2977 // In CUDA, the argument to '#pragma unroll' should not be contained in
2978 // parentheses.
2979 if (PP.getLangOpts().CUDA && ValueInParens)
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002980 PP.Diag(Info->Toks[0].getLocation(),
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002981 diag::warn_pragma_unroll_cuda_value_in_parens);
2982
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002983 if (Tok.isNot(tok::eod)) {
2984 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2985 << "unroll";
2986 return;
2987 }
2988 }
2989
2990 // Generate the hint token.
David Blaikie2eabcc92016-02-09 18:52:09 +00002991 auto TokenArray = llvm::make_unique<Token[]>(1);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002992 TokenArray[0].startToken();
2993 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2994 TokenArray[0].setLocation(PragmaName.getLocation());
David Majnemera8f2f1d2015-03-19 00:10:23 +00002995 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002996 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
David Blaikie2eabcc92016-02-09 18:52:09 +00002997 PP.EnterTokenStream(std::move(TokenArray), 1,
2998 /*DisableMacroExpansion=*/false);
Mark Heffernanbd26f5e2014-07-21 18:08:34 +00002999}
Reid Kleckner3f1ec622016-09-07 16:38:32 +00003000
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003001/// Handle the Microsoft \#pragma intrinsic extension.
Reid Kleckner3f1ec622016-09-07 16:38:32 +00003002///
3003/// The syntax is:
3004/// \code
3005/// #pragma intrinsic(memset)
3006/// #pragma intrinsic(strlen, memcpy)
3007/// \endcode
3008///
3009/// Pragma intrisic tells the compiler to use a builtin version of the
3010/// function. Clang does it anyway, so the pragma doesn't really do anything.
3011/// Anyway, we emit a warning if the function specified in \#pragma intrinsic
3012/// isn't an intrinsic in clang and suggest to include intrin.h.
3013void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
3014 PragmaIntroducerKind Introducer,
3015 Token &Tok) {
3016 PP.Lex(Tok);
3017
3018 if (Tok.isNot(tok::l_paren)) {
3019 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
3020 << "intrinsic";
3021 return;
3022 }
3023 PP.Lex(Tok);
3024
3025 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
3026
3027 while (Tok.is(tok::identifier)) {
3028 IdentifierInfo *II = Tok.getIdentifierInfo();
3029 if (!II->getBuiltinID())
3030 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
3031 << II << SuggestIntrinH;
3032
3033 PP.Lex(Tok);
3034 if (Tok.isNot(tok::comma))
3035 break;
3036 PP.Lex(Tok);
3037 }
3038
3039 if (Tok.isNot(tok::r_paren)) {
3040 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
3041 << "intrinsic";
3042 return;
3043 }
3044 PP.Lex(Tok);
3045
3046 if (Tok.isNot(tok::eod))
3047 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3048 << "intrinsic";
3049}
Hans Wennborg1bbe00e2018-03-20 08:53:11 +00003050
3051// #pragma optimize("gsty", on|off)
3052void PragmaMSOptimizeHandler::HandlePragma(Preprocessor &PP,
3053 PragmaIntroducerKind Introducer,
3054 Token &Tok) {
3055 SourceLocation StartLoc = Tok.getLocation();
3056 PP.Lex(Tok);
3057
3058 if (Tok.isNot(tok::l_paren)) {
3059 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "optimize";
3060 return;
3061 }
3062 PP.Lex(Tok);
3063
3064 if (Tok.isNot(tok::string_literal)) {
3065 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_string) << "optimize";
3066 return;
3067 }
3068 // We could syntax check the string but it's probably not worth the effort.
3069 PP.Lex(Tok);
3070
3071 if (Tok.isNot(tok::comma)) {
3072 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_comma) << "optimize";
3073 return;
3074 }
3075 PP.Lex(Tok);
3076
3077 if (Tok.is(tok::eod) || Tok.is(tok::r_paren)) {
3078 PP.Diag(Tok.getLocation(), diag::warn_pragma_missing_argument)
3079 << "optimize" << /*Expected=*/true << "'on' or 'off'";
3080 return;
3081 }
3082 IdentifierInfo *II = Tok.getIdentifierInfo();
3083 if (!II || (!II->isStr("on") && !II->isStr("off"))) {
3084 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
3085 << PP.getSpelling(Tok) << "optimize" << /*Expected=*/true
3086 << "'on' or 'off'";
3087 return;
3088 }
3089 PP.Lex(Tok);
3090
3091 if (Tok.isNot(tok::r_paren)) {
3092 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "optimize";
3093 return;
3094 }
3095 PP.Lex(Tok);
3096
3097 if (Tok.isNot(tok::eod)) {
3098 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3099 << "optimize";
3100 return;
3101 }
3102 PP.Diag(StartLoc, diag::warn_pragma_optimize);
3103}
3104
Justin Lebar67a78a62016-10-08 22:15:58 +00003105void PragmaForceCUDAHostDeviceHandler::HandlePragma(
3106 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
3107 Token FirstTok = Tok;
3108
3109 PP.Lex(Tok);
3110 IdentifierInfo *Info = Tok.getIdentifierInfo();
3111 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
3112 PP.Diag(FirstTok.getLocation(),
3113 diag::warn_pragma_force_cuda_host_device_bad_arg);
3114 return;
3115 }
3116
3117 if (Info->isStr("begin"))
3118 Actions.PushForceCUDAHostDevice();
3119 else if (!Actions.PopForceCUDAHostDevice())
3120 PP.Diag(FirstTok.getLocation(),
3121 diag::err_pragma_cannot_end_force_cuda_host_device);
3122
3123 PP.Lex(Tok);
3124 if (!Tok.is(tok::eod))
3125 PP.Diag(FirstTok.getLocation(),
3126 diag::warn_pragma_force_cuda_host_device_bad_arg);
3127}
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003128
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003129/// Handle the #pragma clang attribute directive.
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003130///
3131/// The syntax is:
3132/// \code
Erik Pilkington0876cae2018-12-20 22:32:04 +00003133/// #pragma clang attribute push (attribute, subject-set)
Erik Pilkington7d180942018-10-29 17:38:42 +00003134/// #pragma clang attribute push
3135/// #pragma clang attribute (attribute, subject-set)
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003136/// #pragma clang attribute pop
3137/// \endcode
3138///
Erik Pilkington0876cae2018-12-20 22:32:04 +00003139/// There are also 'namespace' variants of push and pop directives. The bare
3140/// '#pragma clang attribute (attribute, subject-set)' version doesn't require a
3141/// namespace, since it always applies attributes to the most recently pushed
3142/// group, regardless of namespace.
3143/// \code
3144/// #pragma clang attribute namespace.push (attribute, subject-set)
3145/// #pragma clang attribute namespace.push
3146/// #pragma clang attribute namespace.pop
3147/// \endcode
3148///
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003149/// The subject-set clause defines the set of declarations which receive the
3150/// attribute. Its exact syntax is described in the LanguageExtensions document
3151/// in Clang's documentation.
3152///
3153/// This directive instructs the compiler to begin/finish applying the specified
3154/// attribute to the set of attribute-specific declarations in the active range
3155/// of the pragma.
3156void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
3157 PragmaIntroducerKind Introducer,
3158 Token &FirstToken) {
3159 Token Tok;
3160 PP.Lex(Tok);
3161 auto *Info = new (PP.getPreprocessorAllocator())
3162 PragmaAttributeInfo(AttributesForPragmaAttribute);
3163
Erik Pilkington0876cae2018-12-20 22:32:04 +00003164 // Parse the optional namespace followed by a period.
3165 if (Tok.is(tok::identifier)) {
3166 IdentifierInfo *II = Tok.getIdentifierInfo();
3167 if (!II->isStr("push") && !II->isStr("pop")) {
3168 Info->Namespace = II;
3169 PP.Lex(Tok);
3170
3171 if (!Tok.is(tok::period)) {
3172 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_period)
3173 << II;
3174 return;
3175 }
3176 PP.Lex(Tok);
3177 }
3178 }
3179
Erik Pilkington7d180942018-10-29 17:38:42 +00003180 if (!Tok.isOneOf(tok::identifier, tok::l_paren)) {
3181 PP.Diag(Tok.getLocation(),
3182 diag::err_pragma_attribute_expected_push_pop_paren);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003183 return;
3184 }
Erik Pilkington7d180942018-10-29 17:38:42 +00003185
3186 // Determine what action this pragma clang attribute represents.
Erik Pilkington0876cae2018-12-20 22:32:04 +00003187 if (Tok.is(tok::l_paren)) {
3188 if (Info->Namespace) {
3189 PP.Diag(Tok.getLocation(),
3190 diag::err_pragma_attribute_namespace_on_attribute);
3191 PP.Diag(Tok.getLocation(),
3192 diag::note_pragma_attribute_namespace_on_attribute);
3193 return;
3194 }
Erik Pilkington7d180942018-10-29 17:38:42 +00003195 Info->Action = PragmaAttributeInfo::Attribute;
Erik Pilkington0876cae2018-12-20 22:32:04 +00003196 } else {
Erik Pilkington7d180942018-10-29 17:38:42 +00003197 const IdentifierInfo *II = Tok.getIdentifierInfo();
3198 if (II->isStr("push"))
3199 Info->Action = PragmaAttributeInfo::Push;
3200 else if (II->isStr("pop"))
3201 Info->Action = PragmaAttributeInfo::Pop;
3202 else {
3203 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
3204 << PP.getSpelling(Tok);
3205 return;
3206 }
3207
3208 PP.Lex(Tok);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003209 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003210
3211 // Parse the actual attribute.
Erik Pilkington7d180942018-10-29 17:38:42 +00003212 if ((Info->Action == PragmaAttributeInfo::Push && Tok.isNot(tok::eod)) ||
3213 Info->Action == PragmaAttributeInfo::Attribute) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003214 if (Tok.isNot(tok::l_paren)) {
3215 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
3216 return;
3217 }
3218 PP.Lex(Tok);
3219
3220 // Lex the attribute tokens.
3221 SmallVector<Token, 16> AttributeTokens;
3222 int OpenParens = 1;
3223 while (Tok.isNot(tok::eod)) {
3224 if (Tok.is(tok::l_paren))
3225 OpenParens++;
3226 else if (Tok.is(tok::r_paren)) {
3227 OpenParens--;
3228 if (OpenParens == 0)
3229 break;
3230 }
3231
3232 AttributeTokens.push_back(Tok);
3233 PP.Lex(Tok);
3234 }
3235
3236 if (AttributeTokens.empty()) {
3237 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
3238 return;
3239 }
3240 if (Tok.isNot(tok::r_paren)) {
3241 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
3242 return;
3243 }
3244 SourceLocation EndLoc = Tok.getLocation();
3245 PP.Lex(Tok);
3246
3247 // Terminate the attribute for parsing.
3248 Token EOFTok;
3249 EOFTok.startToken();
3250 EOFTok.setKind(tok::eof);
3251 EOFTok.setLocation(EndLoc);
3252 AttributeTokens.push_back(EOFTok);
3253
3254 Info->Tokens =
3255 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
3256 }
3257
3258 if (Tok.isNot(tok::eod))
3259 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3260 << "clang attribute";
3261
3262 // Generate the annotated pragma token.
3263 auto TokenArray = llvm::make_unique<Token[]>(1);
3264 TokenArray[0].startToken();
3265 TokenArray[0].setKind(tok::annot_pragma_attribute);
3266 TokenArray[0].setLocation(FirstToken.getLocation());
3267 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
3268 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
3269 PP.EnterTokenStream(std::move(TokenArray), 1,
3270 /*DisableMacroExpansion=*/false);
3271}