From 35ca66debf3b353b9793b07a412619130dcbd586 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Mon, 29 Feb 2016 12:26:20 +0000 Subject: [PATCH] clang-format: Don't format unrelated nested blocks. With this change: SomeFunction( [] { int i; return i; // Format this line. }, [] { return 2; // Don't "fix" this. }); llvm-svn: 262216 --- clang/lib/Format/UnwrappedLineFormatter.cpp | 4 +++- clang/unittests/Format/FormatTestSelective.cpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index f65056907963..d75eaf54a084 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -863,7 +863,9 @@ UnwrappedLineFormatter::format(const SmallVectorImpl &Lines, // If no token in the current line is affected, we still need to format // affected children. if (TheLine.ChildrenAffected) - format(TheLine.Children, DryRun); + for (const FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) + if (!Tok->Children.empty()) + format(Tok->Children, DryRun); // Adapt following lines on the current indent level to the same level // unless the current \c AnnotatedLine is not at the beginning of a line. diff --git a/clang/unittests/Format/FormatTestSelective.cpp b/clang/unittests/Format/FormatTestSelective.cpp index 699600c42d9f..5885cadee621 100644 --- a/clang/unittests/Format/FormatTestSelective.cpp +++ b/clang/unittests/Format/FormatTestSelective.cpp @@ -278,6 +278,23 @@ TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) { " };\n" "});", 0, 0)); + EXPECT_EQ("SomeFunction(\n" + " [] {\n" + " int i;\n" + " return i;\n" // Format this line. + " },\n" + " [] {\n" + " return 2;\n" // Don't fix this. + " });", + format("SomeFunction(\n" + " [] {\n" + " int i;\n" + " return i;\n" // Format this line. + " },\n" + " [] {\n" + " return 2;\n" // Don't fix this. + " });", + 40, 0)); } TEST_F(FormatTestSelective, WrongIndent) {