[cfe-commits] r172001 - 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 13:15:04 PST 2013


Author: nico
Date: Wed Jan  9 15:15:03 2013
New Revision: 172001

URL: http://llvm.org/viewvc/llvm-project?rev=172001&view=rev
Log:
Formatting: Add support for @protocol.

Pull pieces of the @interface code into reusable methods.


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=172001&r1=172000&r2=172001&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Jan  9 15:15:03 2013
@@ -210,6 +210,8 @@
       return parseAccessSpecifier();
     case tok::objc_interface:
       return parseObjCInterface();
+    case tok::objc_protocol:
+      return parseObjCProtocol();
     default:
       break;
     }
@@ -496,6 +498,25 @@
   } 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 @@
     // 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 @@
   // 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() {

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=172001&r1=172000&r2=172001&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Wed Jan  9 15:15:03 2013
@@ -142,7 +142,10 @@
   void parseAccessSpecifier();
   void parseEnum();
   void parseStructOrClass();
+  void parseObjCProtocolList();
+  void parseObjCUntilAtEnd();
   void parseObjCInterface();
+  void parseObjCProtocol();
   void addUnwrappedLine();
   bool eof() const;
   void nextToken();

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=172001&r1=172000&r2=172001&view=diff
==============================================================================
--- cfe/trunk/test/Index/comment-objc-decls.m (original)
+++ cfe/trunk/test/Index/comment-objc-decls.m Wed Jan  9 15:15:03 2013
@@ -30,7 +30,7 @@
 */
 + ClassMethodMyProto;
 @end
-// CHECK: <Declaration>@protocol MyProto @end</Declaration>
+// CHECK: <Declaration>@protocol MyProto\n at 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>

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=172001&r1=172000&r2=172001&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Jan  9 15:15:03 2013
@@ -1249,6 +1249,21 @@
                "@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");





More information about the cfe-commits mailing list