benhamilton created this revision.
benhamilton added reviewers: jolesiak, djasper.
Herald added subscribers: cfe-commits, klimek.

Previously, the Objective-C heuristic failed to detect
headers which #imported Objective-C system framework(s) and declared C
functions on top them.

This extends the heuristic to look for statements of the form:

and check if "Foo" is one of the known Objective-C system frameworks.

Test Plan: New tests added. Ran tests with:

  % make -j12 FormatTests &&
  ./tools/clang/unittests/Format/FormatTests


Repository:
  rC Clang

https://reviews.llvm.org/D44634

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

Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -12097,6 +12097,14 @@
   EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", ""));
   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n"));
   EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "int DoStuff(CGRect rect);\n"));
+  EXPECT_EQ(FormatStyle::LK_ObjC,
+            guessLanguage("foo.h", "#import <Foundation/Foundation.h>\n"));
+  EXPECT_EQ(FormatStyle::LK_ObjC,
+            guessLanguage("foo.h", "#import <UIKit/UIKit.h>\n"));
+  EXPECT_EQ(FormatStyle::LK_Cpp,
+            guessLanguage("foo.h", "#import <some/other/header.h>\n"));
+  EXPECT_EQ(FormatStyle::LK_Cpp,
+            guessLanguage("foo.h", "#include <Foundation/Other/Path.h>\n"));
   EXPECT_EQ(
       FormatStyle::LK_ObjC,
       guessLanguage("foo.h",
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -1513,6 +1513,79 @@
         "UIImage",
         "UIView",
     };
+    // Keep this array sorted, since we are binary searching over it.
+    static constexpr llvm::StringLiteral ObjCFrameworks[] = {
+        "ARKit",
+        "AVFoundation",
+        "Accounts",
+        "AddressBookUI",
+        "AppKit",
+        "AssetsLibrary",
+        "AudioToolbox",
+        "CloudKit",
+        "Contacts",
+        "ContactsUI",
+        "CoreAudioKit",
+        "CoreBluetooth",
+        "CoreData",
+        "CoreImage",
+        "CoreLocation",
+        "CoreMIDI",
+        "CoreML",
+        "CoreMotion",
+        "CoreNFC",
+        "CoreSpotlight",
+        "CoreTelephony",
+        "DeviceCheck",
+        "EventKit",
+        "ExternalAccessory",
+        "FileProvider",
+        "FileProviderUI",
+        "Foundation",
+        "GLKit",
+        "GameController",
+        "GameKit",
+        "GameplayKit",
+        "HealthKit",
+        "HomeKit",
+        "IOSurface",
+        "IdentityLookup",
+        "Intents",
+        "IntentsUI",
+        "JavaScriptCore",
+        "MapKit",
+        "MediaPlayer",
+        "MessageUI",
+        "Metal",
+        "MetalKit",
+        "MetalPerformanceShaders",
+        "ModelIO",
+        "MultipeerConnectivity",
+        "NetworkExtension",
+        "NewsstandKit",
+        "NotificationCenter",
+        "PDFKit",
+        "PassKit",
+        "Photos",
+        "PhotosUI",
+        "PushKit",
+        "QuartzCore",
+        "QuickLook",
+        "ReplayKit",
+        "SafariServices",
+        "SceneKit",
+        "Social",
+        "Speech",
+        "SpriteKit",
+        "StoreKit",
+        "UIKit",
+        "UserNotifications",
+        "VideoSubscriberAccount",
+        "Vision",
+        "WatchConnectivity",
+        "WatchKit",
+        "WebKit",
+    };
 
     for (auto &Line : AnnotatedLines) {
       for (FormatToken *FormatTok = Line->First; FormatTok;
@@ -1534,6 +1607,18 @@
                                TT_ObjCDecl, TT_ObjCForIn, TT_ObjCMethodExpr,
                                TT_ObjCMethodSpecifier, TT_ObjCProperty)) {
           return true;
+        } else if (Line->Type == LT_ImportStatement &&
+                   !FormatTok->getPreviousNonComment() &&
+                   // #import <Foo/Bar.h>
+                   FormatTok->startsSequence(
+                       tok::hash, tok::identifier, tok::less, tok::identifier,
+                       tok::slash, tok::identifier, tok::period,
+                       tok::identifier, tok::greater) &&
+                   FormatTok->Next->TokenText == "import" &&
+                   std::binary_search(std::begin(ObjCFrameworks),
+                                      std::end(ObjCFrameworks),
+                                      FormatTok->Next->Next->Next->TokenText)) {
+          return true;
         }
       }
     }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to