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