[clang] [LifetimeSafety] Correctly place lifetimebound attr in corner cases (PR #181699)
Gábor Horváth via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 17 06:52:23 PST 2026
================
@@ -2952,10 +2958,24 @@ class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper {
unsigned DiagID = (Scope == SuggestionScope::CrossTU)
? diag::warn_lifetime_safety_cross_tu_this_suggestion
: diag::warn_lifetime_safety_intra_tu_this_suggestion;
- SourceLocation InsertionPoint;
- InsertionPoint = Lexer::getLocForEndOfToken(
- MD->getTypeSourceInfo()->getTypeLoc().getEndLoc(), 0,
- S.getSourceManager(), S.getLangOpts());
+ const auto MDL = MD->getTypeSourceInfo()->getTypeLoc();
+ SourceLocation InsertionPoint = Lexer::getLocForEndOfToken(
+ MDL.getEndLoc(), 0, S.getSourceManager(), S.getLangOpts());
+ if (const auto *FPT = MD->getType()->getAs<FunctionProtoType>();
+ FPT && FPT->hasTrailingReturn()) {
+ // For trailing return types, 'getEndLoc()' includes the return type
+ // after '->', placing the attribute in an invalid position.
+ // Instead use 'getLocalRangeEnd()' which gives the '->' location
+ // for trailing returns, so find the last token before it.
+ const auto FTL = MDL.getAs<FunctionTypeLoc>();
+ assert(FTL);
+ InsertionPoint = Lexer::getLocForEndOfToken(
----------------
Xazax-hun wrote:
Yup, moving these utils over to clang would make a lot of sense to me. Your proposal of adding a new header next to `Rewriter.h` sounds good to me.
https://github.com/llvm/llvm-project/pull/181699
More information about the cfe-commits
mailing list