From 5987d7c59da5a7611036c2952f5a1f501d17ad48 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Thu, 22 Apr 2021 10:56:15 +0200 Subject: [PATCH] [clang-format] fix indent in alignChainedConditionals Clang-format was indenting the lines following the `?` in the added test case by +5 instead of +4. This only happens in a very specific situation, where the `?` is followed by a multiline block comment, as in the example. This fix addresses this without regressing any of the existing tests. Differential Revision: https://reviews.llvm.org/D101033 --- clang/lib/Format/WhitespaceManager.cpp | 9 ++++----- clang/unittests/Format/FormatTest.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 6016f8d..e78c5c4 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -778,12 +778,11 @@ void WhitespaceManager::alignChainedConditionals() { Changes, /*StartAt=*/0); } else { static auto AlignWrappedOperand = [](Change const &C) { - auto Previous = C.Tok->getPreviousNonComment(); // Previous; + FormatToken *Previous = C.Tok->getPreviousNonComment(); return C.NewlinesBefore && Previous && Previous->is(TT_ConditionalExpr) && - (Previous->is(tok::question) || - (Previous->is(tok::colon) && - (C.Tok->FakeLParens.size() == 0 || - C.Tok->FakeLParens.back() != prec::Conditional))); + (Previous->is(tok::colon) && + (C.Tok->FakeLParens.size() == 0 || + C.Tok->FakeLParens.back() != prec::Conditional)); }; // Ensure we keep alignment of wrapped operands with non-wrapped operands // Since we actually align the operators, the wrapped operands need the diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 5ae67d9..734ffed 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -6459,6 +6459,30 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " bbbb ? cccccccccccccccccc :\n" " ddddd;\n", Style); + + EXPECT_EQ( + "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" + " /*\n" + " */\n" + " function() {\n" + " try {\n" + " return JJJJJJJJJJJJJJ(\n" + " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" + " }\n" + " } :\n" + " function() {};", + format( + "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" + " /*\n" + " */\n" + " function() {\n" + " try {\n" + " return JJJJJJJJJJJJJJ(\n" + " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" + " }\n" + " } :\n" + " function() {};", + getGoogleStyle(FormatStyle::LK_JavaScript))); } TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { -- 2.7.4