r329918 - [clang-format] Improve ObjC guessing heuristic by supporting all @keywords

Ben Hamilton via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 12 08:11:53 PDT 2018


Author: benhamilton
Date: Thu Apr 12 08:11:53 2018
New Revision: 329918

URL: http://llvm.org/viewvc/llvm-project?rev=329918&view=rev
Log:
[clang-format] Improve ObjC guessing heuristic by supporting all @keywords

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

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=329918&r1=329917&r2=329918&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Apr 12 08:11:53 2018
@@ -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() &&

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=329918&r1=329917&r2=329918&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Apr 12 08:11:53 2018
@@ -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 at 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 at end\n"));
   EXPECT_EQ(FormatStyle::LK_ObjC,




More information about the cfe-commits mailing list