<p dir="ltr">Won't this still be broken in mingw (GCC, but on Windows)?</p>
<div class="gmail_quote">On Mar 24, 2016 9:28 AM, "Eric Liu via cfe-commits" <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: ioeric<br>
Date: Thu Mar 24 08:22:42 2016<br>
New Revision: 264277<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=264277&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=264277&view=rev</a><br>
Log:<br>
Added support for different VFSs in format::getStyle. Disable platform-related test case for MS compilers to avoid breaking buildbot.<br>
<br>
Modified:<br>
    cfe/trunk/include/clang/Format/Format.h<br>
    cfe/trunk/lib/Format/Format.cpp<br>
    cfe/trunk/unittests/Format/FormatTest.cpp<br>
<br>
Modified: cfe/trunk/include/clang/Format/Format.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=264277&r1=264276&r2=264277&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=264277&r1=264276&r2=264277&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Format/Format.h (original)<br>
+++ cfe/trunk/include/clang/Format/Format.h Thu Mar 24 08:22:42 2016<br>
@@ -16,6 +16,7 @@<br>
 #define LLVM_CLANG_FORMAT_FORMAT_H<br>
<br>
 #include "clang/Basic/LangOptions.h"<br>
+#include "clang/Basic/VirtualFileSystem.h"<br>
 #include "clang/Tooling/Core/Replacement.h"<br>
 #include "llvm/ADT/ArrayRef.h"<br>
 #include <system_error><br>
@@ -832,11 +833,13 @@ extern const char *StyleOptionHelpDescri<br>
 /// == "file".<br>
 /// \param[in] FallbackStyle The name of a predefined style used to fallback to<br>
 /// in case the style can't be determined from \p StyleName.<br>
+/// \param[in] FS The underlying file system, in which the file resides. By<br>
+/// default, the file system is the real file system.<br>
 ///<br>
 /// \returns FormatStyle as specified by ``StyleName``. If no style could be<br>
 /// determined, the default is LLVM Style (see ``getLLVMStyle()``).<br>
 FormatStyle getStyle(StringRef StyleName, StringRef FileName,<br>
-                     StringRef FallbackStyle);<br>
+                     StringRef FallbackStyle, vfs::FileSystem *FS = nullptr);<br>
<br>
 } // end namespace format<br>
 } // end namespace clang<br>
<br>
Modified: cfe/trunk/lib/Format/Format.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=264277&r1=264276&r2=264277&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=264277&r1=264276&r2=264277&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Format/Format.cpp (original)<br>
+++ cfe/trunk/lib/Format/Format.cpp Thu Mar 24 08:22:42 2016<br>
@@ -2099,7 +2099,10 @@ static FormatStyle::LanguageKind getLang<br>
 }<br>
<br>
 FormatStyle getStyle(StringRef StyleName, StringRef FileName,<br>
-                     StringRef FallbackStyle) {<br>
+                     StringRef FallbackStyle, vfs::FileSystem *FS) {<br>
+  if (!FS) {<br>
+    FS = vfs::getRealFileSystem().get();<br>
+  }<br>
   FormatStyle Style = getLLVMStyle();<br>
   Style.Language = getLanguageByFileName(FileName);<br>
   if (!getPredefinedStyle(FallbackStyle, Style.Language, &Style)) {<br>
@@ -2130,28 +2133,34 @@ FormatStyle getStyle(StringRef StyleName<br>
   llvm::sys::fs::make_absolute(Path);<br>
   for (StringRef Directory = Path; !Directory.empty();<br>
        Directory = llvm::sys::path::parent_path(Directory)) {<br>
-    if (!llvm::sys::fs::is_directory(Directory))<br>
+<br>
+    auto Status = FS->status(Directory);<br>
+    if (!Status ||<br>
+        Status->getType() != llvm::sys::fs::file_type::directory_file) {<br>
       continue;<br>
+    }<br>
+<br>
     SmallString<128> ConfigFile(Directory);<br>
<br>
     llvm::sys::path::append(ConfigFile, ".clang-format");<br>
     DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");<br>
-    bool IsFile = false;<br>
-    // Ignore errors from is_regular_file: we only need to know if we can read<br>
-    // the file or not.<br>
-    llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);<br>
<br>
+    Status = FS->status(ConfigFile.str());<br>
+    bool IsFile =<br>
+        Status && (Status->getType() == llvm::sys::fs::file_type::regular_file);<br>
     if (!IsFile) {<br>
       // Try _clang-format too, since dotfiles are not commonly used on Windows.<br>
       ConfigFile = Directory;<br>
       llvm::sys::path::append(ConfigFile, "_clang-format");<br>
       DEBUG(llvm::dbgs() << "Trying " << ConfigFile << "...\n");<br>
-      llvm::sys::fs::is_regular_file(Twine(ConfigFile), IsFile);<br>
+      Status = FS->status(ConfigFile.str());<br>
+      IsFile = Status &&<br>
+               (Status->getType() == llvm::sys::fs::file_type::regular_file);<br>
     }<br>
<br>
     if (IsFile) {<br>
       llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Text =<br>
-          llvm::MemoryBuffer::getFile(ConfigFile.c_str());<br>
+          FS->getBufferForFile(ConfigFile.str());<br>
       if (std::error_code EC = Text.getError()) {<br>
         llvm::errs() << EC.message() << "\n";<br>
         break;<br>
<br>
Modified: cfe/trunk/unittests/Format/FormatTest.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=264277&r1=264276&r2=264277&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=264277&r1=264276&r2=264277&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)<br>
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Mar 24 08:22:42 2016<br>
@@ -14,6 +14,7 @@<br>
<br>
 #include "clang/Frontend/TextDiagnosticPrinter.h"<br>
 #include "llvm/Support/Debug.h"<br>
+#include "llvm/Support/MemoryBuffer.h"<br>
 #include "gtest/gtest.h"<br>
<br>
 #define DEBUG_TYPE "format-test"<br>
@@ -11199,6 +11200,39 @@ TEST_F(FormatTest, FormatsTableGenCode)<br>
   verifyFormat("include \"<a href="http://a.td" rel="noreferrer" target="_blank">a.td</a>\"\ninclude \"<a href="http://b.td" rel="noreferrer" target="_blank">b.td</a>\"", Style);<br>
 }<br>
<br>
+// Since this test case uses UNIX-style file path. We disable it for MS<br>
+// compiler.<br>
+#if !defined(_MSC_VER)<br>
+<br>
+TEST(FormatStyle, GetStyleOfFile) {<br>
+  vfs::InMemoryFileSystem FS;<br>
+  // Test 1: format file in the same directory.<br>
+  ASSERT_TRUE(<br>
+      FS.addFile("/a/.clang-format", 0,<br>
+                 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));<br>
+  ASSERT_TRUE(<br>
+      FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));<br>
+  auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);<br>
+  ASSERT_EQ(Style1, getLLVMStyle());<br>
+<br>
+  // Test 2: fallback to default.<br>
+  ASSERT_TRUE(<br>
+      FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));<br>
+  auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);<br>
+  ASSERT_EQ(Style2, getMozillaStyle());<br>
+<br>
+  // Test 3: format file in parent directory.<br>
+  ASSERT_TRUE(<br>
+      FS.addFile("/c/.clang-format", 0,<br>
+                 llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));<br>
+  ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,<br>
+                         llvm::MemoryBuffer::getMemBuffer("int i;")));<br>
+  auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);<br>
+  ASSERT_EQ(Style3, getGoogleStyle());<br>
+}<br>
+<br>
+#endif // _MSC_VER<br>
+<br>
 class ReplacementTest : public ::testing::Test {<br>
 protected:<br>
   tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>