This is an archive of the discontinued LLVM Phabricator instance.

Refactor clang-format's formatter.
ClosedPublic

Authored by klimek on May 11 2015, 7:45 AM.

Details

Reviewers
djasper
Summary

a) Pull out a class LevelIndentTracker whose responsibility is to keep track

of the indent of levels across multiple annotated lines.

b) Put all responsibility for merging lines into the LineJoiner; make the

LineJoiner iterate over the lines so we never operate on a line that might
be merged later; this makes the interface safer to use.

c) Move formatting of the end-of-file whitespace into formatFirstToken.

Fix bugs that became obvious after the refactoring:

  1. We would not format lines with offsets correctly inside nested blocks if only the outer expression was affected: int x = s({ clang-format only this line class X { public: ^ this starts at the non-modified indnent level; previously we would // not fix this, now we correctly outdent it. void f(); }; });
  2. We would incorrectly align comments across lines that do not have comments for lines with nested blocks: int expression; with comment int x = s({ int y; comment int z; we would incorrectly align this comment with the comment on 'expression' });

Diff Detail

Event Timeline

klimek updated this revision to Diff 25469.May 11 2015, 7:45 AM
klimek retitled this revision from to Refactor clang-format's formatter..
klimek updated this object.
klimek edited the test plan for this revision. (Show Details)
klimek added a reviewer: djasper.
klimek added a subscriber: Unknown Object (MLST).
djasper edited edge metadata.May 11 2015, 2:24 PM

I like it.

lib/Format/UnwrappedLineFormatter.cpp
28

I think "Tracks the indent level of \c AnnotatedLines across levels" is slightly clearer as this tracks the indent for multiple lines.

29

I think this should be a class, not a struct.

34

Just use:

for (unsigned i = 0; i != StartLevel; ++i) ...

StartLevel already is a POD type (unsigned even) and we don't get anything.

39–43

Ignore.. Phab doesn't let me delete this comment (Eclipse-like behavior).

62

Isn't this missing an

Offset = getIndentOffset(*Line.First);
106–110

As obvious from my comment above where I probably show that I am not understanding something, it is unclear how this needs to be a class variable instead of a variable calculated per line. Please comment.

142–154

Remove all the braces.

446–447

Can you change the order? My OCD finds it weird to have Next after End ;-).

815

This assert seems a bit odd ..

821–824

The actions for "Format" and "!Format" seem pretty interleaved here. Couldn't we group them more?

821–825

Both, the comment and the variable name are a bit confusing. Lets discuss this offline.

831–837

No braces.. Here and below.

894

Merge this line with the next (there already is a clang-format bug for that).

lib/Format/UnwrappedLineFormatter.h
53–55

If the implementation isn't in the header anymore, this might deserve a small comment.

klimek updated this revision to Diff 25558.May 12 2015, 1:39 AM
klimek edited edge metadata.

Implement review comments.

lib/Format/UnwrappedLineFormatter.cpp
28

Done.

29

Yep.

34

Done.

39–43

Ignored</irony>

62

Nope. Added class comment and local method comment.

106–110

I hope the additional comments on the methods clarify...

142–154

Done. ALL.

446–447

The problem is that my OCD finds it weird to have a non-const member in between the const ones. Here, I added an empty line, perhaps that helps?

815

Resolved via chat: moved around and duplicated the condition.

821–824

Resolved via chat: moved some of the code around.

821–825

Resolved via that: moved into the !Format part and rewrote comment.

831–837

Done.

894

Done.

lib/Format/UnwrappedLineFormatter.h
53–55

Yep.

djasper accepted this revision.May 12 2015, 2:05 AM
djasper edited edge metadata.

Looks very nice now.

lib/Format/UnwrappedLineFormatter.cpp
30

This should be stronger, i.e. updateIndentForLine must be called...

34

This should somehow include that updateLevelIndentFromUnmodifiedLine needs to be called *in addition* to updateIndentForLine.

51

I'd probably just call this nextLine.

71

maybe adjustToUnmodifiedLine

This revision is now accepted and ready to land.May 12 2015, 2:05 AM
klimek added inline comments.May 12 2015, 2:27 AM
lib/Format/UnwrappedLineFormatter.cpp
30

Done.

34

Done.

51

Done.

71

Done.

klimek closed this revision.May 12 2015, 2:27 AM

Commited as r237104