[PATCH] D19804: Make clang-format cleaner remove redundant commas in list and redundant colon in constructor initializer.
Eric Liu via cfe-commits
cfe-commits at lists.llvm.org
Fri May 13 02:37:51 PDT 2016
ioeric added inline comments.
================
Comment at: lib/Format/Format.cpp:1822
@@ +1821,3 @@
+ cleanupRight(Line->First, Line->Last, tok::comma, tok::comma);
+ checkConstructorInitList(*Line);
+ }
----------------
ioeric wrote:
> ioeric wrote:
> > djasper wrote:
> > > 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);
> > > }
> > > }
> > Wouldn't `cleanupLeft(Line->First, tok::comma, tok::l_brace);` also remove the comma from `std::vector<std::vector<int>> = {{...}, {...}}`?
> I should've added this case into unit test, sorry...
>
> But I think we can either handle constructor initializer's tok::l_brace specially or annotate it? The later solution can enable us to do `cleanupLeft(Line->First, tok::comma, TT_CtorInitializerLBrace);`.
Just found out constructor initializer's commas are already annotated. Then, this can be easily fixed with `cleanupLeft(Line->First, TT_CtorInitializerComma, tok::l_brace);`.
================
Comment at: lib/Format/Format.cpp:1914
@@ -1906,1 +1913,3 @@
+ FormatToken *getNextTokenNotDeletedUntilEnd(const FormatToken *Tok,
+ const FormatToken *End,
----------------
djasper wrote:
> 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);
> }
>
>
Thanks for the awesome templates!
http://reviews.llvm.org/D19804
More information about the cfe-commits
mailing list