Index: lib/Frontend/CompilerInstance.cpp =================================================================== --- lib/Frontend/CompilerInstance.cpp +++ lib/Frontend/CompilerInstance.cpp @@ -302,8 +302,8 @@ void CompilerInstance::createFileManager() { if (!hasVirtualFileSystem()) { - // TODO: choose the virtual file system based on the CompilerInvocation. - setVirtualFileSystem(vfs::getRealFileSystem()); + setVirtualFileSystem( + createVFSFromCompilerInvocation(getInvocation(), getDiagnostics())); } FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem); } Index: unittests/Frontend/CMakeLists.txt =================================================================== --- unittests/Frontend/CMakeLists.txt +++ unittests/Frontend/CMakeLists.txt @@ -4,6 +4,7 @@ add_clang_unittest(FrontendTests ASTUnitTest.cpp + CompilerInstanceTest.cpp FrontendActionTest.cpp CodeGenActionTest.cpp PCHPreambleTest.cpp Index: unittests/Frontend/CompilerInstanceTest.cpp =================================================================== --- /dev/null +++ unittests/Frontend/CompilerInstanceTest.cpp @@ -0,0 +1,82 @@ +//===- unittests/Frontend/CompilerInstanceTest.cpp - ASTUnit tests --------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include + +#include "clang/Frontend/ASTUnit.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/CompilerInvocation.h" +#include "clang/Frontend/PCHContainerOperations.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/ToolOutputFile.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +namespace { + +TEST(CompilerInstance, VFSOverlay) { + llvm::SmallString<256> CurrentPath; + llvm::sys::fs::current_path(CurrentPath); + + int VFSFD; + llvm::SmallString<256> VFSFileName; + ASSERT_FALSE( + llvm::sys::fs::createTemporaryFile("vfs", "yaml", VFSFD, VFSFileName)); + tool_output_file VFSFile(VFSFileName, VFSFD); + + llvm::sys::fs::make_absolute(CurrentPath, VFSFileName); + + VFSFile.os() << "{ 'version': 0, 'roots': [\n" + " { 'name': '" + << CurrentPath + << "',\n" + " 'type': 'directory',\n" + " 'contents': [\n" + " { 'name': 'virtual.file', 'type': 'file',\n" + " 'external-contents': '" + << VFSFileName << "'\n" + " }\n" + " ]\n" + " }\n" + "]}\n"; + VFSFile.os().flush(); + + int FD; + llvm::SmallString<256> InputFileName; + ASSERT_FALSE( + llvm::sys::fs::createTemporaryFile("vfs", "cpp", FD, InputFileName)); + tool_output_file input_file(InputFileName, FD); + input_file.os() << ""; + + std::string VFSArg = "-ivfsoverlay" + VFSFileName.str().str(); + + const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", + InputFileName.c_str()}; + + IntrusiveRefCntPtr Diags = + CompilerInstance::createDiagnostics(new DiagnosticOptions()); + + std::shared_ptr CInvok = + createInvocationFromCommandLine(Args, Diags); + + if (!CInvok) + FAIL() << "could not create compiler invocation"; + CompilerInstance Instance; + Instance.setDiagnostics(Diags.get()); + Instance.setInvocation(CInvok); + Instance.createFileManager(); + + ASSERT_TRUE(Instance.getFileManager().getFile("virtual.file")); + ASSERT_FALSE(Instance.getFileManager().getFile("virtual.file2")); +} + +} // anonymous namespace