diff options
author | Oliver Hunt <oliver@apple.com> | 2025-06-03 17:57:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-03 17:57:01 -0700 |
commit | f72054a0ccecb88c694713c44f3f42352d2340a2 (patch) | |
tree | e7af6f48a2fab8cd3040fabc3c432c0144fa2303 /clang/lib/Sema/Sema.cpp | |
parent | 6be4670dfb902fe98d8b3f7935a6397ee96660e0 (diff) | |
download | llvm-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.cpp | 22 |
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 * |