r183112 - Let clang-format remove empty lines before "}".

Daniel Jasper djasper at google.com
Mon Jun 3 09:30:08 PDT 2013


No, this is excluded specifically (there is also a test for it). Basically
we currently only remove the empty line if the next line is "}" or "};".


On Mon, Jun 3, 2013 at 6:22 PM, Jordan Rose <jordan_rose at apple.com> wrote:

> Does this also apply to if-else chains? I sometimes have a blank line
> before the next "} else if (...) {" that I wouldn't want removed.
>
>
> On Jun 3, 2013, at 9:16 , Daniel Jasper <djasper at google.com> wrote:
>
> > Author: djasper
> > Date: Mon Jun  3 11:16:41 2013
> > New Revision: 183112
> >
> > URL: http://llvm.org/viewvc/llvm-project?rev=183112&view=rev
> > Log:
> > Let clang-format remove empty lines before "}".
> >
> > These lines almost never aid readability.
> >
> > Before:
> > void f() {
> >  int i;  // some variable
> >
> > }
> >
> > After:
> > void f() {
> >  int i;  // some variable
> > }
> >
> > Modified:
> >    cfe/trunk/lib/Format/Format.cpp
> >    cfe/trunk/lib/Format/TokenAnnotator.cpp
> >    cfe/trunk/unittests/Format/FormatTest.cpp
> >
> > Modified: cfe/trunk/lib/Format/Format.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=183112&r1=183111&r2=183112&view=diff
> >
> ==============================================================================
> > --- cfe/trunk/lib/Format/Format.cpp (original)
> > +++ cfe/trunk/lib/Format/Format.cpp Mon Jun  3 11:16:41 2013
> > @@ -1592,6 +1592,11 @@ private:
> >                         bool InPPDirective) {
> >     unsigned Newlines =
> >         std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep +
> 1);
> > +    // Remove empty lines before "}" where applicable.
> > +    if (RootToken.is(tok::r_brace) &&
> > +        (!RootToken.Next ||
> > +         (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
> > +      Newlines = std::min(Newlines, 1u);
> >     if (Newlines == 0 && !RootToken.IsFirst)
> >       Newlines = 1;
> >
> >
> > Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=183112&r1=183111&r2=183112&view=diff
> >
> ==============================================================================
> > --- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
> > +++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Jun  3 11:16:41 2013
> > @@ -54,7 +54,7 @@ private:
> >                                 tok::question, tok::colon))
> >         return false;
> >       // If a && or || is found and interpreted as a binary operator,
> this set
> > -      // of angles is like part of something like "a < b && c > d". If
> the
> > +      // of angles is likely part of something like "a < b && c > d".
> If the
> >       // angles are inside an expression, the ||/&& might also be a
> binary
> >       // operator that was misinterpreted because we are parsing template
> >       // parameters.
> >
> > Modified: cfe/trunk/unittests/Format/FormatTest.cpp
> > URL:
> http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=183112&r1=183111&r2=183112&view=diff
> >
> ==============================================================================
> > --- cfe/trunk/unittests/Format/FormatTest.cpp (original)
> > +++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jun  3 11:16:41 2013
> > @@ -192,6 +192,53 @@ TEST_F(FormatTest, RemovesWhitespaceWhen
> >             format("int  a;\n  \n\n int b;", 9, 0, getLLVMStyle()));
> > }
> >
> > +TEST_F(FormatTest, RemovesEmptyLines) {
> > +  EXPECT_EQ("class C {\n"
> > +            "  int i;\n"
> > +            "};",
> > +            format("class C {\n"
> > +                   " int i;\n"
> > +                   "\n"
> > +                   "};"));
> > +
> > +  // Don't remove empty lines in more complex control statements.
> > +  EXPECT_EQ("void f() {\n"
> > +            "  if (a) {\n"
> > +            "    f();\n"
> > +            "\n"
> > +            "  } else if (b) {\n"
> > +            "    f();\n"
> > +            "  }\n"
> > +            "}",
> > +            format("void f() {\n"
> > +                   "  if (a) {\n"
> > +                   "    f();\n"
> > +                   "\n"
> > +                   "  } else if (b) {\n"
> > +                   "    f();\n"
> > +                   "\n"
> > +                   "  }\n"
> > +                   "\n"
> > +                   "}"));
> > +
> > +  // FIXME: This is slightly inconsistent.
> > +  EXPECT_EQ("namespace {\n"
> > +            "int i;\n"
> > +            "}",
> > +            format("namespace {\n"
> > +                   "int i;\n"
> > +                   "\n"
> > +                   "}"));
> > +  EXPECT_EQ("namespace {\n"
> > +            "int i;\n"
> > +            "\n"
> > +            "} // namespace",
> > +            format("namespace {\n"
> > +                   "int i;\n"
> > +                   "\n"
> > +                   "}  // namespace"));
> > +}
> > +
> > TEST_F(FormatTest, ReformatsMovedLines) {
> >   EXPECT_EQ(
> >       "template <typename T> T *getFETokenInfo() const {\n"
> >
> >
> > _______________________________________________
> > cfe-commits mailing list
> > cfe-commits at cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130603/72f91aef/attachment.html>


More information about the cfe-commits mailing list