Skip to content

[clang-format] Fix option BreakBinaryOperations for operator >> #122282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 18, 2025

Conversation

andergnet
Copy link
Contributor

@andergnet andergnet commented Jan 9, 2025

Fixes #106228.

Copy link

github-actions bot commented Jan 9, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/pr-subscribers-clang-format

Author: Ander (andergnet)

Changes

This PR fixes the issue #106228 were the C++ ">>" operator got a different formatting than the "<<" one when using BreakBinaryOperations.

This is my first PR on the project don't be too harsh ;)


Full diff: https://github.com/llvm/llvm-project/pull/122282.diff

2 Files Affected:

  • (modified) clang/lib/Format/ContinuationIndenter.cpp (+2-1)
  • (modified) clang/unittests/Format/FormatTest.cpp (+41-31)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 554b55fa75c926..5e5c93e2a5286f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -147,7 +147,8 @@ static bool startsNextOperand(const FormatToken &Current) {
 // Returns \c true if \c Current is a binary operation that must break.
 static bool mustBreakBinaryOperation(const FormatToken &Current,
                                      const FormatStyle &Style) {
-  return Style.BreakBinaryOperations != FormatStyle::BBO_Never &&
+  return Current.CanBreakBefore &&
+         Style.BreakBinaryOperations != FormatStyle::BBO_Never &&
          (Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None
               ? startsNextOperand
               : isAlignableBinaryOperator)(Current);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4d48bcacddead8..c9cf03bff6acce 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22301,16 +22301,16 @@ TEST_F(FormatTest, HandlesUTF8BOM) {
 #if !defined(_MSC_VER)
 
 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
-  verifyFormat("\"Однажды в студёную зимнюю пору...\"",
+  verifyFormat("\"Однажды в ѝтудёную зимнюю пору...\"",
                getLLVMStyleWithColumns(35));
-  verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
+  verifyFormat("\"一 二 三 四 五 六 七 八 九 坝\"",
                getLLVMStyleWithColumns(31));
-  verifyFormat("// Однажды в студёную зимнюю пору...",
+  verifyFormat("// Однажды в ѝтудёную зимнюю пору...",
                getLLVMStyleWithColumns(36));
-  verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
-  verifyFormat("/* Однажды в студёную зимнюю пору... */",
+  verifyFormat("// 一 二 三 四 五 六 七 八 九 坝", getLLVMStyleWithColumns(32));
+  verifyFormat("/* Однажды в ѝтудёную зимнюю пору... */",
                getLLVMStyleWithColumns(39));
-  verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
+  verifyFormat("/* 一 二 三 四 五 六 七 八 九 坝 */",
                getLLVMStyleWithColumns(35));
 }
 
@@ -22329,18 +22329,18 @@ TEST_F(FormatTest, SplitsUTF8Strings) {
             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
   // FIXME: unstable test case
   EXPECT_EQ("\"Однажды, в \"\n"
-            "\"студёную \"\n"
+            "\"ѝтудёную \"\n"
             "\"зимнюю \"\n"
             "\"пору,\"",
-            format("\"Однажды, в студёную зимнюю пору,\"",
+            format("\"Однажды, в ѝтудёную зимнюю пору,\"",
                    getLLVMStyleWithColumns(13)));
   // FIXME: unstable test case
   EXPECT_EQ(
       "\"一 二 三 \"\n"
       "\"四 五六 \"\n"
       "\"七 八 九 \"\n"
-      "\"十\"",
-      format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
+      "\"坝\"",
+      format("\"一 二 三 四 五六 七 八 九 坝\"", getLLVMStyleWithColumns(11)));
   // FIXME: unstable test case
   EXPECT_EQ("\"一\t\"\n"
             "\"二 \t\"\n"
@@ -22348,8 +22348,8 @@ TEST_F(FormatTest, SplitsUTF8Strings) {
             "\"五\t\"\n"
             "\"六 \t\"\n"
             "\"七 \"\n"
-            "\"八九十\tqq\"",
-            format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
+            "\"八九坝\tqq\"",
+            format("\"一\t二 \t三 四 五\t六 \t七 八九坝\tqq\"",
                    getLLVMStyleWithColumns(11)));
 
   // UTF8 character in an escape sequence.
@@ -22362,44 +22362,44 @@ TEST_F(FormatTest, SplitsUTF8Strings) {
 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
   verifyFormat("const char *sssss =\n"
                "    \"一二三四五六七八\\\n"
-               " 九 十\";",
+               " 九 坝\";",
                "const char *sssss = \"一二三四五六七八\\\n"
-               " 九 十\";",
+               " 九 坝\";",
                getLLVMStyleWithColumns(30));
 }
 
 TEST_F(FormatTest, SplitsUTF8LineComments) {
   verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
-  verifyFormat("// Я из лесу\n"
+  verifyFormat("// Я из леѝу\n"
                "// вышел; был\n"
-               "// сильный\n"
+               "// ѝильный\n"
                "// мороз.",
-               "// Я из лесу вышел; был сильный мороз.",
+               "// Я из леѝу вышел; был ѝильный мороз.",
                getLLVMStyleWithColumns(13));
   verifyFormat("// 一二三\n"
                "// 四五六七\n"
                "// 八  九\n"
-               "// 十",
-               "// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9));
+               "// 坝",
+               "// 一二三 四五六七 八  九 坝", getLLVMStyleWithColumns(9));
 }
 
 TEST_F(FormatTest, SplitsUTF8BlockComments) {
-  verifyFormat("/* Гляжу,\n"
-               " * поднимается\n"
+  verifyFormat("/* Глѝжу,\n"
+               " * поднимаетѝѝ\n"
                " * медленно в\n"
                " * гору\n"
                " * Лошадка,\n"
-               " * везущая\n"
-               " * хворосту\n"
+               " * везущаѝ\n"
+               " * хвороѝту\n"
                " * воз. */",
-               "/* Гляжу, поднимается медленно в гору\n"
-               " * Лошадка, везущая хворосту воз. */",
+               "/* Глѝжу, поднимаетѝѝ медленно в гору\n"
+               " * Лошадка, везущаѝ хвороѝту воз. */",
                getLLVMStyleWithColumns(13));
   verifyFormat("/* 一二三\n"
                " * 四五六七\n"
                " * 八  九\n"
-               " * 十  */",
-               "/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9));
+               " * 坝  */",
+               "/* 一二三 四五六七 八  九 坝  */", getLLVMStyleWithColumns(9));
   verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n"
                " * 𝕓𝕪𝕥𝕖\n"
                " * 𝖀𝕿𝕱-𝟠 */",
@@ -27908,9 +27908,9 @@ TEST_F(FormatTest, BreakAdjacentStringLiterals) {
 
 TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
   verifyFormat(
-      "int rus;      // А теперь комментарии, например, на русском, 2-байта\n"
-      "int long_rus; // Верхний коммент еще не превысил границу в 80, однако\n"
-      "              // уже отодвинут. Перенос, при этом, отрабатывает верно");
+      "int rus;      // Н теперь комментарии, например, на руѝѝком, 2-байта\n"
+      "int long_rus; // Верхний коммент еще не превыѝил границу в 80, однако\n"
+      "              // уже отодвинут. Переноѝ, при ѝтом, отрабатывает верно");
 
   auto Style = getLLVMStyle();
   Style.ColumnLimit = 15;
@@ -27940,7 +27940,7 @@ TEST_F(FormatTest, AlignUTFCommentsAndStringLiterals) {
   verifyFormat("Languages languages = {\n"
                "    Language{{'e', 'n'}, U\"Test English\" },\n"
                "    Language{{'l', 'v'}, U\"Test Latviešu\"},\n"
-               "    Language{{'r', 'u'}, U\"Test Русский\" },\n"
+               "    Language{{'r', 'u'}, U\"Test Руѝѝкий\" },\n"
                "};",
                Style);
 }
@@ -28188,6 +28188,16 @@ TEST_F(FormatTest, BreakBinaryOperations) {
                "                  | byte_buffer[2] << 16\n"
                "                  | byte_buffer[3] << 24;",
                Style);
+
+  Style.BreakBinaryOperations = FormatStyle::BBO_OnePerLine;
+  // Check operator >> special case
+  verifyFormat("std::cout\n"
+               "    << longOperand1\n"
+               "    << longOperand2\n"
+               "    << longOperand3\n"
+               "    << longOperand4\n"
+               "    << longOperand5;",
+               Style);
 }
 
 TEST_F(FormatTest, RemoveEmptyLinesInUnwrappedLines) {

Copy link
Contributor

@owenca owenca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With ColumnLimit: 60, this patch doesn't seem to format the following correctly:
std::cout << longOperand_1 << longOperand_2 << longOperand_3;

@mydeveloperday mydeveloperday changed the title Fix greatergreater [clang-format] Fix greatergreater Jan 13, 2025
@owenca owenca changed the title [clang-format] Fix greatergreater [clang-format] Fix option BreakBinaryOperations for operator >> Jan 18, 2025
@owenca owenca merged commit e240261 into llvm:main Jan 18, 2025
8 checks passed
Copy link

@andergnet Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@andergnet andergnet deleted the fix_greatergreater branch January 18, 2025 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format: Incorrect behavior with BreakBinaryOperations
3 participants