1 //===--- ParsePragma.cpp - Language specific pragma parsing ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the language specific #pragma handlers.
12 //===----------------------------------------------------------------------===//
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/LoopHint.h"
19 #include "clang/Parse/ParseDiagnostic.h"
20 #include "clang/Parse/Parser.h"
21 #include "clang/Parse/RAIIObjectsForParser.h"
22 #include "clang/Sema/Scope.h"
23 #include "llvm/ADT/StringSwitch.h"
24 using namespace clang;
28 struct PragmaAlignHandler : public PragmaHandler {
29 explicit PragmaAlignHandler() : PragmaHandler("align") {}
30 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
31 Token &FirstToken) override;
34 struct PragmaGCCVisibilityHandler : public PragmaHandler {
35 explicit PragmaGCCVisibilityHandler() : PragmaHandler("visibility") {}
36 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
37 Token &FirstToken) override;
40 struct PragmaOptionsHandler : public PragmaHandler {
41 explicit PragmaOptionsHandler() : PragmaHandler("options") {}
42 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
43 Token &FirstToken) override;
46 struct PragmaPackHandler : public PragmaHandler {
47 explicit PragmaPackHandler() : PragmaHandler("pack") {}
48 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
49 Token &FirstToken) override;
52 struct PragmaClangSectionHandler : public PragmaHandler {
53 explicit PragmaClangSectionHandler(Sema &S)
54 : PragmaHandler("section"), Actions(S) {}
55 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
56 Token &FirstToken) override;
61 struct PragmaMSStructHandler : public PragmaHandler {
62 explicit PragmaMSStructHandler() : PragmaHandler("ms_struct") {}
63 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
64 Token &FirstToken) override;
67 struct PragmaUnusedHandler : public PragmaHandler {
68 PragmaUnusedHandler() : PragmaHandler("unused") {}
69 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
70 Token &FirstToken) override;
73 struct PragmaWeakHandler : public PragmaHandler {
74 explicit PragmaWeakHandler() : PragmaHandler("weak") {}
75 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
76 Token &FirstToken) override;
79 struct PragmaRedefineExtnameHandler : public PragmaHandler {
80 explicit PragmaRedefineExtnameHandler() : PragmaHandler("redefine_extname") {}
81 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
82 Token &FirstToken) override;
85 struct PragmaOpenCLExtensionHandler : public PragmaHandler {
86 PragmaOpenCLExtensionHandler() : PragmaHandler("EXTENSION") {}
87 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
88 Token &FirstToken) override;
92 struct PragmaFPContractHandler : public PragmaHandler {
93 PragmaFPContractHandler() : PragmaHandler("FP_CONTRACT") {}
94 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
95 Token &FirstToken) override;
98 // Pragma STDC implementations.
100 /// PragmaSTDC_FENV_ACCESSHandler - "\#pragma STDC FENV_ACCESS ...".
101 struct PragmaSTDC_FENV_ACCESSHandler : public PragmaHandler {
102 PragmaSTDC_FENV_ACCESSHandler() : PragmaHandler("FENV_ACCESS") {}
104 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
105 Token &Tok) override {
106 tok::OnOffSwitch OOS;
107 if (PP.LexOnOffSwitch(OOS))
109 if (OOS == tok::OOS_ON) {
110 PP.Diag(Tok, diag::warn_stdc_fenv_access_not_supported);
113 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
115 Toks[0].startToken();
116 Toks[0].setKind(tok::annot_pragma_fenv_access);
117 Toks[0].setLocation(Tok.getLocation());
118 Toks[0].setAnnotationEndLoc(Tok.getLocation());
119 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
120 static_cast<uintptr_t>(OOS)));
121 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
125 /// PragmaSTDC_CX_LIMITED_RANGEHandler - "\#pragma STDC CX_LIMITED_RANGE ...".
126 struct PragmaSTDC_CX_LIMITED_RANGEHandler : public PragmaHandler {
127 PragmaSTDC_CX_LIMITED_RANGEHandler() : PragmaHandler("CX_LIMITED_RANGE") {}
129 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
130 Token &Tok) override {
131 tok::OnOffSwitch OOS;
132 PP.LexOnOffSwitch(OOS);
136 /// PragmaSTDC_UnknownHandler - "\#pragma STDC ...".
137 struct PragmaSTDC_UnknownHandler : public PragmaHandler {
138 PragmaSTDC_UnknownHandler() = default;
140 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
141 Token &UnknownTok) override {
142 // C99 6.10.6p2, unknown forms are not allowed.
143 PP.Diag(UnknownTok, diag::ext_stdc_pragma_ignored);
147 struct PragmaFPHandler : public PragmaHandler {
148 PragmaFPHandler() : PragmaHandler("fp") {}
149 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
150 Token &FirstToken) override;
153 struct PragmaNoOpenMPHandler : public PragmaHandler {
154 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
155 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
156 Token &FirstToken) override;
159 struct PragmaOpenMPHandler : public PragmaHandler {
160 PragmaOpenMPHandler() : PragmaHandler("omp") { }
161 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
162 Token &FirstToken) override;
165 /// PragmaCommentHandler - "\#pragma comment ...".
166 struct PragmaCommentHandler : public PragmaHandler {
167 PragmaCommentHandler(Sema &Actions)
168 : PragmaHandler("comment"), Actions(Actions) {}
169 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
170 Token &FirstToken) override;
175 struct PragmaDetectMismatchHandler : public PragmaHandler {
176 PragmaDetectMismatchHandler(Sema &Actions)
177 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
178 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
179 Token &FirstToken) override;
184 struct PragmaMSPointersToMembers : public PragmaHandler {
185 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
186 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
187 Token &FirstToken) override;
190 struct PragmaMSVtorDisp : public PragmaHandler {
191 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
192 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
193 Token &FirstToken) override;
196 struct PragmaMSPragma : public PragmaHandler {
197 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
198 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
199 Token &FirstToken) override;
202 /// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
203 struct PragmaOptimizeHandler : public PragmaHandler {
204 PragmaOptimizeHandler(Sema &S)
205 : PragmaHandler("optimize"), Actions(S) {}
206 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
207 Token &FirstToken) override;
212 struct PragmaLoopHintHandler : public PragmaHandler {
213 PragmaLoopHintHandler() : PragmaHandler("loop") {}
214 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
215 Token &FirstToken) override;
218 struct PragmaUnrollHintHandler : public PragmaHandler {
219 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
220 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
221 Token &FirstToken) override;
224 struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
225 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
228 struct PragmaMSIntrinsicHandler : public PragmaHandler {
229 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
230 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
231 Token &FirstToken) override;
234 struct PragmaMSOptimizeHandler : public PragmaHandler {
235 PragmaMSOptimizeHandler() : PragmaHandler("optimize") {}
236 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
237 Token &FirstToken) override;
240 struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
241 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
242 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
243 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
244 Token &FirstToken) override;
250 /// PragmaAttributeHandler - "\#pragma clang attribute ...".
251 struct PragmaAttributeHandler : public PragmaHandler {
252 PragmaAttributeHandler(AttributeFactory &AttrFactory)
253 : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {}
254 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
255 Token &FirstToken) override;
257 /// A pool of attributes that were parsed in \#pragma clang attribute.
258 ParsedAttributes AttributesForPragmaAttribute;
263 void Parser::initializePragmaHandlers() {
264 AlignHandler = llvm::make_unique<PragmaAlignHandler>();
265 PP.AddPragmaHandler(AlignHandler.get());
267 GCCVisibilityHandler = llvm::make_unique<PragmaGCCVisibilityHandler>();
268 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
270 OptionsHandler = llvm::make_unique<PragmaOptionsHandler>();
271 PP.AddPragmaHandler(OptionsHandler.get());
273 PackHandler = llvm::make_unique<PragmaPackHandler>();
274 PP.AddPragmaHandler(PackHandler.get());
276 MSStructHandler = llvm::make_unique<PragmaMSStructHandler>();
277 PP.AddPragmaHandler(MSStructHandler.get());
279 UnusedHandler = llvm::make_unique<PragmaUnusedHandler>();
280 PP.AddPragmaHandler(UnusedHandler.get());
282 WeakHandler = llvm::make_unique<PragmaWeakHandler>();
283 PP.AddPragmaHandler(WeakHandler.get());
285 RedefineExtnameHandler = llvm::make_unique<PragmaRedefineExtnameHandler>();
286 PP.AddPragmaHandler(RedefineExtnameHandler.get());
288 FPContractHandler = llvm::make_unique<PragmaFPContractHandler>();
289 PP.AddPragmaHandler("STDC", FPContractHandler.get());
291 STDCFENVHandler = llvm::make_unique<PragmaSTDC_FENV_ACCESSHandler>();
292 PP.AddPragmaHandler("STDC", STDCFENVHandler.get());
294 STDCCXLIMITHandler = llvm::make_unique<PragmaSTDC_CX_LIMITED_RANGEHandler>();
295 PP.AddPragmaHandler("STDC", STDCCXLIMITHandler.get());
297 STDCUnknownHandler = llvm::make_unique<PragmaSTDC_UnknownHandler>();
298 PP.AddPragmaHandler("STDC", STDCUnknownHandler.get());
300 PCSectionHandler = llvm::make_unique<PragmaClangSectionHandler>(Actions);
301 PP.AddPragmaHandler("clang", PCSectionHandler.get());
303 if (getLangOpts().OpenCL) {
304 OpenCLExtensionHandler = llvm::make_unique<PragmaOpenCLExtensionHandler>();
305 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
307 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
309 if (getLangOpts().OpenMP)
310 OpenMPHandler = llvm::make_unique<PragmaOpenMPHandler>();
312 OpenMPHandler = llvm::make_unique<PragmaNoOpenMPHandler>();
313 PP.AddPragmaHandler(OpenMPHandler.get());
315 if (getLangOpts().MicrosoftExt ||
316 getTargetInfo().getTriple().isOSBinFormatELF()) {
317 MSCommentHandler = llvm::make_unique<PragmaCommentHandler>(Actions);
318 PP.AddPragmaHandler(MSCommentHandler.get());
321 if (getLangOpts().MicrosoftExt) {
322 MSDetectMismatchHandler =
323 llvm::make_unique<PragmaDetectMismatchHandler>(Actions);
324 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
325 MSPointersToMembers = llvm::make_unique<PragmaMSPointersToMembers>();
326 PP.AddPragmaHandler(MSPointersToMembers.get());
327 MSVtorDisp = llvm::make_unique<PragmaMSVtorDisp>();
328 PP.AddPragmaHandler(MSVtorDisp.get());
329 MSInitSeg = llvm::make_unique<PragmaMSPragma>("init_seg");
330 PP.AddPragmaHandler(MSInitSeg.get());
331 MSDataSeg = llvm::make_unique<PragmaMSPragma>("data_seg");
332 PP.AddPragmaHandler(MSDataSeg.get());
333 MSBSSSeg = llvm::make_unique<PragmaMSPragma>("bss_seg");
334 PP.AddPragmaHandler(MSBSSSeg.get());
335 MSConstSeg = llvm::make_unique<PragmaMSPragma>("const_seg");
336 PP.AddPragmaHandler(MSConstSeg.get());
337 MSCodeSeg = llvm::make_unique<PragmaMSPragma>("code_seg");
338 PP.AddPragmaHandler(MSCodeSeg.get());
339 MSSection = llvm::make_unique<PragmaMSPragma>("section");
340 PP.AddPragmaHandler(MSSection.get());
341 MSRuntimeChecks = llvm::make_unique<PragmaMSRuntimeChecksHandler>();
342 PP.AddPragmaHandler(MSRuntimeChecks.get());
343 MSIntrinsic = llvm::make_unique<PragmaMSIntrinsicHandler>();
344 PP.AddPragmaHandler(MSIntrinsic.get());
345 MSOptimize = llvm::make_unique<PragmaMSOptimizeHandler>();
346 PP.AddPragmaHandler(MSOptimize.get());
349 if (getLangOpts().CUDA) {
350 CUDAForceHostDeviceHandler =
351 llvm::make_unique<PragmaForceCUDAHostDeviceHandler>(Actions);
352 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
355 OptimizeHandler = llvm::make_unique<PragmaOptimizeHandler>(Actions);
356 PP.AddPragmaHandler("clang", OptimizeHandler.get());
358 LoopHintHandler = llvm::make_unique<PragmaLoopHintHandler>();
359 PP.AddPragmaHandler("clang", LoopHintHandler.get());
361 UnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("unroll");
362 PP.AddPragmaHandler(UnrollHintHandler.get());
364 NoUnrollHintHandler = llvm::make_unique<PragmaUnrollHintHandler>("nounroll");
365 PP.AddPragmaHandler(NoUnrollHintHandler.get());
367 UnrollAndJamHintHandler =
368 llvm::make_unique<PragmaUnrollHintHandler>("unroll_and_jam");
369 PP.AddPragmaHandler(UnrollAndJamHintHandler.get());
371 NoUnrollAndJamHintHandler =
372 llvm::make_unique<PragmaUnrollHintHandler>("nounroll_and_jam");
373 PP.AddPragmaHandler(NoUnrollAndJamHintHandler.get());
375 FPHandler = llvm::make_unique<PragmaFPHandler>();
376 PP.AddPragmaHandler("clang", FPHandler.get());
378 AttributePragmaHandler =
379 llvm::make_unique<PragmaAttributeHandler>(AttrFactory);
380 PP.AddPragmaHandler("clang", AttributePragmaHandler.get());
383 void Parser::resetPragmaHandlers() {
384 // Remove the pragma handlers we installed.
385 PP.RemovePragmaHandler(AlignHandler.get());
386 AlignHandler.reset();
387 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
388 GCCVisibilityHandler.reset();
389 PP.RemovePragmaHandler(OptionsHandler.get());
390 OptionsHandler.reset();
391 PP.RemovePragmaHandler(PackHandler.get());
393 PP.RemovePragmaHandler(MSStructHandler.get());
394 MSStructHandler.reset();
395 PP.RemovePragmaHandler(UnusedHandler.get());
396 UnusedHandler.reset();
397 PP.RemovePragmaHandler(WeakHandler.get());
399 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
400 RedefineExtnameHandler.reset();
402 if (getLangOpts().OpenCL) {
403 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
404 OpenCLExtensionHandler.reset();
405 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
407 PP.RemovePragmaHandler(OpenMPHandler.get());
408 OpenMPHandler.reset();
410 if (getLangOpts().MicrosoftExt ||
411 getTargetInfo().getTriple().isOSBinFormatELF()) {
412 PP.RemovePragmaHandler(MSCommentHandler.get());
413 MSCommentHandler.reset();
416 PP.RemovePragmaHandler("clang", PCSectionHandler.get());
417 PCSectionHandler.reset();
419 if (getLangOpts().MicrosoftExt) {
420 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
421 MSDetectMismatchHandler.reset();
422 PP.RemovePragmaHandler(MSPointersToMembers.get());
423 MSPointersToMembers.reset();
424 PP.RemovePragmaHandler(MSVtorDisp.get());
426 PP.RemovePragmaHandler(MSInitSeg.get());
428 PP.RemovePragmaHandler(MSDataSeg.get());
430 PP.RemovePragmaHandler(MSBSSSeg.get());
432 PP.RemovePragmaHandler(MSConstSeg.get());
434 PP.RemovePragmaHandler(MSCodeSeg.get());
436 PP.RemovePragmaHandler(MSSection.get());
438 PP.RemovePragmaHandler(MSRuntimeChecks.get());
439 MSRuntimeChecks.reset();
440 PP.RemovePragmaHandler(MSIntrinsic.get());
442 PP.RemovePragmaHandler(MSOptimize.get());
446 if (getLangOpts().CUDA) {
447 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
448 CUDAForceHostDeviceHandler.reset();
451 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
452 FPContractHandler.reset();
454 PP.RemovePragmaHandler("STDC", STDCFENVHandler.get());
455 STDCFENVHandler.reset();
457 PP.RemovePragmaHandler("STDC", STDCCXLIMITHandler.get());
458 STDCCXLIMITHandler.reset();
460 PP.RemovePragmaHandler("STDC", STDCUnknownHandler.get());
461 STDCUnknownHandler.reset();
463 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
464 OptimizeHandler.reset();
466 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
467 LoopHintHandler.reset();
469 PP.RemovePragmaHandler(UnrollHintHandler.get());
470 UnrollHintHandler.reset();
472 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
473 NoUnrollHintHandler.reset();
475 PP.RemovePragmaHandler(UnrollAndJamHintHandler.get());
476 UnrollAndJamHintHandler.reset();
478 PP.RemovePragmaHandler(NoUnrollAndJamHintHandler.get());
479 NoUnrollAndJamHintHandler.reset();
481 PP.RemovePragmaHandler("clang", FPHandler.get());
484 PP.RemovePragmaHandler("clang", AttributePragmaHandler.get());
485 AttributePragmaHandler.reset();
488 /// Handle the annotation token produced for #pragma unused(...)
490 /// Each annot_pragma_unused is followed by the argument token so e.g.
491 /// "#pragma unused(x,y)" becomes:
492 /// annot_pragma_unused 'x' annot_pragma_unused 'y'
493 void Parser::HandlePragmaUnused() {
494 assert(Tok.is(tok::annot_pragma_unused));
495 SourceLocation UnusedLoc = ConsumeAnnotationToken();
496 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
497 ConsumeToken(); // The argument token.
500 void Parser::HandlePragmaVisibility() {
501 assert(Tok.is(tok::annot_pragma_vis));
502 const IdentifierInfo *VisType =
503 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
504 SourceLocation VisLoc = ConsumeAnnotationToken();
505 Actions.ActOnPragmaVisibility(VisType, VisLoc);
509 struct PragmaPackInfo {
510 Sema::PragmaMsStackAction Action;
514 } // end anonymous namespace
516 void Parser::HandlePragmaPack() {
517 assert(Tok.is(tok::annot_pragma_pack));
518 PragmaPackInfo *Info =
519 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
520 SourceLocation PragmaLoc = Tok.getLocation();
521 ExprResult Alignment;
522 if (Info->Alignment.is(tok::numeric_constant)) {
523 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
524 if (Alignment.isInvalid()) {
525 ConsumeAnnotationToken();
529 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
531 // Consume the token after processing the pragma to enable pragma-specific
532 // #include warnings.
533 ConsumeAnnotationToken();
536 void Parser::HandlePragmaMSStruct() {
537 assert(Tok.is(tok::annot_pragma_msstruct));
538 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
539 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
540 Actions.ActOnPragmaMSStruct(Kind);
541 ConsumeAnnotationToken();
544 void Parser::HandlePragmaAlign() {
545 assert(Tok.is(tok::annot_pragma_align));
546 Sema::PragmaOptionsAlignKind Kind =
547 static_cast<Sema::PragmaOptionsAlignKind>(
548 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
549 Actions.ActOnPragmaOptionsAlign(Kind, Tok.getLocation());
550 // Consume the token after processing the pragma to enable pragma-specific
551 // #include warnings.
552 ConsumeAnnotationToken();
555 void Parser::HandlePragmaDump() {
556 assert(Tok.is(tok::annot_pragma_dump));
558 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
559 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
560 ConsumeAnnotationToken();
563 void Parser::HandlePragmaWeak() {
564 assert(Tok.is(tok::annot_pragma_weak));
565 SourceLocation PragmaLoc = ConsumeAnnotationToken();
566 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
568 ConsumeToken(); // The weak name.
571 void Parser::HandlePragmaWeakAlias() {
572 assert(Tok.is(tok::annot_pragma_weakalias));
573 SourceLocation PragmaLoc = ConsumeAnnotationToken();
574 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
575 SourceLocation WeakNameLoc = Tok.getLocation();
577 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
578 SourceLocation AliasNameLoc = Tok.getLocation();
580 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
581 WeakNameLoc, AliasNameLoc);
585 void Parser::HandlePragmaRedefineExtname() {
586 assert(Tok.is(tok::annot_pragma_redefine_extname));
587 SourceLocation RedefLoc = ConsumeAnnotationToken();
588 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
589 SourceLocation RedefNameLoc = Tok.getLocation();
591 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
592 SourceLocation AliasNameLoc = Tok.getLocation();
594 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
595 RedefNameLoc, AliasNameLoc);
598 void Parser::HandlePragmaFPContract() {
599 assert(Tok.is(tok::annot_pragma_fp_contract));
600 tok::OnOffSwitch OOS =
601 static_cast<tok::OnOffSwitch>(
602 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
604 LangOptions::FPContractModeKind FPC;
607 FPC = LangOptions::FPC_On;
610 FPC = LangOptions::FPC_Off;
612 case tok::OOS_DEFAULT:
613 FPC = getLangOpts().getDefaultFPContractMode();
617 Actions.ActOnPragmaFPContract(FPC);
618 ConsumeAnnotationToken();
621 void Parser::HandlePragmaFEnvAccess() {
622 assert(Tok.is(tok::annot_pragma_fenv_access));
623 tok::OnOffSwitch OOS =
624 static_cast<tok::OnOffSwitch>(
625 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
627 LangOptions::FEnvAccessModeKind FPC;
630 FPC = LangOptions::FEA_On;
633 FPC = LangOptions::FEA_Off;
635 case tok::OOS_DEFAULT: // FIXME: Add this cli option when it makes sense.
636 FPC = LangOptions::FEA_Off;
640 Actions.ActOnPragmaFEnvAccess(FPC);
641 ConsumeAnnotationToken();
645 StmtResult Parser::HandlePragmaCaptured()
647 assert(Tok.is(tok::annot_pragma_captured));
648 ConsumeAnnotationToken();
650 if (Tok.isNot(tok::l_brace)) {
651 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
655 SourceLocation Loc = Tok.getLocation();
657 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope |
658 Scope::CompoundStmtScope);
659 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
662 StmtResult R = ParseCompoundStatement();
663 CapturedRegionScope.Exit();
666 Actions.ActOnCapturedRegionError();
670 return Actions.ActOnCapturedRegionEnd(R.get());
674 enum OpenCLExtState : char {
675 Disable, Enable, Begin, End
677 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
680 void Parser::HandlePragmaOpenCLExtension() {
681 assert(Tok.is(tok::annot_pragma_opencl_extension));
682 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
683 auto State = Data->second;
684 auto Ident = Data->first;
685 SourceLocation NameLoc = Tok.getLocation();
686 ConsumeAnnotationToken();
688 auto &Opt = Actions.getOpenCLOptions();
689 auto Name = Ident->getName();
690 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
691 // overriding all previously issued extension directives, but only if the
692 // behavior is set to disable."
694 if (State == Disable) {
696 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
698 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
700 } else if (State == Begin) {
701 if (!Opt.isKnown(Name) ||
702 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
705 Actions.setCurrentOpenCLExtension(Name);
706 } else if (State == End) {
707 if (Name != Actions.getCurrentOpenCLExtension())
708 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
709 Actions.setCurrentOpenCLExtension("");
710 } else if (!Opt.isKnown(Name))
711 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
712 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
713 Opt.enable(Name, State == Enable);
714 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
715 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
717 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
720 void Parser::HandlePragmaMSPointersToMembers() {
721 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
722 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
723 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
724 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
725 SourceLocation PragmaLoc = ConsumeAnnotationToken();
726 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
729 void Parser::HandlePragmaMSVtorDisp() {
730 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
731 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
732 Sema::PragmaMsStackAction Action =
733 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
734 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
735 SourceLocation PragmaLoc = ConsumeAnnotationToken();
736 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
739 void Parser::HandlePragmaMSPragma() {
740 assert(Tok.is(tok::annot_pragma_ms_pragma));
741 // Grab the tokens out of the annotation and enter them into the stream.
743 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
744 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
745 SourceLocation PragmaLocation = ConsumeAnnotationToken();
746 assert(Tok.isAnyIdentifier());
747 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
748 PP.Lex(Tok); // pragma kind
750 // Figure out which #pragma we're dealing with. The switch has no default
751 // because lex shouldn't emit the annotation token for unrecognized pragmas.
752 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
753 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
754 .Case("data_seg", &Parser::HandlePragmaMSSegment)
755 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
756 .Case("const_seg", &Parser::HandlePragmaMSSegment)
757 .Case("code_seg", &Parser::HandlePragmaMSSegment)
758 .Case("section", &Parser::HandlePragmaMSSection)
759 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
761 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
762 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
763 // until eof (really end of line) to prevent follow-on errors.
764 while (Tok.isNot(tok::eof))
770 bool Parser::HandlePragmaMSSection(StringRef PragmaName,
771 SourceLocation PragmaLocation) {
772 if (Tok.isNot(tok::l_paren)) {
773 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
777 // Parsing code for pragma section
778 if (Tok.isNot(tok::string_literal)) {
779 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
783 ExprResult StringResult = ParseStringLiteralExpression();
784 if (StringResult.isInvalid())
785 return false; // Already diagnosed.
786 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
787 if (SegmentName->getCharByteWidth() != 1) {
788 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
792 int SectionFlags = ASTContext::PSF_Read;
793 bool SectionFlagsAreDefault = true;
794 while (Tok.is(tok::comma)) {
796 // Ignore "long" and "short".
797 // They are undocumented, but widely used, section attributes which appear
799 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
800 PP.Lex(Tok); // long/short
804 if (!Tok.isAnyIdentifier()) {
805 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
809 ASTContext::PragmaSectionFlag Flag =
810 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
811 Tok.getIdentifierInfo()->getName())
812 .Case("read", ASTContext::PSF_Read)
813 .Case("write", ASTContext::PSF_Write)
814 .Case("execute", ASTContext::PSF_Execute)
815 .Case("shared", ASTContext::PSF_Invalid)
816 .Case("nopage", ASTContext::PSF_Invalid)
817 .Case("nocache", ASTContext::PSF_Invalid)
818 .Case("discard", ASTContext::PSF_Invalid)
819 .Case("remove", ASTContext::PSF_Invalid)
820 .Default(ASTContext::PSF_None);
821 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
822 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
823 ? diag::warn_pragma_invalid_specific_action
824 : diag::warn_pragma_unsupported_action)
825 << PragmaName << Tok.getIdentifierInfo()->getName();
828 SectionFlags |= Flag;
829 SectionFlagsAreDefault = false;
830 PP.Lex(Tok); // Identifier
832 // If no section attributes are specified, the section will be marked as
834 if (SectionFlagsAreDefault)
835 SectionFlags |= ASTContext::PSF_Write;
836 if (Tok.isNot(tok::r_paren)) {
837 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
841 if (Tok.isNot(tok::eof)) {
842 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
847 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
851 bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
852 SourceLocation PragmaLocation) {
853 if (Tok.isNot(tok::l_paren)) {
854 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
858 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
860 if (Tok.isAnyIdentifier()) {
861 StringRef PushPop = Tok.getIdentifierInfo()->getName();
862 if (PushPop == "push")
863 Action = Sema::PSK_Push;
864 else if (PushPop == "pop")
865 Action = Sema::PSK_Pop;
867 PP.Diag(PragmaLocation,
868 diag::warn_pragma_expected_section_push_pop_or_name)
872 if (Action != Sema::PSK_Reset) {
873 PP.Lex(Tok); // push | pop
874 if (Tok.is(tok::comma)) {
876 // If we've got a comma, we either need a label or a string.
877 if (Tok.isAnyIdentifier()) {
878 SlotLabel = Tok.getIdentifierInfo()->getName();
879 PP.Lex(Tok); // identifier
880 if (Tok.is(tok::comma))
882 else if (Tok.isNot(tok::r_paren)) {
883 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
888 } else if (Tok.isNot(tok::r_paren)) {
889 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
894 // Grab the string literal for our section name.
895 StringLiteral *SegmentName = nullptr;
896 if (Tok.isNot(tok::r_paren)) {
897 if (Tok.isNot(tok::string_literal)) {
898 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
899 diag::warn_pragma_expected_section_name :
900 diag::warn_pragma_expected_section_label_or_name :
901 diag::warn_pragma_expected_section_push_pop_or_name;
902 PP.Diag(PragmaLocation, DiagID) << PragmaName;
905 ExprResult StringResult = ParseStringLiteralExpression();
906 if (StringResult.isInvalid())
907 return false; // Already diagnosed.
908 SegmentName = cast<StringLiteral>(StringResult.get());
909 if (SegmentName->getCharByteWidth() != 1) {
910 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
914 // Setting section "" has no effect
915 if (SegmentName->getLength())
916 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
918 if (Tok.isNot(tok::r_paren)) {
919 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
923 if (Tok.isNot(tok::eof)) {
924 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
929 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
930 SegmentName, PragmaName);
934 // #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
935 bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
936 SourceLocation PragmaLocation) {
937 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
938 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
942 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
946 // Parse either the known section names or the string section name.
947 StringLiteral *SegmentName = nullptr;
948 if (Tok.isAnyIdentifier()) {
949 auto *II = Tok.getIdentifierInfo();
950 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
951 .Case("compiler", "\".CRT$XCC\"")
952 .Case("lib", "\".CRT$XCL\"")
953 .Case("user", "\".CRT$XCU\"")
956 if (!Section.empty()) {
957 // Pretend the user wrote the appropriate string literal here.
959 Toks[0].startToken();
960 Toks[0].setKind(tok::string_literal);
961 Toks[0].setLocation(Tok.getLocation());
962 Toks[0].setLiteralData(Section.data());
963 Toks[0].setLength(Section.size());
965 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
968 } else if (Tok.is(tok::string_literal)) {
969 ExprResult StringResult = ParseStringLiteralExpression();
970 if (StringResult.isInvalid())
972 SegmentName = cast<StringLiteral>(StringResult.get());
973 if (SegmentName->getCharByteWidth() != 1) {
974 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
978 // FIXME: Add support for the '[, func-name]' part of the pragma.
982 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
986 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
988 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
992 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
997 struct PragmaLoopHintInfo {
1000 ArrayRef<Token> Toks;
1002 } // end anonymous namespace
1004 static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
1005 std::string PragmaString;
1006 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
1007 PragmaString = "clang loop ";
1008 PragmaString += Option.getIdentifierInfo()->getName();
1009 } else if (PragmaName.getIdentifierInfo()->getName() == "unroll_and_jam") {
1010 PragmaString = "unroll_and_jam";
1012 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
1013 "Unexpected pragma name");
1014 PragmaString = "unroll";
1016 return PragmaString;
1019 bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
1020 assert(Tok.is(tok::annot_pragma_loop_hint));
1021 PragmaLoopHintInfo *Info =
1022 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
1024 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
1025 Hint.PragmaNameLoc = IdentifierLoc::create(
1026 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
1028 // It is possible that the loop hint has no option identifier, such as
1029 // #pragma unroll(4).
1030 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
1031 ? Info->Option.getIdentifierInfo()
1033 Hint.OptionLoc = IdentifierLoc::create(
1034 Actions.Context, Info->Option.getLocation(), OptionInfo);
1036 llvm::ArrayRef<Token> Toks = Info->Toks;
1038 // Return a valid hint if pragma unroll or nounroll were specified
1039 // without an argument.
1040 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
1041 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
1042 bool PragmaUnrollAndJam = PragmaNameInfo->getName() == "unroll_and_jam";
1043 bool PragmaNoUnrollAndJam = PragmaNameInfo->getName() == "nounroll_and_jam";
1044 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll || PragmaUnrollAndJam ||
1045 PragmaNoUnrollAndJam)) {
1046 ConsumeAnnotationToken();
1047 Hint.Range = Info->PragmaName.getLocation();
1051 // The constant expression is always followed by an eof token, which increases
1052 // the TokSize by 1.
1053 assert(!Toks.empty() &&
1054 "PragmaLoopHintInfo::Toks must contain at least one token.");
1056 // If no option is specified the argument is assumed to be a constant expr.
1057 bool OptionUnroll = false;
1058 bool OptionUnrollAndJam = false;
1059 bool OptionDistribute = false;
1060 bool OptionPipelineDisabled = false;
1061 bool StateOption = false;
1062 if (OptionInfo) { // Pragma Unroll does not specify an option.
1063 OptionUnroll = OptionInfo->isStr("unroll");
1064 OptionUnrollAndJam = OptionInfo->isStr("unroll_and_jam");
1065 OptionDistribute = OptionInfo->isStr("distribute");
1066 OptionPipelineDisabled = OptionInfo->isStr("pipeline");
1067 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
1068 .Case("vectorize", true)
1069 .Case("interleave", true)
1071 OptionUnroll || OptionUnrollAndJam || OptionDistribute ||
1072 OptionPipelineDisabled;
1075 bool AssumeSafetyArg = !OptionUnroll && !OptionUnrollAndJam &&
1076 !OptionDistribute && !OptionPipelineDisabled;
1077 // Verify loop hint has an argument.
1078 if (Toks[0].is(tok::eof)) {
1079 ConsumeAnnotationToken();
1080 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
1081 << /*StateArgument=*/StateOption
1082 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
1083 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
1087 // Validate the argument.
1089 ConsumeAnnotationToken();
1090 SourceLocation StateLoc = Toks[0].getLocation();
1091 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
1093 bool Valid = StateInfo &&
1094 llvm::StringSwitch<bool>(StateInfo->getName())
1095 .Case("disable", true)
1096 .Case("enable", !OptionPipelineDisabled)
1097 .Case("full", OptionUnroll || OptionUnrollAndJam)
1098 .Case("assume_safety", AssumeSafetyArg)
1101 if (OptionPipelineDisabled) {
1102 Diag(Toks[0].getLocation(), diag::err_pragma_pipeline_invalid_keyword);
1104 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
1105 << /*FullKeyword=*/(OptionUnroll || OptionUnrollAndJam)
1106 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
1110 if (Toks.size() > 2)
1111 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1112 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1113 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
1115 // Enter constant expression including eof terminator into token stream.
1116 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
1117 ConsumeAnnotationToken();
1119 ExprResult R = ParseConstantExpression();
1121 // Tokens following an error in an ill-formed constant expression will
1122 // remain in the token stream and must be removed.
1123 if (Tok.isNot(tok::eof)) {
1124 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1125 << PragmaLoopHintString(Info->PragmaName, Info->Option);
1126 while (Tok.isNot(tok::eof))
1130 ConsumeToken(); // Consume the constant expression eof terminator.
1132 if (R.isInvalid() ||
1133 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
1136 // Argument is a constant expression with an integer type.
1137 Hint.ValueExpr = R.get();
1140 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
1141 Info->Toks.back().getLocation());
1146 struct PragmaAttributeInfo {
1147 enum ActionType { Push, Pop, Attribute };
1148 ParsedAttributes &Attributes;
1150 const IdentifierInfo *Namespace = nullptr;
1151 ArrayRef<Token> Tokens;
1153 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1156 #include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1158 } // end anonymous namespace
1160 static StringRef getIdentifier(const Token &Tok) {
1161 if (Tok.is(tok::identifier))
1162 return Tok.getIdentifierInfo()->getName();
1163 const char *S = tok::getKeywordSpelling(Tok.getKind());
1169 static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1170 using namespace attr;
1172 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1175 #include "clang/Basic/AttrSubMatchRulesList.inc"
1177 llvm_unreachable("Invalid attribute subject match rule");
1181 static void diagnoseExpectedAttributeSubjectSubRule(
1182 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1183 SourceLocation SubRuleLoc) {
1185 PRef.Diag(SubRuleLoc,
1186 diag::err_pragma_attribute_expected_subject_sub_identifier)
1188 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1189 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1191 Diagnostic << /*SubRulesSupported=*/0;
1194 static void diagnoseUnknownAttributeSubjectSubRule(
1195 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1196 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1199 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1200 << SubRuleName << PrimaryRuleName;
1201 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1202 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1204 Diagnostic << /*SubRulesSupported=*/0;
1207 bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1208 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1209 SourceLocation &LastMatchRuleEndLoc) {
1211 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1212 if (getIdentifier(Tok) == "any") {
1213 AnyLoc = ConsumeToken();
1215 if (AnyParens.expectAndConsume())
1220 // Parse the subject matcher rule.
1221 StringRef Name = getIdentifier(Tok);
1223 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1226 std::pair<Optional<attr::SubjectMatchRule>,
1227 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1228 Rule = isAttributeSubjectMatchRule(Name);
1230 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1233 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1234 SourceLocation RuleLoc = ConsumeToken();
1236 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1237 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1238 if (Parens.expectAndConsume())
1240 } else if (Parens.consumeOpen()) {
1241 if (!SubjectMatchRules
1243 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1245 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1247 << FixItHint::CreateRemoval(SourceRange(
1248 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1249 LastMatchRuleEndLoc = RuleLoc;
1253 // Parse the sub-rules.
1254 StringRef SubRuleName = getIdentifier(Tok);
1255 if (SubRuleName.empty()) {
1256 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1260 attr::SubjectMatchRule SubRule;
1261 if (SubRuleName == "unless") {
1262 SourceLocation SubRuleLoc = ConsumeToken();
1263 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1264 if (Parens.expectAndConsume())
1266 SubRuleName = getIdentifier(Tok);
1267 if (SubRuleName.empty()) {
1268 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1272 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1273 if (!SubRuleOrNone) {
1274 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1275 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1276 SubRuleUnlessName, SubRuleLoc);
1279 SubRule = *SubRuleOrNone;
1281 if (Parens.consumeClose())
1284 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1285 if (!SubRuleOrNone) {
1286 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1287 SubRuleName, Tok.getLocation());
1290 SubRule = *SubRuleOrNone;
1293 SourceLocation RuleEndLoc = Tok.getLocation();
1294 LastMatchRuleEndLoc = RuleEndLoc;
1295 if (Parens.consumeClose())
1297 if (!SubjectMatchRules
1298 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1300 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1301 << attr::getSubjectMatchRuleSpelling(SubRule)
1302 << FixItHint::CreateRemoval(SourceRange(
1303 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1306 } while (IsAny && TryConsumeToken(tok::comma));
1309 if (AnyParens.consumeClose())
1317 /// Describes the stage at which attribute subject rule parsing was interrupted.
1318 enum class MissingAttributeSubjectRulesRecoveryPoint {
1326 MissingAttributeSubjectRulesRecoveryPoint
1327 getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1328 if (const auto *II = Tok.getIdentifierInfo()) {
1329 if (II->isStr("apply_to"))
1330 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1331 if (II->isStr("any"))
1332 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1334 if (Tok.is(tok::equal))
1335 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1336 return MissingAttributeSubjectRulesRecoveryPoint::None;
1339 /// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1340 /// suggests the possible attribute subject rules in a fix-it together with
1341 /// any other missing tokens.
1342 DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
1343 unsigned DiagID, ParsedAttr &Attribute,
1344 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1345 SourceLocation Loc = PRef.getEndOfPreviousToken();
1346 if (Loc.isInvalid())
1347 Loc = PRef.getCurToken().getLocation();
1348 auto Diagnostic = PRef.Diag(Loc, DiagID);
1350 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1351 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1352 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1354 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1355 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1356 FixIt += "apply_to";
1357 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1358 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1360 SourceRange FixItRange(Loc);
1361 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1362 // Gather the subject match rules that are supported by the attribute.
1363 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1364 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1365 if (SubjectMatchRuleSet.empty()) {
1366 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1367 // placeholders will be supported by the fix-its.
1371 bool NeedsComma = false;
1372 for (const auto &I : SubjectMatchRuleSet) {
1373 // Ensure that the missing rule is reported in the fix-it only when it's
1374 // supported in the current language mode.
1381 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1384 // Check if we need to remove the range
1385 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1386 FixItRange.setEnd(PRef.getCurToken().getLocation());
1388 if (FixItRange.getBegin() == FixItRange.getEnd())
1389 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1391 Diagnostic << FixItHint::CreateReplacement(
1392 CharSourceRange::getCharRange(FixItRange), FixIt);
1396 } // end anonymous namespace
1398 void Parser::HandlePragmaAttribute() {
1399 assert(Tok.is(tok::annot_pragma_attribute) &&
1400 "Expected #pragma attribute annotation token");
1401 SourceLocation PragmaLoc = Tok.getLocation();
1402 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1403 if (Info->Action == PragmaAttributeInfo::Pop) {
1404 ConsumeAnnotationToken();
1405 Actions.ActOnPragmaAttributePop(PragmaLoc, Info->Namespace);
1408 // Parse the actual attribute with its arguments.
1409 assert((Info->Action == PragmaAttributeInfo::Push ||
1410 Info->Action == PragmaAttributeInfo::Attribute) &&
1411 "Unexpected #pragma attribute command");
1413 if (Info->Action == PragmaAttributeInfo::Push && Info->Tokens.empty()) {
1414 ConsumeAnnotationToken();
1415 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace);
1419 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false);
1420 ConsumeAnnotationToken();
1422 ParsedAttributes &Attrs = Info->Attributes;
1423 Attrs.clearListOnly();
1425 auto SkipToEnd = [this]() {
1426 SkipUntil(tok::eof, StopBeforeMatch);
1430 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1431 // Parse the CXX11 style attribute.
1432 ParseCXX11AttributeSpecifier(Attrs);
1433 } else if (Tok.is(tok::kw___attribute)) {
1435 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1438 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1441 if (Tok.isNot(tok::identifier)) {
1442 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1446 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1447 SourceLocation AttrNameLoc = ConsumeToken();
1449 if (Tok.isNot(tok::l_paren))
1450 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
1451 ParsedAttr::AS_GNU);
1453 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1454 /*ScopeName=*/nullptr,
1455 /*ScopeLoc=*/SourceLocation(), ParsedAttr::AS_GNU,
1456 /*Declarator=*/nullptr);
1458 if (ExpectAndConsume(tok::r_paren))
1460 if (ExpectAndConsume(tok::r_paren))
1462 } else if (Tok.is(tok::kw___declspec)) {
1463 ParseMicrosoftDeclSpecs(Attrs);
1465 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1466 if (Tok.getIdentifierInfo()) {
1467 // If we suspect that this is an attribute suggest the use of
1469 if (ParsedAttr::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1470 ParsedAttr::AS_GNU) !=
1471 ParsedAttr::UnknownAttribute) {
1472 SourceLocation InsertStartLoc = Tok.getLocation();
1474 if (Tok.is(tok::l_paren)) {
1476 SkipUntil(tok::r_paren, StopBeforeMatch);
1477 if (Tok.isNot(tok::r_paren))
1480 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1481 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1482 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1489 if (Attrs.empty() || Attrs.begin()->isInvalid()) {
1494 // Ensure that we don't have more than one attribute.
1495 if (Attrs.size() > 1) {
1496 SourceLocation Loc = Attrs[1].getLoc();
1497 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1502 ParsedAttr &Attribute = *Attrs.begin();
1503 if (!Attribute.isSupportedByPragmaAttribute()) {
1504 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
1505 << Attribute.getName();
1510 // Parse the subject-list.
1511 if (!TryConsumeToken(tok::comma)) {
1512 createExpectedAttributeSubjectRulesTokenDiagnostic(
1513 diag::err_expected, Attribute,
1514 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1520 if (Tok.isNot(tok::identifier)) {
1521 createExpectedAttributeSubjectRulesTokenDiagnostic(
1522 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1523 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1527 const IdentifierInfo *II = Tok.getIdentifierInfo();
1528 if (!II->isStr("apply_to")) {
1529 createExpectedAttributeSubjectRulesTokenDiagnostic(
1530 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1531 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1537 if (!TryConsumeToken(tok::equal)) {
1538 createExpectedAttributeSubjectRulesTokenDiagnostic(
1539 diag::err_expected, Attribute,
1540 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1546 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1547 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1548 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1549 LastMatchRuleEndLoc)) {
1554 // Tokens following an ill-formed attribute will remain in the token stream
1555 // and must be removed.
1556 if (Tok.isNot(tok::eof)) {
1557 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1562 // Consume the eof terminator token.
1565 // Handle a mixed push/attribute by desurging to a push, then an attribute.
1566 if (Info->Action == PragmaAttributeInfo::Push)
1567 Actions.ActOnPragmaAttributeEmptyPush(PragmaLoc, Info->Namespace);
1569 Actions.ActOnPragmaAttributeAttribute(Attribute, PragmaLoc,
1570 std::move(SubjectMatchRules));
1573 // #pragma GCC visibility comes in two variants:
1574 // 'push' '(' [visibility] ')'
1576 void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
1577 PragmaIntroducerKind Introducer,
1579 SourceLocation VisLoc = VisTok.getLocation();
1582 PP.LexUnexpandedToken(Tok);
1584 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1586 const IdentifierInfo *VisType;
1587 if (PushPop && PushPop->isStr("pop")) {
1589 } else if (PushPop && PushPop->isStr("push")) {
1590 PP.LexUnexpandedToken(Tok);
1591 if (Tok.isNot(tok::l_paren)) {
1592 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1596 PP.LexUnexpandedToken(Tok);
1597 VisType = Tok.getIdentifierInfo();
1599 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1603 PP.LexUnexpandedToken(Tok);
1604 if (Tok.isNot(tok::r_paren)) {
1605 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1610 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1614 SourceLocation EndLoc = Tok.getLocation();
1615 PP.LexUnexpandedToken(Tok);
1616 if (Tok.isNot(tok::eod)) {
1617 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1622 auto Toks = llvm::make_unique<Token[]>(1);
1623 Toks[0].startToken();
1624 Toks[0].setKind(tok::annot_pragma_vis);
1625 Toks[0].setLocation(VisLoc);
1626 Toks[0].setAnnotationEndLoc(EndLoc);
1627 Toks[0].setAnnotationValue(
1628 const_cast<void*>(static_cast<const void*>(VisType)));
1629 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
1632 // #pragma pack(...) comes in the following delicious flavors:
1633 // pack '(' [integer] ')'
1634 // pack '(' 'show' ')'
1635 // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
1636 void PragmaPackHandler::HandlePragma(Preprocessor &PP,
1637 PragmaIntroducerKind Introducer,
1639 SourceLocation PackLoc = PackTok.getLocation();
1643 if (Tok.isNot(tok::l_paren)) {
1644 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
1648 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1649 StringRef SlotLabel;
1651 Alignment.startToken();
1653 if (Tok.is(tok::numeric_constant)) {
1658 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1659 // the push/pop stack.
1660 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
1662 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
1663 } else if (Tok.is(tok::identifier)) {
1664 const IdentifierInfo *II = Tok.getIdentifierInfo();
1665 if (II->isStr("show")) {
1666 Action = Sema::PSK_Show;
1669 if (II->isStr("push")) {
1670 Action = Sema::PSK_Push;
1671 } else if (II->isStr("pop")) {
1672 Action = Sema::PSK_Pop;
1674 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
1679 if (Tok.is(tok::comma)) {
1682 if (Tok.is(tok::numeric_constant)) {
1683 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1687 } else if (Tok.is(tok::identifier)) {
1688 SlotLabel = Tok.getIdentifierInfo()->getName();
1691 if (Tok.is(tok::comma)) {
1694 if (Tok.isNot(tok::numeric_constant)) {
1695 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1699 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1705 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1710 } else if (PP.getLangOpts().ApplePragmaPack) {
1711 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1712 // the push/pop stack.
1713 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1714 Action = Sema::PSK_Pop;
1717 if (Tok.isNot(tok::r_paren)) {
1718 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
1722 SourceLocation RParenLoc = Tok.getLocation();
1724 if (Tok.isNot(tok::eod)) {
1725 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1729 PragmaPackInfo *Info =
1730 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
1731 Info->Action = Action;
1732 Info->SlotLabel = SlotLabel;
1733 Info->Alignment = Alignment;
1735 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1737 Toks[0].startToken();
1738 Toks[0].setKind(tok::annot_pragma_pack);
1739 Toks[0].setLocation(PackLoc);
1740 Toks[0].setAnnotationEndLoc(RParenLoc);
1741 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1742 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1745 // #pragma ms_struct on
1746 // #pragma ms_struct off
1747 void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1748 PragmaIntroducerKind Introducer,
1749 Token &MSStructTok) {
1750 PragmaMSStructKind Kind = PMSST_OFF;
1754 if (Tok.isNot(tok::identifier)) {
1755 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1758 SourceLocation EndLoc = Tok.getLocation();
1759 const IdentifierInfo *II = Tok.getIdentifierInfo();
1760 if (II->isStr("on")) {
1764 else if (II->isStr("off") || II->isStr("reset"))
1767 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1771 if (Tok.isNot(tok::eod)) {
1772 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1777 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1779 Toks[0].startToken();
1780 Toks[0].setKind(tok::annot_pragma_msstruct);
1781 Toks[0].setLocation(MSStructTok.getLocation());
1782 Toks[0].setAnnotationEndLoc(EndLoc);
1783 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1784 static_cast<uintptr_t>(Kind)));
1785 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1788 // #pragma clang section bss="abc" data="" rodata="def" text=""
1789 void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
1790 PragmaIntroducerKind Introducer, Token &FirstToken) {
1793 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1795 PP.Lex(Tok); // eat 'section'
1796 while (Tok.isNot(tok::eod)) {
1797 if (Tok.isNot(tok::identifier)) {
1798 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1802 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1803 if (SecType->isStr("bss"))
1804 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1805 else if (SecType->isStr("data"))
1806 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1807 else if (SecType->isStr("rodata"))
1808 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1809 else if (SecType->isStr("text"))
1810 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1812 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1816 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1817 if (Tok.isNot(tok::equal)) {
1818 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1822 std::string SecName;
1823 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1826 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1827 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1828 Sema::PragmaClangSectionAction::PCSA_Clear),
1833 // #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1834 // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
1835 static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
1841 if (Tok.isNot(tok::identifier) ||
1842 !Tok.getIdentifierInfo()->isStr("align")) {
1843 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1849 if (Tok.isNot(tok::equal)) {
1850 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1856 if (Tok.isNot(tok::identifier)) {
1857 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1858 << (IsOptions ? "options" : "align");
1862 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
1863 const IdentifierInfo *II = Tok.getIdentifierInfo();
1864 if (II->isStr("native"))
1865 Kind = Sema::POAK_Native;
1866 else if (II->isStr("natural"))
1867 Kind = Sema::POAK_Natural;
1868 else if (II->isStr("packed"))
1869 Kind = Sema::POAK_Packed;
1870 else if (II->isStr("power"))
1871 Kind = Sema::POAK_Power;
1872 else if (II->isStr("mac68k"))
1873 Kind = Sema::POAK_Mac68k;
1874 else if (II->isStr("reset"))
1875 Kind = Sema::POAK_Reset;
1877 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1882 SourceLocation EndLoc = Tok.getLocation();
1884 if (Tok.isNot(tok::eod)) {
1885 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1886 << (IsOptions ? "options" : "align");
1890 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1892 Toks[0].startToken();
1893 Toks[0].setKind(tok::annot_pragma_align);
1894 Toks[0].setLocation(FirstTok.getLocation());
1895 Toks[0].setAnnotationEndLoc(EndLoc);
1896 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1897 static_cast<uintptr_t>(Kind)));
1898 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1901 void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1902 PragmaIntroducerKind Introducer,
1904 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
1907 void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1908 PragmaIntroducerKind Introducer,
1909 Token &OptionsTok) {
1910 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
1913 // #pragma unused(identifier)
1914 void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1915 PragmaIntroducerKind Introducer,
1917 // FIXME: Should we be expanding macros here? My guess is no.
1918 SourceLocation UnusedLoc = UnusedTok.getLocation();
1920 // Lex the left '('.
1923 if (Tok.isNot(tok::l_paren)) {
1924 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1928 // Lex the declaration reference(s).
1929 SmallVector<Token, 5> Identifiers;
1930 SourceLocation RParenLoc;
1937 if (Tok.is(tok::identifier)) {
1938 Identifiers.push_back(Tok);
1944 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1948 // We are execting a ')' or a ','.
1949 if (Tok.is(tok::comma)) {
1954 if (Tok.is(tok::r_paren)) {
1955 RParenLoc = Tok.getLocation();
1960 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
1965 if (Tok.isNot(tok::eod)) {
1966 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1971 // Verify that we have a location for the right parenthesis.
1972 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
1973 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
1975 // For each identifier token, insert into the token stream a
1976 // annot_pragma_unused token followed by the identifier token.
1977 // This allows us to cache a "#pragma unused" that occurs inside an inline
1978 // C++ member function.
1980 MutableArrayRef<Token> Toks(
1981 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1982 2 * Identifiers.size());
1983 for (unsigned i=0; i != Identifiers.size(); i++) {
1984 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1985 pragmaUnusedTok.startToken();
1986 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1987 pragmaUnusedTok.setLocation(UnusedLoc);
1988 idTok = Identifiers[i];
1990 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1993 // #pragma weak identifier
1994 // #pragma weak identifier '=' identifier
1995 void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1996 PragmaIntroducerKind Introducer,
1998 SourceLocation WeakLoc = WeakTok.getLocation();
2002 if (Tok.isNot(tok::identifier)) {
2003 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
2007 Token WeakName = Tok;
2008 bool HasAlias = false;
2012 if (Tok.is(tok::equal)) {
2015 if (Tok.isNot(tok::identifier)) {
2016 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2024 if (Tok.isNot(tok::eod)) {
2025 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
2030 MutableArrayRef<Token> Toks(
2031 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
2032 Token &pragmaUnusedTok = Toks[0];
2033 pragmaUnusedTok.startToken();
2034 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
2035 pragmaUnusedTok.setLocation(WeakLoc);
2036 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
2038 Toks[2] = AliasName;
2039 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2041 MutableArrayRef<Token> Toks(
2042 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
2043 Token &pragmaUnusedTok = Toks[0];
2044 pragmaUnusedTok.startToken();
2045 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
2046 pragmaUnusedTok.setLocation(WeakLoc);
2047 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
2049 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2053 // #pragma redefine_extname identifier identifier
2054 void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
2055 PragmaIntroducerKind Introducer,
2056 Token &RedefToken) {
2057 SourceLocation RedefLoc = RedefToken.getLocation();
2061 if (Tok.isNot(tok::identifier)) {
2062 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2067 Token RedefName = Tok;
2070 if (Tok.isNot(tok::identifier)) {
2071 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2072 << "redefine_extname";
2076 Token AliasName = Tok;
2079 if (Tok.isNot(tok::eod)) {
2080 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2085 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
2087 Token &pragmaRedefTok = Toks[0];
2088 pragmaRedefTok.startToken();
2089 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
2090 pragmaRedefTok.setLocation(RedefLoc);
2091 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
2092 Toks[1] = RedefName;
2093 Toks[2] = AliasName;
2094 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2099 PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
2100 PragmaIntroducerKind Introducer,
2102 tok::OnOffSwitch OOS;
2103 if (PP.LexOnOffSwitch(OOS))
2106 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2108 Toks[0].startToken();
2109 Toks[0].setKind(tok::annot_pragma_fp_contract);
2110 Toks[0].setLocation(Tok.getLocation());
2111 Toks[0].setAnnotationEndLoc(Tok.getLocation());
2112 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
2113 static_cast<uintptr_t>(OOS)));
2114 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2118 PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
2119 PragmaIntroducerKind Introducer,
2121 PP.LexUnexpandedToken(Tok);
2122 if (Tok.isNot(tok::identifier)) {
2123 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
2127 IdentifierInfo *Ext = Tok.getIdentifierInfo();
2128 SourceLocation NameLoc = Tok.getLocation();
2131 if (Tok.isNot(tok::colon)) {
2132 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
2137 if (Tok.isNot(tok::identifier)) {
2138 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
2141 IdentifierInfo *Pred = Tok.getIdentifierInfo();
2143 OpenCLExtState State;
2144 if (Pred->isStr("enable")) {
2146 } else if (Pred->isStr("disable")) {
2148 } else if (Pred->isStr("begin"))
2150 else if (Pred->isStr("end"))
2153 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
2154 << Ext->isStr("all");
2157 SourceLocation StateLoc = Tok.getLocation();
2160 if (Tok.isNot(tok::eod)) {
2161 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2166 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2168 Info->second = State;
2169 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2171 Toks[0].startToken();
2172 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2173 Toks[0].setLocation(NameLoc);
2174 Toks[0].setAnnotationValue(static_cast<void*>(Info));
2175 Toks[0].setAnnotationEndLoc(StateLoc);
2176 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2178 if (PP.getPPCallbacks())
2179 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
2183 /// Handle '#pragma omp ...' when OpenMP is disabled.
2186 PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2187 PragmaIntroducerKind Introducer,
2189 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2190 FirstTok.getLocation())) {
2191 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
2192 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2193 diag::Severity::Ignored, SourceLocation());
2195 PP.DiscardUntilEndOfDirective();
2198 /// Handle '#pragma omp ...' when OpenMP is enabled.
2201 PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2202 PragmaIntroducerKind Introducer,
2204 SmallVector<Token, 16> Pragma;
2207 Tok.setKind(tok::annot_pragma_openmp);
2208 Tok.setLocation(FirstTok.getLocation());
2210 while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) {
2211 Pragma.push_back(Tok);
2213 if (Tok.is(tok::annot_pragma_openmp)) {
2214 PP.Diag(Tok, diag::err_omp_unexpected_directive) << 0;
2215 unsigned InnerPragmaCnt = 1;
2216 while (InnerPragmaCnt != 0) {
2218 if (Tok.is(tok::annot_pragma_openmp))
2220 else if (Tok.is(tok::annot_pragma_openmp_end))
2226 SourceLocation EodLoc = Tok.getLocation();
2228 Tok.setKind(tok::annot_pragma_openmp_end);
2229 Tok.setLocation(EodLoc);
2230 Pragma.push_back(Tok);
2232 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2233 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2234 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
2235 /*DisableMacroExpansion=*/false);
2238 /// Handle '#pragma pointers_to_members'
2239 // The grammar for this pragma is as follows:
2241 // <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2243 // #pragma pointers_to_members '(' 'best_case' ')'
2244 // #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2245 // #pragma pointers_to_members '(' inheritance-model ')'
2246 void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
2247 PragmaIntroducerKind Introducer,
2249 SourceLocation PointersToMembersLoc = Tok.getLocation();
2251 if (Tok.isNot(tok::l_paren)) {
2252 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2253 << "pointers_to_members";
2257 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2259 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2260 << "pointers_to_members";
2265 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
2266 if (Arg->isStr("best_case")) {
2267 RepresentationMethod = LangOptions::PPTMK_BestCase;
2269 if (Arg->isStr("full_generality")) {
2270 if (Tok.is(tok::comma)) {
2273 Arg = Tok.getIdentifierInfo();
2275 PP.Diag(Tok.getLocation(),
2276 diag::err_pragma_pointers_to_members_unknown_kind)
2277 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2281 } else if (Tok.is(tok::r_paren)) {
2282 // #pragma pointers_to_members(full_generality) implicitly specifies
2283 // virtual_inheritance.
2285 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2287 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2288 << "full_generality";
2294 if (Arg->isStr("single_inheritance")) {
2295 RepresentationMethod =
2296 LangOptions::PPTMK_FullGeneralitySingleInheritance;
2297 } else if (Arg->isStr("multiple_inheritance")) {
2298 RepresentationMethod =
2299 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
2300 } else if (Arg->isStr("virtual_inheritance")) {
2301 RepresentationMethod =
2302 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2304 PP.Diag(Tok.getLocation(),
2305 diag::err_pragma_pointers_to_members_unknown_kind)
2306 << Arg << /*HasPointerDeclaration*/ 1;
2312 if (Tok.isNot(tok::r_paren)) {
2313 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2314 << (Arg ? Arg->getName() : "full_generality");
2318 SourceLocation EndLoc = Tok.getLocation();
2320 if (Tok.isNot(tok::eod)) {
2321 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2322 << "pointers_to_members";
2327 AnnotTok.startToken();
2328 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2329 AnnotTok.setLocation(PointersToMembersLoc);
2330 AnnotTok.setAnnotationEndLoc(EndLoc);
2331 AnnotTok.setAnnotationValue(
2332 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
2333 PP.EnterToken(AnnotTok);
2336 /// Handle '#pragma vtordisp'
2337 // The grammar for this pragma is as follows:
2339 // <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2341 // #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2342 // #pragma vtordisp '(' 'pop' ')'
2343 // #pragma vtordisp '(' ')'
2344 void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
2345 PragmaIntroducerKind Introducer,
2347 SourceLocation VtorDispLoc = Tok.getLocation();
2349 if (Tok.isNot(tok::l_paren)) {
2350 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2355 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
2356 const IdentifierInfo *II = Tok.getIdentifierInfo();
2358 if (II->isStr("push")) {
2359 // #pragma vtordisp(push, mode)
2361 if (Tok.isNot(tok::comma)) {
2362 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2366 Action = Sema::PSK_Push_Set;
2367 // not push, could be on/off
2368 } else if (II->isStr("pop")) {
2369 // #pragma vtordisp(pop)
2371 Action = Sema::PSK_Pop;
2373 // not push or pop, could be on/off
2375 if (Tok.is(tok::r_paren)) {
2376 // #pragma vtordisp()
2377 Action = Sema::PSK_Reset;
2383 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
2384 const IdentifierInfo *II = Tok.getIdentifierInfo();
2385 if (II && II->isStr("off")) {
2388 } else if (II && II->isStr("on")) {
2391 } else if (Tok.is(tok::numeric_constant) &&
2392 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2394 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2395 << 0 << 2 << "vtordisp";
2399 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2405 // Finish the pragma: ')' $
2406 if (Tok.isNot(tok::r_paren)) {
2407 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2410 SourceLocation EndLoc = Tok.getLocation();
2412 if (Tok.isNot(tok::eod)) {
2413 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2418 // Enter the annotation.
2420 AnnotTok.startToken();
2421 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2422 AnnotTok.setLocation(VtorDispLoc);
2423 AnnotTok.setAnnotationEndLoc(EndLoc);
2424 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
2425 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
2426 PP.EnterToken(AnnotTok);
2429 /// Handle all MS pragmas. Simply forwards the tokens after inserting
2430 /// an annotation token.
2431 void PragmaMSPragma::HandlePragma(Preprocessor &PP,
2432 PragmaIntroducerKind Introducer,
2434 Token EoF, AnnotTok;
2436 EoF.setKind(tok::eof);
2437 AnnotTok.startToken();
2438 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2439 AnnotTok.setLocation(Tok.getLocation());
2440 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2441 SmallVector<Token, 8> TokenVector;
2442 // Suck up all of the tokens before the eod.
2443 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
2444 TokenVector.push_back(Tok);
2445 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2447 // Add a sentinel EoF token to the end of the list.
2448 TokenVector.push_back(EoF);
2449 // We must allocate this array with new because EnterTokenStream is going to
2451 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2452 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
2453 auto Value = new (PP.getPreprocessorAllocator())
2454 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2455 TokenVector.size());
2456 AnnotTok.setAnnotationValue(Value);
2457 PP.EnterToken(AnnotTok);
2460 /// Handle the Microsoft \#pragma detect_mismatch extension.
2464 /// #pragma detect_mismatch("name", "value")
2466 /// Where 'name' and 'value' are quoted strings. The values are embedded in
2467 /// the object file and passed along to the linker. If the linker detects a
2468 /// mismatch in the object file's values for the given name, a LNK2038 error
2469 /// is emitted. See MSDN for more details.
2470 void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
2471 PragmaIntroducerKind Introducer,
2473 SourceLocation DetectMismatchLoc = Tok.getLocation();
2475 if (Tok.isNot(tok::l_paren)) {
2476 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
2480 // Read the name to embed, which must be a string literal.
2481 std::string NameString;
2482 if (!PP.LexStringLiteral(Tok, NameString,
2483 "pragma detect_mismatch",
2484 /*MacroExpansion=*/true))
2487 // Read the comma followed by a second string literal.
2488 std::string ValueString;
2489 if (Tok.isNot(tok::comma)) {
2490 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2494 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
2495 /*MacroExpansion=*/true))
2498 if (Tok.isNot(tok::r_paren)) {
2499 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2502 PP.Lex(Tok); // Eat the r_paren.
2504 if (Tok.isNot(tok::eod)) {
2505 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2509 // If the pragma is lexically sound, notify any interested PPCallbacks.
2510 if (PP.getPPCallbacks())
2511 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
2514 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
2517 /// Handle the microsoft \#pragma comment extension.
2521 /// #pragma comment(linker, "foo")
2523 /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2524 /// "foo" is a string, which is fully macro expanded, and permits string
2525 /// concatenation, embedded escape characters etc. See MSDN for more details.
2526 void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
2527 PragmaIntroducerKind Introducer,
2529 SourceLocation CommentLoc = Tok.getLocation();
2531 if (Tok.isNot(tok::l_paren)) {
2532 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2536 // Read the identifier.
2538 if (Tok.isNot(tok::identifier)) {
2539 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2543 // Verify that this is one of the 5 whitelisted options.
2544 IdentifierInfo *II = Tok.getIdentifierInfo();
2545 PragmaMSCommentKind Kind =
2546 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2547 .Case("linker", PCK_Linker)
2548 .Case("lib", PCK_Lib)
2549 .Case("compiler", PCK_Compiler)
2550 .Case("exestr", PCK_ExeStr)
2551 .Case("user", PCK_User)
2552 .Default(PCK_Unknown);
2553 if (Kind == PCK_Unknown) {
2554 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2558 if (PP.getTargetInfo().getTriple().isOSBinFormatELF() && Kind != PCK_Lib) {
2559 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2564 // On PS4, issue a warning about any pragma comments other than
2565 // #pragma comment lib.
2566 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
2567 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2572 // Read the optional string if present.
2574 std::string ArgumentString;
2575 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2577 /*MacroExpansion=*/true))
2580 // FIXME: warn that 'exestr' is deprecated.
2581 // FIXME: If the kind is "compiler" warn if the string is present (it is
2583 // The MSDN docs say that "lib" and "linker" require a string and have a short
2584 // whitelist of linker options they support, but in practice MSVC doesn't
2585 // issue a diagnostic. Therefore neither does clang.
2587 if (Tok.isNot(tok::r_paren)) {
2588 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2591 PP.Lex(Tok); // eat the r_paren.
2593 if (Tok.isNot(tok::eod)) {
2594 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2598 // If the pragma is lexically sound, notify any interested PPCallbacks.
2599 if (PP.getPPCallbacks())
2600 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2602 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
2605 // #pragma clang optimize off
2606 // #pragma clang optimize on
2607 void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
2608 PragmaIntroducerKind Introducer,
2609 Token &FirstToken) {
2612 if (Tok.is(tok::eod)) {
2613 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
2614 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
2617 if (Tok.isNot(tok::identifier)) {
2618 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2619 << PP.getSpelling(Tok);
2622 const IdentifierInfo *II = Tok.getIdentifierInfo();
2623 // The only accepted values are 'on' or 'off'.
2625 if (II->isStr("on")) {
2627 } else if (!II->isStr("off")) {
2628 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2629 << PP.getSpelling(Tok);
2634 if (Tok.isNot(tok::eod)) {
2635 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2636 << PP.getSpelling(Tok);
2640 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2644 /// Used as the annotation value for tok::annot_pragma_fp.
2645 struct TokFPAnnotValue {
2646 enum FlagKinds { Contract };
2647 enum FlagValues { On, Off, Fast };
2650 FlagValues FlagValue;
2652 } // end anonymous namespace
2654 void PragmaFPHandler::HandlePragma(Preprocessor &PP,
2655 PragmaIntroducerKind Introducer,
2658 Token PragmaName = Tok;
2659 SmallVector<Token, 1> TokenList;
2662 if (Tok.isNot(tok::identifier)) {
2663 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2664 << /*MissingOption=*/true << "";
2668 while (Tok.is(tok::identifier)) {
2669 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2672 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2673 OptionInfo->getName())
2674 .Case("contract", TokFPAnnotValue::Contract)
2677 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2678 << /*MissingOption=*/false << OptionInfo;
2684 if (Tok.isNot(tok::l_paren)) {
2685 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2690 if (Tok.isNot(tok::identifier)) {
2691 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2692 << PP.getSpelling(Tok) << OptionInfo->getName();
2695 const IdentifierInfo *II = Tok.getIdentifierInfo();
2698 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2700 .Case("on", TokFPAnnotValue::On)
2701 .Case("off", TokFPAnnotValue::Off)
2702 .Case("fast", TokFPAnnotValue::Fast)
2703 .Default(llvm::None);
2706 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2707 << PP.getSpelling(Tok) << OptionInfo->getName();
2713 if (Tok.isNot(tok::r_paren)) {
2714 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2719 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2720 TokFPAnnotValue{*FlagKind, *FlagValue};
2721 // Generate the loop hint token.
2724 FPTok.setKind(tok::annot_pragma_fp);
2725 FPTok.setLocation(PragmaName.getLocation());
2726 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2727 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2728 TokenList.push_back(FPTok);
2731 if (Tok.isNot(tok::eod)) {
2732 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2737 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2738 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2740 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2741 /*DisableMacroExpansion=*/false);
2744 void Parser::HandlePragmaFP() {
2745 assert(Tok.is(tok::annot_pragma_fp));
2747 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2749 LangOptions::FPContractModeKind FPC;
2750 switch (AnnotValue->FlagValue) {
2751 case TokFPAnnotValue::On:
2752 FPC = LangOptions::FPC_On;
2754 case TokFPAnnotValue::Fast:
2755 FPC = LangOptions::FPC_Fast;
2757 case TokFPAnnotValue::Off:
2758 FPC = LangOptions::FPC_Off;
2762 Actions.ActOnPragmaFPContract(FPC);
2763 ConsumeAnnotationToken();
2766 /// Parses loop or unroll pragma hint value and fills in Info.
2767 static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2768 Token Option, bool ValueInParens,
2769 PragmaLoopHintInfo &Info) {
2770 SmallVector<Token, 1> ValueList;
2771 int OpenParens = ValueInParens ? 1 : 0;
2772 // Read constant expression.
2773 while (Tok.isNot(tok::eod)) {
2774 if (Tok.is(tok::l_paren))
2776 else if (Tok.is(tok::r_paren)) {
2778 if (OpenParens == 0 && ValueInParens)
2782 ValueList.push_back(Tok);
2786 if (ValueInParens) {
2788 if (Tok.isNot(tok::r_paren)) {
2789 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2796 EOFTok.startToken();
2797 EOFTok.setKind(tok::eof);
2798 EOFTok.setLocation(Tok.getLocation());
2799 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2801 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
2803 Info.PragmaName = PragmaName;
2804 Info.Option = Option;
2808 /// Handle the \#pragma clang loop directive.
2809 /// #pragma clang 'loop' loop-hints
2812 /// loop-hint loop-hints[opt]
2815 /// 'vectorize' '(' loop-hint-keyword ')'
2816 /// 'interleave' '(' loop-hint-keyword ')'
2817 /// 'unroll' '(' unroll-hint-keyword ')'
2818 /// 'vectorize_width' '(' loop-hint-value ')'
2819 /// 'interleave_count' '(' loop-hint-value ')'
2820 /// 'unroll_count' '(' loop-hint-value ')'
2821 /// 'pipeline' '(' disable ')'
2822 /// 'pipeline_initiation_interval' '(' loop-hint-value ')'
2824 /// loop-hint-keyword:
2829 /// unroll-hint-keyword:
2834 /// loop-hint-value:
2835 /// constant-expression
2837 /// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2838 /// try vectorizing the instructions of the loop it precedes. Specifying
2839 /// interleave(enable) or interleave_count(_value_) instructs llvm to try
2840 /// interleaving multiple iterations of the loop it precedes. The width of the
2841 /// vector instructions is specified by vectorize_width() and the number of
2842 /// interleaved loop iterations is specified by interleave_count(). Specifying a
2843 /// value of 1 effectively disables vectorization/interleaving, even if it is
2844 /// possible and profitable, and 0 is invalid. The loop vectorizer currently
2845 /// only works on inner loops.
2847 /// The unroll and unroll_count directives control the concatenation
2848 /// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2849 /// completely if the trip count is known at compile time and unroll partially
2850 /// if the trip count is not known. Specifying unroll(full) is similar to
2851 /// unroll(enable) but will unroll the loop only if the trip count is known at
2852 /// compile time. Specifying unroll(disable) disables unrolling for the
2853 /// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2854 /// loop the number of times indicated by the value.
2855 void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2856 PragmaIntroducerKind Introducer,
2858 // Incoming token is "loop" from "#pragma clang loop".
2859 Token PragmaName = Tok;
2860 SmallVector<Token, 1> TokenList;
2862 // Lex the optimization option and verify it is an identifier.
2864 if (Tok.isNot(tok::identifier)) {
2865 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2866 << /*MissingOption=*/true << "";
2870 while (Tok.is(tok::identifier)) {
2872 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2874 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
2875 .Case("vectorize", true)
2876 .Case("interleave", true)
2877 .Case("unroll", true)
2878 .Case("distribute", true)
2879 .Case("vectorize_width", true)
2880 .Case("interleave_count", true)
2881 .Case("unroll_count", true)
2882 .Case("pipeline", true)
2883 .Case("pipeline_initiation_interval", true)
2886 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2887 << /*MissingOption=*/false << OptionInfo;
2893 if (Tok.isNot(tok::l_paren)) {
2894 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2899 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2900 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2904 // Generate the loop hint token.
2906 LoopHintTok.startToken();
2907 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
2908 LoopHintTok.setLocation(PragmaName.getLocation());
2909 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
2910 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2911 TokenList.push_back(LoopHintTok);
2914 if (Tok.isNot(tok::eod)) {
2915 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2920 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2921 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2923 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2924 /*DisableMacroExpansion=*/false);
2927 /// Handle the loop unroll optimization pragmas.
2929 /// #pragma unroll unroll-hint-value
2930 /// #pragma unroll '(' unroll-hint-value ')'
2931 /// #pragma nounroll
2932 /// #pragma unroll_and_jam
2933 /// #pragma unroll_and_jam unroll-hint-value
2934 /// #pragma unroll_and_jam '(' unroll-hint-value ')'
2935 /// #pragma nounroll_and_jam
2937 /// unroll-hint-value:
2938 /// constant-expression
2940 /// Loop unrolling hints can be specified with '#pragma unroll' or
2941 /// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2942 /// contained in parentheses. With no argument the directive instructs llvm to
2943 /// try to unroll the loop completely. A positive integer argument can be
2944 /// specified to indicate the number of times the loop should be unrolled. To
2945 /// maximize compatibility with other compilers the unroll count argument can be
2946 /// specified with or without parentheses. Specifying, '#pragma nounroll'
2947 /// disables unrolling of the loop.
2948 void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2949 PragmaIntroducerKind Introducer,
2951 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2952 // "#pragma nounroll".
2953 Token PragmaName = Tok;
2955 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2956 if (Tok.is(tok::eod)) {
2957 // nounroll or unroll pragma without an argument.
2958 Info->PragmaName = PragmaName;
2959 Info->Option.startToken();
2960 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll" ||
2961 PragmaName.getIdentifierInfo()->getName() == "nounroll_and_jam") {
2962 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2963 << PragmaName.getIdentifierInfo()->getName();
2966 // Unroll pragma with an argument: "#pragma unroll N" or
2967 // "#pragma unroll(N)".
2968 // Read '(' if it exists.
2969 bool ValueInParens = Tok.is(tok::l_paren);
2974 Option.startToken();
2975 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
2978 // In CUDA, the argument to '#pragma unroll' should not be contained in
2980 if (PP.getLangOpts().CUDA && ValueInParens)
2981 PP.Diag(Info->Toks[0].getLocation(),
2982 diag::warn_pragma_unroll_cuda_value_in_parens);
2984 if (Tok.isNot(tok::eod)) {
2985 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2991 // Generate the hint token.
2992 auto TokenArray = llvm::make_unique<Token[]>(1);
2993 TokenArray[0].startToken();
2994 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2995 TokenArray[0].setLocation(PragmaName.getLocation());
2996 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
2997 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2998 PP.EnterTokenStream(std::move(TokenArray), 1,
2999 /*DisableMacroExpansion=*/false);
3002 /// Handle the Microsoft \#pragma intrinsic extension.
3006 /// #pragma intrinsic(memset)
3007 /// #pragma intrinsic(strlen, memcpy)
3010 /// Pragma intrisic tells the compiler to use a builtin version of the
3011 /// function. Clang does it anyway, so the pragma doesn't really do anything.
3012 /// Anyway, we emit a warning if the function specified in \#pragma intrinsic
3013 /// isn't an intrinsic in clang and suggest to include intrin.h.
3014 void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
3015 PragmaIntroducerKind Introducer,
3019 if (Tok.isNot(tok::l_paren)) {
3020 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
3026 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
3028 while (Tok.is(tok::identifier)) {
3029 IdentifierInfo *II = Tok.getIdentifierInfo();
3030 if (!II->getBuiltinID())
3031 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
3032 << II << SuggestIntrinH;
3035 if (Tok.isNot(tok::comma))
3040 if (Tok.isNot(tok::r_paren)) {
3041 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
3047 if (Tok.isNot(tok::eod))
3048 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3052 // #pragma optimize("gsty", on|off)
3053 void PragmaMSOptimizeHandler::HandlePragma(Preprocessor &PP,
3054 PragmaIntroducerKind Introducer,
3056 SourceLocation StartLoc = Tok.getLocation();
3059 if (Tok.isNot(tok::l_paren)) {
3060 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "optimize";
3065 if (Tok.isNot(tok::string_literal)) {
3066 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_string) << "optimize";
3069 // We could syntax check the string but it's probably not worth the effort.
3072 if (Tok.isNot(tok::comma)) {
3073 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_comma) << "optimize";
3078 if (Tok.is(tok::eod) || Tok.is(tok::r_paren)) {
3079 PP.Diag(Tok.getLocation(), diag::warn_pragma_missing_argument)
3080 << "optimize" << /*Expected=*/true << "'on' or 'off'";
3083 IdentifierInfo *II = Tok.getIdentifierInfo();
3084 if (!II || (!II->isStr("on") && !II->isStr("off"))) {
3085 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
3086 << PP.getSpelling(Tok) << "optimize" << /*Expected=*/true
3092 if (Tok.isNot(tok::r_paren)) {
3093 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "optimize";
3098 if (Tok.isNot(tok::eod)) {
3099 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3103 PP.Diag(StartLoc, diag::warn_pragma_optimize);
3106 void PragmaForceCUDAHostDeviceHandler::HandlePragma(
3107 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
3108 Token FirstTok = Tok;
3111 IdentifierInfo *Info = Tok.getIdentifierInfo();
3112 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
3113 PP.Diag(FirstTok.getLocation(),
3114 diag::warn_pragma_force_cuda_host_device_bad_arg);
3118 if (Info->isStr("begin"))
3119 Actions.PushForceCUDAHostDevice();
3120 else if (!Actions.PopForceCUDAHostDevice())
3121 PP.Diag(FirstTok.getLocation(),
3122 diag::err_pragma_cannot_end_force_cuda_host_device);
3125 if (!Tok.is(tok::eod))
3126 PP.Diag(FirstTok.getLocation(),
3127 diag::warn_pragma_force_cuda_host_device_bad_arg);
3130 /// Handle the #pragma clang attribute directive.
3134 /// #pragma clang attribute push (attribute, subject-set)
3135 /// #pragma clang attribute push
3136 /// #pragma clang attribute (attribute, subject-set)
3137 /// #pragma clang attribute pop
3140 /// There are also 'namespace' variants of push and pop directives. The bare
3141 /// '#pragma clang attribute (attribute, subject-set)' version doesn't require a
3142 /// namespace, since it always applies attributes to the most recently pushed
3143 /// group, regardless of namespace.
3145 /// #pragma clang attribute namespace.push (attribute, subject-set)
3146 /// #pragma clang attribute namespace.push
3147 /// #pragma clang attribute namespace.pop
3150 /// The subject-set clause defines the set of declarations which receive the
3151 /// attribute. Its exact syntax is described in the LanguageExtensions document
3152 /// in Clang's documentation.
3154 /// This directive instructs the compiler to begin/finish applying the specified
3155 /// attribute to the set of attribute-specific declarations in the active range
3157 void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
3158 PragmaIntroducerKind Introducer,
3159 Token &FirstToken) {
3162 auto *Info = new (PP.getPreprocessorAllocator())
3163 PragmaAttributeInfo(AttributesForPragmaAttribute);
3165 // Parse the optional namespace followed by a period.
3166 if (Tok.is(tok::identifier)) {
3167 IdentifierInfo *II = Tok.getIdentifierInfo();
3168 if (!II->isStr("push") && !II->isStr("pop")) {
3169 Info->Namespace = II;
3172 if (!Tok.is(tok::period)) {
3173 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_period)
3181 if (!Tok.isOneOf(tok::identifier, tok::l_paren)) {
3182 PP.Diag(Tok.getLocation(),
3183 diag::err_pragma_attribute_expected_push_pop_paren);
3187 // Determine what action this pragma clang attribute represents.
3188 if (Tok.is(tok::l_paren)) {
3189 if (Info->Namespace) {
3190 PP.Diag(Tok.getLocation(),
3191 diag::err_pragma_attribute_namespace_on_attribute);
3192 PP.Diag(Tok.getLocation(),
3193 diag::note_pragma_attribute_namespace_on_attribute);
3196 Info->Action = PragmaAttributeInfo::Attribute;
3198 const IdentifierInfo *II = Tok.getIdentifierInfo();
3199 if (II->isStr("push"))
3200 Info->Action = PragmaAttributeInfo::Push;
3201 else if (II->isStr("pop"))
3202 Info->Action = PragmaAttributeInfo::Pop;
3204 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
3205 << PP.getSpelling(Tok);
3212 // Parse the actual attribute.
3213 if ((Info->Action == PragmaAttributeInfo::Push && Tok.isNot(tok::eod)) ||
3214 Info->Action == PragmaAttributeInfo::Attribute) {
3215 if (Tok.isNot(tok::l_paren)) {
3216 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
3221 // Lex the attribute tokens.
3222 SmallVector<Token, 16> AttributeTokens;
3224 while (Tok.isNot(tok::eod)) {
3225 if (Tok.is(tok::l_paren))
3227 else if (Tok.is(tok::r_paren)) {
3229 if (OpenParens == 0)
3233 AttributeTokens.push_back(Tok);
3237 if (AttributeTokens.empty()) {
3238 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
3241 if (Tok.isNot(tok::r_paren)) {
3242 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
3245 SourceLocation EndLoc = Tok.getLocation();
3248 // Terminate the attribute for parsing.
3250 EOFTok.startToken();
3251 EOFTok.setKind(tok::eof);
3252 EOFTok.setLocation(EndLoc);
3253 AttributeTokens.push_back(EOFTok);
3256 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
3259 if (Tok.isNot(tok::eod))
3260 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
3261 << "clang attribute";
3263 // Generate the annotated pragma token.
3264 auto TokenArray = llvm::make_unique<Token[]>(1);
3265 TokenArray[0].startToken();
3266 TokenArray[0].setKind(tok::annot_pragma_attribute);
3267 TokenArray[0].setLocation(FirstToken.getLocation());
3268 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
3269 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
3270 PP.EnterTokenStream(std::move(TokenArray), 1,
3271 /*DisableMacroExpansion=*/false);