Index: lib/Format/FormatTokenLexer.cpp =================================================================== --- lib/Format/FormatTokenLexer.cpp +++ lib/Format/FormatTokenLexer.cpp @@ -533,13 +533,21 @@ // take them into account as whitespace - this pattern is quite frequent // in macro definitions. // FIXME: Add a more explicit test. - while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\' && - FormatTok->TokenText[1] == '\n') { + while (FormatTok->TokenText.size() > 1 && FormatTok->TokenText[0] == '\\') { + unsigned SkippedWhitespace = 0; + if (FormatTok->TokenText.size() > 2 && + (FormatTok->TokenText[1] == '\r' && FormatTok->TokenText[2] == '\n')) + SkippedWhitespace = 3; + else if (FormatTok->TokenText[1] == '\n') + SkippedWhitespace = 2; + else + break; + ++FormatTok->NewlinesBefore; - WhitespaceLength += 2; - FormatTok->LastNewlineOffset = 2; + WhitespaceLength += SkippedWhitespace; + FormatTok->LastNewlineOffset = SkippedWhitespace; Column = 0; - FormatTok->TokenText = FormatTok->TokenText.substr(2); + FormatTok->TokenText = FormatTok->TokenText.substr(SkippedWhitespace); } FormatTok->WhitespaceRange = SourceRange( Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -2231,13 +2231,39 @@ } TEST_F(FormatTest, EscapedNewlines) { - EXPECT_EQ( - "#define A \\\n int i; \\\n int j;", - format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); + auto Narrow = getLLVMStyleWithColumns(11); + auto AlignLeft = getLLVMStyle(); + AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; + + // LF line endings + EXPECT_EQ("#define A \\\n int i; \\\n int j;", + format("#define A \\\nint i;\\\n int j;", Narrow)); EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); EXPECT_EQ("template f();", format("\\\ntemplate f();")); EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); EXPECT_EQ("", format("")); + EXPECT_EQ("#define MACRO(x) \\\n" + "private: \\\n" + " int x(int a);\n", + format("#define MACRO(x) \\\n" + "private: \\\n" + " int x(int a);\n", + AlignLeft)); + + // CRLF line endings + EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", + format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); + EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); + EXPECT_EQ("template f();", format("\\\ntemplate f();")); + EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); + EXPECT_EQ("", format("")); + EXPECT_EQ("#define MACRO(x) \\\r\n" + "private: \\\r\n" + " int x(int a);\r\n", + format("#define MACRO(x) \\\r\n" + "private: \\\r\n" + " int x(int a);\r\n", + AlignLeft)); } TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {