[clang] [clang-format] Fix first-line width calculation for CRLF multiline tokens (PR #228752)
@@ -16681,6 +16681,23 @@ TEST_F(FormatTest,
CountsCharactersInMultilineRawStringLiterals) {
" R\"(single line raw string)\" + bb);");
}
+TEST_F(FormatTest, MultilineRawStringWidthWithCRLF) {
+ auto Style = getWebKitStyle();
w1m024 wrote:
> Yes please.
Already done.
https://github.com/llvm/llvm-project/pull/228752
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix first-line width calculation for CRLF multiline tokens (PR #228752)
https://github.com/w1m024 updated
https://github.com/llvm/llvm-project/pull/228752
>From 269d2dc627036b522a383bd332f9481e48b10fba Mon Sep 17 00:00:00 2001
From: w1m024
Date: Sun, 4 Oct 2026 00:43:03 +0800
Subject: [PATCH 1/2] [clang-format] Fix first-line width calculation for CRLF
multiline tokens
The first-line width of a multiline token is calculated from the text
before the first LF. For CRLF input, this includes the preceding CR,
which is counted as an extra column and can change wrapping at the
column limit.
Exclude the trailing CR when measuring the first-line width while
preserving the token text. Add a regression test covering LF and CRLF
multiline raw strings whose first line, including the preceding code,
exactly fits the column limit.
Fixes #224571
Assisted-by: Codex
---
clang/lib/Format/FormatTokenLexer.cpp | 6 +-
clang/unittests/Format/FormatTest.cpp | 17 +
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/FormatTokenLexer.cpp
b/clang/lib/Format/FormatTokenLexer.cpp
index 2a8bef21ad414..12a6fb8ea9054 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -1473,10 +1473,14 @@ FormatToken *FormatTokenLexer::getNextToken() {
Column += FormatTok->ColumnWidth;
} else {
FormatTok->IsMultiline = true;
+// Exclude the '\r' of a CRLF line ending from the first line's width.
+StringRef FirstLine = Text.substr(0, FirstNewlinePos);
+if (FirstLine.ends_with("\r"))
+ FirstLine = FirstLine.drop_back();
// FIXME: ColumnWidth actually depends on the start column, we need to
// take this into account when the token is moved.
FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
-Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
+FirstLine, Column, Style.TabWidth, Encoding);
// The last line of the token always starts in column 0.
// Thus, the length can be precomputed even in the presence of tabs.
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index 0a5950c25c60e..19e9f9695812e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16681,6 +16681,23 @@ TEST_F(FormatTest,
CountsCharactersInMultilineRawStringLiterals) {
" R\"(single line raw string)\" + bb);");
}
+TEST_F(FormatTest, MultilineRawStringWidthWithCRLF) {
+ auto Style = getWebKitStyle();
+ Style.ColumnLimit = 23;
+ Style.AllowAllArgumentsOnNextLine = false;
+ Style.PackArguments.BinPack = FormatStyle::BPAS_OnePerLine;
+
+ // The raw string's first line reaches ColumnLimit; CR must not add a column.
+ verifyFormat("x = a ? f(R\"(aa\n)\",\n"
+ "b)\n"
+ " : g();",
+ "x = a ? f(R\"(aa\n)\", b) : g();", Style);
+ verifyFormat("x = a ? f(R\"(aa\r\n)\",\r\n"
+ "b)\r\n"
+ " : g();",
+ "x = a ? f(R\"(aa\r\n)\", b) : g();", Style);
+}
+
TEST_F(FormatTest, SkipsUnknownStringLiterals) {
verifyFormat("string a = \"unterminated;");
verifyFormat("function(\"unterminated,\n"
>From e19c0e16fab2bdb1318f876f1f2a9bf7f53f1e0b Mon Sep 17 00:00:00 2001
From: w1m024
Date: Tue, 13 Oct 2026 18:50:25 +0800
Subject: [PATCH 2/2] [clang-format] Simplify CRLF raw string regression test
---
clang/unittests/Format/FormatTest.cpp | 19 +++
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index 19e9f9695812e..8564e4d75c8a8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16682,20 +16682,15 @@ TEST_F(FormatTest,
CountsCharactersInMultilineRawStringLiterals) {
}
TEST_F(FormatTest, MultilineRawStringWidthWithCRLF) {
- auto Style = getWebKitStyle();
- Style.ColumnLimit = 23;
- Style.AllowAllArgumentsOnNextLine = false;
- Style.PackArguments.BinPack = FormatStyle::BPAS_OnePerLine;
+ auto Style = getLLVMStyleWithColumns(23);
// The raw string's first line reaches ColumnLimit; CR must not add a column.
- verifyFormat("x = a ? f(R\"(aa\n)\",\n"
- "b)\n"
- " : g();",
- "x = a ? f(R\"(aa\n)\", b) : g();", Style);
- verifyFormat("x = a ? f(R\"(aa\r\n)\",\r\n"
- "b)\r\n"
- " : g();",
- "x = a ? f(R\"(aa\r\n)\", b) : g();", Style);
+ verifyNoChange("fff(R\"(\n"
+ ")\");",
+ Style);
+ verifyNoChange("fff(R\"(\r\n"
+ ")\");",
+ Style);
}
TEST_F(FormatTest, SkipsUnknownStringLiterals) {
___
cfe-commits mailing list
[email protected]
[clang] [clang-format] Fix first-line width calculation for CRLF multiline tokens (PR #228752)
@@ -16681,6 +16681,23 @@ TEST_F(FormatTest,
CountsCharactersInMultilineRawStringLiterals) {
" R\"(single line raw string)\" + bb);");
}
+TEST_F(FormatTest, MultilineRawStringWidthWithCRLF) {
+ auto Style = getWebKitStyle();
w1m024 wrote:
The `WebKit` style isn't required. The current test case was reduced from the
original reproducer in #224571. Whether it reproduces the issue depends on the
combination of style options, and `WebKit` is one that exposes it.
A simpler test case also reproduces the same issue:
```cpp
auto Style = getLLVMStyleWithColumns(23);
verifyNoChange("fff(R\"(\n"
")\");",
Style);
verifyNoChange("fff(R\"(\r\n"
")\");",
Style);
```
Should I use this simpler test case instead?
https://github.com/llvm/llvm-project/pull/228752
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix first-line width calculation for CRLF multiline tokens (PR #228752)
github-actions[bot] wrote: Hello @w1m024 :wave: Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices. * All contributions to LLVM must follow our [LLVM AI Tool Use Policy](https://llvm.org/docs/AIToolPolicy.html). In particular, if you used AI while working on this PR, remember to add a note to the PR description. * The [LLVM Code-Review Policy and Practices](https://llvm.org/docs/CodeReview.html) document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR. * Our [LLVM Developer Policy](https://llvm.org/docs/DeveloperPolicy.html) describes our expectations for code quality, commit summaries and contains notes on our CI system. Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description. --- ### Frequently asked questions **How do I add reviewers?** This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically. You can also add reviewers manually using the **Reviewers** section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using `@` followed by their GitHub username. **What if there are no comments?** If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers. **Are any special GitHub settings required to contribute to LLVM?** We only require contributors to have a public email address associated with their GitHub commits, see this [section](https://llvm.org/docs/DeveloperPolicy.html#email-addresses) of LLVM Developer Policy for details. --- If you have questions, feel free to leave a comment on this PR, or ask on [LLVM Discord](https://discord.com/invite/xS7Z362) or [LLVM Discourse](https://discourse.llvm.org/). Thank you, The LLVM Community https://github.com/llvm/llvm-project/pull/228752 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix first-line width calculation for CRLF multiline tokens (PR #228752)
llvmorg-github-actions[bot] wrote:
@llvm/pr-subscribers-clang-format
Author: w1m (w1m024)
Changes
The first-line width of a multiline token is calculated from the text before
the first `LF`. For `CRLF` input, this includes the preceding `CR`, which is
counted as an extra column and can change wrapping at the column limit.
Exclude the trailing `CR` when measuring the first-line width while preserving
the token text. Add a regression test covering `LF` and `CRLF` multiline raw
strings whose first line, including the preceding code, exactly fits the column
limit.
Fixes #224571
Assisted-by: Codex
---
Full diff: https://github.com/llvm/llvm-project/pull/228752.diff
2 Files Affected:
- (modified) clang/lib/Format/FormatTokenLexer.cpp (+5-1)
- (modified) clang/unittests/Format/FormatTest.cpp (+17)
``diff
diff --git a/clang/lib/Format/FormatTokenLexer.cpp
b/clang/lib/Format/FormatTokenLexer.cpp
index 2a8bef21ad4143..12a6fb8ea90540 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -1473,10 +1473,14 @@ FormatToken *FormatTokenLexer::getNextToken() {
Column += FormatTok->ColumnWidth;
} else {
FormatTok->IsMultiline = true;
+// Exclude the '\r' of a CRLF line ending from the first line's width.
+StringRef FirstLine = Text.substr(0, FirstNewlinePos);
+if (FirstLine.ends_with("\r"))
+ FirstLine = FirstLine.drop_back();
// FIXME: ColumnWidth actually depends on the start column, we need to
// take this into account when the token is moved.
FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
-Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
+FirstLine, Column, Style.TabWidth, Encoding);
// The last line of the token always starts in column 0.
// Thus, the length can be precomputed even in the presence of tabs.
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index 0a5950c25c60ef..19e9f9695812e2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16681,6 +16681,23 @@ TEST_F(FormatTest,
CountsCharactersInMultilineRawStringLiterals) {
" R\"(single line raw string)\" + bb);");
}
+TEST_F(FormatTest, MultilineRawStringWidthWithCRLF) {
+ auto Style = getWebKitStyle();
+ Style.ColumnLimit = 23;
+ Style.AllowAllArgumentsOnNextLine = false;
+ Style.PackArguments.BinPack = FormatStyle::BPAS_OnePerLine;
+
+ // The raw string's first line reaches ColumnLimit; CR must not add a column.
+ verifyFormat("x = a ? f(R\"(aa\n)\",\n"
+ "b)\n"
+ " : g();",
+ "x = a ? f(R\"(aa\n)\", b) : g();", Style);
+ verifyFormat("x = a ? f(R\"(aa\r\n)\",\r\n"
+ "b)\r\n"
+ " : g();",
+ "x = a ? f(R\"(aa\r\n)\", b) : g();", Style);
+}
+
TEST_F(FormatTest, SkipsUnknownStringLiterals) {
verifyFormat("string a = \"unterminated;");
verifyFormat("function(\"unterminated,\n"
``
https://github.com/llvm/llvm-project/pull/228752
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix first-line width calculation for CRLF multiline tokens (PR #228752)
https://github.com/w1m024 created
https://github.com/llvm/llvm-project/pull/228752
The first-line width of a multiline token is calculated from the text before
the first `LF`. For `CRLF` input, this includes the preceding `CR`, which is
counted as an extra column and can change wrapping at the column limit.
Exclude the trailing `CR` when measuring the first-line width while preserving
the token text. Add a regression test covering `LF` and `CRLF` multiline raw
strings whose first line, including the preceding code, exactly fits the column
limit.
Fixes #224571
Assisted-by: Codex
>From 269d2dc627036b522a383bd332f9481e48b10fba Mon Sep 17 00:00:00 2001
From: w1m024
Date: Sun, 4 Oct 2026 00:43:03 +0800
Subject: [PATCH] [clang-format] Fix first-line width calculation for CRLF
multiline tokens
The first-line width of a multiline token is calculated from the text
before the first LF. For CRLF input, this includes the preceding CR,
which is counted as an extra column and can change wrapping at the
column limit.
Exclude the trailing CR when measuring the first-line width while
preserving the token text. Add a regression test covering LF and CRLF
multiline raw strings whose first line, including the preceding code,
exactly fits the column limit.
Fixes #224571
Assisted-by: Codex
---
clang/lib/Format/FormatTokenLexer.cpp | 6 +-
clang/unittests/Format/FormatTest.cpp | 17 +
2 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/FormatTokenLexer.cpp
b/clang/lib/Format/FormatTokenLexer.cpp
index 2a8bef21ad4143..12a6fb8ea90540 100644
--- a/clang/lib/Format/FormatTokenLexer.cpp
+++ b/clang/lib/Format/FormatTokenLexer.cpp
@@ -1473,10 +1473,14 @@ FormatToken *FormatTokenLexer::getNextToken() {
Column += FormatTok->ColumnWidth;
} else {
FormatTok->IsMultiline = true;
+// Exclude the '\r' of a CRLF line ending from the first line's width.
+StringRef FirstLine = Text.substr(0, FirstNewlinePos);
+if (FirstLine.ends_with("\r"))
+ FirstLine = FirstLine.drop_back();
// FIXME: ColumnWidth actually depends on the start column, we need to
// take this into account when the token is moved.
FormatTok->ColumnWidth = encoding::columnWidthWithTabs(
-Text.substr(0, FirstNewlinePos), Column, Style.TabWidth, Encoding);
+FirstLine, Column, Style.TabWidth, Encoding);
// The last line of the token always starts in column 0.
// Thus, the length can be precomputed even in the presence of tabs.
diff --git a/clang/unittests/Format/FormatTest.cpp
b/clang/unittests/Format/FormatTest.cpp
index 0a5950c25c60ef..19e9f9695812e2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -16681,6 +16681,23 @@ TEST_F(FormatTest,
CountsCharactersInMultilineRawStringLiterals) {
" R\"(single line raw string)\" + bb);");
}
+TEST_F(FormatTest, MultilineRawStringWidthWithCRLF) {
+ auto Style = getWebKitStyle();
+ Style.ColumnLimit = 23;
+ Style.AllowAllArgumentsOnNextLine = false;
+ Style.PackArguments.BinPack = FormatStyle::BPAS_OnePerLine;
+
+ // The raw string's first line reaches ColumnLimit; CR must not add a column.
+ verifyFormat("x = a ? f(R\"(aa\n)\",\n"
+ "b)\n"
+ " : g();",
+ "x = a ? f(R\"(aa\n)\", b) : g();", Style);
+ verifyFormat("x = a ? f(R\"(aa\r\n)\",\r\n"
+ "b)\r\n"
+ " : g();",
+ "x = a ? f(R\"(aa\r\n)\", b) : g();", Style);
+}
+
TEST_F(FormatTest, SkipsUnknownStringLiterals) {
verifyFormat("string a = \"unterminated;");
verifyFormat("function(\"unterminated,\n"
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
