summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2017-12-12 13:43:59 +0000
committerKrasimir Georgiev <krasimir@google.com>2017-12-12 13:43:59 +0000
commit11ef531b5cc7c94765a07ea247e438b6a48b9c31 (patch)
tree4d3179395729a9022c4b885cb75f27985afb28cb
parent2128df7e7ba35697ad749540b962c8170778e499 (diff)
downloadbcm5719-llvm-11ef531b5cc7c94765a07ea247e438b6a48b9c31.tar.gz
bcm5719-llvm-11ef531b5cc7c94765a07ea247e438b6a48b9c31.zip
[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! llvm-svn: 320479
-rw-r--r--clang/lib/Format/Format.cpp4
-rw-r--r--clang/unittests/Format/FormatTestObjC.cpp11
2 files changed, 14 insertions, 1 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index a30ecc21ea3..38e8163d3fe 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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();
diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp
index 1f9fc451d21..4220b44b4c4 100644
--- a/clang/unittests/Format/FormatTestObjC.cpp
+++ b/clang/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);
OpenPOWER on IntegriCloud