]> granicus.if.org Git - clang/commitdiff
Formatter: Add support for @implementation.
authorNico Weber <nicolasweber@gmx.de>
Wed, 9 Jan 2013 23:25:37 +0000 (23:25 +0000)
committerNico Weber <nicolasweber@gmx.de>
Wed, 9 Jan 2013 23:25:37 +0000 (23:25 +0000)
Just reuse the @interface code for this. It accepts slightly more than
necessary (@implementation cannot have protocol lists), but that's ok.

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

lib/Format/UnwrappedLineParser.cpp
lib/Format/UnwrappedLineParser.h
test/Index/comment-objc-decls.m
unittests/Format/FormatTest.cpp

index 1d0cb30af8459a609906b3e646ede4c658aa8d5a..c049ac607d3f0ac101b210b9f7bb52662394ca0c 100644 (file)
@@ -209,7 +209,8 @@ void UnwrappedLineParser::parseStructuralElement() {
     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 @@ void UnwrappedLineParser::parseObjCUntilAtEnd() {
   } while (!eof());
 }
 
-void UnwrappedLineParser::parseObjCInterface() {
+void UnwrappedLineParser::parseObjCInterfaceOrImplementation() {
   nextToken();
   nextToken();  // interface name
 
index 303afc2d98daa86f830d13f90738b0a84a3e1749..16ad37eec631c416ae86664d1fdb621f96e48791 100644 (file)
@@ -144,7 +144,7 @@ private:
   void parseStructOrClass();
   void parseObjCProtocolList();
   void parseObjCUntilAtEnd();
-  void parseObjCInterface();
+  void parseObjCInterfaceOrImplementation();
   void parseObjCProtocol();
   void addUnwrappedLine();
   bool eof() const;
index 4c9f878c3564759184db1533ee31e179efbe7247..c61d99598d0cc4a1bbfc7d9a3d97172a07544d1f 100644 (file)
 */
 - (void) setPropertyMyClassCategory : (id) arg {}
 @end
-// CHECK: <Declaration>@implementation MyClass(Category)  @end</Declaration>
+// CHECK: <Declaration>@implementation MyClass(Category)\n@end</Declaration>
 // CHECK: <Declaration>- (void)MethodMyClassCategory;</Declaration>
 // CHECK: <Declaration>- (id)PropertyMyClassCategory;</Declaration>
 // CHECK: <Declaration>- (void)setPropertyMyClassCategory:(id)arg;</Declaration>
 */
 @implementation NSObject
 @end
-// CHECK: <Declaration>@implementation NSObject @end</Declaration>
+// CHECK: <Declaration>@implementation NSObject\n@end</Declaration>
index 15fe9aa9d737ad2087f55c3fd3a38b9bc75e1a5a..b536e012ebd10ed78f0ee4d93a7bd75880bd059b 100644 (file)
@@ -1251,6 +1251,77 @@ TEST_F(FormatTest, FormatObjCInterface) {
                "@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"