]> granicus.if.org Git - clang/commitdiff
Formatting: Add support for @protocol.
authorNico Weber <nicolasweber@gmx.de>
Wed, 9 Jan 2013 21:15:03 +0000 (21:15 +0000)
committerNico Weber <nicolasweber@gmx.de>
Wed, 9 Jan 2013 21:15:03 +0000 (21:15 +0000)
Pull pieces of the @interface code into reusable methods.

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

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

index 131d982906b6f907bdee82222f8d8010c6238d93..688937c1e6aa5af2f691c0fab159f32cccf66b0a 100644 (file)
@@ -210,6 +210,8 @@ void UnwrappedLineParser::parseStructuralElement() {
       return parseAccessSpecifier();
     case tok::objc_interface:
       return parseObjCInterface();
+    case tok::objc_protocol:
+      return parseObjCProtocol();
     default:
       break;
     }
@@ -496,6 +498,25 @@ void UnwrappedLineParser::parseStructOrClass() {
   } while (!eof());
 }
 
+void UnwrappedLineParser::parseObjCProtocolList() {
+  assert(FormatTok.Tok.is(tok::less) && "'<' expected.");
+  do
+    nextToken();
+  while (!eof() && FormatTok.Tok.isNot(tok::greater));
+  nextToken(); // Skip '>'.
+}
+
+void UnwrappedLineParser::parseObjCUntilAtEnd() {
+  do {
+    if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
+      nextToken();
+      addUnwrappedLine();
+      break;
+    }
+    parseStructuralElement();
+  } while (!eof());
+}
+
 void UnwrappedLineParser::parseObjCInterface() {
   nextToken();
   nextToken();  // interface name
@@ -508,13 +529,8 @@ void UnwrappedLineParser::parseObjCInterface() {
     // Skip category, if present.
     parseParens();
 
-  // Skip protocol list, if present.
-  if (FormatTok.Tok.is(tok::less)) {
-    do
-      nextToken();
-    while (!eof() && FormatTok.Tok.isNot(tok::greater));
-    nextToken(); // Skip '>'.
-  }
+  if (FormatTok.Tok.is(tok::less))
+    parseObjCProtocolList();
 
   // If instance variables are present, keep the '{' on the first line too.
   if (FormatTok.Tok.is(tok::l_brace))
@@ -524,16 +540,24 @@ void UnwrappedLineParser::parseObjCInterface() {
   // variables, this ends the @interface line.
   addUnwrappedLine();
 
-  // Read everything up to the @end.
-  do {
-    if (FormatTok.Tok.isObjCAtKeyword(tok::objc_end)) {
-      nextToken();
-      addUnwrappedLine();
-      break;
-    }
+  parseObjCUntilAtEnd();
+}
 
-    parseStructuralElement();
-  } while (!eof());
+void UnwrappedLineParser::parseObjCProtocol() {
+  nextToken();
+  nextToken();  // protocol name
+
+  if (FormatTok.Tok.is(tok::less))
+    parseObjCProtocolList();
+
+  // Check for protocol declaration.
+  if (FormatTok.Tok.is(tok::semi)) {
+    nextToken();
+    return addUnwrappedLine();
+  }
+
+  addUnwrappedLine();
+  parseObjCUntilAtEnd();
 }
 
 void UnwrappedLineParser::addUnwrappedLine() {
index 28ef235190019a19feb31021bc22206bff4be648..303afc2d98daa86f830d13f90738b0a84a3e1749 100644 (file)
@@ -142,7 +142,10 @@ private:
   void parseAccessSpecifier();
   void parseEnum();
   void parseStructOrClass();
+  void parseObjCProtocolList();
+  void parseObjCUntilAtEnd();
   void parseObjCInterface();
+  void parseObjCProtocol();
   void addUnwrappedLine();
   bool eof() const;
   void nextToken();
index 4b9a4bcaba031d8e2156c54420497b0418bd4b40..4c9f878c3564759184db1533ee31e179efbe7247 100644 (file)
@@ -30,7 +30,7 @@
 */
 + ClassMethodMyProto;
 @end
-// CHECK: <Declaration>@protocol MyProto @end</Declaration>
+// CHECK: <Declaration>@protocol MyProto\n@end</Declaration>
 // CHECK: <Declaration>- (unsigned int)MethodMyProto:(id)anObject inRange:(unsigned int)range;</Declaration>
 // CHECK: <Declaration>@optional\n    @property(readwrite, copy, atomic) id PropertyMyProto;</Declaration>
 // CHECK: <Declaration>+ (id)ClassMethodMyProto;</Declaration>
index 4decf94c766f5dfeaa537883c5958b04e21e8436..e06544a95f20574f89692a069f862c189c19bbbf 100644 (file)
@@ -1249,6 +1249,21 @@ TEST_F(FormatTest, FormatObjCInterface) {
                "@end");
 }
 
+TEST_F(FormatTest, FormatObjCProtocol) {
+  verifyFormat("@protocol Foo\n"
+               "@property(weak) id delegate;\n"
+               "- (NSUInteger)numberOfThings;\n"
+               "@end");
+
+  // FIXME: In LLVM style, there should be a space before '<' for protocols.
+  verifyFormat("@protocol MyProtocol<NSObject>\n"
+               "- (NSUInteger)numberOfThings;\n"
+               "@end");
+
+  verifyFormat("@protocol Foo;\n"
+               "@protocol Bar;\n");
+}
+
 TEST_F(FormatTest, ObjCAt) {
   verifyFormat("@autoreleasepool");
   verifyFormat("@catch");