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 18 using namespace llvm; 19 using namespace clang; 20 21 namespace { 22 23 TEST(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(CurrentPath); 32 sys::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. 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(VFSYaml, CurrentPathStr.c_str(), 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(new DiagnosticOptions()); 57 58 std::shared_ptr<CompilerInvocation> CInvok = 59 createInvocationFromCommandLine(Args, Diags); 60 61 if (!CInvok) 62 FAIL() << "could not create compiler invocation"; 63 // Create a minimal CompilerInstance which should use the VFS we specified 64 // in the CompilerInvocation (as we don't explicitly set our own). 65 CompilerInstance Instance; 66 Instance.setDiagnostics(Diags.get()); 67 Instance.setInvocation(CInvok); 68 Instance.createFileManager(); 69 70 // Check if the virtual file exists which means that our VFS is used by the 71 // CompilerInstance. 72 ASSERT_TRUE(Instance.getFileManager().getFile("vfs-virtual.file")); 73 } 74 75 TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) { 76 auto DiagOpts = new DiagnosticOptions(); 77 // Tell the diagnostics engine to emit the diagnostic log to STDERR. This 78 // ensures that a chained diagnostic consumer is created so that the test can 79 // exercise the unowned diagnostic consumer in a chained consumer. 80 DiagOpts->DiagnosticLogFile = "-"; 81 82 // Create the diagnostic engine with unowned consumer. 83 std::string DiagnosticOutput; 84 llvm::raw_string_ostream DiagnosticsOS(DiagnosticOutput); 85 auto DiagPrinter = std::make_unique<TextDiagnosticPrinter>( 86 DiagnosticsOS, new DiagnosticOptions()); 87 CompilerInstance Instance; 88 IntrusiveRefCntPtr<DiagnosticsEngine> Diags = Instance.createDiagnostics( 89 DiagOpts, DiagPrinter.get(), /*ShouldOwnClient=*/false); 90 91 Diags->Report(diag::err_expected) << "no crash"; 92 ASSERT_EQ(DiagnosticsOS.str(), "error: expected no crash\n"); 93 } 94 95 } // anonymous namespace 96