[clang-format] Add PackConstructorInitializers backward compat test

Add backward compatibility tests for mapping the deprecated
ConstructorInitializerAllOnOneLineOrOnePerLine and
AllowAllConstructorInitializersOnNextLine to
PackConstructorInitializers.

Differential Revision: https://reviews.llvm.org/D108882
This commit is contained in:
owenca 2021-08-28 15:21:23 -07:00
parent 3726039561
commit 4b1fde8a2b
2 changed files with 33 additions and 8 deletions

View file

@ -658,7 +658,14 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("PackConstructorInitializers",
Style.PackConstructorInitializers);
// For backward compatibility.
// For backward compatibility:
// The default value of ConstructorInitializerAllOnOneLineOrOnePerLine was
// false unless BasedOnStyle was Google or Chromium whereas that of
// AllowAllConstructorInitializersOnNextLine was always true, so the
// equivalent default value of PackConstructorInitializers is PCIS_NextLine
// for Google/Chromium or PCIS_BinPack otherwise. If the deprecated options
// had a non-default value while PackConstructorInitializers has a default
// value, set the latter to an equivalent non-default value if needed.
StringRef BasedOn;
IO.mapOptional("BasedOnStyle", BasedOn);
const bool IsGoogleOrChromium = BasedOn.equals_insensitive("google") ||
@ -668,17 +675,19 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("ConstructorInitializerAllOnOneLineOrOnePerLine",
OnCurrentLine);
IO.mapOptional("AllowAllConstructorInitializersOnNextLine", OnNextLine);
if (IsGoogleOrChromium &&
Style.PackConstructorInitializers == FormatStyle::PCIS_NextLine) {
if (!IsGoogleOrChromium) {
if (Style.PackConstructorInitializers == FormatStyle::PCIS_BinPack &&
OnCurrentLine) {
Style.PackConstructorInitializers = OnNextLine
? FormatStyle::PCIS_NextLine
: FormatStyle::PCIS_CurrentLine;
}
} else if (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;
}
IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);

View file

@ -18467,6 +18467,22 @@ TEST_F(FormatTest, ParsesConfiguration) {
PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
CHECK_PARSE("PackConstructorInitializers: NextLine",
PackConstructorInitializers, FormatStyle::PCIS_NextLine);
// For backward compatibility:
CHECK_PARSE("BasedOnStyle: Google\n"
"ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
"AllowAllConstructorInitializersOnNextLine: false",
PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
Style.PackConstructorInitializers = FormatStyle::PCIS_NextLine;
CHECK_PARSE("BasedOnStyle: Google\n"
"ConstructorInitializerAllOnOneLineOrOnePerLine: false",
PackConstructorInitializers, FormatStyle::PCIS_BinPack);
CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
"AllowAllConstructorInitializersOnNextLine: true",
PackConstructorInitializers, FormatStyle::PCIS_NextLine);
Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
CHECK_PARSE("ConstructorInitializerAllOnOneLineOrOnePerLine: true\n"
"AllowAllConstructorInitializersOnNextLine: false",
PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",