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