1 //===--- SemaAttr.cpp - Semantic Analysis for Attributes ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements semantic analysis for non-trivial attributes and
12 //===----------------------------------------------------------------------===//
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;
23 //===----------------------------------------------------------------------===//
24 // Pragma 'pack' and 'options align'
25 //===----------------------------------------------------------------------===//
27 Sema::PragmaStackSentinelRAII::PragmaStackSentinelRAII(Sema &S,
30 : S(S), SlotLabel(SlotLabel), ShouldAct(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);
40 Sema::PragmaStackSentinelRAII::~PragmaStackSentinelRAII() {
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);
50 void Sema::AddAlignmentAttributesForRecord(RecordDecl *RD) {
51 // If there is no pack value, we don't need any attributes.
52 if (!PackStack.CurrentValue)
55 // Otherwise, check to see if we need a max field alignment attribute.
56 if (unsigned Alignment = PackStack.CurrentValue) {
57 if (Alignment == Sema::kMac68kAlignmentSentinel)
58 RD->addAttr(AlignMac68kAttr::CreateImplicit(Context));
60 RD->addAttr(MaxFieldAlignmentAttr::CreateImplicit(Context,
63 if (PackIncludeStack.empty())
65 // The #pragma pack affected a record in an included file, so Clang should
66 // warn when that pragma was written in a file that included the included
68 for (auto &PackedInclude : llvm::reverse(PackIncludeStack)) {
69 if (PackedInclude.CurrentPragmaLocation != PackStack.CurrentPragmaLocation)
71 if (PackedInclude.HasNonDefaultValue)
72 PackedInclude.ShouldWarnOnInclude = true;
76 void Sema::AddMsStructLayoutForRecord(RecordDecl *RD) {
78 RD->addAttr(MSStructAttr::CreateImplicit(Context));
80 // FIXME: We should merge AddAlignmentAttributesForRecord with
81 // AddMsStructLayoutForRecord into AddPragmaAttributesForRecord, which takes
82 // all active pragmas and applies them as attributes to class definitions.
83 if (VtorDispStack.CurrentValue != getLangOpts().VtorDispMode)
85 MSVtorDispAttr::CreateImplicit(Context, VtorDispStack.CurrentValue));
88 void Sema::ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind,
89 SourceLocation PragmaLoc) {
90 PragmaMsStackAction Action = Sema::PSK_Reset;
91 unsigned Alignment = 0;
93 // For all targets we support native and natural are the same.
95 // FIXME: This is not true on Darwin/PPC.
99 Action = Sema::PSK_Push_Set;
103 // Note that '#pragma options align=packed' is not equivalent to attribute
104 // packed, it has a different precedence relative to attribute aligned.
106 Action = Sema::PSK_Push_Set;
111 // Check if the target supports this.
112 if (!this->Context.getTargetInfo().hasAlignMac68kSupport()) {
113 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
116 Action = Sema::PSK_Push_Set;
117 Alignment = Sema::kMac68kAlignmentSentinel;
121 // Reset just pops the top of the stack, or resets the current alignment to
123 Action = Sema::PSK_Pop;
124 if (PackStack.Stack.empty()) {
125 if (PackStack.CurrentValue) {
126 Action = Sema::PSK_Reset;
128 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
136 PackStack.Act(PragmaLoc, Action, StringRef(), Alignment);
139 void Sema::ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action,
140 PragmaClangSectionKind SecKind, StringRef SecName) {
141 PragmaClangSection *CSec;
143 case PragmaClangSectionKind::PCSK_BSS:
144 CSec = &PragmaClangBSSSection;
146 case PragmaClangSectionKind::PCSK_Data:
147 CSec = &PragmaClangDataSection;
149 case PragmaClangSectionKind::PCSK_Rodata:
150 CSec = &PragmaClangRodataSection;
152 case PragmaClangSectionKind::PCSK_Text:
153 CSec = &PragmaClangTextSection;
156 llvm_unreachable("invalid clang section kind");
159 if (Action == PragmaClangSectionAction::PCSA_Clear) {
165 CSec->SectionName = SecName;
166 CSec->PragmaLocation = PragmaLoc;
169 void Sema::ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action,
170 StringRef SlotLabel, Expr *alignment) {
171 Expr *Alignment = static_cast<Expr *>(alignment);
173 // If specified then alignment must be a "small" power of two.
174 unsigned AlignmentVal = 0;
178 // pack(0) is like pack(), which just works out since that is what
179 // we use 0 for in PackAttr.
180 if (Alignment->isTypeDependent() ||
181 Alignment->isValueDependent() ||
182 !Alignment->isIntegerConstantExpr(Val, Context) ||
183 !(Val == 0 || Val.isPowerOf2()) ||
184 Val.getZExtValue() > 16) {
185 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
189 AlignmentVal = (unsigned) Val.getZExtValue();
191 if (Action == Sema::PSK_Show) {
192 // Show the current alignment, making sure to show the right value
194 // FIXME: This should come from the target.
195 AlignmentVal = PackStack.CurrentValue;
196 if (AlignmentVal == 0)
198 if (AlignmentVal == Sema::kMac68kAlignmentSentinel)
199 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
201 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
203 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
204 // "#pragma pack(pop, identifier, n) is undefined"
205 if (Action & Sema::PSK_Pop) {
206 if (Alignment && !SlotLabel.empty())
207 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment);
208 if (PackStack.Stack.empty())
209 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty";
212 PackStack.Act(PragmaLoc, Action, SlotLabel, AlignmentVal);
215 void Sema::DiagnoseNonDefaultPragmaPack(PragmaPackDiagnoseKind Kind,
216 SourceLocation IncludeLoc) {
217 if (Kind == PragmaPackDiagnoseKind::NonDefaultStateAtInclude) {
218 SourceLocation PrevLocation = PackStack.CurrentPragmaLocation;
219 // Warn about non-default alignment at #includes (without redundant
220 // warnings for the same directive in nested includes).
221 // The warning is delayed until the end of the file to avoid warnings
222 // for files that don't have any records that are affected by the modified
224 bool HasNonDefaultValue =
225 PackStack.hasValue() &&
226 (PackIncludeStack.empty() ||
227 PackIncludeStack.back().CurrentPragmaLocation != PrevLocation);
228 PackIncludeStack.push_back(
229 {PackStack.CurrentValue,
230 PackStack.hasValue() ? PrevLocation : SourceLocation(),
231 HasNonDefaultValue, /*ShouldWarnOnInclude*/ false});
235 assert(Kind == PragmaPackDiagnoseKind::ChangedStateAtExit && "invalid kind");
236 PackIncludeState PrevPackState = PackIncludeStack.pop_back_val();
237 if (PrevPackState.ShouldWarnOnInclude) {
238 // Emit the delayed non-default alignment at #include warning.
239 Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include);
240 Diag(PrevPackState.CurrentPragmaLocation, diag::note_pragma_pack_here);
242 // Warn about modified alignment after #includes.
243 if (PrevPackState.CurrentValue != PackStack.CurrentValue) {
244 Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include);
245 Diag(PackStack.CurrentPragmaLocation, diag::note_pragma_pack_here);
249 void Sema::DiagnoseUnterminatedPragmaPack() {
250 if (PackStack.Stack.empty())
252 bool IsInnermost = true;
253 for (const auto &StackSlot : llvm::reverse(PackStack.Stack)) {
254 Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof);
255 // The user might have already reset the alignment, so suggest replacing
256 // the reset with a pop.
257 if (IsInnermost && PackStack.CurrentValue == PackStack.DefaultValue) {
258 DiagnosticBuilder DB = Diag(PackStack.CurrentPragmaLocation,
259 diag::note_pragma_pack_pop_instead_reset);
260 SourceLocation FixItLoc = Lexer::findLocationAfterToken(
261 PackStack.CurrentPragmaLocation, tok::l_paren, SourceMgr, LangOpts,
262 /*SkipTrailing=*/false);
263 if (FixItLoc.isValid())
264 DB << FixItHint::CreateInsertion(FixItLoc, "pop");
270 void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
271 MSStructPragmaOn = (Kind == PMSST_ON);
274 void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
275 PragmaMSCommentKind Kind, StringRef Arg) {
276 auto *PCD = PragmaCommentDecl::Create(
277 Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
278 Context.getTranslationUnitDecl()->addDecl(PCD);
279 Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
282 void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name,
284 auto *PDMD = PragmaDetectMismatchDecl::Create(
285 Context, Context.getTranslationUnitDecl(), Loc, Name, Value);
286 Context.getTranslationUnitDecl()->addDecl(PDMD);
287 Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD));
290 void Sema::ActOnPragmaMSPointersToMembers(
291 LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
292 SourceLocation PragmaLoc) {
293 MSPointerToMemberRepresentationMethod = RepresentationMethod;
294 ImplicitMSInheritanceAttrLoc = PragmaLoc;
297 void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action,
298 SourceLocation PragmaLoc,
299 MSVtorDispAttr::Mode Mode) {
300 if (Action & PSK_Pop && VtorDispStack.Stack.empty())
301 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
303 VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode);
306 template<typename ValueType>
307 void Sema::PragmaStack<ValueType>::Act(SourceLocation PragmaLocation,
308 PragmaMsStackAction Action,
309 llvm::StringRef StackSlotLabel,
311 if (Action == PSK_Reset) {
312 CurrentValue = DefaultValue;
313 CurrentPragmaLocation = PragmaLocation;
316 if (Action & PSK_Push)
317 Stack.emplace_back(StackSlotLabel, CurrentValue, CurrentPragmaLocation,
319 else if (Action & PSK_Pop) {
320 if (!StackSlotLabel.empty()) {
321 // If we've got a label, try to find it and jump there.
322 auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
323 return x.StackSlotLabel == StackSlotLabel;
325 // If we found the label so pop from there.
326 if (I != Stack.rend()) {
327 CurrentValue = I->Value;
328 CurrentPragmaLocation = I->PragmaLocation;
329 Stack.erase(std::prev(I.base()), Stack.end());
331 } else if (!Stack.empty()) {
332 // We do not have a label, just pop the last entry.
333 CurrentValue = Stack.back().Value;
334 CurrentPragmaLocation = Stack.back().PragmaLocation;
338 if (Action & PSK_Set) {
339 CurrentValue = Value;
340 CurrentPragmaLocation = PragmaLocation;
344 bool Sema::UnifySection(StringRef SectionName,
346 DeclaratorDecl *Decl) {
347 auto Section = Context.SectionInfos.find(SectionName);
348 if (Section == Context.SectionInfos.end()) {
349 Context.SectionInfos[SectionName] =
350 ASTContext::SectionInfo(Decl, SourceLocation(), SectionFlags);
353 // A pre-declared section takes precedence w/o diagnostic.
354 if (Section->second.SectionFlags == SectionFlags ||
355 !(Section->second.SectionFlags & ASTContext::PSF_Implicit))
357 auto OtherDecl = Section->second.Decl;
358 Diag(Decl->getLocation(), diag::err_section_conflict)
359 << Decl << OtherDecl;
360 Diag(OtherDecl->getLocation(), diag::note_declared_at)
361 << OtherDecl->getName();
362 if (auto A = Decl->getAttr<SectionAttr>())
364 Diag(A->getLocation(), diag::note_pragma_entered_here);
365 if (auto A = OtherDecl->getAttr<SectionAttr>())
367 Diag(A->getLocation(), diag::note_pragma_entered_here);
371 bool Sema::UnifySection(StringRef SectionName,
373 SourceLocation PragmaSectionLocation) {
374 auto Section = Context.SectionInfos.find(SectionName);
375 if (Section != Context.SectionInfos.end()) {
376 if (Section->second.SectionFlags == SectionFlags)
378 if (!(Section->second.SectionFlags & ASTContext::PSF_Implicit)) {
379 Diag(PragmaSectionLocation, diag::err_section_conflict)
380 << "this" << "a prior #pragma section";
381 Diag(Section->second.PragmaSectionLocation,
382 diag::note_pragma_entered_here);
386 Context.SectionInfos[SectionName] =
387 ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
391 /// Called on well formed \#pragma bss_seg().
392 void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
393 PragmaMsStackAction Action,
394 llvm::StringRef StackSlotLabel,
395 StringLiteral *SegmentName,
396 llvm::StringRef PragmaName) {
397 PragmaStack<StringLiteral *> *Stack =
398 llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
399 .Case("data_seg", &DataSegStack)
400 .Case("bss_seg", &BSSSegStack)
401 .Case("const_seg", &ConstSegStack)
402 .Case("code_seg", &CodeSegStack);
403 if (Action & PSK_Pop && Stack->Stack.empty())
404 Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
407 if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString()))
410 if (SegmentName->getString() == ".drectve" &&
411 Context.getTargetInfo().getCXXABI().isMicrosoft())
412 Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName;
415 Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
418 /// Called on well formed \#pragma bss_seg().
419 void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
420 int SectionFlags, StringLiteral *SegmentName) {
421 UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
424 void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
425 StringLiteral *SegmentName) {
426 // There's no stack to maintain, so we just have a current section. When we
427 // see the default section, reset our current section back to null so we stop
428 // tacking on unnecessary attributes.
429 CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
430 CurInitSegLoc = PragmaLocation;
433 void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
434 SourceLocation PragmaLoc) {
436 IdentifierInfo *Name = IdTok.getIdentifierInfo();
437 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
438 LookupParsedName(Lookup, curScope, nullptr, true);
440 if (Lookup.empty()) {
441 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
442 << Name << SourceRange(IdTok.getLocation());
446 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
448 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
449 << Name << SourceRange(IdTok.getLocation());
453 // Warn if this was used before being marked unused.
455 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
457 VD->addAttr(UnusedAttr::CreateImplicit(Context, UnusedAttr::GNU_unused,
458 IdTok.getLocation()));
461 void Sema::AddCFAuditedAttribute(Decl *D) {
462 SourceLocation Loc = PP.getPragmaARCCFCodeAuditedLoc();
463 if (!Loc.isValid()) return;
465 // Don't add a redundant or conflicting attribute.
466 if (D->hasAttr<CFAuditedTransferAttr>() ||
467 D->hasAttr<CFUnknownTransferAttr>())
470 D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Loc));
475 Optional<attr::SubjectMatchRule>
476 getParentAttrMatcherRule(attr::SubjectMatchRule Rule) {
477 using namespace attr;
481 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
482 #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
485 #include "clang/Basic/AttrSubMatchRulesList.inc"
489 bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) {
490 using namespace attr;
494 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
495 #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
498 #include "clang/Basic/AttrSubMatchRulesList.inc"
502 CharSourceRange replacementRangeForListElement(const Sema &S,
504 // Make sure that the ',' is removed as well.
505 SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken(
506 Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(),
507 /*SkipTrailingWhitespaceAndNewLine=*/false);
508 if (AfterCommaLoc.isValid())
509 return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc);
511 return CharSourceRange::getTokenRange(Range);
515 attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) {
517 llvm::raw_string_ostream OS(Result);
518 for (const auto &I : llvm::enumerate(Rules)) {
520 OS << (I.index() == Rules.size() - 1 ? ", and " : ", ");
521 OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'";
526 } // end anonymous namespace
528 void Sema::ActOnPragmaAttributeAttribute(
529 ParsedAttr &Attribute, SourceLocation PragmaLoc,
530 attr::ParsedSubjectMatchRuleSet Rules) {
531 Attribute.setIsPragmaClangAttribute();
532 SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules;
533 // Gather the subject match rules that are supported by the attribute.
534 SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4>
535 StrictSubjectMatchRuleSet;
536 Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet);
538 // Figure out which subject matching rules are valid.
539 if (StrictSubjectMatchRuleSet.empty()) {
540 // Check for contradicting match rules. Contradicting match rules are
542 // - a top-level rule and one of its sub-rules. E.g. variable and
543 // variable(is_parameter).
544 // - a sub-rule and a sibling that's negated. E.g.
545 // variable(is_thread_local) and variable(unless(is_parameter))
546 llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2>
547 RulesToFirstSpecifiedNegatedSubRule;
548 for (const auto &Rule : Rules) {
549 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
550 Optional<attr::SubjectMatchRule> ParentRule =
551 getParentAttrMatcherRule(MatchRule);
554 auto It = Rules.find(*ParentRule);
555 if (It != Rules.end()) {
556 // A sub-rule contradicts a parent rule.
557 Diag(Rule.second.getBegin(),
558 diag::err_pragma_attribute_matcher_subrule_contradicts_rule)
559 << attr::getSubjectMatchRuleSpelling(MatchRule)
560 << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second
561 << FixItHint::CreateRemoval(
562 replacementRangeForListElement(*this, Rule.second));
563 // Keep going without removing this rule as it won't change the set of
564 // declarations that receive the attribute.
567 if (isNegatedAttrMatcherSubRule(MatchRule))
568 RulesToFirstSpecifiedNegatedSubRule.insert(
569 std::make_pair(*ParentRule, Rule));
571 bool IgnoreNegatedSubRules = false;
572 for (const auto &Rule : Rules) {
573 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
574 Optional<attr::SubjectMatchRule> ParentRule =
575 getParentAttrMatcherRule(MatchRule);
578 auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule);
579 if (It != RulesToFirstSpecifiedNegatedSubRule.end() &&
580 It->second != Rule) {
581 // Negated sub-rule contradicts another sub-rule.
583 It->second.second.getBegin(),
585 err_pragma_attribute_matcher_negated_subrule_contradicts_subrule)
586 << attr::getSubjectMatchRuleSpelling(
587 attr::SubjectMatchRule(It->second.first))
588 << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second
589 << FixItHint::CreateRemoval(
590 replacementRangeForListElement(*this, It->second.second));
591 // Keep going but ignore all of the negated sub-rules.
592 IgnoreNegatedSubRules = true;
593 RulesToFirstSpecifiedNegatedSubRule.erase(It);
597 if (!IgnoreNegatedSubRules) {
598 for (const auto &Rule : Rules)
599 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
601 for (const auto &Rule : Rules) {
602 if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first)))
603 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
608 for (const auto &Rule : StrictSubjectMatchRuleSet) {
609 if (Rules.erase(Rule.first)) {
610 // Add the rule to the set of attribute receivers only if it's supported
611 // in the current language mode.
613 SubjectMatchRules.push_back(Rule.first);
618 if (!Rules.empty()) {
620 Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers)
621 << Attribute.getName();
622 SmallVector<attr::SubjectMatchRule, 2> ExtraRules;
623 for (const auto &Rule : Rules) {
624 ExtraRules.push_back(attr::SubjectMatchRule(Rule.first));
625 Diagnostic << FixItHint::CreateRemoval(
626 replacementRangeForListElement(*this, Rule.second));
628 Diagnostic << attrMatcherRuleListToString(ExtraRules);
631 if (PragmaAttributeStack.empty()) {
632 Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push);
636 PragmaAttributeStack.back().Entries.push_back(
637 {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false});
640 void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc,
641 const IdentifierInfo *Namespace) {
642 PragmaAttributeStack.emplace_back();
643 PragmaAttributeStack.back().Loc = PragmaLoc;
644 PragmaAttributeStack.back().Namespace = Namespace;
647 void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
648 const IdentifierInfo *Namespace) {
649 if (PragmaAttributeStack.empty()) {
650 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
654 // Dig back through the stack trying to find the most recently pushed group
655 // that in Namespace. Note that this works fine if no namespace is present,
656 // think of push/pops without namespaces as having an implicit "nullptr"
658 for (size_t Index = PragmaAttributeStack.size(); Index;) {
660 if (PragmaAttributeStack[Index].Namespace == Namespace) {
661 for (const PragmaAttributeEntry &Entry :
662 PragmaAttributeStack[Index].Entries) {
664 assert(Entry.Attribute && "Expected an attribute");
665 Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused)
667 Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here);
670 PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index);
676 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch)
677 << 0 << Namespace->getName();
679 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
682 void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
683 if (PragmaAttributeStack.empty())
685 for (auto &Group : PragmaAttributeStack) {
686 for (auto &Entry : Group.Entries) {
687 ParsedAttr *Attribute = Entry.Attribute;
688 assert(Attribute && "Expected an attribute");
689 assert(Attribute->isPragmaClangAttribute() &&
690 "expected #pragma clang attribute");
692 // Ensure that the attribute can be applied to the given declaration.
693 bool Applies = false;
694 for (const auto &Rule : Entry.MatchRules) {
695 if (Attribute->appliesToDecl(D, Rule)) {
703 PragmaAttributeCurrentTargetDecl = D;
704 ParsedAttributesView Attrs;
705 Attrs.addAtEnd(Attribute);
706 ProcessDeclAttributeList(S, D, Attrs);
707 PragmaAttributeCurrentTargetDecl = nullptr;
712 void Sema::PrintPragmaAttributeInstantiationPoint() {
713 assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration");
714 Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(),
715 diag::note_pragma_attribute_applied_decl_here);
718 void Sema::DiagnoseUnterminatedPragmaAttribute() {
719 if (PragmaAttributeStack.empty())
721 Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof);
724 void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
726 OptimizeOffPragmaLocation = SourceLocation();
728 OptimizeOffPragmaLocation = PragmaLoc;
731 void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
732 // In the future, check other pragmas if they're implemented (e.g. pragma
733 // optimize 0 will probably map to this functionality too).
734 if(OptimizeOffPragmaLocation.isValid())
735 AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
738 void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
739 SourceLocation Loc) {
740 // Don't add a conflicting attribute. No diagnostic is needed.
741 if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
744 // Add attributes only if required. Optnone requires noinline as well, but if
745 // either is already present then don't bother adding them.
746 if (!FD->hasAttr<OptimizeNoneAttr>())
747 FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
748 if (!FD->hasAttr<NoInlineAttr>())
749 FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
752 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
753 enum : unsigned { NoVisibility = ~0U };
755 void Sema::AddPushedVisibilityAttribute(Decl *D) {
759 NamedDecl *ND = dyn_cast<NamedDecl>(D);
760 if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
763 VisStack *Stack = static_cast<VisStack*>(VisContext);
764 unsigned rawType = Stack->back().first;
765 if (rawType == NoVisibility) return;
767 VisibilityAttr::VisibilityType type
768 = (VisibilityAttr::VisibilityType) rawType;
769 SourceLocation loc = Stack->back().second;
771 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
774 /// FreeVisContext - Deallocate and null out VisContext.
775 void Sema::FreeVisContext() {
776 delete static_cast<VisStack*>(VisContext);
777 VisContext = nullptr;
780 static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
781 // Put visibility on stack.
783 S.VisContext = new VisStack;
785 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
786 Stack->push_back(std::make_pair(type, loc));
789 void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
790 SourceLocation PragmaLoc) {
792 // Compute visibility to use.
793 VisibilityAttr::VisibilityType T;
794 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
795 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
798 PushPragmaVisibility(*this, T, PragmaLoc);
800 PopPragmaVisibility(false, PragmaLoc);
804 void Sema::ActOnPragmaFPContract(LangOptions::FPContractModeKind FPC) {
806 case LangOptions::FPC_On:
807 FPFeatures.setAllowFPContractWithinStatement();
809 case LangOptions::FPC_Fast:
810 FPFeatures.setAllowFPContractAcrossStatement();
812 case LangOptions::FPC_Off:
813 FPFeatures.setDisallowFPContract();
818 void Sema::ActOnPragmaFEnvAccess(LangOptions::FEnvAccessModeKind FPC) {
820 case LangOptions::FEA_On:
821 FPFeatures.setAllowFEnvAccess();
823 case LangOptions::FEA_Off:
824 FPFeatures.setDisallowFEnvAccess();
830 void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
831 SourceLocation Loc) {
832 // Visibility calculations will consider the namespace's visibility.
833 // Here we just want to note that we're in a visibility context
834 // which overrides any enclosing #pragma context, but doesn't itself
835 // contribute visibility.
836 PushPragmaVisibility(*this, NoVisibility, Loc);
839 void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
841 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
845 // Pop visibility from stack
846 VisStack *Stack = static_cast<VisStack*>(VisContext);
848 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
849 bool StartsWithPragma = Back->first != NoVisibility;
850 if (StartsWithPragma && IsNamespaceEnd) {
851 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
852 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
854 // For better error recovery, eat all pushes inside the namespace.
857 Back = &Stack->back();
858 StartsWithPragma = Back->first != NoVisibility;
859 } while (StartsWithPragma);
860 } else if (!StartsWithPragma && !IsNamespaceEnd) {
861 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
862 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
867 // To simplify the implementation, never keep around an empty stack.