[cfe-commits] r171761 - in /cfe/trunk: lib/Format/UnwrappedLineParser.cpp lib/Format/UnwrappedLineParser.h test/Index/comment-cplus-decls.cpp test/Index/comment-to-html-xml-conversion.cpp unittests/Format/FormatTest.cpp

Manuel Klimek klimek at google.com
Mon Jan 7 10:10:23 PST 2013


Author: klimek
Date: Mon Jan  7 12:10:23 2013
New Revision: 171761

URL: http://llvm.org/viewvc/llvm-project?rev=171761&view=rev
Log:
Fix parsing of variable declarations directly after a class / struct.

Previous indent:
class A {
}
a;
void f() {
};

With this patch:
class A {
} a;
void f() {
}
;

The patch introduces a production for classes and structs, and parses
the rest of the line to the semicolon after the class scope.
This allowed us to remove a long-standing wart in the parser that would
just much the semicolon after any block.
Due to this suboptimal formating some tests were broken.

Some unrelated formatting tests broke; those hit a bug in the ast
printing, and need to be fixed separately.

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.h
    cfe/trunk/test/Index/comment-cplus-decls.cpp
    cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=171761&r1=171760&r2=171761&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Mon Jan  7 12:10:23 2013
@@ -129,13 +129,10 @@
   parseLevel(/*HasOpeningBrace=*/true);
   Line.Level -= AddLevels;
 
-  // FIXME: Add error handling.
   if (!FormatTok.Tok.is(tok::r_brace))
     return true;
 
-  nextToken();
-  if (FormatTok.Tok.is(tok::semi))
-    nextToken();
+  nextToken();  // Munch the closing brace.
   return false;
 }
 
@@ -246,6 +243,10 @@
     case tok::kw_enum:
       parseEnum();
       return;
+    case tok::kw_struct:  // fallthrough
+    case tok::kw_class:
+      parseStructOrClass();
+      return;
     case tok::semi:
       nextToken();
       addUnwrappedLine();
@@ -459,6 +460,26 @@
   } while (!eof());
 }
 
+void UnwrappedLineParser::parseStructOrClass() {
+  nextToken();
+  do {
+    switch (FormatTok.Tok.getKind()) {
+    case tok::l_brace:
+      // FIXME: Think about how to resolve the error handling here.
+      parseBlock();
+      parseStructuralElement();
+      return;
+    case tok::semi:
+      nextToken();
+      addUnwrappedLine();
+      return;
+    default:
+      nextToken();
+      break;
+    }
+  } while (!eof());
+}
+
 void UnwrappedLineParser::addUnwrappedLine() {
   // Consume trailing comments.
   while (!eof() && FormatTok.NewlinesBefore == 0 &&

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=171761&r1=171760&r2=171761&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Mon Jan  7 12:10:23 2013
@@ -128,6 +128,7 @@
   void parseNamespace();
   void parseAccessSpecifier();
   void parseEnum();
+  void parseStructOrClass();
   void addUnwrappedLine();
   bool eof() const;
   void nextToken();

Modified: cfe/trunk/test/Index/comment-cplus-decls.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/comment-cplus-decls.cpp?rev=171761&r1=171760&r2=171761&view=diff
==============================================================================
--- cfe/trunk/test/Index/comment-cplus-decls.cpp (original)
+++ cfe/trunk/test/Index/comment-cplus-decls.cpp Mon Jan  7 12:10:23 2013
@@ -162,7 +162,7 @@
 */
     template <class T> friend class valarray;
 };
-// CHECK: <Declaration>template <class T = unsigned int> class valarray {\n}\ntemplate <class T> class valarray</Declaration>
+// CHECK: <Declaration>template <class T = unsigned int> class valarray {\n} template <class T> class valarray</Declaration>
 // CHECK: <Declaration>friend template <class T> class valarray</Declaration>
 
 class gslice

Modified: cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp?rev=171761&r1=171760&r2=171761&view=diff
==============================================================================
--- cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp (original)
+++ cfe/trunk/test/Index/comment-to-html-xml-conversion.cpp Mon Jan  7 12:10:23 2013
@@ -670,7 +670,7 @@
 template<typename T, typename U>
 class comment_to_xml_conversion_11 { };
 
-// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplate=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CT>2#T#T at comment_to_xml_conversion_11</USR><Declaration>template <typename T = int, typename U = int>\nclass comment_to_xml_conversion_11 {\n}\ntemplate <typename T, typename U> class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:7: ClassTemplate=comment_to_xml_conversion_11:{{.*}} FullCommentAsXML=[<Class templateKind="template" file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="7"><Name>comment_to_xml_conversion_11</Name><USR>c:@CT>2#T#T at comment_to_xml_conversion_11</USR><Declaration>template <typename T = int, typename U = int>\nclass comment_to_xml_conversion_11 {\n} template <typename T, typename U> class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
 
 /// Aaa.
 template<typename T>

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171761&r1=171760&r2=171761&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan  7 12:10:23 2013
@@ -332,6 +332,13 @@
                "};");
 }
 
+TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
+  verifyFormat("class A {\n"
+               "} a, b;");
+  verifyFormat("struct A {\n"
+               "} a, b;");
+}
+
 TEST_F(FormatTest, FormatsEnum) {
   verifyFormat("enum {\n"
                "  Zero,\n"
@@ -624,7 +631,7 @@
 TEST_F(FormatTest, BreaksDesireably) {
   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
-               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};");
+               "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
 
   verifyFormat(
       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
@@ -974,14 +981,14 @@
                "int qwerty;");
   verifyFormat("public\n"
                "B {\n"
-               "};");
+               "}");
   verifyFormat("public\n"
                "{\n"
-               "};");
+               "}");
   verifyFormat("public\n"
                "B {\n"
                "  int x;\n"
-               "};");
+               "}");
 }
 
 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
@@ -990,16 +997,16 @@
 
 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
   verifyFormat("do {\n"
-               "};");
+               "}");
   verifyFormat("do {\n"
-               "};\n"
+               "}\n"
                "f();");
   verifyFormat("do {\n"
                "}\n"
                "wheeee(fun);");
   verifyFormat("do {\n"
                "  f();\n"
-               "};");
+               "}");
 }
 
 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {





More information about the cfe-commits mailing list