[PATCH] D91949: [clang-format] Add BeforeStructInitialization option in BraceWrapping configuration

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 10 02:26:05 PDT 2021


krasimir added a comment.

Do we have some widely used code style that requires the new option (https://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options)?
I agree with @MyDeveloperDay that this option as-is is likely not addressing all the cases where we would want this to apply.

I don't think we should later be adding something like `BreakBeforeClassInit` -- IMO whichever mechanism is used to control `struct` cases should also apply to `class` cases (modulo commonly used style guide rules).

Note that there is a big difference in context between this and, e.g., `AfterStruct`: when you're defining a `struct`, generally the struct keyword is required, and that's why the check `Line.startsWith(tok::kw_struct)` in that context is appropriate. In the context of initialization this is not the case, so, as a user I'd expect `BreakBeforeStructInit` to work in cases like this where the `struct` keyword is not present:

  struct point {
    int x, y;
  };
  
  int f() {
    // here:
    point p =
    {
      x = 1,
      y = 2,
    };
  }

Since clang-format doesn't really do any semantic analysis, it might be more appropriate to generalize this concept in a way that more closely matches the C++ syntax structures, e.g., to something that controls the breaking behavior before `{` in appropriate cases of some copy-list-initialization forms (https://en.cppreference.com/w/cpp/language/list_initialization), but without some style guide to drive the design it's hard to tell what would make sense.



================
Comment at: clang/unittests/Format/FormatTest.cpp:5063
+               "    a = 1,\n"
+               "    b = 2,\n"
+               "};",
----------------
anastasiia_lukianenko wrote:
> MyDeveloperDay wrote:
> > anastasiia_lukianenko wrote:
> > > MyDeveloperDay wrote:
> > > > could you add an additional test without the trailing `,`
> > > Yes, I can add the test, but before I want to discuss the expected result.
> > > Now with my patch and BeforeStructInitialization = false the behavior is the following:
> > > 
> > > ```
> > > struct new_struct struct_name = {a = 1};
> > > struct new_struct struct_name = {a = 1, b = 2};
> > > ```
> > > And with BeforeStructInitialization = true:
> > > 
> > > ```
> > > struct new_struct struct_name =
> > > {a = 1};
> > > struct new_struct struct_name =
> > > {a = 1, b = 2};
> > > ```
> > > Is it okay?
> > that is kind of what I expected, and adding the `,` is a known trick to cause it to expand the struct out.
> > 
> > I think its worth putting a test in, but I'll ask the same question what do you think it should look like?
> I agree with you. And I'll add these tests to patch.
I'd expect the lines starting with `{` to be indented in those cases, probably by +4 spaces in LLVM-like style (ContinuationIndent), consistently with other places where the RHS of a binary expression gets wrapped on a new line:
Not:
```
struct new_struct struct_name =
{a = 1};
struct new_struct struct_name =
{a = 1, b = 2};
```
But:
```
struct new_struct struct_name =
    {a = 1};
struct new_struct struct_name =
    {a = 1, b = 2};
```



CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D91949/new/

https://reviews.llvm.org/D91949



More information about the cfe-commits mailing list