[clang-tools-extra] [clang-tidy][clangd] Fixed removeFunctionArgs don't remove comma for use-ranges check (PR #118568)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 28 06:41:01 PST 2024


================
@@ -164,6 +164,33 @@ void UseRangesCheck::registerMatchers(MatchFinder *Finder) {
 static void removeFunctionArgs(DiagnosticBuilder &Diag, const CallExpr &Call,
                                ArrayRef<unsigned> Indexes,
                                const ASTContext &Ctx) {
+  auto GetCommaLoc =
+      [&](SourceLocation BeginLoc,
+          SourceLocation EndLoc) -> std::optional<CharSourceRange> {
+    auto Invalid = false;
+    auto SourceText = Lexer::getSourceText(
----------------
5chmidti wrote:

Doing all of this with strings is error-prone, which is why you are removing the comma inside a comment by accident, as you've noted.
Instead, use the Lexer through one of the utility functions here: https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clang-tidy/utils/LexerUtils.h

This can probably look something like:

```c++
auto RemovalRange = Arg->getSourceRange();
if (/*not first arg*/) { // so we can extend the range backward to the preceeding comma
  RemovalRange.setBegin(utils::lexer::findPreviousAnyTokenKind(RemovalRange.getBegin(), SM, LO, tok::TokenKind::comma));
} else if (/*not the only arg*/) { // so we can extend the range forwards to the next comma
  RemovalRange.setEnd(findNextAnyTokenKind(RemovalRange.getEnd(), SM, LO, tok::TokenKind::comma));
}
```

https://github.com/llvm/llvm-project/pull/118568


More information about the cfe-commits mailing list