[clang] [clang-format] Don't align comments over scopes (PR #68743)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Oct 21 22:45:02 PDT 2023
================
@@ -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)) {
+ auto Prev = Tok->getPreviousNonComment();
+ if (!Prev) {
+ // A do-while-loop without braces.
+ return Tok->NewlinesBefore > 0;
+ }
----------------
owenca wrote:
```suggestion
const auto *Prev = Tok->getPreviousNonComment();
if (!Prev)
return true;
```
to cover the cases like `/**/ while (i > 0); //`.
https://github.com/llvm/llvm-project/pull/68743
More information about the cfe-commits
mailing list