benhamilton created this revision. benhamilton added reviewers: vsapsai, jolesiak, krasimir. Herald added subscribers: cfe-commits, klimek.
https://reviews.llvm.org/D43522 caused an assertion failure when getStyle() was called with an empty filename: https://reviews.llvm.org/P8065 This adds a test to reproduce the failure and fixes the issue by ensuring we never pass an empty filename to Environment::CreateVirtualEnvironment(). Test Plan: New test added. Ran test with: % make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests Before diff, test failed with P8065. Now, test passes. Repository: rC Clang https://reviews.llvm.org/D43590 Files: lib/Format/Format.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -11723,6 +11723,12 @@ 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. Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -2297,12 +2297,13 @@ 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()) {
Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -11723,6 +11723,12 @@ 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. Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -2297,12 +2297,13 @@ 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()) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits