]> granicus.if.org Git - clang/commitdiff
[clang-format] Improve ObjC headers detection.
authorKrasimir Georgiev <krasimir@google.com>
Tue, 12 Dec 2017 13:43:59 +0000 (13:43 +0000)
committerKrasimir Georgiev <krasimir@google.com>
Tue, 12 Dec 2017 13:43:59 +0000 (13:43 +0000)
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!

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

lib/Format/Format.cpp
unittests/Format/FormatTestObjC.cpp

index a30ecc21ea32452cc4096d30e878874b13566532..38e8163d3feb3af94838a52fa69d582830535098 100644 (file)
@@ -2129,7 +2129,9 @@ llvm::Expected<FormatStyle> getStyle(StringRef StyleName, StringRef FileName,
   // 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@end\n") || Code.contains("\n@end ") ||
+       Code.endswith("@end")))
     Style.Language = FormatStyle::LK_ObjC;
 
   FormatStyle FallbackStyle = getNoStyle();
index 1f9fc451d213b7afe4aa22699ef9224b765d20ba..4220b44b4c47fa96ffbdf982f5af341713879e21 100644 (file)
@@ -79,6 +79,17 @@ TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
   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);