1 //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements semantic analysis for non-trivial attributes and 10 // pragmas. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/ASTConsumer.h" 15 #include "clang/AST/Attr.h" 16 #include "clang/AST/Expr.h" 17 #include "clang/Basic/TargetInfo.h" 18 #include "clang/Lex/Preprocessor.h" 19 #include "clang/Sema/Lookup.h" 20 #include "clang/Sema/SemaInternal.h" 21 using namespace clang; 22 23 //===----------------------------------------------------------------------===// 24 // Pragma 'pack' and 'options align' 25 //===----------------------------------------------------------------------===// 26 27 Sema::PragmaStackSentinelRAII::PragmaStackSentinelRAII(Sema &S, 28 StringRef SlotLabel, 29 bool ShouldAct) 30 : S(S), SlotLabel(SlotLabel), ShouldAct(ShouldAct) { 31 if (ShouldAct) { 32 S.VtorDispStack.SentinelAction(PSK_Push, SlotLabel); 33 S.DataSegStack.SentinelAction(PSK_Push, SlotLabel); 34 S.BSSSegStack.SentinelAction(PSK_Push, SlotLabel); 35 S.ConstSegStack.SentinelAction(PSK_Push, SlotLabel); 36 S.CodeSegStack.SentinelAction(PSK_Push, SlotLabel); 37 } 38 } 39 40 Sema::PragmaStackSentinelRAII::~PragmaStackSentinelRAII() { 41 if (ShouldAct) { 42 S.VtorDispStack.SentinelAction(PSK_Pop, SlotLabel); 43 S.DataSegStack.SentinelAction(PSK_Pop, SlotLabel); 44 S.BSSSegStack.SentinelAction(PSK_Pop, SlotLabel); 45 S.ConstSegStack.SentinelAction(PSK_Pop, SlotLabel); 46 S.CodeSegStack.SentinelAction(PSK_Pop, SlotLabel); 47 } 48 } 49 50 void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) { 51 AlignPackInfo InfoVal = AlignPackStack.CurrentValue; 52 AlignPackInfo::Mode M = InfoVal.getAlignMode(); 53 bool IsPackSet = InfoVal.IsPackSet(); 54 bool IsXLPragma = getLangOpts().XLPragmaPack; 55 56 // If we are not under mac68k/natural alignment mode and also there is no pack 57 // value, we don't need any attributes. 58 if (!IsPackSet && M != AlignPackInfo::Mac68k && M != AlignPackInfo::Natural) 59 return; 60 61 if (M == AlignPackInfo::Mac68k && (IsXLPragma || InfoVal.IsAlignAttr())) { 62 RD->addAttr(AlignMac68kAttr::CreateImplicit(Context)); 63 } else if (IsPackSet) { 64 // Check to see if we need a max field alignment attribute. 65 RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit( 66 Context, InfoVal.getPackNumber() * 8)); 67 } 68 69 if (IsXLPragma && M == AlignPackInfo::Natural) 70 RD->addAttr(AlignNaturalAttr::CreateImplicit(Context)); 71 72 if (AlignPackIncludeStack.empty()) 73 return; 74 // The #pragma align/pack affected a record in an included file, so Clang 75 // should warn when that pragma was written in a file that included the 76 // included file. 77 for (auto &AlignPackedInclude : llvm::reverse(AlignPackIncludeStack)) { 78 if (AlignPackedInclude.CurrentPragmaLocation != 79 AlignPackStack.CurrentPragmaLocation) 80 break; 81 if (AlignPackedInclude.HasNonDefaultValue) 82 AlignPackedInclude.ShouldWarnOnInclude = true; 83 } 84 } 85 86 void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) { 87 if (MSStructPragmaOn) 88 RD->addAttr(MSStructAttr::CreateImplicit(Context)); 89 90 // FIXME: We should merge AddAlignmentAttributesForRecord with 91 // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes 92 // all active pragmas and applies them as attributes to class definitions. 93 if (VtorDispStack.CurrentValue != getLangOpts().getVtorDispMode()) 94 RD->addAttr(MSVtorDispAttr::CreateImplicit( 95 Context, unsigned(VtorDispStack.CurrentValue))); 96 } 97 98 template <typename Attribute> 99 static void addGslOwnerPointerAttributeIfNotExisting(ASTContext &Context, 100 CXXRecordDecl *Record) { 101 if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>()) 102 return; 103 104 for (Decl *Redecl : Record->redecls()) 105 Redecl->addAttr(Attribute::CreateImplicit(Context, /*DerefType=*/nullptr)); 106 } 107 108 void Sema::inferGslPointerAttribute(NamedDecl *ND, 109 CXXRecordDecl *UnderlyingRecord) { 110 if (!UnderlyingRecord) 111 return; 112 113 const auto *Parent = dyn_cast<CXXRecordDecl>(ND->getDeclContext()); 114 if (!Parent) 115 return; 116 117 static llvm::StringSet<> Containers{ 118 "array", 119 "basic_string", 120 "deque", 121 "forward_list", 122 "vector", 123 "list", 124 "map", 125 "multiset", 126 "multimap", 127 "priority_queue", 128 "queue", 129 "set", 130 "stack", 131 "unordered_set", 132 "unordered_map", 133 "unordered_multiset", 134 "unordered_multimap", 135 }; 136 137 static llvm::StringSet<> Iterators{"iterator", "const_iterator", 138 "reverse_iterator", 139 "const_reverse_iterator"}; 140 141 if (Parent->isInStdNamespace() && Iterators.count(ND->getName()) && 142 Containers.count(Parent->getName())) 143 addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context, 144 UnderlyingRecord); 145 } 146 147 void Sema::inferGslPointerAttribute(TypedefNameDecl *TD) { 148 149 QualType Canonical = TD->getUnderlyingType().getCanonicalType(); 150 151 CXXRecordDecl *RD = Canonical->getAsCXXRecordDecl(); 152 if (!RD) { 153 if (auto *TST = 154 dyn_cast<TemplateSpecializationType>(Canonical.getTypePtr())) { 155 156 RD = dyn_cast_or_null<CXXRecordDecl>( 157 TST->getTemplateName().getAsTemplateDecl()->getTemplatedDecl()); 158 } 159 } 160 161 inferGslPointerAttribute(TD, RD); 162 } 163 164 void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) { 165 static llvm::StringSet<> StdOwners{ 166 "any", 167 "array", 168 "basic_regex", 169 "basic_string", 170 "deque", 171 "forward_list", 172 "vector", 173 "list", 174 "map", 175 "multiset", 176 "multimap", 177 "optional", 178 "priority_queue", 179 "queue", 180 "set", 181 "stack", 182 "unique_ptr", 183 "unordered_set", 184 "unordered_map", 185 "unordered_multiset", 186 "unordered_multimap", 187 "variant", 188 }; 189 static llvm::StringSet<> StdPointers{ 190 "basic_string_view", 191 "reference_wrapper", 192 "regex_iterator", 193 }; 194 195 if (!Record->getIdentifier()) 196 return; 197 198 // Handle classes that directly appear in std namespace. 199 if (Record->isInStdNamespace()) { 200 if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>()) 201 return; 202 203 if (StdOwners.count(Record->getName())) 204 addGslOwnerPointerAttributeIfNotExisting<OwnerAttr>(Context, Record); 205 else if (StdPointers.count(Record->getName())) 206 addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context, Record); 207 208 return; 209 } 210 211 // Handle nested classes that could be a gsl::Pointer. 212 inferGslPointerAttribute(Record, Record); 213 } 214 215 void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, 216 SourceLocation PragmaLoc) { 217 PragmaMsStackAction Action = Sema::PSK_Reset; 218 AlignPackInfo::Mode ModeVal = AlignPackInfo::Native; 219 220 switch (Kind) { 221 // For most of the platforms we support, native and natural are the same. 222 // With XL, native is the same as power, natural means something else. 223 // 224 // FIXME: This is not true on Darwin/PPC. 225 case POAK_Native: 226 case POAK_Power: 227 Action = Sema::PSK_Push_Set; 228 break; 229 case POAK_Natural: 230 Action = Sema::PSK_Push_Set; 231 ModeVal = AlignPackInfo::Natural; 232 break; 233 234 // Note that '#pragma options align=packed' is not equivalent to attribute 235 // packed, it has a different precedence relative to attribute aligned. 236 case POAK_Packed: 237 Action = Sema::PSK_Push_Set; 238 ModeVal = AlignPackInfo::Packed; 239 break; 240 241 case POAK_Mac68k: 242 // Check if the target supports this. 243 if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) { 244 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported); 245 return; 246 } 247 Action = Sema::PSK_Push_Set; 248 ModeVal = AlignPackInfo::Mac68k; 249 break; 250 case POAK_Reset: 251 // Reset just pops the top of the stack, or resets the current alignment to 252 // default. 253 Action = Sema::PSK_Pop; 254 if (AlignPackStack.Stack.empty()) { 255 if (AlignPackStack.CurrentValue.getAlignMode() != AlignPackInfo::Native || 256 AlignPackStack.CurrentValue.IsPackAttr()) { 257 Action = Sema::PSK_Reset; 258 } else { 259 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed) 260 << "stack empty"; 261 return; 262 } 263 } 264 break; 265 } 266 267 AlignPackInfo Info(ModeVal, getLangOpts().XLPragmaPack); 268 269 AlignPackStack.Act(PragmaLoc, Action, StringRef(), Info); 270 } 271 272 void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, 273 PragmaClangSectionAction Action, 274 PragmaClangSectionKind SecKind, 275 StringRef SecName) { 276 PragmaClangSection *CSec; 277 int SectionFlags = ASTContext::PSF_Read; 278 switch (SecKind) { 279 case PragmaClangSectionKind::PCSK_BSS: 280 CSec = &PragmaClangBSSSection; 281 SectionFlags |= ASTContext::PSF_Write | ASTContext::PSF_ZeroInit; 282 break; 283 case PragmaClangSectionKind::PCSK_Data: 284 CSec = &PragmaClangDataSection; 285 SectionFlags |= ASTContext::PSF_Write; 286 break; 287 case PragmaClangSectionKind::PCSK_Rodata: 288 CSec = &PragmaClangRodataSection; 289 break; 290 case PragmaClangSectionKind::PCSK_Relro: 291 CSec = &PragmaClangRelroSection; 292 break; 293 case PragmaClangSectionKind::PCSK_Text: 294 CSec = &PragmaClangTextSection; 295 SectionFlags |= ASTContext::PSF_Execute; 296 break; 297 default: 298 llvm_unreachable("invalid clang section kind"); 299 } 300 301 if (Action == PragmaClangSectionAction::PCSA_Clear) { 302 CSec->Valid = false; 303 return; 304 } 305 306 if (llvm::Error E = isValidSectionSpecifier(SecName)) { 307 Diag(PragmaLoc, diag::err_pragma_section_invalid_for_target) 308 << toString(std::move(E)); 309 CSec->Valid = false; 310 return; 311 } 312 313 if (UnifySection(SecName, SectionFlags, PragmaLoc)) 314 return; 315 316 CSec->Valid = true; 317 CSec->SectionName = std::string(SecName); 318 CSec->PragmaLocation = PragmaLoc; 319 } 320 321 void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action, 322 StringRef SlotLabel, Expr *alignment) { 323 bool IsXLPragma = getLangOpts().XLPragmaPack; 324 // XL pragma pack does not support identifier syntax. 325 if (IsXLPragma && !SlotLabel.empty()) { 326 Diag(PragmaLoc, diag::err_pragma_pack_identifer_not_supported); 327 return; 328 } 329 330 const AlignPackInfo CurVal = AlignPackStack.CurrentValue; 331 Expr *Alignment = static_cast<Expr *>(alignment); 332 333 // If specified then alignment must be a "small" power of two. 334 unsigned AlignmentVal = 0; 335 AlignPackInfo::Mode ModeVal = CurVal.getAlignMode(); 336 337 if (Alignment) { 338 Optional<llvm::APSInt> Val; 339 Val = Alignment->getIntegerConstantExpr(Context); 340 341 // pack(0) is like pack(), which just works out since that is what 342 // we use 0 for in PackAttr. 343 if (Alignment->isTypeDependent() || !Val || 344 !(*Val == 0 || Val->isPowerOf2()) || Val->getZExtValue() > 16) { 345 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment); 346 return; // Ignore 347 } 348 349 if (IsXLPragma && *Val == 0) { 350 // pack(0) does not work out with XL. 351 Diag(PragmaLoc, diag::err_pragma_pack_invalid_alignment); 352 return; // Ignore 353 } 354 355 AlignmentVal = (unsigned)Val->getZExtValue(); 356 } 357 358 if (Action == Sema::PSK_Show) { 359 // Show the current alignment, making sure to show the right value 360 // for the default. 361 // FIXME: This should come from the target. 362 AlignmentVal = CurVal.IsPackSet() ? CurVal.getPackNumber() : 8; 363 if (ModeVal == AlignPackInfo::Mac68k && 364 (IsXLPragma || CurVal.IsAlignAttr())) 365 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k"; 366 else 367 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal; 368 } 369 370 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack: 371 // "#pragma pack(pop, identifier, n) is undefined" 372 if (Action & Sema::PSK_Pop) { 373 if (Alignment && !SlotLabel.empty()) 374 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment); 375 if (AlignPackStack.Stack.empty()) { 376 assert(CurVal.getAlignMode() == AlignPackInfo::Native && 377 "Empty pack stack can only be at Native alignment mode."); 378 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty"; 379 } 380 } 381 382 AlignPackInfo Info(ModeVal, AlignmentVal, IsXLPragma); 383 384 AlignPackStack.Act(PragmaLoc, Action, SlotLabel, Info); 385 } 386 387 bool Sema::ConstantFoldAttrArgs(const AttributeCommonInfo &CI, 388 MutableArrayRef<Expr *> Args) { 389 llvm::SmallVector<PartialDiagnosticAt, 8> Notes; 390 for (unsigned Idx = 0; Idx < Args.size(); Idx++) { 391 Expr *&E = Args.begin()[Idx]; 392 assert(E && "error are handled before"); 393 if (E->isValueDependent() || E->isTypeDependent()) 394 continue; 395 396 // FIXME: Use DefaultFunctionArrayLValueConversion() in place of the logic 397 // that adds implicit casts here. 398 if (E->getType()->isArrayType()) 399 E = ImpCastExprToType(E, Context.getPointerType(E->getType()), 400 clang::CK_ArrayToPointerDecay) 401 .get(); 402 if (E->getType()->isFunctionType()) 403 E = ImplicitCastExpr::Create(Context, 404 Context.getPointerType(E->getType()), 405 clang::CK_FunctionToPointerDecay, E, nullptr, 406 VK_PRValue, FPOptionsOverride()); 407 if (E->isLValue()) 408 E = ImplicitCastExpr::Create(Context, E->getType().getNonReferenceType(), 409 clang::CK_LValueToRValue, E, nullptr, 410 VK_PRValue, FPOptionsOverride()); 411 412 Expr::EvalResult Eval; 413 Notes.clear(); 414 Eval.Diag = &Notes; 415 416 bool Result = E->EvaluateAsConstantExpr(Eval, Context); 417 418 /// Result means the expression can be folded to a constant. 419 /// Note.empty() means the expression is a valid constant expression in the 420 /// current language mode. 421 if (!Result || !Notes.empty()) { 422 Diag(E->getBeginLoc(), diag::err_attribute_argument_n_type) 423 << CI << (Idx + 1) << AANT_ArgumentConstantExpr; 424 for (auto &Note : Notes) 425 Diag(Note.first, Note.second); 426 return false; 427 } 428 assert(Eval.Val.hasValue()); 429 E = ConstantExpr::Create(Context, E, Eval.Val); 430 } 431 432 return true; 433 } 434 435 void Sema::DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind, 436 SourceLocation IncludeLoc) { 437 if (Kind == PragmaAlignPackDiagnoseKind::NonDefaultStateAtInclude) { 438 SourceLocation PrevLocation = AlignPackStack.CurrentPragmaLocation; 439 // Warn about non-default alignment at #includes (without redundant 440 // warnings for the same directive in nested includes). 441 // The warning is delayed until the end of the file to avoid warnings 442 // for files that don't have any records that are affected by the modified 443 // alignment. 444 bool HasNonDefaultValue = 445 AlignPackStack.hasValue() && 446 (AlignPackIncludeStack.empty() || 447 AlignPackIncludeStack.back().CurrentPragmaLocation != PrevLocation); 448 AlignPackIncludeStack.push_back( 449 {AlignPackStack.CurrentValue, 450 AlignPackStack.hasValue() ? PrevLocation : SourceLocation(), 451 HasNonDefaultValue, /*ShouldWarnOnInclude*/ false}); 452 return; 453 } 454 455 assert(Kind == PragmaAlignPackDiagnoseKind::ChangedStateAtExit && 456 "invalid kind"); 457 AlignPackIncludeState PrevAlignPackState = 458 AlignPackIncludeStack.pop_back_val(); 459 // FIXME: AlignPackStack may contain both #pragma align and #pragma pack 460 // information, diagnostics below might not be accurate if we have mixed 461 // pragmas. 462 if (PrevAlignPackState.ShouldWarnOnInclude) { 463 // Emit the delayed non-default alignment at #include warning. 464 Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include); 465 Diag(PrevAlignPackState.CurrentPragmaLocation, diag::note_pragma_pack_here); 466 } 467 // Warn about modified alignment after #includes. 468 if (PrevAlignPackState.CurrentValue != AlignPackStack.CurrentValue) { 469 Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include); 470 Diag(AlignPackStack.CurrentPragmaLocation, diag::note_pragma_pack_here); 471 } 472 } 473 474 void Sema::DiagnoseUnterminatedPragmaAlignPack() { 475 if (AlignPackStack.Stack.empty()) 476 return; 477 bool IsInnermost = true; 478 479 // FIXME: AlignPackStack may contain both #pragma align and #pragma pack 480 // information, diagnostics below might not be accurate if we have mixed 481 // pragmas. 482 for (const auto &StackSlot : llvm::reverse(AlignPackStack.Stack)) { 483 Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof); 484 // The user might have already reset the alignment, so suggest replacing 485 // the reset with a pop. 486 if (IsInnermost && 487 AlignPackStack.CurrentValue == AlignPackStack.DefaultValue) { 488 auto DB = Diag(AlignPackStack.CurrentPragmaLocation, 489 diag::note_pragma_pack_pop_instead_reset); 490 SourceLocation FixItLoc = 491 Lexer::findLocationAfterToken(AlignPackStack.CurrentPragmaLocation, 492 tok::l_paren, SourceMgr, LangOpts, 493 /*SkipTrailing=*/false); 494 if (FixItLoc.isValid()) 495 DB << FixItHint::CreateInsertion(FixItLoc, "pop"); 496 } 497 IsInnermost = false; 498 } 499 } 500 501 void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) { 502 MSStructPragmaOn = (Kind == PMSST_ON); 503 } 504 505 void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc, 506 PragmaMSCommentKind Kind, StringRef Arg) { 507 auto *PCD = PragmaCommentDecl::Create( 508 Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg); 509 Context.getTranslationUnitDecl()->addDecl(PCD); 510 Consumer.HandleTopLevelDecl(DeclGroupRef(PCD)); 511 } 512 513 void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name, 514 StringRef Value) { 515 auto *PDMD = PragmaDetectMismatchDecl::Create( 516 Context, Context.getTranslationUnitDecl(), Loc, Name, Value); 517 Context.getTranslationUnitDecl()->addDecl(PDMD); 518 Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD)); 519 } 520 521 void Sema::ActOnPragmaFPEvalMethod(SourceLocation Loc, 522 LangOptions::FPEvalMethodKind Value) { 523 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); 524 switch (Value) { 525 default: 526 llvm_unreachable("invalid pragma eval_method kind"); 527 case LangOptions::FEM_Source: 528 NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Source); 529 break; 530 case LangOptions::FEM_Double: 531 NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Double); 532 break; 533 case LangOptions::FEM_Extended: 534 NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Extended); 535 break; 536 } 537 if (getLangOpts().ApproxFunc) 538 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 0; 539 if (getLangOpts().AllowFPReassoc) 540 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 1; 541 if (getLangOpts().AllowRecip) 542 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 2; 543 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures); 544 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); 545 PP.setCurrentFPEvalMethod(Loc, Value); 546 } 547 548 void Sema::ActOnPragmaFloatControl(SourceLocation Loc, 549 PragmaMsStackAction Action, 550 PragmaFloatControlKind Value) { 551 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); 552 if ((Action == PSK_Push_Set || Action == PSK_Push || Action == PSK_Pop) && 553 !CurContext->getRedeclContext()->isFileContext()) { 554 // Push and pop can only occur at file or namespace scope, or within a 555 // language linkage declaration. 556 Diag(Loc, diag::err_pragma_fc_pp_scope); 557 return; 558 } 559 switch (Value) { 560 default: 561 llvm_unreachable("invalid pragma float_control kind"); 562 case PFC_Precise: 563 NewFPFeatures.setFPPreciseEnabled(true); 564 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures); 565 if (PP.getCurrentFPEvalMethod() == 566 LangOptions::FPEvalMethodKind::FEM_Indeterminable && 567 PP.getLastFPEvalPragmaLocation().isValid()) 568 // A preceding `pragma float_control(precise,off)` has changed 569 // the value of the evaluation method. 570 // Set it back to its old value. 571 PP.setCurrentFPEvalMethod(SourceLocation(), PP.getLastFPEvalMethod()); 572 break; 573 case PFC_NoPrecise: 574 if (CurFPFeatures.getExceptionMode() == LangOptions::FPE_Strict) 575 Diag(Loc, diag::err_pragma_fc_noprecise_requires_noexcept); 576 else if (CurFPFeatures.getAllowFEnvAccess()) 577 Diag(Loc, diag::err_pragma_fc_noprecise_requires_nofenv); 578 else 579 NewFPFeatures.setFPPreciseEnabled(false); 580 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures); 581 PP.setLastFPEvalMethod(PP.getCurrentFPEvalMethod()); 582 // `AllowFPReassoc` or `AllowReciprocal` option is enabled. 583 PP.setCurrentFPEvalMethod( 584 Loc, LangOptions::FPEvalMethodKind::FEM_Indeterminable); 585 break; 586 case PFC_Except: 587 if (!isPreciseFPEnabled()) 588 Diag(Loc, diag::err_pragma_fc_except_requires_precise); 589 else 590 NewFPFeatures.setSpecifiedExceptionModeOverride(LangOptions::FPE_Strict); 591 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures); 592 break; 593 case PFC_NoExcept: 594 NewFPFeatures.setSpecifiedExceptionModeOverride(LangOptions::FPE_Ignore); 595 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures); 596 break; 597 case PFC_Push: 598 FpPragmaStack.Act(Loc, Sema::PSK_Push_Set, StringRef(), NewFPFeatures); 599 break; 600 case PFC_Pop: 601 if (FpPragmaStack.Stack.empty()) { 602 Diag(Loc, diag::warn_pragma_pop_failed) << "float_control" 603 << "stack empty"; 604 return; 605 } 606 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures); 607 NewFPFeatures = FpPragmaStack.CurrentValue; 608 if (CurFPFeatures.getAllowFPReassociate() || 609 CurFPFeatures.getAllowReciprocal()) 610 // Since we are popping the pragma, we don't want to be passing 611 // a location here. 612 PP.setCurrentFPEvalMethod(SourceLocation(), 613 CurFPFeatures.getFPEvalMethod()); 614 break; 615 } 616 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); 617 } 618 619 void Sema::ActOnPragmaMSPointersToMembers( 620 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod, 621 SourceLocation PragmaLoc) { 622 MSPointerToMemberRepresentationMethod = RepresentationMethod; 623 ImplicitMSInheritanceAttrLoc = PragmaLoc; 624 } 625 626 void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action, 627 SourceLocation PragmaLoc, 628 MSVtorDispMode Mode) { 629 if (Action & PSK_Pop && VtorDispStack.Stack.empty()) 630 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp" 631 << "stack empty"; 632 VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode); 633 } 634 635 template <> 636 void Sema::PragmaStack<Sema::AlignPackInfo>::Act(SourceLocation PragmaLocation, 637 PragmaMsStackAction Action, 638 llvm::StringRef StackSlotLabel, 639 AlignPackInfo Value) { 640 if (Action == PSK_Reset) { 641 CurrentValue = DefaultValue; 642 CurrentPragmaLocation = PragmaLocation; 643 return; 644 } 645 if (Action & PSK_Push) 646 Stack.emplace_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation, 647 PragmaLocation)); 648 else if (Action & PSK_Pop) { 649 if (!StackSlotLabel.empty()) { 650 // If we've got a label, try to find it and jump there. 651 auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) { 652 return x.StackSlotLabel == StackSlotLabel; 653 }); 654 // We found the label, so pop from there. 655 if (I != Stack.rend()) { 656 CurrentValue = I->Value; 657 CurrentPragmaLocation = I->PragmaLocation; 658 Stack.erase(std::prev(I.base()), Stack.end()); 659 } 660 } else if (Value.IsXLStack() && Value.IsAlignAttr() && 661 CurrentValue.IsPackAttr()) { 662 // XL '#pragma align(reset)' would pop the stack until 663 // a current in effect pragma align is popped. 664 auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) { 665 return x.Value.IsAlignAttr(); 666 }); 667 // If we found pragma align so pop from there. 668 if (I != Stack.rend()) { 669 Stack.erase(std::prev(I.base()), Stack.end()); 670 if (Stack.empty()) { 671 CurrentValue = DefaultValue; 672 CurrentPragmaLocation = PragmaLocation; 673 } else { 674 CurrentValue = Stack.back().Value; 675 CurrentPragmaLocation = Stack.back().PragmaLocation; 676 Stack.pop_back(); 677 } 678 } 679 } else if (!Stack.empty()) { 680 // xl '#pragma align' sets the baseline, and `#pragma pack` cannot pop 681 // over the baseline. 682 if (Value.IsXLStack() && Value.IsPackAttr() && CurrentValue.IsAlignAttr()) 683 return; 684 685 // We don't have a label, just pop the last entry. 686 CurrentValue = Stack.back().Value; 687 CurrentPragmaLocation = Stack.back().PragmaLocation; 688 Stack.pop_back(); 689 } 690 } 691 if (Action & PSK_Set) { 692 CurrentValue = Value; 693 CurrentPragmaLocation = PragmaLocation; 694 } 695 } 696 697 bool Sema::UnifySection(StringRef SectionName, int SectionFlags, 698 NamedDecl *Decl) { 699 SourceLocation PragmaLocation; 700 if (auto A = Decl->getAttr<SectionAttr>()) 701 if (A->isImplicit()) 702 PragmaLocation = A->getLocation(); 703 auto SectionIt = Context.SectionInfos.find(SectionName); 704 if (SectionIt == Context.SectionInfos.end()) { 705 Context.SectionInfos[SectionName] = 706 ASTContext::SectionInfo(Decl, PragmaLocation, SectionFlags); 707 return false; 708 } 709 // A pre-declared section takes precedence w/o diagnostic. 710 const auto &Section = SectionIt->second; 711 if (Section.SectionFlags == SectionFlags || 712 ((SectionFlags & ASTContext::PSF_Implicit) && 713 !(Section.SectionFlags & ASTContext::PSF_Implicit))) 714 return false; 715 Diag(Decl->getLocation(), diag::err_section_conflict) << Decl << Section; 716 if (Section.Decl) 717 Diag(Section.Decl->getLocation(), diag::note_declared_at) 718 << Section.Decl->getName(); 719 if (PragmaLocation.isValid()) 720 Diag(PragmaLocation, diag::note_pragma_entered_here); 721 if (Section.PragmaSectionLocation.isValid()) 722 Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here); 723 return true; 724 } 725 726 bool Sema::UnifySection(StringRef SectionName, 727 int SectionFlags, 728 SourceLocation PragmaSectionLocation) { 729 auto SectionIt = Context.SectionInfos.find(SectionName); 730 if (SectionIt != Context.SectionInfos.end()) { 731 const auto &Section = SectionIt->second; 732 if (Section.SectionFlags == SectionFlags) 733 return false; 734 if (!(Section.SectionFlags & ASTContext::PSF_Implicit)) { 735 Diag(PragmaSectionLocation, diag::err_section_conflict) 736 << "this" << Section; 737 if (Section.Decl) 738 Diag(Section.Decl->getLocation(), diag::note_declared_at) 739 << Section.Decl->getName(); 740 if (Section.PragmaSectionLocation.isValid()) 741 Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here); 742 return true; 743 } 744 } 745 Context.SectionInfos[SectionName] = 746 ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags); 747 return false; 748 } 749 750 /// Called on well formed \#pragma bss_seg(). 751 void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation, 752 PragmaMsStackAction Action, 753 llvm::StringRef StackSlotLabel, 754 StringLiteral *SegmentName, 755 llvm::StringRef PragmaName) { 756 PragmaStack<StringLiteral *> *Stack = 757 llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName) 758 .Case("data_seg", &DataSegStack) 759 .Case("bss_seg", &BSSSegStack) 760 .Case("const_seg", &ConstSegStack) 761 .Case("code_seg", &CodeSegStack); 762 if (Action & PSK_Pop && Stack->Stack.empty()) 763 Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName 764 << "stack empty"; 765 if (SegmentName) { 766 if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString())) 767 return; 768 769 if (SegmentName->getString() == ".drectve" && 770 Context.getTargetInfo().getCXXABI().isMicrosoft()) 771 Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName; 772 } 773 774 Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName); 775 } 776 777 /// Called on well formed \#pragma bss_seg(). 778 void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation, 779 int SectionFlags, StringLiteral *SegmentName) { 780 UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation); 781 } 782 783 void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation, 784 StringLiteral *SegmentName) { 785 // There's no stack to maintain, so we just have a current section. When we 786 // see the default section, reset our current section back to null so we stop 787 // tacking on unnecessary attributes. 788 CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName; 789 CurInitSegLoc = PragmaLocation; 790 } 791 792 void Sema::ActOnPragmaMSAllocText( 793 SourceLocation PragmaLocation, StringRef Section, 794 const SmallVector<std::tuple<IdentifierInfo *, SourceLocation>> 795 &Functions) { 796 if (!CurContext->getRedeclContext()->isFileContext()) { 797 Diag(PragmaLocation, diag::err_pragma_expected_file_scope) << "alloc_text"; 798 return; 799 } 800 801 for (auto &Function : Functions) { 802 IdentifierInfo *II; 803 SourceLocation Loc; 804 std::tie(II, Loc) = Function; 805 806 DeclarationName DN(II); 807 NamedDecl *ND = LookupSingleName(TUScope, DN, Loc, LookupOrdinaryName); 808 if (!ND) { 809 Diag(Loc, diag::err_undeclared_use) << II->getName(); 810 return; 811 } 812 813 DeclContext *DC = ND->getDeclContext(); 814 if (getLangOpts().CPlusPlus && !DC->isExternCContext()) { 815 Diag(Loc, diag::err_pragma_alloc_text_c_linkage); 816 return; 817 } 818 819 FunctionToSectionMap[II->getName()] = std::make_tuple(Section, Loc); 820 } 821 } 822 823 void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope, 824 SourceLocation PragmaLoc) { 825 826 IdentifierInfo *Name = IdTok.getIdentifierInfo(); 827 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName); 828 LookupParsedName(Lookup, curScope, nullptr, true); 829 830 if (Lookup.empty()) { 831 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var) 832 << Name << SourceRange(IdTok.getLocation()); 833 return; 834 } 835 836 VarDecl *VD = Lookup.getAsSingle<VarDecl>(); 837 if (!VD) { 838 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg) 839 << Name << SourceRange(IdTok.getLocation()); 840 return; 841 } 842 843 // Warn if this was used before being marked unused. 844 if (VD->isUsed()) 845 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name; 846 847 VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation(), 848 AttributeCommonInfo::AS_Pragma, 849 UnusedAttr::GNU_unused)); 850 } 851 852 void Sema::AddCFAuditedAttribute(Decl *D) { 853 IdentifierInfo *Ident; 854 SourceLocation Loc; 855 std::tie(Ident, Loc) = PP.getPragmaARCCFCodeAuditedInfo(); 856 if (!Loc.isValid()) return; 857 858 // Don't add a redundant or conflicting attribute. 859 if (D->hasAttr<CFAuditedTransferAttr>() || 860 D->hasAttr<CFUnknownTransferAttr>()) 861 return; 862 863 AttributeCommonInfo Info(Ident, SourceRange(Loc), 864 AttributeCommonInfo::AS_Pragma); 865 D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Info)); 866 } 867 868 namespace { 869 870 Optional<attr::SubjectMatchRule> 871 getParentAttrMatcherRule(attr::SubjectMatchRule Rule) { 872 using namespace attr; 873 switch (Rule) { 874 default: 875 return None; 876 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) 877 #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \ 878 case Value: \ 879 return Parent; 880 #include "clang/Basic/AttrSubMatchRulesList.inc" 881 } 882 } 883 884 bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) { 885 using namespace attr; 886 switch (Rule) { 887 default: 888 return false; 889 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract) 890 #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \ 891 case Value: \ 892 return IsNegated; 893 #include "clang/Basic/AttrSubMatchRulesList.inc" 894 } 895 } 896 897 CharSourceRange replacementRangeForListElement(const Sema &S, 898 SourceRange Range) { 899 // Make sure that the ',' is removed as well. 900 SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken( 901 Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(), 902 /*SkipTrailingWhitespaceAndNewLine=*/false); 903 if (AfterCommaLoc.isValid()) 904 return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc); 905 else 906 return CharSourceRange::getTokenRange(Range); 907 } 908 909 std::string 910 attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) { 911 std::string Result; 912 llvm::raw_string_ostream OS(Result); 913 for (const auto &I : llvm::enumerate(Rules)) { 914 if (I.index()) 915 OS << (I.index() == Rules.size() - 1 ? ", and " : ", "); 916 OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'"; 917 } 918 return Result; 919 } 920 921 } // end anonymous namespace 922 923 void Sema::ActOnPragmaAttributeAttribute( 924 ParsedAttr &Attribute, SourceLocation PragmaLoc, 925 attr::ParsedSubjectMatchRuleSet Rules) { 926 Attribute.setIsPragmaClangAttribute(); 927 SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules; 928 // Gather the subject match rules that are supported by the attribute. 929 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4> 930 StrictSubjectMatchRuleSet; 931 Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet); 932 933 // Figure out which subject matching rules are valid. 934 if (StrictSubjectMatchRuleSet.empty()) { 935 // Check for contradicting match rules. Contradicting match rules are 936 // either: 937 // - a top-level rule and one of its sub-rules. E.g. variable and 938 // variable(is_parameter). 939 // - a sub-rule and a sibling that's negated. E.g. 940 // variable(is_thread_local) and variable(unless(is_parameter)) 941 llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2> 942 RulesToFirstSpecifiedNegatedSubRule; 943 for (const auto &Rule : Rules) { 944 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first); 945 Optional<attr::SubjectMatchRule> ParentRule = 946 getParentAttrMatcherRule(MatchRule); 947 if (!ParentRule) 948 continue; 949 auto It = Rules.find(*ParentRule); 950 if (It != Rules.end()) { 951 // A sub-rule contradicts a parent rule. 952 Diag(Rule.second.getBegin(), 953 diag::err_pragma_attribute_matcher_subrule_contradicts_rule) 954 << attr::getSubjectMatchRuleSpelling(MatchRule) 955 << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second 956 << FixItHint::CreateRemoval( 957 replacementRangeForListElement(*this, Rule.second)); 958 // Keep going without removing this rule as it won't change the set of 959 // declarations that receive the attribute. 960 continue; 961 } 962 if (isNegatedAttrMatcherSubRule(MatchRule)) 963 RulesToFirstSpecifiedNegatedSubRule.insert( 964 std::make_pair(*ParentRule, Rule)); 965 } 966 bool IgnoreNegatedSubRules = false; 967 for (const auto &Rule : Rules) { 968 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first); 969 Optional<attr::SubjectMatchRule> ParentRule = 970 getParentAttrMatcherRule(MatchRule); 971 if (!ParentRule) 972 continue; 973 auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule); 974 if (It != RulesToFirstSpecifiedNegatedSubRule.end() && 975 It->second != Rule) { 976 // Negated sub-rule contradicts another sub-rule. 977 Diag( 978 It->second.second.getBegin(), 979 diag:: 980 err_pragma_attribute_matcher_negated_subrule_contradicts_subrule) 981 << attr::getSubjectMatchRuleSpelling( 982 attr::SubjectMatchRule(It->second.first)) 983 << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second 984 << FixItHint::CreateRemoval( 985 replacementRangeForListElement(*this, It->second.second)); 986 // Keep going but ignore all of the negated sub-rules. 987 IgnoreNegatedSubRules = true; 988 RulesToFirstSpecifiedNegatedSubRule.erase(It); 989 } 990 } 991 992 if (!IgnoreNegatedSubRules) { 993 for (const auto &Rule : Rules) 994 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first)); 995 } else { 996 for (const auto &Rule : Rules) { 997 if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first))) 998 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first)); 999 } 1000 } 1001 Rules.clear(); 1002 } else { 1003 // Each rule in Rules must be a strict subset of the attribute's 1004 // SubjectMatch rules. I.e. we're allowed to use 1005 // `apply_to=variables(is_global)` on an attrubute with SubjectList<[Var]>, 1006 // but should not allow `apply_to=variables` on an attribute which has 1007 // `SubjectList<[GlobalVar]>`. 1008 for (const auto &StrictRule : StrictSubjectMatchRuleSet) { 1009 // First, check for exact match. 1010 if (Rules.erase(StrictRule.first)) { 1011 // Add the rule to the set of attribute receivers only if it's supported 1012 // in the current language mode. 1013 if (StrictRule.second) 1014 SubjectMatchRules.push_back(StrictRule.first); 1015 } 1016 } 1017 // Check remaining rules for subset matches. 1018 auto RulesToCheck = Rules; 1019 for (const auto &Rule : RulesToCheck) { 1020 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first); 1021 if (auto ParentRule = getParentAttrMatcherRule(MatchRule)) { 1022 if (llvm::any_of(StrictSubjectMatchRuleSet, 1023 [ParentRule](const auto &StrictRule) { 1024 return StrictRule.first == *ParentRule && 1025 StrictRule.second; // IsEnabled 1026 })) { 1027 SubjectMatchRules.push_back(MatchRule); 1028 Rules.erase(MatchRule); 1029 } 1030 } 1031 } 1032 } 1033 1034 if (!Rules.empty()) { 1035 auto Diagnostic = 1036 Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers) 1037 << Attribute; 1038 SmallVector<attr::SubjectMatchRule, 2> ExtraRules; 1039 for (const auto &Rule : Rules) { 1040 ExtraRules.push_back(attr::SubjectMatchRule(Rule.first)); 1041 Diagnostic << FixItHint::CreateRemoval( 1042 replacementRangeForListElement(*this, Rule.second)); 1043 } 1044 Diagnostic << attrMatcherRuleListToString(ExtraRules); 1045 } 1046 1047 if (PragmaAttributeStack.empty()) { 1048 Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push); 1049 return; 1050 } 1051 1052 PragmaAttributeStack.back().Entries.push_back( 1053 {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false}); 1054 } 1055 1056 void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc, 1057 const IdentifierInfo *Namespace) { 1058 PragmaAttributeStack.emplace_back(); 1059 PragmaAttributeStack.back().Loc = PragmaLoc; 1060 PragmaAttributeStack.back().Namespace = Namespace; 1061 } 1062 1063 void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc, 1064 const IdentifierInfo *Namespace) { 1065 if (PragmaAttributeStack.empty()) { 1066 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1; 1067 return; 1068 } 1069 1070 // Dig back through the stack trying to find the most recently pushed group 1071 // that in Namespace. Note that this works fine if no namespace is present, 1072 // think of push/pops without namespaces as having an implicit "nullptr" 1073 // namespace. 1074 for (size_t Index = PragmaAttributeStack.size(); Index;) { 1075 --Index; 1076 if (PragmaAttributeStack[Index].Namespace == Namespace) { 1077 for (const PragmaAttributeEntry &Entry : 1078 PragmaAttributeStack[Index].Entries) { 1079 if (!Entry.IsUsed) { 1080 assert(Entry.Attribute && "Expected an attribute"); 1081 Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused) 1082 << *Entry.Attribute; 1083 Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here); 1084 } 1085 } 1086 PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index); 1087 return; 1088 } 1089 } 1090 1091 if (Namespace) 1092 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) 1093 << 0 << Namespace->getName(); 1094 else 1095 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1; 1096 } 1097 1098 void Sema::AddPragmaAttributes(Scope *S, Decl *D) { 1099 if (PragmaAttributeStack.empty()) 1100 return; 1101 for (auto &Group : PragmaAttributeStack) { 1102 for (auto &Entry : Group.Entries) { 1103 ParsedAttr *Attribute = Entry.Attribute; 1104 assert(Attribute && "Expected an attribute"); 1105 assert(Attribute->isPragmaClangAttribute() && 1106 "expected #pragma clang attribute"); 1107 1108 // Ensure that the attribute can be applied to the given declaration. 1109 bool Applies = false; 1110 for (const auto &Rule : Entry.MatchRules) { 1111 if (Attribute->appliesToDecl(D, Rule)) { 1112 Applies = true; 1113 break; 1114 } 1115 } 1116 if (!Applies) 1117 continue; 1118 Entry.IsUsed = true; 1119 PragmaAttributeCurrentTargetDecl = D; 1120 ParsedAttributesView Attrs; 1121 Attrs.addAtEnd(Attribute); 1122 ProcessDeclAttributeList(S, D, Attrs); 1123 PragmaAttributeCurrentTargetDecl = nullptr; 1124 } 1125 } 1126 } 1127 1128 void Sema::PrintPragmaAttributeInstantiationPoint() { 1129 assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration"); 1130 Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(), 1131 diag::note_pragma_attribute_applied_decl_here); 1132 } 1133 1134 void Sema::DiagnoseUnterminatedPragmaAttribute() { 1135 if (PragmaAttributeStack.empty()) 1136 return; 1137 Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof); 1138 } 1139 1140 void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) { 1141 if(On) 1142 OptimizeOffPragmaLocation = SourceLocation(); 1143 else 1144 OptimizeOffPragmaLocation = PragmaLoc; 1145 } 1146 1147 void Sema::ActOnPragmaMSOptimize(SourceLocation Loc, bool IsOn) { 1148 if (!CurContext->getRedeclContext()->isFileContext()) { 1149 Diag(Loc, diag::err_pragma_expected_file_scope) << "optimize"; 1150 return; 1151 } 1152 1153 MSPragmaOptimizeIsOn = IsOn; 1154 } 1155 1156 void Sema::ActOnPragmaMSFunction( 1157 SourceLocation Loc, const llvm::SmallVectorImpl<StringRef> &NoBuiltins) { 1158 if (!CurContext->getRedeclContext()->isFileContext()) { 1159 Diag(Loc, diag::err_pragma_expected_file_scope) << "function"; 1160 return; 1161 } 1162 1163 MSFunctionNoBuiltins.insert(NoBuiltins.begin(), NoBuiltins.end()); 1164 } 1165 1166 void Sema::AddRangeBasedOptnone(FunctionDecl *FD) { 1167 // In the future, check other pragmas if they're implemented (e.g. pragma 1168 // optimize 0 will probably map to this functionality too). 1169 if(OptimizeOffPragmaLocation.isValid()) 1170 AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation); 1171 } 1172 1173 void Sema::AddSectionMSAllocText(FunctionDecl *FD) { 1174 if (!FD->getIdentifier()) 1175 return; 1176 1177 StringRef Name = FD->getName(); 1178 auto It = FunctionToSectionMap.find(Name); 1179 if (It != FunctionToSectionMap.end()) { 1180 StringRef Section; 1181 SourceLocation Loc; 1182 std::tie(Section, Loc) = It->second; 1183 1184 if (!FD->hasAttr<SectionAttr>()) 1185 FD->addAttr(SectionAttr::CreateImplicit(Context, Section)); 1186 } 1187 } 1188 1189 void Sema::ModifyFnAttributesMSPragmaOptimize(FunctionDecl *FD) { 1190 // Don't modify the function attributes if it's "on". "on" resets the 1191 // optimizations to the ones listed on the command line 1192 if (!MSPragmaOptimizeIsOn) 1193 AddOptnoneAttributeIfNoConflicts(FD, FD->getBeginLoc()); 1194 } 1195 1196 void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, 1197 SourceLocation Loc) { 1198 // Don't add a conflicting attribute. No diagnostic is needed. 1199 if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>()) 1200 return; 1201 1202 // Add attributes only if required. Optnone requires noinline as well, but if 1203 // either is already present then don't bother adding them. 1204 if (!FD->hasAttr<OptimizeNoneAttr>()) 1205 FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc)); 1206 if (!FD->hasAttr<NoInlineAttr>()) 1207 FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc)); 1208 } 1209 1210 void Sema::AddImplicitMSFunctionNoBuiltinAttr(FunctionDecl *FD) { 1211 SmallVector<StringRef> V(MSFunctionNoBuiltins.begin(), 1212 MSFunctionNoBuiltins.end()); 1213 if (!MSFunctionNoBuiltins.empty()) 1214 FD->addAttr(NoBuiltinAttr::CreateImplicit(Context, V.data(), V.size())); 1215 } 1216 1217 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack; 1218 enum : unsigned { NoVisibility = ~0U }; 1219 1220 void Sema::AddPushedVisibilityAttribute(Decl *D) { 1221 if (!VisContext) 1222 return; 1223 1224 NamedDecl *ND = dyn_cast<NamedDecl>(D); 1225 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue)) 1226 return; 1227 1228 VisStack *Stack = static_cast<VisStack*>(VisContext); 1229 unsigned rawType = Stack->back().first; 1230 if (rawType == NoVisibility) return; 1231 1232 VisibilityAttr::VisibilityType type 1233 = (VisibilityAttr::VisibilityType) rawType; 1234 SourceLocation loc = Stack->back().second; 1235 1236 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc)); 1237 } 1238 1239 /// FreeVisContext - Deallocate and null out VisContext. 1240 void Sema::FreeVisContext() { 1241 delete static_cast<VisStack*>(VisContext); 1242 VisContext = nullptr; 1243 } 1244 1245 static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) { 1246 // Put visibility on stack. 1247 if (!S.VisContext) 1248 S.VisContext = new VisStack; 1249 1250 VisStack *Stack = static_cast<VisStack*>(S.VisContext); 1251 Stack->push_back(std::make_pair(type, loc)); 1252 } 1253 1254 void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType, 1255 SourceLocation PragmaLoc) { 1256 if (VisType) { 1257 // Compute visibility to use. 1258 VisibilityAttr::VisibilityType T; 1259 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) { 1260 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType; 1261 return; 1262 } 1263 PushPragmaVisibility(*this, T, PragmaLoc); 1264 } else { 1265 PopPragmaVisibility(false, PragmaLoc); 1266 } 1267 } 1268 1269 void Sema::ActOnPragmaFPContract(SourceLocation Loc, 1270 LangOptions::FPModeKind FPC) { 1271 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); 1272 switch (FPC) { 1273 case LangOptions::FPM_On: 1274 NewFPFeatures.setAllowFPContractWithinStatement(); 1275 break; 1276 case LangOptions::FPM_Fast: 1277 NewFPFeatures.setAllowFPContractAcrossStatement(); 1278 break; 1279 case LangOptions::FPM_Off: 1280 NewFPFeatures.setDisallowFPContract(); 1281 break; 1282 case LangOptions::FPM_FastHonorPragmas: 1283 llvm_unreachable("Should not happen"); 1284 } 1285 FpPragmaStack.Act(Loc, Sema::PSK_Set, StringRef(), NewFPFeatures); 1286 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); 1287 } 1288 1289 void Sema::ActOnPragmaFPReassociate(SourceLocation Loc, bool IsEnabled) { 1290 if (IsEnabled) { 1291 // For value unsafe context, combining this pragma with eval method 1292 // setting is not recommended. See comment in function FixupInvocation#506. 1293 int Reason = -1; 1294 if (getLangOpts().getFPEvalMethod() != LangOptions::FEM_UnsetOnCommandLine) 1295 // Eval method set using the option 'ffp-eval-method'. 1296 Reason = 1; 1297 if (PP.getLastFPEvalPragmaLocation().isValid()) 1298 // Eval method set using the '#pragma clang fp eval_method'. 1299 // We could have both an option and a pragma used to the set the eval 1300 // method. The pragma overrides the option in the command line. The Reason 1301 // of the diagnostic is overriden too. 1302 Reason = 0; 1303 if (Reason != -1) 1304 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) 1305 << Reason << 4; 1306 } 1307 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); 1308 NewFPFeatures.setAllowFPReassociateOverride(IsEnabled); 1309 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures); 1310 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); 1311 } 1312 1313 void Sema::ActOnPragmaFEnvRound(SourceLocation Loc, llvm::RoundingMode FPR) { 1314 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); 1315 NewFPFeatures.setConstRoundingModeOverride(FPR); 1316 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures); 1317 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); 1318 } 1319 1320 void Sema::setExceptionMode(SourceLocation Loc, 1321 LangOptions::FPExceptionModeKind FPE) { 1322 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); 1323 NewFPFeatures.setSpecifiedExceptionModeOverride(FPE); 1324 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures); 1325 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); 1326 } 1327 1328 void Sema::ActOnPragmaFEnvAccess(SourceLocation Loc, bool IsEnabled) { 1329 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides(); 1330 if (IsEnabled) { 1331 // Verify Microsoft restriction: 1332 // You can't enable fenv_access unless precise semantics are enabled. 1333 // Precise semantics can be enabled either by the float_control 1334 // pragma, or by using the /fp:precise or /fp:strict compiler options 1335 if (!isPreciseFPEnabled()) 1336 Diag(Loc, diag::err_pragma_fenv_requires_precise); 1337 } 1338 NewFPFeatures.setAllowFEnvAccessOverride(IsEnabled); 1339 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures); 1340 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts()); 1341 } 1342 1343 void Sema::ActOnPragmaFPExceptions(SourceLocation Loc, 1344 LangOptions::FPExceptionModeKind FPE) { 1345 setExceptionMode(Loc, FPE); 1346 } 1347 1348 void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, 1349 SourceLocation Loc) { 1350 // Visibility calculations will consider the namespace's visibility. 1351 // Here we just want to note that we're in a visibility context 1352 // which overrides any enclosing #pragma context, but doesn't itself 1353 // contribute visibility. 1354 PushPragmaVisibility(*this, NoVisibility, Loc); 1355 } 1356 1357 void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) { 1358 if (!VisContext) { 1359 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); 1360 return; 1361 } 1362 1363 // Pop visibility from stack 1364 VisStack *Stack = static_cast<VisStack*>(VisContext); 1365 1366 const std::pair<unsigned, SourceLocation> *Back = &Stack->back(); 1367 bool StartsWithPragma = Back->first != NoVisibility; 1368 if (StartsWithPragma && IsNamespaceEnd) { 1369 Diag(Back->second, diag::err_pragma_push_visibility_mismatch); 1370 Diag(EndLoc, diag::note_surrounding_namespace_ends_here); 1371 1372 // For better error recovery, eat all pushes inside the namespace. 1373 do { 1374 Stack->pop_back(); 1375 Back = &Stack->back(); 1376 StartsWithPragma = Back->first != NoVisibility; 1377 } while (StartsWithPragma); 1378 } else if (!StartsWithPragma && !IsNamespaceEnd) { 1379 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch); 1380 Diag(Back->second, diag::note_surrounding_namespace_starts_here); 1381 return; 1382 } 1383 1384 Stack->pop_back(); 1385 // To simplify the implementation, never keep around an empty stack. 1386 if (Stack->empty()) 1387 FreeVisContext(); 1388 } 1389 1390 template <typename Ty> 1391 static bool checkCommonAttributeFeatures(Sema &S, const Ty *Node, 1392 const ParsedAttr &A, 1393 bool SkipArgCountCheck) { 1394 // Several attributes carry different semantics than the parsing requires, so 1395 // those are opted out of the common argument checks. 1396 // 1397 // We also bail on unknown and ignored attributes because those are handled 1398 // as part of the target-specific handling logic. 1399 if (A.getKind() == ParsedAttr::UnknownAttribute) 1400 return false; 1401 // Check whether the attribute requires specific language extensions to be 1402 // enabled. 1403 if (!A.diagnoseLangOpts(S)) 1404 return true; 1405 // Check whether the attribute appertains to the given subject. 1406 if (!A.diagnoseAppertainsTo(S, Node)) 1407 return true; 1408 // Check whether the attribute is mutually exclusive with other attributes 1409 // that have already been applied to the declaration. 1410 if (!A.diagnoseMutualExclusion(S, Node)) 1411 return true; 1412 // Check whether the attribute exists in the target architecture. 1413 if (S.CheckAttrTarget(A)) 1414 return true; 1415 1416 if (A.hasCustomParsing()) 1417 return false; 1418 1419 if (!SkipArgCountCheck) { 1420 if (A.getMinArgs() == A.getMaxArgs()) { 1421 // If there are no optional arguments, then checking for the argument 1422 // count is trivial. 1423 if (!A.checkExactlyNumArgs(S, A.getMinArgs())) 1424 return true; 1425 } else { 1426 // There are optional arguments, so checking is slightly more involved. 1427 if (A.getMinArgs() && !A.checkAtLeastNumArgs(S, A.getMinArgs())) 1428 return true; 1429 else if (!A.hasVariadicArg() && A.getMaxArgs() && 1430 !A.checkAtMostNumArgs(S, A.getMaxArgs())) 1431 return true; 1432 } 1433 } 1434 1435 return false; 1436 } 1437 1438 bool Sema::checkCommonAttributeFeatures(const Decl *D, const ParsedAttr &A, 1439 bool SkipArgCountCheck) { 1440 return ::checkCommonAttributeFeatures(*this, D, A, SkipArgCountCheck); 1441 } 1442 bool Sema::checkCommonAttributeFeatures(const Stmt *S, const ParsedAttr &A, 1443 bool SkipArgCountCheck) { 1444 return ::checkCommonAttributeFeatures(*this, S, A, SkipArgCountCheck); 1445 } 1446