[PATCH] D43590: [clang-format] Fix regression when getStyle() called with empty filename

Ben Hamilton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 21 13:30:28 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rC325722: [clang-format] Fix regression when getStyle() called with empty filename (authored by benhamilton, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D43590?vs=135314&id=135321#toc

Repository:
  rC Clang

https://reviews.llvm.org/D43590

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp


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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D43590.135321.patch
Type: text/x-patch
Size: 1663 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180221/d0ff4950/attachment-0001.bin>


More information about the cfe-commits mailing list