aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/Sema.cpp
diff options
context:
space:
mode:
authorOliver Hunt <oliver@apple.com>2025-06-03 17:57:01 -0700
committerGitHub <noreply@github.com>2025-06-03 17:57:01 -0700
commitf72054a0ccecb88c694713c44f3f42352d2340a2 (patch)
treee7af6f48a2fab8cd3040fabc3c432c0144fa2303 /clang/lib/Sema/Sema.cpp
parent6be4670dfb902fe98d8b3f7935a6397ee96660e0 (diff)
downloadllvm-f72054a0ccecb88c694713c44f3f42352d2340a2.zip
llvm-f72054a0ccecb88c694713c44f3f42352d2340a2.tar.gz
llvm-f72054a0ccecb88c694713c44f3f42352d2340a2.tar.bz2
[clang] Correct FixIt ranges for unused capture warnings (#141148)
Fixes #106445 by using the lexer to find the correct range for the removal FixIts. Previously the ranges that were generated assuming no unsurprising formatting, which for the most part works. Being correct in all cases requires using the lexer to find the bounding tokens for the region to remove. As part of this it adds Sema::getRangeForNextToken to wrap Lexer::findNextToken.
Diffstat (limited to 'clang/lib/Sema/Sema.cpp')
-rw-r--r--clang/lib/Sema/Sema.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 925f2d4..370ade6 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -84,6 +84,28 @@ SourceLocation Sema::getLocForEndOfToken(SourceLocation Loc, unsigned Offset) {
return Lexer::getLocForEndOfToken(Loc, Offset, SourceMgr, LangOpts);
}
+SourceRange
+Sema::getRangeForNextToken(SourceLocation Loc, bool IncludeMacros,
+ bool IncludeComments,
+ std::optional<tok::TokenKind> ExpectedToken) {
+ if (!Loc.isValid())
+ return SourceRange();
+ std::optional<Token> NextToken =
+ Lexer::findNextToken(Loc, SourceMgr, LangOpts, IncludeComments);
+ if (!NextToken)
+ return SourceRange();
+ if (ExpectedToken && NextToken->getKind() != *ExpectedToken)
+ return SourceRange();
+ SourceLocation TokenStart = NextToken->getLocation();
+ SourceLocation TokenEnd = NextToken->getLastLoc();
+ if (!TokenStart.isValid() || !TokenEnd.isValid())
+ return SourceRange();
+ if (!IncludeMacros && (TokenStart.isMacroID() || TokenEnd.isMacroID()))
+ return SourceRange();
+
+ return SourceRange(TokenStart, TokenEnd);
+}
+
ModuleLoader &Sema::getModuleLoader() const { return PP.getModuleLoader(); }
DarwinSDKInfo *