diff options
-rw-r--r-- | clang/lib/Format/Format.cpp | 7 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 6 |
2 files changed, 10 insertions, 3 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 54b10f474c0..b82aab97113 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -2297,12 +2297,13 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) { FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) { FormatStyle::LanguageKind result = getLanguageByFileName(FileName); if (result == FormatStyle::LK_Cpp) { - auto extension = llvm::sys::path::extension(FileName); + 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") { + if (Extension.empty() || Extension == ".h") { + auto NonEmptyFileName = FileName.empty() ? "guess.h" : FileName; std::unique_ptr<Environment> Env = - Environment::CreateVirtualEnvironment(Code, FileName, /*Ranges=*/{}); + Environment::CreateVirtualEnvironment(Code, NonEmptyFileName, /*Ranges=*/{}); ObjCHeaderStyleGuesser Guesser(*Env, getLLVMStyle()); Guesser.process(); if (Guesser.isObjC()) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c029a749318..f87fd8481af 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -11723,6 +11723,12 @@ TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } +TEST(FormatStyle, GetStyleWithEmptyFileName) { + auto Style1 = getStyle("file", "", "Google"); + ASSERT_TRUE((bool)Style1); + ASSERT_EQ(*Style1, getGoogleStyle()); +} + TEST(FormatStyle, GetStyleOfFile) { vfs::InMemoryFileSystem FS; // Test 1: format file in the same directory. |