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 = ConsumeAnnotationToken();
426 ExprResult Alignment;
427 if (Info->Alignment.is(tok::numeric_constant)) {
428 Alignment = Actions.ActOnNumericConstant(Info->Alignment);
429 if (Alignment.isInvalid())
432 Actions.ActOnPragmaPack(PragmaLoc, Info->Action, Info->SlotLabel,
436 void Parser::HandlePragmaMSStruct() {
437 assert(Tok.is(tok::annot_pragma_msstruct));
438 PragmaMSStructKind Kind = static_cast<PragmaMSStructKind>(
439 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
440 Actions.ActOnPragmaMSStruct(Kind);
441 ConsumeAnnotationToken();
444 void Parser::HandlePragmaAlign() {
445 assert(Tok.is(tok::annot_pragma_align));
446 Sema::PragmaOptionsAlignKind Kind =
447 static_cast<Sema::PragmaOptionsAlignKind>(
448 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
449 SourceLocation PragmaLoc = ConsumeAnnotationToken();
450 Actions.ActOnPragmaOptionsAlign(Kind, PragmaLoc);
453 void Parser::HandlePragmaDump() {
454 assert(Tok.is(tok::annot_pragma_dump));
456 reinterpret_cast<IdentifierInfo *>(Tok.getAnnotationValue());
457 Actions.ActOnPragmaDump(getCurScope(), Tok.getLocation(), II);
458 ConsumeAnnotationToken();
461 void Parser::HandlePragmaWeak() {
462 assert(Tok.is(tok::annot_pragma_weak));
463 SourceLocation PragmaLoc = ConsumeAnnotationToken();
464 Actions.ActOnPragmaWeakID(Tok.getIdentifierInfo(), PragmaLoc,
466 ConsumeToken(); // The weak name.
469 void Parser::HandlePragmaWeakAlias() {
470 assert(Tok.is(tok::annot_pragma_weakalias));
471 SourceLocation PragmaLoc = ConsumeAnnotationToken();
472 IdentifierInfo *WeakName = Tok.getIdentifierInfo();
473 SourceLocation WeakNameLoc = Tok.getLocation();
475 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
476 SourceLocation AliasNameLoc = Tok.getLocation();
478 Actions.ActOnPragmaWeakAlias(WeakName, AliasName, PragmaLoc,
479 WeakNameLoc, AliasNameLoc);
483 void Parser::HandlePragmaRedefineExtname() {
484 assert(Tok.is(tok::annot_pragma_redefine_extname));
485 SourceLocation RedefLoc = ConsumeAnnotationToken();
486 IdentifierInfo *RedefName = Tok.getIdentifierInfo();
487 SourceLocation RedefNameLoc = Tok.getLocation();
489 IdentifierInfo *AliasName = Tok.getIdentifierInfo();
490 SourceLocation AliasNameLoc = Tok.getLocation();
492 Actions.ActOnPragmaRedefineExtname(RedefName, AliasName, RedefLoc,
493 RedefNameLoc, AliasNameLoc);
496 void Parser::HandlePragmaFPContract() {
497 assert(Tok.is(tok::annot_pragma_fp_contract));
498 tok::OnOffSwitch OOS =
499 static_cast<tok::OnOffSwitch>(
500 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
502 LangOptions::FPContractModeKind FPC;
505 FPC = LangOptions::FPC_On;
508 FPC = LangOptions::FPC_Off;
510 case tok::OOS_DEFAULT:
511 FPC = getLangOpts().getDefaultFPContractMode();
515 Actions.ActOnPragmaFPContract(FPC);
516 ConsumeAnnotationToken();
519 StmtResult Parser::HandlePragmaCaptured()
521 assert(Tok.is(tok::annot_pragma_captured));
522 ConsumeAnnotationToken();
524 if (Tok.isNot(tok::l_brace)) {
525 PP.Diag(Tok, diag::err_expected) << tok::l_brace;
529 SourceLocation Loc = Tok.getLocation();
531 ParseScope CapturedRegionScope(this, Scope::FnScope | Scope::DeclScope);
532 Actions.ActOnCapturedRegionStart(Loc, getCurScope(), CR_Default,
535 StmtResult R = ParseCompoundStatement();
536 CapturedRegionScope.Exit();
539 Actions.ActOnCapturedRegionError();
543 return Actions.ActOnCapturedRegionEnd(R.get());
547 enum OpenCLExtState : char {
548 Disable, Enable, Begin, End
550 typedef std::pair<const IdentifierInfo *, OpenCLExtState> OpenCLExtData;
553 void Parser::HandlePragmaOpenCLExtension() {
554 assert(Tok.is(tok::annot_pragma_opencl_extension));
555 OpenCLExtData *Data = static_cast<OpenCLExtData*>(Tok.getAnnotationValue());
556 auto State = Data->second;
557 auto Ident = Data->first;
558 SourceLocation NameLoc = Tok.getLocation();
559 ConsumeAnnotationToken();
561 auto &Opt = Actions.getOpenCLOptions();
562 auto Name = Ident->getName();
563 // OpenCL 1.1 9.1: "The all variant sets the behavior for all extensions,
564 // overriding all previously issued extension directives, but only if the
565 // behavior is set to disable."
567 if (State == Disable) {
569 Opt.enableSupportedCore(getLangOpts().OpenCLVersion);
571 PP.Diag(NameLoc, diag::warn_pragma_expected_predicate) << 1;
573 } else if (State == Begin) {
574 if (!Opt.isKnown(Name) ||
575 !Opt.isSupported(Name, getLangOpts().OpenCLVersion)) {
578 Actions.setCurrentOpenCLExtension(Name);
579 } else if (State == End) {
580 if (Name != Actions.getCurrentOpenCLExtension())
581 PP.Diag(NameLoc, diag::warn_pragma_begin_end_mismatch);
582 Actions.setCurrentOpenCLExtension("");
583 } else if (!Opt.isKnown(Name))
584 PP.Diag(NameLoc, diag::warn_pragma_unknown_extension) << Ident;
585 else if (Opt.isSupportedExtension(Name, getLangOpts().OpenCLVersion))
586 Opt.enable(Name, State == Enable);
587 else if (Opt.isSupportedCore(Name, getLangOpts().OpenCLVersion))
588 PP.Diag(NameLoc, diag::warn_pragma_extension_is_core) << Ident;
590 PP.Diag(NameLoc, diag::warn_pragma_unsupported_extension) << Ident;
593 void Parser::HandlePragmaMSPointersToMembers() {
594 assert(Tok.is(tok::annot_pragma_ms_pointers_to_members));
595 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod =
596 static_cast<LangOptions::PragmaMSPointersToMembersKind>(
597 reinterpret_cast<uintptr_t>(Tok.getAnnotationValue()));
598 SourceLocation PragmaLoc = ConsumeAnnotationToken();
599 Actions.ActOnPragmaMSPointersToMembers(RepresentationMethod, PragmaLoc);
602 void Parser::HandlePragmaMSVtorDisp() {
603 assert(Tok.is(tok::annot_pragma_ms_vtordisp));
604 uintptr_t Value = reinterpret_cast<uintptr_t>(Tok.getAnnotationValue());
605 Sema::PragmaMsStackAction Action =
606 static_cast<Sema::PragmaMsStackAction>((Value >> 16) & 0xFFFF);
607 MSVtorDispAttr::Mode Mode = MSVtorDispAttr::Mode(Value & 0xFFFF);
608 SourceLocation PragmaLoc = ConsumeAnnotationToken();
609 Actions.ActOnPragmaMSVtorDisp(Action, PragmaLoc, Mode);
612 void Parser::HandlePragmaMSPragma() {
613 assert(Tok.is(tok::annot_pragma_ms_pragma));
614 // Grab the tokens out of the annotation and enter them into the stream.
616 (std::pair<std::unique_ptr<Token[]>, size_t> *)Tok.getAnnotationValue();
617 PP.EnterTokenStream(std::move(TheTokens->first), TheTokens->second, true);
618 SourceLocation PragmaLocation = ConsumeAnnotationToken();
619 assert(Tok.isAnyIdentifier());
620 StringRef PragmaName = Tok.getIdentifierInfo()->getName();
621 PP.Lex(Tok); // pragma kind
623 // Figure out which #pragma we're dealing with. The switch has no default
624 // because lex shouldn't emit the annotation token for unrecognized pragmas.
625 typedef bool (Parser::*PragmaHandler)(StringRef, SourceLocation);
626 PragmaHandler Handler = llvm::StringSwitch<PragmaHandler>(PragmaName)
627 .Case("data_seg", &Parser::HandlePragmaMSSegment)
628 .Case("bss_seg", &Parser::HandlePragmaMSSegment)
629 .Case("const_seg", &Parser::HandlePragmaMSSegment)
630 .Case("code_seg", &Parser::HandlePragmaMSSegment)
631 .Case("section", &Parser::HandlePragmaMSSection)
632 .Case("init_seg", &Parser::HandlePragmaMSInitSeg);
634 if (!(this->*Handler)(PragmaName, PragmaLocation)) {
635 // Pragma handling failed, and has been diagnosed. Slurp up the tokens
636 // until eof (really end of line) to prevent follow-on errors.
637 while (Tok.isNot(tok::eof))
643 bool Parser::HandlePragmaMSSection(StringRef PragmaName,
644 SourceLocation PragmaLocation) {
645 if (Tok.isNot(tok::l_paren)) {
646 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
650 // Parsing code for pragma section
651 if (Tok.isNot(tok::string_literal)) {
652 PP.Diag(PragmaLocation, diag::warn_pragma_expected_section_name)
656 ExprResult StringResult = ParseStringLiteralExpression();
657 if (StringResult.isInvalid())
658 return false; // Already diagnosed.
659 StringLiteral *SegmentName = cast<StringLiteral>(StringResult.get());
660 if (SegmentName->getCharByteWidth() != 1) {
661 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
665 int SectionFlags = ASTContext::PSF_Read;
666 bool SectionFlagsAreDefault = true;
667 while (Tok.is(tok::comma)) {
669 // Ignore "long" and "short".
670 // They are undocumented, but widely used, section attributes which appear
672 if (Tok.is(tok::kw_long) || Tok.is(tok::kw_short)) {
673 PP.Lex(Tok); // long/short
677 if (!Tok.isAnyIdentifier()) {
678 PP.Diag(PragmaLocation, diag::warn_pragma_expected_action_or_r_paren)
682 ASTContext::PragmaSectionFlag Flag =
683 llvm::StringSwitch<ASTContext::PragmaSectionFlag>(
684 Tok.getIdentifierInfo()->getName())
685 .Case("read", ASTContext::PSF_Read)
686 .Case("write", ASTContext::PSF_Write)
687 .Case("execute", ASTContext::PSF_Execute)
688 .Case("shared", ASTContext::PSF_Invalid)
689 .Case("nopage", ASTContext::PSF_Invalid)
690 .Case("nocache", ASTContext::PSF_Invalid)
691 .Case("discard", ASTContext::PSF_Invalid)
692 .Case("remove", ASTContext::PSF_Invalid)
693 .Default(ASTContext::PSF_None);
694 if (Flag == ASTContext::PSF_None || Flag == ASTContext::PSF_Invalid) {
695 PP.Diag(PragmaLocation, Flag == ASTContext::PSF_None
696 ? diag::warn_pragma_invalid_specific_action
697 : diag::warn_pragma_unsupported_action)
698 << PragmaName << Tok.getIdentifierInfo()->getName();
701 SectionFlags |= Flag;
702 SectionFlagsAreDefault = false;
703 PP.Lex(Tok); // Identifier
705 // If no section attributes are specified, the section will be marked as
707 if (SectionFlagsAreDefault)
708 SectionFlags |= ASTContext::PSF_Write;
709 if (Tok.isNot(tok::r_paren)) {
710 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
714 if (Tok.isNot(tok::eof)) {
715 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
720 Actions.ActOnPragmaMSSection(PragmaLocation, SectionFlags, SegmentName);
724 bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
725 SourceLocation PragmaLocation) {
726 if (Tok.isNot(tok::l_paren)) {
727 PP.Diag(PragmaLocation, diag::warn_pragma_expected_lparen) << PragmaName;
731 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
733 if (Tok.isAnyIdentifier()) {
734 StringRef PushPop = Tok.getIdentifierInfo()->getName();
735 if (PushPop == "push")
736 Action = Sema::PSK_Push;
737 else if (PushPop == "pop")
738 Action = Sema::PSK_Pop;
740 PP.Diag(PragmaLocation,
741 diag::warn_pragma_expected_section_push_pop_or_name)
745 if (Action != Sema::PSK_Reset) {
746 PP.Lex(Tok); // push | pop
747 if (Tok.is(tok::comma)) {
749 // If we've got a comma, we either need a label or a string.
750 if (Tok.isAnyIdentifier()) {
751 SlotLabel = Tok.getIdentifierInfo()->getName();
752 PP.Lex(Tok); // identifier
753 if (Tok.is(tok::comma))
755 else if (Tok.isNot(tok::r_paren)) {
756 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc)
761 } else if (Tok.isNot(tok::r_paren)) {
762 PP.Diag(PragmaLocation, diag::warn_pragma_expected_punc) << PragmaName;
767 // Grab the string literal for our section name.
768 StringLiteral *SegmentName = nullptr;
769 if (Tok.isNot(tok::r_paren)) {
770 if (Tok.isNot(tok::string_literal)) {
771 unsigned DiagID = Action != Sema::PSK_Reset ? !SlotLabel.empty() ?
772 diag::warn_pragma_expected_section_name :
773 diag::warn_pragma_expected_section_label_or_name :
774 diag::warn_pragma_expected_section_push_pop_or_name;
775 PP.Diag(PragmaLocation, DiagID) << PragmaName;
778 ExprResult StringResult = ParseStringLiteralExpression();
779 if (StringResult.isInvalid())
780 return false; // Already diagnosed.
781 SegmentName = cast<StringLiteral>(StringResult.get());
782 if (SegmentName->getCharByteWidth() != 1) {
783 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
787 // Setting section "" has no effect
788 if (SegmentName->getLength())
789 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
791 if (Tok.isNot(tok::r_paren)) {
792 PP.Diag(PragmaLocation, diag::warn_pragma_expected_rparen) << PragmaName;
796 if (Tok.isNot(tok::eof)) {
797 PP.Diag(PragmaLocation, diag::warn_pragma_extra_tokens_at_eol)
802 Actions.ActOnPragmaMSSeg(PragmaLocation, Action, SlotLabel,
803 SegmentName, PragmaName);
807 // #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
808 bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
809 SourceLocation PragmaLocation) {
810 if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
811 PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
815 if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
819 // Parse either the known section names or the string section name.
820 StringLiteral *SegmentName = nullptr;
821 if (Tok.isAnyIdentifier()) {
822 auto *II = Tok.getIdentifierInfo();
823 StringRef Section = llvm::StringSwitch<StringRef>(II->getName())
824 .Case("compiler", "\".CRT$XCC\"")
825 .Case("lib", "\".CRT$XCL\"")
826 .Case("user", "\".CRT$XCU\"")
829 if (!Section.empty()) {
830 // Pretend the user wrote the appropriate string literal here.
832 Toks[0].startToken();
833 Toks[0].setKind(tok::string_literal);
834 Toks[0].setLocation(Tok.getLocation());
835 Toks[0].setLiteralData(Section.data());
836 Toks[0].setLength(Section.size());
838 cast<StringLiteral>(Actions.ActOnStringLiteral(Toks, nullptr).get());
841 } else if (Tok.is(tok::string_literal)) {
842 ExprResult StringResult = ParseStringLiteralExpression();
843 if (StringResult.isInvalid())
845 SegmentName = cast<StringLiteral>(StringResult.get());
846 if (SegmentName->getCharByteWidth() != 1) {
847 PP.Diag(PragmaLocation, diag::warn_pragma_expected_non_wide_string)
851 // FIXME: Add support for the '[, func-name]' part of the pragma.
855 PP.Diag(PragmaLocation, diag::warn_pragma_expected_init_seg) << PragmaName;
859 if (ExpectAndConsume(tok::r_paren, diag::warn_pragma_expected_rparen,
861 ExpectAndConsume(tok::eof, diag::warn_pragma_extra_tokens_at_eol,
865 Actions.ActOnPragmaMSInitSeg(PragmaLocation, SegmentName);
870 struct PragmaLoopHintInfo {
873 ArrayRef<Token> Toks;
875 } // end anonymous namespace
877 static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
878 std::string PragmaString;
879 if (PragmaName.getIdentifierInfo()->getName() == "loop") {
880 PragmaString = "clang loop ";
881 PragmaString += Option.getIdentifierInfo()->getName();
883 assert(PragmaName.getIdentifierInfo()->getName() == "unroll" &&
884 "Unexpected pragma name");
885 PragmaString = "unroll";
890 bool Parser::HandlePragmaLoopHint(LoopHint &Hint) {
891 assert(Tok.is(tok::annot_pragma_loop_hint));
892 PragmaLoopHintInfo *Info =
893 static_cast<PragmaLoopHintInfo *>(Tok.getAnnotationValue());
895 IdentifierInfo *PragmaNameInfo = Info->PragmaName.getIdentifierInfo();
896 Hint.PragmaNameLoc = IdentifierLoc::create(
897 Actions.Context, Info->PragmaName.getLocation(), PragmaNameInfo);
899 // It is possible that the loop hint has no option identifier, such as
900 // #pragma unroll(4).
901 IdentifierInfo *OptionInfo = Info->Option.is(tok::identifier)
902 ? Info->Option.getIdentifierInfo()
904 Hint.OptionLoc = IdentifierLoc::create(
905 Actions.Context, Info->Option.getLocation(), OptionInfo);
907 llvm::ArrayRef<Token> Toks = Info->Toks;
909 // Return a valid hint if pragma unroll or nounroll were specified
910 // without an argument.
911 bool PragmaUnroll = PragmaNameInfo->getName() == "unroll";
912 bool PragmaNoUnroll = PragmaNameInfo->getName() == "nounroll";
913 if (Toks.empty() && (PragmaUnroll || PragmaNoUnroll)) {
914 ConsumeAnnotationToken();
915 Hint.Range = Info->PragmaName.getLocation();
919 // The constant expression is always followed by an eof token, which increases
921 assert(!Toks.empty() &&
922 "PragmaLoopHintInfo::Toks must contain at least one token.");
924 // If no option is specified the argument is assumed to be a constant expr.
925 bool OptionUnroll = false;
926 bool OptionDistribute = false;
927 bool StateOption = false;
928 if (OptionInfo) { // Pragma Unroll does not specify an option.
929 OptionUnroll = OptionInfo->isStr("unroll");
930 OptionDistribute = OptionInfo->isStr("distribute");
931 StateOption = llvm::StringSwitch<bool>(OptionInfo->getName())
932 .Case("vectorize", true)
933 .Case("interleave", true)
935 OptionUnroll || OptionDistribute;
938 bool AssumeSafetyArg = !OptionUnroll && !OptionDistribute;
939 // Verify loop hint has an argument.
940 if (Toks[0].is(tok::eof)) {
941 ConsumeAnnotationToken();
942 Diag(Toks[0].getLocation(), diag::err_pragma_loop_missing_argument)
943 << /*StateArgument=*/StateOption << /*FullKeyword=*/OptionUnroll
944 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
948 // Validate the argument.
950 ConsumeAnnotationToken();
951 SourceLocation StateLoc = Toks[0].getLocation();
952 IdentifierInfo *StateInfo = Toks[0].getIdentifierInfo();
954 bool Valid = StateInfo &&
955 llvm::StringSwitch<bool>(StateInfo->getName())
956 .Cases("enable", "disable", true)
957 .Case("full", OptionUnroll)
958 .Case("assume_safety", AssumeSafetyArg)
961 Diag(Toks[0].getLocation(), diag::err_pragma_invalid_keyword)
962 << /*FullKeyword=*/OptionUnroll
963 << /*AssumeSafetyKeyword=*/AssumeSafetyArg;
967 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
968 << PragmaLoopHintString(Info->PragmaName, Info->Option);
969 Hint.StateLoc = IdentifierLoc::create(Actions.Context, StateLoc, StateInfo);
971 // Enter constant expression including eof terminator into token stream.
972 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/false);
973 ConsumeAnnotationToken();
975 ExprResult R = ParseConstantExpression();
977 // Tokens following an error in an ill-formed constant expression will
978 // remain in the token stream and must be removed.
979 if (Tok.isNot(tok::eof)) {
980 Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
981 << PragmaLoopHintString(Info->PragmaName, Info->Option);
982 while (Tok.isNot(tok::eof))
986 ConsumeToken(); // Consume the constant expression eof terminator.
989 Actions.CheckLoopHintExpr(R.get(), Toks[0].getLocation()))
992 // Argument is a constant expression with an integer type.
993 Hint.ValueExpr = R.get();
996 Hint.Range = SourceRange(Info->PragmaName.getLocation(),
997 Info->Toks.back().getLocation());
1002 struct PragmaAttributeInfo {
1003 enum ActionType { Push, Pop };
1004 ParsedAttributes &Attributes;
1006 ArrayRef<Token> Tokens;
1008 PragmaAttributeInfo(ParsedAttributes &Attributes) : Attributes(Attributes) {}
1011 #include "clang/Parse/AttrSubMatchRulesParserStringSwitches.inc"
1013 } // end anonymous namespace
1015 static StringRef getIdentifier(const Token &Tok) {
1016 if (Tok.is(tok::identifier))
1017 return Tok.getIdentifierInfo()->getName();
1018 const char *S = tok::getKeywordSpelling(Tok.getKind());
1024 static bool isAbstractAttrMatcherRule(attr::SubjectMatchRule Rule) {
1025 using namespace attr;
1027 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) \
1030 #include "clang/Basic/AttrSubMatchRulesList.inc"
1032 llvm_unreachable("Invalid attribute subject match rule");
1036 static void diagnoseExpectedAttributeSubjectSubRule(
1037 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1038 SourceLocation SubRuleLoc) {
1040 PRef.Diag(SubRuleLoc,
1041 diag::err_pragma_attribute_expected_subject_sub_identifier)
1043 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1044 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1046 Diagnostic << /*SubRulesSupported=*/0;
1049 static void diagnoseUnknownAttributeSubjectSubRule(
1050 Parser &PRef, attr::SubjectMatchRule PrimaryRule, StringRef PrimaryRuleName,
1051 StringRef SubRuleName, SourceLocation SubRuleLoc) {
1054 PRef.Diag(SubRuleLoc, diag::err_pragma_attribute_unknown_subject_sub_rule)
1055 << SubRuleName << PrimaryRuleName;
1056 if (const char *SubRules = validAttributeSubjectMatchSubRules(PrimaryRule))
1057 Diagnostic << /*SubRulesSupported=*/1 << SubRules;
1059 Diagnostic << /*SubRulesSupported=*/0;
1062 bool Parser::ParsePragmaAttributeSubjectMatchRuleSet(
1063 attr::ParsedSubjectMatchRuleSet &SubjectMatchRules, SourceLocation &AnyLoc,
1064 SourceLocation &LastMatchRuleEndLoc) {
1066 BalancedDelimiterTracker AnyParens(*this, tok::l_paren);
1067 if (getIdentifier(Tok) == "any") {
1068 AnyLoc = ConsumeToken();
1070 if (AnyParens.expectAndConsume())
1075 // Parse the subject matcher rule.
1076 StringRef Name = getIdentifier(Tok);
1078 Diag(Tok, diag::err_pragma_attribute_expected_subject_identifier);
1081 std::pair<Optional<attr::SubjectMatchRule>,
1082 Optional<attr::SubjectMatchRule> (*)(StringRef, bool)>
1083 Rule = isAttributeSubjectMatchRule(Name);
1085 Diag(Tok, diag::err_pragma_attribute_unknown_subject_rule) << Name;
1088 attr::SubjectMatchRule PrimaryRule = *Rule.first;
1089 SourceLocation RuleLoc = ConsumeToken();
1091 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1092 if (isAbstractAttrMatcherRule(PrimaryRule)) {
1093 if (Parens.expectAndConsume())
1095 } else if (Parens.consumeOpen()) {
1096 if (!SubjectMatchRules
1098 std::make_pair(PrimaryRule, SourceRange(RuleLoc, RuleLoc)))
1100 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1102 << FixItHint::CreateRemoval(SourceRange(
1103 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleLoc));
1104 LastMatchRuleEndLoc = RuleLoc;
1108 // Parse the sub-rules.
1109 StringRef SubRuleName = getIdentifier(Tok);
1110 if (SubRuleName.empty()) {
1111 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1115 attr::SubjectMatchRule SubRule;
1116 if (SubRuleName == "unless") {
1117 SourceLocation SubRuleLoc = ConsumeToken();
1118 BalancedDelimiterTracker Parens(*this, tok::l_paren);
1119 if (Parens.expectAndConsume())
1121 SubRuleName = getIdentifier(Tok);
1122 if (SubRuleName.empty()) {
1123 diagnoseExpectedAttributeSubjectSubRule(*this, PrimaryRule, Name,
1127 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/true);
1128 if (!SubRuleOrNone) {
1129 std::string SubRuleUnlessName = "unless(" + SubRuleName.str() + ")";
1130 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1131 SubRuleUnlessName, SubRuleLoc);
1134 SubRule = *SubRuleOrNone;
1136 if (Parens.consumeClose())
1139 auto SubRuleOrNone = Rule.second(SubRuleName, /*IsUnless=*/false);
1140 if (!SubRuleOrNone) {
1141 diagnoseUnknownAttributeSubjectSubRule(*this, PrimaryRule, Name,
1142 SubRuleName, Tok.getLocation());
1145 SubRule = *SubRuleOrNone;
1148 SourceLocation RuleEndLoc = Tok.getLocation();
1149 LastMatchRuleEndLoc = RuleEndLoc;
1150 if (Parens.consumeClose())
1152 if (!SubjectMatchRules
1153 .insert(std::make_pair(SubRule, SourceRange(RuleLoc, RuleEndLoc)))
1155 Diag(RuleLoc, diag::err_pragma_attribute_duplicate_subject)
1156 << attr::getSubjectMatchRuleSpelling(SubRule)
1157 << FixItHint::CreateRemoval(SourceRange(
1158 RuleLoc, Tok.is(tok::comma) ? Tok.getLocation() : RuleEndLoc));
1161 } while (IsAny && TryConsumeToken(tok::comma));
1164 if (AnyParens.consumeClose())
1172 /// Describes the stage at which attribute subject rule parsing was interruped.
1173 enum class MissingAttributeSubjectRulesRecoveryPoint {
1181 MissingAttributeSubjectRulesRecoveryPoint
1182 getAttributeSubjectRulesRecoveryPointForToken(const Token &Tok) {
1183 if (const auto *II = Tok.getIdentifierInfo()) {
1184 if (II->isStr("apply_to"))
1185 return MissingAttributeSubjectRulesRecoveryPoint::ApplyTo;
1186 if (II->isStr("any"))
1187 return MissingAttributeSubjectRulesRecoveryPoint::Any;
1189 if (Tok.is(tok::equal))
1190 return MissingAttributeSubjectRulesRecoveryPoint::Equals;
1191 return MissingAttributeSubjectRulesRecoveryPoint::None;
1194 /// Creates a diagnostic for the attribute subject rule parsing diagnostic that
1195 /// suggests the possible attribute subject rules in a fix-it together with
1196 /// any other missing tokens.
1197 DiagnosticBuilder createExpectedAttributeSubjectRulesTokenDiagnostic(
1198 unsigned DiagID, AttributeList &Attribute,
1199 MissingAttributeSubjectRulesRecoveryPoint Point, Parser &PRef) {
1200 SourceLocation Loc = PRef.getEndOfPreviousToken();
1201 if (Loc.isInvalid())
1202 Loc = PRef.getCurToken().getLocation();
1203 auto Diagnostic = PRef.Diag(Loc, DiagID);
1205 MissingAttributeSubjectRulesRecoveryPoint EndPoint =
1206 getAttributeSubjectRulesRecoveryPointForToken(PRef.getCurToken());
1207 if (Point == MissingAttributeSubjectRulesRecoveryPoint::Comma)
1209 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::ApplyTo &&
1210 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::ApplyTo)
1211 FixIt += "apply_to";
1212 if (Point <= MissingAttributeSubjectRulesRecoveryPoint::Equals &&
1213 EndPoint > MissingAttributeSubjectRulesRecoveryPoint::Equals)
1215 SourceRange FixItRange(Loc);
1216 if (EndPoint == MissingAttributeSubjectRulesRecoveryPoint::None) {
1217 // Gather the subject match rules that are supported by the attribute.
1218 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> SubjectMatchRuleSet;
1219 Attribute.getMatchRules(PRef.getLangOpts(), SubjectMatchRuleSet);
1220 if (SubjectMatchRuleSet.empty()) {
1221 // FIXME: We can emit a "fix-it" with a subject list placeholder when
1222 // placeholders will be supported by the fix-its.
1226 bool NeedsComma = false;
1227 for (const auto &I : SubjectMatchRuleSet) {
1228 // Ensure that the missing rule is reported in the fix-it only when it's
1229 // supported in the current language mode.
1236 FixIt += attr::getSubjectMatchRuleSpelling(I.first);
1239 // Check if we need to remove the range
1240 PRef.SkipUntil(tok::eof, Parser::StopBeforeMatch);
1241 FixItRange.setEnd(PRef.getCurToken().getLocation());
1243 if (FixItRange.getBegin() == FixItRange.getEnd())
1244 Diagnostic << FixItHint::CreateInsertion(FixItRange.getBegin(), FixIt);
1246 Diagnostic << FixItHint::CreateReplacement(
1247 CharSourceRange::getCharRange(FixItRange), FixIt);
1251 } // end anonymous namespace
1253 void Parser::HandlePragmaAttribute() {
1254 assert(Tok.is(tok::annot_pragma_attribute) &&
1255 "Expected #pragma attribute annotation token");
1256 SourceLocation PragmaLoc = Tok.getLocation();
1257 auto *Info = static_cast<PragmaAttributeInfo *>(Tok.getAnnotationValue());
1258 if (Info->Action == PragmaAttributeInfo::Pop) {
1259 ConsumeAnnotationToken();
1260 Actions.ActOnPragmaAttributePop(PragmaLoc);
1263 // Parse the actual attribute with its arguments.
1264 assert(Info->Action == PragmaAttributeInfo::Push &&
1265 "Unexpected #pragma attribute command");
1266 PP.EnterTokenStream(Info->Tokens, /*DisableMacroExpansion=*/false);
1267 ConsumeAnnotationToken();
1269 ParsedAttributes &Attrs = Info->Attributes;
1270 Attrs.clearListOnly();
1272 auto SkipToEnd = [this]() {
1273 SkipUntil(tok::eof, StopBeforeMatch);
1277 if (Tok.is(tok::l_square) && NextToken().is(tok::l_square)) {
1278 // Parse the CXX11 style attribute.
1279 ParseCXX11AttributeSpecifier(Attrs);
1280 } else if (Tok.is(tok::kw___attribute)) {
1282 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1285 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "("))
1288 if (Tok.isNot(tok::identifier)) {
1289 Diag(Tok, diag::err_pragma_attribute_expected_attribute_name);
1293 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1294 SourceLocation AttrNameLoc = ConsumeToken();
1296 if (Tok.isNot(tok::l_paren))
1297 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
1298 AttributeList::AS_GNU);
1300 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, /*EndLoc=*/nullptr,
1301 /*ScopeName=*/nullptr,
1302 /*ScopeLoc=*/SourceLocation(),
1303 AttributeList::AS_GNU,
1304 /*Declarator=*/nullptr);
1306 if (ExpectAndConsume(tok::r_paren))
1308 if (ExpectAndConsume(tok::r_paren))
1310 } else if (Tok.is(tok::kw___declspec)) {
1311 ParseMicrosoftDeclSpecs(Attrs);
1313 Diag(Tok, diag::err_pragma_attribute_expected_attribute_syntax);
1314 if (Tok.getIdentifierInfo()) {
1315 // If we suspect that this is an attribute suggest the use of
1317 if (AttributeList::getKind(Tok.getIdentifierInfo(), /*ScopeName=*/nullptr,
1318 AttributeList::AS_GNU) !=
1319 AttributeList::UnknownAttribute) {
1320 SourceLocation InsertStartLoc = Tok.getLocation();
1322 if (Tok.is(tok::l_paren)) {
1324 SkipUntil(tok::r_paren, StopBeforeMatch);
1325 if (Tok.isNot(tok::r_paren))
1328 Diag(Tok, diag::note_pragma_attribute_use_attribute_kw)
1329 << FixItHint::CreateInsertion(InsertStartLoc, "__attribute__((")
1330 << FixItHint::CreateInsertion(Tok.getEndLoc(), "))");
1337 if (!Attrs.getList() || Attrs.getList()->isInvalid()) {
1342 // Ensure that we don't have more than one attribute.
1343 if (Attrs.getList()->getNext()) {
1344 SourceLocation Loc = Attrs.getList()->getNext()->getLoc();
1345 Diag(Loc, diag::err_pragma_attribute_multiple_attributes);
1350 if (!Attrs.getList()->isSupportedByPragmaAttribute()) {
1351 Diag(PragmaLoc, diag::err_pragma_attribute_unsupported_attribute)
1352 << Attrs.getList()->getName();
1356 AttributeList &Attribute = *Attrs.getList();
1358 // Parse the subject-list.
1359 if (!TryConsumeToken(tok::comma)) {
1360 createExpectedAttributeSubjectRulesTokenDiagnostic(
1361 diag::err_expected, Attribute,
1362 MissingAttributeSubjectRulesRecoveryPoint::Comma, *this)
1368 if (Tok.isNot(tok::identifier)) {
1369 createExpectedAttributeSubjectRulesTokenDiagnostic(
1370 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1371 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1375 const IdentifierInfo *II = Tok.getIdentifierInfo();
1376 if (!II->isStr("apply_to")) {
1377 createExpectedAttributeSubjectRulesTokenDiagnostic(
1378 diag::err_pragma_attribute_invalid_subject_set_specifier, Attribute,
1379 MissingAttributeSubjectRulesRecoveryPoint::ApplyTo, *this);
1385 if (!TryConsumeToken(tok::equal)) {
1386 createExpectedAttributeSubjectRulesTokenDiagnostic(
1387 diag::err_expected, Attribute,
1388 MissingAttributeSubjectRulesRecoveryPoint::Equals, *this)
1394 attr::ParsedSubjectMatchRuleSet SubjectMatchRules;
1395 SourceLocation AnyLoc, LastMatchRuleEndLoc;
1396 if (ParsePragmaAttributeSubjectMatchRuleSet(SubjectMatchRules, AnyLoc,
1397 LastMatchRuleEndLoc)) {
1402 // Tokens following an ill-formed attribute will remain in the token stream
1403 // and must be removed.
1404 if (Tok.isNot(tok::eof)) {
1405 Diag(Tok, diag::err_pragma_attribute_extra_tokens_after_attribute);
1410 // Consume the eof terminator token.
1413 Actions.ActOnPragmaAttributePush(Attribute, PragmaLoc,
1414 std::move(SubjectMatchRules));
1417 // #pragma GCC visibility comes in two variants:
1418 // 'push' '(' [visibility] ')'
1420 void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
1421 PragmaIntroducerKind Introducer,
1423 SourceLocation VisLoc = VisTok.getLocation();
1426 PP.LexUnexpandedToken(Tok);
1428 const IdentifierInfo *PushPop = Tok.getIdentifierInfo();
1430 const IdentifierInfo *VisType;
1431 if (PushPop && PushPop->isStr("pop")) {
1433 } else if (PushPop && PushPop->isStr("push")) {
1434 PP.LexUnexpandedToken(Tok);
1435 if (Tok.isNot(tok::l_paren)) {
1436 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
1440 PP.LexUnexpandedToken(Tok);
1441 VisType = Tok.getIdentifierInfo();
1443 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1447 PP.LexUnexpandedToken(Tok);
1448 if (Tok.isNot(tok::r_paren)) {
1449 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
1454 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1458 SourceLocation EndLoc = Tok.getLocation();
1459 PP.LexUnexpandedToken(Tok);
1460 if (Tok.isNot(tok::eod)) {
1461 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1466 auto Toks = llvm::make_unique<Token[]>(1);
1467 Toks[0].startToken();
1468 Toks[0].setKind(tok::annot_pragma_vis);
1469 Toks[0].setLocation(VisLoc);
1470 Toks[0].setAnnotationEndLoc(EndLoc);
1471 Toks[0].setAnnotationValue(
1472 const_cast<void*>(static_cast<const void*>(VisType)));
1473 PP.EnterTokenStream(std::move(Toks), 1, /*DisableMacroExpansion=*/true);
1476 // #pragma pack(...) comes in the following delicious flavors:
1477 // pack '(' [integer] ')'
1478 // pack '(' 'show' ')'
1479 // pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
1480 void PragmaPackHandler::HandlePragma(Preprocessor &PP,
1481 PragmaIntroducerKind Introducer,
1483 SourceLocation PackLoc = PackTok.getLocation();
1487 if (Tok.isNot(tok::l_paren)) {
1488 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "pack";
1492 Sema::PragmaMsStackAction Action = Sema::PSK_Reset;
1493 StringRef SlotLabel;
1495 Alignment.startToken();
1497 if (Tok.is(tok::numeric_constant)) {
1502 // In MSVC/gcc, #pragma pack(4) sets the alignment without affecting
1503 // the push/pop stack.
1504 // In Apple gcc, #pragma pack(4) is equivalent to #pragma pack(push, 4)
1506 PP.getLangOpts().ApplePragmaPack ? Sema::PSK_Push_Set : Sema::PSK_Set;
1507 } else if (Tok.is(tok::identifier)) {
1508 const IdentifierInfo *II = Tok.getIdentifierInfo();
1509 if (II->isStr("show")) {
1510 Action = Sema::PSK_Show;
1513 if (II->isStr("push")) {
1514 Action = Sema::PSK_Push;
1515 } else if (II->isStr("pop")) {
1516 Action = Sema::PSK_Pop;
1518 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action) << "pack";
1523 if (Tok.is(tok::comma)) {
1526 if (Tok.is(tok::numeric_constant)) {
1527 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1531 } else if (Tok.is(tok::identifier)) {
1532 SlotLabel = Tok.getIdentifierInfo()->getName();
1535 if (Tok.is(tok::comma)) {
1538 if (Tok.isNot(tok::numeric_constant)) {
1539 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1543 Action = (Sema::PragmaMsStackAction)(Action | Sema::PSK_Set);
1549 PP.Diag(Tok.getLocation(), diag::warn_pragma_pack_malformed);
1554 } else if (PP.getLangOpts().ApplePragmaPack) {
1555 // In MSVC/gcc, #pragma pack() resets the alignment without affecting
1556 // the push/pop stack.
1557 // In Apple gcc #pragma pack() is equivalent to #pragma pack(pop).
1558 Action = Sema::PSK_Pop;
1561 if (Tok.isNot(tok::r_paren)) {
1562 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen) << "pack";
1566 SourceLocation RParenLoc = Tok.getLocation();
1568 if (Tok.isNot(tok::eod)) {
1569 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "pack";
1573 PragmaPackInfo *Info =
1574 PP.getPreprocessorAllocator().Allocate<PragmaPackInfo>(1);
1575 Info->Action = Action;
1576 Info->SlotLabel = SlotLabel;
1577 Info->Alignment = Alignment;
1579 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1581 Toks[0].startToken();
1582 Toks[0].setKind(tok::annot_pragma_pack);
1583 Toks[0].setLocation(PackLoc);
1584 Toks[0].setAnnotationEndLoc(RParenLoc);
1585 Toks[0].setAnnotationValue(static_cast<void*>(Info));
1586 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1589 // #pragma ms_struct on
1590 // #pragma ms_struct off
1591 void PragmaMSStructHandler::HandlePragma(Preprocessor &PP,
1592 PragmaIntroducerKind Introducer,
1593 Token &MSStructTok) {
1594 PragmaMSStructKind Kind = PMSST_OFF;
1598 if (Tok.isNot(tok::identifier)) {
1599 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1602 SourceLocation EndLoc = Tok.getLocation();
1603 const IdentifierInfo *II = Tok.getIdentifierInfo();
1604 if (II->isStr("on")) {
1608 else if (II->isStr("off") || II->isStr("reset"))
1611 PP.Diag(Tok.getLocation(), diag::warn_pragma_ms_struct);
1615 if (Tok.isNot(tok::eod)) {
1616 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1621 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1623 Toks[0].startToken();
1624 Toks[0].setKind(tok::annot_pragma_msstruct);
1625 Toks[0].setLocation(MSStructTok.getLocation());
1626 Toks[0].setAnnotationEndLoc(EndLoc);
1627 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1628 static_cast<uintptr_t>(Kind)));
1629 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1632 // #pragma clang section bss="abc" data="" rodata="def" text=""
1633 void PragmaClangSectionHandler::HandlePragma(Preprocessor &PP,
1634 PragmaIntroducerKind Introducer, Token &FirstToken) {
1637 auto SecKind = Sema::PragmaClangSectionKind::PCSK_Invalid;
1639 PP.Lex(Tok); // eat 'section'
1640 while (Tok.isNot(tok::eod)) {
1641 if (Tok.isNot(tok::identifier)) {
1642 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1646 const IdentifierInfo *SecType = Tok.getIdentifierInfo();
1647 if (SecType->isStr("bss"))
1648 SecKind = Sema::PragmaClangSectionKind::PCSK_BSS;
1649 else if (SecType->isStr("data"))
1650 SecKind = Sema::PragmaClangSectionKind::PCSK_Data;
1651 else if (SecType->isStr("rodata"))
1652 SecKind = Sema::PragmaClangSectionKind::PCSK_Rodata;
1653 else if (SecType->isStr("text"))
1654 SecKind = Sema::PragmaClangSectionKind::PCSK_Text;
1656 PP.Diag(Tok.getLocation(), diag::err_pragma_expected_clang_section_name) << "clang section";
1660 PP.Lex(Tok); // eat ['bss'|'data'|'rodata'|'text']
1661 if (Tok.isNot(tok::equal)) {
1662 PP.Diag(Tok.getLocation(), diag::err_pragma_clang_section_expected_equal) << SecKind;
1666 std::string SecName;
1667 if (!PP.LexStringLiteral(Tok, SecName, "pragma clang section", false))
1670 Actions.ActOnPragmaClangSection(Tok.getLocation(),
1671 (SecName.size()? Sema::PragmaClangSectionAction::PCSA_Set :
1672 Sema::PragmaClangSectionAction::PCSA_Clear),
1677 // #pragma 'align' '=' {'native','natural','mac68k','power','reset'}
1678 // #pragma 'options 'align' '=' {'native','natural','mac68k','power','reset'}
1679 static void ParseAlignPragma(Preprocessor &PP, Token &FirstTok,
1685 if (Tok.isNot(tok::identifier) ||
1686 !Tok.getIdentifierInfo()->isStr("align")) {
1687 PP.Diag(Tok.getLocation(), diag::warn_pragma_options_expected_align);
1693 if (Tok.isNot(tok::equal)) {
1694 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_expected_equal)
1700 if (Tok.isNot(tok::identifier)) {
1701 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1702 << (IsOptions ? "options" : "align");
1706 Sema::PragmaOptionsAlignKind Kind = Sema::POAK_Natural;
1707 const IdentifierInfo *II = Tok.getIdentifierInfo();
1708 if (II->isStr("native"))
1709 Kind = Sema::POAK_Native;
1710 else if (II->isStr("natural"))
1711 Kind = Sema::POAK_Natural;
1712 else if (II->isStr("packed"))
1713 Kind = Sema::POAK_Packed;
1714 else if (II->isStr("power"))
1715 Kind = Sema::POAK_Power;
1716 else if (II->isStr("mac68k"))
1717 Kind = Sema::POAK_Mac68k;
1718 else if (II->isStr("reset"))
1719 Kind = Sema::POAK_Reset;
1721 PP.Diag(Tok.getLocation(), diag::warn_pragma_align_invalid_option)
1726 SourceLocation EndLoc = Tok.getLocation();
1728 if (Tok.isNot(tok::eod)) {
1729 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
1730 << (IsOptions ? "options" : "align");
1734 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1736 Toks[0].startToken();
1737 Toks[0].setKind(tok::annot_pragma_align);
1738 Toks[0].setLocation(FirstTok.getLocation());
1739 Toks[0].setAnnotationEndLoc(EndLoc);
1740 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1741 static_cast<uintptr_t>(Kind)));
1742 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1745 void PragmaAlignHandler::HandlePragma(Preprocessor &PP,
1746 PragmaIntroducerKind Introducer,
1748 ParseAlignPragma(PP, AlignTok, /*IsOptions=*/false);
1751 void PragmaOptionsHandler::HandlePragma(Preprocessor &PP,
1752 PragmaIntroducerKind Introducer,
1753 Token &OptionsTok) {
1754 ParseAlignPragma(PP, OptionsTok, /*IsOptions=*/true);
1757 // #pragma unused(identifier)
1758 void PragmaUnusedHandler::HandlePragma(Preprocessor &PP,
1759 PragmaIntroducerKind Introducer,
1761 // FIXME: Should we be expanding macros here? My guess is no.
1762 SourceLocation UnusedLoc = UnusedTok.getLocation();
1764 // Lex the left '('.
1767 if (Tok.isNot(tok::l_paren)) {
1768 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen) << "unused";
1772 // Lex the declaration reference(s).
1773 SmallVector<Token, 5> Identifiers;
1774 SourceLocation RParenLoc;
1781 if (Tok.is(tok::identifier)) {
1782 Identifiers.push_back(Tok);
1788 PP.Diag(Tok.getLocation(), diag::warn_pragma_unused_expected_var);
1792 // We are execting a ')' or a ','.
1793 if (Tok.is(tok::comma)) {
1798 if (Tok.is(tok::r_paren)) {
1799 RParenLoc = Tok.getLocation();
1804 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_punc) << "unused";
1809 if (Tok.isNot(tok::eod)) {
1810 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1815 // Verify that we have a location for the right parenthesis.
1816 assert(RParenLoc.isValid() && "Valid '#pragma unused' must have ')'");
1817 assert(!Identifiers.empty() && "Valid '#pragma unused' must have arguments");
1819 // For each identifier token, insert into the token stream a
1820 // annot_pragma_unused token followed by the identifier token.
1821 // This allows us to cache a "#pragma unused" that occurs inside an inline
1822 // C++ member function.
1824 MutableArrayRef<Token> Toks(
1825 PP.getPreprocessorAllocator().Allocate<Token>(2 * Identifiers.size()),
1826 2 * Identifiers.size());
1827 for (unsigned i=0; i != Identifiers.size(); i++) {
1828 Token &pragmaUnusedTok = Toks[2*i], &idTok = Toks[2*i+1];
1829 pragmaUnusedTok.startToken();
1830 pragmaUnusedTok.setKind(tok::annot_pragma_unused);
1831 pragmaUnusedTok.setLocation(UnusedLoc);
1832 idTok = Identifiers[i];
1834 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1837 // #pragma weak identifier
1838 // #pragma weak identifier '=' identifier
1839 void PragmaWeakHandler::HandlePragma(Preprocessor &PP,
1840 PragmaIntroducerKind Introducer,
1842 SourceLocation WeakLoc = WeakTok.getLocation();
1846 if (Tok.isNot(tok::identifier)) {
1847 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) << "weak";
1851 Token WeakName = Tok;
1852 bool HasAlias = false;
1856 if (Tok.is(tok::equal)) {
1859 if (Tok.isNot(tok::identifier)) {
1860 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1868 if (Tok.isNot(tok::eod)) {
1869 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) << "weak";
1874 MutableArrayRef<Token> Toks(
1875 PP.getPreprocessorAllocator().Allocate<Token>(3), 3);
1876 Token &pragmaUnusedTok = Toks[0];
1877 pragmaUnusedTok.startToken();
1878 pragmaUnusedTok.setKind(tok::annot_pragma_weakalias);
1879 pragmaUnusedTok.setLocation(WeakLoc);
1880 pragmaUnusedTok.setAnnotationEndLoc(AliasName.getLocation());
1882 Toks[2] = AliasName;
1883 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1885 MutableArrayRef<Token> Toks(
1886 PP.getPreprocessorAllocator().Allocate<Token>(2), 2);
1887 Token &pragmaUnusedTok = Toks[0];
1888 pragmaUnusedTok.startToken();
1889 pragmaUnusedTok.setKind(tok::annot_pragma_weak);
1890 pragmaUnusedTok.setLocation(WeakLoc);
1891 pragmaUnusedTok.setAnnotationEndLoc(WeakLoc);
1893 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1897 // #pragma redefine_extname identifier identifier
1898 void PragmaRedefineExtnameHandler::HandlePragma(Preprocessor &PP,
1899 PragmaIntroducerKind Introducer,
1900 Token &RedefToken) {
1901 SourceLocation RedefLoc = RedefToken.getLocation();
1905 if (Tok.isNot(tok::identifier)) {
1906 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1911 Token RedefName = Tok;
1914 if (Tok.isNot(tok::identifier)) {
1915 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
1916 << "redefine_extname";
1920 Token AliasName = Tok;
1923 if (Tok.isNot(tok::eod)) {
1924 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
1929 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(3),
1931 Token &pragmaRedefTok = Toks[0];
1932 pragmaRedefTok.startToken();
1933 pragmaRedefTok.setKind(tok::annot_pragma_redefine_extname);
1934 pragmaRedefTok.setLocation(RedefLoc);
1935 pragmaRedefTok.setAnnotationEndLoc(AliasName.getLocation());
1936 Toks[1] = RedefName;
1937 Toks[2] = AliasName;
1938 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1943 PragmaFPContractHandler::HandlePragma(Preprocessor &PP,
1944 PragmaIntroducerKind Introducer,
1946 tok::OnOffSwitch OOS;
1947 if (PP.LexOnOffSwitch(OOS))
1950 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
1952 Toks[0].startToken();
1953 Toks[0].setKind(tok::annot_pragma_fp_contract);
1954 Toks[0].setLocation(Tok.getLocation());
1955 Toks[0].setAnnotationEndLoc(Tok.getLocation());
1956 Toks[0].setAnnotationValue(reinterpret_cast<void*>(
1957 static_cast<uintptr_t>(OOS)));
1958 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
1962 PragmaOpenCLExtensionHandler::HandlePragma(Preprocessor &PP,
1963 PragmaIntroducerKind Introducer,
1965 PP.LexUnexpandedToken(Tok);
1966 if (Tok.isNot(tok::identifier)) {
1967 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier) <<
1971 IdentifierInfo *Ext = Tok.getIdentifierInfo();
1972 SourceLocation NameLoc = Tok.getLocation();
1975 if (Tok.isNot(tok::colon)) {
1976 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_colon) << Ext;
1981 if (Tok.isNot(tok::identifier)) {
1982 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate) << 0;
1985 IdentifierInfo *Pred = Tok.getIdentifierInfo();
1987 OpenCLExtState State;
1988 if (Pred->isStr("enable")) {
1990 } else if (Pred->isStr("disable")) {
1992 } else if (Pred->isStr("begin"))
1994 else if (Pred->isStr("end"))
1997 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_predicate)
1998 << Ext->isStr("all");
2001 SourceLocation StateLoc = Tok.getLocation();
2004 if (Tok.isNot(tok::eod)) {
2005 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol) <<
2010 auto Info = PP.getPreprocessorAllocator().Allocate<OpenCLExtData>(1);
2012 Info->second = State;
2013 MutableArrayRef<Token> Toks(PP.getPreprocessorAllocator().Allocate<Token>(1),
2015 Toks[0].startToken();
2016 Toks[0].setKind(tok::annot_pragma_opencl_extension);
2017 Toks[0].setLocation(NameLoc);
2018 Toks[0].setAnnotationValue(static_cast<void*>(Info));
2019 Toks[0].setAnnotationEndLoc(StateLoc);
2020 PP.EnterTokenStream(Toks, /*DisableMacroExpansion=*/true);
2022 if (PP.getPPCallbacks())
2023 PP.getPPCallbacks()->PragmaOpenCLExtension(NameLoc, Ext,
2027 /// \brief Handle '#pragma omp ...' when OpenMP is disabled.
2030 PragmaNoOpenMPHandler::HandlePragma(Preprocessor &PP,
2031 PragmaIntroducerKind Introducer,
2033 if (!PP.getDiagnostics().isIgnored(diag::warn_pragma_omp_ignored,
2034 FirstTok.getLocation())) {
2035 PP.Diag(FirstTok, diag::warn_pragma_omp_ignored);
2036 PP.getDiagnostics().setSeverity(diag::warn_pragma_omp_ignored,
2037 diag::Severity::Ignored, SourceLocation());
2039 PP.DiscardUntilEndOfDirective();
2042 /// \brief Handle '#pragma omp ...' when OpenMP is enabled.
2045 PragmaOpenMPHandler::HandlePragma(Preprocessor &PP,
2046 PragmaIntroducerKind Introducer,
2048 SmallVector<Token, 16> Pragma;
2051 Tok.setKind(tok::annot_pragma_openmp);
2052 Tok.setLocation(FirstTok.getLocation());
2054 while (Tok.isNot(tok::eod)) {
2055 Pragma.push_back(Tok);
2058 SourceLocation EodLoc = Tok.getLocation();
2060 Tok.setKind(tok::annot_pragma_openmp_end);
2061 Tok.setLocation(EodLoc);
2062 Pragma.push_back(Tok);
2064 auto Toks = llvm::make_unique<Token[]>(Pragma.size());
2065 std::copy(Pragma.begin(), Pragma.end(), Toks.get());
2066 PP.EnterTokenStream(std::move(Toks), Pragma.size(),
2067 /*DisableMacroExpansion=*/false);
2070 /// \brief Handle '#pragma pointers_to_members'
2071 // The grammar for this pragma is as follows:
2073 // <inheritance model> ::= ('single' | 'multiple' | 'virtual') '_inheritance'
2075 // #pragma pointers_to_members '(' 'best_case' ')'
2076 // #pragma pointers_to_members '(' 'full_generality' [',' inheritance-model] ')'
2077 // #pragma pointers_to_members '(' inheritance-model ')'
2078 void PragmaMSPointersToMembers::HandlePragma(Preprocessor &PP,
2079 PragmaIntroducerKind Introducer,
2081 SourceLocation PointersToMembersLoc = Tok.getLocation();
2083 if (Tok.isNot(tok::l_paren)) {
2084 PP.Diag(PointersToMembersLoc, diag::warn_pragma_expected_lparen)
2085 << "pointers_to_members";
2089 const IdentifierInfo *Arg = Tok.getIdentifierInfo();
2091 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_identifier)
2092 << "pointers_to_members";
2097 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod;
2098 if (Arg->isStr("best_case")) {
2099 RepresentationMethod = LangOptions::PPTMK_BestCase;
2101 if (Arg->isStr("full_generality")) {
2102 if (Tok.is(tok::comma)) {
2105 Arg = Tok.getIdentifierInfo();
2107 PP.Diag(Tok.getLocation(),
2108 diag::err_pragma_pointers_to_members_unknown_kind)
2109 << Tok.getKind() << /*OnlyInheritanceModels*/ 0;
2113 } else if (Tok.is(tok::r_paren)) {
2114 // #pragma pointers_to_members(full_generality) implicitly specifies
2115 // virtual_inheritance.
2117 RepresentationMethod = LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2119 PP.Diag(Tok.getLocation(), diag::err_expected_punc)
2120 << "full_generality";
2126 if (Arg->isStr("single_inheritance")) {
2127 RepresentationMethod =
2128 LangOptions::PPTMK_FullGeneralitySingleInheritance;
2129 } else if (Arg->isStr("multiple_inheritance")) {
2130 RepresentationMethod =
2131 LangOptions::PPTMK_FullGeneralityMultipleInheritance;
2132 } else if (Arg->isStr("virtual_inheritance")) {
2133 RepresentationMethod =
2134 LangOptions::PPTMK_FullGeneralityVirtualInheritance;
2136 PP.Diag(Tok.getLocation(),
2137 diag::err_pragma_pointers_to_members_unknown_kind)
2138 << Arg << /*HasPointerDeclaration*/ 1;
2144 if (Tok.isNot(tok::r_paren)) {
2145 PP.Diag(Tok.getLocation(), diag::err_expected_rparen_after)
2146 << (Arg ? Arg->getName() : "full_generality");
2150 SourceLocation EndLoc = Tok.getLocation();
2152 if (Tok.isNot(tok::eod)) {
2153 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2154 << "pointers_to_members";
2159 AnnotTok.startToken();
2160 AnnotTok.setKind(tok::annot_pragma_ms_pointers_to_members);
2161 AnnotTok.setLocation(PointersToMembersLoc);
2162 AnnotTok.setAnnotationEndLoc(EndLoc);
2163 AnnotTok.setAnnotationValue(
2164 reinterpret_cast<void *>(static_cast<uintptr_t>(RepresentationMethod)));
2165 PP.EnterToken(AnnotTok);
2168 /// \brief Handle '#pragma vtordisp'
2169 // The grammar for this pragma is as follows:
2171 // <vtordisp-mode> ::= ('off' | 'on' | '0' | '1' | '2' )
2173 // #pragma vtordisp '(' ['push' ','] vtordisp-mode ')'
2174 // #pragma vtordisp '(' 'pop' ')'
2175 // #pragma vtordisp '(' ')'
2176 void PragmaMSVtorDisp::HandlePragma(Preprocessor &PP,
2177 PragmaIntroducerKind Introducer,
2179 SourceLocation VtorDispLoc = Tok.getLocation();
2181 if (Tok.isNot(tok::l_paren)) {
2182 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_lparen) << "vtordisp";
2187 Sema::PragmaMsStackAction Action = Sema::PSK_Set;
2188 const IdentifierInfo *II = Tok.getIdentifierInfo();
2190 if (II->isStr("push")) {
2191 // #pragma vtordisp(push, mode)
2193 if (Tok.isNot(tok::comma)) {
2194 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_punc) << "vtordisp";
2198 Action = Sema::PSK_Push_Set;
2199 // not push, could be on/off
2200 } else if (II->isStr("pop")) {
2201 // #pragma vtordisp(pop)
2203 Action = Sema::PSK_Pop;
2205 // not push or pop, could be on/off
2207 if (Tok.is(tok::r_paren)) {
2208 // #pragma vtordisp()
2209 Action = Sema::PSK_Reset;
2215 if (Action & Sema::PSK_Push || Action & Sema::PSK_Set) {
2216 const IdentifierInfo *II = Tok.getIdentifierInfo();
2217 if (II && II->isStr("off")) {
2220 } else if (II && II->isStr("on")) {
2223 } else if (Tok.is(tok::numeric_constant) &&
2224 PP.parseSimpleIntegerLiteral(Tok, Value)) {
2226 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_integer)
2227 << 0 << 2 << "vtordisp";
2231 PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_action)
2237 // Finish the pragma: ')' $
2238 if (Tok.isNot(tok::r_paren)) {
2239 PP.Diag(VtorDispLoc, diag::warn_pragma_expected_rparen) << "vtordisp";
2242 SourceLocation EndLoc = Tok.getLocation();
2244 if (Tok.isNot(tok::eod)) {
2245 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2250 // Enter the annotation.
2252 AnnotTok.startToken();
2253 AnnotTok.setKind(tok::annot_pragma_ms_vtordisp);
2254 AnnotTok.setLocation(VtorDispLoc);
2255 AnnotTok.setAnnotationEndLoc(EndLoc);
2256 AnnotTok.setAnnotationValue(reinterpret_cast<void *>(
2257 static_cast<uintptr_t>((Action << 16) | (Value & 0xFFFF))));
2258 PP.EnterToken(AnnotTok);
2261 /// \brief Handle all MS pragmas. Simply forwards the tokens after inserting
2262 /// an annotation token.
2263 void PragmaMSPragma::HandlePragma(Preprocessor &PP,
2264 PragmaIntroducerKind Introducer,
2266 Token EoF, AnnotTok;
2268 EoF.setKind(tok::eof);
2269 AnnotTok.startToken();
2270 AnnotTok.setKind(tok::annot_pragma_ms_pragma);
2271 AnnotTok.setLocation(Tok.getLocation());
2272 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2273 SmallVector<Token, 8> TokenVector;
2274 // Suck up all of the tokens before the eod.
2275 for (; Tok.isNot(tok::eod); PP.Lex(Tok)) {
2276 TokenVector.push_back(Tok);
2277 AnnotTok.setAnnotationEndLoc(Tok.getLocation());
2279 // Add a sentinal EoF token to the end of the list.
2280 TokenVector.push_back(EoF);
2281 // We must allocate this array with new because EnterTokenStream is going to
2283 auto TokenArray = llvm::make_unique<Token[]>(TokenVector.size());
2284 std::copy(TokenVector.begin(), TokenVector.end(), TokenArray.get());
2285 auto Value = new (PP.getPreprocessorAllocator())
2286 std::pair<std::unique_ptr<Token[]>, size_t>(std::move(TokenArray),
2287 TokenVector.size());
2288 AnnotTok.setAnnotationValue(Value);
2289 PP.EnterToken(AnnotTok);
2292 /// \brief Handle the Microsoft \#pragma detect_mismatch extension.
2296 /// #pragma detect_mismatch("name", "value")
2298 /// Where 'name' and 'value' are quoted strings. The values are embedded in
2299 /// the object file and passed along to the linker. If the linker detects a
2300 /// mismatch in the object file's values for the given name, a LNK2038 error
2301 /// is emitted. See MSDN for more details.
2302 void PragmaDetectMismatchHandler::HandlePragma(Preprocessor &PP,
2303 PragmaIntroducerKind Introducer,
2305 SourceLocation DetectMismatchLoc = Tok.getLocation();
2307 if (Tok.isNot(tok::l_paren)) {
2308 PP.Diag(DetectMismatchLoc, diag::err_expected) << tok::l_paren;
2312 // Read the name to embed, which must be a string literal.
2313 std::string NameString;
2314 if (!PP.LexStringLiteral(Tok, NameString,
2315 "pragma detect_mismatch",
2316 /*MacroExpansion=*/true))
2319 // Read the comma followed by a second string literal.
2320 std::string ValueString;
2321 if (Tok.isNot(tok::comma)) {
2322 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2326 if (!PP.LexStringLiteral(Tok, ValueString, "pragma detect_mismatch",
2327 /*MacroExpansion=*/true))
2330 if (Tok.isNot(tok::r_paren)) {
2331 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2334 PP.Lex(Tok); // Eat the r_paren.
2336 if (Tok.isNot(tok::eod)) {
2337 PP.Diag(Tok.getLocation(), diag::err_pragma_detect_mismatch_malformed);
2341 // If the pragma is lexically sound, notify any interested PPCallbacks.
2342 if (PP.getPPCallbacks())
2343 PP.getPPCallbacks()->PragmaDetectMismatch(DetectMismatchLoc, NameString,
2346 Actions.ActOnPragmaDetectMismatch(DetectMismatchLoc, NameString, ValueString);
2349 /// \brief Handle the microsoft \#pragma comment extension.
2353 /// #pragma comment(linker, "foo")
2355 /// 'linker' is one of five identifiers: compiler, exestr, lib, linker, user.
2356 /// "foo" is a string, which is fully macro expanded, and permits string
2357 /// concatenation, embedded escape characters etc. See MSDN for more details.
2358 void PragmaCommentHandler::HandlePragma(Preprocessor &PP,
2359 PragmaIntroducerKind Introducer,
2361 SourceLocation CommentLoc = Tok.getLocation();
2363 if (Tok.isNot(tok::l_paren)) {
2364 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2368 // Read the identifier.
2370 if (Tok.isNot(tok::identifier)) {
2371 PP.Diag(CommentLoc, diag::err_pragma_comment_malformed);
2375 // Verify that this is one of the 5 whitelisted options.
2376 IdentifierInfo *II = Tok.getIdentifierInfo();
2377 PragmaMSCommentKind Kind =
2378 llvm::StringSwitch<PragmaMSCommentKind>(II->getName())
2379 .Case("linker", PCK_Linker)
2380 .Case("lib", PCK_Lib)
2381 .Case("compiler", PCK_Compiler)
2382 .Case("exestr", PCK_ExeStr)
2383 .Case("user", PCK_User)
2384 .Default(PCK_Unknown);
2385 if (Kind == PCK_Unknown) {
2386 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_unknown_kind);
2390 // On PS4, issue a warning about any pragma comments other than
2391 // #pragma comment lib.
2392 if (PP.getTargetInfo().getTriple().isPS4() && Kind != PCK_Lib) {
2393 PP.Diag(Tok.getLocation(), diag::warn_pragma_comment_ignored)
2398 // Read the optional string if present.
2400 std::string ArgumentString;
2401 if (Tok.is(tok::comma) && !PP.LexStringLiteral(Tok, ArgumentString,
2403 /*MacroExpansion=*/true))
2406 // FIXME: warn that 'exestr' is deprecated.
2407 // FIXME: If the kind is "compiler" warn if the string is present (it is
2409 // The MSDN docs say that "lib" and "linker" require a string and have a short
2410 // whitelist of linker options they support, but in practice MSVC doesn't
2411 // issue a diagnostic. Therefore neither does clang.
2413 if (Tok.isNot(tok::r_paren)) {
2414 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2417 PP.Lex(Tok); // eat the r_paren.
2419 if (Tok.isNot(tok::eod)) {
2420 PP.Diag(Tok.getLocation(), diag::err_pragma_comment_malformed);
2424 // If the pragma is lexically sound, notify any interested PPCallbacks.
2425 if (PP.getPPCallbacks())
2426 PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
2428 Actions.ActOnPragmaMSComment(CommentLoc, Kind, ArgumentString);
2431 // #pragma clang optimize off
2432 // #pragma clang optimize on
2433 void PragmaOptimizeHandler::HandlePragma(Preprocessor &PP,
2434 PragmaIntroducerKind Introducer,
2435 Token &FirstToken) {
2438 if (Tok.is(tok::eod)) {
2439 PP.Diag(Tok.getLocation(), diag::err_pragma_missing_argument)
2440 << "clang optimize" << /*Expected=*/true << "'on' or 'off'";
2443 if (Tok.isNot(tok::identifier)) {
2444 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2445 << PP.getSpelling(Tok);
2448 const IdentifierInfo *II = Tok.getIdentifierInfo();
2449 // The only accepted values are 'on' or 'off'.
2451 if (II->isStr("on")) {
2453 } else if (!II->isStr("off")) {
2454 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_invalid_argument)
2455 << PP.getSpelling(Tok);
2460 if (Tok.isNot(tok::eod)) {
2461 PP.Diag(Tok.getLocation(), diag::err_pragma_optimize_extra_argument)
2462 << PP.getSpelling(Tok);
2466 Actions.ActOnPragmaOptimize(IsOn, FirstToken.getLocation());
2470 /// Used as the annotation value for tok::annot_pragma_fp.
2471 struct TokFPAnnotValue {
2472 enum FlagKinds { Contract };
2473 enum FlagValues { On, Off, Fast };
2476 FlagValues FlagValue;
2478 } // end anonymous namespace
2480 void PragmaFPHandler::HandlePragma(Preprocessor &PP,
2481 PragmaIntroducerKind Introducer,
2484 Token PragmaName = Tok;
2485 SmallVector<Token, 1> TokenList;
2488 if (Tok.isNot(tok::identifier)) {
2489 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2490 << /*MissingOption=*/true << "";
2494 while (Tok.is(tok::identifier)) {
2495 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2498 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagKinds>>(
2499 OptionInfo->getName())
2500 .Case("contract", TokFPAnnotValue::Contract)
2503 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_option)
2504 << /*MissingOption=*/false << OptionInfo;
2510 if (Tok.isNot(tok::l_paren)) {
2511 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2516 if (Tok.isNot(tok::identifier)) {
2517 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2518 << PP.getSpelling(Tok) << OptionInfo->getName();
2521 const IdentifierInfo *II = Tok.getIdentifierInfo();
2524 llvm::StringSwitch<llvm::Optional<TokFPAnnotValue::FlagValues>>(
2526 .Case("on", TokFPAnnotValue::On)
2527 .Case("off", TokFPAnnotValue::Off)
2528 .Case("fast", TokFPAnnotValue::Fast)
2529 .Default(llvm::None);
2532 PP.Diag(Tok.getLocation(), diag::err_pragma_fp_invalid_argument)
2533 << PP.getSpelling(Tok) << OptionInfo->getName();
2539 if (Tok.isNot(tok::r_paren)) {
2540 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2545 auto *AnnotValue = new (PP.getPreprocessorAllocator())
2546 TokFPAnnotValue{*FlagKind, *FlagValue};
2547 // Generate the loop hint token.
2550 FPTok.setKind(tok::annot_pragma_fp);
2551 FPTok.setLocation(PragmaName.getLocation());
2552 FPTok.setAnnotationEndLoc(PragmaName.getLocation());
2553 FPTok.setAnnotationValue(reinterpret_cast<void *>(AnnotValue));
2554 TokenList.push_back(FPTok);
2557 if (Tok.isNot(tok::eod)) {
2558 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2563 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2564 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2566 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2567 /*DisableMacroExpansion=*/false);
2570 void Parser::HandlePragmaFP() {
2571 assert(Tok.is(tok::annot_pragma_fp));
2573 reinterpret_cast<TokFPAnnotValue *>(Tok.getAnnotationValue());
2575 LangOptions::FPContractModeKind FPC;
2576 switch (AnnotValue->FlagValue) {
2577 case TokFPAnnotValue::On:
2578 FPC = LangOptions::FPC_On;
2580 case TokFPAnnotValue::Fast:
2581 FPC = LangOptions::FPC_Fast;
2583 case TokFPAnnotValue::Off:
2584 FPC = LangOptions::FPC_Off;
2588 Actions.ActOnPragmaFPContract(FPC);
2589 ConsumeAnnotationToken();
2592 /// \brief Parses loop or unroll pragma hint value and fills in Info.
2593 static bool ParseLoopHintValue(Preprocessor &PP, Token &Tok, Token PragmaName,
2594 Token Option, bool ValueInParens,
2595 PragmaLoopHintInfo &Info) {
2596 SmallVector<Token, 1> ValueList;
2597 int OpenParens = ValueInParens ? 1 : 0;
2598 // Read constant expression.
2599 while (Tok.isNot(tok::eod)) {
2600 if (Tok.is(tok::l_paren))
2602 else if (Tok.is(tok::r_paren)) {
2604 if (OpenParens == 0 && ValueInParens)
2608 ValueList.push_back(Tok);
2612 if (ValueInParens) {
2614 if (Tok.isNot(tok::r_paren)) {
2615 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2622 EOFTok.startToken();
2623 EOFTok.setKind(tok::eof);
2624 EOFTok.setLocation(Tok.getLocation());
2625 ValueList.push_back(EOFTok); // Terminates expression for parsing.
2627 Info.Toks = llvm::makeArrayRef(ValueList).copy(PP.getPreprocessorAllocator());
2629 Info.PragmaName = PragmaName;
2630 Info.Option = Option;
2634 /// \brief Handle the \#pragma clang loop directive.
2635 /// #pragma clang 'loop' loop-hints
2638 /// loop-hint loop-hints[opt]
2641 /// 'vectorize' '(' loop-hint-keyword ')'
2642 /// 'interleave' '(' loop-hint-keyword ')'
2643 /// 'unroll' '(' unroll-hint-keyword ')'
2644 /// 'vectorize_width' '(' loop-hint-value ')'
2645 /// 'interleave_count' '(' loop-hint-value ')'
2646 /// 'unroll_count' '(' loop-hint-value ')'
2648 /// loop-hint-keyword:
2653 /// unroll-hint-keyword:
2658 /// loop-hint-value:
2659 /// constant-expression
2661 /// Specifying vectorize(enable) or vectorize_width(_value_) instructs llvm to
2662 /// try vectorizing the instructions of the loop it precedes. Specifying
2663 /// interleave(enable) or interleave_count(_value_) instructs llvm to try
2664 /// interleaving multiple iterations of the loop it precedes. The width of the
2665 /// vector instructions is specified by vectorize_width() and the number of
2666 /// interleaved loop iterations is specified by interleave_count(). Specifying a
2667 /// value of 1 effectively disables vectorization/interleaving, even if it is
2668 /// possible and profitable, and 0 is invalid. The loop vectorizer currently
2669 /// only works on inner loops.
2671 /// The unroll and unroll_count directives control the concatenation
2672 /// unroller. Specifying unroll(enable) instructs llvm to unroll the loop
2673 /// completely if the trip count is known at compile time and unroll partially
2674 /// if the trip count is not known. Specifying unroll(full) is similar to
2675 /// unroll(enable) but will unroll the loop only if the trip count is known at
2676 /// compile time. Specifying unroll(disable) disables unrolling for the
2677 /// loop. Specifying unroll_count(_value_) instructs llvm to try to unroll the
2678 /// loop the number of times indicated by the value.
2679 void PragmaLoopHintHandler::HandlePragma(Preprocessor &PP,
2680 PragmaIntroducerKind Introducer,
2682 // Incoming token is "loop" from "#pragma clang loop".
2683 Token PragmaName = Tok;
2684 SmallVector<Token, 1> TokenList;
2686 // Lex the optimization option and verify it is an identifier.
2688 if (Tok.isNot(tok::identifier)) {
2689 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2690 << /*MissingOption=*/true << "";
2694 while (Tok.is(tok::identifier)) {
2696 IdentifierInfo *OptionInfo = Tok.getIdentifierInfo();
2698 bool OptionValid = llvm::StringSwitch<bool>(OptionInfo->getName())
2699 .Case("vectorize", true)
2700 .Case("interleave", true)
2701 .Case("unroll", true)
2702 .Case("distribute", true)
2703 .Case("vectorize_width", true)
2704 .Case("interleave_count", true)
2705 .Case("unroll_count", true)
2708 PP.Diag(Tok.getLocation(), diag::err_pragma_loop_invalid_option)
2709 << /*MissingOption=*/false << OptionInfo;
2715 if (Tok.isNot(tok::l_paren)) {
2716 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2721 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2722 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, /*ValueInParens=*/true,
2726 // Generate the loop hint token.
2728 LoopHintTok.startToken();
2729 LoopHintTok.setKind(tok::annot_pragma_loop_hint);
2730 LoopHintTok.setLocation(PragmaName.getLocation());
2731 LoopHintTok.setAnnotationEndLoc(PragmaName.getLocation());
2732 LoopHintTok.setAnnotationValue(static_cast<void *>(Info));
2733 TokenList.push_back(LoopHintTok);
2736 if (Tok.isNot(tok::eod)) {
2737 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2742 auto TokenArray = llvm::make_unique<Token[]>(TokenList.size());
2743 std::copy(TokenList.begin(), TokenList.end(), TokenArray.get());
2745 PP.EnterTokenStream(std::move(TokenArray), TokenList.size(),
2746 /*DisableMacroExpansion=*/false);
2749 /// \brief Handle the loop unroll optimization pragmas.
2751 /// #pragma unroll unroll-hint-value
2752 /// #pragma unroll '(' unroll-hint-value ')'
2753 /// #pragma nounroll
2755 /// unroll-hint-value:
2756 /// constant-expression
2758 /// Loop unrolling hints can be specified with '#pragma unroll' or
2759 /// '#pragma nounroll'. '#pragma unroll' can take a numeric argument optionally
2760 /// contained in parentheses. With no argument the directive instructs llvm to
2761 /// try to unroll the loop completely. A positive integer argument can be
2762 /// specified to indicate the number of times the loop should be unrolled. To
2763 /// maximize compatibility with other compilers the unroll count argument can be
2764 /// specified with or without parentheses. Specifying, '#pragma nounroll'
2765 /// disables unrolling of the loop.
2766 void PragmaUnrollHintHandler::HandlePragma(Preprocessor &PP,
2767 PragmaIntroducerKind Introducer,
2769 // Incoming token is "unroll" for "#pragma unroll", or "nounroll" for
2770 // "#pragma nounroll".
2771 Token PragmaName = Tok;
2773 auto *Info = new (PP.getPreprocessorAllocator()) PragmaLoopHintInfo;
2774 if (Tok.is(tok::eod)) {
2775 // nounroll or unroll pragma without an argument.
2776 Info->PragmaName = PragmaName;
2777 Info->Option.startToken();
2778 } else if (PragmaName.getIdentifierInfo()->getName() == "nounroll") {
2779 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2783 // Unroll pragma with an argument: "#pragma unroll N" or
2784 // "#pragma unroll(N)".
2785 // Read '(' if it exists.
2786 bool ValueInParens = Tok.is(tok::l_paren);
2791 Option.startToken();
2792 if (ParseLoopHintValue(PP, Tok, PragmaName, Option, ValueInParens, *Info))
2795 // In CUDA, the argument to '#pragma unroll' should not be contained in
2797 if (PP.getLangOpts().CUDA && ValueInParens)
2798 PP.Diag(Info->Toks[0].getLocation(),
2799 diag::warn_pragma_unroll_cuda_value_in_parens);
2801 if (Tok.isNot(tok::eod)) {
2802 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2808 // Generate the hint token.
2809 auto TokenArray = llvm::make_unique<Token[]>(1);
2810 TokenArray[0].startToken();
2811 TokenArray[0].setKind(tok::annot_pragma_loop_hint);
2812 TokenArray[0].setLocation(PragmaName.getLocation());
2813 TokenArray[0].setAnnotationEndLoc(PragmaName.getLocation());
2814 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2815 PP.EnterTokenStream(std::move(TokenArray), 1,
2816 /*DisableMacroExpansion=*/false);
2819 /// \brief Handle the Microsoft \#pragma intrinsic extension.
2823 /// #pragma intrinsic(memset)
2824 /// #pragma intrinsic(strlen, memcpy)
2827 /// Pragma intrisic tells the compiler to use a builtin version of the
2828 /// function. Clang does it anyway, so the pragma doesn't really do anything.
2829 /// Anyway, we emit a warning if the function specified in \#pragma intrinsic
2830 /// isn't an intrinsic in clang and suggest to include intrin.h.
2831 void PragmaMSIntrinsicHandler::HandlePragma(Preprocessor &PP,
2832 PragmaIntroducerKind Introducer,
2836 if (Tok.isNot(tok::l_paren)) {
2837 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
2843 bool SuggestIntrinH = !PP.isMacroDefined("__INTRIN_H");
2845 while (Tok.is(tok::identifier)) {
2846 IdentifierInfo *II = Tok.getIdentifierInfo();
2847 if (!II->getBuiltinID())
2848 PP.Diag(Tok.getLocation(), diag::warn_pragma_intrinsic_builtin)
2849 << II << SuggestIntrinH;
2852 if (Tok.isNot(tok::comma))
2857 if (Tok.isNot(tok::r_paren)) {
2858 PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
2864 if (Tok.isNot(tok::eod))
2865 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2868 void PragmaForceCUDAHostDeviceHandler::HandlePragma(
2869 Preprocessor &PP, PragmaIntroducerKind Introducer, Token &Tok) {
2870 Token FirstTok = Tok;
2873 IdentifierInfo *Info = Tok.getIdentifierInfo();
2874 if (!Info || (!Info->isStr("begin") && !Info->isStr("end"))) {
2875 PP.Diag(FirstTok.getLocation(),
2876 diag::warn_pragma_force_cuda_host_device_bad_arg);
2880 if (Info->isStr("begin"))
2881 Actions.PushForceCUDAHostDevice();
2882 else if (!Actions.PopForceCUDAHostDevice())
2883 PP.Diag(FirstTok.getLocation(),
2884 diag::err_pragma_cannot_end_force_cuda_host_device);
2887 if (!Tok.is(tok::eod))
2888 PP.Diag(FirstTok.getLocation(),
2889 diag::warn_pragma_force_cuda_host_device_bad_arg);
2892 /// \brief Handle the #pragma clang attribute directive.
2896 /// #pragma clang attribute push(attribute, subject-set)
2897 /// #pragma clang attribute pop
2900 /// The subject-set clause defines the set of declarations which receive the
2901 /// attribute. Its exact syntax is described in the LanguageExtensions document
2902 /// in Clang's documentation.
2904 /// This directive instructs the compiler to begin/finish applying the specified
2905 /// attribute to the set of attribute-specific declarations in the active range
2907 void PragmaAttributeHandler::HandlePragma(Preprocessor &PP,
2908 PragmaIntroducerKind Introducer,
2909 Token &FirstToken) {
2912 auto *Info = new (PP.getPreprocessorAllocator())
2913 PragmaAttributeInfo(AttributesForPragmaAttribute);
2915 // Parse the 'push' or 'pop'.
2916 if (Tok.isNot(tok::identifier)) {
2917 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_push_pop);
2920 const auto *II = Tok.getIdentifierInfo();
2921 if (II->isStr("push"))
2922 Info->Action = PragmaAttributeInfo::Push;
2923 else if (II->isStr("pop"))
2924 Info->Action = PragmaAttributeInfo::Pop;
2926 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_invalid_argument)
2927 << PP.getSpelling(Tok);
2932 // Parse the actual attribute.
2933 if (Info->Action == PragmaAttributeInfo::Push) {
2934 if (Tok.isNot(tok::l_paren)) {
2935 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::l_paren;
2940 // Lex the attribute tokens.
2941 SmallVector<Token, 16> AttributeTokens;
2943 while (Tok.isNot(tok::eod)) {
2944 if (Tok.is(tok::l_paren))
2946 else if (Tok.is(tok::r_paren)) {
2948 if (OpenParens == 0)
2952 AttributeTokens.push_back(Tok);
2956 if (AttributeTokens.empty()) {
2957 PP.Diag(Tok.getLocation(), diag::err_pragma_attribute_expected_attribute);
2960 if (Tok.isNot(tok::r_paren)) {
2961 PP.Diag(Tok.getLocation(), diag::err_expected) << tok::r_paren;
2964 SourceLocation EndLoc = Tok.getLocation();
2967 // Terminate the attribute for parsing.
2969 EOFTok.startToken();
2970 EOFTok.setKind(tok::eof);
2971 EOFTok.setLocation(EndLoc);
2972 AttributeTokens.push_back(EOFTok);
2975 llvm::makeArrayRef(AttributeTokens).copy(PP.getPreprocessorAllocator());
2978 if (Tok.isNot(tok::eod))
2979 PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
2980 << "clang attribute";
2982 // Generate the annotated pragma token.
2983 auto TokenArray = llvm::make_unique<Token[]>(1);
2984 TokenArray[0].startToken();
2985 TokenArray[0].setKind(tok::annot_pragma_attribute);
2986 TokenArray[0].setLocation(FirstToken.getLocation());
2987 TokenArray[0].setAnnotationEndLoc(FirstToken.getLocation());
2988 TokenArray[0].setAnnotationValue(static_cast<void *>(Info));
2989 PP.EnterTokenStream(std::move(TokenArray), 1,
2990 /*DisableMacroExpansion=*/false);