aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Format/ContinuationIndenter.cpp
diff options
context:
space:
mode:
authorGedare Bloom <gedare@rtems.org>2024-01-22 20:01:16 -0700
committerGitHub <noreply@github.com>2024-01-22 19:01:16 -0800
commit4ef646eab3023b52ce8ee529d16605c92caba8ba (patch)
tree44006ef0d966811e0ef718f7fa96e5fe693f4a14 /clang/lib/Format/ContinuationIndenter.cpp
parent7e63940f69d99c12ccc18c76e1fc6b861fd459ec (diff)
downloadllvm-4ef646eab3023b52ce8ee529d16605c92caba8ba.zip
llvm-4ef646eab3023b52ce8ee529d16605c92caba8ba.tar.gz
llvm-4ef646eab3023b52ce8ee529d16605c92caba8ba.tar.bz2
[clang-format]: Fix formatting of if statements with BlockIndent (#77699)
A bug with BlockIndent prevents line breaks within if (and else if) clauses. While fixing this bug, it appears that AlignAfterOpenBracket is not designed to work with loop and if statements, but AlwaysBreak works on if clauses. The documentation and tests are not clear on whether or not this behavior is intended. This PR preserves the `AlwaysBreak` behavior on `if` clauses without supporting `BlockIndent` on `if` clauses to avoid regressions while fixing the bug. It may be reasonable to create an explicit option for alignment of if (and loop) clauses intentionally for both `AlwaysBreak` and `BlockIndent` Fixes #54663. Migrated from Differential Revision: https://reviews.llvm.org/D154755 See more discussion there. Addressed last open comment from the rev about refactoring the complex conditional logic involved with the `AlignAfterOpenBracket` line break behavior.
Diffstat (limited to 'clang/lib/Format/ContinuationIndenter.cpp')
-rw-r--r--clang/lib/Format/ContinuationIndenter.cpp24
1 files changed, 17 insertions, 7 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index a099813..a3eb913 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -771,15 +771,25 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// parenthesis by disallowing any further line breaks if there is no line
// break after the opening parenthesis. Don't break if it doesn't conserve
// columns.
+ auto IsOpeningBracket = [&](const FormatToken &Tok) {
+ auto IsStartOfBracedList = [&]() {
+ return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
+ Style.Cpp11BracedListStyle;
+ };
+ if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
+ !IsStartOfBracedList()) {
+ return false;
+ }
+ if (!Tok.Previous)
+ return true;
+ if (Tok.Previous->isIf())
+ return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak;
+ return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+ tok::kw_switch);
+ };
if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
- (Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
- (Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
- Style.Cpp11BracedListStyle)) &&
- State.Column > getNewLineColumn(State) &&
- (!Previous.Previous ||
- !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
- tok::kw_switch)) &&
+ IsOpeningBracket(Previous) && State.Column > getNewLineColumn(State) &&
// Don't do this for simple (no expressions) one-argument function calls
// as that feels like needlessly wasting whitespace, e.g.:
//