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 "flang/Frontend/CompilerInstance.h"
10 #include "gtest/gtest.h"
11 #include "flang/Frontend/CompilerInvocation.h"
12 #include "clang/Basic/DiagnosticOptions.h"
13 #include "clang/Driver/Options.h"
14 #include "clang/Frontend/TextDiagnosticPrinter.h"
15 #include "llvm/Support/raw_ostream.h"
16 
17 #include <filesystem>
18 using namespace llvm;
19 using namespace Fortran::frontend;
20 
21 namespace {
22 
23 TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
24   // 1. Set-up a basic DiagnosticConsumer
25   std::string diagnosticOutput;
26   llvm::raw_string_ostream diagnosticsOS(diagnosticOutput);
27   auto diagPrinter = std::make_unique<clang::TextDiagnosticPrinter>(
28       diagnosticsOS, new clang::DiagnosticOptions());
29 
30   // 2. Create a CompilerInstance (to manage a DiagnosticEngine)
31   CompilerInstance compInst;
32 
33   // 3. Set-up DiagnosticOptions
34   auto diagOpts = new clang::DiagnosticOptions();
35   // Tell the diagnostics engine to emit the diagnostic log to STDERR. This
36   // ensures that a chained diagnostic consumer is created so that the test can
37   // exercise the unowned diagnostic consumer in a chained consumer.
38   diagOpts->DiagnosticLogFile = "-";
39 
40   // 4. Create a DiagnosticEngine with an unowned consumer
41   IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags =
42       compInst.CreateDiagnostics(diagOpts, diagPrinter.get(),
43           /*ShouldOwnClient=*/false);
44 
45   // 5. Report a diagnostic
46   diags->Report(clang::diag::err_expected) << "no crash";
47 
48   // 6. Verify that the reported diagnostic wasn't lost and did end up in the
49   // output stream
50   ASSERT_EQ(diagnosticsOS.str(), "error: expected no crash\n");
51 }
52 } // namespace
53