]> granicus.if.org Git - clang/commitdiff
Fix parsing of variable declarations directly after a class / struct.
authorManuel Klimek <klimek@google.com>
Mon, 7 Jan 2013 18:10:23 +0000 (18:10 +0000)
committerManuel Klimek <klimek@google.com>
Mon, 7 Jan 2013 18:10:23 +0000 (18:10 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171761 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 00ca3a53347486116255dc49f32a34e5100f2a9c..d7220259b7e7feaaab30e6d2cc79581dba5c908f 100644 (file)
@@ -129,13 +129,10 @@ bool UnwrappedLineParser::parseBlock(unsigned AddLevels) {
   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 @@ void UnwrappedLineParser::parseStructuralElement() {
     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 @@ void UnwrappedLineParser::parseEnum() {
   } 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 &&
index 27c11020554b7f221b21ba5dace9505158868b4a..a9f0475e32f2b600b2721997a6b5dc235d16b0c7 100644 (file)
@@ -128,6 +128,7 @@ private:
   void parseNamespace();
   void parseAccessSpecifier();
   void parseEnum();
+  void parseStructOrClass();
   void addUnwrappedLine();
   bool eof() const;
   void nextToken();
index 29af712e1ce94853ae61eb1a7e2620f0852e4e9b..ed851d84fc84b6341d8bb096b869f71f6ddbfd13 100644 (file)
@@ -162,7 +162,7 @@ private:
 */
     template <class T> friend class valarray;
 };
-// CHECK: <Declaration>template &lt;class T = unsigned int&gt; class valarray {\n}\ntemplate &lt;class T&gt; class valarray</Declaration>
+// CHECK: <Declaration>template &lt;class T = unsigned int&gt; class valarray {\n} template &lt;class T&gt; class valarray</Declaration>
 // CHECK: <Declaration>friend template &lt;class T&gt; class valarray</Declaration>
 
 class gslice
index 1ab49d6fc0152bacf64e2dab742adce8114938e7..139120ec05194f65f90210ae8a73e598399911a6 100644 (file)
@@ -670,7 +670,7 @@ void comment_to_xml_conversion_10(int aaa, int bbb);
 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&gt;2#T#T@comment_to_xml_conversion_11</USR><Declaration>template &lt;typename T = int, typename U = int&gt;\nclass comment_to_xml_conversion_11 {\n}\ntemplate &lt;typename T, typename U&gt; 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&gt;2#T#T@comment_to_xml_conversion_11</USR><Declaration>template &lt;typename T = int, typename U = int&gt;\nclass comment_to_xml_conversion_11 {\n} template &lt;typename T, typename U&gt; class comment_to_xml_conversion_11 {\n}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]
 
 /// Aaa.
 template<typename T>
index d4490dddfc316880ef8539dd80c94a9870366f9f..2ba4765cc7294118301fde54073532f78fc0ab17 100644 (file)
@@ -332,6 +332,13 @@ TEST_F(FormatTest, FormatsDerivedClass) {
                "};");
 }
 
+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, BreaksAsHighAsPossible) {
 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 @@ TEST_F(FormatTest, IncorrectAccessSpecifier) {
                "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, IncorrectCodeUnbalancedBraces) {
 
 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) {