15#ifndef LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H
16#define LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H
31 constexpr unsigned DiagIDs[] = {
32 diag::warn_lifetime_safety_use_after_scope,
33 diag::warn_lifetime_safety_use_after_scope_moved,
34 diag::warn_lifetime_safety_use_after_free,
35 diag::warn_lifetime_safety_return_stack_addr,
36 diag::warn_lifetime_safety_return_stack_addr_moved,
37 diag::warn_lifetime_safety_invalidation,
38 diag::warn_lifetime_safety_dangling_field,
39 diag::warn_lifetime_safety_dangling_field_moved,
40 diag::warn_lifetime_safety_dangling_global,
41 diag::warn_lifetime_safety_dangling_global_moved,
42 diag::warn_lifetime_safety_noescape_escapes,
43 diag::warn_lifetime_safety_lifetimebound_violation,
44 diag::warn_lifetime_safety_cross_tu_misplaced_lifetimebound,
45 diag::warn_lifetime_safety_intra_tu_misplaced_lifetimebound,
46 diag::warn_lifetime_safety_invalidated_field,
47 diag::warn_lifetime_safety_invalidated_global,
48 diag::warn_lifetime_safety_cross_tu_param_suggestion,
49 diag::warn_lifetime_safety_intra_tu_param_suggestion,
50 diag::warn_lifetime_safety_cross_tu_this_suggestion,
51 diag::warn_lifetime_safety_intra_tu_this_suggestion};
52 for (
unsigned DiagID : DiagIDs)
64 const Expr *MovedExpr,
66 unsigned DiagID = MovedExpr
67 ? diag::warn_lifetime_safety_use_after_scope_moved
68 : diag::warn_lifetime_safety_use_after_scope;
71 << getDiagSubjectDescription(IssueExpr) << IssueExpr->
getSourceRange();
73 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
75 S.Diag(FreeLoc, diag::note_lifetime_safety_destroyed_here);
76 S.Diag(UseExpr->
getExprLoc(), diag::note_lifetime_safety_used_here)
81 const Expr *MovedExpr)
override {
82 unsigned DiagID = MovedExpr
83 ? diag::warn_lifetime_safety_return_stack_addr_moved
84 : diag::warn_lifetime_safety_return_stack_addr;
87 << getDiagSubjectDescription(IssueExpr) << IssueExpr->
getSourceRange();
90 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
92 S.Diag(ReturnExpr->
getExprLoc(), diag::note_lifetime_safety_returned_here)
98 const Expr *MovedExpr,
100 unsigned DiagID = MovedExpr
101 ? diag::warn_lifetime_safety_dangling_field_moved
102 : diag::warn_lifetime_safety_dangling_field;
105 << getDiagSubjectDescription(IssueExpr)
106 << getDiagSubjectDescription(DanglingField)
109 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
112 diag::note_lifetime_safety_dangling_field_here)
118 const Expr *MovedExpr,
120 unsigned DiagID = MovedExpr
121 ? diag::warn_lifetime_safety_dangling_global_moved
122 : diag::warn_lifetime_safety_dangling_global;
125 << getDiagSubjectDescription(IssueExpr)
126 << getDiagSubjectDescription(DanglingGlobal)
129 S.Diag(MovedExpr->
getExprLoc(), diag::note_lifetime_safety_moved_here)
133 diag::note_lifetime_safety_dangling_static_here)
137 diag::note_lifetime_safety_dangling_global_here)
142 const Expr *InvalidationExpr)
override {
144 ? diag::warn_lifetime_safety_use_after_free
145 : diag::warn_lifetime_safety_invalidation;
147 ? diag::note_lifetime_safety_freed_here
148 : diag::note_lifetime_safety_invalidated_here;
151 S.Diag(InvalidationExpr->
getExprLoc(), UseDiag)
153 S.Diag(UseExpr->
getExprLoc(), diag::note_lifetime_safety_used_here)
157 const Expr *InvalidationExpr)
override {
160 ? diag::warn_lifetime_safety_use_after_free
161 : diag::warn_lifetime_safety_invalidation;
163 ? diag::note_lifetime_safety_freed_here
164 : diag::note_lifetime_safety_invalidated_here;
168 S.Diag(InvalidationExpr->
getExprLoc(), UseDiag)
170 S.Diag(UseExpr->
getExprLoc(), diag::note_lifetime_safety_used_here)
176 const Expr *InvalidationExpr)
override {
178 ? diag::note_lifetime_safety_freed_here
179 : diag::note_lifetime_safety_invalidated_here;
181 diag::warn_lifetime_safety_invalidated_field)
183 S.Diag(InvalidationExpr->
getExprLoc(), InvalidationDiag)
186 diag::note_lifetime_safety_dangling_field_here)
192 const Expr *InvalidationExpr)
override {
194 ? diag::note_lifetime_safety_freed_here
195 : diag::note_lifetime_safety_invalidated_here;
197 diag::warn_lifetime_safety_invalidated_field)
199 S.Diag(InvalidationExpr->
getExprLoc(), InvalidationDiag)
202 diag::note_lifetime_safety_dangling_field_here)
208 const Expr *InvalidationExpr)
override {
210 ? diag::note_lifetime_safety_freed_here
211 : diag::note_lifetime_safety_invalidated_here;
213 diag::warn_lifetime_safety_invalidated_global)
215 S.Diag(InvalidationExpr->
getExprLoc(), InvalidationDiag)
219 diag::note_lifetime_safety_dangling_static_here)
223 diag::note_lifetime_safety_dangling_global_here)
229 const Expr *InvalidationExpr)
override {
231 ? diag::note_lifetime_safety_freed_here
232 : diag::note_lifetime_safety_invalidated_here;
234 diag::warn_lifetime_safety_invalidated_global)
236 S.Diag(InvalidationExpr->
getExprLoc(), InvalidationDiag)
240 diag::note_lifetime_safety_dangling_static_here)
244 diag::note_lifetime_safety_dangling_global_here)
253 ? diag::warn_lifetime_safety_cross_tu_param_suggestion
254 : diag::warn_lifetime_safety_intra_tu_param_suggestion;
256 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(ParmToAnnotate);
262 if (
const auto *EscapeExpr =
Target.dyn_cast<
const Expr *>())
263 S.Diag(EscapeExpr->getBeginLoc(),
264 diag::note_lifetime_safety_suggestion_returned_here)
265 << EscapeExpr->getSourceRange();
266 else if (
const auto *EscapeField =
Target.dyn_cast<
const FieldDecl *>())
267 S.Diag(EscapeField->getLocation(),
268 diag::note_lifetime_safety_escapes_to_field_here)
269 << EscapeField->getSourceRange();
273 const ParmVarDecl *ParmWithLifetimebound)
override {
274 const auto *
Attr = ParmWithLifetimebound->
getAttr<LifetimeBoundAttr>();
275 StringRef ParamName = ParmWithLifetimebound->
getName();
276 bool HasName = ParamName.size() > 0;
278 diag::warn_lifetime_safety_lifetimebound_violation)
286 assert(
Attr &&
"Expected lifetimebound attribute");
288 diag::warn_lifetime_safety_lifetimebound_violation)
296 assert(
Attr &&
"Expected lifetimebound attribute");
299 ? diag::warn_lifetime_safety_cross_tu_misplaced_lifetimebound
300 : diag::warn_lifetime_safety_intra_tu_misplaced_lifetimebound;
302 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(FDecl);
308 if (IsMacro || InsertionPoint.isInvalid())
311 S.Diag(InsertionPoint, DiagID)
314 S.Diag(
Attr->
getLocation(), diag::note_lifetime_safety_lifetimebound_here)
322 const auto *
Attr = PVDDef->
getAttr<LifetimeBoundAttr>();
323 assert(
Attr &&
"Expected lifetimebound attribute");
326 ? diag::warn_lifetime_safety_cross_tu_misplaced_lifetimebound
327 : diag::warn_lifetime_safety_intra_tu_misplaced_lifetimebound;
329 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(PVDDecl);
335 if (IsMacro || InsertionPoint.isInvalid())
342 S.Diag(
Attr->
getLocation(), diag::note_lifetime_safety_lifetimebound_here)
348 const Expr *EscapeExpr)
override {
350 ? diag::warn_lifetime_safety_cross_tu_this_suggestion
351 : diag::warn_lifetime_safety_intra_tu_this_suggestion;
353 auto [InsertionPoint, FixItText] = getLifetimeBoundFixIt(MD);
355 S.Diag(InsertionPoint, DiagID)
360 diag::note_lifetime_safety_suggestion_returned_here)
365 const Expr *EscapeExpr)
override {
367 diag::warn_lifetime_safety_noescape_escapes)
371 diag::note_lifetime_safety_suggestion_returned_here)
378 diag::warn_lifetime_safety_noescape_escapes)
382 diag::note_lifetime_safety_escapes_to_field_here)
387 const VarDecl *EscapeGlobal)
override {
389 diag::warn_lifetime_safety_noescape_escapes)
393 diag::note_lifetime_safety_escapes_to_static_storage_here)
397 diag::note_lifetime_safety_escapes_to_global_here)
402 S.addLifetimeBoundToImplicitThis(
const_cast<CXXMethodDecl *
>(MD));
406 std::pair<SourceLocation, StringRef>
410 StringRef FixItText =
" [[clang::lifetimebound]]";
412 if (!
Decl->getIdentifier()) {
416 FixItText =
"[[clang::lifetimebound]] ";
417 }
else if (
Decl->hasDefaultArg()) {
423 return {InsertionPoint, FixItText};
426 std::pair<SourceLocation, StringRef>
427 getLifetimeBoundFixIt(
const CXXMethodDecl *MD) {
428 const auto MDL = MD->getTypeSourceInfo()->getTypeLoc();
430 MDL.getEndLoc(), 0, S.getSourceManager(), S.getLangOpts());
432 if (
const auto *FPT = MD->getType()->getAs<FunctionProtoType>();
433 FPT && FPT->hasTrailingReturn()) {
438 const auto FTL = MDL.getAs<FunctionTypeLoc>();
445 0, S.getSourceManager(), S.getLangOpts());
447 return {InsertionPoint,
" [[clang::lifetimebound]]"};
450 std::string getDiagSubjectDescription(
const ValueDecl *VD) {
452 llvm::raw_string_ostream
OS(Res);
457 }
else if (
const auto *Var = dyn_cast<VarDecl>(VD)) {
458 if (Var->isStaticLocal() || Var->isStaticDataMember())
459 OS <<
"static variable";
460 else if (Var->hasGlobalStorage())
461 OS <<
"global variable";
463 OS <<
"local variable";
468 VD->getNameForDiagnostic(OS, S.getPrintingPolicy(),
false);
473 std::string getDiagSubjectDescription(
const Expr *E) {
475 return "local temporary object";
477 if (
const auto *DRE = dyn_cast<DeclRefExpr>(E))
478 return getDiagSubjectDescription(DRE->getDecl());
Attr - This represents one attribute.
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
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.
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.
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.
DiagnosticsEngine & getDiagnostics() const
const LangOptions & getLangOpts() const
SourceManager & getSourceManager() 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
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 reportUseAfterReturn(const Expr *IssueExpr, const Expr *ReturnExpr, const Expr *MovedExpr) override
void reportMisplacedLifetimebound(WarningScope Scope, const ParmVarDecl *PVDDef, const ParmVarDecl *PVDDecl) override
void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const Expr *EscapeExpr) override
void reportUseAfterInvalidation(const ParmVarDecl *PVD, const Expr *UseExpr, const Expr *InvalidationExpr) override
void reportInvalidatedField(const ParmVarDecl *PVD, const FieldDecl *DanglingField, const Expr *InvalidationExpr) 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 reportInvalidatedGlobal(const ParmVarDecl *PVD, const VarDecl *DanglingGlobal, const Expr *InvalidationExpr) override
LifetimeSafetySemaHelperImpl(Sema &S)
void suggestLifetimeboundToParmVar(WarningScope Scope, const ParmVarDecl *ParmToAnnotate, EscapingTarget Target) override
void reportDanglingGlobal(const Expr *IssueExpr, const VarDecl *DanglingGlobal, const Expr *MovedExpr, SourceLocation ExpiryLoc) override
void reportInvalidatedField(const Expr *IssueExpr, const FieldDecl *DanglingField, const Expr *InvalidationExpr) override
void reportDanglingField(const Expr *IssueExpr, const FieldDecl *DanglingField, const Expr *MovedExpr, SourceLocation ExpiryLoc) override
void addLifetimeBoundToImplicitThis(const CXXMethodDecl *MD) override
void reportUseAfterInvalidation(const Expr *IssueExpr, const Expr *UseExpr, const Expr *InvalidationExpr) override
void reportMisplacedLifetimebound(WarningScope Scope, const CXXMethodDecl *FDef, const CXXMethodDecl *FDecl) override
void reportUseAfterScope(const Expr *IssueExpr, const Expr *UseExpr, const Expr *MovedExpr, SourceLocation FreeLoc) override
void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, const FieldDecl *EscapeField) override
void reportInvalidatedGlobal(const Expr *IssueExpr, const VarDecl *DanglingGlobal, const Expr *InvalidationExpr) override
void reportLifetimeboundViolation(const ParmVarDecl *ParmWithLifetimebound) override
llvm::PointerUnion< const Expr *, const FieldDecl *, const VarDecl * > EscapingTarget
LifetimeSafetySemaHelper()=default
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
const LifetimeBoundAttr * getDirectImplicitObjectLifetimeBoundAttr(const FunctionDecl *FD)
WarningScope
Enum to track functions visible across or within TU.
const LifetimeBoundAttr * getImplicitObjectParamLifetimeBoundAttr(const FunctionDecl *FD)
bool IsLifetimeSafetyEnabled(Sema &S, const Decl *D)
bool isa(CodeGen::Address addr)
SourceRange getSourceRange() const LLVM_READONLY
getSourceRange - The range of the declaration name.