llvm-project
97 строк · 3.8 Кб
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.
25int FD;
26SmallString<256> FileName;
27ASSERT_FALSE(sys::fs::createTemporaryFile("vfs", "yaml", FD, FileName));
28ToolOutputFile File(FileName, FD);
29
30SmallString<256> CurrentPath;
31sys::fs::current_path(CurrentPath);
32sys::fs::make_absolute(CurrentPath, 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.
36const std::string CurrentPathStr = std::string(CurrentPath.str());
37const std::string FileNameStr = std::string(FileName.str());
38const 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";
48File.os() << format(VFSYaml, CurrentPathStr.c_str(), FileName.c_str());
49File.os().flush();
50
51// Create a CompilerInvocation that uses this overlay file.
52const std::string VFSArg = "-ivfsoverlay" + FileNameStr;
53const char *Args[] = {"clang", VFSArg.c_str(), "-xc++", "-"};
54
55IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
56CompilerInstance::createDiagnostics(new DiagnosticOptions());
57
58CreateInvocationOptions CIOpts;
59CIOpts.Diags = Diags;
60std::shared_ptr<CompilerInvocation> CInvok =
61createInvocation(Args, std::move(CIOpts));
62
63if (!CInvok)
64FAIL() << "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).
67CompilerInstance Instance;
68Instance.setDiagnostics(Diags.get());
69Instance.setInvocation(CInvok);
70Instance.createFileManager();
71
72// Check if the virtual file exists which means that our VFS is used by the
73// CompilerInstance.
74ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file"));
75}
76
77TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
78auto 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.
82DiagOpts->DiagnosticLogFile = "-";
83
84// Create the diagnostic engine with unowned consumer.
85std::string DiagnosticOutput;
86llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput);
87auto DiagPrinter = std::make_unique<TextDiagnosticPrinter>(
88DiagnosticsOS, new DiagnosticOptions());
89CompilerInstance Instance;
90IntrusiveRefCntPtr<DiagnosticsEngine> Diags = Instance.createDiagnostics(
91DiagOpts, DiagPrinter.get(), /*ShouldOwnClient=*/false);
92
93Diags->Report(diag::err_expected) << "no crash";
94ASSERT_EQ(DiagnosticOutput, "error: expected no crash\n");
95}
96
97} // anonymous namespace
98