Implement -working-directory.

When -working-directory is passed in command line, file paths are resolved relative to the specified directory.
This helps both when using libclang (where we can't require the user to actually change the working directory)
and to help reproduce test cases when the reproduction work comes along.

--FileSystemOptions is introduced which controls how file system operations are performed (currently it just contains
 the working directory value if set).
--FileSystemOptions are passed around to various interfaces that perform file operations.
--Opening & reading the content of files should be done only through FileManager. This is useful in general since
 file operations will be abstracted in the future for the reproduction mechanism.

FileSystemOptions is independent of FileManager so that we can have multiple translation units sharing the same
FileManager but with different FileSystemOptions.

Addresses rdar://8583824.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@118203 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index ac57a69..3afe300 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -1977,9 +1977,11 @@
     return 0;
 
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
+  FileSystemOptions FileSystemOpts;
+  FileSystemOpts.WorkingDir = CXXIdx->getWorkingDirectory();
 
   llvm::IntrusiveRefCntPtr<Diagnostic> Diags;
-  return ASTUnit::LoadFromASTFile(ast_filename, Diags,
+  return ASTUnit::LoadFromASTFile(ast_filename, Diags, FileSystemOpts,
                                   CXXIdx->getOnlyLocalDecls(),
                                   0, 0, true);
 }
@@ -2405,7 +2407,8 @@
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(tu);
 
   FileManager &FMgr = CXXUnit->getFileManager();
-  const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name));
+  const FileEntry *File = FMgr.getFile(file_name, file_name+strlen(file_name),
+                                       CXXUnit->getFileSystemOpts());
   return const_cast<FileEntry *>(File);
 }
 
diff --git a/tools/libclang/CIndexCodeCompletion.cpp b/tools/libclang/CIndexCodeCompletion.cpp
index d42e9ea..c92e2af 100644
--- a/tools/libclang/CIndexCodeCompletion.cpp
+++ b/tools/libclang/CIndexCodeCompletion.cpp
@@ -232,12 +232,14 @@
   
   /// \brief Language options used to adjust source locations.
   LangOptions LangOpts;
-
-  /// \brief Source manager, used for diagnostics.
-  SourceManager SourceMgr;
   
   /// \brief File manager, used for diagnostics.
   FileManager FileMgr;
+
+  FileSystemOptions FileSystemOpts;
+
+  /// \brief Source manager, used for diagnostics.
+  SourceManager SourceMgr;
   
   /// \brief Temporary files that should be removed once we have finished
   /// with the code-completion results.
@@ -248,7 +250,8 @@
 };
 
 AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults() 
-  : CXCodeCompleteResults(), Diag(new Diagnostic), SourceMgr(*Diag) { }
+  : CXCodeCompleteResults(), Diag(new Diagnostic),
+    SourceMgr(*Diag, FileMgr, FileSystemOpts) { }
   
 AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
   for (unsigned I = 0, N = NumResults; I != N; ++I)
diff --git a/tools/libclang/CIndexer.h b/tools/libclang/CIndexer.h
index 89b0987..1fcb964 100644
--- a/tools/libclang/CIndexer.h
+++ b/tools/libclang/CIndexer.h
@@ -32,7 +32,8 @@
   bool DisplayDiagnostics;
 
   llvm::sys::Path ResourcesPath;
-  
+  std::string WorkingDir;
+
 public:
  CIndexer() : OnlyLocalDecls(false), DisplayDiagnostics(false) { }
   
@@ -49,6 +50,9 @@
 
   /// \brief Get the path of the clang resource files.
   std::string getClangResourcesPath();
+
+  const std::string &getWorkingDirectory() const { return WorkingDir; }
+  void setWorkingDirectory(const std::string &Dir) { WorkingDir = Dir; }
 };
 
 namespace clang {