[clang] [clang-format] Don't align comments over scopes (PR #68743)

Björn Schäpers via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 12:06:38 PDT 2023


================
@@ -1118,16 +1121,35 @@ 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.
+    const bool DontAlignThisComment = [&] {
+      if (I == 0 || C.NewlinesBefore > 0)
+        return false;
+      const auto *Tok = Changes[I - 1].Tok;
+      if (Tok->is(tok::semi)) {
+        Tok = Tok->Previous;
+        if (!Tok)
+          return false;
+      }
+      if (Tok->isNot(tok::r_brace))
+        return false;
+      auto LastBrace = Tok;
+      while (Tok->Previous && Tok->Previous->is(tok::r_brace))
+        Tok = Tok->Previous;
+      return Tok->NewlinesBefore > 0 ||
+             LastBrace->isOneOf(TT_ClassRBrace, TT_EnumRBrace,
+                                TT_NamespaceRBrace, TT_StructRBrace,
+                                TT_UnionRBrace);
----------------
HazardyKnusperkeks wrote:

That is a nice argument.

https://github.com/llvm/llvm-project/pull/68743


More information about the cfe-commits mailing list