[PATCH] D41108: [clang-format] Improve ObjC headers detection.

Jacek Olesiak via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 12 04:13:39 PST 2017


jolesiak created this revision.
jolesiak added a reviewer: krasimir.
Herald added a subscriber: klimek.

This patch improves detection of ObjC header files.
Right now many ObjC headers, especially short ones, are categorized as C/C++.

Way of filtering still isn't the best, as most likely it should be token-based.


Repository:
  rC Clang

https://reviews.llvm.org/D41108

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


Index: unittests/Format/FormatTestObjC.cpp
===================================================================
--- unittests/Format/FormatTestObjC.cpp
+++ unittests/Format/FormatTestObjC.cpp
@@ -79,6 +79,17 @@
   ASSERT_TRUE((bool)Style);
   EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
 
+  Style = getStyle("LLVM", "a.h", "none", "@interface\n"
+                                          "@end\n"
+                                          "//comment");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
+  Style = getStyle("LLVM", "a.h", "none", "@interface\n"
+                                          "@end //comment");
+  ASSERT_TRUE((bool)Style);
+  EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
+
   // No recognizable ObjC.
   Style = getStyle("LLVM", "a.h", "none", "void f() {}");
   ASSERT_TRUE((bool)Style);
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -2129,7 +2129,9 @@
   // should be improved over time and probably be done on tokens, not one the
   // bare content of the file.
   if (Style.Language == FormatStyle::LK_Cpp && FileName.endswith(".h") &&
-      (Code.contains("\n- (") || Code.contains("\n+ (")))
+      (Code.contains("\n- (") || Code.contains("\n+ (") ||
+       Code.contains("\n at end\n") || Code.contains("\n at end ") ||
+       Code.endswith("@end")))
     Style.Language = FormatStyle::LK_ObjC;
 
   FormatStyle FallbackStyle = getNoStyle();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D41108.126526.patch
Type: text/x-patch
Size: 1529 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171212/64d890d8/attachment.bin>


More information about the cfe-commits mailing list