Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions clang/lib/Format/FormatTokenLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "FormatTokenLexer.h"
#include "FormatToken.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Format/Format.h"
Expand Down Expand Up @@ -1203,16 +1204,22 @@ static size_t countLeadingWhitespace(StringRef Text) {
const unsigned char *const End = Text.bytes_end();
const unsigned char *Cur = Begin;
while (Cur < End) {
if (isspace(Cur[0])) {
if (isWhitespace(Cur[0])) {
++Cur;
} else if (Cur[0] == '\\' && (Cur[1] == '\n' || Cur[1] == '\r')) {
// A '\' followed by a newline always escapes the newline, regardless
// of whether there is another '\' before it.
} else if (Cur[0] == '\\') {
// A backslash followed by optional horizontal whitespaces (P22232R2) and
// then a newline always escapes the newline.
// The source has a null byte at the end. So the end of the entire input
// isn't reached yet. Also the lexer doesn't break apart an escaped
// newline.
assert(End - Cur >= 2);
Cur += 2;
const auto *Lookahead = Cur + 1;
while (isHorizontalWhitespace(*Lookahead))
++Lookahead;
// No line splice found; the backslash is a token.
if (!isVerticalWhitespace(*Lookahead))
break;
// Splice found, consume it.
Cur = Lookahead + 1;
} else if (Cur[0] == '?' && Cur[1] == '?' && Cur[2] == '/' &&
(Cur[3] == '\n' || Cur[3] == '\r')) {
// Newlines can also be escaped by a '?' '?' '/' trigraph. By the way, the
Expand Down Expand Up @@ -1295,13 +1302,18 @@ FormatToken *FormatTokenLexer::getNextToken() {
case '/':
// The text was entirely whitespace when this loop was entered. Thus
// this has to be an escape sequence.
assert(Text.substr(i, 2) == "\\\r" || Text.substr(i, 2) == "\\\n" ||
Text.substr(i, 4) == "\?\?/\r" ||
assert(Text.substr(i, 4) == "\?\?/\r" ||
Text.substr(i, 4) == "\?\?/\n" ||
(i >= 1 && (Text.substr(i - 1, 4) == "\?\?/\r" ||
Text.substr(i - 1, 4) == "\?\?/\n")) ||
(i >= 2 && (Text.substr(i - 2, 4) == "\?\?/\r" ||
Text.substr(i - 2, 4) == "\?\?/\n")));
Text.substr(i - 2, 4) == "\?\?/\n")) ||
(Text[i] == '\\' && [&]() -> bool {
size_t j = i + 1;
while (j < Text.size() && isHorizontalWhitespace(Text[j]))
++j;
return j < Text.size() && (Text[j] == '\n' || Text[j] == '\r');
}()));
InEscape = true;
break;
default:
Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25768,6 +25768,21 @@ TEST_F(FormatTest, OperatorPassedAsAFunctionPtr) {
verifyFormat("foo(operator, , -42);", Style);
}

TEST_F(FormatTest, LineSpliceWithTrailingWhitespace) {
auto Style = getLLVMStyle();
Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
Style.UseTab = FormatStyle::UT_Never;

verifyFormat("int i;", " \\ \n"
" int i;");
verifyFormat("#define FOO(args) \\\n"
" struct a {};",
"#define FOO( args ) \\ \n"
"struct a{\\\t\t\t\n"
" };",
Style);
}

TEST_F(FormatTest, WhitespaceSensitiveMacros) {
FormatStyle Style = getLLVMStyle();
Style.WhitespaceSensitiveMacros.push_back("FOO");
Expand Down
Loading