[PATCH] D19804: Make clang-format cleaner remove redundant commas in list and redundant colon in constructor initializer.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Thu May 12 09:46:47 PDT 2016


djasper added a comment.

I experimented a bit. What do you think of this?


================
Comment at: lib/Format/Format.cpp:1822
@@ +1821,3 @@
+        cleanupRight(Line->First, Line->Last, tok::comma, tok::comma);
+        checkConstructorInitList(*Line);
+      }
----------------
You could turn this into:

    for (auto &Line : AnnotatedLines) {
      if (Line->Affected) {
        cleanupRight(Line->First, tok::comma, tok::comma);
        cleanupRight(Line->First, TT_CtorInitializerColon, tok::comma);
        cleanupLeft(Line->First, tok::comma, tok::l_brace);
        cleanupLeft(Line->First, TT_CtorInitializerColon, tok::l_brace);
      }
    }

================
Comment at: lib/Format/Format.cpp:1914
@@ -1906,1 +1913,3 @@
 
+  FormatToken *getNextTokenNotDeletedUntilEnd(const FormatToken *Tok,
+                                              const FormatToken *End,
----------------
And all of this into:

  // Checks pairs {start, start->next},..., {end->previous, end} and deletes one
  // of the token in the pair if the left token has \p LK token kind and the
  // right token has \p RK token kind. If \p DeleteLeft is true, the left token
  // is deleted on match; otherwise, the right token is deleted.
  template <typename LeftKind, typename RightKind>
  void cleanupPair(FormatToken *Start, LeftKind LK, RightKind RK,
                   bool DeleteLeft) {
    auto NextNotDeleted = [this](const FormatToken &Tok) -> FormatToken * {
      for (auto *Res = Tok.Next; Res; Res = Res->Next)
        if (!Res->is(tok::comment) &&
            DeletedTokens.find(Res) == DeletedTokens.end())
          return Res;
      return nullptr;
    };
    for (auto *Left = Start; Left;) {
      auto *Right = NextNotDeleted(*Left);
      if (!Right)
        break;
      if (Left->is(LK) && Right->is(RK)) {
        deleteToken(DeleteLeft ? Left : Right);
        // If the right token is deleted, we should keep the left token
        // unchanged and pair it with the new right token.
        if (!DeleteLeft)
          continue;
      }
      Left = Right;
    }
  }

  template <typename LeftKind, typename RightKind>
  void cleanupLeft(FormatToken *Start, LeftKind LK, RightKind RK) {
    cleanupPair(Start, LK, RK, /*DeleteLeft=*/true);
  }

  template <typename LeftKind, typename RightKind>
  void cleanupRight(FormatToken *Start, LeftKind LK, RightKind RK) {
    cleanupPair(Start, LK, RK, /*DeleteLeft=*/false);
  }




http://reviews.llvm.org/D19804





More information about the cfe-commits mailing list