r320479 - [clang-format] Improve ObjC headers detection.

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 12 05:43:59 PST 2017


Author: krasimir
Date: Tue Dec 12 05:43:59 2017
New Revision: 320479

URL: http://llvm.org/viewvc/llvm-project?rev=320479&view=rev
Log:
[clang-format] Improve ObjC headers detection.

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.

Contributed by jolesiak!

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=320479&r1=320478&r2=320479&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Dec 12 05:43:59 2017
@@ -2129,7 +2129,9 @@ llvm::Expected<FormatStyle> getStyle(Str
   // 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();

Modified: cfe/trunk/unittests/Format/FormatTestObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestObjC.cpp?rev=320479&r1=320478&r2=320479&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestObjC.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestObjC.cpp Tue Dec 12 05:43:59 2017
@@ -79,6 +79,17 @@ TEST(FormatTestObjCStyle, DetectsObjCInH
   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);




More information about the cfe-commits mailing list