clang 20.0.0git
SemaAttr.cpp
Go to the documentation of this file.
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 "CheckExprLifetime.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/Expr.h"
20#include "clang/Sema/Lookup.h"
21#include <optional>
22using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Pragma 'pack' and 'options align'
26//===----------------------------------------------------------------------===//
27
29 StringRef SlotLabel,
30 bool ShouldAct)
31 : S(S), SlotLabel(SlotLabel), ShouldAct(ShouldAct) {
32 if (ShouldAct) {
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);
39 }
40}
41
43 if (ShouldAct) {
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);
50 }
51}
52
54 AlignPackInfo InfoVal = AlignPackStack.CurrentValue;
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
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(
99}
100
101template <typename Attribute>
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
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 const 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 const 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
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
165}
166
168 static const 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 const llvm::StringSet<> StdPointers{
193 "basic_string_view",
194 "reference_wrapper",
195 "regex_iterator",
196 "span",
197 };
198
199 if (!Record->getIdentifier())
200 return;
201
202 // Handle classes that directly appear in std namespace.
203 if (Record->isInStdNamespace()) {
204 if (Record->hasAttr<OwnerAttr>() || Record->hasAttr<PointerAttr>())
205 return;
206
207 if (StdOwners.count(Record->getName()))
208 addGslOwnerPointerAttributeIfNotExisting<OwnerAttr>(Context, Record);
209 else if (StdPointers.count(Record->getName()))
210 addGslOwnerPointerAttributeIfNotExisting<PointerAttr>(Context, Record);
211
212 return;
213 }
214
215 // Handle nested classes that could be a gsl::Pointer.
217}
218
220 if (FD->getNumParams() == 0)
221 return;
222
223 if (unsigned BuiltinID = FD->getBuiltinID()) {
224 // Add lifetime attribute to std::move, std::fowrard et al.
225 switch (BuiltinID) {
226 case Builtin::BIaddressof:
227 case Builtin::BI__addressof:
228 case Builtin::BI__builtin_addressof:
229 case Builtin::BIas_const:
230 case Builtin::BIforward:
231 case Builtin::BIforward_like:
232 case Builtin::BImove:
233 case Builtin::BImove_if_noexcept:
234 if (ParmVarDecl *P = FD->getParamDecl(0u);
235 !P->hasAttr<LifetimeBoundAttr>())
236 P->addAttr(
237 LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
238 break;
239 default:
240 break;
241 }
242 return;
243 }
244 if (auto *CMD = dyn_cast<CXXMethodDecl>(FD)) {
245 const auto *CRD = CMD->getParent();
246 if (!CRD->isInStdNamespace() || !CRD->getIdentifier())
247 return;
248
249 if (isa<CXXConstructorDecl>(CMD)) {
250 auto *Param = CMD->getParamDecl(0);
251 if (Param->hasAttr<LifetimeBoundAttr>())
252 return;
253 if (CRD->getName() == "basic_string_view" &&
254 Param->getType()->isPointerType()) {
255 // construct from a char array pointed by a pointer.
256 // basic_string_view(const CharT* s);
257 // basic_string_view(const CharT* s, size_type count);
258 Param->addAttr(
259 LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
260 } else if (CRD->getName() == "span") {
261 // construct from a reference of array.
262 // span(std::type_identity_t<element_type> (&arr)[N]);
263 const auto *LRT = Param->getType()->getAs<LValueReferenceType>();
264 if (LRT && LRT->getPointeeType().IgnoreParens()->isArrayType())
265 Param->addAttr(
266 LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
267 }
268 }
269 }
270}
271
273 auto *MD = dyn_cast_if_present<CXXMethodDecl>(FD);
274 if (!MD || !MD->getParent()->isInStdNamespace())
275 return;
276 auto Annotate = [this](const FunctionDecl *MD) {
277 // Do not infer if any parameter is explicitly annotated.
278 for (ParmVarDecl *PVD : MD->parameters())
279 if (PVD->hasAttr<LifetimeCaptureByAttr>())
280 return;
281 for (ParmVarDecl *PVD : MD->parameters()) {
282 // Methods in standard containers that capture values typically accept
283 // reference-type parameters, e.g., `void push_back(const T& value)`.
284 // We only apply the lifetime_capture_by attribute to parameters of
285 // pointer-like reference types (`const T&`, `T&&`).
286 if (PVD->getType()->isReferenceType() &&
287 sema::isPointerLikeType(PVD->getType().getNonReferenceType())) {
288 int CaptureByThis[] = {LifetimeCaptureByAttr::THIS};
289 PVD->addAttr(
290 LifetimeCaptureByAttr::CreateImplicit(Context, CaptureByThis, 1));
291 }
292 }
293 };
294
295 if (!MD->getIdentifier()) {
296 static const llvm::StringSet<> MapLikeContainer{
297 "map",
298 "multimap",
299 "unordered_map",
300 "unordered_multimap",
301 };
302 // Infer for the map's operator []:
303 // std::map<string_view, ...> m;
304 // m[ReturnString(..)] = ...; // !dangling references in m.
305 if (MD->getOverloadedOperator() == OO_Subscript &&
306 MapLikeContainer.contains(MD->getParent()->getName()))
307 Annotate(MD);
308 return;
309 }
310 static const llvm::StringSet<> CapturingMethods{"insert", "push",
311 "push_front", "push_back"};
312 if (!CapturingMethods.contains(MD->getName()))
313 return;
314 Annotate(MD);
315}
316
318 static const llvm::StringSet<> Nullable{
319 "auto_ptr", "shared_ptr", "unique_ptr", "exception_ptr",
320 "coroutine_handle", "function", "move_only_function",
321 };
322
323 if (CRD->isInStdNamespace() && Nullable.count(CRD->getName()) &&
324 !CRD->hasAttr<TypeNullableAttr>())
325 for (Decl *Redecl : CRD->redecls())
326 Redecl->addAttr(TypeNullableAttr::CreateImplicit(Context));
327}
328
330 SourceLocation PragmaLoc) {
333
334 switch (Kind) {
335 // For most of the platforms we support, native and natural are the same.
336 // With XL, native is the same as power, natural means something else.
337 case POAK_Native:
338 case POAK_Power:
339 Action = Sema::PSK_Push_Set;
340 break;
341 case POAK_Natural:
342 Action = Sema::PSK_Push_Set;
343 ModeVal = AlignPackInfo::Natural;
344 break;
345
346 // Note that '#pragma options align=packed' is not equivalent to attribute
347 // packed, it has a different precedence relative to attribute aligned.
348 case POAK_Packed:
349 Action = Sema::PSK_Push_Set;
350 ModeVal = AlignPackInfo::Packed;
351 break;
352
353 case POAK_Mac68k:
354 // Check if the target supports this.
356 Diag(PragmaLoc, diag::err_pragma_options_align_mac68k_target_unsupported);
357 return;
358 }
359 Action = Sema::PSK_Push_Set;
360 ModeVal = AlignPackInfo::Mac68k;
361 break;
362 case POAK_Reset:
363 // Reset just pops the top of the stack, or resets the current alignment to
364 // default.
365 Action = Sema::PSK_Pop;
366 if (AlignPackStack.Stack.empty()) {
367 if (AlignPackStack.CurrentValue.getAlignMode() != AlignPackInfo::Native ||
368 AlignPackStack.CurrentValue.IsPackAttr()) {
369 Action = Sema::PSK_Reset;
370 } else {
371 Diag(PragmaLoc, diag::warn_pragma_options_align_reset_failed)
372 << "stack empty";
373 return;
374 }
375 }
376 break;
377 }
378
379 AlignPackInfo Info(ModeVal, getLangOpts().XLPragmaPack);
380
381 AlignPackStack.Act(PragmaLoc, Action, StringRef(), Info);
382}
383
387 StringRef SecName) {
388 PragmaClangSection *CSec;
389 int SectionFlags = ASTContext::PSF_Read;
390 switch (SecKind) {
392 CSec = &PragmaClangBSSSection;
394 break;
397 SectionFlags |= ASTContext::PSF_Write;
398 break;
401 break;
404 break;
407 SectionFlags |= ASTContext::PSF_Execute;
408 break;
409 default:
410 llvm_unreachable("invalid clang section kind");
411 }
412
414 CSec->Valid = false;
415 return;
416 }
417
418 if (llvm::Error E = isValidSectionSpecifier(SecName)) {
419 Diag(PragmaLoc, diag::err_pragma_section_invalid_for_target)
420 << toString(std::move(E));
421 CSec->Valid = false;
422 return;
423 }
424
425 if (UnifySection(SecName, SectionFlags, PragmaLoc))
426 return;
427
428 CSec->Valid = true;
429 CSec->SectionName = std::string(SecName);
430 CSec->PragmaLocation = PragmaLoc;
431}
432
434 StringRef SlotLabel, Expr *alignment) {
435 bool IsXLPragma = getLangOpts().XLPragmaPack;
436 // XL pragma pack does not support identifier syntax.
437 if (IsXLPragma && !SlotLabel.empty()) {
438 Diag(PragmaLoc, diag::err_pragma_pack_identifer_not_supported);
439 return;
440 }
441
442 const AlignPackInfo CurVal = AlignPackStack.CurrentValue;
443 Expr *Alignment = static_cast<Expr *>(alignment);
444
445 // If specified then alignment must be a "small" power of two.
446 unsigned AlignmentVal = 0;
447 AlignPackInfo::Mode ModeVal = CurVal.getAlignMode();
448
449 if (Alignment) {
450 std::optional<llvm::APSInt> Val;
451 Val = Alignment->getIntegerConstantExpr(Context);
452
453 // pack(0) is like pack(), which just works out since that is what
454 // we use 0 for in PackAttr.
455 if (Alignment->isTypeDependent() || !Val ||
456 !(*Val == 0 || Val->isPowerOf2()) || Val->getZExtValue() > 16) {
457 Diag(PragmaLoc, diag::warn_pragma_pack_invalid_alignment);
458 return; // Ignore
459 }
460
461 if (IsXLPragma && *Val == 0) {
462 // pack(0) does not work out with XL.
463 Diag(PragmaLoc, diag::err_pragma_pack_invalid_alignment);
464 return; // Ignore
465 }
466
467 AlignmentVal = (unsigned)Val->getZExtValue();
468 }
469
470 if (Action == Sema::PSK_Show) {
471 // Show the current alignment, making sure to show the right value
472 // for the default.
473 // FIXME: This should come from the target.
474 AlignmentVal = CurVal.IsPackSet() ? CurVal.getPackNumber() : 8;
475 if (ModeVal == AlignPackInfo::Mac68k &&
476 (IsXLPragma || CurVal.IsAlignAttr()))
477 Diag(PragmaLoc, diag::warn_pragma_pack_show) << "mac68k";
478 else
479 Diag(PragmaLoc, diag::warn_pragma_pack_show) << AlignmentVal;
480 }
481
482 // MSDN, C/C++ Preprocessor Reference > Pragma Directives > pack:
483 // "#pragma pack(pop, identifier, n) is undefined"
484 if (Action & Sema::PSK_Pop) {
485 if (Alignment && !SlotLabel.empty())
486 Diag(PragmaLoc, diag::warn_pragma_pack_pop_identifier_and_alignment);
487 if (AlignPackStack.Stack.empty()) {
488 assert(CurVal.getAlignMode() == AlignPackInfo::Native &&
489 "Empty pack stack can only be at Native alignment mode.");
490 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "pack" << "stack empty";
491 }
492 }
493
494 AlignPackInfo Info(ModeVal, AlignmentVal, IsXLPragma);
495
496 AlignPackStack.Act(PragmaLoc, Action, SlotLabel, Info);
497}
498
502 for (unsigned Idx = 0; Idx < Args.size(); Idx++) {
503 Expr *&E = Args.begin()[Idx];
504 assert(E && "error are handled before");
505 if (E->isValueDependent() || E->isTypeDependent())
506 continue;
507
508 // FIXME: Use DefaultFunctionArrayLValueConversion() in place of the logic
509 // that adds implicit casts here.
510 if (E->getType()->isArrayType())
512 clang::CK_ArrayToPointerDecay)
513 .get();
514 if (E->getType()->isFunctionType())
517 clang::CK_FunctionToPointerDecay, E, nullptr,
519 if (E->isLValue())
521 clang::CK_LValueToRValue, E, nullptr,
523
524 Expr::EvalResult Eval;
525 Notes.clear();
526 Eval.Diag = &Notes;
527
528 bool Result = E->EvaluateAsConstantExpr(Eval, Context);
529
530 /// Result means the expression can be folded to a constant.
531 /// Note.empty() means the expression is a valid constant expression in the
532 /// current language mode.
533 if (!Result || !Notes.empty()) {
534 Diag(E->getBeginLoc(), diag::err_attribute_argument_n_type)
535 << CI << (Idx + 1) << AANT_ArgumentConstantExpr;
536 for (auto &Note : Notes)
537 Diag(Note.first, Note.second);
538 return false;
539 }
540 assert(Eval.Val.hasValue());
542 }
543
544 return true;
545}
546
548 SourceLocation IncludeLoc) {
550 SourceLocation PrevLocation = AlignPackStack.CurrentPragmaLocation;
551 // Warn about non-default alignment at #includes (without redundant
552 // warnings for the same directive in nested includes).
553 // The warning is delayed until the end of the file to avoid warnings
554 // for files that don't have any records that are affected by the modified
555 // alignment.
556 bool HasNonDefaultValue =
557 AlignPackStack.hasValue() &&
558 (AlignPackIncludeStack.empty() ||
559 AlignPackIncludeStack.back().CurrentPragmaLocation != PrevLocation);
560 AlignPackIncludeStack.push_back(
561 {AlignPackStack.CurrentValue,
562 AlignPackStack.hasValue() ? PrevLocation : SourceLocation(),
563 HasNonDefaultValue, /*ShouldWarnOnInclude*/ false});
564 return;
565 }
566
568 "invalid kind");
569 AlignPackIncludeState PrevAlignPackState =
570 AlignPackIncludeStack.pop_back_val();
571 // FIXME: AlignPackStack may contain both #pragma align and #pragma pack
572 // information, diagnostics below might not be accurate if we have mixed
573 // pragmas.
574 if (PrevAlignPackState.ShouldWarnOnInclude) {
575 // Emit the delayed non-default alignment at #include warning.
576 Diag(IncludeLoc, diag::warn_pragma_pack_non_default_at_include);
577 Diag(PrevAlignPackState.CurrentPragmaLocation, diag::note_pragma_pack_here);
578 }
579 // Warn about modified alignment after #includes.
580 if (PrevAlignPackState.CurrentValue != AlignPackStack.CurrentValue) {
581 Diag(IncludeLoc, diag::warn_pragma_pack_modified_after_include);
582 Diag(AlignPackStack.CurrentPragmaLocation, diag::note_pragma_pack_here);
583 }
584}
585
587 if (AlignPackStack.Stack.empty())
588 return;
589 bool IsInnermost = true;
590
591 // FIXME: AlignPackStack may contain both #pragma align and #pragma pack
592 // information, diagnostics below might not be accurate if we have mixed
593 // pragmas.
594 for (const auto &StackSlot : llvm::reverse(AlignPackStack.Stack)) {
595 Diag(StackSlot.PragmaPushLocation, diag::warn_pragma_pack_no_pop_eof);
596 // The user might have already reset the alignment, so suggest replacing
597 // the reset with a pop.
598 if (IsInnermost &&
599 AlignPackStack.CurrentValue == AlignPackStack.DefaultValue) {
600 auto DB = Diag(AlignPackStack.CurrentPragmaLocation,
601 diag::note_pragma_pack_pop_instead_reset);
602 SourceLocation FixItLoc =
603 Lexer::findLocationAfterToken(AlignPackStack.CurrentPragmaLocation,
604 tok::l_paren, SourceMgr, LangOpts,
605 /*SkipTrailing=*/false);
606 if (FixItLoc.isValid())
607 DB << FixItHint::CreateInsertion(FixItLoc, "pop");
608 }
609 IsInnermost = false;
610 }
611}
612
614 MSStructPragmaOn = (Kind == PMSST_ON);
615}
616
618 PragmaMSCommentKind Kind, StringRef Arg) {
619 auto *PCD = PragmaCommentDecl::Create(
620 Context, Context.getTranslationUnitDecl(), CommentLoc, Kind, Arg);
623}
624
626 StringRef Value) {
631}
632
636 switch (Value) {
637 default:
638 llvm_unreachable("invalid pragma eval_method kind");
640 NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Source);
641 break;
643 NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Double);
644 break;
646 NewFPFeatures.setFPEvalMethodOverride(LangOptions::FEM_Extended);
647 break;
648 }
649 if (getLangOpts().ApproxFunc)
650 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 0;
651 if (getLangOpts().AllowFPReassoc)
652 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 1;
653 if (getLangOpts().AllowRecip)
654 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context) << 0 << 2;
655 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
656 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
658}
659
661 PragmaMsStackAction Action,
664 if ((Action == PSK_Push_Set || Action == PSK_Push || Action == PSK_Pop) &&
666 // Push and pop can only occur at file or namespace scope, or within a
667 // language linkage declaration.
668 Diag(Loc, diag::err_pragma_fc_pp_scope);
669 return;
670 }
671 switch (Value) {
672 default:
673 llvm_unreachable("invalid pragma float_control kind");
674 case PFC_Precise:
675 NewFPFeatures.setFPPreciseEnabled(true);
676 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
677 break;
678 case PFC_NoPrecise:
680 Diag(Loc, diag::err_pragma_fc_noprecise_requires_noexcept);
681 else if (CurFPFeatures.getAllowFEnvAccess())
682 Diag(Loc, diag::err_pragma_fc_noprecise_requires_nofenv);
683 else
684 NewFPFeatures.setFPPreciseEnabled(false);
685 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
686 break;
687 case PFC_Except:
688 if (!isPreciseFPEnabled())
689 Diag(Loc, diag::err_pragma_fc_except_requires_precise);
690 else
691 NewFPFeatures.setSpecifiedExceptionModeOverride(LangOptions::FPE_Strict);
692 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
693 break;
694 case PFC_NoExcept:
695 NewFPFeatures.setSpecifiedExceptionModeOverride(LangOptions::FPE_Ignore);
696 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
697 break;
698 case PFC_Push:
699 FpPragmaStack.Act(Loc, Sema::PSK_Push_Set, StringRef(), NewFPFeatures);
700 break;
701 case PFC_Pop:
702 if (FpPragmaStack.Stack.empty()) {
703 Diag(Loc, diag::warn_pragma_pop_failed) << "float_control"
704 << "stack empty";
705 return;
706 }
707 FpPragmaStack.Act(Loc, Action, StringRef(), NewFPFeatures);
708 NewFPFeatures = FpPragmaStack.CurrentValue;
709 break;
710 }
711 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
712}
713
716 SourceLocation PragmaLoc) {
717 MSPointerToMemberRepresentationMethod = RepresentationMethod;
719}
720
722 SourceLocation PragmaLoc,
723 MSVtorDispMode Mode) {
724 if (Action & PSK_Pop && VtorDispStack.Stack.empty())
725 Diag(PragmaLoc, diag::warn_pragma_pop_failed) << "vtordisp"
726 << "stack empty";
727 VtorDispStack.Act(PragmaLoc, Action, StringRef(), Mode);
728}
729
730template <>
732 PragmaMsStackAction Action,
733 llvm::StringRef StackSlotLabel,
734 AlignPackInfo Value) {
735 if (Action == PSK_Reset) {
736 CurrentValue = DefaultValue;
737 CurrentPragmaLocation = PragmaLocation;
738 return;
739 }
740 if (Action & PSK_Push)
741 Stack.emplace_back(Slot(StackSlotLabel, CurrentValue, CurrentPragmaLocation,
742 PragmaLocation));
743 else if (Action & PSK_Pop) {
744 if (!StackSlotLabel.empty()) {
745 // If we've got a label, try to find it and jump there.
746 auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
747 return x.StackSlotLabel == StackSlotLabel;
748 });
749 // We found the label, so pop from there.
750 if (I != Stack.rend()) {
751 CurrentValue = I->Value;
752 CurrentPragmaLocation = I->PragmaLocation;
753 Stack.erase(std::prev(I.base()), Stack.end());
754 }
755 } else if (Value.IsXLStack() && Value.IsAlignAttr() &&
756 CurrentValue.IsPackAttr()) {
757 // XL '#pragma align(reset)' would pop the stack until
758 // a current in effect pragma align is popped.
759 auto I = llvm::find_if(llvm::reverse(Stack), [&](const Slot &x) {
760 return x.Value.IsAlignAttr();
761 });
762 // If we found pragma align so pop from there.
763 if (I != Stack.rend()) {
764 Stack.erase(std::prev(I.base()), Stack.end());
765 if (Stack.empty()) {
766 CurrentValue = DefaultValue;
767 CurrentPragmaLocation = PragmaLocation;
768 } else {
769 CurrentValue = Stack.back().Value;
770 CurrentPragmaLocation = Stack.back().PragmaLocation;
771 Stack.pop_back();
772 }
773 }
774 } else if (!Stack.empty()) {
775 // xl '#pragma align' sets the baseline, and `#pragma pack` cannot pop
776 // over the baseline.
777 if (Value.IsXLStack() && Value.IsPackAttr() && CurrentValue.IsAlignAttr())
778 return;
779
780 // We don't have a label, just pop the last entry.
781 CurrentValue = Stack.back().Value;
782 CurrentPragmaLocation = Stack.back().PragmaLocation;
783 Stack.pop_back();
784 }
785 }
786 if (Action & PSK_Set) {
787 CurrentValue = Value;
788 CurrentPragmaLocation = PragmaLocation;
789 }
790}
791
792bool Sema::UnifySection(StringRef SectionName, int SectionFlags,
793 NamedDecl *Decl) {
794 SourceLocation PragmaLocation;
795 if (auto A = Decl->getAttr<SectionAttr>())
796 if (A->isImplicit())
797 PragmaLocation = A->getLocation();
798 auto [SectionIt, Inserted] = Context.SectionInfos.try_emplace(
799 SectionName, Decl, PragmaLocation, SectionFlags);
800 if (Inserted)
801 return false;
802 // A pre-declared section takes precedence w/o diagnostic.
803 const auto &Section = SectionIt->second;
804 if (Section.SectionFlags == SectionFlags ||
805 ((SectionFlags & ASTContext::PSF_Implicit) &&
806 !(Section.SectionFlags & ASTContext::PSF_Implicit)))
807 return false;
808 Diag(Decl->getLocation(), diag::err_section_conflict) << Decl << Section;
809 if (Section.Decl)
810 Diag(Section.Decl->getLocation(), diag::note_declared_at)
811 << Section.Decl->getName();
812 if (PragmaLocation.isValid())
813 Diag(PragmaLocation, diag::note_pragma_entered_here);
814 if (Section.PragmaSectionLocation.isValid())
815 Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here);
816 return true;
817}
818
819bool Sema::UnifySection(StringRef SectionName,
820 int SectionFlags,
821 SourceLocation PragmaSectionLocation) {
822 auto SectionIt = Context.SectionInfos.find(SectionName);
823 if (SectionIt != Context.SectionInfos.end()) {
824 const auto &Section = SectionIt->second;
825 if (Section.SectionFlags == SectionFlags)
826 return false;
827 if (!(Section.SectionFlags & ASTContext::PSF_Implicit)) {
828 Diag(PragmaSectionLocation, diag::err_section_conflict)
829 << "this" << Section;
830 if (Section.Decl)
831 Diag(Section.Decl->getLocation(), diag::note_declared_at)
832 << Section.Decl->getName();
833 if (Section.PragmaSectionLocation.isValid())
834 Diag(Section.PragmaSectionLocation, diag::note_pragma_entered_here);
835 return true;
836 }
837 }
838 Context.SectionInfos[SectionName] =
839 ASTContext::SectionInfo(nullptr, PragmaSectionLocation, SectionFlags);
840 return false;
841}
842
843/// Called on well formed \#pragma bss_seg().
845 PragmaMsStackAction Action,
846 llvm::StringRef StackSlotLabel,
847 StringLiteral *SegmentName,
848 llvm::StringRef PragmaName) {
850 llvm::StringSwitch<PragmaStack<StringLiteral *> *>(PragmaName)
851 .Case("data_seg", &DataSegStack)
852 .Case("bss_seg", &BSSSegStack)
853 .Case("const_seg", &ConstSegStack)
854 .Case("code_seg", &CodeSegStack);
855 if (Action & PSK_Pop && Stack->Stack.empty())
856 Diag(PragmaLocation, diag::warn_pragma_pop_failed) << PragmaName
857 << "stack empty";
858 if (SegmentName) {
859 if (!checkSectionName(SegmentName->getBeginLoc(), SegmentName->getString()))
860 return;
861
862 if (SegmentName->getString() == ".drectve" &&
864 Diag(PragmaLocation, diag::warn_attribute_section_drectve) << PragmaName;
865 }
866
867 Stack->Act(PragmaLocation, Action, StackSlotLabel, SegmentName);
868}
869
870/// Called on well formed \#pragma strict_gs_check().
872 PragmaMsStackAction Action,
873 bool Value) {
874 if (Action & PSK_Pop && StrictGuardStackCheckStack.Stack.empty())
875 Diag(PragmaLocation, diag::warn_pragma_pop_failed) << "strict_gs_check"
876 << "stack empty";
877
878 StrictGuardStackCheckStack.Act(PragmaLocation, Action, StringRef(), Value);
879}
880
881/// Called on well formed \#pragma bss_seg().
883 int SectionFlags, StringLiteral *SegmentName) {
884 UnifySection(SegmentName->getString(), SectionFlags, PragmaLocation);
885}
886
888 StringLiteral *SegmentName) {
889 // There's no stack to maintain, so we just have a current section. When we
890 // see the default section, reset our current section back to null so we stop
891 // tacking on unnecessary attributes.
892 CurInitSeg = SegmentName->getString() == ".CRT$XCU" ? nullptr : SegmentName;
893 CurInitSegLoc = PragmaLocation;
894}
895
897 SourceLocation PragmaLocation, StringRef Section,
898 const SmallVector<std::tuple<IdentifierInfo *, SourceLocation>>
899 &Functions) {
901 Diag(PragmaLocation, diag::err_pragma_expected_file_scope) << "alloc_text";
902 return;
903 }
904
905 for (auto &Function : Functions) {
906 IdentifierInfo *II;
908 std::tie(II, Loc) = Function;
909
910 DeclarationName DN(II);
912 if (!ND) {
913 Diag(Loc, diag::err_undeclared_use) << II->getName();
914 return;
915 }
916
917 auto *FD = dyn_cast<FunctionDecl>(ND->getCanonicalDecl());
918 if (!FD) {
919 Diag(Loc, diag::err_pragma_alloc_text_not_function);
920 return;
921 }
922
923 if (getLangOpts().CPlusPlus && !FD->isInExternCContext()) {
924 Diag(Loc, diag::err_pragma_alloc_text_c_linkage);
925 return;
926 }
927
928 FunctionToSectionMap[II->getName()] = std::make_tuple(Section, Loc);
929 }
930}
931
932void Sema::ActOnPragmaUnused(const Token &IdTok, Scope *curScope,
933 SourceLocation PragmaLoc) {
934
935 IdentifierInfo *Name = IdTok.getIdentifierInfo();
936 LookupResult Lookup(*this, Name, IdTok.getLocation(), LookupOrdinaryName);
937 LookupName(Lookup, curScope, /*AllowBuiltinCreation=*/true);
938
939 if (Lookup.empty()) {
940 Diag(PragmaLoc, diag::warn_pragma_unused_undeclared_var)
941 << Name << SourceRange(IdTok.getLocation());
942 return;
943 }
944
945 VarDecl *VD = Lookup.getAsSingle<VarDecl>();
946 if (!VD) {
947 Diag(PragmaLoc, diag::warn_pragma_unused_expected_var_arg)
948 << Name << SourceRange(IdTok.getLocation());
949 return;
950 }
951
952 // Warn if this was used before being marked unused.
953 if (VD->isUsed())
954 Diag(PragmaLoc, diag::warn_used_but_marked_unused) << Name;
955
956 VD->addAttr(UnusedAttr::CreateImplicit(Context, IdTok.getLocation(),
957 UnusedAttr::GNU_unused));
958}
959
960namespace {
961
962std::optional<attr::SubjectMatchRule>
963getParentAttrMatcherRule(attr::SubjectMatchRule Rule) {
964 using namespace attr;
965 switch (Rule) {
966 default:
967 return std::nullopt;
968#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
969#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
970 case Value: \
971 return Parent;
972#include "clang/Basic/AttrSubMatchRulesList.inc"
973 }
974}
975
976bool isNegatedAttrMatcherSubRule(attr::SubjectMatchRule Rule) {
977 using namespace attr;
978 switch (Rule) {
979 default:
980 return false;
981#define ATTR_MATCH_RULE(Value, Spelling, IsAbstract)
982#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, IsNegated) \
983 case Value: \
984 return IsNegated;
985#include "clang/Basic/AttrSubMatchRulesList.inc"
986 }
987}
988
989CharSourceRange replacementRangeForListElement(const Sema &S,
991 // Make sure that the ',' is removed as well.
993 Range.getEnd(), tok::comma, S.getSourceManager(), S.getLangOpts(),
994 /*SkipTrailingWhitespaceAndNewLine=*/false);
995 if (AfterCommaLoc.isValid())
996 return CharSourceRange::getCharRange(Range.getBegin(), AfterCommaLoc);
997 else
999}
1000
1001std::string
1002attrMatcherRuleListToString(ArrayRef<attr::SubjectMatchRule> Rules) {
1003 std::string Result;
1004 llvm::raw_string_ostream OS(Result);
1005 for (const auto &I : llvm::enumerate(Rules)) {
1006 if (I.index())
1007 OS << (I.index() == Rules.size() - 1 ? ", and " : ", ");
1008 OS << "'" << attr::getSubjectMatchRuleSpelling(I.value()) << "'";
1009 }
1010 return Result;
1011}
1012
1013} // end anonymous namespace
1014
1016 ParsedAttr &Attribute, SourceLocation PragmaLoc,
1018 Attribute.setIsPragmaClangAttribute();
1020 // Gather the subject match rules that are supported by the attribute.
1022 StrictSubjectMatchRuleSet;
1023 Attribute.getMatchRules(LangOpts, StrictSubjectMatchRuleSet);
1024
1025 // Figure out which subject matching rules are valid.
1026 if (StrictSubjectMatchRuleSet.empty()) {
1027 // Check for contradicting match rules. Contradicting match rules are
1028 // either:
1029 // - a top-level rule and one of its sub-rules. E.g. variable and
1030 // variable(is_parameter).
1031 // - a sub-rule and a sibling that's negated. E.g.
1032 // variable(is_thread_local) and variable(unless(is_parameter))
1033 llvm::SmallDenseMap<int, std::pair<int, SourceRange>, 2>
1034 RulesToFirstSpecifiedNegatedSubRule;
1035 for (const auto &Rule : Rules) {
1036 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
1037 std::optional<attr::SubjectMatchRule> ParentRule =
1038 getParentAttrMatcherRule(MatchRule);
1039 if (!ParentRule)
1040 continue;
1041 auto It = Rules.find(*ParentRule);
1042 if (It != Rules.end()) {
1043 // A sub-rule contradicts a parent rule.
1044 Diag(Rule.second.getBegin(),
1045 diag::err_pragma_attribute_matcher_subrule_contradicts_rule)
1047 << attr::getSubjectMatchRuleSpelling(*ParentRule) << It->second
1049 replacementRangeForListElement(*this, Rule.second));
1050 // Keep going without removing this rule as it won't change the set of
1051 // declarations that receive the attribute.
1052 continue;
1053 }
1054 if (isNegatedAttrMatcherSubRule(MatchRule))
1055 RulesToFirstSpecifiedNegatedSubRule.insert(
1056 std::make_pair(*ParentRule, Rule));
1057 }
1058 bool IgnoreNegatedSubRules = false;
1059 for (const auto &Rule : Rules) {
1060 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
1061 std::optional<attr::SubjectMatchRule> ParentRule =
1062 getParentAttrMatcherRule(MatchRule);
1063 if (!ParentRule)
1064 continue;
1065 auto It = RulesToFirstSpecifiedNegatedSubRule.find(*ParentRule);
1066 if (It != RulesToFirstSpecifiedNegatedSubRule.end() &&
1067 It->second != Rule) {
1068 // Negated sub-rule contradicts another sub-rule.
1069 Diag(
1070 It->second.second.getBegin(),
1071 diag::
1072 err_pragma_attribute_matcher_negated_subrule_contradicts_subrule)
1074 attr::SubjectMatchRule(It->second.first))
1075 << attr::getSubjectMatchRuleSpelling(MatchRule) << Rule.second
1077 replacementRangeForListElement(*this, It->second.second));
1078 // Keep going but ignore all of the negated sub-rules.
1079 IgnoreNegatedSubRules = true;
1080 RulesToFirstSpecifiedNegatedSubRule.erase(It);
1081 }
1082 }
1083
1084 if (!IgnoreNegatedSubRules) {
1085 for (const auto &Rule : Rules)
1086 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
1087 } else {
1088 for (const auto &Rule : Rules) {
1089 if (!isNegatedAttrMatcherSubRule(attr::SubjectMatchRule(Rule.first)))
1090 SubjectMatchRules.push_back(attr::SubjectMatchRule(Rule.first));
1091 }
1092 }
1093 Rules.clear();
1094 } else {
1095 // Each rule in Rules must be a strict subset of the attribute's
1096 // SubjectMatch rules. I.e. we're allowed to use
1097 // `apply_to=variables(is_global)` on an attrubute with SubjectList<[Var]>,
1098 // but should not allow `apply_to=variables` on an attribute which has
1099 // `SubjectList<[GlobalVar]>`.
1100 for (const auto &StrictRule : StrictSubjectMatchRuleSet) {
1101 // First, check for exact match.
1102 if (Rules.erase(StrictRule.first)) {
1103 // Add the rule to the set of attribute receivers only if it's supported
1104 // in the current language mode.
1105 if (StrictRule.second)
1106 SubjectMatchRules.push_back(StrictRule.first);
1107 }
1108 }
1109 // Check remaining rules for subset matches.
1110 auto RulesToCheck = Rules;
1111 for (const auto &Rule : RulesToCheck) {
1112 attr::SubjectMatchRule MatchRule = attr::SubjectMatchRule(Rule.first);
1113 if (auto ParentRule = getParentAttrMatcherRule(MatchRule)) {
1114 if (llvm::any_of(StrictSubjectMatchRuleSet,
1115 [ParentRule](const auto &StrictRule) {
1116 return StrictRule.first == *ParentRule &&
1117 StrictRule.second; // IsEnabled
1118 })) {
1119 SubjectMatchRules.push_back(MatchRule);
1120 Rules.erase(MatchRule);
1121 }
1122 }
1123 }
1124 }
1125
1126 if (!Rules.empty()) {
1127 auto Diagnostic =
1128 Diag(PragmaLoc, diag::err_pragma_attribute_invalid_matchers)
1129 << Attribute;
1131 for (const auto &Rule : Rules) {
1132 ExtraRules.push_back(attr::SubjectMatchRule(Rule.first));
1134 replacementRangeForListElement(*this, Rule.second));
1135 }
1136 Diagnostic << attrMatcherRuleListToString(ExtraRules);
1137 }
1138
1139 if (PragmaAttributeStack.empty()) {
1140 Diag(PragmaLoc, diag::err_pragma_attr_attr_no_push);
1141 return;
1142 }
1143
1144 PragmaAttributeStack.back().Entries.push_back(
1145 {PragmaLoc, &Attribute, std::move(SubjectMatchRules), /*IsUsed=*/false});
1146}
1147
1149 const IdentifierInfo *Namespace) {
1150 PragmaAttributeStack.emplace_back();
1151 PragmaAttributeStack.back().Loc = PragmaLoc;
1152 PragmaAttributeStack.back().Namespace = Namespace;
1153}
1154
1156 const IdentifierInfo *Namespace) {
1157 if (PragmaAttributeStack.empty()) {
1158 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
1159 return;
1160 }
1161
1162 // Dig back through the stack trying to find the most recently pushed group
1163 // that in Namespace. Note that this works fine if no namespace is present,
1164 // think of push/pops without namespaces as having an implicit "nullptr"
1165 // namespace.
1166 for (size_t Index = PragmaAttributeStack.size(); Index;) {
1167 --Index;
1168 if (PragmaAttributeStack[Index].Namespace == Namespace) {
1169 for (const PragmaAttributeEntry &Entry :
1170 PragmaAttributeStack[Index].Entries) {
1171 if (!Entry.IsUsed) {
1172 assert(Entry.Attribute && "Expected an attribute");
1173 Diag(Entry.Attribute->getLoc(), diag::warn_pragma_attribute_unused)
1174 << *Entry.Attribute;
1175 Diag(PragmaLoc, diag::note_pragma_attribute_region_ends_here);
1176 }
1177 }
1178 PragmaAttributeStack.erase(PragmaAttributeStack.begin() + Index);
1179 return;
1180 }
1181 }
1182
1183 if (Namespace)
1184 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch)
1185 << 0 << Namespace->getName();
1186 else
1187 Diag(PragmaLoc, diag::err_pragma_attribute_stack_mismatch) << 1;
1188}
1189
1191 if (PragmaAttributeStack.empty())
1192 return;
1193 for (auto &Group : PragmaAttributeStack) {
1194 for (auto &Entry : Group.Entries) {
1195 ParsedAttr *Attribute = Entry.Attribute;
1196 assert(Attribute && "Expected an attribute");
1197 assert(Attribute->isPragmaClangAttribute() &&
1198 "expected #pragma clang attribute");
1199
1200 // Ensure that the attribute can be applied to the given declaration.
1201 bool Applies = false;
1202 for (const auto &Rule : Entry.MatchRules) {
1203 if (Attribute->appliesToDecl(D, Rule)) {
1204 Applies = true;
1205 break;
1206 }
1207 }
1208 if (!Applies)
1209 continue;
1210 Entry.IsUsed = true;
1213 Attrs.addAtEnd(Attribute);
1214 ProcessDeclAttributeList(S, D, Attrs);
1216 }
1217 }
1218}
1219
1221 assert(PragmaAttributeCurrentTargetDecl && "Expected an active declaration");
1223 diag::note_pragma_attribute_applied_decl_here);
1224}
1225
1227 for (auto &[Type, Num] : ExcessPrecisionNotSatisfied) {
1229 "expected a valid source location");
1231 diag::warn_excess_precision_not_supported)
1232 << static_cast<bool>(Num);
1233 }
1234}
1235
1237 if (PragmaAttributeStack.empty())
1238 return;
1239 Diag(PragmaAttributeStack.back().Loc, diag::err_pragma_attribute_no_pop_eof);
1240}
1241
1243 if(On)
1245 else
1246 OptimizeOffPragmaLocation = PragmaLoc;
1247}
1248
1251 Diag(Loc, diag::err_pragma_expected_file_scope) << "optimize";
1252 return;
1253 }
1254
1255 MSPragmaOptimizeIsOn = IsOn;
1256}
1257
1261 Diag(Loc, diag::err_pragma_expected_file_scope) << "function";
1262 return;
1263 }
1264
1265 MSFunctionNoBuiltins.insert(NoBuiltins.begin(), NoBuiltins.end());
1266}
1267
1269 // In the future, check other pragmas if they're implemented (e.g. pragma
1270 // optimize 0 will probably map to this functionality too).
1273}
1274
1276 if (!FD->getIdentifier())
1277 return;
1278
1279 StringRef Name = FD->getName();
1280 auto It = FunctionToSectionMap.find(Name);
1281 if (It != FunctionToSectionMap.end()) {
1282 StringRef Section;
1284 std::tie(Section, Loc) = It->second;
1285
1286 if (!FD->hasAttr<SectionAttr>())
1287 FD->addAttr(SectionAttr::CreateImplicit(Context, Section));
1288 }
1289}
1290
1292 // Don't modify the function attributes if it's "on". "on" resets the
1293 // optimizations to the ones listed on the command line
1296}
1297
1300 // Don't add a conflicting attribute. No diagnostic is needed.
1301 if (FD->hasAttr<MinSizeAttr>() || FD->hasAttr<AlwaysInlineAttr>())
1302 return;
1303
1304 // Add attributes only if required. Optnone requires noinline as well, but if
1305 // either is already present then don't bother adding them.
1306 if (!FD->hasAttr<OptimizeNoneAttr>())
1307 FD->addAttr(OptimizeNoneAttr::CreateImplicit(Context, Loc));
1308 if (!FD->hasAttr<NoInlineAttr>())
1309 FD->addAttr(NoInlineAttr::CreateImplicit(Context, Loc));
1310}
1311
1313 if (FD->isDeleted() || FD->isDefaulted())
1314 return;
1316 MSFunctionNoBuiltins.end());
1317 if (!MSFunctionNoBuiltins.empty())
1318 FD->addAttr(NoBuiltinAttr::CreateImplicit(Context, V.data(), V.size()));
1319}
1320
1321typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
1322enum : unsigned { NoVisibility = ~0U };
1323
1325 if (!VisContext)
1326 return;
1327
1328 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1330 return;
1331
1332 VisStack *Stack = static_cast<VisStack*>(VisContext);
1333 unsigned rawType = Stack->back().first;
1334 if (rawType == NoVisibility) return;
1335
1336 VisibilityAttr::VisibilityType type
1337 = (VisibilityAttr::VisibilityType) rawType;
1338 SourceLocation loc = Stack->back().second;
1339
1340 D->addAttr(VisibilityAttr::CreateImplicit(Context, type, loc));
1341}
1342
1344 delete static_cast<VisStack*>(VisContext);
1345 VisContext = nullptr;
1346}
1347
1348static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc) {
1349 // Put visibility on stack.
1350 if (!S.VisContext)
1351 S.VisContext = new VisStack;
1352
1353 VisStack *Stack = static_cast<VisStack*>(S.VisContext);
1354 Stack->push_back(std::make_pair(type, loc));
1355}
1356
1358 SourceLocation PragmaLoc) {
1359 if (VisType) {
1360 // Compute visibility to use.
1361 VisibilityAttr::VisibilityType T;
1362 if (!VisibilityAttr::ConvertStrToVisibilityType(VisType->getName(), T)) {
1363 Diag(PragmaLoc, diag::warn_attribute_unknown_visibility) << VisType;
1364 return;
1365 }
1366 PushPragmaVisibility(*this, T, PragmaLoc);
1367 } else {
1368 PopPragmaVisibility(false, PragmaLoc);
1369 }
1370}
1371
1374 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1375 switch (FPC) {
1377 NewFPFeatures.setAllowFPContractWithinStatement();
1378 break;
1381 NewFPFeatures.setAllowFPContractAcrossStatement();
1382 break;
1384 NewFPFeatures.setDisallowFPContract();
1385 break;
1386 }
1387 FpPragmaStack.Act(Loc, Sema::PSK_Set, StringRef(), NewFPFeatures);
1388 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1389}
1390
1392 PragmaFPKind Kind, bool IsEnabled) {
1393 if (IsEnabled) {
1394 // For value unsafe context, combining this pragma with eval method
1395 // setting is not recommended. See comment in function FixupInvocation#506.
1396 int Reason = -1;
1397 if (getLangOpts().getFPEvalMethod() != LangOptions::FEM_UnsetOnCommandLine)
1398 // Eval method set using the option 'ffp-eval-method'.
1399 Reason = 1;
1401 // Eval method set using the '#pragma clang fp eval_method'.
1402 // We could have both an option and a pragma used to the set the eval
1403 // method. The pragma overrides the option in the command line. The Reason
1404 // of the diagnostic is overriden too.
1405 Reason = 0;
1406 if (Reason != -1)
1407 Diag(Loc, diag::err_setting_eval_method_used_in_unsafe_context)
1408 << Reason << (Kind == PFK_Reassociate ? 4 : 5);
1409 }
1410
1411 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1412 switch (Kind) {
1413 case PFK_Reassociate:
1414 NewFPFeatures.setAllowFPReassociateOverride(IsEnabled);
1415 break;
1416 case PFK_Reciprocal:
1417 NewFPFeatures.setAllowReciprocalOverride(IsEnabled);
1418 break;
1419 default:
1420 llvm_unreachable("unhandled value changing pragma fp");
1421 }
1422
1423 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1424 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1425}
1426
1427void Sema::ActOnPragmaFEnvRound(SourceLocation Loc, llvm::RoundingMode FPR) {
1428 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1429 NewFPFeatures.setConstRoundingModeOverride(FPR);
1430 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1431 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1432}
1433
1436 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1437 NewFPFeatures.setSpecifiedExceptionModeOverride(FPE);
1438 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1439 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1440}
1441
1443 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1444 if (IsEnabled) {
1445 // Verify Microsoft restriction:
1446 // You can't enable fenv_access unless precise semantics are enabled.
1447 // Precise semantics can be enabled either by the float_control
1448 // pragma, or by using the /fp:precise or /fp:strict compiler options
1449 if (!isPreciseFPEnabled())
1450 Diag(Loc, diag::err_pragma_fenv_requires_precise);
1451 }
1452 NewFPFeatures.setAllowFEnvAccessOverride(IsEnabled);
1453 NewFPFeatures.setRoundingMathOverride(IsEnabled);
1454 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1455 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1456}
1457
1460 FPOptionsOverride NewFPFeatures = CurFPFeatureOverrides();
1461 NewFPFeatures.setComplexRangeOverride(Range);
1462 FpPragmaStack.Act(Loc, PSK_Set, StringRef(), NewFPFeatures);
1463 CurFPFeatures = NewFPFeatures.applyOverrides(getLangOpts());
1464}
1465
1468 setExceptionMode(Loc, FPE);
1469}
1470
1471void Sema::PushNamespaceVisibilityAttr(const VisibilityAttr *Attr,
1473 // Visibility calculations will consider the namespace's visibility.
1474 // Here we just want to note that we're in a visibility context
1475 // which overrides any enclosing #pragma context, but doesn't itself
1476 // contribute visibility.
1478}
1479
1480void Sema::PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc) {
1481 if (!VisContext) {
1482 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
1483 return;
1484 }
1485
1486 // Pop visibility from stack
1487 VisStack *Stack = static_cast<VisStack*>(VisContext);
1488
1489 const std::pair<unsigned, SourceLocation> *Back = &Stack->back();
1490 bool StartsWithPragma = Back->first != NoVisibility;
1491 if (StartsWithPragma && IsNamespaceEnd) {
1492 Diag(Back->second, diag::err_pragma_push_visibility_mismatch);
1493 Diag(EndLoc, diag::note_surrounding_namespace_ends_here);
1494
1495 // For better error recovery, eat all pushes inside the namespace.
1496 do {
1497 Stack->pop_back();
1498 Back = &Stack->back();
1499 StartsWithPragma = Back->first != NoVisibility;
1500 } while (StartsWithPragma);
1501 } else if (!StartsWithPragma && !IsNamespaceEnd) {
1502 Diag(EndLoc, diag::err_pragma_pop_visibility_mismatch);
1503 Diag(Back->second, diag::note_surrounding_namespace_starts_here);
1504 return;
1505 }
1506
1507 Stack->pop_back();
1508 // To simplify the implementation, never keep around an empty stack.
1509 if (Stack->empty())
1511}
1512
1513template <typename Ty>
1514static bool checkCommonAttributeFeatures(Sema &S, const Ty *Node,
1515 const ParsedAttr &A,
1516 bool SkipArgCountCheck) {
1517 // Several attributes carry different semantics than the parsing requires, so
1518 // those are opted out of the common argument checks.
1519 //
1520 // We also bail on unknown and ignored attributes because those are handled
1521 // as part of the target-specific handling logic.
1523 return false;
1524 // Check whether the attribute requires specific language extensions to be
1525 // enabled.
1526 if (!A.diagnoseLangOpts(S))
1527 return true;
1528 // Check whether the attribute appertains to the given subject.
1529 if (!A.diagnoseAppertainsTo(S, Node))
1530 return true;
1531 // Check whether the attribute is mutually exclusive with other attributes
1532 // that have already been applied to the declaration.
1533 if (!A.diagnoseMutualExclusion(S, Node))
1534 return true;
1535 // Check whether the attribute exists in the target architecture.
1536 if (S.CheckAttrTarget(A))
1537 return true;
1538
1539 if (A.hasCustomParsing())
1540 return false;
1541
1542 if (!SkipArgCountCheck) {
1543 if (A.getMinArgs() == A.getMaxArgs()) {
1544 // If there are no optional arguments, then checking for the argument
1545 // count is trivial.
1546 if (!A.checkExactlyNumArgs(S, A.getMinArgs()))
1547 return true;
1548 } else {
1549 // There are optional arguments, so checking is slightly more involved.
1550 if (A.getMinArgs() && !A.checkAtLeastNumArgs(S, A.getMinArgs()))
1551 return true;
1552 else if (!A.hasVariadicArg() && A.getMaxArgs() &&
1553 !A.checkAtMostNumArgs(S, A.getMaxArgs()))
1554 return true;
1555 }
1556 }
1557
1558 return false;
1559}
1560
1562 bool SkipArgCountCheck) {
1563 return ::checkCommonAttributeFeatures(*this, D, A, SkipArgCountCheck);
1564}
1566 bool SkipArgCountCheck) {
1567 return ::checkCommonAttributeFeatures(*this, S, A, SkipArgCountCheck);
1568}
#define V(N, I)
Definition: ASTContext.h:3443
NodeId Parent
Definition: ASTDiff.cpp:191
DynTypedNode Node
StringRef P
const Decl * D
Expr * E
llvm::MachO::Record Record
Definition: MachO.h:31
Defines the clang::Preprocessor interface.
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
std::vector< std::pair< unsigned, SourceLocation > > VisStack
Definition: SemaAttr.cpp:1321
static void PushPragmaVisibility(Sema &S, unsigned type, SourceLocation loc)
Definition: SemaAttr.cpp:1348
@ NoVisibility
Definition: SemaAttr.cpp:1322
static void addGslOwnerPointerAttributeIfNotExisting(ASTContext &Context, CXXRecordDecl *Record)
Definition: SemaAttr.cpp:102
SourceRange Range
Definition: SemaObjC.cpp:758
SourceLocation Loc
Definition: SemaObjC.cpp:759
bool hasValue() const
Definition: APValue.h:441
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:18
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1141
llvm::StringMap< SectionInfo > SectionInfos
Definition: ASTContext.h:3559
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:799
PtrTy get() const
Definition: Ownership.h:170
Attr - This represents one attribute.
Definition: Attr.h:43
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
Represents a character-granular source range.
static CharSourceRange getCharRange(SourceRange R)
static CharSourceRange getTokenRange(SourceRange R)
static ConstantExpr * Create(const ASTContext &Context, Expr *E, const APValue &Result)
Definition: Expr.cpp:350
bool isFileContext() const
Definition: DeclBase.h:2160
DeclContext * getRedeclContext()
getRedeclContext - Retrieve the context in which an entity conflicts with other entities of the same ...
Definition: DeclBase.cpp:1990
void addDecl(Decl *D)
Add the declaration D into this context.
Definition: DeclBase.cpp:1768
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Decl()=delete
bool isInStdNamespace() const
Definition: DeclBase.cpp:422
T * getAttr() const
Definition: DeclBase.h:576
void addAttr(Attr *A)
Definition: DeclBase.cpp:1010
SourceLocation getLocation() const
Definition: DeclBase.h:442
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
Definition: DeclBase.h:1038
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
Definition: DeclBase.cpp:549
DeclContext * getDeclContext()
Definition: DeclBase.h:451
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: DeclBase.h:434
bool hasAttr() const
Definition: DeclBase.h:580
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclBase.h:967
The name of a declaration.
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Decl.h:786
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine a...
Definition: Diagnostic.h:1512
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1493
This represents one expression.
Definition: Expr.h:110
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition: Expr.h:175
bool isTypeDependent() const
Determines whether the type of this expression depends on.
Definition: Expr.h:192
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition: Expr.h:277
bool EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx, ConstantExprKind Kind=ConstantExprKind::Normal) const
Evaluate an expression that is required to be a constant expression.
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc=nullptr) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
QualType getType() const
Definition: Expr.h:142
Represents difference between two FPOptions values.
Definition: LangOptions.h:978
void setAllowFPContractAcrossStatement()
Definition: LangOptions.h:1010
void setFPPreciseEnabled(bool Value)
Definition: LangOptions.h:1018
void setAllowFPContractWithinStatement()
Definition: LangOptions.h:1006
FPOptions applyOverrides(FPOptions Base)
Definition: LangOptions.h:1048
LangOptions::FPExceptionModeKind getExceptionMode() const
Definition: LangOptions.h:924
static FixItHint CreateRemoval(CharSourceRange RemoveRange)
Create a code modification hint that removes the given source range.
Definition: Diagnostic.h:127
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Definition: Diagnostic.h:101
Represents a function declaration or definition.
Definition: Decl.h:1935
const ParmVarDecl * getParamDecl(unsigned i) const
Definition: Decl.h:2672
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition: Decl.cpp:3638
bool isDeleted() const
Whether this function has been deleted.
Definition: Decl.h:2468
bool isDefaulted() const
Whether this function is defaulted.
Definition: Decl.h:2313
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition: Decl.cpp:3702
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
static ImplicitCastExpr * Create(const ASTContext &Context, QualType T, CastKind Kind, Expr *Operand, const CXXCastPath *BasePath, ExprValueKind Cat, FPOptionsOverride FPO)
Definition: Expr.cpp:2082
An lvalue reference type, per C++11 [dcl.ref].
Definition: Type.h:3483
ComplexRangeKind
Controls the various implementations for complex multiplication and.
Definition: LangOptions.h:441
FPEvalMethodKind
Possible float expression evaluation method choices.
Definition: LangOptions.h:299
@ FEM_Extended
Use extended type for fp arithmetic.
Definition: LangOptions.h:308
@ FEM_Double
Use the type double for fp arithmetic.
Definition: LangOptions.h:306
@ FEM_UnsetOnCommandLine
Used only for FE option processing; this is only used to indicate that the user did not specify an ex...
Definition: LangOptions.h:313
@ FEM_Source
Use the declared type for fp arithmetic.
Definition: LangOptions.h:304
FPExceptionModeKind
Possible floating point exception behavior.
Definition: LangOptions.h:287
@ FPE_Strict
Strictly preserve the floating-point exception semantics.
Definition: LangOptions.h:293
@ FPE_Ignore
Assume that floating-point exceptions are masked.
Definition: LangOptions.h:289
static SourceLocation findLocationAfterToken(SourceLocation loc, tok::TokenKind TKind, const SourceManager &SM, const LangOptions &LangOpts, bool SkipTrailingWhitespaceAndNewLine)
Checks that the given token is the first token that occurs after the given location (this excludes co...
Definition: Lexer.cpp:1357
Represents the results of name lookup.
Definition: Lookup.h:46
DeclClass * getAsSingle() const
Definition: Lookup.h:558
bool empty() const
Return true if no decls were found.
Definition: Lookup.h:362
This represents a decl that may have a name.
Definition: Decl.h:253
@ VisibilityForValue
Do an LV computation for, ultimately, a non-type declaration.
Definition: Decl.h:440
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:274
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:280
std::optional< Visibility > getExplicitVisibility(ExplicitVisibilityKind kind) const
If visibility was explicitly specified for this declaration, return that visibility.
Definition: Decl.cpp:1304
Represents a parameter to a function.
Definition: Decl.h:1725
ParsedAttr - Represents a syntactic attribute.
Definition: ParsedAttr.h:129
bool hasCustomParsing() const
Definition: ParsedAttr.cpp:156
bool appliesToDecl(const Decl *D, attr::SubjectMatchRule MatchRule) const
Definition: ParsedAttr.cpp:172
unsigned getMinArgs() const
Definition: ParsedAttr.cpp:146
bool checkExactlyNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has exactly as many args as Num.
Definition: ParsedAttr.cpp:296
bool hasVariadicArg() const
Definition: ParsedAttr.cpp:264
bool diagnoseMutualExclusion(class Sema &S, const Decl *D) const
Definition: ParsedAttr.cpp:168
bool diagnoseAppertainsTo(class Sema &S, const Decl *D) const
Definition: ParsedAttr.cpp:160
bool checkAtLeastNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at least as many args as Num.
Definition: ParsedAttr.cpp:301
unsigned getMaxArgs() const
Definition: ParsedAttr.cpp:148
bool isPragmaClangAttribute() const
True if the attribute is specified using '#pragma clang attribute'.
Definition: ParsedAttr.h:378
AttributeCommonInfo::Kind getKind() const
Definition: ParsedAttr.h:625
void getMatchRules(const LangOptions &LangOpts, SmallVectorImpl< std::pair< attr::SubjectMatchRule, bool > > &MatchRules) const
Definition: ParsedAttr.cpp:177
bool diagnoseLangOpts(class Sema &S) const
Definition: ParsedAttr.cpp:184
void setIsPragmaClangAttribute()
Definition: ParsedAttr.h:380
bool checkAtMostNumArgs(class Sema &S, unsigned Num) const
Check if the attribute has at most as many args as Num.
Definition: ParsedAttr.cpp:306
void addAtEnd(ParsedAttr *newAttr)
Definition: ParsedAttr.h:846
static PragmaCommentDecl * Create(const ASTContext &C, TranslationUnitDecl *DC, SourceLocation CommentLoc, PragmaMSCommentKind CommentKind, StringRef Arg)
Definition: Decl.cpp:5308
static PragmaDetectMismatchDecl * Create(const ASTContext &C, TranslationUnitDecl *DC, SourceLocation Loc, StringRef Name, StringRef Value)
Definition: Decl.cpp:5331
SourceLocation getLastFPEvalPragmaLocation() const
void setCurrentFPEvalMethod(SourceLocation PragmaLoc, LangOptions::FPEvalMethodKind Val)
A (possibly-)qualified type.
Definition: Type.h:929
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition: Type.h:7931
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:8134
QualType getCanonicalType() const
Definition: Type.h:7983
Represents a struct/union/class.
Definition: Decl.h:4148
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
Definition: Redeclarable.h:295
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
Definition: SemaBase.cpp:60
unsigned getPackNumber() const
Definition: Sema.h:1504
bool IsPackSet() const
Definition: Sema.h:1506
bool IsAlignAttr() const
Definition: Sema.h:1500
Mode getAlignMode() const
Definition: Sema.h:1502
PragmaStackSentinelRAII(Sema &S, StringRef SlotLabel, bool ShouldAct)
Definition: SemaAttr.cpp:28
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:463
void ActOnPragmaMSOptimize(SourceLocation Loc, bool IsOn)
#pragma optimize("[optimization-list]", on | off).
Definition: SemaAttr.cpp:1249
bool ConstantFoldAttrArgs(const AttributeCommonInfo &CI, MutableArrayRef< Expr * > Args)
ConstantFoldAttrArgs - Folds attribute arguments into ConstantExprs (unless they are value dependent ...
Definition: SemaAttr.cpp:499
void ActOnPragmaAttributeEmptyPush(SourceLocation PragmaLoc, const IdentifierInfo *Namespace)
Definition: SemaAttr.cpp:1148
@ LookupOrdinaryName
Ordinary name lookup, which finds ordinary names (functions, variables, typedefs, etc....
Definition: Sema.h:8983
void ActOnPragmaAttributePop(SourceLocation PragmaLoc, const IdentifierInfo *Namespace)
Called on well-formed '#pragma clang attribute pop'.
Definition: SemaAttr.cpp:1155
void ActOnPragmaDetectMismatch(SourceLocation Loc, StringRef Name, StringRef Value)
ActOnPragmaDetectMismatch - Call on well-formed #pragma detect_mismatch.
Definition: SemaAttr.cpp:625
const Decl * PragmaAttributeCurrentTargetDecl
The declaration that is currently receiving an attribute from the #pragma attribute stack.
Definition: Sema.h:1719
void ActOnPragmaFEnvRound(SourceLocation Loc, llvm::RoundingMode)
Called to set constant rounding mode for floating point operations.
Definition: SemaAttr.cpp:1427
PragmaClangSection PragmaClangRodataSection
Definition: Sema.h:1426
bool checkSectionName(SourceLocation LiteralLoc, StringRef Str)
void AddPragmaAttributes(Scope *S, Decl *D)
Adds the attributes that have been specified using the '#pragma clang attribute push' directives to t...
Definition: SemaAttr.cpp:1190
bool checkCommonAttributeFeatures(const Decl *D, const ParsedAttr &A, bool SkipArgCountCheck=false)
Handles semantic checking for features that are common to all attributes, such as checking whether a ...
Definition: SemaAttr.cpp:1561
PragmaClangSectionAction
Definition: Sema.h:1416
@ PCSA_Clear
Definition: Sema.h:1416
void AddOptnoneAttributeIfNoConflicts(FunctionDecl *FD, SourceLocation Loc)
Adds the 'optnone' attribute to the function declaration if there are no conflicts; Loc represents th...
Definition: SemaAttr.cpp:1298
SourceLocation LocationOfExcessPrecisionNotSatisfied
Definition: Sema.h:7924
void inferLifetimeCaptureByAttribute(FunctionDecl *FD)
Add [[clang:::lifetime_capture_by(this)]] to STL container methods.
Definition: SemaAttr.cpp:272
PragmaStack< FPOptionsOverride > FpPragmaStack
Definition: Sema.h:1657
PragmaStack< StringLiteral * > CodeSegStack
Definition: Sema.h:1651
void AddRangeBasedOptnone(FunctionDecl *FD)
Only called on function definitions; if there is a pragma in scope with the effect of a range-based o...
Definition: SemaAttr.cpp:1268
void ActOnPragmaMSComment(SourceLocation CommentLoc, PragmaMSCommentKind Kind, StringRef Arg)
ActOnPragmaMSComment - Called on well formed #pragma comment(kind, "arg").
Definition: SemaAttr.cpp:617
SmallVector< AlignPackIncludeState, 8 > AlignPackIncludeStack
Definition: Sema.h:1646
void AddAlignmentAttributesForRecord(RecordDecl *RD)
AddAlignmentAttributesForRecord - Adds any needed alignment attributes to a the record decl,...
Definition: SemaAttr.cpp:53
FPOptionsOverride CurFPFeatureOverrides()
Definition: Sema.h:1658
void ActOnPragmaMSSeg(SourceLocation PragmaLocation, PragmaMsStackAction Action, llvm::StringRef StackSlotLabel, StringLiteral *SegmentName, llvm::StringRef PragmaName)
Called on well formed #pragma bss_seg/data_seg/const_seg/code_seg.
Definition: SemaAttr.cpp:844
NamedDecl * LookupSingleName(Scope *S, DeclarationName Name, SourceLocation Loc, LookupNameKind NameKind, RedeclarationKind Redecl=RedeclarationKind::NotForRedeclaration)
Look up a name, looking for a single declaration.
void ActOnPragmaFloatControl(SourceLocation Loc, PragmaMsStackAction Action, PragmaFloatControlKind Value)
ActOnPragmaFloatControl - Call on well-formed #pragma float_control.
Definition: SemaAttr.cpp:660
void ActOnPragmaMSPointersToMembers(LangOptions::PragmaMSPointersToMembersKind Kind, SourceLocation PragmaLoc)
ActOnPragmaMSPointersToMembers - called on well formed #pragma pointers_to_members(representation met...
Definition: SemaAttr.cpp:714
void DiagnosePrecisionLossInComplexDivision()
Definition: SemaAttr.cpp:1226
ASTContext & Context
Definition: Sema.h:908
void ActOnPragmaUnused(const Token &Identifier, Scope *curScope, SourceLocation PragmaLoc)
ActOnPragmaUnused - Called on well-formed '#pragma unused'.
Definition: SemaAttr.cpp:932
void ActOnPragmaMSAllocText(SourceLocation PragmaLocation, StringRef Section, const SmallVector< std::tuple< IdentifierInfo *, SourceLocation > > &Functions)
Called on well-formed #pragma alloc_text().
Definition: SemaAttr.cpp:896
PragmaStack< bool > StrictGuardStackCheckStack
Definition: Sema.h:1654
void ActOnPragmaAttributeAttribute(ParsedAttr &Attribute, SourceLocation PragmaLoc, attr::ParsedSubjectMatchRuleSet Rules)
Definition: SemaAttr.cpp:1015
PragmaStack< StringLiteral * > ConstSegStack
Definition: Sema.h:1650
ExprResult ImpCastExprToType(Expr *E, QualType Type, CastKind CK, ExprValueKind VK=VK_PRValue, const CXXCastPath *BasePath=nullptr, CheckedConversionKind CCK=CheckedConversionKind::Implicit)
ImpCastExprToType - If Expr is not of type 'Type', insert an implicit cast.
Definition: Sema.cpp:690
void ActOnPragmaOptionsAlign(PragmaOptionsAlignKind Kind, SourceLocation PragmaLoc)
ActOnPragmaOptionsAlign - Called on well formed #pragma options align.
Definition: SemaAttr.cpp:329
void ActOnPragmaCXLimitedRange(SourceLocation Loc, LangOptions::ComplexRangeKind Range)
ActOnPragmaCXLimitedRange - Called on well formed #pragma STDC CX_LIMITED_RANGE.
Definition: SemaAttr.cpp:1458
void ActOnPragmaFPExceptions(SourceLocation Loc, LangOptions::FPExceptionModeKind)
Called on well formed '#pragma clang fp' that has option 'exceptions'.
Definition: SemaAttr.cpp:1466
void inferGslPointerAttribute(NamedDecl *ND, CXXRecordDecl *UnderlyingRecord)
Add gsl::Pointer attribute to std::container::iterator.
Definition: SemaAttr.cpp:111
void ActOnPragmaFPEvalMethod(SourceLocation Loc, LangOptions::FPEvalMethodKind Value)
Definition: SemaAttr.cpp:633
void ActOnPragmaClangSection(SourceLocation PragmaLoc, PragmaClangSectionAction Action, PragmaClangSectionKind SecKind, StringRef SecName)
ActOnPragmaClangSection - Called on well formed #pragma clang section.
Definition: SemaAttr.cpp:384
bool UnifySection(StringRef SectionName, int SectionFlags, NamedDecl *TheDecl)
Definition: SemaAttr.cpp:792
void setExceptionMode(SourceLocation Loc, LangOptions::FPExceptionModeKind)
Called to set exception behavior for floating point operations.
Definition: SemaAttr.cpp:1434
void PrintPragmaAttributeInstantiationPoint()
Definition: SemaAttr.cpp:1220
void inferGslOwnerPointerAttribute(CXXRecordDecl *Record)
Add [[gsl::Owner]] and [[gsl::Pointer]] attributes for std:: types.
Definition: SemaAttr.cpp:167
const LangOptions & getLangOpts() const
Definition: Sema.h:524
SourceLocation CurInitSegLoc
Definition: Sema.h:1690
void ActOnPragmaMSVtorDisp(PragmaMsStackAction Action, SourceLocation PragmaLoc, MSVtorDispMode Value)
Called on well formed #pragma vtordisp().
Definition: SemaAttr.cpp:721
void inferLifetimeBoundAttribute(FunctionDecl *FD)
Add [[clang:::lifetimebound]] attr for std:: functions and methods.
Definition: SemaAttr.cpp:219
Preprocessor & PP
Definition: Sema.h:907
bool MSPragmaOptimizeIsOn
The "on" or "off" argument passed by #pragma optimize, that denotes whether the optimizations in the ...
Definition: Sema.h:1737
SmallVector< PragmaAttributeGroup, 2 > PragmaAttributeStack
Definition: Sema.h:1715
const LangOptions & LangOpts
Definition: Sema.h:906
PragmaClangSection PragmaClangRelroSection
Definition: Sema.h:1427
SourceLocation ImplicitMSInheritanceAttrLoc
Source location for newly created implicit MSInheritanceAttrs.
Definition: Sema.h:1404
void ProcessDeclAttributeList(Scope *S, Decl *D, const ParsedAttributesView &AttrList, const ProcessDeclAttributeOptions &Options=ProcessDeclAttributeOptions())
ProcessDeclAttributeList - Apply all the decl attributes in the specified attribute list to the speci...
void AddImplicitMSFunctionNoBuiltinAttr(FunctionDecl *FD)
Only called on function definitions; if there is a pragma in scope with the effect of a range-based n...
Definition: SemaAttr.cpp:1312
PragmaStack< AlignPackInfo > AlignPackStack
Definition: Sema.h:1639
PragmaStack< StringLiteral * > BSSSegStack
Definition: Sema.h:1649
llvm::StringMap< std::tuple< StringRef, SourceLocation > > FunctionToSectionMap
Sections used with #pragma alloc_text.
Definition: Sema.h:1693
llvm::SmallSetVector< StringRef, 4 > MSFunctionNoBuiltins
Set of no-builtin functions listed by #pragma function.
Definition: Sema.h:1740
void AddPushedVisibilityAttribute(Decl *RD)
AddPushedVisibilityAttribute - If '#pragma GCC visibility' was used, add an appropriate visibility at...
Definition: SemaAttr.cpp:1324
StringLiteral * CurInitSeg
Last section used with #pragma init_seg.
Definition: Sema.h:1689
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
Definition: Sema.h:1043
void ActOnPragmaMSFunction(SourceLocation Loc, const llvm::SmallVectorImpl< StringRef > &NoBuiltins)
Call on well formed #pragma function.
Definition: SemaAttr.cpp:1258
void ActOnPragmaMSStruct(PragmaMSStructKind Kind)
ActOnPragmaMSStruct - Called on well formed #pragma ms_struct [on|off].
Definition: SemaAttr.cpp:613
bool isPreciseFPEnabled()
Are precise floating point semantics currently enabled?
Definition: Sema.h:1829
void ActOnPragmaMSInitSeg(SourceLocation PragmaLocation, StringLiteral *SegmentName)
Called on well-formed #pragma init_seg().
Definition: SemaAttr.cpp:887
bool MSStructPragmaOn
Definition: Sema.h:1401
SourceManager & getSourceManager() const
Definition: Sema.h:529
void DiagnoseNonDefaultPragmaAlignPack(PragmaAlignPackDiagnoseKind Kind, SourceLocation IncludeLoc)
Definition: SemaAttr.cpp:547
PragmaClangSection PragmaClangTextSection
Definition: Sema.h:1428
void ActOnPragmaFPValueChangingOption(SourceLocation Loc, PragmaFPKind Kind, bool IsEnabled)
Called on well formed #pragma clang fp reassociate or #pragma clang fp reciprocal.
Definition: SemaAttr.cpp:1391
std::vector< std::pair< QualType, unsigned > > ExcessPrecisionNotSatisfied
Definition: Sema.h:7923
PragmaClangSection PragmaClangDataSection
Definition: Sema.h:1425
llvm::Error isValidSectionSpecifier(StringRef Str)
Used to implement to perform semantic checking on attribute((section("foo"))) specifiers.
void PushNamespaceVisibilityAttr(const VisibilityAttr *Attr, SourceLocation Loc)
PushNamespaceVisibilityAttr - Note that we've entered a namespace with a visibility attribute.
Definition: SemaAttr.cpp:1471
PragmaStack< MSVtorDispMode > VtorDispStack
Whether to insert vtordisps prior to virtual bases in the Microsoft C++ ABI.
Definition: Sema.h:1638
void AddMsStructLayoutForRecord(RecordDecl *RD)
AddMsStructLayoutForRecord - Adds ms_struct layout attribute to record.
Definition: SemaAttr.cpp:89
void * VisContext
VisContext - Manages the stack for #pragma GCC visibility.
Definition: Sema.h:1696
bool CheckAttrTarget(const ParsedAttr &CurrAttr)
ASTConsumer & Consumer
Definition: Sema.h:909
PragmaAlignPackDiagnoseKind
Definition: Sema.h:1807
Scope * TUScope
Translation Unit Scope - useful to Objective-C actions that need to lookup file scope declarations in...
Definition: Sema.h:871
void DiagnoseUnterminatedPragmaAttribute()
Definition: SemaAttr.cpp:1236
void FreeVisContext()
FreeVisContext - Deallocate and null out VisContext.
Definition: SemaAttr.cpp:1343
void ModifyFnAttributesMSPragmaOptimize(FunctionDecl *FD)
Only called on function definitions; if there is a MSVC #pragma optimize in scope,...
Definition: SemaAttr.cpp:1291
void PopPragmaVisibility(bool IsNamespaceEnd, SourceLocation EndLoc)
PopPragmaVisibility - Pop the top element of the visibility stack; used for '#pragma GCC visibility' ...
Definition: SemaAttr.cpp:1480
SourceManager & SourceMgr
Definition: Sema.h:911
void ActOnPragmaPack(SourceLocation PragmaLoc, PragmaMsStackAction Action, StringRef SlotLabel, Expr *Alignment)
ActOnPragmaPack - Called on well formed #pragma pack(...).
Definition: SemaAttr.cpp:433
DiagnosticsEngine & Diags
Definition: Sema.h:910
void DiagnoseUnterminatedPragmaAlignPack()
Definition: SemaAttr.cpp:586
FPOptions CurFPFeatures
Definition: Sema.h:904
PragmaStack< StringLiteral * > DataSegStack
Definition: Sema.h:1648
void ActOnPragmaVisibility(const IdentifierInfo *VisType, SourceLocation PragmaLoc)
ActOnPragmaVisibility - Called on well formed #pragma GCC visibility... .
Definition: SemaAttr.cpp:1357
SourceLocation OptimizeOffPragmaLocation
This represents the last location of a "#pragma clang optimize off" directive if such a directive has...
Definition: Sema.h:1724
LangOptions::PragmaMSPointersToMembersKind MSPointerToMemberRepresentationMethod
Controls member pointer representation format under the MS ABI.
Definition: Sema.h:1399
void ActOnPragmaMSStrictGuardStackCheck(SourceLocation PragmaLocation, PragmaMsStackAction Action, bool Value)
ActOnPragmaMSStrictGuardStackCheck - Called on well formed #pragma strict_gs_check.
Definition: SemaAttr.cpp:871
PragmaClangSection PragmaClangBSSSection
Definition: Sema.h:1424
PragmaOptionsAlignKind
Definition: Sema.h:1777
@ POAK_Power
Definition: Sema.h:1781
@ POAK_Reset
Definition: Sema.h:1783
@ POAK_Packed
Definition: Sema.h:1780
@ POAK_Mac68k
Definition: Sema.h:1782
@ POAK_Natural
Definition: Sema.h:1779
@ POAK_Native
Definition: Sema.h:1778
void ActOnPragmaFPContract(SourceLocation Loc, LangOptions::FPModeKind FPC)
ActOnPragmaFPContract - Called on well formed #pragma {STDC,OPENCL} FP_CONTRACT and #pragma clang fp ...
Definition: SemaAttr.cpp:1372
void ActOnPragmaOptimize(bool On, SourceLocation PragmaLoc)
Called on well formed #pragma clang optimize.
Definition: SemaAttr.cpp:1242
bool LookupName(LookupResult &R, Scope *S, bool AllowBuiltinCreation=false, bool ForceNoCPlusPlus=false)
Perform unqualified name lookup starting from a given scope.
void ActOnPragmaFEnvAccess(SourceLocation Loc, bool IsEnabled)
ActOnPragmaFenvAccess - Called on well formed #pragma STDC FENV_ACCESS.
Definition: SemaAttr.cpp:1442
void AddSectionMSAllocText(FunctionDecl *FD)
Only called on function definitions; if there is a #pragma alloc_text that decides which code section...
Definition: SemaAttr.cpp:1275
void ActOnPragmaMSSection(SourceLocation PragmaLocation, int SectionFlags, StringLiteral *SegmentName)
Called on well formed #pragma section().
Definition: SemaAttr.cpp:882
PragmaClangSectionKind
pragma clang section kind
Definition: Sema.h:1407
@ PCSK_BSS
Definition: Sema.h:1409
@ PCSK_Data
Definition: Sema.h:1410
@ PCSK_Text
Definition: Sema.h:1412
@ PCSK_Relro
Definition: Sema.h:1413
@ PCSK_Rodata
Definition: Sema.h:1411
void inferNullableClassAttribute(CXXRecordDecl *CRD)
Add _Nullable attributes for std:: types.
Definition: SemaAttr.cpp:317
PragmaMsStackAction
Definition: Sema.h:1430
@ PSK_Push_Set
Definition: Sema.h:1436
@ PSK_Reset
Definition: Sema.h:1431
@ PSK_Show
Definition: Sema.h:1435
@ PSK_Pop
Definition: Sema.h:1434
@ PSK_Set
Definition: Sema.h:1432
@ PSK_Push
Definition: Sema.h:1433
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
Definition: Stmt.h:84
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:345
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1778
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Expr.h:1959
StringRef getString() const
Definition: Expr.h:1855
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:136
bool hasAlignMac68kSupport() const
Check whether this target support '#pragma options align=mac68k'.
Definition: TargetInfo.h:966
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1333
Token - This structure provides full information about a lexed token.
Definition: Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:187
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:132
The base class of the type hierarchy.
Definition: Type.h:1828
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1916
bool isArrayType() const
Definition: Type.h:8258
bool isFunctionType() const
Definition: Type.h:8182
Base class for declarations which introduce a typedef-name.
Definition: Decl.h:3413
QualType getUnderlyingType() const
Definition: Decl.h:3468
Represents a variable declaration or definition.
Definition: Decl.h:882
Defines the clang::TargetInfo interface.
const internal::VariadicAllOfMatcher< Attr > attr
Matches attributes.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
llvm::DenseMap< int, SourceRange > ParsedSubjectMatchRuleSet
SubjectMatchRule
A list of all the recognized kinds of attributes.
const char * getSubjectMatchRuleSpelling(SubjectMatchRule Rule)
Definition: Attributes.cpp:72
bool isPointerLikeType(QualType QT)
The JSON file list parser is used to communicate input to InstallAPI.
PragmaFPKind
Definition: PragmaKinds.h:38
@ PFK_Reassociate
Definition: PragmaKinds.h:40
@ PFK_Reciprocal
Definition: PragmaKinds.h:41
@ CPlusPlus
Definition: LangStandard.h:55
PragmaMSCommentKind
Definition: PragmaKinds.h:14
@ Nullable
Values of this type can be null.
@ AANT_ArgumentConstantExpr
Definition: ParsedAttr.h:1082
@ Result
The result type of a method or function.
MSVtorDispMode
In the Microsoft ABI, this controls the placement of virtual displacement members used to implement v...
Definition: LangOptions.h:36
PragmaMSStructKind
Definition: PragmaKinds.h:23
@ PMSST_ON
Definition: PragmaKinds.h:25
PragmaFloatControlKind
Definition: PragmaKinds.h:28
@ PFC_NoExcept
Definition: PragmaKinds.h:33
@ PFC_NoPrecise
Definition: PragmaKinds.h:31
@ PFC_Pop
Definition: PragmaKinds.h:35
@ PFC_Precise
Definition: PragmaKinds.h:30
@ PFC_Except
Definition: PragmaKinds.h:32
@ PFC_Push
Definition: PragmaKinds.h:34
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:135
const FunctionProtoType * T
EvalResult is a struct with detailed info about an evaluated expression.
Definition: Expr.h:642
APValue Val
Val - This is the value the expression can be folded to.
Definition: Expr.h:644
SmallVectorImpl< PartialDiagnosticAt > * Diag
Diag - If this is non-null, it will be filled in with a stack of notes indicating why evaluation fail...
Definition: Expr.h:630
SourceLocation CurrentPragmaLocation
Definition: Sema.h:1643
This an attribute introduced by #pragma clang attribute.
Definition: Sema.h:1699
SourceLocation PragmaLocation
Definition: Sema.h:1421
ValueType CurrentValue
Definition: Sema.h:1624
void SentinelAction(PragmaMsStackAction Action, StringRef Label)
Definition: Sema.h:1610
SmallVector< Slot, 2 > Stack
Definition: Sema.h:1622
void Act(SourceLocation PragmaLocation, PragmaMsStackAction Action, llvm::StringRef StackSlotLabel, ValueType Value)
Definition: Sema.h:1561