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