Clang-format: Braces Indent Style Whitesmiths

Hi all,

I was looking into the clang tools to experiment with; hoping to convince some colleagues that tooling can save us time.
Anyhow; since I was trying to take baby steps; clang format looked like a very good start.
Unfortunately, we would not be programmers if we wouldn’t do things a bit different than the rest of the world.

The current code; with too many files; uses the Whitesmiths style at most places.
Though this is not yet supported by clang format.
Going through the code, adding support for it did not look like a big thing; since Allman and GNU are very similar.
The thing I did not realize is that both of them indent all code by one level compared to the {,
which is not done by Whitesmiths style.

I already wrote a unit test, based on the Allman unit test; useful for test driven development
and experimented already with the code in lib/Format/UnwrappedLineParser.cpp
Though can’t seem to find out how to prevent the extra indent;
does anyone with more codebase experience have an idea where to start?

Hi all,

I was looking into the clang tools to experiment with; hoping to convince
some colleagues that tooling can save us time.
Anyhow; since I was trying to take baby steps; clang format looked like a
very good start.
Unfortunately, we would not be programmers if we wouldn't do things a bit
different than the rest of the world.

The current code; with too many files; uses the Whitesmiths style
<Indentation style - Wikipedia; at most
places.
Though this is not yet supported by clang format.
Going through the code, adding support for it did not look like a big
thing; since Allman and GNU are very similar.
The thing I did not realize is that both of them indent all code by one
level compared to the {,
which is not done by Whitesmiths style.

That actually shouldn't matter much. The whether or not to break before the
"{" is controlled by calling addUnwrappedLine() one more time whereas the
indentation is controlled by changing Line->Level. Basically all you should
need to do is to change the order of "addUnwrappedLine(); ++Line->Level;"
for the addUnwrappedLine() call for the "{". Unfortunately, there is no
nice abstraction for this and you'll need to look at how to do this for the
different kinds of syntactic elements individually. I can look into it and
give more help if you get stuck.

I already wrote a unit test, based on the Allman unit test; useful for test

Hi Daniel,

Most of appears to be working with the Whitesmiths; however I still have one group of tests failing.
These tests contain enumerations, the scenario is the following:

enum X
{
Y = 0
}

which gets formatted to:

enum X
{
Y = 0
}

If I call addUnwrappedLine() at the end of parseEnum() the result becomes the following:

enum X
{
Y = 0
}

The debug information tells me the following:

Line(0): enum[47] identifier[47]
Line(1): l_brace[47] identifier[47] equal[47] numeric_constant[47] r_brace[47]
Line(0): eof[47]

Hower for this result I expect it to be more like:

Line(0): enum[47] identifier[47]
Line(1): l_brace[47]
Line(2): identifier[47] equal[47] numeric_constant[47]
Line(1): r_brace[47]
Line(0): eof[47]

However I’m guessing that I am missing something obvious;
I do however suspect parseBracedList() from doing something which I don’t completely understand
OR something in the UnwrappedLineFormatter.cpp file to mess this up.

Any idea on where to look for the responsible?

whitesmiths.diff (20.5 KB)

Yeah. Enums are special. The reason is that we want to treat them somewhat close to other braced lists. They do not really contain individual statements or declarations and thus we parse them into a single unwrapped line. The actual wrapping/indentation is then done later in ContinuationIndenter.cpp. The logic might be a bit convoluted there, let me know if you need further help

One thing fixed, another one broken; that’s why you have tests.

I did found the ContinuationIndenter::getNewLineColumn(),
if I understand it right it tells you how many spaces are added in front of some symbol.
Though because this both handles the enum values and the braced lists, reducing the indent for the enums (return State.Stack.back().Indent - Style.IndentWidth;) results in all other stuff to get messed up.

This makes the following code:

void someLongFunction(
…int someLongParameter) const
…{
…}

into:

void someLongFunction(
…int someLongParameter) const
…{
…}

Is their any way to correct the indent selectively when parsing the enumeration and return it without incorrect correction in all other cases?

Could you clean up the patch (your previous version contained changes to some unrelated files) and upload it to phabricator (reviews.llvm.org)? I’d like to patch in the code, take a look and give you precise feedback.