1 //===--- CompilerInstance.cpp ---------------------------------------------===//
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/CompilerInvocation.h"
11 #include "clang/Frontend/TextDiagnosticPrinter.h"
12 #include "llvm/Support/raw_ostream.h"
13 
14 using namespace Fortran::frontend;
15 
16 CompilerInstance::CompilerInstance() : invocation_(new CompilerInvocation()) {}
17 
18 CompilerInstance::~CompilerInstance() = default;
19 
20 void CompilerInstance::CreateDiagnostics(
21     clang::DiagnosticConsumer *client, bool shouldOwnClient) {
22   diagnostics_ =
23       CreateDiagnostics(&GetDiagnosticOpts(), client, shouldOwnClient);
24 }
25 
26 clang::IntrusiveRefCntPtr<clang::DiagnosticsEngine>
27 CompilerInstance::CreateDiagnostics(clang::DiagnosticOptions *opts,
28     clang::DiagnosticConsumer *client, bool shouldOwnClient) {
29   clang::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagID(
30       new clang::DiagnosticIDs());
31   clang::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags(
32       new clang::DiagnosticsEngine(diagID, opts));
33 
34   // Create the diagnostic client for reporting errors or for
35   // implementing -verify.
36   if (client) {
37     diags->setClient(client, shouldOwnClient);
38   } else {
39     diags->setClient(new clang::TextDiagnosticPrinter(llvm::errs(), opts));
40   }
41   return diags;
42 }
43