diff options
author | Björn Schäpers <bjoern@hazardy.de> | 2023-10-25 12:50:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-25 12:50:15 +0200 |
commit | 5efa84cf6feaaa03e6325836f89503b0ab2be0d8 (patch) | |
tree | 64b06bd9ac996762fc2ac3e36edafd056dc27918 /clang/lib/Format/WhitespaceManager.cpp | |
parent | 6242c8ca18bcd6765094c73dd2c8b49200a6cec8 (diff) | |
download | llvm-5efa84cf6feaaa03e6325836f89503b0ab2be0d8.zip llvm-5efa84cf6feaaa03e6325836f89503b0ab2be0d8.tar.gz llvm-5efa84cf6feaaa03e6325836f89503b0ab2be0d8.tar.bz2 |
[clang-format] Don't align comments over scopes
We now stop aligning trailing comments on all closing braces, for
classes etc. we even check for the semicolon between the comment and the
brace.
Fixes #67906.
Diffstat (limited to 'clang/lib/Format/WhitespaceManager.cpp')
-rw-r--r-- | clang/lib/Format/WhitespaceManager.cpp | 53 |
1 files changed, 44 insertions, 9 deletions
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index dbe6175..ff8b1e6 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -1048,6 +1048,9 @@ void WhitespaceManager::alignChainedConditionals() { } void WhitespaceManager::alignTrailingComments() { + if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never) + return; + const int Size = Changes.size(); int MinColumn = 0; int StartOfSequence = 0; @@ -1118,16 +1121,48 @@ void WhitespaceManager::alignTrailingComments() { } } - // We don't want to align namespace end comments. - const bool DontAlignThisComment = - I > 0 && C.NewlinesBefore == 0 && - Changes[I - 1].Tok->is(TT_NamespaceRBrace); - if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Never || - DontAlignThisComment) { + // We don't want to align comments which end a scope, which are here + // identified by most closing braces. + auto DontAlignThisComment = [](const auto *Tok) { + if (Tok->is(tok::semi)) { + Tok = Tok->getPreviousNonComment(); + if (!Tok) + return false; + } + if (Tok->is(tok::r_paren)) { + // Back up past the parentheses and a `TT_DoWhile` that may precede. + Tok = Tok->MatchingParen; + if (!Tok) + return false; + Tok = Tok->getPreviousNonComment(); + if (!Tok) + return false; + if (Tok->is(TT_DoWhile)) { + const auto *Prev = Tok->getPreviousNonComment(); + if (!Prev) { + // A do-while-loop without braces. + return true; + } + Tok = Prev; + } + } + + if (Tok->isNot(tok::r_brace)) + return false; + + while (Tok->Previous && Tok->Previous->is(tok::r_brace)) + Tok = Tok->Previous; + return Tok->NewlinesBefore > 0; + }; + + if (I > 0 && C.NewlinesBefore == 0 && + DontAlignThisComment(Changes[I - 1].Tok)) { alignTrailingComments(StartOfSequence, I, MinColumn); - MinColumn = ChangeMinColumn; - MaxColumn = ChangeMinColumn; - StartOfSequence = I; + // Reset to initial values, but skip this change for the next alignment + // pass. + MinColumn = 0; + MaxColumn = INT_MAX; + StartOfSequence = I + 1; } else if (BreakBeforeNext || Newlines > NewLineThreshold || (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) || // Break the comment sequence if the previous line did not end |