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/ParseDiagnostic.h"
19 #include "clang/Parse/Parser.h"
20 #include "clang/Parse/RAIIObjectsForParser.h"
21 #include "clang/Sema/LoopHint.h"
22 #include "clang/Sema/Scope.h"
23 #include "llvm/ADT/StringSwitch.h"
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 struct PragmaFPHandler : public PragmaHandler {
99 PragmaFPHandler() : PragmaHandler("fp") {}
100 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
101 Token &FirstToken) override;
104 struct PragmaNoOpenMPHandler : public PragmaHandler {
105 PragmaNoOpenMPHandler() : PragmaHandler("omp") { }
106 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
107 Token &FirstToken) override;
110 struct PragmaOpenMPHandler : public PragmaHandler {
111 PragmaOpenMPHandler() : PragmaHandler("omp") { }
112 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
113 Token &FirstToken) override;
116 /// PragmaCommentHandler - "\#pragma comment ...".
117 struct PragmaCommentHandler : public PragmaHandler {
118 PragmaCommentHandler(Sema &Actions)
119 : PragmaHandler("comment"), Actions(Actions) {}
120 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
121 Token &FirstToken) override;
126 struct PragmaDetectMismatchHandler : public PragmaHandler {
127 PragmaDetectMismatchHandler(Sema &Actions)
128 : PragmaHandler("detect_mismatch"), Actions(Actions) {}
129 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
130 Token &FirstToken) override;
135 struct PragmaMSPointersToMembers : public PragmaHandler {
136 explicit PragmaMSPointersToMembers() : PragmaHandler("pointers_to_members") {}
137 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
138 Token &FirstToken) override;
141 struct PragmaMSVtorDisp : public PragmaHandler {
142 explicit PragmaMSVtorDisp() : PragmaHandler("vtordisp") {}
143 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
144 Token &FirstToken) override;
147 struct PragmaMSPragma : public PragmaHandler {
148 explicit PragmaMSPragma(const char *name) : PragmaHandler(name) {}
149 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
150 Token &FirstToken) override;
153 /// PragmaOptimizeHandler - "\#pragma clang optimize on/off".
154 struct PragmaOptimizeHandler : public PragmaHandler {
155 PragmaOptimizeHandler(Sema &S)
156 : PragmaHandler("optimize"), Actions(S) {}
157 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
158 Token &FirstToken) override;
163 struct PragmaLoopHintHandler : public PragmaHandler {
164 PragmaLoopHintHandler() : PragmaHandler("loop") {}
165 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
166 Token &FirstToken) override;
169 struct PragmaUnrollHintHandler : public PragmaHandler {
170 PragmaUnrollHintHandler(const char *name) : PragmaHandler(name) {}
171 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
172 Token &FirstToken) override;
175 struct PragmaMSRuntimeChecksHandler : public EmptyPragmaHandler {
176 PragmaMSRuntimeChecksHandler() : EmptyPragmaHandler("runtime_checks") {}
179 struct PragmaMSIntrinsicHandler : public PragmaHandler {
180 PragmaMSIntrinsicHandler() : PragmaHandler("intrinsic") {}
181 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
182 Token &FirstToken) override;
185 struct PragmaForceCUDAHostDeviceHandler : public PragmaHandler {
186 PragmaForceCUDAHostDeviceHandler(Sema &Actions)
187 : PragmaHandler("force_cuda_host_device"), Actions(Actions) {}
188 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
189 Token &FirstToken) override;
195 /// PragmaAttributeHandler - "\#pragma clang attribute ...".
196 struct PragmaAttributeHandler : public PragmaHandler {
197 PragmaAttributeHandler(AttributeFactory &AttrFactory)
198 : PragmaHandler("attribute"), AttributesForPragmaAttribute(AttrFactory) {}
199 void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer,
200 Token &FirstToken) override;
202 /// A pool of attributes that were parsed in \#pragma clang attribute.
203 ParsedAttributes AttributesForPragmaAttribute;
208 void Parser::initializePragmaHandlers() {
209 AlignHandler.reset(new PragmaAlignHandler());
210 PP.AddPragmaHandler(AlignHandler.get());
212 GCCVisibilityHandler.reset(new PragmaGCCVisibilityHandler());
213 PP.AddPragmaHandler("GCC", GCCVisibilityHandler.get());
215 OptionsHandler.reset(new PragmaOptionsHandler());
216 PP.AddPragmaHandler(OptionsHandler.get());
218 PackHandler.reset(new PragmaPackHandler());
219 PP.AddPragmaHandler(PackHandler.get());
221 MSStructHandler.reset(new PragmaMSStructHandler());
222 PP.AddPragmaHandler(MSStructHandler.get());
224 UnusedHandler.reset(new PragmaUnusedHandler());
225 PP.AddPragmaHandler(UnusedHandler.get());
227 WeakHandler.reset(new PragmaWeakHandler());
228 PP.AddPragmaHandler(WeakHandler.get());
230 RedefineExtnameHandler.reset(new PragmaRedefineExtnameHandler());
231 PP.AddPragmaHandler(RedefineExtnameHandler.get());
233 FPContractHandler.reset(new PragmaFPContractHandler());
234 PP.AddPragmaHandler("STDC", FPContractHandler.get());
236 PCSectionHandler.reset(new PragmaClangSectionHandler(Actions));
237 PP.AddPragmaHandler("clang", PCSectionHandler.get());
239 if (getLangOpts().OpenCL) {
240 OpenCLExtensionHandler.reset(new PragmaOpenCLExtensionHandler());
241 PP.AddPragmaHandler("OPENCL", OpenCLExtensionHandler.get());
243 PP.AddPragmaHandler("OPENCL", FPContractHandler.get());
245 if (getLangOpts().OpenMP)
246 OpenMPHandler.reset(new PragmaOpenMPHandler());
248 OpenMPHandler.reset(new PragmaNoOpenMPHandler());
249 PP.AddPragmaHandler(OpenMPHandler.get());
251 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
252 MSCommentHandler.reset(new PragmaCommentHandler(Actions));
253 PP.AddPragmaHandler(MSCommentHandler.get());
256 if (getLangOpts().MicrosoftExt) {
257 MSDetectMismatchHandler.reset(new PragmaDetectMismatchHandler(Actions));
258 PP.AddPragmaHandler(MSDetectMismatchHandler.get());
259 MSPointersToMembers.reset(new PragmaMSPointersToMembers());
260 PP.AddPragmaHandler(MSPointersToMembers.get());
261 MSVtorDisp.reset(new PragmaMSVtorDisp());
262 PP.AddPragmaHandler(MSVtorDisp.get());
263 MSInitSeg.reset(new PragmaMSPragma("init_seg"));
264 PP.AddPragmaHandler(MSInitSeg.get());
265 MSDataSeg.reset(new PragmaMSPragma("data_seg"));
266 PP.AddPragmaHandler(MSDataSeg.get());
267 MSBSSSeg.reset(new PragmaMSPragma("bss_seg"));
268 PP.AddPragmaHandler(MSBSSSeg.get());
269 MSConstSeg.reset(new PragmaMSPragma("const_seg"));
270 PP.AddPragmaHandler(MSConstSeg.get());
271 MSCodeSeg.reset(new PragmaMSPragma("code_seg"));
272 PP.AddPragmaHandler(MSCodeSeg.get());
273 MSSection.reset(new PragmaMSPragma("section"));
274 PP.AddPragmaHandler(MSSection.get());
275 MSRuntimeChecks.reset(new PragmaMSRuntimeChecksHandler());
276 PP.AddPragmaHandler(MSRuntimeChecks.get());
277 MSIntrinsic.reset(new PragmaMSIntrinsicHandler());
278 PP.AddPragmaHandler(MSIntrinsic.get());
281 if (getLangOpts().CUDA) {
282 CUDAForceHostDeviceHandler.reset(
283 new PragmaForceCUDAHostDeviceHandler(Actions));
284 PP.AddPragmaHandler("clang", CUDAForceHostDeviceHandler.get());
287 OptimizeHandler.reset(new PragmaOptimizeHandler(Actions));
288 PP.AddPragmaHandler("clang", OptimizeHandler.get());
290 LoopHintHandler.reset(new PragmaLoopHintHandler());
291 PP.AddPragmaHandler("clang", LoopHintHandler.get());
293 UnrollHintHandler.reset(new PragmaUnrollHintHandler("unroll"));
294 PP.AddPragmaHandler(UnrollHintHandler.get());
296 NoUnrollHintHandler.reset(new PragmaUnrollHintHandler("nounroll"));
297 PP.AddPragmaHandler(NoUnrollHintHandler.get());
299 FPHandler.reset(new PragmaFPHandler());
300 PP.AddPragmaHandler("clang", FPHandler.get());
302 AttributePragmaHandler.reset(new PragmaAttributeHandler(AttrFactory));
303 PP.AddPragmaHandler("clang", AttributePragmaHandler.get());
306 void Parser::resetPragmaHandlers() {
307 // Remove the pragma handlers we installed.
308 PP.RemovePragmaHandler(AlignHandler.get());
309 AlignHandler.reset();
310 PP.RemovePragmaHandler("GCC", GCCVisibilityHandler.get());
311 GCCVisibilityHandler.reset();
312 PP.RemovePragmaHandler(OptionsHandler.get());
313 OptionsHandler.reset();
314 PP.RemovePragmaHandler(PackHandler.get());
316 PP.RemovePragmaHandler(MSStructHandler.get());
317 MSStructHandler.reset();
318 PP.RemovePragmaHandler(UnusedHandler.get());
319 UnusedHandler.reset();
320 PP.RemovePragmaHandler(WeakHandler.get());
322 PP.RemovePragmaHandler(RedefineExtnameHandler.get());
323 RedefineExtnameHandler.reset();
325 if (getLangOpts().OpenCL) {
326 PP.RemovePragmaHandler("OPENCL", OpenCLExtensionHandler.get());
327 OpenCLExtensionHandler.reset();
328 PP.RemovePragmaHandler("OPENCL", FPContractHandler.get());
330 PP.RemovePragmaHandler(OpenMPHandler.get());
331 OpenMPHandler.reset();
333 if (getLangOpts().MicrosoftExt || getTargetInfo().getTriple().isPS4()) {
334 PP.RemovePragmaHandler(MSCommentHandler.get());
335 MSCommentHandler.reset();
338 PP.RemovePragmaHandler("clang", PCSectionHandler.get());
339 PCSectionHandler.reset();
341 if (getLangOpts().MicrosoftExt) {
342 PP.RemovePragmaHandler(MSDetectMismatchHandler.get());
343 MSDetectMismatchHandler.reset();
344 PP.RemovePragmaHandler(MSPointersToMembers.get());
345 MSPointersToMembers.reset();
346 PP.RemovePragmaHandler(MSVtorDisp.get());
348 PP.RemovePragmaHandler(MSInitSeg.get());
350 PP.RemovePragmaHandler(MSDataSeg.get());
352 PP.RemovePragmaHandler(MSBSSSeg.get());
354 PP.RemovePragmaHandler(MSConstSeg.get());
356 PP.RemovePragmaHandler(MSCodeSeg.get());
358 PP.RemovePragmaHandler(MSSection.get());
360 PP.RemovePragmaHandler(MSRuntimeChecks.get());
361 MSRuntimeChecks.reset();
362 PP.RemovePragmaHandler(MSIntrinsic.get());
366 if (getLangOpts().CUDA) {
367 PP.RemovePragmaHandler("clang", CUDAForceHostDeviceHandler.get());
368 CUDAForceHostDeviceHandler.reset();
371 PP.RemovePragmaHandler("STDC", FPContractHandler.get());
372 FPContractHandler.reset();
374 PP.RemovePragmaHandler("clang", OptimizeHandler.get());
375 OptimizeHandler.reset();
377 PP.RemovePragmaHandler("clang", LoopHintHandler.get());
378 LoopHintHandler.reset();
380 PP.RemovePragmaHandler(UnrollHintHandler.get());
381 UnrollHintHandler.reset();
383 PP.RemovePragmaHandler(NoUnrollHintHandler.get());
384 NoUnrollHintHandler.reset();
386 PP.RemovePragmaHandler("clang", FPHandler.get());
389 PP.RemovePragmaHandler("clang", AttributePragmaHandler.get());
390 AttributePragmaHandler.reset();
393 /// \brief Handle the annotation token produced for #pragma unused(...)
395 /// Each annot_pragma_unused is followed by the argument token so e.g.
396 /// "#pragma unused(x,y)" becomes:
397 /// annot_pragma_unused 'x' annot_pragma_unused 'y'
398 void Parser::HandlePragmaUnused() {
399 assert(Tok.is(tok::annot_pragma_unused));
400 SourceLocation UnusedLoc = ConsumeAnnotationToken();
401 Actions.ActOnPragmaUnused(Tok, getCurScope(), UnusedLoc);
402 ConsumeToken(); // The argument token.
405 void Parser::HandlePragmaVisibility() {
406 assert(Tok.is(tok::annot_pragma_vis));
407 const IdentifierInfo *VisType =
408 static_cast<IdentifierInfo *>(Tok.getAnnotationValue());
409 SourceLocation VisLoc = ConsumeAnnotationToken();
410 Actions.ActOnPragmaVisibility(VisType, VisLoc);
414 struct PragmaPackInfo {
415 Sema::PragmaMsStackAction Action;
419 } // end anonymous namespace
421 void Parser::HandlePragmaPack() {
422 assert(Tok.is(tok::annot_pragma_pack));
423 PragmaPackInfo *Info =
424 static_cast<PragmaPackInfo *>(Tok.getAnnotationValue());
425 SourceLocation PragmaLoc = Tok.getLocation();
426 ExprResult Alignment;
427 if (Info->Alignment.is(tok::numeric_constant)) {
428 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
429 if (Alignment.isInvalid()) {
430 ConsumeAnnotationToken();
434 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
436 // Consume the token after processing the pragma to enable pragma-specific
437 // #include warnings.
438 ConsumeAnnotationToken();
441 void Parser::HandlePragmaMSStruct() {
442 assert(Tok.is(tok::annot_pragma_msstruct));
443 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
444 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
445 Actions.ActOnPragmaMSStruct(Kind);
446 ConsumeAnnotationToken();
449 void Parser::HandlePragmaAlign() {
450 assert(Tok.is(tok::annot_pragma_align));
451 Sema::PragmaOptionsAlignKind Kind =
452 static_cast<Sema::PragmaOptionsAlignKind>(
453 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
454 SourceLocation PragmaLoc = ConsumeAnnotationToken();
455 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
458 void Parser::HandlePragmaDump() {
459 assert(Tok.is(tok::annot_pragma_dump));
461 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
462 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
463 ConsumeAnnotationToken();
466 void Parser::HandlePragmaWeak() {
467 assert(Tok.is(tok::annot_pragma_weak));
468 SourceLocation PragmaLoc = ConsumeAnnotationToken();
469 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
471 ConsumeToken(); // The weak name.
474 void Parser::HandlePragmaWeakAlias() {
475 assert(Tok.is(tok::annot_pragma_weakalias));
476 SourceLocation PragmaLoc = ConsumeAnnotationToken();
477 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
478 SourceLocation WeakNameLoc = Tok.getLocation();
480 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
481 SourceLocation AliasNameLoc = Tok.getLocation();
483 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
484 WeakNameLoc, AliasNameLoc);
488 void Parser::HandlePragmaRedefineExtname() {
489 assert(Tok.is(tok::annot_pragma_redefine_extname));
490 SourceLocation RedefLoc = ConsumeAnnotationToken();
491 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
492 SourceLocation RedefNameLoc = Tok.getLocation();
494 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
495 SourceLocation AliasNameLoc = Tok.getLocation();
497 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
498 RedefNameLoc, AliasNameLoc);
501 void Parser::HandlePragmaFPContract() {
502 assert(Tok.is(tok::annot_pragma_fp_contract));
503 tok::OnOffSwitch OOS =
504 static_cast<tok::OnOffSwitch>(
505 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
507 LangOptions::FPContractModeKind FPC;
510 FPC = LangOptions::FPC_On;
513 FPC = LangOptions::FPC_Off;
515 case tok::OOS_DEFAULT:
516 FPC = getLangOpts().getDefaultFPContractMode();
520 Actions.ActOnPragmaFPContract(FPC);
521 ConsumeAnnotationToken();
524 StmtResult Parser::HandlePragmaCaptured()
526 assert(Tok.is(tok::annot_pragma_captured));
527 ConsumeAnnotationToken();
529 if (Tok.isNot(tok::l_brace)) {
530 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
534 SourceLocation Loc = Tok.getLocation();
536 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope |
537 Scope::CompoundStmtScope);
538 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
541 StmtResult R = ParseCompoundStatement();
542 CapturedRegionScope.Exit();
545 Actions.ActOnCapturedRegionError();
549 return Actions.ActOnCapturedRegionEnd(R.get());
553 enum OpenCLExtState : char {
554 Disable, Enable, Begin, End
556 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
559 void Parser::HandlePragmaOpenCLExtension() {
560 assert(Tok.is(tok::annot_pragma_opencl_extension));
561 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
562 auto State = Data->second;
563 auto Ident = Data->first;
564 SourceLocation NameLoc = Tok.getLocation();
565 ConsumeAnnotationToken();
567 auto &Opt = Actions.getOpenCLOptions();
568 auto Name = Ident->getName();
569 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
570 // overriding all previously issued extension directives, but only if the
571 // behavior is set to disable."
573 if (State == Disable) {
575 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
577 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
579 } else if (State == Begin) {
580 if (!Opt.isKnown(Name) ||
581 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
584 Actions.setCurrentOpenCLExtension(Name);
585 } else if (State == End) {
586 if (Name != Actions.getCurrentOpenCLExtension())
587 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
588 Actions.setCurrentOpenCLExtension("");
589 } else if (!Opt.isKnown(Name))
590 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
591 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
592 Opt.enable(Name, State == Enable);
593 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
594 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
596 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
599 void Parser::HandlePragmaMSPointersToMembers() {
600 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
601 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
602 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
603 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
604 SourceLocation PragmaLoc = ConsumeAnnotationToken();
605 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
608 void Parser::HandlePragmaMSVtorDisp() {
609 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
610 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
611 Sema::PragmaMsStackAction Action =
612 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
613 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
614 SourceLocation PragmaLoc = ConsumeAnnotationToken();
615 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
618 void Parser::HandlePragmaMSPragma() {
619 assert(Tok.is(tok::annot_pragma_ms_pragma));
620 // Grab the tokens out of the annotation and enter them into the stream.
622 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
623 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
624 SourceLocation PragmaLocation = ConsumeAnnotationToken();
625 assert(Tok.isAnyIdentifier());
626 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
627 PP.Lex(Tok); // pragma kind
629 // Figure out which #pragma we're dealing with. The switch has no default
630 // because lex shouldn't emit the annotation token for unrecognized pragmas.
631 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
632 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
633 .Case("data_seg", &Parser::HandlePragmaMSSegment)
634 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
635 .Case("const_seg", &Parser::HandlePragmaMSSegment)
636 .Case("code_seg", &Parser::HandlePragmaMSSegment)
637 .Case("section", &Parser::HandlePragmaMSSection)
638 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
640 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
641 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
642 // until eof (really end of line) to prevent follow-on errors.
643 while (Tok.isNot(tok::eof))
649 bool Parser::HandlePragmaMSSection(StringRef PragmaName,
650 SourceLocation PragmaLocation) {
651 if (Tok.isNot(tok::l_paren)) {
652 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
656 // Parsing code for pragma section
657 if (Tok.isNot(tok::string_literal)) {
658 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
662 ExprResult StringResult = ParseStringLiteralExpression();
663 if (StringResult.isInvalid())
664 return false; // Already diagnosed.
665 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
666 if (SegmentName->getCharByteWidth() != 1) {
667 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
671 int SectionFlags = ASTContext::PSF_Read;
672 bool SectionFlagsAreDefault = true;
673 while (Tok.is(tok::comma)) {
675 // Ignore "long" and "short".
676 // They are undocumented, but widely used, section attributes which appear
678 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
679 PP.Lex(Tok); // long/short
683 if (!Tok.isAnyIdentifier()) {
684 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
688 ASTContext::PragmaSectionFlag Flag =
689 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
690 Tok.getIdentifierInfo()->getName())
691 .Case("read", ASTContext::PSF_Read)
692 .Case("write", ASTContext::PSF_Write)
693 .Case("execute", ASTContext::PSF_Execute)
694 .Case("shared", ASTContext::PSF_Invalid)
695 .Case("nopage", ASTContext::PSF_Invalid)
696 .Case("nocache", ASTContext::PSF_Invalid)
697 .Case("discard", ASTContext::PSF_Invalid)
698 .Case("remove", ASTContext::PSF_Invalid)
699 .Default(ASTContext::PSF_None);
700 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
701 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
702 ? diag::warn_pragma_invalid_specific_action
703 : diag::warn_pragma_unsupported_action)
704 << PragmaName << Tok.getIdentifierInfo()->getName();
707 SectionFlags |= Flag;
708 SectionFlagsAreDefault = false;
709 PP.Lex(Tok); // Identifier
711 // If no section attributes are specified, the section will be marked as
713 if (SectionFlagsAreDefault)
714 SectionFlags |= ASTContext::PSF_Write;
715 if (Tok.isNot(tok::r_paren)) {
716 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
720 if (Tok.isNot(tok::eof)) {
721 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
726 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
730 bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
731 SourceLocation PragmaLocation) {
732 if (Tok.isNot(tok::l_paren)) {
733 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
737 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
739 if (Tok.isAnyIdentifier()) {
740 StringRef PushPop = Tok.getIdentifierInfo()->getName();
741 if (PushPop == "push")
742 Action = Sema::PSK_Push;
743 else if (PushPop == "pop")
744 Action = Sema::PSK_Pop;
746 PP.Diag(PragmaLocation,
747 diag::warn_pragma_expected_section_push_pop_or_name)
751 if (Action != Sema::PSK_Reset) {
752 PP.Lex(Tok); // push | pop
753 if (Tok.is(tok::comma)) {
755 // If we've got a comma, we either need a label or a string.
756 if (Tok.isAnyIdentifier()) {
757 SlotLabel = Tok.getIdentifierInfo()->getName();
758 PP.Lex(Tok); // identifier
759 if (Tok.is(tok::comma))
761 else if (Tok.isNot(tok::r_paren)) {
762 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
767 } else if (Tok.isNot(tok::r_paren)) {
768 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
773 // Grab the string literal for our section name.
774 StringLiteral *SegmentName = nullptr;
775 if (Tok.isNot(tok::r_paren)) {
776 if (Tok.isNot(tok::string_literal)) {
777 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
778 diag::warn_pragma_expected_section_name :
779 diag::warn_pragma_expected_section_label_or_name :
780 diag::warn_pragma_expected_section_push_pop_or_name;
781 PP.Diag(PragmaLocation, DiagID) << PragmaName;
784 ExprResult StringResult = ParseStringLiteralExpression();
785 if (StringResult.isInvalid())
786 return false; // Already diagnosed.
787 SegmentName = cast<StringLiteral>(StringResult.get());
788 if (SegmentName->getCharByteWidth() != 1) {
789 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
793 // Setting section "" has no effect
794 if (SegmentName->getLength())
795 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
797 if (Tok.isNot(tok::r_paren)) {
798 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
802 if (Tok.isNot(tok::eof)) {
803 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
808 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
809 SegmentName, PragmaName);
813 // #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
814 bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
815 SourceLocation PragmaLocation) {
816 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
817 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
821 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
825 // Parse either the known section names or the string section name.
826 StringLiteral *SegmentName = nullptr;
827 if (Tok.isAnyIdentifier()) {
828 auto *II = Tok.getIdentifierInfo();
829 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
830 .Case("compiler", "\".CRT$XCC\"")
831 .Case("lib", "\".CRT$XCL\"")
832 .Case("user", "\".CRT$XCU\"")
835 if (!Section.empty()) {
836 // Pretend the user wrote the appropriate string literal here.
838 Toks[0].startToken();
839 Toks[0].setKind(tok::string_literal);
840 Toks[0].setLocation(Tok.getLocation());
841 Toks[0].setLiteralData(Section.data());
842 Toks[0].setLength(Section.size());
844 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
847 } else if (Tok.is(tok::string_literal)) {
848 ExprResult StringResult = ParseStringLiteralExpression();
849 if (StringResult.isInvalid())
851 SegmentName = cast<StringLiteral>(StringResult.get());
852 if (SegmentName->getCharByteWidth() != 1) {
853 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
857 // FIXME: Add support for the '[, func-name]' part of the pragma.
861 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
865 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
867 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
871 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
876 struct PragmaLoopHintInfo {
879 ArrayRef<Token> Toks;
881 } // end anonymous namespace
883 static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
884 std::string PragmaString;
885 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
886 PragmaString = "clang loop ";
887 PragmaString += Option.getIdentifierInfo()->getName();
889 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
890 "Unexpected pragma name");
891 PragmaString = "unroll";
896 bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
897 assert(Tok.is(tok::annot_pragma_loop_hint));
898 PragmaLoopHintInfo *Info =
899 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
901 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
902 Hint.PragmaNameLoc = IdentifierLoc::create(
903 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
905 // It is possible that the loop hint has no option identifier, such as
906 // #pragma unroll(4).
907 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
908 ? Info->Option.getIdentifierInfo()
910 Hint.OptionLoc = IdentifierLoc::create(
911 Actions.Context, Info->Option.getLocation(), OptionInfo);
913 llvm::ArrayRef<Token> Toks = Info->Toks;
915 // Return a valid hint if pragma unroll or nounroll were specified
916 // without an argument.
917 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
918 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
919 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
920 ConsumeAnnotationToken();
921 Hint.Range = Info->PragmaName.getLocation();
925 // The constant expression is always followed by an eof token, which increases
927 assert(!Toks.empty() &&
928 "PragmaLoopHintInfo::Toks must contain at least one token.");
930 // If no option is specified the argument is assumed to be a constant expr.
931 bool OptionUnroll = false;
932 bool OptionDistribute = false;
933 bool StateOption = false;
934 if (OptionInfo) { // Pragma Unroll does not specify an option.
935 OptionUnroll = OptionInfo->isStr("unroll");
936 OptionDistribute = OptionInfo->isStr("distribute");
937 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
938 .Case("vectorize", true)
939 .Case("interleave", true)
941 OptionUnroll || OptionDistribute;
944 bool AssumeSafetyArg = !OptionUnroll && !OptionDistribute;
945 // Verify loop hint has an argument.
946 if (Toks[0].is(tok::eof)) {
947 ConsumeAnnotationToken();
948 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
949 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll
950 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
954 // Validate the argument.
956 ConsumeAnnotationToken();
957 SourceLocation StateLoc = Toks[0].getLocation();
958 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
960 bool Valid = StateInfo &&
961 llvm::StringSwitch<bool>(StateInfo->getName())
962 .Cases("enable", "disable", true)
963 .Case("full", OptionUnroll)
964 .Case("assume_safety", AssumeSafetyArg)
967 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
968 << /*FullKeyword=*/OptionUnroll
969 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
973 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
974 << PragmaLoopHintString(Info->PragmaName, Info->Option);
975 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
977 // Enter constant expression including eof terminator into token stream.
978 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
979 ConsumeAnnotationToken();
981 ExprResult R = ParseConstantExpression();
983 // Tokens following an error in an ill-formed constant expression will
984 // remain in the token stream and must be removed.
985 if (Tok.isNot(tok::eof)) {
986 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
987 << PragmaLoopHintString(Info->PragmaName, Info->Option);
988 while (Tok.isNot(tok::eof))
992 ConsumeToken(); // Consume the constant expression eof terminator.
995 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
998 // Argument is a constant expression with an integer type.
999 Hint.ValueExpr = R.get();
1002 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
1003 Info->Toks.back().getLocation());
1008 struct PragmaAttributeInfo {
1009 enum ActionType { Push, Pop };
1010 ParsedAttributes &Attributes;
1012 ArrayRef<Token> Tokens;
1014 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1017 #include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1019 } // end anonymous namespace
1021 static StringRef getIdentifier(const Token &Tok) {
1022 if (Tok.is(tok::identifier))
1023 return Tok.getIdentifierInfo()->getName();
1024 const char *S = tok::getKeywordSpelling(Tok.getKind());
1030 static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1031 using namespace attr;
1033 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1036 #include "clang/Basic/AttrSubMatchRulesList.inc"
1038 llvm_unreachable("Invalid attribute subject match rule");
1042 static void diagnoseExpectedAttributeSubjectSubRule(
1043 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1044 SourceLocation SubRuleLoc) {
1046 PRef.Diag(SubRuleLoc,
1047 diag::err_pragma_attribute_expected_subject_sub_identifier)
1049 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1050 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1052 Diagnostic << /*SubRulesSupported=*/0;
1055 static void diagnoseUnknownAttributeSubjectSubRule(
1056 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1057 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1060 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1061 << SubRuleName << PrimaryRuleName;
1062 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1063 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1065 Diagnostic << /*SubRulesSupported=*/0;
1068 bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1069 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1070 SourceLocation &LastMatchRuleEndLoc) {
1072 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1073 if (getIdentifier(Tok) == "any") {
1074 AnyLoc = ConsumeToken();
1076 if (AnyParens.expectAndConsume())
1081 // Parse the subject matcher rule.
1082 StringRef Name = getIdentifier(Tok);
1084 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1087 std::pair<Optional<attr::SubjectMatchRule>,
1088 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1089 Rule = isAttributeSubjectMatchRule(Name);
1091 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1094 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1095 SourceLocation RuleLoc = ConsumeToken();
1097 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1098 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1099 if (Parens.expectAndConsume())
1101 } else if (Parens.consumeOpen()) {
1102 if (!SubjectMatchRules
1104 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1106 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1108 << FixItHint::CreateRemoval(SourceRange(
1109 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1110 LastMatchRuleEndLoc = RuleLoc;
1114 // Parse the sub-rules.
1115 StringRef SubRuleName = getIdentifier(Tok);
1116 if (SubRuleName.empty()) {
1117 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1121 attr::SubjectMatchRule SubRule;
1122 if (SubRuleName == "unless") {
1123 SourceLocation SubRuleLoc = ConsumeToken();
1124 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1125 if (Parens.expectAndConsume())
1127 SubRuleName = getIdentifier(Tok);
1128 if (SubRuleName.empty()) {
1129 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1133 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1134 if (!SubRuleOrNone) {
1135 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1136 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1137 SubRuleUnlessName, SubRuleLoc);
1140 SubRule = *SubRuleOrNone;
1142 if (Parens.consumeClose())
1145 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1146 if (!SubRuleOrNone) {
1147 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1148 SubRuleName, Tok.getLocation());
1151 SubRule = *SubRuleOrNone;
1154 SourceLocation RuleEndLoc = Tok.getLocation();
1155 LastMatchRuleEndLoc = RuleEndLoc;
1156 if (Parens.consumeClose())
1158 if (!SubjectMatchRules
1159 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1161 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1162 << attr::getSubjectMatchRuleSpelling(SubRule)
1163 << FixItHint::CreateRemoval(SourceRange(
1164 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1167 } while (IsAny && TryConsumeToken(tok::comma));
1170 if (AnyParens.consumeClose())
1178 /// Describes the stage at which attribute subject rule parsing was interruped.
1179 enum class MissingAttributeSubjectRulesRecoveryPoint {
1187 MissingAttributeSubjectRulesRecoveryPoint
1188 getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1189 if (const auto *II = Tok.getIdentifierInfo()) {
1190 if (II->isStr("apply_to"))
1191 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1192 if (II->isStr("any"))
1193 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1195 if (Tok.is(tok::equal))
1196 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1197 return MissingAttributeSubjectRulesRecoveryPoint::None;
1200 /// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1201 /// suggests the possible attribute subject rules in a fix-it together with
1202 /// any other missing tokens.
1203 DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
1204 unsigned DiagID, AttributeList &Attribute,
1205 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1206 SourceLocation Loc = PRef.getEndOfPreviousToken();
1207 if (Loc.isInvalid())
1208 Loc = PRef.getCurToken().getLocation();
1209 auto Diagnostic = PRef.Diag(Loc, DiagID);
1211 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1212 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1213 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1215 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1216 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1217 FixIt += "apply_to";
1218 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1219 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1221 SourceRange FixItRange(Loc);
1222 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1223 // Gather the subject match rules that are supported by the attribute.
1224 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1225 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1226 if (SubjectMatchRuleSet.empty()) {
1227 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1228 // placeholders will be supported by the fix-its.
1232 bool NeedsComma = false;
1233 for (const auto &I : SubjectMatchRuleSet) {
1234 // Ensure that the missing rule is reported in the fix-it only when it's
1235 // supported in the current language mode.
1242 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1245 // Check if we need to remove the range
1246 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1247 FixItRange.setEnd(PRef.getCurToken().getLocation());
1249 if (FixItRange.getBegin() == FixItRange.getEnd())
1250 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1252 Diagnostic << FixItHint::CreateReplacement(
1253 CharSourceRange::getCharRange(FixItRange), FixIt);
1257 } // end anonymous namespace
1259 void Parser::HandlePragmaAttribute() {
1260 assert(Tok.is(tok::annot_pragma_attribute) &&
1261 "Expected #pragma attribute annotation token");
1262 SourceLocation PragmaLoc = Tok.getLocation();
1263 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1264 if (Info->Action == PragmaAttributeInfo::Pop) {
1265 ConsumeAnnotationToken();
1266 Actions.ActOnPragmaAttributePop(PragmaLoc);
1269 // Parse the actual attribute with its arguments.
1270 assert(Info->Action == PragmaAttributeInfo::Push &&
1271 "Unexpected #pragma attribute command");
1272 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false);
1273 ConsumeAnnotationToken();
1275 ParsedAttributes &Attrs = Info->Attributes;
1276 Attrs.clearListOnly();
1278 auto SkipToEnd = [this]() {
1279 SkipUntil(tok::eof, StopBeforeMatch);
1283 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1284 // Parse the CXX11 style attribute.
1285 ParseCXX11AttributeSpecifier(Attrs);
1286 } else if (Tok.is(tok::kw___attribute)) {
1288 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1291 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1294 if (Tok.isNot(tok::identifier)) {
1295 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1300 SourceLocation AttrNameLoc = ConsumeToken();
1302 if (Tok.isNot(tok::l_paren))
1303 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
1304 AttributeList::AS_GNU);
1306 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1307 /*ScopeName=*/nullptr,
1308 /*ScopeLoc=*/SourceLocation(),
1309 AttributeList::AS_GNU,
1310 /*Declarator=*/nullptr);
1312 if (ExpectAndConsume(tok::r_paren))
1314 if (ExpectAndConsume(tok::r_paren))
1316 } else if (Tok.is(tok::kw___declspec)) {
1317 ParseMicrosoftDeclSpecs(Attrs);
1319 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1320 if (Tok.getIdentifierInfo()) {
1321 // If we suspect that this is an attribute suggest the use of
1323 if (AttributeList::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1324 AttributeList::AS_GNU) !=
1325 AttributeList::UnknownAttribute) {
1326 SourceLocation InsertStartLoc = Tok.getLocation();
1328 if (Tok.is(tok::l_paren)) {
1330 SkipUntil(tok::r_paren, StopBeforeMatch);
1331 if (Tok.isNot(tok::r_paren))
1334 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1335 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1336 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1343 if (!Attrs.getList() || Attrs.getList()->isInvalid()) {
1348 // Ensure that we don't have more than one attribute.
1349 if (Attrs.getList()->getNext()) {
1350 SourceLocation Loc = Attrs.getList()->getNext()->getLoc();
1351 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1356 if (!Attrs.getList()->isSupportedByPragmaAttribute()) {
1357 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
1358 << Attrs.getList()->getName();
1362 AttributeList &Attribute = *Attrs.getList();
1364 // Parse the subject-list.
1365 if (!TryConsumeToken(tok::comma)) {
1366 createExpectedAttributeSubjectRulesTokenDiagnostic(
1367 diag::err_expected, Attribute,
1368 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1374 if (Tok.isNot(tok::identifier)) {
1375 createExpectedAttributeSubjectRulesTokenDiagnostic(
1376 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1377 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1381 const IdentifierInfo *II = Tok.getIdentifierInfo();
1382 if (!II->isStr("apply_to")) {
1383 createExpectedAttributeSubjectRulesTokenDiagnostic(
1384 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1385 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1391 if (!TryConsumeToken(tok::equal)) {
1392 createExpectedAttributeSubjectRulesTokenDiagnostic(
1393 diag::err_expected, Attribute,
1394 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1400 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1401 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1402 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1403 LastMatchRuleEndLoc)) {
1408 // Tokens following an ill-formed attribute will remain in the token stream
1409 // and must be removed.
1410 if (Tok.isNot(tok::eof)) {
1411 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1416 // Consume the eof terminator token.
1419 Actions.ActOnPragmaAttributePush(Attribute, PragmaLoc,
1420 std::move(SubjectMatchRules));
1423 // #pragma GCC visibility comes in two variants:
1424 // 'push' '(' [visibility] ')'
1426 void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
1427 PragmaIntroducerKind Introducer,
1429 SourceLocation VisLoc = VisTok.getLocation();
1432 PP.LexUnexpandedToken(Tok);
1434 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1436 const IdentifierInfo *VisType;
1437 if (PushPop && PushPop->isStr("pop")) {
1439 } else if (PushPop && PushPop->isStr("push")) {
1440 PP.LexUnexpandedToken(Tok);
1441 if (Tok.isNot(tok::l_paren)) {
1442 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1446 PP.LexUnexpandedToken(Tok);
1447 VisType = Tok.getIdentifierInfo();
1449 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1453 PP.LexUnexpandedToken(Tok);
1454 if (Tok.isNot(tok::r_paren)) {
1455 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1460 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1464 SourceLocation EndLoc = Tok.getLocation();
1465 PP.LexUnexpandedToken(Tok);
1466 if (Tok.isNot(tok::eod)) {
1467 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1472 auto Toks = llvm::make_unique<Token[]>(1);
1473 Toks[0].startToken();
1474 Toks[0].setKind(tok::annot_pragma_vis);
1475 Toks[0].setLocation(VisLoc);
1476 Toks[0].setAnnotationEndLoc(EndLoc);
1477 Toks[0].setAnnotationValue(
1478 const_cast<void*>(static_cast<const void*>(VisType)));
1479 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
1482 // #pragma pack(...) comes in the following delicious flavors:
1483 // pack '(' [integer] ')'
1484 // pack '(' 'show' ')'
1485 // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
1486 void PragmaPackHandler::HandlePragma(Preprocessor &PP,
1487 PragmaIntroducerKind Introducer,
1489 SourceLocation PackLoc = PackTok.getLocation();
1493 if (Tok.isNot(tok::l_paren)) {
1494 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
1498 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1499 StringRef SlotLabel;
1501 Alignment.startToken();
1503 if (Tok.is(tok::numeric_constant)) {
1508 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1509 // the push/pop stack.
1510 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
1512 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
1513 } else if (Tok.is(tok::identifier)) {
1514 const IdentifierInfo *II = Tok.getIdentifierInfo();
1515 if (II->isStr("show")) {
1516 Action = Sema::PSK_Show;
1519 if (II->isStr("push")) {
1520 Action = Sema::PSK_Push;
1521 } else if (II->isStr("pop")) {
1522 Action = Sema::PSK_Pop;
1524 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
1529 if (Tok.is(tok::comma)) {
1532 if (Tok.is(tok::numeric_constant)) {
1533 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1537 } else if (Tok.is(tok::identifier)) {
1538 SlotLabel = Tok.getIdentifierInfo()->getName();
1541 if (Tok.is(tok::comma)) {
1544 if (Tok.isNot(tok::numeric_constant)) {
1545 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1549 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1555 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1560 } else if (PP.getLangOpts().ApplePragmaPack) {
1561 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1562 // the push/pop stack.
1563 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1564 Action = Sema::PSK_Pop;
1567 if (Tok.isNot(tok::r_paren)) {
1568 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
1572 SourceLocation RParenLoc = Tok.getLocation();
1574 if (Tok.isNot(tok::eod)) {
1575 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1579 PragmaPackInfo *Info =
1580 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
1581 Info->Action = Action;
1582 Info->SlotLabel = SlotLabel;
1583 Info->Alignment = Alignment;
1585 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1587 Toks[0].startToken();
1588 Toks[0].setKind(tok::annot_pragma_pack);
1589 Toks[0].setLocation(PackLoc);
1590 Toks[0].setAnnotationEndLoc(RParenLoc);
1591 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1592 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1595 // #pragma ms_struct on
1596 // #pragma ms_struct off
1597 void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1598 PragmaIntroducerKind Introducer,
1599 Token &MSStructTok) {
1600 PragmaMSStructKind Kind = PMSST_OFF;
1604 if (Tok.isNot(tok::identifier)) {
1605 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1608 SourceLocation EndLoc = Tok.getLocation();
1609 const IdentifierInfo *II = Tok.getIdentifierInfo();
1610 if (II->isStr("on")) {
1614 else if (II->isStr("off") || II->isStr("reset"))
1617 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1621 if (Tok.isNot(tok::eod)) {
1622 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1627 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1629 Toks[0].startToken();
1630 Toks[0].setKind(tok::annot_pragma_msstruct);
1631 Toks[0].setLocation(MSStructTok.getLocation());
1632 Toks[0].setAnnotationEndLoc(EndLoc);
1633 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1634 static_cast<uintptr_t>(Kind)));
1635 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1638 // #pragma clang section bss="abc" data="" rodata="def" text=""
1639 void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
1640 PragmaIntroducerKind Introducer, Token &FirstToken) {
1643 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1645 PP.Lex(Tok); // eat 'section'
1646 while (Tok.isNot(tok::eod)) {
1647 if (Tok.isNot(tok::identifier)) {
1648 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1652 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1653 if (SecType->isStr("bss"))
1654 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1655 else if (SecType->isStr("data"))
1656 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1657 else if (SecType->isStr("rodata"))
1658 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1659 else if (SecType->isStr("text"))
1660 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1662 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1666 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1667 if (Tok.isNot(tok::equal)) {
1668 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1672 std::string SecName;
1673 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1676 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1677 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1678 Sema::PragmaClangSectionAction::PCSA_Clear),
1683 // #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1684 // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
1685 static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
1691 if (Tok.isNot(tok::identifier) ||
1692 !Tok.getIdentifierInfo()->isStr("align")) {
1693 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1699 if (Tok.isNot(tok::equal)) {
1700 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1706 if (Tok.isNot(tok::identifier)) {
1707 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1708 << (IsOptions ? "options" : "align");
1712 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
1713 const IdentifierInfo *II = Tok.getIdentifierInfo();
1714 if (II->isStr("native"))
1715 Kind = Sema::POAK_Native;
1716 else if (II->isStr("natural"))
1717 Kind = Sema::POAK_Natural;
1718 else if (II->isStr("packed"))
1719 Kind = Sema::POAK_Packed;
1720 else if (II->isStr("power"))
1721 Kind = Sema::POAK_Power;
1722 else if (II->isStr("mac68k"))
1723 Kind = Sema::POAK_Mac68k;
1724 else if (II->isStr("reset"))
1725 Kind = Sema::POAK_Reset;
1727 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1732 SourceLocation EndLoc = Tok.getLocation();
1734 if (Tok.isNot(tok::eod)) {
1735 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1736 << (IsOptions ? "options" : "align");
1740 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1742 Toks[0].startToken();
1743 Toks[0].setKind(tok::annot_pragma_align);
1744 Toks[0].setLocation(FirstTok.getLocation());
1745 Toks[0].setAnnotationEndLoc(EndLoc);
1746 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1747 static_cast<uintptr_t>(Kind)));
1748 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1751 void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1752 PragmaIntroducerKind Introducer,
1754 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
1757 void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1758 PragmaIntroducerKind Introducer,
1759 Token &OptionsTok) {
1760 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
1763 // #pragma unused(identifier)
1764 void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1765 PragmaIntroducerKind Introducer,
1767 // FIXME: Should we be expanding macros here? My guess is no.
1768 SourceLocation UnusedLoc = UnusedTok.getLocation();
1770 // Lex the left '('.
1773 if (Tok.isNot(tok::l_paren)) {
1774 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1778 // Lex the declaration reference(s).
1779 SmallVector<Token, 5> Identifiers;
1780 SourceLocation RParenLoc;
1787 if (Tok.is(tok::identifier)) {
1788 Identifiers.push_back(Tok);
1794 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1798 // We are execting a ')' or a ','.
1799 if (Tok.is(tok::comma)) {
1804 if (Tok.is(tok::r_paren)) {
1805 RParenLoc = Tok.getLocation();
1810 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
1815 if (Tok.isNot(tok::eod)) {
1816 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1821 // Verify that we have a location for the right parenthesis.
1822 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
1823 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
1825 // For each identifier token, insert into the token stream a
1826 // annot_pragma_unused token followed by the identifier token.
1827 // This allows us to cache a "#pragma unused" that occurs inside an inline
1828 // C++ member function.
1830 MutableArrayRef<Token> Toks(
1831 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1832 2 * Identifiers.size());
1833 for (unsigned i=0; i != Identifiers.size(); i++) {
1834 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1835 pragmaUnusedTok.startToken();
1836 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1837 pragmaUnusedTok.setLocation(UnusedLoc);
1838 idTok = Identifiers[i];
1840 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1843 // #pragma weak identifier
1844 // #pragma weak identifier '=' identifier
1845 void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1846 PragmaIntroducerKind Introducer,
1848 SourceLocation WeakLoc = WeakTok.getLocation();
1852 if (Tok.isNot(tok::identifier)) {
1853 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1857 Token WeakName = Tok;
1858 bool HasAlias = false;
1862 if (Tok.is(tok::equal)) {
1865 if (Tok.isNot(tok::identifier)) {
1866 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1874 if (Tok.isNot(tok::eod)) {
1875 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1880 MutableArrayRef<Token> Toks(
1881 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
1882 Token &pragmaUnusedTok = Toks[0];
1883 pragmaUnusedTok.startToken();
1884 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1885 pragmaUnusedTok.setLocation(WeakLoc);
1886 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
1888 Toks[2] = AliasName;
1889 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1891 MutableArrayRef<Token> Toks(
1892 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
1893 Token &pragmaUnusedTok = Toks[0];
1894 pragmaUnusedTok.startToken();
1895 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1896 pragmaUnusedTok.setLocation(WeakLoc);
1897 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
1899 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1903 // #pragma redefine_extname identifier identifier
1904 void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1905 PragmaIntroducerKind Introducer,
1906 Token &RedefToken) {
1907 SourceLocation RedefLoc = RedefToken.getLocation();
1911 if (Tok.isNot(tok::identifier)) {
1912 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1917 Token RedefName = Tok;
1920 if (Tok.isNot(tok::identifier)) {
1921 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1922 << "redefine_extname";
1926 Token AliasName = Tok;
1929 if (Tok.isNot(tok::eod)) {
1930 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1935 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1937 Token &pragmaRedefTok = Toks[0];
1938 pragmaRedefTok.startToken();
1939 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1940 pragmaRedefTok.setLocation(RedefLoc);
1941 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
1942 Toks[1] = RedefName;
1943 Toks[2] = AliasName;
1944 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1949 PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1950 PragmaIntroducerKind Introducer,
1952 tok::OnOffSwitch OOS;
1953 if (PP.LexOnOffSwitch(OOS))
1956 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1958 Toks[0].startToken();
1959 Toks[0].setKind(tok::annot_pragma_fp_contract);
1960 Toks[0].setLocation(Tok.getLocation());
1961 Toks[0].setAnnotationEndLoc(Tok.getLocation());
1962 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1963 static_cast<uintptr_t>(OOS)));
1964 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1968 PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1969 PragmaIntroducerKind Introducer,
1971 PP.LexUnexpandedToken(Tok);
1972 if (Tok.isNot(tok::identifier)) {
1973 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1977 IdentifierInfo *Ext = Tok.getIdentifierInfo();
1978 SourceLocation NameLoc = Tok.getLocation();
1981 if (Tok.isNot(tok::colon)) {
1982 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
1987 if (Tok.isNot(tok::identifier)) {
1988 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
1991 IdentifierInfo *Pred = Tok.getIdentifierInfo();
1993 OpenCLExtState State;
1994 if (Pred->isStr("enable")) {
1996 } else if (Pred->isStr("disable")) {
1998 } else if (Pred->isStr("begin"))
2000 else if (Pred->isStr("end"))
2003 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
2004 << Ext->isStr("all");
2007 SourceLocation StateLoc = Tok.getLocation();
2010 if (Tok.isNot(tok::eod)) {
2011 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2016 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2018 Info->second = State;
2019 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2021 Toks[0].startToken();
2022 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2023 Toks[0].setLocation(NameLoc);
2024 Toks[0].setAnnotationValue(static_cast<void*>(Info));
2025 Toks[0].setAnnotationEndLoc(StateLoc);
2026 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2028 if (PP.getPPCallbacks())
2029 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
2033 /// \brief Handle '#pragma omp ...' when OpenMP is disabled.
2036 PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2037 PragmaIntroducerKind Introducer,
2039 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2040 FirstTok.getLocation())) {
2041 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
2042 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2043 diag::Severity::Ignored, SourceLocation());
2045 PP.DiscardUntilEndOfDirective();
2048 /// \brief Handle '#pragma omp ...' when OpenMP is enabled.
2051 PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2052 PragmaIntroducerKind Introducer,
2054 SmallVector<Token, 16> Pragma;
2057 Tok.setKind(tok::annot_pragma_openmp);
2058 Tok.setLocation(FirstTok.getLocation());
2060 while (Tok.isNot(tok::eod)) {
2061 Pragma.push_back(Tok);
2064 SourceLocation EodLoc = Tok.getLocation();
2066 Tok.setKind(tok::annot_pragma_openmp_end);
2067 Tok.setLocation(EodLoc);
2068 Pragma.push_back(Tok);
2070 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2071 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2072 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
2073 /*DisableMacroExpansion=*/false);
2076 /// \brief Handle '#pragma pointers_to_members'
2077 // The grammar for this pragma is as follows:
2079 // <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2081 // #pragma pointers_to_members '(' 'best_case' ')'
2082 // #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2083 // #pragma pointers_to_members '(' inheritance-model ')'
2084 void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
2085 PragmaIntroducerKind Introducer,
2087 SourceLocation PointersToMembersLoc = Tok.getLocation();
2089 if (Tok.isNot(tok::l_paren)) {
2090 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2091 << "pointers_to_members";
2095 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2097 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2098 << "pointers_to_members";
2103 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
2104 if (Arg->isStr("best_case")) {
2105 RepresentationMethod = LangOptions::PPTMK_BestCase;
2107 if (Arg->isStr("full_generality")) {
2108 if (Tok.is(tok::comma)) {
2111 Arg = Tok.getIdentifierInfo();
2113 PP.Diag(Tok.getLocation(),
2114 diag::err_pragma_pointers_to_members_unknown_kind)
2115 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2119 } else if (Tok.is(tok::r_paren)) {
2120 // #pragma pointers_to_members(full_generality) implicitly specifies
2121 // virtual_inheritance.
2123 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2125 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2126 << "full_generality";
2132 if (Arg->isStr("single_inheritance")) {
2133 RepresentationMethod =
2134 LangOptions::PPTMK_FullGeneralitySingleInheritance;
2135 } else if (Arg->isStr("multiple_inheritance")) {
2136 RepresentationMethod =
2137 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
2138 } else if (Arg->isStr("virtual_inheritance")) {
2139 RepresentationMethod =
2140 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2142 PP.Diag(Tok.getLocation(),
2143 diag::err_pragma_pointers_to_members_unknown_kind)
2144 << Arg << /*HasPointerDeclaration*/ 1;
2150 if (Tok.isNot(tok::r_paren)) {
2151 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2152 << (Arg ? Arg->getName() : "full_generality");
2156 SourceLocation EndLoc = Tok.getLocation();
2158 if (Tok.isNot(tok::eod)) {
2159 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2160 << "pointers_to_members";
2165 AnnotTok.startToken();
2166 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2167 AnnotTok.setLocation(PointersToMembersLoc);
2168 AnnotTok.setAnnotationEndLoc(EndLoc);
2169 AnnotTok.setAnnotationValue(
2170 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
2171 PP.EnterToken(AnnotTok);
2174 /// \brief Handle '#pragma vtordisp'
2175 // The grammar for this pragma is as follows:
2177 // <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2179 // #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2180 // #pragma vtordisp '(' 'pop' ')'
2181 // #pragma vtordisp '(' ')'
2182 void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
2183 PragmaIntroducerKind Introducer,
2185 SourceLocation VtorDispLoc = Tok.getLocation();
2187 if (Tok.isNot(tok::l_paren)) {
2188 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2193 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
2194 const IdentifierInfo *II = Tok.getIdentifierInfo();
2196 if (II->isStr("push")) {
2197 // #pragma vtordisp(push, mode)
2199 if (Tok.isNot(tok::comma)) {
2200 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2204 Action = Sema::PSK_Push_Set;
2205 // not push, could be on/off
2206 } else if (II->isStr("pop")) {
2207 // #pragma vtordisp(pop)
2209 Action = Sema::PSK_Pop;
2211 // not push or pop, could be on/off
2213 if (Tok.is(tok::r_paren)) {
2214 // #pragma vtordisp()
2215 Action = Sema::PSK_Reset;
2221 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
2222 const IdentifierInfo *II = Tok.getIdentifierInfo();
2223 if (II && II->isStr("off")) {
2226 } else if (II && II->isStr("on")) {
2229 } else if (Tok.is(tok::numeric_constant) &&
2230 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2232 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2233 << 0 << 2 << "vtordisp";
2237 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2243 // Finish the pragma: ')' $
2244 if (Tok.isNot(tok::r_paren)) {
2245 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2248 SourceLocation EndLoc = Tok.getLocation();
2250 if (Tok.isNot(tok::eod)) {
2251 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2256 // Enter the annotation.
2258 AnnotTok.startToken();
2259 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2260 AnnotTok.setLocation(VtorDispLoc);
2261 AnnotTok.setAnnotationEndLoc(EndLoc);
2262 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
2263 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
2264 PP.EnterToken(AnnotTok);
2267 /// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
2268 /// an annotation token.
2269 void PragmaMSPragma::HandlePragma(Preprocessor &PP,
2270 PragmaIntroducerKind Introducer,
2272 Token EoF, AnnotTok;
2274 EoF.setKind(tok::eof);
2275 AnnotTok.startToken();
2276 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2277 AnnotTok.setLocation(Tok.getLocation());
2278 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2279 SmallVector<Token, 8> TokenVector;
2280 // Suck up all of the tokens before the eod.
2281 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
2282 TokenVector.push_back(Tok);
2283 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2285 // Add a sentinal EoF token to the end of the list.
2286 TokenVector.push_back(EoF);
2287 // We must allocate this array with new because EnterTokenStream is going to
2289 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2290 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
2291 auto Value = new (PP.getPreprocessorAllocator())
2292 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2293 TokenVector.size());
2294 AnnotTok.setAnnotationValue(Value);
2295 PP.EnterToken(AnnotTok);
2298 /// \brief Handle the Microsoft \#pragma detect_mismatch extension.
2302 /// #pragma detect_mismatch("name", "value")
2304 /// Where 'name' and 'value' are quoted strings. The values are embedded in
2305 /// the object file and passed along to the linker. If the linker detects a
2306 /// mismatch in the object file's values for the given name, a LNK2038 error
2307 /// is emitted. See MSDN for more details.
2308 void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
2309 PragmaIntroducerKind Introducer,
2311 SourceLocation DetectMismatchLoc = Tok.getLocation();
2313 if (Tok.isNot(tok::l_paren)) {
2314 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
2318 // Read the name to embed, which must be a string literal.
2319 std::string NameString;
2320 if (!PP.LexStringLiteral(Tok, NameString,
2321 "pragma detect_mismatch",
2322 /*MacroExpansion=*/true))
2325 // Read the comma followed by a second string literal.
2326 std::string ValueString;
2327 if (Tok.isNot(tok::comma)) {
2328 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2332 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
2333 /*MacroExpansion=*/true))
2336 if (Tok.isNot(tok::r_paren)) {
2337 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2340 PP.Lex(Tok); // Eat the r_paren.
2342 if (Tok.isNot(tok::eod)) {
2343 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2347 // If the pragma is lexically sound, notify any interested PPCallbacks.
2348 if (PP.getPPCallbacks())
2349 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
2352 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
2355 /// \brief Handle the microsoft \#pragma comment extension.
2359 /// #pragma comment(linker, "foo")
2361 /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2362 /// "foo" is a string, which is fully macro expanded, and permits string
2363 /// concatenation, embedded escape characters etc. See MSDN for more details.
2364 void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
2365 PragmaIntroducerKind Introducer,
2367 SourceLocation CommentLoc = Tok.getLocation();
2369 if (Tok.isNot(tok::l_paren)) {
2370 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2374 // Read the identifier.
2376 if (Tok.isNot(tok::identifier)) {
2377 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2381 // Verify that this is one of the 5 whitelisted options.
2382 IdentifierInfo *II = Tok.getIdentifierInfo();
2383 PragmaMSCommentKind Kind =
2384 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2385 .Case("linker", PCK_Linker)
2386 .Case("lib", PCK_Lib)
2387 .Case("compiler", PCK_Compiler)
2388 .Case("exestr", PCK_ExeStr)
2389 .Case("user", PCK_User)
2390 .Default(PCK_Unknown);
2391 if (Kind == PCK_Unknown) {
2392 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2396 // On PS4, issue a warning about any pragma comments other than
2397 // #pragma comment lib.
2398 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
2399 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2404 // Read the optional string if present.
2406 std::string ArgumentString;
2407 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2409 /*MacroExpansion=*/true))
2412 // FIXME: warn that 'exestr' is deprecated.
2413 // FIXME: If the kind is "compiler" warn if the string is present (it is
2415 // The MSDN docs say that "lib" and "linker" require a string and have a short
2416 // whitelist of linker options they support, but in practice MSVC doesn't
2417 // issue a diagnostic. Therefore neither does clang.
2419 if (Tok.isNot(tok::r_paren)) {
2420 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2423 PP.Lex(Tok); // eat the r_paren.
2425 if (Tok.isNot(tok::eod)) {
2426 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2430 // If the pragma is lexically sound, notify any interested PPCallbacks.
2431 if (PP.getPPCallbacks())
2432 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2434 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
2437 // #pragma clang optimize off
2438 // #pragma clang optimize on
2439 void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
2440 PragmaIntroducerKind Introducer,
2441 Token &FirstToken) {
2444 if (Tok.is(tok::eod)) {
2445 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
2446 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
2449 if (Tok.isNot(tok::identifier)) {
2450 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2451 << PP.getSpelling(Tok);
2454 const IdentifierInfo *II = Tok.getIdentifierInfo();
2455 // The only accepted values are 'on' or 'off'.
2457 if (II->isStr("on")) {
2459 } else if (!II->isStr("off")) {
2460 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2461 << PP.getSpelling(Tok);
2466 if (Tok.isNot(tok::eod)) {
2467 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2468 << PP.getSpelling(Tok);
2472 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2476 /// Used as the annotation value for tok::annot_pragma_fp.
2477 struct TokFPAnnotValue {
2478 enum FlagKinds { Contract };
2479 enum FlagValues { On, Off, Fast };
2482 FlagValues FlagValue;
2484 } // end anonymous namespace
2486 void PragmaFPHandler::HandlePragma(Preprocessor &PP,
2487 PragmaIntroducerKind Introducer,
2490 Token PragmaName = Tok;
2491 SmallVector<Token, 1> TokenList;
2494 if (Tok.isNot(tok::identifier)) {
2495 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2496 << /*MissingOption=*/true << "";
2500 while (Tok.is(tok::identifier)) {
2501 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2504 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2505 OptionInfo->getName())
2506 .Case("contract", TokFPAnnotValue::Contract)
2509 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2510 << /*MissingOption=*/false << OptionInfo;
2516 if (Tok.isNot(tok::l_paren)) {
2517 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2522 if (Tok.isNot(tok::identifier)) {
2523 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2524 << PP.getSpelling(Tok) << OptionInfo->getName();
2527 const IdentifierInfo *II = Tok.getIdentifierInfo();
2530 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2532 .Case("on", TokFPAnnotValue::On)
2533 .Case("off", TokFPAnnotValue::Off)
2534 .Case("fast", TokFPAnnotValue::Fast)
2535 .Default(llvm::None);
2538 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2539 << PP.getSpelling(Tok) << OptionInfo->getName();
2545 if (Tok.isNot(tok::r_paren)) {
2546 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2551 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2552 TokFPAnnotValue{*FlagKind, *FlagValue};
2553 // Generate the loop hint token.
2556 FPTok.setKind(tok::annot_pragma_fp);
2557 FPTok.setLocation(PragmaName.getLocation());
2558 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2559 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2560 TokenList.push_back(FPTok);
2563 if (Tok.isNot(tok::eod)) {
2564 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2569 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2570 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2572 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2573 /*DisableMacroExpansion=*/false);
2576 void Parser::HandlePragmaFP() {
2577 assert(Tok.is(tok::annot_pragma_fp));
2579 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2581 LangOptions::FPContractModeKind FPC;
2582 switch (AnnotValue->FlagValue) {
2583 case TokFPAnnotValue::On:
2584 FPC = LangOptions::FPC_On;
2586 case TokFPAnnotValue::Fast:
2587 FPC = LangOptions::FPC_Fast;
2589 case TokFPAnnotValue::Off:
2590 FPC = LangOptions::FPC_Off;
2594 Actions.ActOnPragmaFPContract(FPC);
2595 ConsumeAnnotationToken();
2598 /// \brief Parses loop or unroll pragma hint value and fills in Info.
2599 static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2600 Token Option, bool ValueInParens,
2601 PragmaLoopHintInfo &Info) {
2602 SmallVector<Token, 1> ValueList;
2603 int OpenParens = ValueInParens ? 1 : 0;
2604 // Read constant expression.
2605 while (Tok.isNot(tok::eod)) {
2606 if (Tok.is(tok::l_paren))
2608 else if (Tok.is(tok::r_paren)) {
2610 if (OpenParens == 0 && ValueInParens)
2614 ValueList.push_back(Tok);
2618 if (ValueInParens) {
2620 if (Tok.isNot(tok::r_paren)) {
2621 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2628 EOFTok.startToken();
2629 EOFTok.setKind(tok::eof);
2630 EOFTok.setLocation(Tok.getLocation());
2631 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2633 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
2635 Info.PragmaName = PragmaName;
2636 Info.Option = Option;
2640 /// \brief Handle the \#pragma clang loop directive.
2641 /// #pragma clang 'loop' loop-hints
2644 /// loop-hint loop-hints[opt]
2647 /// 'vectorize' '(' loop-hint-keyword ')'
2648 /// 'interleave' '(' loop-hint-keyword ')'
2649 /// 'unroll' '(' unroll-hint-keyword ')'
2650 /// 'vectorize_width' '(' loop-hint-value ')'
2651 /// 'interleave_count' '(' loop-hint-value ')'
2652 /// 'unroll_count' '(' loop-hint-value ')'
2654 /// loop-hint-keyword:
2659 /// unroll-hint-keyword:
2664 /// loop-hint-value:
2665 /// constant-expression
2667 /// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2668 /// try vectorizing the instructions of the loop it precedes. Specifying
2669 /// interleave(enable) or interleave_count(_value_) instructs llvm to try
2670 /// interleaving multiple iterations of the loop it precedes. The width of the
2671 /// vector instructions is specified by vectorize_width() and the number of
2672 /// interleaved loop iterations is specified by interleave_count(). Specifying a
2673 /// value of 1 effectively disables vectorization/interleaving, even if it is
2674 /// possible and profitable, and 0 is invalid. The loop vectorizer currently
2675 /// only works on inner loops.
2677 /// The unroll and unroll_count directives control the concatenation
2678 /// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2679 /// completely if the trip count is known at compile time and unroll partially
2680 /// if the trip count is not known. Specifying unroll(full) is similar to
2681 /// unroll(enable) but will unroll the loop only if the trip count is known at
2682 /// compile time. Specifying unroll(disable) disables unrolling for the
2683 /// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2684 /// loop the number of times indicated by the value.
2685 void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2686 PragmaIntroducerKind Introducer,
2688 // Incoming token is "loop" from "#pragma clang loop".
2689 Token PragmaName = Tok;
2690 SmallVector<Token, 1> TokenList;
2692 // Lex the optimization option and verify it is an identifier.
2694 if (Tok.isNot(tok::identifier)) {
2695 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2696 << /*MissingOption=*/true << "";
2700 while (Tok.is(tok::identifier)) {
2702 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2704 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
2705 .Case("vectorize", true)
2706 .Case("interleave", true)
2707 .Case("unroll", true)
2708 .Case("distribute", true)
2709 .Case("vectorize_width", true)
2710 .Case("interleave_count", true)
2711 .Case("unroll_count", true)
2714 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2715 << /*MissingOption=*/false << OptionInfo;
2721 if (Tok.isNot(tok::l_paren)) {
2722 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2727 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2728 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2732 // Generate the loop hint token.
2734 LoopHintTok.startToken();
2735 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
2736 LoopHintTok.setLocation(PragmaName.getLocation());
2737 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
2738 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2739 TokenList.push_back(LoopHintTok);
2742 if (Tok.isNot(tok::eod)) {
2743 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2748 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2749 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2751 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2752 /*DisableMacroExpansion=*/false);
2755 /// \brief Handle the loop unroll optimization pragmas.
2757 /// #pragma unroll unroll-hint-value
2758 /// #pragma unroll '(' unroll-hint-value ')'
2759 /// #pragma nounroll
2761 /// unroll-hint-value:
2762 /// constant-expression
2764 /// Loop unrolling hints can be specified with '#pragma unroll' or
2765 /// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2766 /// contained in parentheses. With no argument the directive instructs llvm to
2767 /// try to unroll the loop completely. A positive integer argument can be
2768 /// specified to indicate the number of times the loop should be unrolled. To
2769 /// maximize compatibility with other compilers the unroll count argument can be
2770 /// specified with or without parentheses. Specifying, '#pragma nounroll'
2771 /// disables unrolling of the loop.
2772 void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2773 PragmaIntroducerKind Introducer,
2775 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2776 // "#pragma nounroll".
2777 Token PragmaName = Tok;
2779 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2780 if (Tok.is(tok::eod)) {
2781 // nounroll or unroll pragma without an argument.
2782 Info->PragmaName = PragmaName;
2783 Info->Option.startToken();
2784 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2785 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2789 // Unroll pragma with an argument: "#pragma unroll N" or
2790 // "#pragma unroll(N)".
2791 // Read '(' if it exists.
2792 bool ValueInParens = Tok.is(tok::l_paren);
2797 Option.startToken();
2798 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
2801 // In CUDA, the argument to '#pragma unroll' should not be contained in
2803 if (PP.getLangOpts().CUDA && ValueInParens)
2804 PP.Diag(Info->Toks[0].getLocation(),
2805 diag::warn_pragma_unroll_cuda_value_in_parens);
2807 if (Tok.isNot(tok::eod)) {
2808 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2814 // Generate the hint token.
2815 auto TokenArray = llvm::make_unique<Token[]>(1);
2816 TokenArray[0].startToken();
2817 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2818 TokenArray[0].setLocation(PragmaName.getLocation());
2819 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
2820 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2821 PP.EnterTokenStream(std::move(TokenArray), 1,
2822 /*DisableMacroExpansion=*/false);
2825 /// \brief Handle the Microsoft \#pragma intrinsic extension.
2829 /// #pragma intrinsic(memset)
2830 /// #pragma intrinsic(strlen, memcpy)
2833 /// Pragma intrisic tells the compiler to use a builtin version of the
2834 /// function. Clang does it anyway, so the pragma doesn't really do anything.
2835 /// Anyway, we emit a warning if the function specified in \#pragma intrinsic
2836 /// isn't an intrinsic in clang and suggest to include intrin.h.
2837 void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
2838 PragmaIntroducerKind Introducer,
2842 if (Tok.isNot(tok::l_paren)) {
2843 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
2849 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
2851 while (Tok.is(tok::identifier)) {
2852 IdentifierInfo *II = Tok.getIdentifierInfo();
2853 if (!II->getBuiltinID())
2854 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
2855 << II << SuggestIntrinH;
2858 if (Tok.isNot(tok::comma))
2863 if (Tok.isNot(tok::r_paren)) {
2864 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
2870 if (Tok.isNot(tok::eod))
2871 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2874 void PragmaForceCUDAHostDeviceHandler::HandlePragma(
2875 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
2876 Token FirstTok = Tok;
2879 IdentifierInfo *Info = Tok.getIdentifierInfo();
2880 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
2881 PP.Diag(FirstTok.getLocation(),
2882 diag::warn_pragma_force_cuda_host_device_bad_arg);
2886 if (Info->isStr("begin"))
2887 Actions.PushForceCUDAHostDevice();
2888 else if (!Actions.PopForceCUDAHostDevice())
2889 PP.Diag(FirstTok.getLocation(),
2890 diag::err_pragma_cannot_end_force_cuda_host_device);
2893 if (!Tok.is(tok::eod))
2894 PP.Diag(FirstTok.getLocation(),
2895 diag::warn_pragma_force_cuda_host_device_bad_arg);
2898 /// \brief Handle the #pragma clang attribute directive.
2902 /// #pragma clang attribute push(attribute, subject-set)
2903 /// #pragma clang attribute pop
2906 /// The subject-set clause defines the set of declarations which receive the
2907 /// attribute. Its exact syntax is described in the LanguageExtensions document
2908 /// in Clang's documentation.
2910 /// This directive instructs the compiler to begin/finish applying the specified
2911 /// attribute to the set of attribute-specific declarations in the active range
2913 void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
2914 PragmaIntroducerKind Introducer,
2915 Token &FirstToken) {
2918 auto *Info = new (PP.getPreprocessorAllocator())
2919 PragmaAttributeInfo(AttributesForPragmaAttribute);
2921 // Parse the 'push' or 'pop'.
2922 if (Tok.isNot(tok::identifier)) {
2923 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_push_pop);
2926 const auto *II = Tok.getIdentifierInfo();
2927 if (II->isStr("push"))
2928 Info->Action = PragmaAttributeInfo::Push;
2929 else if (II->isStr("pop"))
2930 Info->Action = PragmaAttributeInfo::Pop;
2932 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
2933 << PP.getSpelling(Tok);
2938 // Parse the actual attribute.
2939 if (Info->Action == PragmaAttributeInfo::Push) {
2940 if (Tok.isNot(tok::l_paren)) {
2941 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2946 // Lex the attribute tokens.
2947 SmallVector<Token, 16> AttributeTokens;
2949 while (Tok.isNot(tok::eod)) {
2950 if (Tok.is(tok::l_paren))
2952 else if (Tok.is(tok::r_paren)) {
2954 if (OpenParens == 0)
2958 AttributeTokens.push_back(Tok);
2962 if (AttributeTokens.empty()) {
2963 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
2966 if (Tok.isNot(tok::r_paren)) {
2967 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2970 SourceLocation EndLoc = Tok.getLocation();
2973 // Terminate the attribute for parsing.
2975 EOFTok.startToken();
2976 EOFTok.setKind(tok::eof);
2977 EOFTok.setLocation(EndLoc);
2978 AttributeTokens.push_back(EOFTok);
2981 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
2984 if (Tok.isNot(tok::eod))
2985 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2986 << "clang attribute";
2988 // Generate the annotated pragma token.
2989 auto TokenArray = llvm::make_unique<Token[]>(1);
2990 TokenArray[0].startToken();
2991 TokenArray[0].setKind(tok::annot_pragma_attribute);
2992 TokenArray[0].setLocation(FirstToken.getLocation());
2993 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
2994 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2995 PP.EnterTokenStream(std::move(TokenArray), 1,
2996 /*DisableMacroExpansion=*/false);