Clang Project

clang_source_code/unittests/Frontend/CompilerInstanceTest.cpp
1//===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
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
16using namespace llvm;
17using namespace clang;
18
19namespace {
20
21TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) {
22  // Create a temporary VFS overlay yaml file.
23  int FD;
24  SmallString<256FileName;
25  ASSERT_FALSE(sys::fs::createTemporaryFile("vfs""yaml", FD, FileName));
26  ToolOutputFile File(FileName, FD);
27
28  SmallString<256CurrentPath;
29  sys::fs::current_path(CurrentPath);
30  sys::fs::make_absolute(CurrentPath, FileName);
31
32  // Mount the VFS file itself on the path 'virtual.file'. Makes this test
33  // a bit shorter than creating a new dummy file just for this purpose.
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  // Create a CompilerInvocation that uses this overlay file.
50  const std::string VFSArg = "-ivfsoverlay" + FileNameStr;
51  const char *Args[] = {"clang"VFSArg.c_str(), "-xc++""-"};
52
53  IntrusiveRefCntPtr<DiagnosticsEngineDiags =
54      CompilerInstance::createDiagnostics(new DiagnosticOptions());
55
56  std::shared_ptr<CompilerInvocationCInvok =
57      createInvocationFromCommandLine(Args, Diags);
58
59  if (!CInvok)
60    FAIL() << "could not create compiler invocation";
61  // Create a minimal CompilerInstance which should use the VFS we specified
62  // in the CompilerInvocation (as we don't explicitly set our own).
63  CompilerInstance Instance;
64  Instance.setDiagnostics(Diags.get());
65  Instance.setInvocation(CInvok);
66  Instance.createFileManager();
67
68  // Check if the virtual file exists which means that our VFS is used by the
69  // CompilerInstance.
70  ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file"));
71}
72
73// anonymous namespace
74