xref: /freebsd/contrib/llvm-project/clang/lib/Sema/SemaAttr.cpp (revision e7437ae907c89bf85a99c5cbb7ddd194a1ff1354)
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 void Sema::DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind,
388                                              SourceLocation IncludeLoc) {
389   if (Kind == PragmaAlignPackDiagnoseKind::NonDefaultStateAtInclude) {
390     SourceLocation PrevLocation = AlignPackStack.CurrentPragmaLocation;
391     // Warn about non-default alignment at #includes (without redundant
392     // warnings for the same directive in nested includes).
393     // The warning is delayed until the end of the file to avoid warnings
394     // for files that don't have any records that are affected by the modified
395     // alignment.
396     bool HasNonDefaultValue =
397         AlignPackStack.hasValue() &&
398         (AlignPackIncludeStack.empty() ||
399          AlignPackIncludeStack.back().CurrentPragmaLocation != PrevLocation);
400     AlignPackIncludeStack.push_back(
401         {AlignPackStack.CurrentValue,
402          AlignPackStack.hasValue() ? PrevLocation : SourceLocation(),
403          HasNonDefaultValue, /*ShouldWarnOnInclude*/ false});
404     return;
405   }
406 
407   assert(Kind == PragmaAlignPackDiagnoseKind::ChangedStateAtExit &&
408          "invalid kind");
409   AlignPackIncludeState PrevAlignPackState =
410       AlignPackIncludeStack.pop_back_val();
411   // FIXME: AlignPackStack may contain both #pragma align and #pragma pack
412   // information, diagnostics below might not be accurate if we have mixed
413   // pragmas.
414   if (PrevAlignPackState.ShouldWarnOnInclude) {
415     // Emit the delayed non-default alignment at #include warning.
416     Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include);
417     Diag(PrevAlignPackState.CurrentPragmaLocation, diag::note_pragma_pack_here);
418   }
419   // Warn about modified alignment after #includes.
420   if (PrevAlignPackState.CurrentValue != AlignPackStack.CurrentValue) {
421     Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include);
422     Diag(AlignPackStack.CurrentPragmaLocation, diag::note_pragma_pack_here);
423   }
424 }
425 
426 void Sema::DiagnoseUnterminatedPragmaAlignPack() {
427   if (AlignPackStack.Stack.empty())
428     return;
429   bool IsInnermost = true;
430 
431   // FIXME: AlignPackStack may contain both #pragma align and #pragma pack
432   // information, diagnostics below might not be accurate if we have mixed
433   // pragmas.
434   for (const auto &StackSlot : llvm::reverse(AlignPackStack.Stack)) {
435     Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof);
436     // The user might have already reset the alignment, so suggest replacing
437     // the reset with a pop.
438     if (IsInnermost &&
439         AlignPackStack.CurrentValue == AlignPackStack.DefaultValue) {
440       auto DB = Diag(AlignPackStack.CurrentPragmaLocation,
441                      diag::note_pragma_pack_pop_instead_reset);
442       SourceLocation FixItLoc =
443           Lexer::findLocationAfterToken(AlignPackStack.CurrentPragmaLocation,
444                                         tok::l_paren, SourceMgr, LangOpts,
445                                         /*SkipTrailing=*/false);
446       if (FixItLoc.isValid())
447         DB << FixItHint::CreateInsertion(FixItLoc, "pop");
448     }
449     IsInnermost = false;
450   }
451 }
452 
453 void Sema::ActOnPragmaMSStruct(PragmaMSStructKind Kind) {
454   MSStructPragmaOn = (Kind == PMSST_ON);
455 }
456 
457 void Sema::ActOnPragmaMSComment(SourceLocation CommentLoc,
458                                 PragmaMSCommentKind Kind, StringRef Arg) {
459   auto *PCD = PragmaCommentDecl::Create(
460       Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
461   Context.getTranslationUnitDecl()->addDecl(PCD);
462   Consumer.HandleTopLevelDecl(DeclGroupRef(PCD));
463 }
464 
465 void Sema::ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name,
466                                      StringRef Value) {
467   auto *PDMD = PragmaDetectMismatchDecl::Create(
468       Context, Context.getTranslationUnitDecl(), Loc, Name, Value);
469   Context.getTranslationUnitDecl()->addDecl(PDMD);
470   Consumer.HandleTopLevelDecl(DeclGroupRef(PDMD));
471 }
472 
473 void Sema::ActOnPragmaFloatControl(SourceLocation Loc,
474                                    PragmaMsStackAction Action,
475                                    PragmaFloatControlKind Value) {
476   FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
477   if ((Action == PSK_Push_Set || Action == PSK_Push || Action == PSK_Pop) &&
478       !CurContext->getRedeclContext()->isFileContext()) {
479     // Push and pop can only occur at file or namespace scope, or within a
480     // language linkage declaration.
481     Diag(Loc, diag::err_pragma_fc_pp_scope);
482     return;
483   }
484   switch (Value) {
485   default:
486     llvm_unreachable("invalid pragma float_control kind");
487   case PFC_Precise:
488     NewFPFeatures.setFPPreciseEnabled(true);
489     FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
490     break;
491   case PFC_NoPrecise:
492     if (CurFPFeatures.getFPExceptionMode() == LangOptions::FPE_Strict)
493       Diag(Loc, diag::err_pragma_fc_noprecise_requires_noexcept);
494     else if (CurFPFeatures.getAllowFEnvAccess())
495       Diag(Loc, diag::err_pragma_fc_noprecise_requires_nofenv);
496     else
497       NewFPFeatures.setFPPreciseEnabled(false);
498     FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
499     break;
500   case PFC_Except:
501     if (!isPreciseFPEnabled())
502       Diag(Loc, diag::err_pragma_fc_except_requires_precise);
503     else
504       NewFPFeatures.setFPExceptionModeOverride(LangOptions::FPE_Strict);
505     FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
506     break;
507   case PFC_NoExcept:
508     NewFPFeatures.setFPExceptionModeOverride(LangOptions::FPE_Ignore);
509     FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
510     break;
511   case PFC_Push:
512     FpPragmaStack.Act(Loc, Sema::PSK_Push_Set, StringRef(), NewFPFeatures);
513     break;
514   case PFC_Pop:
515     if (FpPragmaStack.Stack.empty()) {
516       Diag(Loc, diag::warn_pragma_pop_failed) << "float_control"
517                                               << "stack empty";
518       return;
519     }
520     FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
521     NewFPFeatures = FpPragmaStack.CurrentValue;
522     break;
523   }
524   CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
525 }
526 
527 void Sema::ActOnPragmaMSPointersToMembers(
528     LangOptions::PragmaMSPointersToMembersKind RepresentationMethod,
529     SourceLocation PragmaLoc) {
530   MSPointerToMemberRepresentationMethod = RepresentationMethod;
531   ImplicitMSInheritanceAttrLoc = PragmaLoc;
532 }
533 
534 void Sema::ActOnPragmaMSVtorDisp(PragmaMsStackAction Action,
535                                  SourceLocation PragmaLoc,
536                                  MSVtorDispMode Mode) {
537   if (Action & PSK_Pop && VtorDispStack.Stack.empty())
538     Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
539                                                   << "stack empty";
540   VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode);
541 }
542 
543 template <>
544 void Sema::PragmaStack<Sema::AlignPackInfo>::Act(SourceLocation PragmaLocation,
545                                                  PragmaMsStackAction Action,
546                                                  llvm::StringRef StackSlotLabel,
547                                                  AlignPackInfo Value) {
548   if (Action == PSK_Reset) {
549     CurrentValue = DefaultValue;
550     CurrentPragmaLocation = PragmaLocation;
551     return;
552   }
553   if (Action & PSK_Push)
554     Stack.emplace_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation,
555                             PragmaLocation));
556   else if (Action & PSK_Pop) {
557     if (!StackSlotLabel.empty()) {
558       // If we've got a label, try to find it and jump there.
559       auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
560         return x.StackSlotLabel == StackSlotLabel;
561       });
562       // We found the label, so pop from there.
563       if (I != Stack.rend()) {
564         CurrentValue = I->Value;
565         CurrentPragmaLocation = I->PragmaLocation;
566         Stack.erase(std::prev(I.base()), Stack.end());
567       }
568     } else if (Value.IsXLStack() && Value.IsAlignAttr() &&
569                CurrentValue.IsPackAttr()) {
570       // XL '#pragma align(reset)' would pop the stack until
571       // a current in effect pragma align is popped.
572       auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
573         return x.Value.IsAlignAttr();
574       });
575       // If we found pragma align so pop from there.
576       if (I != Stack.rend()) {
577         Stack.erase(std::prev(I.base()), Stack.end());
578         if (Stack.empty()) {
579           CurrentValue = DefaultValue;
580           CurrentPragmaLocation = PragmaLocation;
581         } else {
582           CurrentValue = Stack.back().Value;
583           CurrentPragmaLocation = Stack.back().PragmaLocation;
584           Stack.pop_back();
585         }
586       }
587     } else if (!Stack.empty()) {
588       // xl '#pragma align' sets the baseline, and `#pragma pack` cannot pop
589       // over the baseline.
590       if (Value.IsXLStack() && Value.IsPackAttr() && CurrentValue.IsAlignAttr())
591         return;
592 
593       // We don't have a label, just pop the last entry.
594       CurrentValue = Stack.back().Value;
595       CurrentPragmaLocation = Stack.back().PragmaLocation;
596       Stack.pop_back();
597     }
598   }
599   if (Action & PSK_Set) {
600     CurrentValue = Value;
601     CurrentPragmaLocation = PragmaLocation;
602   }
603 }
604 
605 bool Sema::UnifySection(StringRef SectionName, int SectionFlags,
606                         NamedDecl *Decl) {
607   SourceLocation PragmaLocation;
608   if (auto A = Decl->getAttr<SectionAttr>())
609     if (A->isImplicit())
610       PragmaLocation = A->getLocation();
611   auto SectionIt = Context.SectionInfos.find(SectionName);
612   if (SectionIt == Context.SectionInfos.end()) {
613     Context.SectionInfos[SectionName] =
614         ASTContext::SectionInfo(Decl, PragmaLocation, SectionFlags);
615     return false;
616   }
617   // A pre-declared section takes precedence w/o diagnostic.
618   const auto &Section = SectionIt->second;
619   if (Section.SectionFlags == SectionFlags ||
620       ((SectionFlags & ASTContext::PSF_Implicit) &&
621        !(Section.SectionFlags & ASTContext::PSF_Implicit)))
622     return false;
623   Diag(Decl->getLocation(), diag::err_section_conflict) << Decl << Section;
624   if (Section.Decl)
625     Diag(Section.Decl->getLocation(), diag::note_declared_at)
626         << Section.Decl->getName();
627   if (PragmaLocation.isValid())
628     Diag(PragmaLocation, diag::note_pragma_entered_here);
629   if (Section.PragmaSectionLocation.isValid())
630     Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here);
631   return true;
632 }
633 
634 bool Sema::UnifySection(StringRef SectionName,
635                         int SectionFlags,
636                         SourceLocation PragmaSectionLocation) {
637   auto SectionIt = Context.SectionInfos.find(SectionName);
638   if (SectionIt != Context.SectionInfos.end()) {
639     const auto &Section = SectionIt->second;
640     if (Section.SectionFlags == SectionFlags)
641       return false;
642     if (!(Section.SectionFlags & ASTContext::PSF_Implicit)) {
643       Diag(PragmaSectionLocation, diag::err_section_conflict)
644           << "this" << Section;
645       if (Section.Decl)
646         Diag(Section.Decl->getLocation(), diag::note_declared_at)
647             << Section.Decl->getName();
648       if (Section.PragmaSectionLocation.isValid())
649         Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here);
650       return true;
651     }
652   }
653   Context.SectionInfos[SectionName] =
654       ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
655   return false;
656 }
657 
658 /// Called on well formed \#pragma bss_seg().
659 void Sema::ActOnPragmaMSSeg(SourceLocation PragmaLocation,
660                             PragmaMsStackAction Action,
661                             llvm::StringRef StackSlotLabel,
662                             StringLiteral *SegmentName,
663                             llvm::StringRef PragmaName) {
664   PragmaStack<StringLiteral *> *Stack =
665     llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
666         .Case("data_seg", &DataSegStack)
667         .Case("bss_seg", &BSSSegStack)
668         .Case("const_seg", &ConstSegStack)
669         .Case("code_seg", &CodeSegStack);
670   if (Action & PSK_Pop && Stack->Stack.empty())
671     Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
672         << "stack empty";
673   if (SegmentName) {
674     if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString()))
675       return;
676 
677     if (SegmentName->getString() == ".drectve" &&
678         Context.getTargetInfo().getCXXABI().isMicrosoft())
679       Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName;
680   }
681 
682   Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
683 }
684 
685 /// Called on well formed \#pragma bss_seg().
686 void Sema::ActOnPragmaMSSection(SourceLocation PragmaLocation,
687                                 int SectionFlags, StringLiteral *SegmentName) {
688   UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
689 }
690 
691 void Sema::ActOnPragmaMSInitSeg(SourceLocation PragmaLocation,
692                                 StringLiteral *SegmentName) {
693   // There's no stack to maintain, so we just have a current section.  When we
694   // see the default section, reset our current section back to null so we stop
695   // tacking on unnecessary attributes.
696   CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
697   CurInitSegLoc = PragmaLocation;
698 }
699 
700 void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
701                              SourceLocation PragmaLoc) {
702 
703   IdentifierInfo *Name = IdTok.getIdentifierInfo();
704   LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
705   LookupParsedName(Lookup, curScope, nullptr, true);
706 
707   if (Lookup.empty()) {
708     Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
709       << Name << SourceRange(IdTok.getLocation());
710     return;
711   }
712 
713   VarDecl *VD = Lookup.getAsSingle<VarDecl>();
714   if (!VD) {
715     Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
716       << Name << SourceRange(IdTok.getLocation());
717     return;
718   }
719 
720   // Warn if this was used before being marked unused.
721   if (VD->isUsed())
722     Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
723 
724   VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation(),
725                                          AttributeCommonInfo::AS_Pragma,
726                                          UnusedAttr::GNU_unused));
727 }
728 
729 void Sema::AddCFAuditedAttribute(Decl *D) {
730   IdentifierInfo *Ident;
731   SourceLocation Loc;
732   std::tie(Ident, Loc) = PP.getPragmaARCCFCodeAuditedInfo();
733   if (!Loc.isValid()) return;
734 
735   // Don't add a redundant or conflicting attribute.
736   if (D->hasAttr<CFAuditedTransferAttr>() ||
737       D->hasAttr<CFUnknownTransferAttr>())
738     return;
739 
740   AttributeCommonInfo Info(Ident, SourceRange(Loc),
741                            AttributeCommonInfo::AS_Pragma);
742   D->addAttr(CFAuditedTransferAttr::CreateImplicit(Context, Info));
743 }
744 
745 namespace {
746 
747 Optional<attr::SubjectMatchRule>
748 getParentAttrMatcherRule(attr::SubjectMatchRule Rule) {
749   using namespace attr;
750   switch (Rule) {
751   default:
752     return None;
753 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
754 #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated)    \
755   case Value:                                                                  \
756     return Parent;
757 #include "clang/Basic/AttrSubMatchRulesList.inc"
758   }
759 }
760 
761 bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) {
762   using namespace attr;
763   switch (Rule) {
764   default:
765     return false;
766 #define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
767 #define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated)    \
768   case Value:                                                                  \
769     return IsNegated;
770 #include "clang/Basic/AttrSubMatchRulesList.inc"
771   }
772 }
773 
774 CharSourceRange replacementRangeForListElement(const Sema &S,
775                                                SourceRange Range) {
776   // Make sure that the ',' is removed as well.
777   SourceLocation AfterCommaLoc = Lexer::findLocationAfterToken(
778       Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(),
779       /*SkipTrailingWhitespaceAndNewLine=*/false);
780   if (AfterCommaLoc.isValid())
781     return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc);
782   else
783     return CharSourceRange::getTokenRange(Range);
784 }
785 
786 std::string
787 attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) {
788   std::string Result;
789   llvm::raw_string_ostream OS(Result);
790   for (const auto &I : llvm::enumerate(Rules)) {
791     if (I.index())
792       OS << (I.index() == Rules.size() - 1 ? ", and " : ", ");
793     OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'";
794   }
795   return Result;
796 }
797 
798 } // end anonymous namespace
799 
800 void Sema::ActOnPragmaAttributeAttribute(
801     ParsedAttr &Attribute, SourceLocation PragmaLoc,
802     attr::ParsedSubjectMatchRuleSet Rules) {
803   Attribute.setIsPragmaClangAttribute();
804   SmallVector<attr::SubjectMatchRule, 4> SubjectMatchRules;
805   // Gather the subject match rules that are supported by the attribute.
806   SmallVector<std::pair<attr::SubjectMatchRule, bool>, 4>
807       StrictSubjectMatchRuleSet;
808   Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet);
809 
810   // Figure out which subject matching rules are valid.
811   if (StrictSubjectMatchRuleSet.empty()) {
812     // Check for contradicting match rules. Contradicting match rules are
813     // either:
814     //  - a top-level rule and one of its sub-rules. E.g. variable and
815     //    variable(is_parameter).
816     //  - a sub-rule and a sibling that's negated. E.g.
817     //    variable(is_thread_local) and variable(unless(is_parameter))
818     llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2>
819         RulesToFirstSpecifiedNegatedSubRule;
820     for (const auto &Rule : Rules) {
821       attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
822       Optional<attr::SubjectMatchRule> ParentRule =
823           getParentAttrMatcherRule(MatchRule);
824       if (!ParentRule)
825         continue;
826       auto It = Rules.find(*ParentRule);
827       if (It != Rules.end()) {
828         // A sub-rule contradicts a parent rule.
829         Diag(Rule.second.getBegin(),
830              diag::err_pragma_attribute_matcher_subrule_contradicts_rule)
831             << attr::getSubjectMatchRuleSpelling(MatchRule)
832             << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second
833             << FixItHint::CreateRemoval(
834                    replacementRangeForListElement(*this, Rule.second));
835         // Keep going without removing this rule as it won't change the set of
836         // declarations that receive the attribute.
837         continue;
838       }
839       if (isNegatedAttrMatcherSubRule(MatchRule))
840         RulesToFirstSpecifiedNegatedSubRule.insert(
841             std::make_pair(*ParentRule, Rule));
842     }
843     bool IgnoreNegatedSubRules = false;
844     for (const auto &Rule : Rules) {
845       attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
846       Optional<attr::SubjectMatchRule> ParentRule =
847           getParentAttrMatcherRule(MatchRule);
848       if (!ParentRule)
849         continue;
850       auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule);
851       if (It != RulesToFirstSpecifiedNegatedSubRule.end() &&
852           It->second != Rule) {
853         // Negated sub-rule contradicts another sub-rule.
854         Diag(
855             It->second.second.getBegin(),
856             diag::
857                 err_pragma_attribute_matcher_negated_subrule_contradicts_subrule)
858             << attr::getSubjectMatchRuleSpelling(
859                    attr::SubjectMatchRule(It->second.first))
860             << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second
861             << FixItHint::CreateRemoval(
862                    replacementRangeForListElement(*this, It->second.second));
863         // Keep going but ignore all of the negated sub-rules.
864         IgnoreNegatedSubRules = true;
865         RulesToFirstSpecifiedNegatedSubRule.erase(It);
866       }
867     }
868 
869     if (!IgnoreNegatedSubRules) {
870       for (const auto &Rule : Rules)
871         SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
872     } else {
873       for (const auto &Rule : Rules) {
874         if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first)))
875           SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
876       }
877     }
878     Rules.clear();
879   } else {
880     // Each rule in Rules must be a strict subset of the attribute's
881     // SubjectMatch rules.  I.e. we're allowed to use
882     // `apply_to=variables(is_global)` on an attrubute with SubjectList<[Var]>,
883     // but should not allow `apply_to=variables` on an attribute which has
884     // `SubjectList<[GlobalVar]>`.
885     for (const auto &StrictRule : StrictSubjectMatchRuleSet) {
886       // First, check for exact match.
887       if (Rules.erase(StrictRule.first)) {
888         // Add the rule to the set of attribute receivers only if it's supported
889         // in the current language mode.
890         if (StrictRule.second)
891           SubjectMatchRules.push_back(StrictRule.first);
892       }
893     }
894     // Check remaining rules for subset matches.
895     auto RulesToCheck = Rules;
896     for (const auto &Rule : RulesToCheck) {
897       attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
898       if (auto ParentRule = getParentAttrMatcherRule(MatchRule)) {
899         if (llvm::any_of(StrictSubjectMatchRuleSet,
900                          [ParentRule](const auto &StrictRule) {
901                            return StrictRule.first == *ParentRule &&
902                                   StrictRule.second; // IsEnabled
903                          })) {
904           SubjectMatchRules.push_back(MatchRule);
905           Rules.erase(MatchRule);
906         }
907       }
908     }
909   }
910 
911   if (!Rules.empty()) {
912     auto Diagnostic =
913         Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers)
914         << Attribute;
915     SmallVector<attr::SubjectMatchRule, 2> ExtraRules;
916     for (const auto &Rule : Rules) {
917       ExtraRules.push_back(attr::SubjectMatchRule(Rule.first));
918       Diagnostic << FixItHint::CreateRemoval(
919           replacementRangeForListElement(*this, Rule.second));
920     }
921     Diagnostic << attrMatcherRuleListToString(ExtraRules);
922   }
923 
924   if (PragmaAttributeStack.empty()) {
925     Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push);
926     return;
927   }
928 
929   PragmaAttributeStack.back().Entries.push_back(
930       {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false});
931 }
932 
933 void Sema::ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc,
934                                          const IdentifierInfo *Namespace) {
935   PragmaAttributeStack.emplace_back();
936   PragmaAttributeStack.back().Loc = PragmaLoc;
937   PragmaAttributeStack.back().Namespace = Namespace;
938 }
939 
940 void Sema::ActOnPragmaAttributePop(SourceLocation PragmaLoc,
941                                    const IdentifierInfo *Namespace) {
942   if (PragmaAttributeStack.empty()) {
943     Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
944     return;
945   }
946 
947   // Dig back through the stack trying to find the most recently pushed group
948   // that in Namespace. Note that this works fine if no namespace is present,
949   // think of push/pops without namespaces as having an implicit "nullptr"
950   // namespace.
951   for (size_t Index = PragmaAttributeStack.size(); Index;) {
952     --Index;
953     if (PragmaAttributeStack[Index].Namespace == Namespace) {
954       for (const PragmaAttributeEntry &Entry :
955            PragmaAttributeStack[Index].Entries) {
956         if (!Entry.IsUsed) {
957           assert(Entry.Attribute && "Expected an attribute");
958           Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused)
959               << *Entry.Attribute;
960           Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here);
961         }
962       }
963       PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index);
964       return;
965     }
966   }
967 
968   if (Namespace)
969     Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch)
970         << 0 << Namespace->getName();
971   else
972     Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
973 }
974 
975 void Sema::AddPragmaAttributes(Scope *S, Decl *D) {
976   if (PragmaAttributeStack.empty())
977     return;
978   for (auto &Group : PragmaAttributeStack) {
979     for (auto &Entry : Group.Entries) {
980       ParsedAttr *Attribute = Entry.Attribute;
981       assert(Attribute && "Expected an attribute");
982       assert(Attribute->isPragmaClangAttribute() &&
983              "expected #pragma clang attribute");
984 
985       // Ensure that the attribute can be applied to the given declaration.
986       bool Applies = false;
987       for (const auto &Rule : Entry.MatchRules) {
988         if (Attribute->appliesToDecl(D, Rule)) {
989           Applies = true;
990           break;
991         }
992       }
993       if (!Applies)
994         continue;
995       Entry.IsUsed = true;
996       PragmaAttributeCurrentTargetDecl = D;
997       ParsedAttributesView Attrs;
998       Attrs.addAtEnd(Attribute);
999       ProcessDeclAttributeList(S, D, Attrs);
1000       PragmaAttributeCurrentTargetDecl = nullptr;
1001     }
1002   }
1003 }
1004 
1005 void Sema::PrintPragmaAttributeInstantiationPoint() {
1006   assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration");
1007   Diags.Report(PragmaAttributeCurrentTargetDecl->getBeginLoc(),
1008                diag::note_pragma_attribute_applied_decl_here);
1009 }
1010 
1011 void Sema::DiagnoseUnterminatedPragmaAttribute() {
1012   if (PragmaAttributeStack.empty())
1013     return;
1014   Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof);
1015 }
1016 
1017 void Sema::ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc) {
1018   if(On)
1019     OptimizeOffPragmaLocation = SourceLocation();
1020   else
1021     OptimizeOffPragmaLocation = PragmaLoc;
1022 }
1023 
1024 void Sema::AddRangeBasedOptnone(FunctionDecl *FD) {
1025   // In the future, check other pragmas if they're implemented (e.g. pragma
1026   // optimize 0 will probably map to this functionality too).
1027   if(OptimizeOffPragmaLocation.isValid())
1028     AddOptnoneAttributeIfNoConflicts(FD, OptimizeOffPragmaLocation);
1029 }
1030 
1031 void Sema::AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD,
1032                                             SourceLocation Loc) {
1033   // Don't add a conflicting attribute. No diagnostic is needed.
1034   if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
1035     return;
1036 
1037   // Add attributes only if required. Optnone requires noinline as well, but if
1038   // either is already present then don't bother adding them.
1039   if (!FD->hasAttr<OptimizeNoneAttr>())
1040     FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
1041   if (!FD->hasAttr<NoInlineAttr>())
1042     FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
1043 }
1044 
1045 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
1046 enum : unsigned { NoVisibility = ~0U };
1047 
1048 void Sema::AddPushedVisibilityAttribute(Decl *D) {
1049   if (!VisContext)
1050     return;
1051 
1052   NamedDecl *ND = dyn_cast<NamedDecl>(D);
1053   if (ND && ND->getExplicitVisibility(NamedDecl::VisibilityForValue))
1054     return;
1055 
1056   VisStack *Stack = static_cast<VisStack*>(VisContext);
1057   unsigned rawType = Stack->back().first;
1058   if (rawType == NoVisibility) return;
1059 
1060   VisibilityAttr::VisibilityType type
1061     = (VisibilityAttr::VisibilityType) rawType;
1062   SourceLocation loc = Stack->back().second;
1063 
1064   D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
1065 }
1066 
1067 /// FreeVisContext - Deallocate and null out VisContext.
1068 void Sema::FreeVisContext() {
1069   delete static_cast<VisStack*>(VisContext);
1070   VisContext = nullptr;
1071 }
1072 
1073 static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
1074   // Put visibility on stack.
1075   if (!S.VisContext)
1076     S.VisContext = new VisStack;
1077 
1078   VisStack *Stack = static_cast<VisStack*>(S.VisContext);
1079   Stack->push_back(std::make_pair(type, loc));
1080 }
1081 
1082 void Sema::ActOnPragmaVisibility(const IdentifierInfo* VisType,
1083                                  SourceLocation PragmaLoc) {
1084   if (VisType) {
1085     // Compute visibility to use.
1086     VisibilityAttr::VisibilityType T;
1087     if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
1088       Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
1089       return;
1090     }
1091     PushPragmaVisibility(*this, T, PragmaLoc);
1092   } else {
1093     PopPragmaVisibility(false, PragmaLoc);
1094   }
1095 }
1096 
1097 void Sema::ActOnPragmaFPContract(SourceLocation Loc,
1098                                  LangOptions::FPModeKind FPC) {
1099   FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1100   switch (FPC) {
1101   case LangOptions::FPM_On:
1102     NewFPFeatures.setAllowFPContractWithinStatement();
1103     break;
1104   case LangOptions::FPM_Fast:
1105     NewFPFeatures.setAllowFPContractAcrossStatement();
1106     break;
1107   case LangOptions::FPM_Off:
1108     NewFPFeatures.setDisallowFPContract();
1109     break;
1110   case LangOptions::FPM_FastHonorPragmas:
1111     llvm_unreachable("Should not happen");
1112   }
1113   FpPragmaStack.Act(Loc, Sema::PSK_Set, StringRef(), NewFPFeatures);
1114   CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1115 }
1116 
1117 void Sema::ActOnPragmaFPReassociate(SourceLocation Loc, bool IsEnabled) {
1118   FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1119   NewFPFeatures.setAllowFPReassociateOverride(IsEnabled);
1120   FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1121   CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1122 }
1123 
1124 void Sema::setRoundingMode(SourceLocation Loc, llvm::RoundingMode FPR) {
1125   // C2x: 7.6.2p3  If the FE_DYNAMIC mode is specified and FENV_ACCESS is "off",
1126   // the translator may assume that the default rounding mode is in effect.
1127   if (FPR == llvm::RoundingMode::Dynamic &&
1128       !CurFPFeatures.getAllowFEnvAccess() &&
1129       CurFPFeatures.getFPExceptionMode() == LangOptions::FPE_Ignore)
1130     FPR = llvm::RoundingMode::NearestTiesToEven;
1131 
1132   FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1133   NewFPFeatures.setRoundingModeOverride(FPR);
1134   FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1135   CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1136 }
1137 
1138 void Sema::setExceptionMode(SourceLocation Loc,
1139                             LangOptions::FPExceptionModeKind FPE) {
1140   FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1141   NewFPFeatures.setFPExceptionModeOverride(FPE);
1142   FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1143   CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1144 }
1145 
1146 void Sema::ActOnPragmaFEnvAccess(SourceLocation Loc, bool IsEnabled) {
1147   FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1148   auto LO = getLangOpts();
1149   if (IsEnabled) {
1150     // Verify Microsoft restriction:
1151     // You can't enable fenv_access unless precise semantics are enabled.
1152     // Precise semantics can be enabled either by the float_control
1153     // pragma, or by using the /fp:precise or /fp:strict compiler options
1154     if (!isPreciseFPEnabled())
1155       Diag(Loc, diag::err_pragma_fenv_requires_precise);
1156     NewFPFeatures.setAllowFEnvAccessOverride(true);
1157     // Enabling FENV access sets the RoundingMode to Dynamic.
1158     // and ExceptionBehavior to Strict
1159     NewFPFeatures.setRoundingModeOverride(llvm::RoundingMode::Dynamic);
1160     NewFPFeatures.setFPExceptionModeOverride(LangOptions::FPE_Strict);
1161   } else {
1162     NewFPFeatures.setAllowFEnvAccessOverride(false);
1163   }
1164   FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1165   CurFPFeatures = NewFPFeatures.applyOverrides(LO);
1166 }
1167 
1168 void Sema::ActOnPragmaFPExceptions(SourceLocation Loc,
1169                                    LangOptions::FPExceptionModeKind FPE) {
1170   setExceptionMode(Loc, FPE);
1171 }
1172 
1173 void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
1174                                        SourceLocation Loc) {
1175   // Visibility calculations will consider the namespace's visibility.
1176   // Here we just want to note that we're in a visibility context
1177   // which overrides any enclosing #pragma context, but doesn't itself
1178   // contribute visibility.
1179   PushPragmaVisibility(*this, NoVisibility, Loc);
1180 }
1181 
1182 void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
1183   if (!VisContext) {
1184     Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
1185     return;
1186   }
1187 
1188   // Pop visibility from stack
1189   VisStack *Stack = static_cast<VisStack*>(VisContext);
1190 
1191   const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
1192   bool StartsWithPragma = Back->first != NoVisibility;
1193   if (StartsWithPragma && IsNamespaceEnd) {
1194     Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
1195     Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
1196 
1197     // For better error recovery, eat all pushes inside the namespace.
1198     do {
1199       Stack->pop_back();
1200       Back = &Stack->back();
1201       StartsWithPragma = Back->first != NoVisibility;
1202     } while (StartsWithPragma);
1203   } else if (!StartsWithPragma && !IsNamespaceEnd) {
1204     Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
1205     Diag(Back->second, diag::note_surrounding_namespace_starts_here);
1206     return;
1207   }
1208 
1209   Stack->pop_back();
1210   // To simplify the implementation, never keep around an empty stack.
1211   if (Stack->empty())
1212     FreeVisContext();
1213 }
1214 
1215 template <typename Ty>
1216 static bool checkCommonAttributeFeatures(Sema& S, const Ty *Node,
1217                                          const ParsedAttr& A) {
1218   // Several attributes carry different semantics than the parsing requires, so
1219   // those are opted out of the common argument checks.
1220   //
1221   // We also bail on unknown and ignored attributes because those are handled
1222   // as part of the target-specific handling logic.
1223   if (A.getKind() == ParsedAttr::UnknownAttribute)
1224     return false;
1225   // Check whether the attribute requires specific language extensions to be
1226   // enabled.
1227   if (!A.diagnoseLangOpts(S))
1228     return true;
1229   // Check whether the attribute appertains to the given subject.
1230   if (!A.diagnoseAppertainsTo(S, Node))
1231     return true;
1232   // Check whether the attribute is mutually exclusive with other attributes
1233   // that have already been applied to the declaration.
1234   if (!A.diagnoseMutualExclusion(S, Node))
1235     return true;
1236   // Check whether the attribute exists in the target architecture.
1237   if (S.CheckAttrTarget(A))
1238     return true;
1239 
1240   if (A.hasCustomParsing())
1241     return false;
1242 
1243   if (A.getMinArgs() == A.getMaxArgs()) {
1244     // If there are no optional arguments, then checking for the argument count
1245     // is trivial.
1246     if (!A.checkExactlyNumArgs(S, A.getMinArgs()))
1247       return true;
1248   } else {
1249     // There are optional arguments, so checking is slightly more involved.
1250     if (A.getMinArgs() && !A.checkAtLeastNumArgs(S, A.getMinArgs()))
1251       return true;
1252     else if (!A.hasVariadicArg() && A.getMaxArgs() &&
1253              !A.checkAtMostNumArgs(S, A.getMaxArgs()))
1254       return true;
1255   }
1256 
1257   return false;
1258 }
1259 
1260 bool Sema::checkCommonAttributeFeatures(const Decl *D, const ParsedAttr &A) {
1261   return ::checkCommonAttributeFeatures(*this, D, A);
1262 }
1263 bool Sema::checkCommonAttributeFeatures(const Stmt *S, const ParsedAttr &A) {
1264   return ::checkCommonAttributeFeatures(*this, S, A);
1265 }
1266