From: Ben Hamilton Date: Tue, 27 Feb 2018 15:56:40 +0000 (+0000) Subject: [clang-format] Tidy up new API guessLanguage() X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=caee7084597e6ac9a181a2e60997e3b09dc51cf3;p=clang [clang-format] Tidy up new API guessLanguage() Summary: This fixes a few issues djasper@ brought up in his review of D43522. Test Plan: make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D43598 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326205 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index b82aab9711..9229e224ea 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -2295,8 +2295,8 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { } FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) { - FormatStyle::LanguageKind result = getLanguageByFileName(FileName); - if (result == FormatStyle::LK_Cpp) { + const auto GuessedLanguage = getLanguageByFileName(FileName); + if (GuessedLanguage == FormatStyle::LK_Cpp) { auto Extension = llvm::sys::path::extension(FileName); // If there's no file extension (or it's .h), we need to check the contents // of the code to see if it contains Objective-C. @@ -2306,12 +2306,11 @@ FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) { Environment::CreateVirtualEnvironment(Code, NonEmptyFileName, /*Ranges=*/{}); ObjCHeaderStyleGuesser Guesser(*Env, getLLVMStyle()); Guesser.process(); - if (Guesser.isObjC()) { - result = FormatStyle::LK_ObjC; - } + if (Guesser.isObjC()) + return FormatStyle::LK_ObjC; } } - return result; + return GuessedLanguage; } llvm::Expected getStyle(StringRef StyleName, StringRef FileName, diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 082f6e4864..1cda9827e1 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -11959,34 +11959,16 @@ TEST_F(FormatTest, StructuredBindings) { verifyFormat("auto const &[ a, b ] = f();", Spaces); } -struct GuessLanguageTestCase { - const char *const FileName; - const char *const Code; - const FormatStyle::LanguageKind ExpectedResult; -}; - -class GuessLanguageTest - : public FormatTest, - public ::testing::WithParamInterface {}; - -TEST_P(GuessLanguageTest, FileAndCode) { - auto TestCase = GetParam(); - EXPECT_EQ(TestCase.ExpectedResult, - guessLanguage(TestCase.FileName, TestCase.Code)); +TEST_F(FormatTest, FileAndCode) { + EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", "")); + EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", "")); + EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", "")); + EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.h", "")); + EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.h", "@interface Foo\n@end\n")); + EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo", "")); + EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); } -static const GuessLanguageTestCase TestCases[] = { - {"foo.cc", "", FormatStyle::LK_Cpp}, - {"foo.m", "", FormatStyle::LK_ObjC}, - {"foo.mm", "", FormatStyle::LK_ObjC}, - {"foo.h", "", FormatStyle::LK_Cpp}, - {"foo.h", "@interface Foo\n@end\n", FormatStyle::LK_ObjC}, - {"foo", "", FormatStyle::LK_Cpp}, - {"foo", "@interface Foo\n@end\n", FormatStyle::LK_ObjC}, -}; -INSTANTIATE_TEST_CASE_P(ValidLanguages, GuessLanguageTest, - ::testing::ValuesIn(TestCases)); - } // end namespace } // end namespace format } // end namespace clang