[PATCH] D43124: [clang-format] Improve ObjC headers detection

Jacek Olesiak via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 14 02:43:46 PST 2018


jolesiak updated this revision to Diff 134180.
jolesiak added a comment.
This revision is now accepted and ready to land.

Added support for ObjC characteristic keywords starting a new line.


Repository:
  rC Clang

https://reviews.llvm.org/D43124

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTestObjC.cpp


Index: unittests/Format/FormatTestObjC.cpp
===================================================================
--- unittests/Format/FormatTestObjC.cpp
+++ unittests/Format/FormatTestObjC.cpp
@@ -113,10 +113,6 @@
   ASSERT_TRUE((bool)Style);
   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
 
-  Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
-  ASSERT_TRUE((bool)Style);
-  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
-
   Style =
       getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
   ASSERT_TRUE((bool)Style);
@@ -126,10 +122,6 @@
   ASSERT_TRUE((bool)Style);
   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
 
-  Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
-  ASSERT_TRUE((bool)Style);
-  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
-
   Style =
       getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
   ASSERT_TRUE((bool)Style);
@@ -154,6 +146,23 @@
                    "inline void Foo() { int foo[] = {1, 2, 3}; }\n");
   ASSERT_TRUE((bool)Style);
   EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
+
+  // ObjC characteristic types.
+  Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+  Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+  Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+  Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
 }
 
 TEST_F(FormatTestObjC, FormatObjCTryCatch) {
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1440,7 +1440,9 @@
         "NSAffineTransform",
         "NSArray",
         "NSAttributedString",
+        "NSBundle",
         "NSCache",
+        "NSCalendar",
         "NSCharacterSet",
         "NSCountedSet",
         "NSData",
@@ -1466,6 +1468,7 @@
         "NSMutableString",
         "NSNumber",
         "NSNumberFormatter",
+        "NSObject",
         "NSOrderedSet",
         "NSPoint",
         "NSPointerArray",
@@ -1475,17 +1478,19 @@
         "NSSet",
         "NSSize",
         "NSString",
+        "NSTimeZone",
         "NSUInteger",
         "NSURL",
         "NSURLComponents",
         "NSURLQueryItem",
         "NSUUID",
+        "NSValue",
     };
 
     for (auto &Line : AnnotatedLines) {
-      for (FormatToken *FormatTok = Line->First->Next; FormatTok;
+      for (FormatToken *FormatTok = Line->First; FormatTok;
            FormatTok = FormatTok->Next) {
-        if ((FormatTok->Previous->is(tok::at) &&
+        if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) &&
              (FormatTok->isObjCAtKeyword(tok::objc_interface) ||
               FormatTok->isObjCAtKeyword(tok::objc_implementation) ||
               FormatTok->isObjCAtKeyword(tok::objc_protocol) ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43124.134180.patch
Type: text/x-patch
Size: 3174 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180214/00becfad/attachment-0001.bin>


More information about the cfe-commits mailing list