From f72054a0ccecb88c694713c44f3f42352d2340a2 Mon Sep 17 00:00:00 2001 From: Oliver Hunt Date: Tue, 3 Jun 2025 17:57:01 -0700 Subject: [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. --- clang/lib/Sema/Sema.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'clang/lib/Sema/Sema.cpp') 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 ExpectedToken) { + if (!Loc.isValid()) + return SourceRange(); + std::optional 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 * -- cgit v1.1