diff options
author | Owen Pan <owenpiano@gmail.com> | 2024-05-06 19:55:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-06 19:55:55 -0700 |
commit | 236b3e1aad45e2bab8ede0da6397b7b01f9cc9d8 (patch) | |
tree | f2de149014b37d8d1c9b00a8ad7c7ace85ba997a /clang/lib/Format/WhitespaceManager.cpp | |
parent | aac83fcf3ec6bbe5e0d83b76d2d236b1b4bfbe89 (diff) | |
download | llvm-236b3e1aad45e2bab8ede0da6397b7b01f9cc9d8.zip llvm-236b3e1aad45e2bab8ede0da6397b7b01f9cc9d8.tar.gz llvm-236b3e1aad45e2bab8ede0da6397b7b01f9cc9d8.tar.bz2 |
[clang-format] Handle Java switch expressions (#91112)
Also adds AllowShortCaseExpressionOnASingleLine option and
AlignCaseArrows suboption of AlignConsecutiveShortCaseStatements.
Fixes #55903.
Diffstat (limited to 'clang/lib/Format/WhitespaceManager.cpp')
-rw-r--r-- | clang/lib/Format/WhitespaceManager.cpp | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 44fd807..ed06d609 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -107,7 +107,8 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() { llvm::sort(Changes, Change::IsBeforeInFile(SourceMgr)); calculateLineBreakInformation(); alignConsecutiveMacros(); - alignConsecutiveShortCaseStatements(); + alignConsecutiveShortCaseStatements(/*IsExpr=*/true); + alignConsecutiveShortCaseStatements(/*IsExpr=*/false); alignConsecutiveDeclarations(); alignConsecutiveBitFields(); alignConsecutiveAssignments(); @@ -878,22 +879,27 @@ void WhitespaceManager::alignConsecutiveColons( Changes, /*StartAt=*/0, AlignStyle); } -void WhitespaceManager::alignConsecutiveShortCaseStatements() { +void WhitespaceManager::alignConsecutiveShortCaseStatements(bool IsExpr) { if (!Style.AlignConsecutiveShortCaseStatements.Enabled || - !Style.AllowShortCaseLabelsOnASingleLine) { + !(IsExpr ? Style.AllowShortCaseExpressionOnASingleLine + : Style.AllowShortCaseLabelsOnASingleLine)) { return; } + const auto Type = IsExpr ? TT_CaseLabelArrow : TT_CaseLabelColon; + const auto &Option = Style.AlignConsecutiveShortCaseStatements; + const bool AlignArrowOrColon = + IsExpr ? Option.AlignCaseArrows : Option.AlignCaseColons; + auto Matches = [&](const Change &C) { - if (Style.AlignConsecutiveShortCaseStatements.AlignCaseColons) - return C.Tok->is(TT_CaseLabelColon); + if (AlignArrowOrColon) + return C.Tok->is(Type); // Ignore 'IsInsideToken' to allow matching trailing comments which // need to be reflowed as that causes the token to appear in two // different changes, which will cause incorrect alignment as we'll // reflow early due to detecting multiple aligning tokens per line. - return !C.IsInsideToken && C.Tok->Previous && - C.Tok->Previous->is(TT_CaseLabelColon); + return !C.IsInsideToken && C.Tok->Previous && C.Tok->Previous->is(Type); }; unsigned MinColumn = 0; @@ -944,7 +950,7 @@ void WhitespaceManager::alignConsecutiveShortCaseStatements() { if (Changes[I].Tok->isNot(tok::comment)) LineIsComment = false; - if (Changes[I].Tok->is(TT_CaseLabelColon)) { + if (Changes[I].Tok->is(Type)) { LineIsEmptyCase = !Changes[I].Tok->Next || Changes[I].Tok->Next->isTrailingComment(); |