From: Krasimir Georgiev Date: Tue, 12 Dec 2017 13:43:59 +0000 (+0000) Subject: [clang-format] Improve ObjC headers detection. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=006661785ecc08a648d2cb01fc21b898f81e3c40;p=clang [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! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@320479 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index a30ecc21ea..38e8163d3f 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -2129,7 +2129,9 @@ llvm::Expected 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(); diff --git a/unittests/Format/FormatTestObjC.cpp b/unittests/Format/FormatTestObjC.cpp index 1f9fc451d2..4220b44b4c 100644 --- a/unittests/Format/FormatTestObjC.cpp +++ b/unittests/Format/FormatTestObjC.cpp @@ -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);