xref: /llvm-project/clang/unittests/Frontend/CompilerInstanceTest.cpp (revision edea75d6f42e4db9383260a8d592e6cf1569d622)
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 "clang/Frontend/TextDiagnosticPrinter.h"
12 #include "llvm/Support/FileSystem.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/ToolOutputFile.h"
15 #include "gtest/gtest.h"
16 
17 using namespace llvm;
18 using namespace clang;
19 
20 namespace {
21 
22 TEST(CompilerInstance, DefaultVFSOverlayFromInvocation) {
23   // Create a temporary VFS overlay yaml file.
24   int FD;
25   SmallString<256> FileName;
26   ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName));
27   ToolOutputFile File(FileName, FD);
28 
29   SmallString<256> CurrentPath;
30   sys::fs::current_path(CurrentPath);
31   sys::fs::make_absolute(CurrentPath, FileName);
32 
33   // Mount the VFS file itself on the path 'virtual.file'. Makes this test
34   // a bit shorter than creating a new dummy file just for this purpose.
35   const std::string CurrentPathStr = CurrentPath.str();
36   const std::string FileNameStr = FileName.str();
37   const char *VFSYaml = "{ 'version': 0, 'roots': [\n"
38                         "  { 'name': '%s',\n"
39                         "    'type': 'directory',\n"
40                         "    'contents': [\n"
41                         "      { 'name': 'vfs-virtual.file', 'type': 'file',\n"
42                         "        'external-contents': '%s'\n"
43                         "      }\n"
44                         "    ]\n"
45                         "  }\n"
46                         "]}\n";
47   File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str());
48   File.os().flush();
49 
50   // Create a CompilerInvocation that uses this overlay file.
51   const std::string VFSArg = "-ivfsoverlay" + FileNameStr;
52   const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"};
53 
54   IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
55       CompilerInstance::createDiagnostics(new DiagnosticOptions());
56 
57   std::shared_ptr<CompilerInvocation> CInvok =
58       createInvocationFromCommandLine(Args, Diags);
59 
60   if (!CInvok)
61     FAIL() << "could not create compiler invocation";
62   // Create a minimal CompilerInstance which should use the VFS we specified
63   // in the CompilerInvocation (as we don't explicitly set our own).
64   CompilerInstance Instance;
65   Instance.setDiagnostics(Diags.get());
66   Instance.setInvocation(CInvok);
67   Instance.createFileManager();
68 
69   // Check if the virtual file exists which means that our VFS is used by the
70   // CompilerInstance.
71   ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file"));
72 }
73 
74 TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
75   auto DiagOpts = new DiagnosticOptions();
76 
77   // Create the diagnostic engine with unowned consumer.
78   std::string DiagnosticOutput;
79   llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput);
80   auto DiagPrinter = llvm::make_unique<TextDiagnosticPrinter>(
81       DiagnosticsOS, new DiagnosticOptions());
82   CompilerInstance Instance;
83   IntrusiveRefCntPtr<DiagnosticsEngine> Diags = Instance.createDiagnostics(
84       DiagOpts, DiagPrinter.get(), /*ShouldOwnClient=*/false);
85 
86   Diags->Report(diag::err_expected) << "no crash";
87   ASSERT_EQ(DiagnosticsOS.str(), "error: expected no crash\n");
88 }
89 
90 } // anonymous namespace
91