[PATCH] D37416: Use the VFS from the CompilerInvocation by default
This revision was automatically updated to reflect the committed changes. Closed by commit rL313049: Use the VFS from the CompilerInvocation by default (authored by teemperor). Changed prior to commit: https://reviews.llvm.org/D37416?vs=114823=114857#toc Repository: rL LLVM https://reviews.llvm.org/D37416 Files: cfe/trunk/include/clang/Frontend/CompilerInstance.h cfe/trunk/lib/Frontend/CompilerInstance.cpp cfe/trunk/lib/Frontend/FrontendAction.cpp cfe/trunk/unittests/Frontend/CMakeLists.txt cfe/trunk/unittests/Frontend/CompilerInstanceTest.cpp Index: cfe/trunk/lib/Frontend/FrontendAction.cpp === --- cfe/trunk/lib/Frontend/FrontendAction.cpp +++ cfe/trunk/lib/Frontend/FrontendAction.cpp @@ -633,18 +633,12 @@ return true; } - if (!CI.hasVirtualFileSystem()) { -if (IntrusiveRefCntPtr VFS = - createVFSFromCompilerInvocation(CI.getInvocation(), - CI.getDiagnostics())) - CI.setVirtualFileSystem(VFS); -else + // Set up the file and source managers, if needed. + if (!CI.hasFileManager()) { +if (!CI.createFileManager()) { goto failure; +} } - - // Set up the file and source managers, if needed. - if (!CI.hasFileManager()) -CI.createFileManager(); if (!CI.hasSourceManager()) CI.createSourceManager(CI.getFileManager()); Index: cfe/trunk/lib/Frontend/CompilerInstance.cpp === --- cfe/trunk/lib/Frontend/CompilerInstance.cpp +++ cfe/trunk/lib/Frontend/CompilerInstance.cpp @@ -300,12 +300,16 @@ // File Manager -void CompilerInstance::createFileManager() { +FileManager *CompilerInstance::createFileManager() { if (!hasVirtualFileSystem()) { -// TODO: choose the virtual file system based on the CompilerInvocation. -setVirtualFileSystem(vfs::getRealFileSystem()); +if (IntrusiveRefCntPtr VFS = +createVFSFromCompilerInvocation(getInvocation(), getDiagnostics())) + setVirtualFileSystem(VFS); +else + return nullptr; } FileMgr = new FileManager(getFileSystemOpts(), VirtualFileSystem); + return FileMgr.get(); } // Source Manager Index: cfe/trunk/unittests/Frontend/CMakeLists.txt === --- cfe/trunk/unittests/Frontend/CMakeLists.txt +++ cfe/trunk/unittests/Frontend/CMakeLists.txt @@ -4,6 +4,7 @@ add_clang_unittest(FrontendTests ASTUnitTest.cpp + CompilerInstanceTest.cpp FrontendActionTest.cpp CodeGenActionTest.cpp PCHPreambleTest.cpp Index: cfe/trunk/unittests/Frontend/CompilerInstanceTest.cpp === --- cfe/trunk/unittests/Frontend/CompilerInstanceTest.cpp +++ cfe/trunk/unittests/Frontend/CompilerInstanceTest.cpp @@ -0,0 +1,74 @@ +//===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/CompilerInvocation.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/ToolOutputFile.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +namespace { + +TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) { + // Create a temporary VFS overlay yaml file. + int FD; + SmallString<256> FileName; + ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName)); + tool_output_file File(FileName, FD); + + SmallString<256> CurrentPath; + sys::fs::current_path(CurrentPath); + sys::fs::make_absolute(CurrentPath, FileName); + + // Mount the VFS file itself on the path 'virtual.file'. Makes this test + // a bit shorter than creating a new dummy file just for this purpose. + const std::string CurrentPathStr = CurrentPath.str(); + const std::string FileNameStr = FileName.str(); + const char *VFSYaml = "{ 'version': 0, 'roots': [\n" +" { 'name': '%s',\n" +"'type': 'directory',\n" +"'contents': [\n" +" { 'name': 'vfs-virtual.file', 'type': 'file',\n" +"'external-contents': '%s'\n" +" }\n" +"]\n" +" }\n" +"]}\n"; + File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str()); + File.os().flush(); + + // Create a CompilerInvocation that uses this overlay file. + const std::string VFSArg = "-ivfsoverlay" + FileNameStr; + const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"}; + + IntrusiveRefCntPtr
[PATCH] D37416: Use the VFS from the CompilerInvocation by default
teemperor updated this revision to Diff 114823. teemperor added a comment. - Rebased with the correct diff... https://reviews.llvm.org/D37416 Files: include/clang/Frontend/CompilerInstance.h lib/Frontend/CompilerInstance.cpp lib/Frontend/FrontendAction.cpp unittests/Frontend/CMakeLists.txt unittests/Frontend/CompilerInstanceTest.cpp Index: unittests/Frontend/CompilerInstanceTest.cpp === --- /dev/null +++ unittests/Frontend/CompilerInstanceTest.cpp @@ -0,0 +1,74 @@ +//===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/CompilerInvocation.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/ToolOutputFile.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +namespace { + +TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) { + // Create a temporary VFS overlay yaml file. + int FD; + SmallString<256> FileName; + ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName)); + tool_output_file File(FileName, FD); + + SmallString<256> CurrentPath; + sys::fs::current_path(CurrentPath); + sys::fs::make_absolute(CurrentPath, FileName); + + // Mount the VFS file itself on the path 'virtual.file'. Makes this test + // a bit shorter than creating a new dummy file just for this purpose. + const std::string CurrentPathStr = CurrentPath.str(); + const std::string FileNameStr = FileName.str(); + const char *VFSYaml = "{ 'version': 0, 'roots': [\n" +" { 'name': '%s',\n" +"'type': 'directory',\n" +"'contents': [\n" +" { 'name': 'vfs-virtual.file', 'type': 'file',\n" +"'external-contents': '%s'\n" +" }\n" +"]\n" +" }\n" +"]}\n"; + File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str()); + File.os().flush(); + + // Create a CompilerInvocation that uses this overlay file. + const std::string VFSArg = "-ivfsoverlay" + FileNameStr; + const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"}; + + IntrusiveRefCntPtr Diags = + CompilerInstance::createDiagnostics(new DiagnosticOptions()); + + std::shared_ptr CInvok = + createInvocationFromCommandLine(Args, Diags); + + if (!CInvok) +FAIL() << "could not create compiler invocation"; + // Create a minimal CompilerInstance which should use the VFS we specified + // in the CompilerInvocation (as we don't explicitly set our own). + CompilerInstance Instance; + Instance.setDiagnostics(Diags.get()); + Instance.setInvocation(CInvok); + Instance.createFileManager(); + + // Check if the virtual file exists which means that our VFS is used by the + // CompilerInstance. + ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file")); +} + +} // anonymous namespace 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: lib/Frontend/FrontendAction.cpp === --- lib/Frontend/FrontendAction.cpp +++ lib/Frontend/FrontendAction.cpp @@ -633,18 +633,12 @@ return true; } - if (!CI.hasVirtualFileSystem()) { -if (IntrusiveRefCntPtr VFS = - createVFSFromCompilerInvocation(CI.getInvocation(), - CI.getDiagnostics())) - CI.setVirtualFileSystem(VFS); -else + // Set up the file and source managers, if needed. + if (!CI.hasFileManager()) { +if (!CI.createFileManager()) { goto failure; +} } - - // Set up the file and source managers, if needed. - if (!CI.hasFileManager()) -CI.createFileManager(); if (!CI.hasSourceManager()) CI.createSourceManager(CI.getFileManager()); Index: lib/Frontend/CompilerInstance.cpp === --- lib/Frontend/CompilerInstance.cpp +++ lib/Frontend/CompilerInstance.cpp @@ -300,12 +300,16 @@ // File Manager -void CompilerInstance::createFileManager() { +FileManager *CompilerInstance::createFileManager() { if (!hasVirtualFileSystem()) { -// TODO: choose the virtual file system based on the CompilerInvocation. -
[PATCH] D37416: Use the VFS from the CompilerInvocation by default
teemperor updated this revision to Diff 114790. teemperor added a comment. - Rebased diff before merging. https://reviews.llvm.org/D37416 Files: lib/Frontend/CompilerInstance.cpp unittests/Frontend/CMakeLists.txt unittests/Frontend/CompilerInstanceTest.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 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: 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); } ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D37416: Use the VFS from the CompilerInvocation by default
v.g.vassilev accepted this revision. v.g.vassilev added a comment. This revision is now accepted and ready to land. This patch can rely on a post commit review if necessary. https://reviews.llvm.org/D37416 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D37416: Use the VFS from the CompilerInvocation by default
teemperor updated this revision to Diff 114524. teemperor added a comment. - Moved code from FrontendAction.cpp into createFileManager() https://reviews.llvm.org/D37416 Files: include/clang/Frontend/CompilerInstance.h lib/Frontend/CompilerInstance.cpp lib/Frontend/FrontendAction.cpp unittests/Frontend/CMakeLists.txt unittests/Frontend/CompilerInstanceTest.cpp Index: unittests/Frontend/CompilerInstanceTest.cpp === --- /dev/null +++ unittests/Frontend/CompilerInstanceTest.cpp @@ -0,0 +1,74 @@ +//===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/CompilerInvocation.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/ToolOutputFile.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +namespace { + +TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) { + // Create a temporary VFS overlay yaml file. + int FD; + SmallString<256> FileName; + ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName)); + tool_output_file File(FileName, FD); + + SmallString<256> CurrentPath; + sys::fs::current_path(CurrentPath); + sys::fs::make_absolute(CurrentPath, FileName); + + // Mount the VFS file itself on the path 'virtual.file'. Makes this test + // a bit shorter than creating a new dummy file just for this purpose. + const std::string CurrentPathStr = CurrentPath.str(); + const std::string FileNameStr = FileName.str(); + const char *VFSYaml = "{ 'version': 0, 'roots': [\n" +" { 'name': '%s',\n" +"'type': 'directory',\n" +"'contents': [\n" +" { 'name': 'vfs-virtual.file', 'type': 'file',\n" +"'external-contents': '%s'\n" +" }\n" +"]\n" +" }\n" +"]}\n"; + File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str()); + File.os().flush(); + + // Create a CompilerInvocation that uses this overlay file. + const std::string VFSArg = "-ivfsoverlay" + FileNameStr; + const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"}; + + IntrusiveRefCntPtr Diags = + CompilerInstance::createDiagnostics(new DiagnosticOptions()); + + std::shared_ptr CInvok = + createInvocationFromCommandLine(Args, Diags); + + if (!CInvok) +FAIL() << "could not create compiler invocation"; + // Create a minimal CompilerInstance which should use the VFS we specified + // in the CompilerInvocation (as we don't explicitly set our own). + CompilerInstance Instance; + Instance.setDiagnostics(Diags.get()); + Instance.setInvocation(CInvok); + Instance.createFileManager(); + + // Check if the virtual file exists which means that our VFS is used by the + // CompilerInstance. + ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file")); +} + +} // anonymous namespace 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 ) Index: lib/Frontend/FrontendAction.cpp === --- lib/Frontend/FrontendAction.cpp +++ lib/Frontend/FrontendAction.cpp @@ -633,18 +633,12 @@ return true; } - if (!CI.hasVirtualFileSystem()) { -if (IntrusiveRefCntPtr VFS = - createVFSFromCompilerInvocation(CI.getInvocation(), - CI.getDiagnostics())) - CI.setVirtualFileSystem(VFS); -else + // Set up the file and source managers, if needed. + if (!CI.hasFileManager()) { +if (!CI.createFileManager()) { goto failure; +} } - - // Set up the file and source managers, if needed. - if (!CI.hasFileManager()) -CI.createFileManager(); if (!CI.hasSourceManager()) CI.createSourceManager(CI.getFileManager()); Index: lib/Frontend/CompilerInstance.cpp === --- lib/Frontend/CompilerInstance.cpp +++ lib/Frontend/CompilerInstance.cpp @@ -300,12 +300,16 @@ // File Manager -void CompilerInstance::createFileManager() { +FileManager *CompilerInstance::createFileManager() { if (!hasVirtualFileSystem()) { -// TODO: choose the virtual file system based on the
[PATCH] D37416: Use the VFS from the CompilerInvocation by default
teemperor updated this revision to Diff 114504. teemperor added a comment. Herald added a subscriber: mgorny. - Added test case. https://reviews.llvm.org/D37416 Files: lib/Frontend/CompilerInstance.cpp unittests/Frontend/CMakeLists.txt unittests/Frontend/CompilerInstanceTest.cpp Index: unittests/Frontend/CompilerInstanceTest.cpp === --- /dev/null +++ unittests/Frontend/CompilerInstanceTest.cpp @@ -0,0 +1,74 @@ +//===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===--===// + +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/CompilerInvocation.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/ToolOutputFile.h" +#include "gtest/gtest.h" + +using namespace llvm; +using namespace clang; + +namespace { + +TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) { + // Create a temporary VFS overlay yaml file. + int FD; + SmallString<256> FileName; + ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName)); + tool_output_file File(FileName, FD); + + SmallString<256> CurrentPath; + sys::fs::current_path(CurrentPath); + sys::fs::make_absolute(CurrentPath, FileName); + + // Mount the VFS file itself on the path 'virtual.file'. Makes this test + // a bit shorter than creating a new dummy file just for this purpose. + const std::string CurrentPathStr = CurrentPath.str(); + const std::string FileNameStr = FileName.str(); + const char *VFSYaml = "{ 'version': 0, 'roots': [\n" +" { 'name': '%s',\n" +"'type': 'directory',\n" +"'contents': [\n" +" { 'name': 'vfs-virtual.file', 'type': 'file',\n" +"'external-contents': '%s'\n" +" }\n" +"]\n" +" }\n" +"]}\n"; + File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str()); + File.os().flush(); + + // Create a CompilerInvocation that uses this overlay file. + const std::string VFSArg = "-ivfsoverlay" + FileNameStr; + const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"}; + + IntrusiveRefCntPtr Diags = + CompilerInstance::createDiagnostics(new DiagnosticOptions()); + + std::shared_ptr CInvok = + createInvocationFromCommandLine(Args, Diags); + + if (!CInvok) +FAIL() << "could not create compiler invocation"; + // Create a minimal CompilerInstance which should use the VFS we specified + // in the CompilerInvocation (as we don't explicitly set our own). + CompilerInstance Instance; + Instance.setDiagnostics(Diags.get()); + Instance.setInvocation(CInvok); + Instance.createFileManager(); + + // Check if the virtual file exists which means that our VFS is used by the + // CompilerInstance. + ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file")); +} + +} // anonymous namespace 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 ) 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); } ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D37416: Use the VFS from the CompilerInvocation by default
v.g.vassilev added a comment. You should probably update the code creating the vfs before the call to `createFileManager` in `lib/Frontend/FrontendAction.cpp`. Repository: rL LLVM https://reviews.llvm.org/D37416 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits