[clang] [clang-format] Fix a bug in SpacesInParens InConditionalStatements (PR #108797)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 16 00:16:53 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->64416.
---
Full diff: https://github.com/llvm/llvm-project/pull/108797.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+17-19)
- (modified) clang/unittests/Format/FormatTest.cpp (+6)
``````````diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index dfa703aed0d34d..9e36570bc6ff41 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4418,31 +4418,29 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Right.MatchingParen == &Left && Line.Children.empty()) {
return Style.SpaceInEmptyBlock;
}
- if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
- (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
- Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
- return Style.SpacesInParensOptions.InEmptyParentheses;
- }
- if (Style.SpacesInParens == FormatStyle::SIPO_Custom &&
- Style.SpacesInParensOptions.ExceptDoubleParentheses &&
- Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
- auto *InnerLParen = Left.MatchingParen;
- if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
- InnerLParen->SpacesRequiredBefore = 0;
- return false;
+ if (Style.SpacesInParens == FormatStyle::SIPO_Custom) {
+ if ((Left.is(tok::l_paren) && Right.is(tok::r_paren)) ||
+ (Left.is(tok::l_brace) && Left.isNot(BK_Block) &&
+ Right.is(tok::r_brace) && Right.isNot(BK_Block))) {
+ return Style.SpacesInParensOptions.InEmptyParentheses;
+ }
+ if (Style.SpacesInParensOptions.ExceptDoubleParentheses &&
+ Left.is(tok::r_paren) && Right.is(tok::r_paren)) {
+ auto *InnerLParen = Left.MatchingParen;
+ if (InnerLParen && InnerLParen->Previous == Right.MatchingParen) {
+ InnerLParen->SpacesRequiredBefore = 0;
+ return false;
+ }
}
- }
- if (Style.SpacesInParensOptions.InConditionalStatements) {
const FormatToken *LeftParen = nullptr;
if (Left.is(tok::l_paren))
LeftParen = &Left;
else if (Right.is(tok::r_paren) && Right.MatchingParen)
LeftParen = Right.MatchingParen;
- if (LeftParen) {
- if (LeftParen->is(TT_ConditionLParen))
- return true;
- if (LeftParen->Previous && isKeywordWithCondition(*LeftParen->Previous))
- return true;
+ if (LeftParen && (LeftParen->is(TT_ConditionLParen) ||
+ (LeftParen->Previous &&
+ isKeywordWithCondition(*LeftParen->Previous)))) {
+ return Style.SpacesInParensOptions.InConditionalStatements;
}
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5ebf0d7068dd6c..f9c6a6eca1d724 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17282,6 +17282,12 @@ TEST_F(FormatTest, ConfigurableSpacesInParens) {
Spaces.SpacesInParens = FormatStyle::SIPO_Custom;
Spaces.SpacesInParensOptions = {};
Spaces.SpacesInParensOptions.Other = true;
+
+ EXPECT_FALSE(Spaces.SpacesInParensOptions.InConditionalStatements);
+ verifyFormat("if (a)\n"
+ " return;",
+ Spaces);
+
Spaces.SpacesInParensOptions.InConditionalStatements = true;
verifyFormat("do_something( ::globalVar );", Spaces);
verifyFormat("call( x, y, z );", Spaces);
``````````
</details>
https://github.com/llvm/llvm-project/pull/108797
More information about the cfe-commits
mailing list