]> granicus.if.org Git - clang/commitdiff
[clang-format] Improve ObjC guessing heuristic by supporting all @keywords
authorBen Hamilton <benhamilton@google.com>
Thu, 12 Apr 2018 15:11:53 +0000 (15:11 +0000)
committerBen Hamilton <benhamilton@google.com>
Thu, 12 Apr 2018 15:11:53 +0000 (15:11 +0000)
Summary:
This diff improves the Objective-C guessing heuristic by
replacing the hard-coded list of a subset of Objective-C @keywords
with a general check which supports all @keywords.

I also added a few more Foundation keywords which were missing from
the heuristic.

Test Plan: Unit tests updated. Ran tests with:
  % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests

Reviewers: djasper, jolesiak

Reviewed By: djasper

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D45521

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

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index f7e74abab37b4fdef096c7be87b8c4a713b2385c..98b2656ee2996d992d4e33c4b82dcc417b284e09 100644 (file)
@@ -1465,6 +1465,7 @@ private:
         "NSAffineTransform",
         "NSArray",
         "NSAttributedString",
+        "NSBlockOperation",
         "NSBundle",
         "NSCache",
         "NSCalendar",
@@ -1480,6 +1481,7 @@ private:
         "NSIndexPath",
         "NSIndexSet",
         "NSInteger",
+        "NSInvocationOperation",
         "NSLocale",
         "NSMapTable",
         "NSMutableArray",
@@ -1494,9 +1496,13 @@ private:
         "NSNumber",
         "NSNumberFormatter",
         "NSObject",
+        "NSOperation",
+        "NSOperationQueue",
+        "NSOperationQueuePriority",
         "NSOrderedSet",
         "NSPoint",
         "NSPointerArray",
+        "NSQualityOfService",
         "NSRange",
         "NSRect",
         "NSRegularExpression",
@@ -1518,10 +1524,7 @@ private:
       for (const FormatToken *FormatTok = Line->First; FormatTok;
            FormatTok = FormatTok->Next) {
         if ((FormatTok->Previous && FormatTok->Previous->is(tok::at) &&
-             (FormatTok->isObjCAtKeyword(tok::objc_interface) ||
-              FormatTok->isObjCAtKeyword(tok::objc_implementation) ||
-              FormatTok->isObjCAtKeyword(tok::objc_protocol) ||
-              FormatTok->isObjCAtKeyword(tok::objc_end) ||
+             (FormatTok->Tok.getObjCKeywordID() != tok::objc_not_keyword ||
               FormatTok->isOneOf(tok::numeric_constant, tok::l_square,
                                  tok::l_brace))) ||
             (FormatTok->Tok.isAnyIdentifier() &&
index d1869d2494a34da31c2ab8d5d8a013abc4a6d210..4397cdfe69275227f7627eb039d9578c849d2368 100644 (file)
@@ -12118,6 +12118,12 @@ TEST_F(FormatTest, FileAndCode) {
   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", ""));
   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n"));
+  EXPECT_EQ(
+      FormatStyle::LK_ObjC,
+      guessLanguage("foo.h", "#define TRY(x, y) @try { x; } @finally { y; }"));
+  EXPECT_EQ(FormatStyle::LK_ObjC,
+            guessLanguage("foo.h", "#define AVAIL(x) @available(x, *))"));
+  EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@class Foo;"));
   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
   EXPECT_EQ(FormatStyle::LK_ObjC,