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

source code of clang/unittests/Frontend/CompilerInstanceTest.cpp