This is an archive of the discontinued LLVM Phabricator instance.

Added calculateRangesAfterReplaments() to calculate affacted ranges in the new code.
ClosedPublic

Authored by ioeric on Jun 20 2016, 10:48 PM.

Diff Detail

Event Timeline

ioeric updated this revision to Diff 61337.Jun 20 2016, 10:48 PM
ioeric retitled this revision from to Added calculateRangesAfterReplaments() to calculate affacted ranges in the new code..
ioeric updated this object.
ioeric added reviewers: djasper, klimek.
ioeric added a subscriber: cfe-commits.
ioeric updated this revision to Diff 61340.Jun 21 2016, 12:45 AM
  • fixed commenting.
djasper added inline comments.Jun 21 2016, 3:35 AM
include/clang/Tooling/Core/Replacement.h
73

Can we keep these private for now?

238

Should we make the latter optional?

241

s/Replacemments/Replacements/

246

I'd call this getShiftedRanges().

247

const vector&?

lib/Tooling/Core/Replacement.cpp
39

No need for parentheses.

330

Remove " a"

331

Just return the new vector and use a const parameter.

354

Couldn't we implement this using mergeReplacements?

Fundamentally:

  • Turn Ranges into Replacements at (offset, length) with an empty (unimportant) replacement text of length "length".
  • Merge these into Replaces
  • Convert the resulting Replacements into Ranges, each with the Replacement's (offset, replacement_text.size()) and shifting subsequent ones forward.
ioeric updated this revision to Diff 61359.Jun 21 2016, 5:17 AM
ioeric marked 6 inline comments as done.
  • Use mergeReplacements to calculate the new affected ranges.
ioeric added inline comments.Jun 21 2016, 5:19 AM
include/clang/Tooling/Core/Replacement.h
238

Why would why want to exclude the newly affected ranges? But if replaces overlap with original ranges, do we consider sub-ranges in original ranges being replaced as parts of the shifted ranges in the new code?

246

getShiftedRanges sounds like it's excluding affected ranges of Replaces. I think often time we want both?

lib/Tooling/Core/Replacement.cpp
331

Shall we pass Ranges by value instead since it's gonna be sorted anyway?

Also, does it make sense to have calculateChangedRanges() call this?

354

That actually works very well! Thanks!

djasper edited edge metadata.Jun 21 2016, 7:21 AM

Nice :)

include/clang/Tooling/Core/Replacement.h
62

Can we just add a lambda that does this to the std::sort operation? I am not sure that this is actually a natural order for all Replacements.

69

Why is this required?

lib/Tooling/Core/Replacement.cpp
287

maybe:

std::vector<Range> Result;
for (const auto &R: Ranges) {
  if (Result.empty() || !Result.back().overlapsWith(R)) {
    Result.push_back(R);
  } else {
    unsigned NewEnd = std::max(Result.back.getOffset() + Result.back.getLength(), R.getOffset() + R.getLength());
    Result[Result.size() - 1] = Range(Result.back.getOffset(), NewEnd - Result.back.getOffset());
  }
}
320

I'd probably leave these early exits out. I don't think anyone will ever notice the performance difference.

322

I think you need to do:

Ranges = mergeAndSortRanges(Ranges);

here

ioeric updated this revision to Diff 61375.Jun 21 2016, 8:08 AM
ioeric marked 5 inline comments as done and an inline comment as not done.
ioeric edited edge metadata.
  • Addressed reviewer's comments.
include/clang/Tooling/Core/Replacement.h
69

It is used in unit test to test the equality between two ranges.

lib/Tooling/Core/Replacement.cpp
287

Thanks! Just one minor point: we also want to merge ranges R1 and R2 if

R1.getOffset() + R1.getLength() == R2.getOffset()

For example, if we have R1(1, 1) and R2(2, 1), then we can merge them into R3(1, 2).

djasper accepted this revision.Jun 21 2016, 9:58 AM
djasper edited edge metadata.

A couple of small comments, otherwise looks good.

lib/Tooling/Core/Replacement.cpp
284

I don't think you need "-> bool".

285

or std::tie(LHS.getOffset(), LHS.getLength()) < std::tie(RHS.getOffset(), RHS.getLength())?

292

Maybe pull out:

unsigned CurrentEnd = Result.back().getOffset() + Result.back().getLength();

Will probably save a line or two :)

This revision is now accepted and ready to land.Jun 21 2016, 9:58 AM
ioeric updated this revision to Diff 61405.Jun 21 2016, 10:53 AM
ioeric marked 2 inline comments as done.
ioeric edited edge metadata.
  • removed redundant return type in lambda function.
lib/Tooling/Core/Replacement.cpp
285

I tried std::tie(args...), but it takes references of args...and return values are temp values.

292

But there is Result.empty()...?

This revision was automatically updated to reflect the committed changes.