15#ifndef LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H
16#define LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H
30 constexpr unsigned DiagIDs[] = {
31 diag::warn_lifetime_safety_use_after_scope,
32 diag::warn_lifetime_safety_use_after_scope_moved,
33 diag::warn_lifetime_safety_use_after_free,
34 diag::warn_lifetime_safety_return_stack_addr,
35 diag::warn_lifetime_safety_return_stack_addr_moved,
36 diag::warn_lifetime_safety_invalidation,
37 diag::warn_lifetime_safety_dangling_field,
38 diag::warn_lifetime_safety_dangling_field_moved,
39 diag::warn_lifetime_safety_dangling_field_lambda_capture,
40 diag::warn_lifetime_safety_dangling_global,
41 diag::warn_lifetime_safety_dangling_global_moved,
42 diag::warn_lifetime_safety_dangling_global_in_main,
43 diag::warn_lifetime_safety_invalidated_field,
44 diag::warn_lifetime_safety_invalidated_global};
45 for (
unsigned DiagID : DiagIDs)
53 diag::warn_lifetime_safety_noescape_escapes, D->
getBeginLoc());
58 diag::warn_lifetime_safety_lifetimebound_violation, D->
getBeginLoc());
63 diag::warn_lifetime_safety_capture_by_violation, D->
getBeginLoc());
68 constexpr unsigned DiagIDs[] = {
69 diag::warn_lifetime_safety_cross_tu_misplaced_lifetimebound,
70 diag::warn_lifetime_safety_intra_tu_misplaced_lifetimebound};
71 for (
unsigned DiagID : DiagIDs)
79 diag::warn_lifetime_safety_inapplicable_lifetimebound, D->
getBeginLoc());
84 diag::warn_lifetime_safety_field_origin_escape, D->
getBeginLoc());
89 constexpr unsigned DiagIDs[] = {
90 diag::warn_lifetime_safety_destructor_use_after_scope,
91 diag::warn_lifetime_safety_destructor_use_after_invalidation};
92 for (
unsigned DiagID : DiagIDs)
100 constexpr unsigned DiagIDs[] = {
101 diag::warn_lifetime_safety_intra_tu_param_suggestion,
102 diag::warn_lifetime_safety_cross_tu_param_suggestion,
103 diag::warn_lifetime_safety_intra_tu_ctor_param_suggestion,
104 diag::warn_lifetime_safety_cross_tu_ctor_param_suggestion,
105 diag::warn_lifetime_safety_intra_tu_this_suggestion,
106 diag::warn_lifetime_safety_cross_tu_this_suggestion};
107 for (
unsigned DiagID : DiagIDs)
124 if (S.
getLangOpts().EnableLifetimeSafetyTUAnalysis)
172 DiagID = diag::warn_lifetime_safety_destructor_use_after_scope;
174 DiagID = MovedExpr ? diag::warn_lifetime_safety_use_after_scope_moved
175 : diag::warn_lifetime_safety_use_after_scope;
176 std::string DestroyedSubject = getDiagSubjectDescription(IssueExpr);
181 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
183 S.Diag(FreeLoc, diag::note_lifetime_safety_destroyed_here)
185 reportAliasingChain(ExprChain);
190 const Expr *MovedExpr,
192 unsigned DiagID = MovedExpr
193 ? diag::warn_lifetime_safety_return_stack_addr_moved
194 : diag::warn_lifetime_safety_return_stack_addr;
197 << getDiagSubjectDescription(IssueExpr) << IssueExpr->
getSourceRange();
200 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
202 reportAliasingChain(ExprChain);
203 S.Diag(ReturnExpr->
getExprLoc(), diag::note_lifetime_safety_returned_here)
209 const Expr *MovedExpr,
bool IsCapturedByLambda,
214 ? diag::warn_lifetime_safety_dangling_field_lambda_capture
215 : (MovedExpr ? diag::warn_lifetime_safety_dangling_field_moved
216 : diag::warn_lifetime_safety_dangling_field);
219 << getDiagSubjectDescription(IssueExpr)
220 << getDiagSubjectDescription(DanglingField)
223 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
225 reportAliasingChain(ExprChain);
227 diag::note_lifetime_safety_dangling_field_here)
235 bool IsMain =
false)
override {
238 DiagID = MovedExpr ? diag::warn_lifetime_safety_dangling_global_moved
239 : diag::warn_lifetime_safety_dangling_global_in_main;
241 DiagID = MovedExpr ? diag::warn_lifetime_safety_dangling_global_moved
242 : diag::warn_lifetime_safety_dangling_global;
246 << getDiagSubjectDescription(IssueExpr)
247 << getDiagSubjectDescription(DanglingGlobal)
250 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
252 reportAliasingChain(ExprChain);
255 diag::note_lifetime_safety_dangling_static_here)
259 diag::note_lifetime_safety_dangling_global_here)
265 const Expr *InvalidationExpr,
267 reportUseAfterInvalidationImpl(
269 getDiagSubjectDescription(IssueExpr), InvalidationExpr, ExprChain, Use);
273 const Expr *InvalidationExpr,
275 reportUseAfterInvalidationImpl(
277 getDiagSubjectDescription(PVD), InvalidationExpr, ExprChain, Use);
282 const Expr *InvalidationExpr,
284 std::string InvalidatedSubject = getDiagSubjectDescription(IssueExpr);
286 diag::warn_lifetime_safety_invalidated_field)
287 << InvalidatedSubject << getDiagSubjectDescription(DanglingField)
289 reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
290 reportAliasingChain(ExprChain);
292 diag::note_lifetime_safety_dangling_field_here)
298 const Expr *InvalidationExpr,
300 std::string InvalidatedSubject = getDiagSubjectDescription(PVD);
302 diag::warn_lifetime_safety_invalidated_field)
303 << InvalidatedSubject << getDiagSubjectDescription(DanglingField)
305 reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
306 reportAliasingChain(ExprChain);
308 diag::note_lifetime_safety_dangling_field_here)
314 const Expr *InvalidationExpr,
316 std::string InvalidatedSubject = getDiagSubjectDescription(IssueExpr);
318 diag::warn_lifetime_safety_invalidated_global)
319 << InvalidatedSubject << getDiagSubjectDescription(DanglingGlobal)
321 reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
322 reportAliasingChain(ExprChain);
325 diag::note_lifetime_safety_dangling_static_here)
329 diag::note_lifetime_safety_dangling_global_here)
335 const Expr *InvalidationExpr,
337 std::string InvalidatedSubject = getDiagSubjectDescription(PVD);
339 diag::warn_lifetime_safety_invalidated_global)
340 << InvalidatedSubject << getDiagSubjectDescription(DanglingGlobal)
342 reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
343 reportAliasingChain(ExprChain);
346 diag::note_lifetime_safety_dangling_static_here)
350 diag::note_lifetime_safety_dangling_global_here)
360 ? diag::warn_lifetime_safety_cross_tu_ctor_param_suggestion
361 : diag::warn_lifetime_safety_intra_tu_ctor_param_suggestion;
364 ? diag::warn_lifetime_safety_cross_tu_param_suggestion
365 : diag::warn_lifetime_safety_intra_tu_param_suggestion;
367 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(ParmToAnnotate);
369 S.Diag(InsertionPoint, DiagID)
373 if (
const auto *EscapeExpr = dyn_cast<const Expr *>(
Target))
374 S.Diag(EscapeExpr->getBeginLoc(),
375 diag::note_lifetime_safety_suggestion_returned_here)
376 << EscapeExpr->getSourceRange();
377 else if (
const auto *EscapeField = dyn_cast<const FieldDecl *>(
Target))
378 S.Diag(EscapeField->getLocation(),
379 diag::note_lifetime_safety_escapes_to_field_here)
380 << EscapeField->getSourceRange();
384 const ParmVarDecl *ParmWithLifetimebound)
override {
385 const auto *
Attr = ParmWithLifetimebound->
getAttr<LifetimeBoundAttr>();
386 StringRef ParamName = ParmWithLifetimebound->
getName();
387 bool HasName = ParamName.size() > 0;
389 diag::warn_lifetime_safety_lifetimebound_violation)
397 assert(
Attr &&
"Expected lifetimebound attribute");
399 diag::warn_lifetime_safety_lifetimebound_violation)
404 const LifetimeCaptureByAttr *
Attr,
407 assert(!
Attr->
isImplicit() &&
"only written attributes are verified");
408 assert((CapturerIdx == LifetimeCaptureByAttr::This ||
409 CapturerIdx == LifetimeCaptureByAttr::Global) &&
410 "only this and global capturers are verified");
411 S.Diag(
Attr->
getLocation(), diag::warn_lifetime_safety_capture_by_violation)
412 << PVD << (CapturerIdx == LifetimeCaptureByAttr::Global)
416 unsigned NoteID = diag::note_lifetime_safety_escapes_to_global_here;
418 NoteID = diag::note_lifetime_safety_escapes_to_field_here;
420 VD->isStaticLocal() || VD->isStaticDataMember())
421 NoteID = diag::note_lifetime_safety_escapes_to_static_storage_here;
429 assert(
Attr &&
"Expected lifetimebound attribute");
432 ? diag::warn_lifetime_safety_cross_tu_misplaced_lifetimebound
433 : diag::warn_lifetime_safety_intra_tu_misplaced_lifetimebound;
435 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(FDecl);
441 if (IsMacro || InsertionPoint.isInvalid())
444 S.Diag(InsertionPoint, DiagID)
447 S.Diag(
Attr->
getLocation(), diag::note_lifetime_safety_lifetimebound_here)
455 const auto *
Attr = PVDDef->
getAttr<LifetimeBoundAttr>();
456 assert(
Attr &&
"Expected lifetimebound attribute");
459 ? diag::warn_lifetime_safety_cross_tu_misplaced_lifetimebound
460 : diag::warn_lifetime_safety_intra_tu_misplaced_lifetimebound;
462 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(PVDDecl);
468 if (IsMacro || InsertionPoint.isInvalid())
471 S.Diag(InsertionPoint, DiagID)
475 S.Diag(
Attr->
getLocation(), diag::note_lifetime_safety_lifetimebound_here)
480 assert(PVD->
hasAttr<LifetimeBoundAttr>() &&
481 "Expected parameter to have lifetimebound attribute");
482 const auto *
Attr = PVD->
getAttr<LifetimeBoundAttr>();
484 diag::warn_lifetime_safety_inapplicable_lifetimebound)
490 const Expr *EscapeExpr)
override {
492 ? diag::warn_lifetime_safety_cross_tu_this_suggestion
493 : diag::warn_lifetime_safety_intra_tu_this_suggestion;
495 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(MD);
497 S.Diag(InsertionPoint, DiagID)
502 diag::note_lifetime_safety_suggestion_returned_here)
507 const Expr *EscapeExpr)
override {
508 std::string FieldDesc = getDiagSubjectDescription(FD);
509 S.Diag(MD->
getLocation(), diag::warn_lifetime_safety_field_origin_escape)
512 S.Diag(EscapeExpr->
getBeginLoc(), diag::note_lifetime_safety_returned_here)
518 diag::note_lifetime_safety_owned_field_suggestion)
523 const Expr *EscapeExpr)
override {
525 diag::warn_lifetime_safety_noescape_escapes)
529 diag::note_lifetime_safety_suggestion_returned_here)
536 diag::warn_lifetime_safety_noescape_escapes)
540 diag::note_lifetime_safety_escapes_to_field_here)
545 const VarDecl *EscapeGlobal)
override {
547 diag::warn_lifetime_safety_noescape_escapes)
551 diag::note_lifetime_safety_escapes_to_static_storage_here)
555 diag::note_lifetime_safety_escapes_to_global_here)
560 S.addLifetimeBoundToImplicitThis(
const_cast<CXXMethodDecl *
>(MD));
566 static bool isFreeingExpr(
const Expr *InvalidationExpr) {
569 const FunctionDecl *FD = FunctionCallInfo(InvalidationExpr).FD;
573 struct LifetimeBoundMacroCache {
574 bool IsBuilt =
false;
578 void buildLifetimeBoundMacroCache(LifetimeBoundMacroCache &
Cache,
585 for (
const auto &M : PP.macros()) {
586 const IdentifierInfo *II = M.first;
587 const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
592 for (MacroDirective::DefInfo Def = MD->getDefinition(); Def;
593 Def = Def.getPreviousDefinition()) {
594 const MacroInfo *MI = Def.getMacroInfo();
595 if (MI->isObjectLike() && Tokens.size() == MI->getNumTokens() &&
596 std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin())) {
597 Cache.Candidates.push_back(II);
602 Cache.IsBuilt =
true;
605 StringRef getLastCachedMacroWithSpelling(SourceLocation Loc,
606 llvm::ArrayRef<TokenValue> Tokens,
607 LifetimeBoundMacroCache &
Cache) {
611 buildLifetimeBoundMacroCache(
Cache, Tokens);
613 const Preprocessor &PP = S.getPreprocessor();
614 const SourceManager &SM = S.getSourceManager();
615 SourceLocation BestLocation;
616 StringRef BestSpelling;
617 for (
const IdentifierInfo *II :
Cache.Candidates) {
618 const MacroDirective *MD = PP.getLocalMacroDirectiveHistory(II);
619 const MacroDirective::DefInfo Def = MD->findDirectiveAtLoc(Loc, SM);
620 if (!Def || !Def.getMacroInfo())
624 const MacroInfo *MI = Def.getMacroInfo();
625 if (!MI->isObjectLike() || Tokens.size() != MI->getNumTokens() ||
626 !std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin()))
630 SourceLocation Location = Def.getLocation();
631 assert(Location.isInvalid() ||
632 SM.isBeforeInTranslationUnit(Location, Loc));
633 if (BestLocation.isInvalid() ||
634 (Location.isValid() &&
635 SM.isBeforeInTranslationUnit(BestLocation, Location))) {
636 BestLocation = Location;
637 BestSpelling = II->getName();
643 void reportInvalidationSite(
const Expr *InvalidationExpr,
644 StringRef InvalidatedSubject) {
645 auto Diag = isFreeingExpr(InvalidationExpr)
646 ? diag::note_lifetime_safety_freed_here
647 : diag::note_lifetime_safety_invalidated_here;
648 S.Diag(InvalidationExpr->getExprLoc(),
Diag)
649 << InvalidatedSubject << InvalidationExpr->getSourceRange();
652 void reportUseAfterInvalidationImpl(SourceLocation IssueLoc,
653 SourceRange IssueRange,
654 StringRef InvalidatedSubject,
655 const Expr *InvalidationExpr,
656 llvm::ArrayRef<const Expr *> ExprChain,
657 const UseSite &Use) {
658 S.Diag(IssueLoc, getInvalidationWarnDiag(isFreeingExpr(InvalidationExpr),
660 << InvalidatedSubject << IssueRange;
661 reportInvalidationSite(InvalidationExpr, InvalidatedSubject);
662 reportAliasingChain(ExprChain);
666 static unsigned getInvalidationWarnDiag(
bool IsFree,
UseKind Kind) {
669 ? diag::warn_lifetime_safety_destructor_use_after_scope
670 : diag::warn_lifetime_safety_destructor_use_after_invalidation;
671 return IsFree ? diag::warn_lifetime_safety_use_after_free
672 : diag::warn_lifetime_safety_invalidation;
675 void reportUseNote(
const UseSite &Use) {
676 switch (Use.getKind()) {
678 S.Diag(Use.getLoc(), diag::note_lifetime_safety_used_here)
682 S.Diag(Use.getLoc(), diag::note_lifetime_safety_used_by_destructor)
683 << Use.getScopeExitVD();
686 S.Diag(Use.getLoc(), diag::note_lifetime_safety_used_by_cleanup)
687 << Use.getScopeExitVD();
690 llvm_unreachable(
"unknown UseKind");
693 std::string getLifetimeBoundFixItText(SourceLocation Loc,
bool LeadingSpace,
694 bool AllowGNUAttrMacro =
true) {
695 const bool UseCXX11AttrSpelling =
696 S.getLangOpts().CPlusPlus || S.getLangOpts().C23;
697 const StringRef Fallback = UseCXX11AttrSpelling
698 ?
"[[clang::lifetimebound]]"
699 :
"__attribute__((lifetimebound))";
700 StringRef Spelling = S.getLangOpts().LifetimeSafetyLifetimeBoundMacro;
701 if (Spelling.empty() && Loc.isValid()) {
702 const Preprocessor &PP = S.getPreprocessor();
703 if (UseCXX11AttrSpelling)
704 Spelling = getLastCachedMacroWithSpelling(
706 {tok::l_square, tok::l_square, PP.getIdentifierInfo(
"clang"),
707 tok::coloncolon, PP.getIdentifierInfo(
"lifetimebound"),
708 tok::r_square, tok::r_square},
709 ClangLifetimeBoundMacroCache);
711 if (Spelling.empty() && AllowGNUAttrMacro)
712 Spelling = getLastCachedMacroWithSpelling(
714 {tok::kw___attribute, tok::l_paren, tok::l_paren,
715 PP.getIdentifierInfo(
"lifetimebound"), tok::r_paren, tok::r_paren},
716 GNULifetimeBoundMacroCache);
718 const std::string
Text = Spelling.empty() ? Fallback.str() : Spelling.str();
719 return LeadingSpace ?
" " +
Text :
Text +
" ";
722 std::pair<SourceLocation, std::string>
723 getLifetimeBoundFixIt(
const ParmVarDecl *Decl) {
725 Decl->getEndLoc(), 0, S.getSourceManager(), S.getLangOpts());
726 bool LeadingSpace =
true;
728 if (!
Decl->getIdentifier()) {
731 InsertionPoint =
Decl->getBeginLoc();
732 LeadingSpace =
false;
733 }
else if (
Decl->hasDefaultArg()) {
737 Decl->getLocation(), 0, S.getSourceManager(), S.getLangOpts());
739 return {InsertionPoint,
740 getLifetimeBoundFixItText(InsertionPoint, LeadingSpace)};
743 std::pair<SourceLocation, std::string>
744 getLifetimeBoundFixIt(
const CXXMethodDecl *MD) {
745 const auto MDL = MD->getTypeSourceInfo()->getTypeLoc();
747 MDL.getEndLoc(), 0, S.getSourceManager(), S.getLangOpts());
749 if (
const auto *FPT = MD->getType()->getAs<FunctionProtoType>();
750 FPT && FPT->hasTrailingReturn()) {
755 const auto FTL = MDL.getAs<FunctionTypeLoc>();
762 0, S.getSourceManager(), S.getLangOpts());
764 return {InsertionPoint,
765 getLifetimeBoundFixItText(InsertionPoint,
true,
769 std::string getDiagSubjectDescription(
const ValueDecl *VD) {
771 llvm::raw_string_ostream
OS(Res);
776 }
else if (
const auto *Var = dyn_cast<VarDecl>(VD)) {
777 if (Var->isStaticLocal() || Var->isStaticDataMember())
778 OS <<
"static variable";
779 else if (Var->hasGlobalStorage())
780 OS <<
"global variable";
782 OS <<
"local variable";
787 VD->getNameForDiagnostic(OS, S.getPrintingPolicy(),
false);
792 std::string getDiagSubjectDescription(
const Expr *E) {
794 if (
const auto *DAE = dyn_cast<CXXDefaultArgExpr>(E))
795 return getDiagSubjectDescription(DAE->getExpr()->IgnoreParenBaseCasts());
797 return "temporary object";
799 return "allocated object";
800 if (
const auto *DRE = dyn_cast<DeclRefExpr>(E))
801 return getDiagSubjectDescription(DRE->getDecl());
802 if (
const auto *ME = dyn_cast<MemberExpr>(E))
803 if (
const auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl()))
804 return getDiagSubjectDescription(VD);
806 if (
const auto *CE = dyn_cast<CallExpr>(E)) {
807 const auto *FD = CE->getDirectCallee();
809 return "result of call";
811 llvm::raw_string_ostream
OS(Name);
812 FD->getNameForDiagnostic(OS, S.getPrintingPolicy(),
815 return "object allocated by '" + Name +
"'";
816 return "result of call to '" + Name +
"'";
823 bool shouldShowInAliasChain(
const Expr *CurrExpr,
const Expr *LastExpr) {
832 return CurrExpr->getSourceRange() != LastExpr->getSourceRange();
835 void reportAliasingChain(llvm::ArrayRef<const Expr *> OriginExprChain) {
836 if (OriginExprChain.empty())
839 const Expr *LastExpr = OriginExprChain.back();
840 const Expr *VisibleLastExpr = LastExpr;
841 std::string IssueStr = getDiagSubjectDescription(VisibleLastExpr);
843 for (
const Expr *CurrExpr : reverse(OriginExprChain.drop_back())) {
844 if (!shouldShowInAliasChain(CurrExpr, VisibleLastExpr)) {
848 std::optional<LifetimeBoundParamInfo> ParamInfo =
853 bool IsInferred =
true;
854 std::string ParamName;
855 if (!IsImplicitObject) {
857 if (
const auto *Attr = Param->getAttr<LifetimeBoundAttr>())
858 IsInferred = Attr->isImplicit();
859 ParamName = Param->getIdentifier()
860 ?
"'" + Param->getNameAsString() +
"'"
864 IsInferred = Attr->isImplicit();
866 S.Diag(CurrExpr->getBeginLoc(),
867 diag::note_lifetime_safety_aliases_storage_lifetimebound)
868 << CurrExpr->getSourceRange() << getDiagSubjectDescription(CurrExpr)
869 << IssueStr << IsImplicitObject << ParamName << IsInferred;
871 S.Diag(CurrExpr->getBeginLoc(),
872 diag::note_lifetime_safety_aliases_storage)
873 << CurrExpr->getSourceRange() << getDiagSubjectDescription(CurrExpr)
875 VisibleLastExpr = CurrExpr;
879 LifetimeBoundMacroCache ClangLifetimeBoundMacroCache;
880 LifetimeBoundMacroCache GNULifetimeBoundMacroCache;
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Defines the clang::Preprocessor interface.
TypePropertyCache< Private > Cache
Attr - This represents one attribute.
bool isImplicit() const
Returns true if the attribute has been implicitly created instead of explicitly written by the user.
SourceLocation getLocation() const
SourceRange getRange() const
Represents a static or instance method of a struct/union/class.
Decl - This represents one declaration (or definition), e.g.
SourceLocation getEndLoc() const LLVM_READONLY
SourceLocation getLocation() const
DeclContext * getDeclContext()
SourceLocation getBeginLoc() const LLVM_READONLY
SourceLocation getBeginLoc() const LLVM_READONLY
Concrete class used by the front-end to report problems and issues.
bool isIgnored(unsigned DiagID, SourceLocation Loc) const
Determine whether the diagnostic is known to be ignored.
This represents one expression.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Represents a member of a struct/union/class.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
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.
Represents a function declaration or definition.
DeclarationNameInfo getNameInfo() const
static std::optional< Token > findPreviousToken(SourceLocation Loc, const SourceManager &SM, const LangOptions &LangOpts, bool IncludeComments)
Finds the token that comes before the given location.
static SourceLocation getLocForEndOfToken(SourceLocation Loc, unsigned Offset, const SourceManager &SM, const LangOptions &LangOpts)
Computes the source location just past the end of the token at this source location.
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Represents a parameter to a function.
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Scope - A scope is a transient data structure that is used while parsing the program.
Sema - This implements semantic analysis and AST building for C.
Preprocessor & getPreprocessor() const
DiagnosticsEngine & getDiagnostics() const
const LangOptions & getLangOpts() const
Encodes a location in the source.
SourceLocation getBegin() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
SourceLocation getBeginLoc() const LLVM_READONLY
bool isPointerType() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
bool isStaticDataMember() const
Determines whether this is a static data member.
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
void reportInvalidatedField(const Expr *IssueExpr, const FieldDecl *DanglingField, const Expr *InvalidationExpr, llvm::ArrayRef< const Expr * > ExprChain) override
void reportMisplacedLifetimebound(WarningScope Scope, const ParmVarDecl *PVDDef, const ParmVarDecl *PVDDecl) override
void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const Expr *EscapeExpr) override
void reportInvalidatedGlobal(const Expr *IssueExpr, const VarDecl *DanglingGlobal, const Expr *InvalidationExpr, llvm::ArrayRef< const Expr * > ExprChain) override
void reportLifetimeboundViolation(const CXXMethodDecl *MDWithLifetimebound) override
void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const VarDecl *EscapeGlobal) override
void suggestLifetimeboundToImplicitThis(WarningScope Scope, const CXXMethodDecl *MD, const Expr *EscapeExpr) override
void reportUseAfterReturn(const Expr *IssueExpr, const Expr *ReturnExpr, const Expr *MovedExpr, llvm::ArrayRef< const Expr * > ExprChain) override
void reportUseAfterScope(const Expr *IssueExpr, const UseSite &Use, const Expr *MovedExpr, SourceLocation FreeLoc, llvm::ArrayRef< const Expr * > ExprChain) override
void reportInvalidatedField(const ParmVarDecl *PVD, const FieldDecl *DanglingField, const Expr *InvalidationExpr, llvm::ArrayRef< const Expr * > ExprChain) override
void reportFieldOriginEscape(const CXXMethodDecl *MD, const FieldDecl *FD, const Expr *EscapeExpr) override
LifetimeSafetySemaHelperImpl(Sema &S)
void suggestLifetimeboundToParmVar(WarningScope Scope, const ParmVarDecl *ParmToAnnotate, EscapingTarget Target) override
void reportCaptureByViolation(const ParmVarDecl *PVD, const LifetimeCaptureByAttr *Attr, int CapturerIdx, const ValueDecl *CapturedBy) override
void reportInapplicableLifetimebound(const ParmVarDecl *PVD) override
void reportDanglingField(const Expr *IssueExpr, const FieldDecl *DanglingField, const Expr *MovedExpr, bool IsCapturedByLambda, SourceLocation ExpiryLoc, llvm::ArrayRef< const Expr * > ExprChain) override
void addLifetimeBoundToImplicitThis(const CXXMethodDecl *MD) override
void reportUseAfterInvalidation(const Expr *IssueExpr, const UseSite &Use, const Expr *InvalidationExpr, llvm::ArrayRef< const Expr * > ExprChain) override
void reportMisplacedLifetimebound(WarningScope Scope, const CXXMethodDecl *FDef, const CXXMethodDecl *FDecl) override
void reportInvalidatedGlobal(const ParmVarDecl *PVD, const VarDecl *DanglingGlobal, const Expr *InvalidationExpr, llvm::ArrayRef< const Expr * > ExprChain) override
void reportUseAfterInvalidation(const ParmVarDecl *PVD, const UseSite &Use, const Expr *InvalidationExpr, llvm::ArrayRef< const Expr * > ExprChain) override
void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const FieldDecl *EscapeField) override
void reportDanglingGlobal(const Expr *IssueExpr, const VarDecl *DanglingGlobal, const Expr *MovedExpr, SourceLocation ExpiryLoc, llvm::ArrayRef< const Expr * > ExprChain, bool IsMain=false) override
void reportLifetimeboundViolation(const ParmVarDecl *ParmWithLifetimebound) override
llvm::PointerUnion< const Expr *, const FieldDecl *, const VarDecl * > EscapingTarget
LifetimeSafetySemaHelper()=default
Where and how an origin is used, for diagnostics.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool ShouldCheckCaptureByViolations(Sema &S, const Decl *D)
UseKind
How an origin is used: by an expression, or implicitly at scope exit by a destructor or a cleanup cal...
const Expr * ignoreParensAndImpCasts(const Expr *E)
bool isFreeingFunction(const FunctionDecl &FD)
const LifetimeBoundAttr * getDirectImplicitObjectLifetimeBoundAttr(const FunctionDecl *FD)
bool ShouldCheckLifetimeboundViolations(Sema &S, const Decl *D)
bool ShouldCheckFieldOriginEscape(Sema &S, const Decl *D)
LifetimeSafetyOpts GetLifetimeSafetyOpts(Sema &S, const Decl *D)
WarningScope
Enum to track functions visible across or within TU.
bool ShouldCheckDestructorUses(Sema &S, const Decl *D)
bool ShouldCheckNoescapeViolations(Sema &S, const Decl *D)
bool ShouldCheckInapplicableLifetimebound(Sema &S, const Decl *D)
const LifetimeBoundAttr * getImplicitObjectParamLifetimeBoundAttr(const FunctionDecl *FD)
std::optional< LifetimeBoundParamInfo > getTrackingInfoForCallArg(const Expr *Call, const Expr *Source)
bool ShouldSuggestLifetimeAnnotations(Sema &S, const Decl *D)
bool isAllocatingFunction(const FunctionDecl &FD)
bool ShouldCheckMisplacedLifetimebound(Sema &S, const Decl *D)
bool ShouldCheckSafety(Sema &S, const Decl *D)
bool IsLifetimeSafetyEnabled(Sema &S, const Decl *D)
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
bool isa(CodeGen::Address addr)
U cast(CodeGen::Address addr)
SourceRange getSourceRange() const LLVM_READONLY
getSourceRange - The range of the declaration name.
bool SuggestAnnotations
Whether to suggest lifetime annotations.
bool CheckNoescapeViolations
size_t MaxCFGBlocks
Maximum number of CFG blocks to analyze.
bool CheckMisplacedLifetimebound
bool CheckFieldOriginEscape
bool CheckInapplicableLifetimebound
bool CheckCaptureByViolations
bool CheckLifetimeboundViolations
bool CheckDestructorUses
Whether to model non-trivial destructors at scope exit as uses.