[PATCH] D27971: Make FormatStyle.GetStyleOfFile test work on MSVC

Antonio Maiorano via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 19 21:39:45 PST 2016


amaiorano created this revision.
amaiorano added reviewers: klimek, djasper, hans, cfe-commits.

This is more a workaround than a real fix. The real problem is that FS.addFile uses clang::vfs::FileSystem::makeAbsolute to convert the input path to an absolute path, while getStyle uses llvm::sys::fs::make_absolute, which while named similarly,  behave differently. Both will join the input path to the current working directory (CWD), except that in the former case, you need to have set the CWD explicitly via clang::vfs::FileSystem::setCurrentWorkingDirectory, while the latter retrieves the CWD from the platform abstraction (llvm::sys::fs::current_path).

A better fix might be to make clang::vfs::FileSystem match the behaviour of llvm::sys::fs, having it retrieve the CWD from the platform, rather than having the client set it explicitly. Or perhaps clang::vfs::FileSystem should be rewritten in terms of llvm::sys::fs, and deprecate the former so that code moves towards using the latter.


https://reviews.llvm.org/D27971

Files:
  unittests/Format/FormatTest.cpp


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -10907,12 +10907,16 @@
             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
 }
 
-// Since this test case uses UNIX-style file path. We disable it for MS
-// compiler.
-#if !defined(_MSC_VER) && !defined(__MINGW32__)
-
 TEST(FormatStyle, GetStyleOfFile) {
   vfs::InMemoryFileSystem FS;
+
+  // Set CWD so that clang::vfs::FileSystem::makeAbsolute and
+  // llvm::sys::fs::make_absolute return the same thing.
+  llvm::SmallString<128> InitialDirectory;
+  std::error_code EC = llvm::sys::fs::current_path(InitialDirectory);
+  EXPECT_EQ(EC.value(), 0);
+  FS.setCurrentWorkingDirectory(InitialDirectory);
+
   // Test 1: format file in the same directory.
   ASSERT_TRUE(
       FS.addFile("/a/.clang-format", 0,
@@ -10938,8 +10942,6 @@
   ASSERT_EQ(Style3, getGoogleStyle());
 }
 
-#endif // _MSC_VER
-
 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
   // Column limit is 20.
   std::string Code = "Type *a =\n"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27971.82063.patch
Type: text/x-patch
Size: 1125 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161220/bc6da8bb/attachment.bin>


More information about the cfe-commits mailing list