This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] C++11 braced lists should respect the SpacesInParentheses setting
ClosedPublic

Authored by mitchell-stellar on Oct 3 2019, 12:12 PM.

Details

Summary

According to the clang-format documentation, "Fundamentally, C++11 braced lists are formatted exactly like function calls would be formatted in their place. If the braced list follows a name (e.g. a type or variable name), clang-format formats as if the {} were the parentheses of a function call with that name."

This patch furthers the treatment of C++11 braced list braces as parentheses by respecting the SpacesInParentheses setting.

Diff Detail

Repository
rL LLVM

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptOct 3 2019, 12:12 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MyDeveloperDay added a comment.EditedOct 3 2019, 12:37 PM

Should we have SpacesInBraces? as a first class formatting option, given that we have?

SpaceBetweenBraces.SpacesInAngles = true;
SpaceBetweenBraces.SpacesInParentheses = true;
SpaceBetweenBraces.SpacesInSquareBrackets = true;

How would that interact with SpaceInEmptyParentheses

I don't think that's a good idea, considering the fact that braces can mean different things in different contexts, and it would cause trouble for existing clang-format settings.

If a hypothetical SpacesInBraces were false by default, then you could have clang-format output something like:

[](int foo) {bar(foo);}

Currently, it is [](int foo) { bar(foo); }.

If a hypothetical SpacesInBraces were true by default, then you could have clang-format output something like:

vector<int> x{ 1, 2, 3, 4 };

Currently, it is vector<int> x{1, 2, 3, 4};.

This patch is minimally invasive and adheres to the existing documentation.

Added additional unit tests to verify that the SpaceInEmptyParentheses setting is also correctly adhered to.

what if you have this

vector<int>{1, 2, 3};
new int[3]{1, 2, 3};
foo(int f);

but want this:

vector<int>{ 1, 2, 3 };
new int[3]{ 1, 2, 3 };
foo(int f);

wouldn't turning on SpacesInParentheses: true now mean you get

vector<int>{ 1, 2, 3 };
new int[3]{ 1, 2, 3 };
foo( int f );

so there is no way of still having foo(int f); without a space before the int and after the f?

Sorry did I miss something? Generally, I agree that the change is good for the braced case I just don't want to break everyones functions

If you want spaces in your C++11 initializers and nothing else, then you don't want the Cpp11BracedListStyle setting enabled. Turning it off gives you spaces inside without affecting anything else.

understood, ok LGTM

MyDeveloperDay accepted this revision.Oct 3 2019, 1:20 PM
This revision is now accepted and ready to land.Oct 3 2019, 1:20 PM

Thanks. Please commit on my behalf when you have a chance.

@mitchell-stellar I pulled the patch to commit it, but the unit tests fail, could you double-check them for me?

[       OK ] FormatTest.LayoutBraceInitializersInReturnStatement (13 ms)
[ RUN      ] FormatTest.LayoutCxx11BraceInitializers
C:/cygwin64/buildareas/clang/llvm-project/clang/unittests/Format/FormatTest.cpp(70): error:       Expected: Expected.str()
      Which is: "vector< int > x{ };"
To be equal to: format(Expected, Style)
      Which is: "vector< int > x{};"
Expected code is not stable
C:/cygwin64/buildareas/clang/llvm-project/clang/unittests/Format/FormatTest.cpp(72): error:       Expected: Expected.str()
      Which is: "vector< int > x{ };"
To be equal to: format(Code, Style)
      Which is: "vector< int > x{};"
C:/cygwin64/buildareas/clang/llvm-project/clang/unittests/Format/FormatTest.cpp(78): error:       Expected: Expected.str()
      Which is: "vector< int > x{ };"
To be equal to: format(test::messUp(Code), ObjCStyle)
      Which is: "vector< int > x{};"
[  FAILED  ] FormatTest.LayoutCxx11BraceInitializers (1024 ms)
[ RUN      ] FormatTest.FormatsBracedListsInColumnLayout

You are correct. I'll work on this.

Fixed empty parentheses unit test for C++11 braced initializer list.

LGTM and passes

[----------] Global test environment tear-down
[==========] 700 tests from 21 test cases ran. (67039 ms total)
[  PASSED  ] 700 tests.
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptOct 4 2019, 7:26 AM