Skip to content

[clang-format][NFC] Simplify some logic in BreakableLineCommentSection #148324

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 1 commit into from
Jul 14, 2025

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Jul 12, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jul 12, 2025

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

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

1 Files Affected:

  • (modified) clang/lib/Format/BreakableToken.cpp (+4-6)
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index 24912c25ef8c6..c36cb74bc4501 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -927,14 +927,12 @@ BreakableLineCommentSection::BreakableLineCommentSection(
       }
 
       if (Lines[i].size() != IndentPrefix.size()) {
-        PrefixSpaceChange[i] = FirstLineSpaceChange;
+        assert(Lines[i].size() > IndentPrefix.size());
 
-        if (SpacesInPrefix + PrefixSpaceChange[i] < Minimum) {
-          PrefixSpaceChange[i] +=
-              Minimum - (SpacesInPrefix + PrefixSpaceChange[i]);
-        }
+        PrefixSpaceChange[i] = SpacesInPrefix + FirstLineSpaceChange < Minimum
+                                   ? Minimum - SpacesInPrefix
+                                   : FirstLineSpaceChange;
 
-        assert(Lines[i].size() > IndentPrefix.size());
         const auto FirstNonSpace = Lines[i][IndentPrefix.size()];
         const bool IsFormatComment = LineTok && switchesFormatting(*LineTok);
         const bool LineRequiresLeadingSpace =

PrefixSpaceChange[i] +=
Minimum - (SpacesInPrefix + PrefixSpaceChange[i]);
}
PrefixSpaceChange[i] = SpacesInPrefix + FirstLineSpaceChange < Minimum
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
PrefixSpaceChange[i] = SpacesInPrefix + FirstLineSpaceChange < Minimum
PrefixSpaceChange[i] = (SpacesInPrefix + FirstLineSpaceChange) < Minimum

This is what you wanted, or?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why would we want to add the redundant parentheses? They were not there in the if statement before.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking wrong here. But adding parentheses when using the ternary operator helps reading it. I thought that SpaceInPrefix was always the base.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see, but in general, how could var1 + var2 < var3 be misread as var1 + (var2 < var3), whether the former is in if/while, ternary, or assignment? In fact, I would always remove the redundant parentheses in (var1 + var2) < var3.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I was mistaken.

But, even is not needed, I'd do this:

Suggested change
PrefixSpaceChange[i] = SpacesInPrefix + FirstLineSpaceChange < Minimum
PrefixSpaceChange[i] = (SpacesInPrefix + FirstLineSpaceChange < Minimum)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like the examples of conditional/ternary operators, I never add redundant parentheses because they have the lowest precedence relative to arithmetic/logical operators. This is analogous to a + (b * c) or (a / b) - c.

The clang-format codebase has a mixture of both expr1 ? expr2 : expr3 and (expr1) ? expr2 : expr3, and IMO we should consistently use the former. Although I can understand why some people prefer the latter when expr1 and expr2 are on the same line, there's really nothing to gain when the questions mark starts on a new line:

expr1
    ? expr2

PrefixSpaceChange[i] +=
Minimum - (SpacesInPrefix + PrefixSpaceChange[i]);
}
PrefixSpaceChange[i] = SpacesInPrefix + FirstLineSpaceChange < Minimum
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I was mistaken.

But, even is not needed, I'd do this:

Suggested change
PrefixSpaceChange[i] = SpacesInPrefix + FirstLineSpaceChange < Minimum
PrefixSpaceChange[i] = (SpacesInPrefix + FirstLineSpaceChange < Minimum)

@owenca owenca merged commit b79c763 into llvm:main Jul 14, 2025
11 checks passed
@owenca owenca deleted the BreakableLineCommentSection branch July 14, 2025 03:35
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.

3 participants