]> granicus.if.org Git - clang/commitdiff
[vfs] Normalize working directory if requested.
authorBenjamin Kramer <benny.kra@googlemail.com>
Sat, 9 Jan 2016 16:33:16 +0000 (16:33 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Sat, 9 Jan 2016 16:33:16 +0000 (16:33 +0000)
FixedCompilationDatabase sets the working dir to "." by default. For
chdir(".") this is a noop but this lead to InMemoryFileSystem to create
bogus paths. Fixes PR25327.

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

include/clang/Basic/VirtualFileSystem.h
lib/Basic/VirtualFileSystem.cpp
unittests/Basic/VirtualFileSystemTest.cpp

index 1df4947dd7e81cd8180fc00980920df664c76525..bab88c90b0a88ae274ffdb9d6fa5b8e22dcd4176 100644 (file)
@@ -299,10 +299,7 @@ public:
   llvm::ErrorOr<std::string> getCurrentWorkingDirectory() const override {
     return WorkingDirectory;
   }
-  std::error_code setCurrentWorkingDirectory(const Twine &Path) override {
-    WorkingDirectory = Path.str();
-    return std::error_code();
-  }
+  std::error_code setCurrentWorkingDirectory(const Twine &Path) override;
 };
 
 /// \brief Get a globally unique ID for a virtual file or directory.
index cf5a8d681eaccb5571a3d9440d720135f8a63fa7..6977f400287fa78893416fcc61ddeb5128b21993 100644 (file)
@@ -658,6 +658,23 @@ directory_iterator InMemoryFileSystem::dir_begin(const Twine &Dir,
   EC = make_error_code(llvm::errc::not_a_directory);
   return directory_iterator(std::make_shared<InMemoryDirIterator>());
 }
+
+std::error_code InMemoryFileSystem::setCurrentWorkingDirectory(const Twine &P) {
+  SmallString<128> Path;
+  P.toVector(Path);
+
+  // Fix up relative paths. This just prepends the current working directory.
+  std::error_code EC = makeAbsolute(Path);
+  assert(!EC);
+  (void)EC;
+
+  if (useNormalizedPaths())
+    llvm::sys::path::remove_dots(Path, /*remove_dot_dot=*/true);
+
+  if (!Path.empty())
+    WorkingDirectory = Path.str();
+  return std::error_code();
+}
 }
 }
 
index ac07035d3e1f29db01ae3a47aff5a2cffce2860e..b72b757b5ffc4b52203524b0a1e2559c8bfe8d19 100644 (file)
@@ -657,6 +657,12 @@ TEST_F(InMemoryFileSystemTest, WorkingDirectory) {
 
   Stat = FS.status("c");
   ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString();
+
+  NormalizedFS.setCurrentWorkingDirectory("/b/c");
+  NormalizedFS.setCurrentWorkingDirectory(".");
+  ASSERT_EQ("/b/c", NormalizedFS.getCurrentWorkingDirectory().get());
+  NormalizedFS.setCurrentWorkingDirectory("..");
+  ASSERT_EQ("/b", NormalizedFS.getCurrentWorkingDirectory().get());
 }
 
 // NOTE: in the tests below, we use '//root/' as our root directory, since it is