Index: lib/Format/WhitespaceManager.cpp =================================================================== --- lib/Format/WhitespaceManager.cpp +++ lib/Format/WhitespaceManager.cpp @@ -557,9 +557,22 @@ } void WhitespaceManager::alignEscapedNewlines() { - if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign) + // If we are not aligning escaped newlines, just set EscapedNewlineColumn + // to point to the end of each line. + if (Style.AlignEscapedNewlines == FormatStyle::ENAS_DontAlign) { + bool PreviousContinuerHadNLBefore = false; // used to detect blank lines + for (Change &C : Changes) { + if (C.ContinuesPPDirective) { + if (C.NewlinesBefore > 0) + C.EscapedNewlineColumn = + PreviousContinuerHadNLBefore ? 2 : C.PreviousEndOfTokenColumn + 2; + PreviousContinuerHadNLBefore = C.NewlinesBefore > 0; + } + } return; + } + // Otherwise, compute the max width and then apply it to all lines. bool AlignLeft = Style.AlignEscapedNewlines == FormatStyle::ENAS_Left; unsigned MaxEndOfLine = AlignLeft ? 0 : Style.ColumnLimit; unsigned StartOfMacro = 0; @@ -644,6 +657,7 @@ unsigned PreviousEndOfTokenColumn, unsigned EscapedNewlineColumn) { if (Newlines > 0) { + assert(EscapedNewlineColumn >= 2); unsigned Offset = std::min(EscapedNewlineColumn - 2, PreviousEndOfTokenColumn); for (unsigned i = 0; i < Newlines; ++i) { Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -2309,6 +2309,30 @@ EXPECT_EQ("template f();", format("\\\ntemplate f();")); EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); EXPECT_EQ("", format("")); + + FormatStyle DontAlign = getLLVMStyle(); + DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; + DontAlign.MaxEmptyLinesToKeep = 3; + // FIXME: can't use verifyFormat here because the newline before + // "public:" is not inserted the first time it's reformatted + EXPECT_EQ("#define A \\\n" + " class Foo { \\\n" + " void bar(); \\\n" + " \\\n" + " \\\n" + " \\\n" + " public: \\\n" + " void baz(); \\\n" + " };", + format("#define A \\\n" + " class Foo { \\\n" + " void bar(); \\\n" + " \\\n" + " \\\n" + " \\\n" + " public: \\\n" + " void baz(); \\\n" + " };", DontAlign)); } TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {