[cfe-commits] r172019 - in /cfe/trunk: lib/Format/UnwrappedLineParser.cpp lib/Format/UnwrappedLineParser.h test/Index/comment-objc-decls.m unittests/Format/FormatTest.cpp

Nico Weber nicolasweber at gmx.de
Wed Jan 9 15:25:38 PST 2013


Author: nico
Date: Wed Jan  9 17:25:37 2013
New Revision: 172019

URL: http://llvm.org/viewvc/llvm-project?rev=172019&view=rev
Log:
Formatter: Add support for @implementation.

Just reuse the @interface code for this. It accepts slightly more than
necessary (@implementation cannot have protocol lists), but that's ok.


Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.h
    cfe/trunk/test/Index/comment-objc-decls.m
    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=172019&r1=172018&r2=172019&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Jan  9 17:25:37 2013
@@ -209,7 +209,8 @@
     case tok::objc_private:
       return parseAccessSpecifier();
     case tok::objc_interface:
-      return parseObjCInterface();
+    case tok::objc_implementation:
+      return parseObjCInterfaceOrImplementation();
     case tok::objc_protocol:
       return parseObjCProtocol();
     case tok::objc_end:
@@ -519,7 +520,7 @@
   } while (!eof());
 }
 
-void UnwrappedLineParser::parseObjCInterface() {
+void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
   nextToken();
   nextToken();  // interface name
 

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=172019&r1=172018&r2=172019&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Wed Jan  9 17:25:37 2013
@@ -144,7 +144,7 @@
   void parseStructOrClass();
   void parseObjCProtocolList();
   void parseObjCUntilAtEnd();
-  void parseObjCInterface();
+  void parseObjCInterfaceOrImplementation();
   void parseObjCProtocol();
   void addUnwrappedLine();
   bool eof() const;

Modified: cfe/trunk/test/Index/comment-objc-decls.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/comment-objc-decls.m?rev=172019&r1=172018&r2=172019&view=diff
==============================================================================
--- cfe/trunk/test/Index/comment-objc-decls.m (original)
+++ cfe/trunk/test/Index/comment-objc-decls.m Wed Jan  9 17:25:37 2013
@@ -162,7 +162,7 @@
 */
 - (void) setPropertyMyClassCategory : (id) arg {}
 @end
-// CHECK: <Declaration>@implementation MyClass(Category)  @end</Declaration>
+// CHECK: <Declaration>@implementation MyClass(Category)\n at end</Declaration>
 // CHECK: <Declaration>- (void)MethodMyClassCategory;</Declaration>
 // CHECK: <Declaration>- (id)PropertyMyClassCategory;</Declaration>
 // CHECK: <Declaration>- (void)setPropertyMyClassCategory:(id)arg;</Declaration>
@@ -172,4 +172,4 @@
 */
 @implementation NSObject
 @end
-// CHECK: <Declaration>@implementation NSObject @end</Declaration>
+// CHECK: <Declaration>@implementation NSObject\n at end</Declaration>

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172019&r1=172018&r2=172019&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan  9 17:25:37 2013
@@ -1251,6 +1251,77 @@
                "@end");
 }
 
+TEST_F(FormatTest, FormatObjCImplementation) {
+  verifyFormat("@implementation Foo : NSObject {\n"
+               "@public\n"
+               "  int field1;\n"
+               "@protected\n"
+               "  int field2;\n"
+               "@private\n"
+               "  int field3;\n"
+               "@package\n"
+               "  int field4;\n"
+               "}\n"
+               "+ (id)init {\n"
+               "}\n"
+               "@end");
+
+  verifyGoogleFormat("@implementation Foo : NSObject {\n"
+                     " @public\n"
+                     "  int field1;\n"
+                     " @protected\n"
+                     "  int field2;\n"
+                     " @private\n"
+                     "  int field3;\n"
+                     " @package\n"
+                     "  int field4;\n"
+                     "}\n"
+                     "+ (id)init {\n"
+                     "}\n"
+                     "@end");
+
+  verifyFormat("@implementation Foo\n"
+               "+ (id)init {\n"
+               "  if (true)\n"
+               "    return nil;\n"
+               "}\n"
+               "// Look, a comment!\n"
+               "- (int)answerWith:(int)i {\n"
+               "  return i;\n"
+               "}\n"
+               "@end");
+
+  verifyFormat("@implementation Foo\n"
+               "@end\n"
+               "@implementation Bar\n"
+               "@end");
+
+  verifyFormat("@implementation Foo : Bar\n"
+               "+ (id)init {\n"
+               "}\n"
+               "@end");
+
+  verifyFormat("@implementation Foo {\n"
+               "  int _i;\n"
+               "}\n"
+               "+ (id)init {\n"
+               "}\n"
+               "@end");
+
+  verifyFormat("@implementation Foo : Bar {\n"
+               "  int _i;\n"
+               "}\n"
+               "+ (id)init {\n"
+               "}\n"
+               "@end");
+
+  // FIXME: there should be a space before '(' for categories.
+  verifyFormat("@implementation Foo(HackStuff)\n"
+               "+ (id)init {\n"
+               "}\n"
+               "@end");
+}
+
 TEST_F(FormatTest, FormatObjCProtocol) {
   verifyFormat("@protocol Foo\n"
                "@property(weak) id delegate;\n"





More information about the cfe-commits mailing list