| 1 | //===--- SemaLifetimeSafety.h - Sema support for lifetime safety =---------==// |
| 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 defines the Sema-specific implementation for lifetime safety |
| 10 | // analysis. It provides diagnostic reporting and helper functions that bridge |
| 11 | // the lifetime safety analysis framework with Sema's diagnostic engine. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H |
| 16 | #define LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H |
| 17 | |
| 18 | #include "clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h" |
| 19 | #include "clang/Basic/DiagnosticSema.h" |
| 20 | #include "clang/Lex/Lexer.h" |
| 21 | #include "clang/Sema/Sema.h" |
| 22 | |
| 23 | namespace clang::lifetimes { |
| 24 | |
| 25 | inline bool IsLifetimeSafetyDiagnosticEnabled(Sema &S, const Decl *D) { |
| 26 | DiagnosticsEngine &Diags = S.getDiagnostics(); |
| 27 | return !Diags.isIgnored(DiagID: diag::warn_lifetime_safety_use_after_scope, |
| 28 | Loc: D->getBeginLoc()) || |
| 29 | !Diags.isIgnored(DiagID: diag::warn_lifetime_safety_use_after_scope_moved, |
| 30 | Loc: D->getBeginLoc()) || |
| 31 | !Diags.isIgnored(DiagID: diag::warn_lifetime_safety_return_stack_addr, |
| 32 | Loc: D->getBeginLoc()) || |
| 33 | !Diags.isIgnored(DiagID: diag::warn_lifetime_safety_return_stack_addr_moved, |
| 34 | Loc: D->getBeginLoc()) || |
| 35 | !Diags.isIgnored(DiagID: diag::warn_lifetime_safety_invalidation, |
| 36 | Loc: D->getBeginLoc()) || |
| 37 | !Diags.isIgnored(DiagID: diag::warn_lifetime_safety_noescape_escapes, |
| 38 | Loc: D->getBeginLoc()); |
| 39 | } |
| 40 | |
| 41 | class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper { |
| 42 | |
| 43 | public: |
| 44 | LifetimeSafetySemaHelperImpl(Sema &S) : S(S) {} |
| 45 | |
| 46 | void reportUseAfterFree(const Expr *IssueExpr, const Expr *UseExpr, |
| 47 | const Expr *MovedExpr, |
| 48 | SourceLocation FreeLoc) override { |
| 49 | S.Diag(Loc: IssueExpr->getExprLoc(), |
| 50 | DiagID: MovedExpr ? diag::warn_lifetime_safety_use_after_scope_moved |
| 51 | : diag::warn_lifetime_safety_use_after_scope) |
| 52 | << IssueExpr->getSourceRange(); |
| 53 | if (MovedExpr) |
| 54 | S.Diag(Loc: MovedExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_moved_here) |
| 55 | << MovedExpr->getSourceRange(); |
| 56 | S.Diag(Loc: FreeLoc, DiagID: diag::note_lifetime_safety_destroyed_here); |
| 57 | S.Diag(Loc: UseExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_used_here) |
| 58 | << UseExpr->getSourceRange(); |
| 59 | } |
| 60 | |
| 61 | void reportUseAfterReturn(const Expr *IssueExpr, const Expr *ReturnExpr, |
| 62 | const Expr *MovedExpr, |
| 63 | SourceLocation ExpiryLoc) override { |
| 64 | S.Diag(Loc: IssueExpr->getExprLoc(), |
| 65 | DiagID: MovedExpr ? diag::warn_lifetime_safety_return_stack_addr_moved |
| 66 | : diag::warn_lifetime_safety_return_stack_addr) |
| 67 | << IssueExpr->getSourceRange(); |
| 68 | if (MovedExpr) |
| 69 | S.Diag(Loc: MovedExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_moved_here) |
| 70 | << MovedExpr->getSourceRange(); |
| 71 | S.Diag(Loc: ReturnExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_returned_here) |
| 72 | << ReturnExpr->getSourceRange(); |
| 73 | } |
| 74 | |
| 75 | void reportDanglingField(const Expr *IssueExpr, |
| 76 | const FieldDecl *DanglingField, |
| 77 | const Expr *MovedExpr, |
| 78 | SourceLocation ExpiryLoc) override { |
| 79 | S.Diag(Loc: IssueExpr->getExprLoc(), |
| 80 | DiagID: MovedExpr ? diag::warn_lifetime_safety_dangling_field_moved |
| 81 | : diag::warn_lifetime_safety_dangling_field) |
| 82 | << IssueExpr->getSourceRange(); |
| 83 | if (MovedExpr) |
| 84 | S.Diag(Loc: MovedExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_moved_here) |
| 85 | << MovedExpr->getSourceRange(); |
| 86 | S.Diag(Loc: DanglingField->getLocation(), |
| 87 | DiagID: diag::note_lifetime_safety_dangling_field_here) |
| 88 | << DanglingField->getEndLoc(); |
| 89 | } |
| 90 | |
| 91 | void reportDanglingGlobal(const Expr *IssueExpr, |
| 92 | const VarDecl *DanglingGlobal, |
| 93 | const Expr *MovedExpr, |
| 94 | SourceLocation ExpiryLoc) override { |
| 95 | S.Diag(Loc: IssueExpr->getExprLoc(), |
| 96 | DiagID: MovedExpr ? diag::warn_lifetime_safety_dangling_global_moved |
| 97 | : diag::warn_lifetime_safety_dangling_global) |
| 98 | << IssueExpr->getSourceRange(); |
| 99 | if (MovedExpr) |
| 100 | S.Diag(Loc: MovedExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_moved_here) |
| 101 | << MovedExpr->getSourceRange(); |
| 102 | if (DanglingGlobal->isStaticLocal() || DanglingGlobal->isStaticDataMember()) |
| 103 | S.Diag(Loc: DanglingGlobal->getLocation(), |
| 104 | DiagID: diag::note_lifetime_safety_dangling_static_here) |
| 105 | << DanglingGlobal->getEndLoc(); |
| 106 | else |
| 107 | S.Diag(Loc: DanglingGlobal->getLocation(), |
| 108 | DiagID: diag::note_lifetime_safety_dangling_global_here) |
| 109 | << DanglingGlobal->getEndLoc(); |
| 110 | } |
| 111 | |
| 112 | void reportUseAfterInvalidation(const Expr *IssueExpr, const Expr *UseExpr, |
| 113 | const Expr *InvalidationExpr) override { |
| 114 | S.Diag(Loc: IssueExpr->getExprLoc(), DiagID: diag::warn_lifetime_safety_invalidation) |
| 115 | << false << IssueExpr->getSourceRange(); |
| 116 | S.Diag(Loc: InvalidationExpr->getExprLoc(), |
| 117 | DiagID: diag::note_lifetime_safety_invalidated_here) |
| 118 | << InvalidationExpr->getSourceRange(); |
| 119 | S.Diag(Loc: UseExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_used_here) |
| 120 | << UseExpr->getSourceRange(); |
| 121 | } |
| 122 | void reportUseAfterInvalidation(const ParmVarDecl *PVD, const Expr *UseExpr, |
| 123 | const Expr *InvalidationExpr) override { |
| 124 | S.Diag(Loc: PVD->getSourceRange().getBegin(), |
| 125 | DiagID: diag::warn_lifetime_safety_invalidation) |
| 126 | << true << PVD->getSourceRange(); |
| 127 | S.Diag(Loc: InvalidationExpr->getExprLoc(), |
| 128 | DiagID: diag::note_lifetime_safety_invalidated_here) |
| 129 | << InvalidationExpr->getSourceRange(); |
| 130 | S.Diag(Loc: UseExpr->getExprLoc(), DiagID: diag::note_lifetime_safety_used_here) |
| 131 | << UseExpr->getSourceRange(); |
| 132 | } |
| 133 | |
| 134 | void suggestLifetimeboundToParmVar(SuggestionScope Scope, |
| 135 | const ParmVarDecl *ParmToAnnotate, |
| 136 | const Expr *EscapeExpr) override { |
| 137 | unsigned DiagID = |
| 138 | (Scope == SuggestionScope::CrossTU) |
| 139 | ? diag::warn_lifetime_safety_cross_tu_param_suggestion |
| 140 | : diag::warn_lifetime_safety_intra_tu_param_suggestion; |
| 141 | SourceLocation InsertionPoint = Lexer::getLocForEndOfToken( |
| 142 | Loc: ParmToAnnotate->getEndLoc(), Offset: 0, SM: S.getSourceManager(), LangOpts: S.getLangOpts()); |
| 143 | StringRef FixItText = " [[clang::lifetimebound]]" ; |
| 144 | if (!ParmToAnnotate->getIdentifier()) { |
| 145 | // For unnamed parameters, placing attributes after the type would be |
| 146 | // parsed as a type attribute, not a parameter attribute. |
| 147 | InsertionPoint = ParmToAnnotate->getBeginLoc(); |
| 148 | FixItText = "[[clang::lifetimebound]] " ; |
| 149 | } |
| 150 | S.Diag(Loc: ParmToAnnotate->getBeginLoc(), DiagID) |
| 151 | << ParmToAnnotate->getSourceRange() |
| 152 | << FixItHint::CreateInsertion(InsertionLoc: InsertionPoint, Code: FixItText); |
| 153 | S.Diag(Loc: EscapeExpr->getBeginLoc(), |
| 154 | DiagID: diag::note_lifetime_safety_suggestion_returned_here) |
| 155 | << EscapeExpr->getSourceRange(); |
| 156 | } |
| 157 | |
| 158 | void suggestLifetimeboundToImplicitThis(SuggestionScope Scope, |
| 159 | const CXXMethodDecl *MD, |
| 160 | const Expr *EscapeExpr) override { |
| 161 | unsigned DiagID = (Scope == SuggestionScope::CrossTU) |
| 162 | ? diag::warn_lifetime_safety_cross_tu_this_suggestion |
| 163 | : diag::warn_lifetime_safety_intra_tu_this_suggestion; |
| 164 | const auto MDL = MD->getTypeSourceInfo()->getTypeLoc(); |
| 165 | SourceLocation InsertionPoint = Lexer::getLocForEndOfToken( |
| 166 | Loc: MDL.getEndLoc(), Offset: 0, SM: S.getSourceManager(), LangOpts: S.getLangOpts()); |
| 167 | if (const auto *FPT = MD->getType()->getAs<FunctionProtoType>(); |
| 168 | FPT && FPT->hasTrailingReturn()) { |
| 169 | // For trailing return types, 'getEndLoc()' includes the return type |
| 170 | // after '->', placing the attribute in an invalid position. |
| 171 | // Instead use 'getLocalRangeEnd()' which gives the '->' location |
| 172 | // for trailing returns, so find the last token before it. |
| 173 | const auto FTL = MDL.getAs<FunctionTypeLoc>(); |
| 174 | assert(FTL); |
| 175 | InsertionPoint = Lexer::getLocForEndOfToken( |
| 176 | Loc: Lexer::findPreviousToken(Loc: FTL.getLocalRangeEnd(), SM: S.getSourceManager(), |
| 177 | LangOpts: S.getLangOpts(), |
| 178 | /*IncludeComments=*/IncludeComments: false) |
| 179 | ->getLocation(), |
| 180 | Offset: 0, SM: S.getSourceManager(), LangOpts: S.getLangOpts()); |
| 181 | } |
| 182 | S.Diag(Loc: InsertionPoint, DiagID) |
| 183 | << MD->getNameInfo().getSourceRange() |
| 184 | << FixItHint::CreateInsertion(InsertionLoc: InsertionPoint, |
| 185 | Code: " [[clang::lifetimebound]]" ); |
| 186 | S.Diag(Loc: EscapeExpr->getBeginLoc(), |
| 187 | DiagID: diag::note_lifetime_safety_suggestion_returned_here) |
| 188 | << EscapeExpr->getSourceRange(); |
| 189 | } |
| 190 | |
| 191 | void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, |
| 192 | const Expr *EscapeExpr) override { |
| 193 | S.Diag(Loc: ParmWithNoescape->getBeginLoc(), |
| 194 | DiagID: diag::warn_lifetime_safety_noescape_escapes) |
| 195 | << ParmWithNoescape->getSourceRange(); |
| 196 | |
| 197 | S.Diag(Loc: EscapeExpr->getBeginLoc(), |
| 198 | DiagID: diag::note_lifetime_safety_suggestion_returned_here) |
| 199 | << EscapeExpr->getSourceRange(); |
| 200 | } |
| 201 | |
| 202 | void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, |
| 203 | const FieldDecl *EscapeField) override { |
| 204 | S.Diag(Loc: ParmWithNoescape->getBeginLoc(), |
| 205 | DiagID: diag::warn_lifetime_safety_noescape_escapes) |
| 206 | << ParmWithNoescape->getSourceRange(); |
| 207 | |
| 208 | S.Diag(Loc: EscapeField->getLocation(), |
| 209 | DiagID: diag::note_lifetime_safety_escapes_to_field_here) |
| 210 | << EscapeField->getEndLoc(); |
| 211 | } |
| 212 | |
| 213 | void reportNoescapeViolation(const ParmVarDecl *ParmWithNoescape, |
| 214 | const VarDecl *EscapeGlobal) override { |
| 215 | S.Diag(Loc: ParmWithNoescape->getBeginLoc(), |
| 216 | DiagID: diag::warn_lifetime_safety_noescape_escapes) |
| 217 | << ParmWithNoescape->getSourceRange(); |
| 218 | if (EscapeGlobal->isStaticLocal() || EscapeGlobal->isStaticDataMember()) |
| 219 | S.Diag(Loc: EscapeGlobal->getLocation(), |
| 220 | DiagID: diag::note_lifetime_safety_escapes_to_static_storage_here) |
| 221 | << EscapeGlobal->getEndLoc(); |
| 222 | else |
| 223 | S.Diag(Loc: EscapeGlobal->getLocation(), |
| 224 | DiagID: diag::note_lifetime_safety_escapes_to_global_here) |
| 225 | << EscapeGlobal->getEndLoc(); |
| 226 | } |
| 227 | |
| 228 | void addLifetimeBoundToImplicitThis(const CXXMethodDecl *MD) override { |
| 229 | S.addLifetimeBoundToImplicitThis(MD: const_cast<CXXMethodDecl *>(MD)); |
| 230 | } |
| 231 | |
| 232 | private: |
| 233 | Sema &S; |
| 234 | }; |
| 235 | |
| 236 | } // namespace clang::lifetimes |
| 237 | |
| 238 | #endif // LLVM_CLANG_LIB_SEMA_SEMALIFETIMESAFETY_H |
| 239 | |