Page MenuHomePhabricator

[clang-format] Group options that pack constructor initializers
ClosedPublic

Authored by owenpan on Aug 26 2021, 1:36 AM.

Details

Summary

Add a new option PackConstructorInitializers and deprecate the related options ConstructorInitializerAllOnOneLineOrOnePerLine and AllowAllConstructorInitializersOnNextLine. Below is the mapping:

PackConstructorInitializers  ConstructorInitializer... AllowAll...
        Never                            -                  -
        BinPack                        false                -
        CurrentLine                    true               false
        NextLine                       true               true

The option value Never fixes PR50549 by always placing each constructor initializer on its own line. Please see D105099 for previous discussions.

Diff Detail

Event Timeline

owenpan requested review of this revision.Aug 26 2021, 1:36 AM
owenpan created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptAug 26 2021, 1:36 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
MyDeveloperDay accepted this revision.Aug 26 2021, 5:58 AM

This looks good, I like to move towards one style, it was getting too confusing. Did you test this on a large code base at all? and maybe wait for one of the others to take a look.

I'll pull the patch to my local source and see if it makes any changes, but to be honest we only use the BreakConstructorInitializersBeforeComma option

This revision is now accepted and ready to land.Aug 26 2021, 5:58 AM
HazardyKnusperkeks added inline comments.
clang/unittests/Format/FormatTest.cpp
18470

Should there be a test for the mapping?

owenpan added inline comments.Aug 27 2021, 2:00 AM
clang/unittests/Format/FormatTest.cpp
18470

Ideally yes, but CHECK_PARSE can't check mappings of multiple to one, e.g.:

CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
            "AllowAllConstructorInitializersOnNextLine: false",
            PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
owenpan added inline comments.Aug 27 2021, 3:11 AM
clang/lib/Format/Format.cpp
662–673

Also need to handle the default value PCIS_NextLine for Google and Chromium styles:

StringRef BasedOn;
IO.mapOptional("BasedOnStyle", BasedOn);
const bool IsGoogleOrChromium = BasedOn.equals_insensitive("google") ||
                                BasedOn.equals_insensitive("chromium");
bool OnCurrentLine = IsGoogleOrChromium;
bool OnNextLine = IsGoogleOrChromium;
IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
               OnCurrentLine);
IO.mapOptional("AllowAllConstructorInitializersOnNextLine", OnNextLine);
if (IsGoogleOrChromium &&
    Style.PackConstructorInitializers == FormatStyle::PCIS_NextLine) {
  if (!OnCurrentLine)
    Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
  else if (!OnNextLine)
    Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
} else if (Style.PackConstructorInitializers == FormatStyle::PCIS_BinPack &&
           OnCurrentLine) {
  Style.PackConstructorInitializers = OnNextLine
                                          ? FormatStyle::PCIS_NextLine
                                          : FormatStyle::PCIS_CurrentLine;
}
owenpan added inline comments.Aug 27 2021, 3:22 AM
clang/lib/Format/Format.cpp
1194

Need to move this line down to keep the options sorted.

clang/unittests/Format/FormatTest.cpp
18461–18469

Will use the default PCIS_BinPack as the initial value and move the last CHECK_PARSE to the top.

owenpan updated this revision to Diff 369061.Aug 27 2021, 3:43 AM

Added backward compatibility for the default value PCIS_NextLine in Google and Chromium styles and aforementioned cleanups.

This looks good, I like to move towards one style, it was getting too confusing.

I agree!

Did you test this on a large code base at all?

No. I don't think it's necessary (as this patch doesn't impact breaking constructor initializers except perhaps for the new value Never, which is already covered by the new test cases) but will try.

I'll pull the patch to my local source and see if it makes any changes, but to be honest we only use the BreakConstructorInitializersBeforeComma option

Thanks!

owenpan marked an inline comment as done.Aug 28 2021, 3:45 PM
owenpan added inline comments.
clang/unittests/Format/FormatTest.cpp
18470