From 2ce0ac5a8c6773dbb36d20f93f78f481812e1a34 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 9 Jan 2013 23:25:37 +0000 Subject: [PATCH] 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. llvm-svn: 172019 --- clang/lib/Format/UnwrappedLineParser.cpp | 5 +- clang/lib/Format/UnwrappedLineParser.h | 2 +- clang/test/Index/comment-objc-decls.m | 4 +- clang/unittests/Format/FormatTest.cpp | 71 ++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 1d0cb30af845..c049ac607d3f 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -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 diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h index 303afc2d98da..16ad37eec631 100644 --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -144,7 +144,7 @@ private: void parseStructOrClass(); void parseObjCProtocolList(); void parseObjCUntilAtEnd(); - void parseObjCInterface(); + void parseObjCInterfaceOrImplementation(); void parseObjCProtocol(); void addUnwrappedLine(); bool eof() const; diff --git a/clang/test/Index/comment-objc-decls.m b/clang/test/Index/comment-objc-decls.m index 4c9f878c3564..c61d99598d0c 100644 --- a/clang/test/Index/comment-objc-decls.m +++ b/clang/test/Index/comment-objc-decls.m @@ -162,7 +162,7 @@ */ - (void) setPropertyMyClassCategory : (id) arg {} @end -// CHECK: @implementation MyClass(Category) @end +// CHECK: @implementation MyClass(Category)\n@end // CHECK: - (void)MethodMyClassCategory; // CHECK: - (id)PropertyMyClassCategory; // CHECK: - (void)setPropertyMyClassCategory:(id)arg; @@ -172,4 +172,4 @@ */ @implementation NSObject @end -// CHECK: @implementation NSObject @end +// CHECK: @implementation NSObject\n@end diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 15fe9aa9d737..b536e012ebd1 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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"