Skip to content

Commit 12a3afe

Browse files
authored
[clang-format] Remove code related to trigraphs (#148640)
When reviewing #147156, the reviewers pointed out that we didn't need to support the trigraph. The code never handled it right. In the debug build, this kind of input caused the assertion in the function `countLeadingWhitespace` to fail. The release build without assertions outputted `?` `?` `/` separated by spaces. ```C #define A ??/ int i; ``` This is because the code in `countLeadingWhitespace` assumed that the underlying lexer recognized the entire `??/` sequence as a single token. In fact, the lexer recognized it as 3 separate tokens. The flag to make the lexer recognize trigraphs was never enabled. This patch enables the flag in the underlying lexer. This way, the program now either turns the trigraph into a single `\` or removes it altogether if the line is short enough. There are operators like the `??=` in C#. So the flag is not enabled for all input languages. Instead the check for the token size is moved from the assert line into the if line. The problem was introduced by my own patch 370bee4 from about 3 years ago. I added code to count the number of characters in the escape sequence probably just because the block of code used to have a comment saying someone should add the feature. Maybe I forgot to enable assertions when I ran the code. I found the problem because reviewing pull request 145243 made me look at the code again.
1 parent ed2bfd1 commit 12a3afe

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

clang/lib/Format/FormatTokenLexer.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ void FormatTokenLexer::truncateToken(size_t NewLen) {
11981198
/// Count the length of leading whitespace in a token.
11991199
static size_t countLeadingWhitespace(StringRef Text) {
12001200
// Basically counting the length matched by this regex.
1201-
// "^([\n\r\f\v \t]|(\\\\|\\?\\?/)[\n\r])+"
1201+
// "^([\n\r\f\v \t]|\\\\[\n\r])+"
12021202
// Directly using the regex turned out to be slow. With the regex
12031203
// version formatting all files in this directory took about 1.25
12041204
// seconds. This version took about 0.5 seconds.
@@ -1222,13 +1222,6 @@ static size_t countLeadingWhitespace(StringRef Text) {
12221222
break;
12231223
// Splice found, consume it.
12241224
Cur = Lookahead + 1;
1225-
} else if (Cur[0] == '?' && Cur[1] == '?' && Cur[2] == '/' &&
1226-
(Cur[3] == '\n' || Cur[3] == '\r')) {
1227-
// Newlines can also be escaped by a '?' '?' '/' trigraph. By the way, the
1228-
// characters are quoted individually in this comment because if we write
1229-
// them together some compilers warn that we have a trigraph in the code.
1230-
assert(End - Cur >= 4);
1231-
Cur += 4;
12321225
} else {
12331226
break;
12341227
}
@@ -1300,22 +1293,16 @@ FormatToken *FormatTokenLexer::getNextToken() {
13001293
Style.TabWidth - (Style.TabWidth ? Column % Style.TabWidth : 0);
13011294
break;
13021295
case '\\':
1303-
case '?':
1304-
case '/':
1305-
// The text was entirely whitespace when this loop was entered. Thus
1306-
// this has to be an escape sequence.
1307-
assert(Text.substr(i, 4) == "\?\?/\r" ||
1308-
Text.substr(i, 4) == "\?\?/\n" ||
1309-
(i >= 1 && (Text.substr(i - 1, 4) == "\?\?/\r" ||
1310-
Text.substr(i - 1, 4) == "\?\?/\n")) ||
1311-
(i >= 2 && (Text.substr(i - 2, 4) == "\?\?/\r" ||
1312-
Text.substr(i - 2, 4) == "\?\?/\n")) ||
1313-
(Text[i] == '\\' && [&]() -> bool {
1314-
size_t j = i + 1;
1315-
while (j < Text.size() && isHorizontalWhitespace(Text[j]))
1316-
++j;
1317-
return j < Text.size() && (Text[j] == '\n' || Text[j] == '\r');
1318-
}()));
1296+
// The code preceding the loop and in the countLeadingWhitespace
1297+
// function guarantees that Text is entirely whitespace, not including
1298+
// comments but including escaped newlines. So the character shows up,
1299+
// then it has to be in an escape sequence.
1300+
assert([&]() -> bool {
1301+
size_t j = i + 1;
1302+
while (j < Text.size() && isHorizontalWhitespace(Text[j]))
1303+
++j;
1304+
return j < Text.size() && (Text[j] == '\n' || Text[j] == '\r');
1305+
}());
13191306
InEscape = true;
13201307
break;
13211308
default:

clang/unittests/Format/FormatTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6681,6 +6681,17 @@ TEST_F(FormatTest, EscapedNewlines) {
66816681
" int x(int a);",
66826682
AlignLeft);
66836683

6684+
// Escaped with a trigraph. The program just has to avoid crashing.
6685+
verifyNoCrash("#define A \?\?/\n"
6686+
"int i;\?\?/\n"
6687+
" int j;");
6688+
verifyNoCrash("#define A \?\?/\r\n"
6689+
"int i;\?\?/\r\n"
6690+
" int j;");
6691+
verifyNoCrash("#define A \?\?/\n"
6692+
"int i;",
6693+
getGoogleStyle(FormatStyle::LK_CSharp));
6694+
66846695
// CRLF line endings
66856696
verifyFormat("#define A \\\r\n int i; \\\r\n int j;",
66866697
"#define A \\\r\nint i;\\\r\n int j;", Narrow);

0 commit comments

Comments
 (0)