Bug Summary

File:tools/clang/lib/Parse/ParsePragma.cpp
Warning:line 2864, column 9
Forming reference to null pointer

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ParsePragma.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/clang/lib/Parse -I /build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse -I /build/llvm-toolchain-snapshot-7~svn325118/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn325118/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn325118/build-llvm/tools/clang/lib/Parse -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-02-14-150435-17243-1 -x c++ /build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp
1//===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the language specific #pragma handlers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/Basic/PragmaKinds.h"
16#include "clang/Basic/TargetInfo.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Parse/ParseDiagnostic.h"
19#include "clang/Parse/Parser.h"
20#include "clang/Parse/RAIIObjectsForParser.h"
21#include "clang/Sema/LoopHint.h"
22#include "clang/Sema/Scope.h"
23#include "llvm/ADT/StringSwitch.h"
24using namespace clang;
25
26namespace {
27
28struct PragmaAlignHandler : public PragmaHandler {
29 explicit PragmaAlignHandler() : PragmaHandler("align") {}
30 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
31 Token &FirstToken) override;
32};
33
34struct PragmaGCCVisibilityHandler : public PragmaHandler {
35 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
36 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
37 Token &FirstToken) override;
38};
39
40struct PragmaOptionsHandler : public PragmaHandler {
41 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
42 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
43 Token &FirstToken) override;
44};
45
46struct PragmaPackHandler : public PragmaHandler {
47 explicit PragmaPackHandler() : PragmaHandler("pack") {}
48 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
49 Token &FirstToken) override;
50};
51
52struct PragmaClangSectionHandler : public PragmaHandler {
53 explicit PragmaClangSectionHandler(Sema &S)
54 : PragmaHandler("section"), Actions(S) {}
55 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
56 Token &FirstToken) override;
57private:
58 Sema &Actions;
59};
60
61struct PragmaMSStructHandler : public PragmaHandler {
62 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
63 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
64 Token &FirstToken) override;
65};
66
67struct PragmaUnusedHandler : public PragmaHandler {
68 PragmaUnusedHandler() : PragmaHandler("unused") {}
69 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
70 Token &FirstToken) override;
71};
72
73struct PragmaWeakHandler : public PragmaHandler {
74 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
75 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
76 Token &FirstToken) override;
77};
78
79struct PragmaRedefineExtnameHandler : public PragmaHandler {
80 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
81 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
82 Token &FirstToken) override;
83};
84
85struct PragmaOpenCLExtensionHandler : public PragmaHandler {
86 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
87 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
88 Token &FirstToken) override;
89};
90
91
92struct PragmaFPContractHandler : public PragmaHandler {
93 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
94 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
95 Token &FirstToken) override;
96};
97
98// Pragma STDC implementations.
99
100/// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
101struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
102 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
103
104 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
105 Token &Tok) override {
106 tok::OnOffSwitch OOS;
107 if (PP.LexOnOffSwitch(OOS))
108 return;
109 if (OOS == tok::OOS_ON)
110 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
111 }
112};
113
114/// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
115struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
116 PragmaSTDC_CX_LIMITED_RANGEHandler() : PragmaHandler("CX_LIMITED_RANGE") {}
117
118 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
119 Token &Tok) override {
120 tok::OnOffSwitch OOS;
121 PP.LexOnOffSwitch(OOS);
122 }
123};
124
125/// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
126struct PragmaSTDC_UnknownHandler : public PragmaHandler {
127 PragmaSTDC_UnknownHandler() = default;
128
129 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
130 Token &UnknownTok) override {
131 // C99 6.10.6p2, unknown forms are not allowed.
132 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
133 }
134};
135
136struct PragmaFPHandler : public PragmaHandler {
137 PragmaFPHandler() : PragmaHandler("fp") {}
138 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
139 Token &FirstToken) override;
140};
141
142struct PragmaNoOpenMPHandler : public PragmaHandler {
143 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
144 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
145 Token &FirstToken) override;
146};
147
148struct PragmaOpenMPHandler : public PragmaHandler {
149 PragmaOpenMPHandler() : PragmaHandler("omp") { }
150 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
151 Token &FirstToken) override;
152};
153
154/// PragmaCommentHandler - "\#pragma comment ...".
155struct PragmaCommentHandler : public PragmaHandler {
156 PragmaCommentHandler(Sema &Actions)
157 : PragmaHandler("comment"), Actions(Actions) {}
158 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
159 Token &FirstToken) override;
160private:
161 Sema &Actions;
162};
163
164struct PragmaDetectMismatchHandler : public PragmaHandler {
165 PragmaDetectMismatchHandler(Sema &Actions)
166 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
167 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
168 Token &FirstToken) override;
169private:
170 Sema &Actions;
171};
172
173struct PragmaMSPointersToMembers : public PragmaHandler {
174 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
175 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
176 Token &FirstToken) override;
177};
178
179struct PragmaMSVtorDisp : public PragmaHandler {
180 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
181 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
182 Token &FirstToken) override;
183};
184
185struct PragmaMSPragma : public PragmaHandler {
186 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
187 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
188 Token &FirstToken) override;
189};
190
191/// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
192struct PragmaOptimizeHandler : public PragmaHandler {
193 PragmaOptimizeHandler(Sema &S)
194 : PragmaHandler("optimize"), Actions(S) {}
195 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
196 Token &FirstToken) override;
197private:
198 Sema &Actions;
199};
200
201struct PragmaLoopHintHandler : public PragmaHandler {
202 PragmaLoopHintHandler() : PragmaHandler("loop") {}
203 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
204 Token &FirstToken) override;
205};
206
207struct PragmaUnrollHintHandler : public PragmaHandler {
208 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
209 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
210 Token &FirstToken) override;
211};
212
213struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
214 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
215};
216
217struct PragmaMSIntrinsicHandler : public PragmaHandler {
218 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
219 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
220 Token &FirstToken) override;
221};
222
223struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
224 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
225 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
226 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
227 Token &FirstToken) override;
228
229private:
230 Sema &Actions;
231};
232
233/// PragmaAttributeHandler - "\#pragma clang attribute ...".
234struct PragmaAttributeHandler : public PragmaHandler {
235 PragmaAttributeHandler(AttributeFactory &AttrFactory)
236 : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {}
237 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
238 Token &FirstToken) override;
239
240 /// A pool of attributes that were parsed in \#pragma clang attribute.
241 ParsedAttributes AttributesForPragmaAttribute;
242};
243
244} // end namespace
245
246void Parser::initializePragmaHandlers() {
247 AlignHandler.reset(new PragmaAlignHandler());
248 PP.AddPragmaHandler(AlignHandler.get());
249
250 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
251 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
252
253 OptionsHandler.reset(new PragmaOptionsHandler());
254 PP.AddPragmaHandler(OptionsHandler.get());
255
256 PackHandler.reset(new PragmaPackHandler());
257 PP.AddPragmaHandler(PackHandler.get());
258
259 MSStructHandler.reset(new PragmaMSStructHandler());
260 PP.AddPragmaHandler(MSStructHandler.get());
261
262 UnusedHandler.reset(new PragmaUnusedHandler());
263 PP.AddPragmaHandler(UnusedHandler.get());
264
265 WeakHandler.reset(new PragmaWeakHandler());
266 PP.AddPragmaHandler(WeakHandler.get());
267
268 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
269 PP.AddPragmaHandler(RedefineExtnameHandler.get());
270
271 FPContractHandler.reset(new PragmaFPContractHandler());
272 PP.AddPragmaHandler("STDC", FPContractHandler.get());
273
274 STDCFENVHandler.reset(new PragmaSTDC_FENV_ACCESSHandler());
275 PP.AddPragmaHandler("STDC", STDCFENVHandler.get());
276
277 STDCCXLIMITHandler.reset(new PragmaSTDC_CX_LIMITED_RANGEHandler());
278 PP.AddPragmaHandler("STDC", STDCCXLIMITHandler.get());
279
280 STDCUnknownHandler.reset(new PragmaSTDC_UnknownHandler());
281 PP.AddPragmaHandler("STDC", STDCUnknownHandler.get());
282
283 PCSectionHandler.reset(new PragmaClangSectionHandler(Actions));
284 PP.AddPragmaHandler("clang", PCSectionHandler.get());
285
286 if (getLangOpts().OpenCL) {
287 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
288 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
289
290 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
291 }
292 if (getLangOpts().OpenMP)
293 OpenMPHandler.reset(new PragmaOpenMPHandler());
294 else
295 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
296 PP.AddPragmaHandler(OpenMPHandler.get());
297
298 if (getLangOpts().MicrosoftExt ||
299 getTargetInfo().getTriple().isOSBinFormatELF()) {
300 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
301 PP.AddPragmaHandler(MSCommentHandler.get());
302 }
303
304 if (getLangOpts().MicrosoftExt) {
305 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
306 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
307 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
308 PP.AddPragmaHandler(MSPointersToMembers.get());
309 MSVtorDisp.reset(new PragmaMSVtorDisp());
310 PP.AddPragmaHandler(MSVtorDisp.get());
311 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
312 PP.AddPragmaHandler(MSInitSeg.get());
313 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
314 PP.AddPragmaHandler(MSDataSeg.get());
315 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
316 PP.AddPragmaHandler(MSBSSSeg.get());
317 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
318 PP.AddPragmaHandler(MSConstSeg.get());
319 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
320 PP.AddPragmaHandler(MSCodeSeg.get());
321 MSSection.reset(new PragmaMSPragma("section"));
322 PP.AddPragmaHandler(MSSection.get());
323 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
324 PP.AddPragmaHandler(MSRuntimeChecks.get());
325 MSIntrinsic.reset(new PragmaMSIntrinsicHandler());
326 PP.AddPragmaHandler(MSIntrinsic.get());
327 }
328
329 if (getLangOpts().CUDA) {
330 CUDAForceHostDeviceHandler.reset(
331 new PragmaForceCUDAHostDeviceHandler(Actions));
332 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
333 }
334
335 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
336 PP.AddPragmaHandler("clang", OptimizeHandler.get());
337
338 LoopHintHandler.reset(new PragmaLoopHintHandler());
339 PP.AddPragmaHandler("clang", LoopHintHandler.get());
340
341 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
342 PP.AddPragmaHandler(UnrollHintHandler.get());
343
344 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
345 PP.AddPragmaHandler(NoUnrollHintHandler.get());
346
347 FPHandler.reset(new PragmaFPHandler());
348 PP.AddPragmaHandler("clang", FPHandler.get());
349
350 AttributePragmaHandler.reset(new PragmaAttributeHandler(AttrFactory));
351 PP.AddPragmaHandler("clang", AttributePragmaHandler.get());
352}
353
354void Parser::resetPragmaHandlers() {
355 // Remove the pragma handlers we installed.
356 PP.RemovePragmaHandler(AlignHandler.get());
357 AlignHandler.reset();
358 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
359 GCCVisibilityHandler.reset();
360 PP.RemovePragmaHandler(OptionsHandler.get());
361 OptionsHandler.reset();
362 PP.RemovePragmaHandler(PackHandler.get());
363 PackHandler.reset();
364 PP.RemovePragmaHandler(MSStructHandler.get());
365 MSStructHandler.reset();
366 PP.RemovePragmaHandler(UnusedHandler.get());
367 UnusedHandler.reset();
368 PP.RemovePragmaHandler(WeakHandler.get());
369 WeakHandler.reset();
370 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
371 RedefineExtnameHandler.reset();
372
373 if (getLangOpts().OpenCL) {
374 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
375 OpenCLExtensionHandler.reset();
376 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
377 }
378 PP.RemovePragmaHandler(OpenMPHandler.get());
379 OpenMPHandler.reset();
380
381 if (getLangOpts().MicrosoftExt ||
382 getTargetInfo().getTriple().isOSBinFormatELF()) {
383 PP.RemovePragmaHandler(MSCommentHandler.get());
384 MSCommentHandler.reset();
385 }
386
387 PP.RemovePragmaHandler("clang", PCSectionHandler.get());
388 PCSectionHandler.reset();
389
390 if (getLangOpts().MicrosoftExt) {
391 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
392 MSDetectMismatchHandler.reset();
393 PP.RemovePragmaHandler(MSPointersToMembers.get());
394 MSPointersToMembers.reset();
395 PP.RemovePragmaHandler(MSVtorDisp.get());
396 MSVtorDisp.reset();
397 PP.RemovePragmaHandler(MSInitSeg.get());
398 MSInitSeg.reset();
399 PP.RemovePragmaHandler(MSDataSeg.get());
400 MSDataSeg.reset();
401 PP.RemovePragmaHandler(MSBSSSeg.get());
402 MSBSSSeg.reset();
403 PP.RemovePragmaHandler(MSConstSeg.get());
404 MSConstSeg.reset();
405 PP.RemovePragmaHandler(MSCodeSeg.get());
406 MSCodeSeg.reset();
407 PP.RemovePragmaHandler(MSSection.get());
408 MSSection.reset();
409 PP.RemovePragmaHandler(MSRuntimeChecks.get());
410 MSRuntimeChecks.reset();
411 PP.RemovePragmaHandler(MSIntrinsic.get());
412 MSIntrinsic.reset();
413 }
414
415 if (getLangOpts().CUDA) {
416 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
417 CUDAForceHostDeviceHandler.reset();
418 }
419
420 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
421 FPContractHandler.reset();
422
423 PP.RemovePragmaHandler("STDC", STDCFENVHandler.get());
424 STDCFENVHandler.reset();
425
426 PP.RemovePragmaHandler("STDC", STDCCXLIMITHandler.get());
427 STDCCXLIMITHandler.reset();
428
429 PP.RemovePragmaHandler("STDC", STDCUnknownHandler.get());
430 STDCUnknownHandler.reset();
431
432 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
433 OptimizeHandler.reset();
434
435 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
436 LoopHintHandler.reset();
437
438 PP.RemovePragmaHandler(UnrollHintHandler.get());
439 UnrollHintHandler.reset();
440
441 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
442 NoUnrollHintHandler.reset();
443
444 PP.RemovePragmaHandler("clang", FPHandler.get());
445 FPHandler.reset();
446
447 PP.RemovePragmaHandler("clang", AttributePragmaHandler.get());
448 AttributePragmaHandler.reset();
449}
450
451/// \brief Handle the annotation token produced for #pragma unused(...)
452///
453/// Each annot_pragma_unused is followed by the argument token so e.g.
454/// "#pragma unused(x,y)" becomes:
455/// annot_pragma_unused 'x' annot_pragma_unused 'y'
456void Parser::HandlePragmaUnused() {
457 assert(Tok.is(tok::annot_pragma_unused))(static_cast <bool> (Tok.is(tok::annot_pragma_unused)) ?
void (0) : __assert_fail ("Tok.is(tok::annot_pragma_unused)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 457, __extension__ __PRETTY_FUNCTION__))
;
458 SourceLocation UnusedLoc = ConsumeAnnotationToken();
459 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
460 ConsumeToken(); // The argument token.
461}
462
463void Parser::HandlePragmaVisibility() {
464 assert(Tok.is(tok::annot_pragma_vis))(static_cast <bool> (Tok.is(tok::annot_pragma_vis)) ? void
(0) : __assert_fail ("Tok.is(tok::annot_pragma_vis)", "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 464, __extension__ __PRETTY_FUNCTION__))
;
465 const IdentifierInfo *VisType =
466 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
467 SourceLocation VisLoc = ConsumeAnnotationToken();
468 Actions.ActOnPragmaVisibility(VisType, VisLoc);
469}
470
471namespace {
472struct PragmaPackInfo {
473 Sema::PragmaMsStackAction Action;
474 StringRef SlotLabel;
475 Token Alignment;
476};
477} // end anonymous namespace
478
479void Parser::HandlePragmaPack() {
480 assert(Tok.is(tok::annot_pragma_pack))(static_cast <bool> (Tok.is(tok::annot_pragma_pack)) ? void
(0) : __assert_fail ("Tok.is(tok::annot_pragma_pack)", "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 480, __extension__ __PRETTY_FUNCTION__))
;
481 PragmaPackInfo *Info =
482 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
483 SourceLocation PragmaLoc = Tok.getLocation();
484 ExprResult Alignment;
485 if (Info->Alignment.is(tok::numeric_constant)) {
486 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
487 if (Alignment.isInvalid()) {
488 ConsumeAnnotationToken();
489 return;
490 }
491 }
492 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
493 Alignment.get());
494 // Consume the token after processing the pragma to enable pragma-specific
495 // #include warnings.
496 ConsumeAnnotationToken();
497}
498
499void Parser::HandlePragmaMSStruct() {
500 assert(Tok.is(tok::annot_pragma_msstruct))(static_cast <bool> (Tok.is(tok::annot_pragma_msstruct)
) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_msstruct)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 500, __extension__ __PRETTY_FUNCTION__))
;
501 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
502 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
503 Actions.ActOnPragmaMSStruct(Kind);
504 ConsumeAnnotationToken();
505}
506
507void Parser::HandlePragmaAlign() {
508 assert(Tok.is(tok::annot_pragma_align))(static_cast <bool> (Tok.is(tok::annot_pragma_align)) ?
void (0) : __assert_fail ("Tok.is(tok::annot_pragma_align)",
"/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 508, __extension__ __PRETTY_FUNCTION__))
;
509 Sema::PragmaOptionsAlignKind Kind =
510 static_cast<Sema::PragmaOptionsAlignKind>(
511 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
512 Actions.ActOnPragmaOptionsAlign(Kind, Tok.getLocation());
513 // Consume the token after processing the pragma to enable pragma-specific
514 // #include warnings.
515 ConsumeAnnotationToken();
516}
517
518void Parser::HandlePragmaDump() {
519 assert(Tok.is(tok::annot_pragma_dump))(static_cast <bool> (Tok.is(tok::annot_pragma_dump)) ? void
(0) : __assert_fail ("Tok.is(tok::annot_pragma_dump)", "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 519, __extension__ __PRETTY_FUNCTION__))
;
520 IdentifierInfo *II =
521 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
522 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
523 ConsumeAnnotationToken();
524}
525
526void Parser::HandlePragmaWeak() {
527 assert(Tok.is(tok::annot_pragma_weak))(static_cast <bool> (Tok.is(tok::annot_pragma_weak)) ? void
(0) : __assert_fail ("Tok.is(tok::annot_pragma_weak)", "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 527, __extension__ __PRETTY_FUNCTION__))
;
528 SourceLocation PragmaLoc = ConsumeAnnotationToken();
529 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
530 Tok.getLocation());
531 ConsumeToken(); // The weak name.
532}
533
534void Parser::HandlePragmaWeakAlias() {
535 assert(Tok.is(tok::annot_pragma_weakalias))(static_cast <bool> (Tok.is(tok::annot_pragma_weakalias
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_weakalias)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 535, __extension__ __PRETTY_FUNCTION__))
;
536 SourceLocation PragmaLoc = ConsumeAnnotationToken();
537 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
538 SourceLocation WeakNameLoc = Tok.getLocation();
539 ConsumeToken();
540 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
541 SourceLocation AliasNameLoc = Tok.getLocation();
542 ConsumeToken();
543 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
544 WeakNameLoc, AliasNameLoc);
545
546}
547
548void Parser::HandlePragmaRedefineExtname() {
549 assert(Tok.is(tok::annot_pragma_redefine_extname))(static_cast <bool> (Tok.is(tok::annot_pragma_redefine_extname
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_redefine_extname)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 549, __extension__ __PRETTY_FUNCTION__))
;
550 SourceLocation RedefLoc = ConsumeAnnotationToken();
551 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
552 SourceLocation RedefNameLoc = Tok.getLocation();
553 ConsumeToken();
554 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
555 SourceLocation AliasNameLoc = Tok.getLocation();
556 ConsumeToken();
557 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
558 RedefNameLoc, AliasNameLoc);
559}
560
561void Parser::HandlePragmaFPContract() {
562 assert(Tok.is(tok::annot_pragma_fp_contract))(static_cast <bool> (Tok.is(tok::annot_pragma_fp_contract
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_fp_contract)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 562, __extension__ __PRETTY_FUNCTION__))
;
563 tok::OnOffSwitch OOS =
564 static_cast<tok::OnOffSwitch>(
565 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
566
567 LangOptions::FPContractModeKind FPC;
568 switch (OOS) {
569 case tok::OOS_ON:
570 FPC = LangOptions::FPC_On;
571 break;
572 case tok::OOS_OFF:
573 FPC = LangOptions::FPC_Off;
574 break;
575 case tok::OOS_DEFAULT:
576 FPC = getLangOpts().getDefaultFPContractMode();
577 break;
578 }
579
580 Actions.ActOnPragmaFPContract(FPC);
581 ConsumeAnnotationToken();
582}
583
584StmtResult Parser::HandlePragmaCaptured()
585{
586 assert(Tok.is(tok::annot_pragma_captured))(static_cast <bool> (Tok.is(tok::annot_pragma_captured)
) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_captured)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 586, __extension__ __PRETTY_FUNCTION__))
;
587 ConsumeAnnotationToken();
588
589 if (Tok.isNot(tok::l_brace)) {
590 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
591 return StmtError();
592 }
593
594 SourceLocation Loc = Tok.getLocation();
595
596 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope |
597 Scope::CompoundStmtScope);
598 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
599 /*NumParams=*/1);
600
601 StmtResult R = ParseCompoundStatement();
602 CapturedRegionScope.Exit();
603
604 if (R.isInvalid()) {
605 Actions.ActOnCapturedRegionError();
606 return StmtError();
607 }
608
609 return Actions.ActOnCapturedRegionEnd(R.get());
610}
611
612namespace {
613 enum OpenCLExtState : char {
614 Disable, Enable, Begin, End
615 };
616 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
617}
618
619void Parser::HandlePragmaOpenCLExtension() {
620 assert(Tok.is(tok::annot_pragma_opencl_extension))(static_cast <bool> (Tok.is(tok::annot_pragma_opencl_extension
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_opencl_extension)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 620, __extension__ __PRETTY_FUNCTION__))
;
621 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
622 auto State = Data->second;
623 auto Ident = Data->first;
624 SourceLocation NameLoc = Tok.getLocation();
625 ConsumeAnnotationToken();
626
627 auto &Opt = Actions.getOpenCLOptions();
628 auto Name = Ident->getName();
629 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
630 // overriding all previously issued extension directives, but only if the
631 // behavior is set to disable."
632 if (Name == "all") {
633 if (State == Disable) {
634 Opt.disableAll();
635 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
636 } else {
637 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
638 }
639 } else if (State == Begin) {
640 if (!Opt.isKnown(Name) ||
641 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
642 Opt.support(Name);
643 }
644 Actions.setCurrentOpenCLExtension(Name);
645 } else if (State == End) {
646 if (Name != Actions.getCurrentOpenCLExtension())
647 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
648 Actions.setCurrentOpenCLExtension("");
649 } else if (!Opt.isKnown(Name))
650 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
651 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
652 Opt.enable(Name, State == Enable);
653 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
654 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
655 else
656 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
657}
658
659void Parser::HandlePragmaMSPointersToMembers() {
660 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members))(static_cast <bool> (Tok.is(tok::annot_pragma_ms_pointers_to_members
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_ms_pointers_to_members)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 660, __extension__ __PRETTY_FUNCTION__))
;
661 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
662 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
663 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
664 SourceLocation PragmaLoc = ConsumeAnnotationToken();
665 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
666}
667
668void Parser::HandlePragmaMSVtorDisp() {
669 assert(Tok.is(tok::annot_pragma_ms_vtordisp))(static_cast <bool> (Tok.is(tok::annot_pragma_ms_vtordisp
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_ms_vtordisp)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 669, __extension__ __PRETTY_FUNCTION__))
;
670 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
671 Sema::PragmaMsStackAction Action =
672 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
673 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
674 SourceLocation PragmaLoc = ConsumeAnnotationToken();
675 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
676}
677
678void Parser::HandlePragmaMSPragma() {
679 assert(Tok.is(tok::annot_pragma_ms_pragma))(static_cast <bool> (Tok.is(tok::annot_pragma_ms_pragma
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_ms_pragma)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 679, __extension__ __PRETTY_FUNCTION__))
;
680 // Grab the tokens out of the annotation and enter them into the stream.
681 auto TheTokens =
682 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
683 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
684 SourceLocation PragmaLocation = ConsumeAnnotationToken();
685 assert(Tok.isAnyIdentifier())(static_cast <bool> (Tok.isAnyIdentifier()) ? void (0) :
__assert_fail ("Tok.isAnyIdentifier()", "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 685, __extension__ __PRETTY_FUNCTION__))
;
686 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
687 PP.Lex(Tok); // pragma kind
688
689 // Figure out which #pragma we're dealing with. The switch has no default
690 // because lex shouldn't emit the annotation token for unrecognized pragmas.
691 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
692 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
693 .Case("data_seg", &Parser::HandlePragmaMSSegment)
694 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
695 .Case("const_seg", &Parser::HandlePragmaMSSegment)
696 .Case("code_seg", &Parser::HandlePragmaMSSegment)
697 .Case("section", &Parser::HandlePragmaMSSection)
698 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
699
700 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
701 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
702 // until eof (really end of line) to prevent follow-on errors.
703 while (Tok.isNot(tok::eof))
704 PP.Lex(Tok);
705 PP.Lex(Tok);
706 }
707}
708
709bool Parser::HandlePragmaMSSection(StringRef PragmaName,
710 SourceLocation PragmaLocation) {
711 if (Tok.isNot(tok::l_paren)) {
712 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
713 return false;
714 }
715 PP.Lex(Tok); // (
716 // Parsing code for pragma section
717 if (Tok.isNot(tok::string_literal)) {
718 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
719 << PragmaName;
720 return false;
721 }
722 ExprResult StringResult = ParseStringLiteralExpression();
723 if (StringResult.isInvalid())
724 return false; // Already diagnosed.
725 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
726 if (SegmentName->getCharByteWidth() != 1) {
727 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
728 << PragmaName;
729 return false;
730 }
731 int SectionFlags = ASTContext::PSF_Read;
732 bool SectionFlagsAreDefault = true;
733 while (Tok.is(tok::comma)) {
734 PP.Lex(Tok); // ,
735 // Ignore "long" and "short".
736 // They are undocumented, but widely used, section attributes which appear
737 // to do nothing.
738 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
739 PP.Lex(Tok); // long/short
740 continue;
741 }
742
743 if (!Tok.isAnyIdentifier()) {
744 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
745 << PragmaName;
746 return false;
747 }
748 ASTContext::PragmaSectionFlag Flag =
749 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
750 Tok.getIdentifierInfo()->getName())
751 .Case("read", ASTContext::PSF_Read)
752 .Case("write", ASTContext::PSF_Write)
753 .Case("execute", ASTContext::PSF_Execute)
754 .Case("shared", ASTContext::PSF_Invalid)
755 .Case("nopage", ASTContext::PSF_Invalid)
756 .Case("nocache", ASTContext::PSF_Invalid)
757 .Case("discard", ASTContext::PSF_Invalid)
758 .Case("remove", ASTContext::PSF_Invalid)
759 .Default(ASTContext::PSF_None);
760 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
761 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
762 ? diag::warn_pragma_invalid_specific_action
763 : diag::warn_pragma_unsupported_action)
764 << PragmaName << Tok.getIdentifierInfo()->getName();
765 return false;
766 }
767 SectionFlags |= Flag;
768 SectionFlagsAreDefault = false;
769 PP.Lex(Tok); // Identifier
770 }
771 // If no section attributes are specified, the section will be marked as
772 // read/write.
773 if (SectionFlagsAreDefault)
774 SectionFlags |= ASTContext::PSF_Write;
775 if (Tok.isNot(tok::r_paren)) {
776 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
777 return false;
778 }
779 PP.Lex(Tok); // )
780 if (Tok.isNot(tok::eof)) {
781 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
782 << PragmaName;
783 return false;
784 }
785 PP.Lex(Tok); // eof
786 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
787 return true;
788}
789
790bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
791 SourceLocation PragmaLocation) {
792 if (Tok.isNot(tok::l_paren)) {
793 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
794 return false;
795 }
796 PP.Lex(Tok); // (
797 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
798 StringRef SlotLabel;
799 if (Tok.isAnyIdentifier()) {
800 StringRef PushPop = Tok.getIdentifierInfo()->getName();
801 if (PushPop == "push")
802 Action = Sema::PSK_Push;
803 else if (PushPop == "pop")
804 Action = Sema::PSK_Pop;
805 else {
806 PP.Diag(PragmaLocation,
807 diag::warn_pragma_expected_section_push_pop_or_name)
808 << PragmaName;
809 return false;
810 }
811 if (Action != Sema::PSK_Reset) {
812 PP.Lex(Tok); // push | pop
813 if (Tok.is(tok::comma)) {
814 PP.Lex(Tok); // ,
815 // If we've got a comma, we either need a label or a string.
816 if (Tok.isAnyIdentifier()) {
817 SlotLabel = Tok.getIdentifierInfo()->getName();
818 PP.Lex(Tok); // identifier
819 if (Tok.is(tok::comma))
820 PP.Lex(Tok);
821 else if (Tok.isNot(tok::r_paren)) {
822 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
823 << PragmaName;
824 return false;
825 }
826 }
827 } else if (Tok.isNot(tok::r_paren)) {
828 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
829 return false;
830 }
831 }
832 }
833 // Grab the string literal for our section name.
834 StringLiteral *SegmentName = nullptr;
835 if (Tok.isNot(tok::r_paren)) {
836 if (Tok.isNot(tok::string_literal)) {
837 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
838 diag::warn_pragma_expected_section_name :
839 diag::warn_pragma_expected_section_label_or_name :
840 diag::warn_pragma_expected_section_push_pop_or_name;
841 PP.Diag(PragmaLocation, DiagID) << PragmaName;
842 return false;
843 }
844 ExprResult StringResult = ParseStringLiteralExpression();
845 if (StringResult.isInvalid())
846 return false; // Already diagnosed.
847 SegmentName = cast<StringLiteral>(StringResult.get());
848 if (SegmentName->getCharByteWidth() != 1) {
849 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
850 << PragmaName;
851 return false;
852 }
853 // Setting section "" has no effect
854 if (SegmentName->getLength())
855 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
856 }
857 if (Tok.isNot(tok::r_paren)) {
858 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
859 return false;
860 }
861 PP.Lex(Tok); // )
862 if (Tok.isNot(tok::eof)) {
863 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
864 << PragmaName;
865 return false;
866 }
867 PP.Lex(Tok); // eof
868 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
869 SegmentName, PragmaName);
870 return true;
871}
872
873// #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
874bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
875 SourceLocation PragmaLocation) {
876 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
877 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
878 return false;
879 }
880
881 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
882 PragmaName))
883 return false;
884
885 // Parse either the known section names or the string section name.
886 StringLiteral *SegmentName = nullptr;
887 if (Tok.isAnyIdentifier()) {
888 auto *II = Tok.getIdentifierInfo();
889 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
890 .Case("compiler", "\".CRT$XCC\"")
891 .Case("lib", "\".CRT$XCL\"")
892 .Case("user", "\".CRT$XCU\"")
893 .Default("");
894
895 if (!Section.empty()) {
896 // Pretend the user wrote the appropriate string literal here.
897 Token Toks[1];
898 Toks[0].startToken();
899 Toks[0].setKind(tok::string_literal);
900 Toks[0].setLocation(Tok.getLocation());
901 Toks[0].setLiteralData(Section.data());
902 Toks[0].setLength(Section.size());
903 SegmentName =
904 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
905 PP.Lex(Tok);
906 }
907 } else if (Tok.is(tok::string_literal)) {
908 ExprResult StringResult = ParseStringLiteralExpression();
909 if (StringResult.isInvalid())
910 return false;
911 SegmentName = cast<StringLiteral>(StringResult.get());
912 if (SegmentName->getCharByteWidth() != 1) {
913 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
914 << PragmaName;
915 return false;
916 }
917 // FIXME: Add support for the '[, func-name]' part of the pragma.
918 }
919
920 if (!SegmentName) {
921 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
922 return false;
923 }
924
925 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
926 PragmaName) ||
927 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
928 PragmaName))
929 return false;
930
931 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
932 return true;
933}
934
935namespace {
936struct PragmaLoopHintInfo {
937 Token PragmaName;
938 Token Option;
939 ArrayRef<Token> Toks;
940};
941} // end anonymous namespace
942
943static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
944 std::string PragmaString;
945 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
946 PragmaString = "clang loop ";
947 PragmaString += Option.getIdentifierInfo()->getName();
948 } else {
949 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&(static_cast <bool> (PragmaName.getIdentifierInfo()->
getName() == "unroll" && "Unexpected pragma name") ? void
(0) : __assert_fail ("PragmaName.getIdentifierInfo()->getName() == \"unroll\" && \"Unexpected pragma name\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 950, __extension__ __PRETTY_FUNCTION__))
950 "Unexpected pragma name")(static_cast <bool> (PragmaName.getIdentifierInfo()->
getName() == "unroll" && "Unexpected pragma name") ? void
(0) : __assert_fail ("PragmaName.getIdentifierInfo()->getName() == \"unroll\" && \"Unexpected pragma name\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 950, __extension__ __PRETTY_FUNCTION__))
;
951 PragmaString = "unroll";
952 }
953 return PragmaString;
954}
955
956bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
957 assert(Tok.is(tok::annot_pragma_loop_hint))(static_cast <bool> (Tok.is(tok::annot_pragma_loop_hint
)) ? void (0) : __assert_fail ("Tok.is(tok::annot_pragma_loop_hint)"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 957, __extension__ __PRETTY_FUNCTION__))
;
958 PragmaLoopHintInfo *Info =
959 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
960
961 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
962 Hint.PragmaNameLoc = IdentifierLoc::create(
963 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
964
965 // It is possible that the loop hint has no option identifier, such as
966 // #pragma unroll(4).
967 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
968 ? Info->Option.getIdentifierInfo()
969 : nullptr;
970 Hint.OptionLoc = IdentifierLoc::create(
971 Actions.Context, Info->Option.getLocation(), OptionInfo);
972
973 llvm::ArrayRef<Token> Toks = Info->Toks;
974
975 // Return a valid hint if pragma unroll or nounroll were specified
976 // without an argument.
977 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
978 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
979 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
980 ConsumeAnnotationToken();
981 Hint.Range = Info->PragmaName.getLocation();
982 return true;
983 }
984
985 // The constant expression is always followed by an eof token, which increases
986 // the TokSize by 1.
987 assert(!Toks.empty() &&(static_cast <bool> (!Toks.empty() && "PragmaLoopHintInfo::Toks must contain at least one token."
) ? void (0) : __assert_fail ("!Toks.empty() && \"PragmaLoopHintInfo::Toks must contain at least one token.\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 988, __extension__ __PRETTY_FUNCTION__))
988 "PragmaLoopHintInfo::Toks must contain at least one token.")(static_cast <bool> (!Toks.empty() && "PragmaLoopHintInfo::Toks must contain at least one token."
) ? void (0) : __assert_fail ("!Toks.empty() && \"PragmaLoopHintInfo::Toks must contain at least one token.\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 988, __extension__ __PRETTY_FUNCTION__))
;
989
990 // If no option is specified the argument is assumed to be a constant expr.
991 bool OptionUnroll = false;
992 bool OptionDistribute = false;
993 bool StateOption = false;
994 if (OptionInfo) { // Pragma Unroll does not specify an option.
995 OptionUnroll = OptionInfo->isStr("unroll");
996 OptionDistribute = OptionInfo->isStr("distribute");
997 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
998 .Case("vectorize", true)
999 .Case("interleave", true)
1000 .Default(false) ||
1001 OptionUnroll || OptionDistribute;
1002 }
1003
1004 bool AssumeSafetyArg = !OptionUnroll && !OptionDistribute;
1005 // Verify loop hint has an argument.
1006 if (Toks[0].is(tok::eof)) {
1007 ConsumeAnnotationToken();
1008 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
1009 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll
1010 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
1011 return false;
1012 }
1013
1014 // Validate the argument.
1015 if (StateOption) {
1016 ConsumeAnnotationToken();
1017 SourceLocation StateLoc = Toks[0].getLocation();
1018 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
1019
1020 bool Valid = StateInfo &&
1021 llvm::StringSwitch<bool>(StateInfo->getName())
1022 .Cases("enable", "disable", true)
1023 .Case("full", OptionUnroll)
1024 .Case("assume_safety", AssumeSafetyArg)
1025 .Default(false);
1026 if (!Valid) {
1027 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
1028 << /*FullKeyword=*/OptionUnroll
1029 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
1030 return false;
1031 }
1032 if (Toks.size() > 2)
1033 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1034 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1035 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
1036 } else {
1037 // Enter constant expression including eof terminator into token stream.
1038 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
1039 ConsumeAnnotationToken();
1040
1041 ExprResult R = ParseConstantExpression();
1042
1043 // Tokens following an error in an ill-formed constant expression will
1044 // remain in the token stream and must be removed.
1045 if (Tok.isNot(tok::eof)) {
1046 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1047 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1048 while (Tok.isNot(tok::eof))
1049 ConsumeAnyToken();
1050 }
1051
1052 ConsumeToken(); // Consume the constant expression eof terminator.
1053
1054 if (R.isInvalid() ||
1055 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
1056 return false;
1057
1058 // Argument is a constant expression with an integer type.
1059 Hint.ValueExpr = R.get();
1060 }
1061
1062 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
1063 Info->Toks.back().getLocation());
1064 return true;
1065}
1066
1067namespace {
1068struct PragmaAttributeInfo {
1069 enum ActionType { Push, Pop };
1070 ParsedAttributes &Attributes;
1071 ActionType Action;
1072 ArrayRef<Token> Tokens;
1073
1074 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1075};
1076
1077#include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1078
1079} // end anonymous namespace
1080
1081static StringRef getIdentifier(const Token &Tok) {
1082 if (Tok.is(tok::identifier))
1083 return Tok.getIdentifierInfo()->getName();
1084 const char *S = tok::getKeywordSpelling(Tok.getKind());
1085 if (!S)
1086 return "";
1087 return S;
1088}
1089
1090static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1091 using namespace attr;
1092 switch (Rule) {
1093#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1094 case Value: \
1095 return IsAbstract;
1096#include "clang/Basic/AttrSubMatchRulesList.inc"
1097 }
1098 llvm_unreachable("Invalid attribute subject match rule")::llvm::llvm_unreachable_internal("Invalid attribute subject match rule"
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 1098)
;
1099 return false;
1100}
1101
1102static void diagnoseExpectedAttributeSubjectSubRule(
1103 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1104 SourceLocation SubRuleLoc) {
1105 auto Diagnostic =
1106 PRef.Diag(SubRuleLoc,
1107 diag::err_pragma_attribute_expected_subject_sub_identifier)
1108 << PrimaryRuleName;
1109 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1110 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1111 else
1112 Diagnostic << /*SubRulesSupported=*/0;
1113}
1114
1115static void diagnoseUnknownAttributeSubjectSubRule(
1116 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1117 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1118
1119 auto Diagnostic =
1120 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1121 << SubRuleName << PrimaryRuleName;
1122 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1123 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1124 else
1125 Diagnostic << /*SubRulesSupported=*/0;
1126}
1127
1128bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1129 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1130 SourceLocation &LastMatchRuleEndLoc) {
1131 bool IsAny = false;
1132 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1133 if (getIdentifier(Tok) == "any") {
1134 AnyLoc = ConsumeToken();
1135 IsAny = true;
1136 if (AnyParens.expectAndConsume())
1137 return true;
1138 }
1139
1140 do {
1141 // Parse the subject matcher rule.
1142 StringRef Name = getIdentifier(Tok);
1143 if (Name.empty()) {
1144 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1145 return true;
1146 }
1147 std::pair<Optional<attr::SubjectMatchRule>,
1148 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1149 Rule = isAttributeSubjectMatchRule(Name);
1150 if (!Rule.first) {
1151 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1152 return true;
1153 }
1154 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1155 SourceLocation RuleLoc = ConsumeToken();
1156
1157 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1158 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1159 if (Parens.expectAndConsume())
1160 return true;
1161 } else if (Parens.consumeOpen()) {
1162 if (!SubjectMatchRules
1163 .insert(
1164 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1165 .second)
1166 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1167 << Name
1168 << FixItHint::CreateRemoval(SourceRange(
1169 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1170 LastMatchRuleEndLoc = RuleLoc;
1171 continue;
1172 }
1173
1174 // Parse the sub-rules.
1175 StringRef SubRuleName = getIdentifier(Tok);
1176 if (SubRuleName.empty()) {
1177 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1178 Tok.getLocation());
1179 return true;
1180 }
1181 attr::SubjectMatchRule SubRule;
1182 if (SubRuleName == "unless") {
1183 SourceLocation SubRuleLoc = ConsumeToken();
1184 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1185 if (Parens.expectAndConsume())
1186 return true;
1187 SubRuleName = getIdentifier(Tok);
1188 if (SubRuleName.empty()) {
1189 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1190 SubRuleLoc);
1191 return true;
1192 }
1193 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1194 if (!SubRuleOrNone) {
1195 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1196 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1197 SubRuleUnlessName, SubRuleLoc);
1198 return true;
1199 }
1200 SubRule = *SubRuleOrNone;
1201 ConsumeToken();
1202 if (Parens.consumeClose())
1203 return true;
1204 } else {
1205 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1206 if (!SubRuleOrNone) {
1207 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1208 SubRuleName, Tok.getLocation());
1209 return true;
1210 }
1211 SubRule = *SubRuleOrNone;
1212 ConsumeToken();
1213 }
1214 SourceLocation RuleEndLoc = Tok.getLocation();
1215 LastMatchRuleEndLoc = RuleEndLoc;
1216 if (Parens.consumeClose())
1217 return true;
1218 if (!SubjectMatchRules
1219 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1220 .second) {
1221 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1222 << attr::getSubjectMatchRuleSpelling(SubRule)
1223 << FixItHint::CreateRemoval(SourceRange(
1224 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1225 continue;
1226 }
1227 } while (IsAny && TryConsumeToken(tok::comma));
1228
1229 if (IsAny)
1230 if (AnyParens.consumeClose())
1231 return true;
1232
1233 return false;
1234}
1235
1236namespace {
1237
1238/// Describes the stage at which attribute subject rule parsing was interruped.
1239enum class MissingAttributeSubjectRulesRecoveryPoint {
1240 Comma,
1241 ApplyTo,
1242 Equals,
1243 Any,
1244 None,
1245};
1246
1247MissingAttributeSubjectRulesRecoveryPoint
1248getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1249 if (const auto *II = Tok.getIdentifierInfo()) {
1250 if (II->isStr("apply_to"))
1251 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1252 if (II->isStr("any"))
1253 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1254 }
1255 if (Tok.is(tok::equal))
1256 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1257 return MissingAttributeSubjectRulesRecoveryPoint::None;
1258}
1259
1260/// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1261/// suggests the possible attribute subject rules in a fix-it together with
1262/// any other missing tokens.
1263DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
1264 unsigned DiagID, AttributeList &Attribute,
1265 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1266 SourceLocation Loc = PRef.getEndOfPreviousToken();
1267 if (Loc.isInvalid())
1268 Loc = PRef.getCurToken().getLocation();
1269 auto Diagnostic = PRef.Diag(Loc, DiagID);
1270 std::string FixIt;
1271 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1272 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1273 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1274 FixIt = ", ";
1275 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1276 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1277 FixIt += "apply_to";
1278 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1279 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1280 FixIt += " = ";
1281 SourceRange FixItRange(Loc);
1282 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1283 // Gather the subject match rules that are supported by the attribute.
1284 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1285 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1286 if (SubjectMatchRuleSet.empty()) {
1287 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1288 // placeholders will be supported by the fix-its.
1289 return Diagnostic;
1290 }
1291 FixIt += "any(";
1292 bool NeedsComma = false;
1293 for (const auto &I : SubjectMatchRuleSet) {
1294 // Ensure that the missing rule is reported in the fix-it only when it's
1295 // supported in the current language mode.
1296 if (!I.second)
1297 continue;
1298 if (NeedsComma)
1299 FixIt += ", ";
1300 else
1301 NeedsComma = true;
1302 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1303 }
1304 FixIt += ")";
1305 // Check if we need to remove the range
1306 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1307 FixItRange.setEnd(PRef.getCurToken().getLocation());
1308 }
1309 if (FixItRange.getBegin() == FixItRange.getEnd())
1310 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1311 else
1312 Diagnostic << FixItHint::CreateReplacement(
1313 CharSourceRange::getCharRange(FixItRange), FixIt);
1314 return Diagnostic;
1315}
1316
1317} // end anonymous namespace
1318
1319void Parser::HandlePragmaAttribute() {
1320 assert(Tok.is(tok::annot_pragma_attribute) &&(static_cast <bool> (Tok.is(tok::annot_pragma_attribute
) && "Expected #pragma attribute annotation token") ?
void (0) : __assert_fail ("Tok.is(tok::annot_pragma_attribute) && \"Expected #pragma attribute annotation token\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 1321, __extension__ __PRETTY_FUNCTION__))
1321 "Expected #pragma attribute annotation token")(static_cast <bool> (Tok.is(tok::annot_pragma_attribute
) && "Expected #pragma attribute annotation token") ?
void (0) : __assert_fail ("Tok.is(tok::annot_pragma_attribute) && \"Expected #pragma attribute annotation token\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 1321, __extension__ __PRETTY_FUNCTION__))
;
1322 SourceLocation PragmaLoc = Tok.getLocation();
1323 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1324 if (Info->Action == PragmaAttributeInfo::Pop) {
1325 ConsumeAnnotationToken();
1326 Actions.ActOnPragmaAttributePop(PragmaLoc);
1327 return;
1328 }
1329 // Parse the actual attribute with its arguments.
1330 assert(Info->Action == PragmaAttributeInfo::Push &&(static_cast <bool> (Info->Action == PragmaAttributeInfo
::Push && "Unexpected #pragma attribute command") ? void
(0) : __assert_fail ("Info->Action == PragmaAttributeInfo::Push && \"Unexpected #pragma attribute command\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 1331, __extension__ __PRETTY_FUNCTION__))
1331 "Unexpected #pragma attribute command")(static_cast <bool> (Info->Action == PragmaAttributeInfo
::Push && "Unexpected #pragma attribute command") ? void
(0) : __assert_fail ("Info->Action == PragmaAttributeInfo::Push && \"Unexpected #pragma attribute command\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 1331, __extension__ __PRETTY_FUNCTION__))
;
1332 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false);
1333 ConsumeAnnotationToken();
1334
1335 ParsedAttributes &Attrs = Info->Attributes;
1336 Attrs.clearListOnly();
1337
1338 auto SkipToEnd = [this]() {
1339 SkipUntil(tok::eof, StopBeforeMatch);
1340 ConsumeToken();
1341 };
1342
1343 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1344 // Parse the CXX11 style attribute.
1345 ParseCXX11AttributeSpecifier(Attrs);
1346 } else if (Tok.is(tok::kw___attribute)) {
1347 ConsumeToken();
1348 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1349 "attribute"))
1350 return SkipToEnd();
1351 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1352 return SkipToEnd();
1353
1354 if (Tok.isNot(tok::identifier)) {
1355 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1356 SkipToEnd();
1357 return;
1358 }
1359 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1360 SourceLocation AttrNameLoc = ConsumeToken();
1361
1362 if (Tok.isNot(tok::l_paren))
1363 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
1364 AttributeList::AS_GNU);
1365 else
1366 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1367 /*ScopeName=*/nullptr,
1368 /*ScopeLoc=*/SourceLocation(),
1369 AttributeList::AS_GNU,
1370 /*Declarator=*/nullptr);
1371
1372 if (ExpectAndConsume(tok::r_paren))
1373 return SkipToEnd();
1374 if (ExpectAndConsume(tok::r_paren))
1375 return SkipToEnd();
1376 } else if (Tok.is(tok::kw___declspec)) {
1377 ParseMicrosoftDeclSpecs(Attrs);
1378 } else {
1379 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1380 if (Tok.getIdentifierInfo()) {
1381 // If we suspect that this is an attribute suggest the use of
1382 // '__attribute__'.
1383 if (AttributeList::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1384 AttributeList::AS_GNU) !=
1385 AttributeList::UnknownAttribute) {
1386 SourceLocation InsertStartLoc = Tok.getLocation();
1387 ConsumeToken();
1388 if (Tok.is(tok::l_paren)) {
1389 ConsumeAnyToken();
1390 SkipUntil(tok::r_paren, StopBeforeMatch);
1391 if (Tok.isNot(tok::r_paren))
1392 return SkipToEnd();
1393 }
1394 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1395 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1396 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1397 }
1398 }
1399 SkipToEnd();
1400 return;
1401 }
1402
1403 if (!Attrs.getList() || Attrs.getList()->isInvalid()) {
1404 SkipToEnd();
1405 return;
1406 }
1407
1408 // Ensure that we don't have more than one attribute.
1409 if (Attrs.getList()->getNext()) {
1410 SourceLocation Loc = Attrs.getList()->getNext()->getLoc();
1411 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1412 SkipToEnd();
1413 return;
1414 }
1415
1416 if (!Attrs.getList()->isSupportedByPragmaAttribute()) {
1417 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
1418 << Attrs.getList()->getName();
1419 SkipToEnd();
1420 return;
1421 }
1422 AttributeList &Attribute = *Attrs.getList();
1423
1424 // Parse the subject-list.
1425 if (!TryConsumeToken(tok::comma)) {
1426 createExpectedAttributeSubjectRulesTokenDiagnostic(
1427 diag::err_expected, Attribute,
1428 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1429 << tok::comma;
1430 SkipToEnd();
1431 return;
1432 }
1433
1434 if (Tok.isNot(tok::identifier)) {
1435 createExpectedAttributeSubjectRulesTokenDiagnostic(
1436 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1437 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1438 SkipToEnd();
1439 return;
1440 }
1441 const IdentifierInfo *II = Tok.getIdentifierInfo();
1442 if (!II->isStr("apply_to")) {
1443 createExpectedAttributeSubjectRulesTokenDiagnostic(
1444 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1445 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1446 SkipToEnd();
1447 return;
1448 }
1449 ConsumeToken();
1450
1451 if (!TryConsumeToken(tok::equal)) {
1452 createExpectedAttributeSubjectRulesTokenDiagnostic(
1453 diag::err_expected, Attribute,
1454 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1455 << tok::equal;
1456 SkipToEnd();
1457 return;
1458 }
1459
1460 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1461 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1462 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1463 LastMatchRuleEndLoc)) {
1464 SkipToEnd();
1465 return;
1466 }
1467
1468 // Tokens following an ill-formed attribute will remain in the token stream
1469 // and must be removed.
1470 if (Tok.isNot(tok::eof)) {
1471 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1472 SkipToEnd();
1473 return;
1474 }
1475
1476 // Consume the eof terminator token.
1477 ConsumeToken();
1478
1479 Actions.ActOnPragmaAttributePush(Attribute, PragmaLoc,
1480 std::move(SubjectMatchRules));
1481}
1482
1483// #pragma GCC visibility comes in two variants:
1484// 'push' '(' [visibility] ')'
1485// 'pop'
1486void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
1487 PragmaIntroducerKind Introducer,
1488 Token &VisTok) {
1489 SourceLocation VisLoc = VisTok.getLocation();
1490
1491 Token Tok;
1492 PP.LexUnexpandedToken(Tok);
1493
1494 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1495
1496 const IdentifierInfo *VisType;
1497 if (PushPop && PushPop->isStr("pop")) {
1498 VisType = nullptr;
1499 } else if (PushPop && PushPop->isStr("push")) {
1500 PP.LexUnexpandedToken(Tok);
1501 if (Tok.isNot(tok::l_paren)) {
1502 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1503 << "visibility";
1504 return;
1505 }
1506 PP.LexUnexpandedToken(Tok);
1507 VisType = Tok.getIdentifierInfo();
1508 if (!VisType) {
1509 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1510 << "visibility";
1511 return;
1512 }
1513 PP.LexUnexpandedToken(Tok);
1514 if (Tok.isNot(tok::r_paren)) {
1515 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1516 << "visibility";
1517 return;
1518 }
1519 } else {
1520 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1521 << "visibility";
1522 return;
1523 }
1524 SourceLocation EndLoc = Tok.getLocation();
1525 PP.LexUnexpandedToken(Tok);
1526 if (Tok.isNot(tok::eod)) {
1527 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1528 << "visibility";
1529 return;
1530 }
1531
1532 auto Toks = llvm::make_unique<Token[]>(1);
1533 Toks[0].startToken();
1534 Toks[0].setKind(tok::annot_pragma_vis);
1535 Toks[0].setLocation(VisLoc);
1536 Toks[0].setAnnotationEndLoc(EndLoc);
1537 Toks[0].setAnnotationValue(
1538 const_cast<void*>(static_cast<const void*>(VisType)));
1539 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
1540}
1541
1542// #pragma pack(...) comes in the following delicious flavors:
1543// pack '(' [integer] ')'
1544// pack '(' 'show' ')'
1545// pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
1546void PragmaPackHandler::HandlePragma(Preprocessor &PP,
1547 PragmaIntroducerKind Introducer,
1548 Token &PackTok) {
1549 SourceLocation PackLoc = PackTok.getLocation();
1550
1551 Token Tok;
1552 PP.Lex(Tok);
1553 if (Tok.isNot(tok::l_paren)) {
1554 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
1555 return;
1556 }
1557
1558 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1559 StringRef SlotLabel;
1560 Token Alignment;
1561 Alignment.startToken();
1562 PP.Lex(Tok);
1563 if (Tok.is(tok::numeric_constant)) {
1564 Alignment = Tok;
1565
1566 PP.Lex(Tok);
1567
1568 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1569 // the push/pop stack.
1570 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
1571 Action =
1572 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
1573 } else if (Tok.is(tok::identifier)) {
1574 const IdentifierInfo *II = Tok.getIdentifierInfo();
1575 if (II->isStr("show")) {
1576 Action = Sema::PSK_Show;
1577 PP.Lex(Tok);
1578 } else {
1579 if (II->isStr("push")) {
1580 Action = Sema::PSK_Push;
1581 } else if (II->isStr("pop")) {
1582 Action = Sema::PSK_Pop;
1583 } else {
1584 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
1585 return;
1586 }
1587 PP.Lex(Tok);
1588
1589 if (Tok.is(tok::comma)) {
1590 PP.Lex(Tok);
1591
1592 if (Tok.is(tok::numeric_constant)) {
1593 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1594 Alignment = Tok;
1595
1596 PP.Lex(Tok);
1597 } else if (Tok.is(tok::identifier)) {
1598 SlotLabel = Tok.getIdentifierInfo()->getName();
1599 PP.Lex(Tok);
1600
1601 if (Tok.is(tok::comma)) {
1602 PP.Lex(Tok);
1603
1604 if (Tok.isNot(tok::numeric_constant)) {
1605 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1606 return;
1607 }
1608
1609 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1610 Alignment = Tok;
1611
1612 PP.Lex(Tok);
1613 }
1614 } else {
1615 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1616 return;
1617 }
1618 }
1619 }
1620 } else if (PP.getLangOpts().ApplePragmaPack) {
1621 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1622 // the push/pop stack.
1623 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1624 Action = Sema::PSK_Pop;
1625 }
1626
1627 if (Tok.isNot(tok::r_paren)) {
1628 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
1629 return;
1630 }
1631
1632 SourceLocation RParenLoc = Tok.getLocation();
1633 PP.Lex(Tok);
1634 if (Tok.isNot(tok::eod)) {
1635 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1636 return;
1637 }
1638
1639 PragmaPackInfo *Info =
1640 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
1641 Info->Action = Action;
1642 Info->SlotLabel = SlotLabel;
1643 Info->Alignment = Alignment;
1644
1645 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1646 1);
1647 Toks[0].startToken();
1648 Toks[0].setKind(tok::annot_pragma_pack);
1649 Toks[0].setLocation(PackLoc);
1650 Toks[0].setAnnotationEndLoc(RParenLoc);
1651 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1652 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1653}
1654
1655// #pragma ms_struct on
1656// #pragma ms_struct off
1657void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1658 PragmaIntroducerKind Introducer,
1659 Token &MSStructTok) {
1660 PragmaMSStructKind Kind = PMSST_OFF;
1661
1662 Token Tok;
1663 PP.Lex(Tok);
1664 if (Tok.isNot(tok::identifier)) {
1665 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1666 return;
1667 }
1668 SourceLocation EndLoc = Tok.getLocation();
1669 const IdentifierInfo *II = Tok.getIdentifierInfo();
1670 if (II->isStr("on")) {
1671 Kind = PMSST_ON;
1672 PP.Lex(Tok);
1673 }
1674 else if (II->isStr("off") || II->isStr("reset"))
1675 PP.Lex(Tok);
1676 else {
1677 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1678 return;
1679 }
1680
1681 if (Tok.isNot(tok::eod)) {
1682 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1683 << "ms_struct";
1684 return;
1685 }
1686
1687 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1688 1);
1689 Toks[0].startToken();
1690 Toks[0].setKind(tok::annot_pragma_msstruct);
1691 Toks[0].setLocation(MSStructTok.getLocation());
1692 Toks[0].setAnnotationEndLoc(EndLoc);
1693 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1694 static_cast<uintptr_t>(Kind)));
1695 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1696}
1697
1698// #pragma clang section bss="abc" data="" rodata="def" text=""
1699void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
1700 PragmaIntroducerKind Introducer, Token &FirstToken) {
1701
1702 Token Tok;
1703 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1704
1705 PP.Lex(Tok); // eat 'section'
1706 while (Tok.isNot(tok::eod)) {
1707 if (Tok.isNot(tok::identifier)) {
1708 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1709 return;
1710 }
1711
1712 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1713 if (SecType->isStr("bss"))
1714 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1715 else if (SecType->isStr("data"))
1716 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1717 else if (SecType->isStr("rodata"))
1718 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1719 else if (SecType->isStr("text"))
1720 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1721 else {
1722 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1723 return;
1724 }
1725
1726 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1727 if (Tok.isNot(tok::equal)) {
1728 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1729 return;
1730 }
1731
1732 std::string SecName;
1733 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1734 return;
1735
1736 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1737 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1738 Sema::PragmaClangSectionAction::PCSA_Clear),
1739 SecKind, SecName);
1740 }
1741}
1742
1743// #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1744// #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
1745static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
1746 bool IsOptions) {
1747 Token Tok;
1748
1749 if (IsOptions) {
1750 PP.Lex(Tok);
1751 if (Tok.isNot(tok::identifier) ||
1752 !Tok.getIdentifierInfo()->isStr("align")) {
1753 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1754 return;
1755 }
1756 }
1757
1758 PP.Lex(Tok);
1759 if (Tok.isNot(tok::equal)) {
1760 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1761 << IsOptions;
1762 return;
1763 }
1764
1765 PP.Lex(Tok);
1766 if (Tok.isNot(tok::identifier)) {
1767 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1768 << (IsOptions ? "options" : "align");
1769 return;
1770 }
1771
1772 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
1773 const IdentifierInfo *II = Tok.getIdentifierInfo();
1774 if (II->isStr("native"))
1775 Kind = Sema::POAK_Native;
1776 else if (II->isStr("natural"))
1777 Kind = Sema::POAK_Natural;
1778 else if (II->isStr("packed"))
1779 Kind = Sema::POAK_Packed;
1780 else if (II->isStr("power"))
1781 Kind = Sema::POAK_Power;
1782 else if (II->isStr("mac68k"))
1783 Kind = Sema::POAK_Mac68k;
1784 else if (II->isStr("reset"))
1785 Kind = Sema::POAK_Reset;
1786 else {
1787 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1788 << IsOptions;
1789 return;
1790 }
1791
1792 SourceLocation EndLoc = Tok.getLocation();
1793 PP.Lex(Tok);
1794 if (Tok.isNot(tok::eod)) {
1795 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1796 << (IsOptions ? "options" : "align");
1797 return;
1798 }
1799
1800 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1801 1);
1802 Toks[0].startToken();
1803 Toks[0].setKind(tok::annot_pragma_align);
1804 Toks[0].setLocation(FirstTok.getLocation());
1805 Toks[0].setAnnotationEndLoc(EndLoc);
1806 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1807 static_cast<uintptr_t>(Kind)));
1808 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1809}
1810
1811void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1812 PragmaIntroducerKind Introducer,
1813 Token &AlignTok) {
1814 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
1815}
1816
1817void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1818 PragmaIntroducerKind Introducer,
1819 Token &OptionsTok) {
1820 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
1821}
1822
1823// #pragma unused(identifier)
1824void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1825 PragmaIntroducerKind Introducer,
1826 Token &UnusedTok) {
1827 // FIXME: Should we be expanding macros here? My guess is no.
1828 SourceLocation UnusedLoc = UnusedTok.getLocation();
1829
1830 // Lex the left '('.
1831 Token Tok;
1832 PP.Lex(Tok);
1833 if (Tok.isNot(tok::l_paren)) {
1834 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1835 return;
1836 }
1837
1838 // Lex the declaration reference(s).
1839 SmallVector<Token, 5> Identifiers;
1840 SourceLocation RParenLoc;
1841 bool LexID = true;
1842
1843 while (true) {
1844 PP.Lex(Tok);
1845
1846 if (LexID) {
1847 if (Tok.is(tok::identifier)) {
1848 Identifiers.push_back(Tok);
1849 LexID = false;
1850 continue;
1851 }
1852
1853 // Illegal token!
1854 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1855 return;
1856 }
1857
1858 // We are execting a ')' or a ','.
1859 if (Tok.is(tok::comma)) {
1860 LexID = true;
1861 continue;
1862 }
1863
1864 if (Tok.is(tok::r_paren)) {
1865 RParenLoc = Tok.getLocation();
1866 break;
1867 }
1868
1869 // Illegal token!
1870 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
1871 return;
1872 }
1873
1874 PP.Lex(Tok);
1875 if (Tok.isNot(tok::eod)) {
1876 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1877 "unused";
1878 return;
1879 }
1880
1881 // Verify that we have a location for the right parenthesis.
1882 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'")(static_cast <bool> (RParenLoc.isValid() && "Valid '#pragma unused' must have ')'"
) ? void (0) : __assert_fail ("RParenLoc.isValid() && \"Valid '#pragma unused' must have ')'\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 1882, __extension__ __PRETTY_FUNCTION__))
;
1883 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments")(static_cast <bool> (!Identifiers.empty() && "Valid '#pragma unused' must have arguments"
) ? void (0) : __assert_fail ("!Identifiers.empty() && \"Valid '#pragma unused' must have arguments\""
, "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 1883, __extension__ __PRETTY_FUNCTION__))
;
1884
1885 // For each identifier token, insert into the token stream a
1886 // annot_pragma_unused token followed by the identifier token.
1887 // This allows us to cache a "#pragma unused" that occurs inside an inline
1888 // C++ member function.
1889
1890 MutableArrayRef<Token> Toks(
1891 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1892 2 * Identifiers.size());
1893 for (unsigned i=0; i != Identifiers.size(); i++) {
1894 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1895 pragmaUnusedTok.startToken();
1896 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1897 pragmaUnusedTok.setLocation(UnusedLoc);
1898 idTok = Identifiers[i];
1899 }
1900 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1901}
1902
1903// #pragma weak identifier
1904// #pragma weak identifier '=' identifier
1905void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1906 PragmaIntroducerKind Introducer,
1907 Token &WeakTok) {
1908 SourceLocation WeakLoc = WeakTok.getLocation();
1909
1910 Token Tok;
1911 PP.Lex(Tok);
1912 if (Tok.isNot(tok::identifier)) {
1913 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1914 return;
1915 }
1916
1917 Token WeakName = Tok;
1918 bool HasAlias = false;
1919 Token AliasName;
1920
1921 PP.Lex(Tok);
1922 if (Tok.is(tok::equal)) {
1923 HasAlias = true;
1924 PP.Lex(Tok);
1925 if (Tok.isNot(tok::identifier)) {
1926 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1927 << "weak";
1928 return;
1929 }
1930 AliasName = Tok;
1931 PP.Lex(Tok);
1932 }
1933
1934 if (Tok.isNot(tok::eod)) {
1935 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1936 return;
1937 }
1938
1939 if (HasAlias) {
1940 MutableArrayRef<Token> Toks(
1941 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
1942 Token &pragmaUnusedTok = Toks[0];
1943 pragmaUnusedTok.startToken();
1944 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1945 pragmaUnusedTok.setLocation(WeakLoc);
1946 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
1947 Toks[1] = WeakName;
1948 Toks[2] = AliasName;
1949 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1950 } else {
1951 MutableArrayRef<Token> Toks(
1952 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
1953 Token &pragmaUnusedTok = Toks[0];
1954 pragmaUnusedTok.startToken();
1955 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1956 pragmaUnusedTok.setLocation(WeakLoc);
1957 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
1958 Toks[1] = WeakName;
1959 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1960 }
1961}
1962
1963// #pragma redefine_extname identifier identifier
1964void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1965 PragmaIntroducerKind Introducer,
1966 Token &RedefToken) {
1967 SourceLocation RedefLoc = RedefToken.getLocation();
1968
1969 Token Tok;
1970 PP.Lex(Tok);
1971 if (Tok.isNot(tok::identifier)) {
1972 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1973 "redefine_extname";
1974 return;
1975 }
1976
1977 Token RedefName = Tok;
1978 PP.Lex(Tok);
1979
1980 if (Tok.isNot(tok::identifier)) {
1981 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1982 << "redefine_extname";
1983 return;
1984 }
1985
1986 Token AliasName = Tok;
1987 PP.Lex(Tok);
1988
1989 if (Tok.isNot(tok::eod)) {
1990 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1991 "redefine_extname";
1992 return;
1993 }
1994
1995 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1996 3);
1997 Token &pragmaRedefTok = Toks[0];
1998 pragmaRedefTok.startToken();
1999 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
2000 pragmaRedefTok.setLocation(RedefLoc);
2001 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
2002 Toks[1] = RedefName;
2003 Toks[2] = AliasName;
2004 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2005}
2006
2007
2008void
2009PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
2010 PragmaIntroducerKind Introducer,
2011 Token &Tok) {
2012 tok::OnOffSwitch OOS;
2013 if (PP.LexOnOffSwitch(OOS))
2014 return;
2015
2016 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2017 1);
2018 Toks[0].startToken();
2019 Toks[0].setKind(tok::annot_pragma_fp_contract);
2020 Toks[0].setLocation(Tok.getLocation());
2021 Toks[0].setAnnotationEndLoc(Tok.getLocation());
2022 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
2023 static_cast<uintptr_t>(OOS)));
2024 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2025}
2026
2027void
2028PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
2029 PragmaIntroducerKind Introducer,
2030 Token &Tok) {
2031 PP.LexUnexpandedToken(Tok);
2032 if (Tok.isNot(tok::identifier)) {
2033 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2034 "OPENCL";
2035 return;
2036 }
2037 IdentifierInfo *Ext = Tok.getIdentifierInfo();
2038 SourceLocation NameLoc = Tok.getLocation();
2039
2040 PP.Lex(Tok);
2041 if (Tok.isNot(tok::colon)) {
2042 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
2043 return;
2044 }
2045
2046 PP.Lex(Tok);
2047 if (Tok.isNot(tok::identifier)) {
2048 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
2049 return;
2050 }
2051 IdentifierInfo *Pred = Tok.getIdentifierInfo();
2052
2053 OpenCLExtState State;
2054 if (Pred->isStr("enable")) {
2055 State = Enable;
2056 } else if (Pred->isStr("disable")) {
2057 State = Disable;
2058 } else if (Pred->isStr("begin"))
2059 State = Begin;
2060 else if (Pred->isStr("end"))
2061 State = End;
2062 else {
2063 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
2064 << Ext->isStr("all");
2065 return;
2066 }
2067 SourceLocation StateLoc = Tok.getLocation();
2068
2069 PP.Lex(Tok);
2070 if (Tok.isNot(tok::eod)) {
2071 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2072 "OPENCL EXTENSION";
2073 return;
2074 }
2075
2076 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2077 Info->first = Ext;
2078 Info->second = State;
2079 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2080 1);
2081 Toks[0].startToken();
2082 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2083 Toks[0].setLocation(NameLoc);
2084 Toks[0].setAnnotationValue(static_cast<void*>(Info));
2085 Toks[0].setAnnotationEndLoc(StateLoc);
2086 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2087
2088 if (PP.getPPCallbacks())
2089 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
2090 StateLoc, State);
2091}
2092
2093/// \brief Handle '#pragma omp ...' when OpenMP is disabled.
2094///
2095void
2096PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2097 PragmaIntroducerKind Introducer,
2098 Token &FirstTok) {
2099 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2100 FirstTok.getLocation())) {
2101 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
2102 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2103 diag::Severity::Ignored, SourceLocation());
2104 }
2105 PP.DiscardUntilEndOfDirective();
2106}
2107
2108/// \brief Handle '#pragma omp ...' when OpenMP is enabled.
2109///
2110void
2111PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2112 PragmaIntroducerKind Introducer,
2113 Token &FirstTok) {
2114 SmallVector<Token, 16> Pragma;
2115 Token Tok;
2116 Tok.startToken();
2117 Tok.setKind(tok::annot_pragma_openmp);
2118 Tok.setLocation(FirstTok.getLocation());
2119
2120 while (Tok.isNot(tok::eod)) {
2121 Pragma.push_back(Tok);
2122 PP.Lex(Tok);
2123 }
2124 SourceLocation EodLoc = Tok.getLocation();
2125 Tok.startToken();
2126 Tok.setKind(tok::annot_pragma_openmp_end);
2127 Tok.setLocation(EodLoc);
2128 Pragma.push_back(Tok);
2129
2130 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2131 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2132 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
2133 /*DisableMacroExpansion=*/false);
2134}
2135
2136/// \brief Handle '#pragma pointers_to_members'
2137// The grammar for this pragma is as follows:
2138//
2139// <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2140//
2141// #pragma pointers_to_members '(' 'best_case' ')'
2142// #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2143// #pragma pointers_to_members '(' inheritance-model ')'
2144void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
2145 PragmaIntroducerKind Introducer,
2146 Token &Tok) {
2147 SourceLocation PointersToMembersLoc = Tok.getLocation();
2148 PP.Lex(Tok);
2149 if (Tok.isNot(tok::l_paren)) {
2150 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2151 << "pointers_to_members";
2152 return;
2153 }
2154 PP.Lex(Tok);
2155 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2156 if (!Arg) {
2157 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2158 << "pointers_to_members";
2159 return;
2160 }
2161 PP.Lex(Tok);
2162
2163 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
2164 if (Arg->isStr("best_case")) {
2165 RepresentationMethod = LangOptions::PPTMK_BestCase;
2166 } else {
2167 if (Arg->isStr("full_generality")) {
2168 if (Tok.is(tok::comma)) {
2169 PP.Lex(Tok);
2170
2171 Arg = Tok.getIdentifierInfo();
2172 if (!Arg) {
2173 PP.Diag(Tok.getLocation(),
2174 diag::err_pragma_pointers_to_members_unknown_kind)
2175 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2176 return;
2177 }
2178 PP.Lex(Tok);
2179 } else if (Tok.is(tok::r_paren)) {
2180 // #pragma pointers_to_members(full_generality) implicitly specifies
2181 // virtual_inheritance.
2182 Arg = nullptr;
2183 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2184 } else {
2185 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2186 << "full_generality";
2187 return;
2188 }
2189 }
2190
2191 if (Arg) {
2192 if (Arg->isStr("single_inheritance")) {
2193 RepresentationMethod =
2194 LangOptions::PPTMK_FullGeneralitySingleInheritance;
2195 } else if (Arg->isStr("multiple_inheritance")) {
2196 RepresentationMethod =
2197 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
2198 } else if (Arg->isStr("virtual_inheritance")) {
2199 RepresentationMethod =
2200 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2201 } else {
2202 PP.Diag(Tok.getLocation(),
2203 diag::err_pragma_pointers_to_members_unknown_kind)
2204 << Arg << /*HasPointerDeclaration*/ 1;
2205 return;
2206 }
2207 }
2208 }
2209
2210 if (Tok.isNot(tok::r_paren)) {
2211 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2212 << (Arg ? Arg->getName() : "full_generality");
2213 return;
2214 }
2215
2216 SourceLocation EndLoc = Tok.getLocation();
2217 PP.Lex(Tok);
2218 if (Tok.isNot(tok::eod)) {
2219 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2220 << "pointers_to_members";
2221 return;
2222 }
2223
2224 Token AnnotTok;
2225 AnnotTok.startToken();
2226 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2227 AnnotTok.setLocation(PointersToMembersLoc);
2228 AnnotTok.setAnnotationEndLoc(EndLoc);
2229 AnnotTok.setAnnotationValue(
2230 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
2231 PP.EnterToken(AnnotTok);
2232}
2233
2234/// \brief Handle '#pragma vtordisp'
2235// The grammar for this pragma is as follows:
2236//
2237// <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2238//
2239// #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2240// #pragma vtordisp '(' 'pop' ')'
2241// #pragma vtordisp '(' ')'
2242void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
2243 PragmaIntroducerKind Introducer,
2244 Token &Tok) {
2245 SourceLocation VtorDispLoc = Tok.getLocation();
2246 PP.Lex(Tok);
2247 if (Tok.isNot(tok::l_paren)) {
2248 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2249 return;
2250 }
2251 PP.Lex(Tok);
2252
2253 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
2254 const IdentifierInfo *II = Tok.getIdentifierInfo();
2255 if (II) {
2256 if (II->isStr("push")) {
2257 // #pragma vtordisp(push, mode)
2258 PP.Lex(Tok);
2259 if (Tok.isNot(tok::comma)) {
2260 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2261 return;
2262 }
2263 PP.Lex(Tok);
2264 Action = Sema::PSK_Push_Set;
2265 // not push, could be on/off
2266 } else if (II->isStr("pop")) {
2267 // #pragma vtordisp(pop)
2268 PP.Lex(Tok);
2269 Action = Sema::PSK_Pop;
2270 }
2271 // not push or pop, could be on/off
2272 } else {
2273 if (Tok.is(tok::r_paren)) {
2274 // #pragma vtordisp()
2275 Action = Sema::PSK_Reset;
2276 }
2277 }
2278
2279
2280 uint64_t Value = 0;
2281 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
2282 const IdentifierInfo *II = Tok.getIdentifierInfo();
2283 if (II && II->isStr("off")) {
2284 PP.Lex(Tok);
2285 Value = 0;
2286 } else if (II && II->isStr("on")) {
2287 PP.Lex(Tok);
2288 Value = 1;
2289 } else if (Tok.is(tok::numeric_constant) &&
2290 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2291 if (Value > 2) {
2292 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2293 << 0 << 2 << "vtordisp";
2294 return;
2295 }
2296 } else {
2297 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2298 << "vtordisp";
2299 return;
2300 }
2301 }
2302
2303 // Finish the pragma: ')' $
2304 if (Tok.isNot(tok::r_paren)) {
2305 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2306 return;
2307 }
2308 SourceLocation EndLoc = Tok.getLocation();
2309 PP.Lex(Tok);
2310 if (Tok.isNot(tok::eod)) {
2311 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2312 << "vtordisp";
2313 return;
2314 }
2315
2316 // Enter the annotation.
2317 Token AnnotTok;
2318 AnnotTok.startToken();
2319 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2320 AnnotTok.setLocation(VtorDispLoc);
2321 AnnotTok.setAnnotationEndLoc(EndLoc);
2322 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
2323 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
2324 PP.EnterToken(AnnotTok);
2325}
2326
2327/// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
2328/// an annotation token.
2329void PragmaMSPragma::HandlePragma(Preprocessor &PP,
2330 PragmaIntroducerKind Introducer,
2331 Token &Tok) {
2332 Token EoF, AnnotTok;
2333 EoF.startToken();
2334 EoF.setKind(tok::eof);
2335 AnnotTok.startToken();
2336 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2337 AnnotTok.setLocation(Tok.getLocation());
2338 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2339 SmallVector<Token, 8> TokenVector;
2340 // Suck up all of the tokens before the eod.
2341 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
2342 TokenVector.push_back(Tok);
2343 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2344 }
2345 // Add a sentinal EoF token to the end of the list.
2346 TokenVector.push_back(EoF);
2347 // We must allocate this array with new because EnterTokenStream is going to
2348 // delete it later.
2349 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2350 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
2351 auto Value = new (PP.getPreprocessorAllocator())
2352 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2353 TokenVector.size());
2354 AnnotTok.setAnnotationValue(Value);
2355 PP.EnterToken(AnnotTok);
2356}
2357
2358/// \brief Handle the Microsoft \#pragma detect_mismatch extension.
2359///
2360/// The syntax is:
2361/// \code
2362/// #pragma detect_mismatch("name", "value")
2363/// \endcode
2364/// Where 'name' and 'value' are quoted strings. The values are embedded in
2365/// the object file and passed along to the linker. If the linker detects a
2366/// mismatch in the object file's values for the given name, a LNK2038 error
2367/// is emitted. See MSDN for more details.
2368void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
2369 PragmaIntroducerKind Introducer,
2370 Token &Tok) {
2371 SourceLocation DetectMismatchLoc = Tok.getLocation();
2372 PP.Lex(Tok);
2373 if (Tok.isNot(tok::l_paren)) {
2374 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
2375 return;
2376 }
2377
2378 // Read the name to embed, which must be a string literal.
2379 std::string NameString;
2380 if (!PP.LexStringLiteral(Tok, NameString,
2381 "pragma detect_mismatch",
2382 /*MacroExpansion=*/true))
2383 return;
2384
2385 // Read the comma followed by a second string literal.
2386 std::string ValueString;
2387 if (Tok.isNot(tok::comma)) {
2388 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2389 return;
2390 }
2391
2392 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
2393 /*MacroExpansion=*/true))
2394 return;
2395
2396 if (Tok.isNot(tok::r_paren)) {
2397 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2398 return;
2399 }
2400 PP.Lex(Tok); // Eat the r_paren.
2401
2402 if (Tok.isNot(tok::eod)) {
2403 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2404 return;
2405 }
2406
2407 // If the pragma is lexically sound, notify any interested PPCallbacks.
2408 if (PP.getPPCallbacks())
2409 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
2410 ValueString);
2411
2412 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
2413}
2414
2415/// \brief Handle the microsoft \#pragma comment extension.
2416///
2417/// The syntax is:
2418/// \code
2419/// #pragma comment(linker, "foo")
2420/// \endcode
2421/// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2422/// "foo" is a string, which is fully macro expanded, and permits string
2423/// concatenation, embedded escape characters etc. See MSDN for more details.
2424void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
2425 PragmaIntroducerKind Introducer,
2426 Token &Tok) {
2427 SourceLocation CommentLoc = Tok.getLocation();
2428 PP.Lex(Tok);
2429 if (Tok.isNot(tok::l_paren)) {
2430 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2431 return;
2432 }
2433
2434 // Read the identifier.
2435 PP.Lex(Tok);
2436 if (Tok.isNot(tok::identifier)) {
2437 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2438 return;
2439 }
2440
2441 // Verify that this is one of the 5 whitelisted options.
2442 IdentifierInfo *II = Tok.getIdentifierInfo();
2443 PragmaMSCommentKind Kind =
2444 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2445 .Case("linker", PCK_Linker)
2446 .Case("lib", PCK_Lib)
2447 .Case("compiler", PCK_Compiler)
2448 .Case("exestr", PCK_ExeStr)
2449 .Case("user", PCK_User)
2450 .Default(PCK_Unknown);
2451 if (Kind == PCK_Unknown) {
2452 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2453 return;
2454 }
2455
2456 if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
2457 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2458 << II->getName();
2459 return;
2460 }
2461
2462 // On PS4, issue a warning about any pragma comments other than
2463 // #pragma comment lib.
2464 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
2465 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2466 << II->getName();
2467 return;
2468 }
2469
2470 // Read the optional string if present.
2471 PP.Lex(Tok);
2472 std::string ArgumentString;
2473 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2474 "pragma comment",
2475 /*MacroExpansion=*/true))
2476 return;
2477
2478 // FIXME: warn that 'exestr' is deprecated.
2479 // FIXME: If the kind is "compiler" warn if the string is present (it is
2480 // ignored).
2481 // The MSDN docs say that "lib" and "linker" require a string and have a short
2482 // whitelist of linker options they support, but in practice MSVC doesn't
2483 // issue a diagnostic. Therefore neither does clang.
2484
2485 if (Tok.isNot(tok::r_paren)) {
2486 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2487 return;
2488 }
2489 PP.Lex(Tok); // eat the r_paren.
2490
2491 if (Tok.isNot(tok::eod)) {
2492 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2493 return;
2494 }
2495
2496 // If the pragma is lexically sound, notify any interested PPCallbacks.
2497 if (PP.getPPCallbacks())
2498 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2499
2500 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
2501}
2502
2503// #pragma clang optimize off
2504// #pragma clang optimize on
2505void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
2506 PragmaIntroducerKind Introducer,
2507 Token &FirstToken) {
2508 Token Tok;
2509 PP.Lex(Tok);
2510 if (Tok.is(tok::eod)) {
2511 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
2512 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
2513 return;
2514 }
2515 if (Tok.isNot(tok::identifier)) {
2516 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2517 << PP.getSpelling(Tok);
2518 return;
2519 }
2520 const IdentifierInfo *II = Tok.getIdentifierInfo();
2521 // The only accepted values are 'on' or 'off'.
2522 bool IsOn = false;
2523 if (II->isStr("on")) {
2524 IsOn = true;
2525 } else if (!II->isStr("off")) {
2526 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2527 << PP.getSpelling(Tok);
2528 return;
2529 }
2530 PP.Lex(Tok);
2531
2532 if (Tok.isNot(tok::eod)) {
2533 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2534 << PP.getSpelling(Tok);
2535 return;
2536 }
2537
2538 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2539}
2540
2541namespace {
2542/// Used as the annotation value for tok::annot_pragma_fp.
2543struct TokFPAnnotValue {
2544 enum FlagKinds { Contract };
2545 enum FlagValues { On, Off, Fast };
2546
2547 FlagKinds FlagKind;
2548 FlagValues FlagValue;
2549};
2550} // end anonymous namespace
2551
2552void PragmaFPHandler::HandlePragma(Preprocessor &PP,
2553 PragmaIntroducerKind Introducer,
2554 Token &Tok) {
2555 // fp
2556 Token PragmaName = Tok;
2557 SmallVector<Token, 1> TokenList;
2558
2559 PP.Lex(Tok);
2560 if (Tok.isNot(tok::identifier)) {
2561 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2562 << /*MissingOption=*/true << "";
2563 return;
2564 }
2565
2566 while (Tok.is(tok::identifier)) {
2567 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2568
2569 auto FlagKind =
2570 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2571 OptionInfo->getName())
2572 .Case("contract", TokFPAnnotValue::Contract)
2573 .Default(None);
2574 if (!FlagKind) {
2575 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2576 << /*MissingOption=*/false << OptionInfo;
2577 return;
2578 }
2579 PP.Lex(Tok);
2580
2581 // Read '('
2582 if (Tok.isNot(tok::l_paren)) {
2583 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2584 return;
2585 }
2586 PP.Lex(Tok);
2587
2588 if (Tok.isNot(tok::identifier)) {
2589 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2590 << PP.getSpelling(Tok) << OptionInfo->getName();
2591 return;
2592 }
2593 const IdentifierInfo *II = Tok.getIdentifierInfo();
2594
2595 auto FlagValue =
2596 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2597 II->getName())
2598 .Case("on", TokFPAnnotValue::On)
2599 .Case("off", TokFPAnnotValue::Off)
2600 .Case("fast", TokFPAnnotValue::Fast)
2601 .Default(llvm::None);
2602
2603 if (!FlagValue) {
2604 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2605 << PP.getSpelling(Tok) << OptionInfo->getName();
2606 return;
2607 }
2608 PP.Lex(Tok);
2609
2610 // Read ')'
2611 if (Tok.isNot(tok::r_paren)) {
2612 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2613 return;
2614 }
2615 PP.Lex(Tok);
2616
2617 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2618 TokFPAnnotValue{*FlagKind, *FlagValue};
2619 // Generate the loop hint token.
2620 Token FPTok;
2621 FPTok.startToken();
2622 FPTok.setKind(tok::annot_pragma_fp);
2623 FPTok.setLocation(PragmaName.getLocation());
2624 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2625 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2626 TokenList.push_back(FPTok);
2627 }
2628
2629 if (Tok.isNot(tok::eod)) {
2630 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2631 << "clang fp";
2632 return;
2633 }
2634
2635 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2636 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2637
2638 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2639 /*DisableMacroExpansion=*/false);
2640}
2641
2642void Parser::HandlePragmaFP() {
2643 assert(Tok.is(tok::annot_pragma_fp))(static_cast <bool> (Tok.is(tok::annot_pragma_fp)) ? void
(0) : __assert_fail ("Tok.is(tok::annot_pragma_fp)", "/build/llvm-toolchain-snapshot-7~svn325118/tools/clang/lib/Parse/ParsePragma.cpp"
, 2643, __extension__ __PRETTY_FUNCTION__))
;
2644 auto *AnnotValue =
2645 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2646
2647 LangOptions::FPContractModeKind FPC;
2648 switch (AnnotValue->FlagValue) {
2649 case TokFPAnnotValue::On:
2650 FPC = LangOptions::FPC_On;
2651 break;
2652 case TokFPAnnotValue::Fast:
2653 FPC = LangOptions::FPC_Fast;
2654 break;
2655 case TokFPAnnotValue::Off:
2656 FPC = LangOptions::FPC_Off;
2657 break;
2658 }
2659
2660 Actions.ActOnPragmaFPContract(FPC);
2661 ConsumeAnnotationToken();
2662}
2663
2664/// \brief Parses loop or unroll pragma hint value and fills in Info.
2665static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2666 Token Option, bool ValueInParens,
2667 PragmaLoopHintInfo &Info) {
2668 SmallVector<Token, 1> ValueList;
2669 int OpenParens = ValueInParens ? 1 : 0;
2670 // Read constant expression.
2671 while (Tok.isNot(tok::eod)) {
2672 if (Tok.is(tok::l_paren))
2673 OpenParens++;
2674 else if (Tok.is(tok::r_paren)) {
2675 OpenParens--;
2676 if (OpenParens == 0 && ValueInParens)
2677 break;
2678 }
2679
2680 ValueList.push_back(Tok);
2681 PP.Lex(Tok);
2682 }
2683
2684 if (ValueInParens) {
2685 // Read ')'
2686 if (Tok.isNot(tok::r_paren)) {
2687 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2688 return true;
2689 }
2690 PP.Lex(Tok);
2691 }
2692
2693 Token EOFTok;
2694 EOFTok.startToken();
2695 EOFTok.setKind(tok::eof);
2696 EOFTok.setLocation(Tok.getLocation());
2697 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2698
2699 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
2700
2701 Info.PragmaName = PragmaName;
2702 Info.Option = Option;
2703 return false;
2704}
2705
2706/// \brief Handle the \#pragma clang loop directive.
2707/// #pragma clang 'loop' loop-hints
2708///
2709/// loop-hints:
2710/// loop-hint loop-hints[opt]
2711///
2712/// loop-hint:
2713/// 'vectorize' '(' loop-hint-keyword ')'
2714/// 'interleave' '(' loop-hint-keyword ')'
2715/// 'unroll' '(' unroll-hint-keyword ')'
2716/// 'vectorize_width' '(' loop-hint-value ')'
2717/// 'interleave_count' '(' loop-hint-value ')'
2718/// 'unroll_count' '(' loop-hint-value ')'
2719///
2720/// loop-hint-keyword:
2721/// 'enable'
2722/// 'disable'
2723/// 'assume_safety'
2724///
2725/// unroll-hint-keyword:
2726/// 'enable'
2727/// 'disable'
2728/// 'full'
2729///
2730/// loop-hint-value:
2731/// constant-expression
2732///
2733/// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2734/// try vectorizing the instructions of the loop it precedes. Specifying
2735/// interleave(enable) or interleave_count(_value_) instructs llvm to try
2736/// interleaving multiple iterations of the loop it precedes. The width of the
2737/// vector instructions is specified by vectorize_width() and the number of
2738/// interleaved loop iterations is specified by interleave_count(). Specifying a
2739/// value of 1 effectively disables vectorization/interleaving, even if it is
2740/// possible and profitable, and 0 is invalid. The loop vectorizer currently
2741/// only works on inner loops.
2742///
2743/// The unroll and unroll_count directives control the concatenation
2744/// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2745/// completely if the trip count is known at compile time and unroll partially
2746/// if the trip count is not known. Specifying unroll(full) is similar to
2747/// unroll(enable) but will unroll the loop only if the trip count is known at
2748/// compile time. Specifying unroll(disable) disables unrolling for the
2749/// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2750/// loop the number of times indicated by the value.
2751void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2752 PragmaIntroducerKind Introducer,
2753 Token &Tok) {
2754 // Incoming token is "loop" from "#pragma clang loop".
2755 Token PragmaName = Tok;
2756 SmallVector<Token, 1> TokenList;
2757
2758 // Lex the optimization option and verify it is an identifier.
2759 PP.Lex(Tok);
2760 if (Tok.isNot(tok::identifier)) {
2761 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2762 << /*MissingOption=*/true << "";
2763 return;
2764 }
2765
2766 while (Tok.is(tok::identifier)) {
2767 Token Option = Tok;
2768 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2769
2770 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
2771 .Case("vectorize", true)
2772 .Case("interleave", true)
2773 .Case("unroll", true)
2774 .Case("distribute", true)
2775 .Case("vectorize_width", true)
2776 .Case("interleave_count", true)
2777 .Case("unroll_count", true)
2778 .Default(false);
2779 if (!OptionValid) {
2780 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2781 << /*MissingOption=*/false << OptionInfo;
2782 return;
2783 }
2784 PP.Lex(Tok);
2785
2786 // Read '('
2787 if (Tok.isNot(tok::l_paren)) {
2788 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2789 return;
2790 }
2791 PP.Lex(Tok);
2792
2793 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2794 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2795 *Info))
2796 return;
2797
2798 // Generate the loop hint token.
2799 Token LoopHintTok;
2800 LoopHintTok.startToken();
2801 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
2802 LoopHintTok.setLocation(PragmaName.getLocation());
2803 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
2804 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2805 TokenList.push_back(LoopHintTok);
2806 }
2807
2808 if (Tok.isNot(tok::eod)) {
2809 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2810 << "clang loop";
2811 return;
2812 }
2813
2814 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2815 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2816
2817 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2818 /*DisableMacroExpansion=*/false);
2819}
2820
2821/// \brief Handle the loop unroll optimization pragmas.
2822/// #pragma unroll
2823/// #pragma unroll unroll-hint-value
2824/// #pragma unroll '(' unroll-hint-value ')'
2825/// #pragma nounroll
2826///
2827/// unroll-hint-value:
2828/// constant-expression
2829///
2830/// Loop unrolling hints can be specified with '#pragma unroll' or
2831/// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2832/// contained in parentheses. With no argument the directive instructs llvm to
2833/// try to unroll the loop completely. A positive integer argument can be
2834/// specified to indicate the number of times the loop should be unrolled. To
2835/// maximize compatibility with other compilers the unroll count argument can be
2836/// specified with or without parentheses. Specifying, '#pragma nounroll'
2837/// disables unrolling of the loop.
2838void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2839 PragmaIntroducerKind Introducer,
2840 Token &Tok) {
2841 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2842 // "#pragma nounroll".
2843 Token PragmaName = Tok;
2844 PP.Lex(Tok);
2845 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
1
'Info' initialized to a null pointer value
2846 if (Tok.is(tok::eod)) {
2
Taking false branch
2847 // nounroll or unroll pragma without an argument.
2848 Info->PragmaName = PragmaName;
2849 Info->Option.startToken();
2850 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
3
Assuming the condition is false
4
Taking false branch
2851 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2852 << "nounroll";
2853 return;
2854 } else {
2855 // Unroll pragma with an argument: "#pragma unroll N" or
2856 // "#pragma unroll(N)".
2857 // Read '(' if it exists.
2858 bool ValueInParens = Tok.is(tok::l_paren);
2859 if (ValueInParens)
5
Taking false branch
2860 PP.Lex(Tok);
2861
2862 Token Option;
2863 Option.startToken();
2864 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
6
Forming reference to null pointer
2865 return;
2866
2867 // In CUDA, the argument to '#pragma unroll' should not be contained in
2868 // parentheses.
2869 if (PP.getLangOpts().CUDA && ValueInParens)
2870 PP.Diag(Info->Toks[0].getLocation(),
2871 diag::warn_pragma_unroll_cuda_value_in_parens);
2872
2873 if (Tok.isNot(tok::eod)) {
2874 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2875 << "unroll";
2876 return;
2877 }
2878 }
2879
2880 // Generate the hint token.
2881 auto TokenArray = llvm::make_unique<Token[]>(1);
2882 TokenArray[0].startToken();
2883 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2884 TokenArray[0].setLocation(PragmaName.getLocation());
2885 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
2886 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2887 PP.EnterTokenStream(std::move(TokenArray), 1,
2888 /*DisableMacroExpansion=*/false);
2889}
2890
2891/// \brief Handle the Microsoft \#pragma intrinsic extension.
2892///
2893/// The syntax is:
2894/// \code
2895/// #pragma intrinsic(memset)
2896/// #pragma intrinsic(strlen, memcpy)
2897/// \endcode
2898///
2899/// Pragma intrisic tells the compiler to use a builtin version of the
2900/// function. Clang does it anyway, so the pragma doesn't really do anything.
2901/// Anyway, we emit a warning if the function specified in \#pragma intrinsic
2902/// isn't an intrinsic in clang and suggest to include intrin.h.
2903void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
2904 PragmaIntroducerKind Introducer,
2905 Token &Tok) {
2906 PP.Lex(Tok);
2907
2908 if (Tok.isNot(tok::l_paren)) {
2909 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
2910 << "intrinsic";
2911 return;
2912 }
2913 PP.Lex(Tok);
2914
2915 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
2916
2917 while (Tok.is(tok::identifier)) {
2918 IdentifierInfo *II = Tok.getIdentifierInfo();
2919 if (!II->getBuiltinID())
2920 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
2921 << II << SuggestIntrinH;
2922
2923 PP.Lex(Tok);
2924 if (Tok.isNot(tok::comma))
2925 break;
2926 PP.Lex(Tok);
2927 }
2928
2929 if (Tok.isNot(tok::r_paren)) {
2930 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
2931 << "intrinsic";
2932 return;
2933 }
2934 PP.Lex(Tok);
2935
2936 if (Tok.isNot(tok::eod))
2937 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2938 << "intrinsic";
2939}
2940void PragmaForceCUDAHostDeviceHandler::HandlePragma(
2941 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
2942 Token FirstTok = Tok;
2943
2944 PP.Lex(Tok);
2945 IdentifierInfo *Info = Tok.getIdentifierInfo();
2946 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
2947 PP.Diag(FirstTok.getLocation(),
2948 diag::warn_pragma_force_cuda_host_device_bad_arg);
2949 return;
2950 }
2951
2952 if (Info->isStr("begin"))
2953 Actions.PushForceCUDAHostDevice();
2954 else if (!Actions.PopForceCUDAHostDevice())
2955 PP.Diag(FirstTok.getLocation(),
2956 diag::err_pragma_cannot_end_force_cuda_host_device);
2957
2958 PP.Lex(Tok);
2959 if (!Tok.is(tok::eod))
2960 PP.Diag(FirstTok.getLocation(),
2961 diag::warn_pragma_force_cuda_host_device_bad_arg);
2962}
2963
2964/// \brief Handle the #pragma clang attribute directive.
2965///
2966/// The syntax is:
2967/// \code
2968/// #pragma clang attribute push(attribute, subject-set)
2969/// #pragma clang attribute pop
2970/// \endcode
2971///
2972/// The subject-set clause defines the set of declarations which receive the
2973/// attribute. Its exact syntax is described in the LanguageExtensions document
2974/// in Clang's documentation.
2975///
2976/// This directive instructs the compiler to begin/finish applying the specified
2977/// attribute to the set of attribute-specific declarations in the active range
2978/// of the pragma.
2979void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
2980 PragmaIntroducerKind Introducer,
2981 Token &FirstToken) {
2982 Token Tok;
2983 PP.Lex(Tok);
2984 auto *Info = new (PP.getPreprocessorAllocator())
2985 PragmaAttributeInfo(AttributesForPragmaAttribute);
2986
2987 // Parse the 'push' or 'pop'.
2988 if (Tok.isNot(tok::identifier)) {
2989 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_push_pop);
2990 return;
2991 }
2992 const auto *II = Tok.getIdentifierInfo();
2993 if (II->isStr("push"))
2994 Info->Action = PragmaAttributeInfo::Push;
2995 else if (II->isStr("pop"))
2996 Info->Action = PragmaAttributeInfo::Pop;
2997 else {
2998 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
2999 << PP.getSpelling(Tok);
3000 return;
3001 }
3002 PP.Lex(Tok);
3003
3004 // Parse the actual attribute.
3005 if (Info->Action == PragmaAttributeInfo::Push) {
3006 if (Tok.isNot(tok::l_paren)) {
3007 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
3008 return;
3009 }
3010 PP.Lex(Tok);
3011
3012 // Lex the attribute tokens.
3013 SmallVector<Token, 16> AttributeTokens;
3014 int OpenParens = 1;
3015 while (Tok.isNot(tok::eod)) {
3016 if (Tok.is(tok::l_paren))
3017 OpenParens++;
3018 else if (Tok.is(tok::r_paren)) {
3019 OpenParens--;
3020 if (OpenParens == 0)
3021 break;
3022 }
3023
3024 AttributeTokens.push_back(Tok);
3025 PP.Lex(Tok);
3026 }
3027
3028 if (AttributeTokens.empty()) {
3029 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
3030 return;
3031 }
3032 if (Tok.isNot(tok::r_paren)) {
3033 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
3034 return;
3035 }
3036 SourceLocation EndLoc = Tok.getLocation();
3037 PP.Lex(Tok);
3038
3039 // Terminate the attribute for parsing.
3040 Token EOFTok;
3041 EOFTok.startToken();
3042 EOFTok.setKind(tok::eof);
3043 EOFTok.setLocation(EndLoc);
3044 AttributeTokens.push_back(EOFTok);
3045
3046 Info->Tokens =
3047 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
3048 }
3049
3050 if (Tok.isNot(tok::eod))
3051 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3052 << "clang attribute";
3053
3054 // Generate the annotated pragma token.
3055 auto TokenArray = llvm::make_unique<Token[]>(1);
3056 TokenArray[0].startToken();
3057 TokenArray[0].setKind(tok::annot_pragma_attribute);
3058 TokenArray[0].setLocation(FirstToken.getLocation());
3059 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
3060 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
3061 PP.EnterTokenStream(std::move(TokenArray), 1,
3062 /*DisableMacroExpansion=*/false);
3063}