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