r325722 - [clang-format] Fix regression when getStyle() called with empty filename
Ben Hamilton via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 21 13:27:27 PST 2018
Author: benhamilton
Date: Wed Feb 21 13:27:27 2018
New Revision: 325722
URL: http://llvm.org/viewvc/llvm-project?rev=325722&view=rev
Log:
[clang-format] Fix regression when getStyle() called with empty filename
Summary:
D43522 caused an assertion failure when getStyle() was called with
an empty filename:
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.
Reviewers: vsapsai, jolesiak, krasimir
Reviewed By: vsapsai
Subscribers: klimek, cfe-commits
Differential Revision: https://reviews.llvm.org/D43590
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=325722&r1=325721&r2=325722&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Wed Feb 21 13:27:27 2018
@@ -2297,12 +2297,13 @@ static FormatStyle::LanguageKind getLang
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()) {
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=325722&r1=325721&r2=325722&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Feb 21 13:27:27 2018
@@ -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.
More information about the cfe-commits
mailing list