r181996 - Comments should not prevent single-line functions.

Daniel Jasper djasper at google.com
Thu May 16 03:17:39 PDT 2013


Author: djasper
Date: Thu May 16 05:17:39 2013
New Revision: 181996

URL: http://llvm.org/viewvc/llvm-project?rev=181996&view=rev
Log:
Comments should not prevent single-line functions.

Before:
void f() {}
void g() {
} // comment

After:
void f() {}
void g() {} // comment

Modified:
    cfe/trunk/lib/Format/Format.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=181996&r1=181995&r2=181996&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu May 16 05:17:39 2013
@@ -1418,7 +1418,7 @@ private:
       return;
 
     AnnotatedToken *Tok = &(I + 1)->First;
-    if (Tok->Children.empty() && Tok->is(tok::r_brace) &&
+    if (Tok->getNextNoneComment() == NULL && Tok->is(tok::r_brace) &&
         !Tok->MustBreakBefore) {
       // We merge empty blocks even if the line exceeds the column limit.
       Tok->SpacesRequiredBefore = 0;
@@ -1443,7 +1443,7 @@ private:
 
       // Last, check that the third line contains a single closing brace.
       Tok = &(I + 2)->First;
-      if (!Tok->Children.empty() || Tok->isNot(tok::r_brace) ||
+      if (Tok->getNextNoneComment() != NULL || Tok->isNot(tok::r_brace) ||
           Tok->MustBreakBefore)
         return;
 

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=181996&r1=181995&r2=181996&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu May 16 05:17:39 2013
@@ -3043,6 +3043,8 @@ TEST_F(FormatTest, PullTrivialFunctionDe
                "  int a;\n"
                "#error {\n"
                "}");
+  verifyFormat("void f() {} // comment");
+  verifyFormat("void f() { int a; } // comment");
 
   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));





More information about the cfe-commits mailing list