| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "clang/Frontend/CompilerInstance.h" |
| 10 | #include "clang/Frontend/CompilerInvocation.h" |
| 11 | #include "llvm/Support/FileSystem.h" |
| 12 | #include "llvm/Support/Format.h" |
| 13 | #include "llvm/Support/ToolOutputFile.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace clang; |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) { |
| 22 | |
| 23 | int FD; |
| 24 | SmallString<256> FileName; |
| 25 | ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName)); |
| 26 | ToolOutputFile File(FileName, FD); |
| 27 | |
| 28 | SmallString<256> CurrentPath; |
| 29 | sys::fs::current_path(CurrentPath); |
| 30 | sys::fs::make_absolute(CurrentPath, FileName); |
| 31 | |
| 32 | |
| 33 | |
| 34 | const std::string CurrentPathStr = CurrentPath.str(); |
| 35 | const std::string FileNameStr = FileName.str(); |
| 36 | const char *VFSYaml = "{ 'version': 0, 'roots': [\n" |
| 37 | " { 'name': '%s',\n" |
| 38 | " 'type': 'directory',\n" |
| 39 | " 'contents': [\n" |
| 40 | " { 'name': 'vfs-virtual.file', 'type': 'file',\n" |
| 41 | " 'external-contents': '%s'\n" |
| 42 | " }\n" |
| 43 | " ]\n" |
| 44 | " }\n" |
| 45 | "]}\n"; |
| 46 | File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str()); |
| 47 | File.os().flush(); |
| 48 | |
| 49 | |
| 50 | const std::string VFSArg = "-ivfsoverlay" + FileNameStr; |
| 51 | const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"}; |
| 52 | |
| 53 | IntrusiveRefCntPtr<DiagnosticsEngine> Diags = |
| 54 | CompilerInstance::createDiagnostics(new DiagnosticOptions()); |
| 55 | |
| 56 | std::shared_ptr<CompilerInvocation> CInvok = |
| 57 | createInvocationFromCommandLine(Args, Diags); |
| 58 | |
| 59 | if (!CInvok) |
| 60 | FAIL() << "could not create compiler invocation"; |
| 61 | |
| 62 | |
| 63 | CompilerInstance Instance; |
| 64 | Instance.setDiagnostics(Diags.get()); |
| 65 | Instance.setInvocation(CInvok); |
| 66 | Instance.createFileManager(); |
| 67 | |
| 68 | |
| 69 | |
| 70 | ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file")); |
| 71 | } |
| 72 | |
| 73 | } |
| 74 | |