curdeius created this revision. curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan. curdeius requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Fixes https://github.com/llvm/llvm-project/issues/44292. Fixes https://github.com/llvm/llvm-project/issues/45874. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D119419 Files: clang/lib/Format/WhitespaceManager.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -17277,6 +17277,31 @@ "const unsigned g;\n" "Const unsigned h;", Alignment); + + // See PR46529 + FormatStyle BracedAlign = getLLVMStyle(); + BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; + verifyFormat("const auto result{[]() {\n" + " const auto something = 1;\n" + " return 2;\n" + "}};\n", + BracedAlign); + verifyFormat("int foo{[]() {\n" + " int bar{0};\n" + " return 0;\n" + "}()};", + BracedAlign); + BracedAlign.Cpp11BracedListStyle = false; + verifyFormat("const auto result{ []() {\n" + " const auto something = 1;\n" + " return 2;\n" + "} };\n", + BracedAlign); + verifyFormat("int foo{ []() {\n" + " int bar{ 0 };\n" + " return 0;\n" + "}() };", + BracedAlign); } TEST_F(FormatTest, AlignWithLineBreaks) { Index: clang/lib/Format/WhitespaceManager.cpp =================================================================== --- clang/lib/Format/WhitespaceManager.cpp +++ clang/lib/Format/WhitespaceManager.cpp @@ -331,6 +331,10 @@ FoundMatchOnLine = true; Shift = Column - Changes[i].StartOfTokenColumn; Changes[i].Spaces += Shift; + if (Changes[i].NewlinesBefore == 0) + Changes[i].Spaces = + std::max(Changes[i].Spaces, + static_cast<int>(Changes[i].Tok->SpacesRequiredBefore)); } // This is for function parameters that are split across multiple lines, @@ -399,6 +403,12 @@ if (ContinuedStringLiteral) Changes[i].Spaces += Shift; + // We should not remove required spaces unless we break the line before. + assert(Changes[i].NewlinesBefore || + Changes[i].Spaces >= + static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) || + Changes[i].Tok->is(tok::eof)); + Changes[i].StartOfTokenColumn += Shift; if (i + 1 != Changes.size()) Changes[i + 1].PreviousEndOfTokenColumn += Shift;
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -17277,6 +17277,31 @@ "const unsigned g;\n" "Const unsigned h;", Alignment); + + // See PR46529 + FormatStyle BracedAlign = getLLVMStyle(); + BracedAlign.AlignConsecutiveDeclarations = FormatStyle::ACS_Consecutive; + verifyFormat("const auto result{[]() {\n" + " const auto something = 1;\n" + " return 2;\n" + "}};\n", + BracedAlign); + verifyFormat("int foo{[]() {\n" + " int bar{0};\n" + " return 0;\n" + "}()};", + BracedAlign); + BracedAlign.Cpp11BracedListStyle = false; + verifyFormat("const auto result{ []() {\n" + " const auto something = 1;\n" + " return 2;\n" + "} };\n", + BracedAlign); + verifyFormat("int foo{ []() {\n" + " int bar{ 0 };\n" + " return 0;\n" + "}() };", + BracedAlign); } TEST_F(FormatTest, AlignWithLineBreaks) { Index: clang/lib/Format/WhitespaceManager.cpp =================================================================== --- clang/lib/Format/WhitespaceManager.cpp +++ clang/lib/Format/WhitespaceManager.cpp @@ -331,6 +331,10 @@ FoundMatchOnLine = true; Shift = Column - Changes[i].StartOfTokenColumn; Changes[i].Spaces += Shift; + if (Changes[i].NewlinesBefore == 0) + Changes[i].Spaces = + std::max(Changes[i].Spaces, + static_cast<int>(Changes[i].Tok->SpacesRequiredBefore)); } // This is for function parameters that are split across multiple lines, @@ -399,6 +403,12 @@ if (ContinuedStringLiteral) Changes[i].Spaces += Shift; + // We should not remove required spaces unless we break the line before. + assert(Changes[i].NewlinesBefore || + Changes[i].Spaces >= + static_cast<int>(Changes[i].Tok->SpacesRequiredBefore) || + Changes[i].Tok->is(tok::eof)); + Changes[i].StartOfTokenColumn += Shift; if (i + 1 != Changes.size()) Changes[i + 1].PreviousEndOfTokenColumn += Shift;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits