1*abc3d04eSRaphael Isemann //===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -------------===// 2*abc3d04eSRaphael Isemann // 3*abc3d04eSRaphael Isemann // The LLVM Compiler Infrastructure 4*abc3d04eSRaphael Isemann // 5*abc3d04eSRaphael Isemann // This file is distributed under the University of Illinois Open Source 6*abc3d04eSRaphael Isemann // License. See LICENSE.TXT for details. 7*abc3d04eSRaphael Isemann // 8*abc3d04eSRaphael Isemann //===----------------------------------------------------------------------===// 9*abc3d04eSRaphael Isemann 10*abc3d04eSRaphael Isemann #include "clang/Frontend/CompilerInstance.h" 11*abc3d04eSRaphael Isemann #include "clang/Frontend/CompilerInvocation.h" 12*abc3d04eSRaphael Isemann #include "llvm/Support/FileSystem.h" 13*abc3d04eSRaphael Isemann #include "llvm/Support/Format.h" 14*abc3d04eSRaphael Isemann #include "llvm/Support/ToolOutputFile.h" 15*abc3d04eSRaphael Isemann #include "gtest/gtest.h" 16*abc3d04eSRaphael Isemann 17*abc3d04eSRaphael Isemann using namespace llvm; 18*abc3d04eSRaphael Isemann using namespace clang; 19*abc3d04eSRaphael Isemann 20*abc3d04eSRaphael Isemann namespace { 21*abc3d04eSRaphael Isemann 22*abc3d04eSRaphael Isemann TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) { 23*abc3d04eSRaphael Isemann // Create a temporary VFS overlay yaml file. 24*abc3d04eSRaphael Isemann int FD; 25*abc3d04eSRaphael Isemann SmallString<256> FileName; 26*abc3d04eSRaphael Isemann ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName)); 27*abc3d04eSRaphael Isemann tool_output_file File(FileName, FD); 28*abc3d04eSRaphael Isemann 29*abc3d04eSRaphael Isemann SmallString<256> CurrentPath; 30*abc3d04eSRaphael Isemann sys::fs::current_path(CurrentPath); 31*abc3d04eSRaphael Isemann sys::fs::make_absolute(CurrentPath, FileName); 32*abc3d04eSRaphael Isemann 33*abc3d04eSRaphael Isemann // Mount the VFS file itself on the path 'virtual.file'. Makes this test 34*abc3d04eSRaphael Isemann // a bit shorter than creating a new dummy file just for this purpose. 35*abc3d04eSRaphael Isemann const std::string CurrentPathStr = CurrentPath.str(); 36*abc3d04eSRaphael Isemann const std::string FileNameStr = FileName.str(); 37*abc3d04eSRaphael Isemann const char *VFSYaml = "{ 'version': 0, 'roots': [\n" 38*abc3d04eSRaphael Isemann " { 'name': '%s',\n" 39*abc3d04eSRaphael Isemann " 'type': 'directory',\n" 40*abc3d04eSRaphael Isemann " 'contents': [\n" 41*abc3d04eSRaphael Isemann " { 'name': 'vfs-virtual.file', 'type': 'file',\n" 42*abc3d04eSRaphael Isemann " 'external-contents': '%s'\n" 43*abc3d04eSRaphael Isemann " }\n" 44*abc3d04eSRaphael Isemann " ]\n" 45*abc3d04eSRaphael Isemann " }\n" 46*abc3d04eSRaphael Isemann "]}\n"; 47*abc3d04eSRaphael Isemann File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str()); 48*abc3d04eSRaphael Isemann File.os().flush(); 49*abc3d04eSRaphael Isemann 50*abc3d04eSRaphael Isemann // Create a CompilerInvocation that uses this overlay file. 51*abc3d04eSRaphael Isemann const std::string VFSArg = "-ivfsoverlay" + FileNameStr; 52*abc3d04eSRaphael Isemann const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"}; 53*abc3d04eSRaphael Isemann 54*abc3d04eSRaphael Isemann IntrusiveRefCntPtr<DiagnosticsEngine> Diags = 55*abc3d04eSRaphael Isemann CompilerInstance::createDiagnostics(new DiagnosticOptions()); 56*abc3d04eSRaphael Isemann 57*abc3d04eSRaphael Isemann std::shared_ptr<CompilerInvocation> CInvok = 58*abc3d04eSRaphael Isemann createInvocationFromCommandLine(Args, Diags); 59*abc3d04eSRaphael Isemann 60*abc3d04eSRaphael Isemann if (!CInvok) 61*abc3d04eSRaphael Isemann FAIL() << "could not create compiler invocation"; 62*abc3d04eSRaphael Isemann // Create a minimal CompilerInstance which should use the VFS we specified 63*abc3d04eSRaphael Isemann // in the CompilerInvocation (as we don't explicitly set our own). 64*abc3d04eSRaphael Isemann CompilerInstance Instance; 65*abc3d04eSRaphael Isemann Instance.setDiagnostics(Diags.get()); 66*abc3d04eSRaphael Isemann Instance.setInvocation(CInvok); 67*abc3d04eSRaphael Isemann Instance.createFileManager(); 68*abc3d04eSRaphael Isemann 69*abc3d04eSRaphael Isemann // Check if the virtual file exists which means that our VFS is used by the 70*abc3d04eSRaphael Isemann // CompilerInstance. 71*abc3d04eSRaphael Isemann ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file")); 72*abc3d04eSRaphael Isemann } 73*abc3d04eSRaphael Isemann 74*abc3d04eSRaphael Isemann } // anonymous namespace 75