1 //===- unittests/Frontend/CompilerInstanceTest.cpp - CI tests -------------===//
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
7 //===----------------------------------------------------------------------===//
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"
19 using namespace clang
;
23 TEST(CompilerInstance
, DefaultVFSOverlayFromInvocation
) {
24 // Create a temporary VFS overlay yaml file.
26 SmallString
<256> FileName
;
27 ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD
, FileName
));
28 ToolOutputFile
File(FileName
, FD
);
30 SmallString
<256> CurrentPath
;
31 sys::fs::current_path(CurrentPath
);
32 sys::fs::make_absolute(CurrentPath
, FileName
);
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"
40 " 'type': 'directory',\n"
42 " { 'name': 'vfs-virtual.file', 'type': 'file',\n"
43 " 'external-contents': '%s'\n"
48 File
.os() << format(VFSYaml
, CurrentPathStr
.c_str(), FileName
.c_str());
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++", "-"};
55 IntrusiveRefCntPtr
<DiagnosticsEngine
> Diags
=
56 CompilerInstance::createDiagnostics(new DiagnosticOptions());
58 CreateInvocationOptions CIOpts
;
60 std::shared_ptr
<CompilerInvocation
> CInvok
=
61 createInvocation(Args
, std::move(CIOpts
));
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();
72 // Check if the virtual file exists which means that our VFS is used by the
74 ASSERT_TRUE(Instance
.getFileManager().getOptionalFileRef("vfs-virtual.file"));
77 TEST(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
= "-";
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 DiagnosticsOS
, new DiagnosticOptions());
89 CompilerInstance Instance
;
90 IntrusiveRefCntPtr
<DiagnosticsEngine
> Diags
= Instance
.createDiagnostics(
91 DiagOpts
, DiagPrinter
.get(), /*ShouldOwnClient=*/false);
93 Diags
->Report(diag::err_expected
) << "no crash";
94 ASSERT_EQ(DiagnosticOutput
, "error: expected no crash\n");
97 } // anonymous namespace