This is an archive of the discontinued LLVM Phabricator instance.

Make clang-format remove duplicate headers when sorting #includes.
ClosedPublic

Authored by ioeric on Aug 8 2016, 9:49 AM.

Details

Summary

When sorting #includes, #include directives that have the same text will be deduplicated when sorting #includes, and only the first #include in the duplicate #includes remains. If the Cursor is provided and put on a deleted #include, it will be put on the remaining #include in the duplicate #includes.

Diff Detail

Repository
rL LLVM

Event Timeline

ioeric updated this revision to Diff 67190.Aug 8 2016, 9:49 AM
ioeric retitled this revision from to Add an option to clang-format to remove duplicate headers..
ioeric updated this object.
ioeric added a reviewer: djasper.
ioeric added a subscriber: cfe-commits.
ioeric updated this revision to Diff 67191.Aug 8 2016, 9:52 AM
  • Check code that is not in affected range is not reformatted in lit test.
djasper added inline comments.Aug 10 2016, 1:04 AM
lib/Format/Format.cpp
1242 ↗(On Diff #67191)

This needs a comment on what it is actually doing (including what the intended behavior is). And that comment should also go into the patch description.

Also, it might be a good idea to pull this out into a function.

ioeric retitled this revision from Add an option to clang-format to remove duplicate headers. to Add an style option "DeduplicateIncludes" to clang-format to remove duplicate headers when sorting #includes..Aug 10 2016, 1:48 AM
ioeric updated this object.
djasper added inline comments.Aug 10 2016, 1:50 AM
include/clang/Format/Format.h
551 ↗(On Diff #67191)

Actually, how about we just do this unconditionally? I think people will always want it.

ioeric updated this revision to Diff 67483.Aug 10 2016, 1:52 AM
ioeric marked an inline comment as done.
  • Addressed review comments.
ioeric added inline comments.Aug 10 2016, 1:56 AM
include/clang/Format/Format.h
551 ↗(On Diff #67483)

Sounds good to me. I'll get rid of the option then.

ioeric updated this revision to Diff 67484.Aug 10 2016, 2:05 AM
  • Always deduplicate when sorting includes. Get rid of the option.
ioeric retitled this revision from Add an style option "DeduplicateIncludes" to clang-format to remove duplicate headers when sorting #includes. to Make clang-format remove duplicate headers when sorting #includes..Aug 10 2016, 2:05 AM
ioeric updated this object.
djasper added inline comments.Aug 10 2016, 2:09 AM
lib/Format/Format.cpp
1224 ↗(On Diff #67483)

First off, make this return a pair<unsigned, unsigned> with the two values.

And then the comment needs to be more precise:

// Returns a pair (Index, OffsetToEOL) describing the position of the cursor
// before sorting/deduplicating. Index is the index of the include under the cursor
// in the original set of includes. If this include has duplicates, it is the index of
// the first of the duplicates as the others are going to be removed. OffsetToEOL
// Describes the cursor's position relative to the end of its current line.
1260 ↗(On Diff #67483)

Fix indent while here.

1306 ↗(On Diff #67483)

This can also be calculated upfront with:

Includes.back().Offset + Includes.back().Text.size() - Includes.front().Offset

And maybe you can pull some of those out into variables as they are also used in the very first line of this function.

ioeric updated this revision to Diff 67489.Aug 10 2016, 2:26 AM
ioeric marked 3 inline comments as done.
  • Addressed review comments.
djasper accepted this revision.Aug 10 2016, 2:27 AM
djasper edited edge metadata.

Looks good.

This revision is now accepted and ready to land.Aug 10 2016, 2:27 AM
This revision was automatically updated to reflect the committed changes.