Index: cfe/trunk/include/clang/Format/Format.h =================================================================== --- cfe/trunk/include/clang/Format/Format.h +++ cfe/trunk/include/clang/Format/Format.h @@ -1981,6 +1981,10 @@ StringRef Code = "", vfs::FileSystem *FS = nullptr); +// \brief Guesses the language from the ``FileName`` and ``Code`` to be formatted. +// Defaults to FormatStyle::LK_Cpp. +FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code); + // \brief Returns a string representation of ``Language``. inline StringRef getLanguageName(FormatStyle::LanguageKind Language) { switch (Language) { Index: cfe/trunk/lib/Format/Format.cpp =================================================================== --- cfe/trunk/lib/Format/Format.cpp +++ cfe/trunk/lib/Format/Format.cpp @@ -2294,6 +2294,25 @@ return FormatStyle::LK_Cpp; } +FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) { + FormatStyle::LanguageKind result = getLanguageByFileName(FileName); + if (result == 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. + if (extension.empty() || extension == ".h") { + std::unique_ptr Env = + Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); + ObjCHeaderStyleGuesser Guesser(*Env, getLLVMStyle()); + Guesser.process(); + if (Guesser.isObjC()) { + result = FormatStyle::LK_ObjC; + } + } + } + return result; +} + llvm::Expected getStyle(StringRef StyleName, StringRef FileName, StringRef FallbackStyleName, StringRef Code, vfs::FileSystem *FS) { @@ -2301,17 +2320,7 @@ FS = vfs::getRealFileSystem().get(); } FormatStyle Style = getLLVMStyle(); - Style.Language = getLanguageByFileName(FileName); - - if (Style.Language == FormatStyle::LK_Cpp && FileName.endswith(".h")) { - std::unique_ptr Env = - Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); - ObjCHeaderStyleGuesser Guesser(*Env, Style); - Guesser.process(); - if (Guesser.isObjC()) { - Style.Language = FormatStyle::LK_ObjC; - } - } + Style.Language = guessLanguage(FileName, Code); FormatStyle FallbackStyle = getNoStyle(); if (!getPredefinedStyle(FallbackStyleName, Style.Language, &FallbackStyle)) Index: cfe/trunk/unittests/Format/FormatTest.cpp =================================================================== --- cfe/trunk/unittests/Format/FormatTest.cpp +++ cfe/trunk/unittests/Format/FormatTest.cpp @@ -11952,6 +11952,34 @@ 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)); +} + +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