[clang] [clang-format] Handle Trailing Whitespace After Line Continuation (P2223R2) (PR #145243)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 23 22:08:28 PDT 2025
================
@@ -1205,14 +1206,23 @@ static size_t countLeadingWhitespace(StringRef Text) {
while (Cur < End) {
if (isspace(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 '\' followed by a optional horizontal whitespace (P22232R2) and then
+ // newline always escapes the newline, regardless of whether there is
+ // another '\' before it.
// 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 unsigned char *Lookahead = Cur + 1;
+ while (isHorizontalWhitespace(*Lookahead))
+ ++Lookahead;
+ if (*Lookahead == '\n' || *Lookahead == '\r') {
+ // Splice found, consume it.
+ Cur = Lookahead + 1;
+ continue;
+ }
+ // No line splice found; the '\' is a token.
+ break;
----------------
owenca wrote:
```suggestion
// No line splice found; the backslash is a token.
if (!isVerticalWhitespace(*Lookahead))
break;
// Splice found, consume it.
Cur = Lookahead + 1;
```
https://github.com/llvm/llvm-project/pull/145243
More information about the cfe-commits
mailing list