This is an archive of the discontinued LLVM Phabricator instance.

Implement tooling::Replacements as a class.
ClosedPublic

Authored by ioeric on Jun 27 2016, 4:57 AM.

Details

Summary
  • Implement clang::tooling::Replacements as a class to provide interfaces to control how replacements for a single file are combined and provide guarantee on the order of replacements being applied.
  • tooling::Replacements only contains replacements for the same file now. Use std::map<std::string, tooling::Replacements> to represent multi-file replacements.
  • Error handling for the interface change will be improved in followup patches.

Diff Detail

Event Timeline

ioeric updated this revision to Diff 61957.Jun 27 2016, 4:57 AM
ioeric retitled this revision from to Implement tooling::Replacements as a class..
ioeric updated this object.
ioeric updated this object.Jun 29 2016, 3:55 AM
ioeric removed a subscriber: klimek.
ioeric updated this revision to Diff 62204.Jun 29 2016, 4:45 AM
  • minor changes
ioeric updated this revision to Diff 63662.Jul 12 2016, 3:22 AM
  • Merged with origin/master
ioeric added inline comments.Jul 12 2016, 3:35 AM
lib/Format/Format.cpp
859

Not sure about the appropriate error handling here.

Another option is calculating the range of the conflicting replacement in the code after replacements are applied and then use merge to add the replacement with new range. For example:

auto Err = Replaces.add(R);
if (Err) {
  auto NewRanges = calculateRangesAfterReplacements(Replaces, {R.getRange()});
  R = tooling::Replacement(R.getFilePath(), NewRange.getOffset(), NewRange.getLength(), R.getReplacementText());
  Replaces = Replaces.merge(R);
}

And we might want to make this a member function as well?

ioeric updated this object.
ioeric added a subscriber: cfe-commits.
klimek added inline comments.Jul 14 2016, 4:48 AM
include/clang/Tooling/Core/Replacement.h
160

a conflict

163

This prevents users from adding order-dependent replacements.

172โ€“173

That sentence doesn't parse...

176

merges

179

Here, above and below:
Don't start function comments with "This" or the function name - just leave it out, like this:
"Returns the affected ranges ..."

lib/Tooling/Core/Replacement.cpp
299

I'd probably add a single-replacement constructor for convenience.

305

So, this doesn't do the same as Replacements::merge, right? I think we're getting into a bit confusing terminology - perhaps we can find a better name for this?

327

This function could need a comment on how it actually works - I don't remember, and the function names called don't really give us a good explanation. I know this is just code you're moving, but I think we can make it a bit easier to understand while we're at it :)

ioeric updated this revision to Diff 64016.Jul 14 2016, 11:20 AM
ioeric marked 6 inline comments as done.
ioeric added inline comments.Jul 15 2016, 2:58 AM
lib/Tooling/Core/Replacement.cpp
299

Right...added single-replacement constructor and removed merge(R).

305

Changed to combineAndSortRanges...not sure if this is better though

klimek added inline comments.Jul 20 2016, 5:50 AM
include/clang/Tooling/Core/Replacement.h
145โ€“150

s/This //

lib/Tooling/Core/Replacement.cpp
144

DO NOT SCREAM!!! :D

305

I believe it's better. Can you add a comment expanding what this is for, so we don't have to re-think about that next time we stumble over it? :)

ioeric updated this revision to Diff 64675.Jul 20 2016, 6:10 AM
ioeric marked 5 inline comments as done.
  • Addressed review comments.
klimek added inline comments.Jul 26 2016, 7:20 AM
unittests/Tooling/RefactoringTest.cpp
102

Looks like we should put this function into some header, so we don't need to re-implement it in every test?

ioeric updated this revision to Diff 65686.Jul 27 2016, 2:12 AM
  • Moved ReplacementTest and toReplacements to ReplacementTest.h
  • Merge branch 'master' of http://llvm.org/git/clang into replace
ioeric updated this revision to Diff 65687.Jul 27 2016, 2:17 AM
  • Clean up ReplacemenTest.h header a bit.
ioeric marked an inline comment as done.Jul 27 2016, 2:20 AM
ioeric added inline comments.
unittests/Tooling/RefactoringTest.cpp
102

Done.

Added unittests/Tooling/ReplacementTest.h that contains ReplacementTest class and toReplacements, both of which are used in multiple test files.

klimek accepted this revision.Jul 27 2016, 5:12 AM
klimek edited edge metadata.

lg

This revision is now accepted and ready to land.Jul 27 2016, 5:12 AM
ioeric updated this revision to Diff 66093.Jul 29 2016, 1:53 AM
ioeric marked an inline comment as done.
ioeric edited edge metadata.
  • getShiftedCodePosition: do not minus 1 when there is no replacement text.
ioeric updated this revision to Diff 66294.Aug 1 2016, 3:16 AM
This revision was automatically updated to reflect the committed changes.