Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 44 additions & 12 deletions clang-tools-extra/clang-tidy/utils/UseRangesCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
StringRef SourceText = Lexer::getSourceText(
CharSourceRange::getCharRange({BeginLoc, EndLoc}),
Ctx.getSourceManager(), Ctx.getLangOpts(), &Invalid);
assert(!Invalid);

size_t I = 0;
while (I < SourceText.size() && SourceText[I] != ',') {
I++;
}

if (I < SourceText.size()) {
// also remove space after ,
size_t J = I + 1;
while (J < SourceText.size() && SourceText[J] == ' ') {
J++;
}

return std::make_optional(CharSourceRange::getCharRange(
{BeginLoc.getLocWithOffset(I), BeginLoc.getLocWithOffset(J)}));
}
return std::nullopt;
};

llvm::SmallVector<unsigned> Sorted(Indexes);
llvm::sort(Sorted);
// Keep track of commas removed
Expand All @@ -173,22 +200,27 @@ static void removeFunctionArgs(DiagnosticBuilder &Diag, const CallExpr &Call,
for (unsigned Index : Sorted) {
const Expr *Arg = Call.getArg(Index);
if (Commas[Index]) {
if (Index >= Commas.size()) {
Diag << FixItHint::CreateRemoval(Arg->getSourceRange());
} else {
if (Index + 1 < Call.getNumArgs()) {
// Remove the next comma
Commas[Index + 1] = true;
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
{Arg->getBeginLoc(),
Lexer::getLocForEndOfToken(
Arg->getEndLoc(), 0, Ctx.getSourceManager(), Ctx.getLangOpts())
.getLocWithOffset(1)}));
const Expr *NextArg = Call.getArg(Index + 1);
std::optional<CharSourceRange> CommaLoc = GetCommaLoc(
Arg->getEndLoc().getLocWithOffset(1), NextArg->getBeginLoc());
if (CommaLoc) {
Commas[Index + 1] = true;
Diag << FixItHint::CreateRemoval(*CommaLoc);
}
}
} else {
Diag << FixItHint::CreateRemoval(CharSourceRange::getTokenRange(
Arg->getBeginLoc().getLocWithOffset(-1), Arg->getEndLoc()));
Commas[Index] = true;
// At this point we know Index > 0 because `Commas[0] = true` earlier
const Expr *PrevArg = Call.getArg(Index - 1);
std::optional<CharSourceRange> CommaLoc = GetCommaLoc(
PrevArg->getEndLoc().getLocWithOffset(1), Arg->getBeginLoc());
if (CommaLoc) {
Commas[Index] = true;
Diag << FixItHint::CreateRemoval(*CommaLoc);
}
}
Diag << FixItHint::CreateRemoval(Arg->getSourceRange());
}
}

Expand Down
5 changes: 5 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ Changes in existing checks
<clang-tidy/checks/readability/identifier-naming>` check to
validate ``namespace`` aliases.

- Improved :doc:`modernize-use-ranges
<clang-tidy/checks/modernize/use-ranges>` and :doc:`boost-use-ranges
<clang-tidy/checks/boost/use-ranges>` check to more precisely remove
comma.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Loading