diff options
| author | Krasimir Georgiev <krasimir@google.com> | 2017-10-30 14:41:34 +0000 |
|---|---|---|
| committer | Krasimir Georgiev <krasimir@google.com> | 2017-10-30 14:41:34 +0000 |
| commit | 7f64fa8022befa17e50ae493e896fb8ecc85f301 (patch) | |
| tree | 741668d6015a3c5c41e5abfb690623addea5aeff | |
| parent | 561ac06ff29f5890939394084c9b079d0d4d89ac (diff) | |
| download | bcm5719-llvm-7f64fa8022befa17e50ae493e896fb8ecc85f301.tar.gz bcm5719-llvm-7f64fa8022befa17e50ae493e896fb8ecc85f301.zip | |
[clang-format] Handle CRLF correctly when formatting escaped newlines
Subscribers: klimek
Differential Revision: https://reviews.llvm.org/D39420
Contributed by @peterbudai!
llvm-svn: 316910
| -rw-r--r-- | clang/lib/Format/FormatTokenLexer.cpp | 18 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 34 |
2 files changed, 43 insertions, 9 deletions
diff --git a/clang/lib/Format/FormatTokenLexer.cpp b/clang/lib/Format/FormatTokenLexer.cpp index d37fcec6c5e..727437a2a58 100644 --- a/clang/lib/Format/FormatTokenLexer.cpp +++ b/clang/lib/Format/FormatTokenLexer.cpp @@ -554,13 +554,21 @@ FormatToken *FormatTokenLexer::getNextToken() { // 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( diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 3651fa539d7..82530c0376a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2629,14 +2629,39 @@ TEST_F(FormatTest, FormatUnbalancedStructuralElements) { } TEST_F(FormatTest, EscapedNewlines) { - EXPECT_EQ( - "#define A \\\n int i; \\\n int j;", - format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); + FormatStyle Narrow = getLLVMStyleWithColumns(11); + 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 <class T> f();", format("\\\ntemplate <class T> f();")); EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>")); + FormatStyle AlignLeft = getLLVMStyle(); + AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; + 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 <class T> f();", format("\\\ntemplate <class T> f();")); + EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); + EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>")); + 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)); + FormatStyle DontAlign = getLLVMStyle(); DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; DontAlign.MaxEmptyLinesToKeep = 3; @@ -2659,7 +2684,8 @@ TEST_F(FormatTest, EscapedNewlines) { "\\\n" " public: \\\n" " void baz(); \\\n" - " };", DontAlign)); + " };", + DontAlign)); } TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |

