]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix regression when getStyle() called with empty filename
authorBen Hamilton <benhamilton@google.com>
Wed, 21 Feb 2018 21:27:27 +0000 (21:27 +0000)
committerBen Hamilton <benhamilton@google.com>
Wed, 21 Feb 2018 21:27:27 +0000 (21:27 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@325722 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 54b10f474c0509ad7641dacc4f77244b3187e4d5..b82aab9711331ebbba632ce911cd23c5b08ee956 100644 (file)
@@ -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()) {
index c029a749318135214123e4f05938fa494e738cd3..f87fd8481afc5a46d1be80a7f622dac13deeda30 100644 (file)
@@ -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.