[clang] [clang-format] Handle Trailing Whitespace After Line Continuation (P2223R2) (PR #145243)

via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 25 08:10:12 PDT 2025


================
@@ -1295,13 +1305,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');
+               }()));
----------------
sstwcw wrote:

Why was it put there?  Before my patch, the code did not handle trigraphs.  It looked at the character following the backslash.  My patch added support for trigraphs.  To continue checking for the character following the trigraph meant I had to add more code.  I realized that I did not have to add code for checking past the first character because the entire block only ran when there was whitespace.  So I removed the code for checking past the first character.  Then @owenca asked why it was unnecessary to check further than the first character.  I thought that if someone had to ask then that meant the reasoning was not obvious.  So I added an assertion.  I thought it could help developers understand the code.

Why can `Text.substr(i, 4)` be equal to `??/\r`, when `Text[i]` is `/`?  If you click on the arrow to see more lines around the changed code, you will see that the block also runs when the character is `\` or `?`.

https://github.com/llvm/llvm-project/pull/145243


More information about the cfe-commits mailing list