[clang] [LifetimeSafety] Fix annotation suggestion fix-its for macro locations (PR #216818)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 17 12:49:05 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-temporal-safety
Author: Utkarsh Saxena (usx95)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/216818.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaLifetimeSafety.h (+10-2)
- (modified) clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp (+15)
``````````diff
diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h
index bffe1d61b0234..3251dfedb6a45 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -571,8 +571,12 @@ class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper {
std::pair<SourceLocation, std::string>
getLifetimeBoundFixIt(const ParmVarDecl *Decl) {
+ SourceLocation EndLoc = Decl->getEndLoc();
+ if (EndLoc.isMacroID())
+ EndLoc = S.getSourceManager().getExpansionRange(EndLoc).getEnd();
+
SourceLocation InsertionPoint = Lexer::getLocForEndOfToken(
- Decl->getEndLoc(), 0, S.getSourceManager(), S.getLangOpts());
+ EndLoc, 0, S.getSourceManager(), S.getLangOpts());
bool LeadingSpace = true;
if (!Decl->getIdentifier()) {
@@ -593,8 +597,12 @@ class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper {
std::pair<SourceLocation, std::string>
getLifetimeBoundFixIt(const CXXMethodDecl *MD) {
const auto MDL = MD->getTypeSourceInfo()->getTypeLoc();
+ SourceLocation EndLoc = MDL.getEndLoc();
+ if (EndLoc.isMacroID())
+ EndLoc = S.getSourceManager().getExpansionRange(EndLoc).getEnd();
+
SourceLocation InsertionPoint = Lexer::getLocForEndOfToken(
- MDL.getEndLoc(), 0, S.getSourceManager(), S.getLangOpts());
+ EndLoc, 0, S.getSourceManager(), S.getLangOpts());
if (const auto *FPT = MD->getType()->getAs<FunctionProtoType>();
FPT && FPT->hasTrailingReturn()) {
diff --git a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
index ed462f77008a8..2359818a8fbcd 100644
--- a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
+++ b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp
@@ -700,3 +700,18 @@ S foo() {
return S(create_up());
}
} // namespace GH193747
+
+namespace SuggestAroundMacro {
+#define ABSL_ATTR_THIS [[clang::annotate_type("a")]]
+#define ABSL_ATTR_DECL [[clang::annotate("a")]]
+struct S {
+ int x;
+ const int& getX() const ABSL_ATTR_THIS { return x; } // expected-warning {{implicit this in intra-TU function should be marked [[clang::lifetimebound]]}} expected-note {{param returned here}}
+
+ // Also test without macro
+ const int& getY() const { return x; } // expected-warning {{implicit this in intra-TU function should be marked [[clang::lifetimebound]]}} expected-note {{param returned here}}
+};
+
+// Parameter test mapping to ABSL_ATTR_DECL
+const int& getParam(const int& p ABSL_ATTR_DECL) { return p; } // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} expected-note {{param returned here}}
+} // namespace SuggestAroundMacro
``````````
</details>
https://github.com/llvm/llvm-project/pull/216818
More information about the cfe-commits
mailing list