• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
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 // This diagnostic client prints out their diagnostic messages.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "flang/Frontend/TextDiagnosticPrinter.h"
14 #include "flang/Frontend/TextDiagnostic.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 using namespace Fortran::frontend;
21 
TextDiagnosticPrinter(raw_ostream & os,clang::DiagnosticOptions * diags)22 TextDiagnosticPrinter::TextDiagnosticPrinter(
23     raw_ostream &os, clang::DiagnosticOptions *diags)
24     : os_(os), diagOpts_(diags) {}
25 
~TextDiagnosticPrinter()26 TextDiagnosticPrinter::~TextDiagnosticPrinter() {}
27 
HandleDiagnostic(clang::DiagnosticsEngine::Level level,const clang::Diagnostic & info)28 void TextDiagnosticPrinter::HandleDiagnostic(
29     clang::DiagnosticsEngine::Level level, const clang::Diagnostic &info) {
30   // Default implementation (Warnings/errors count).
31   DiagnosticConsumer::HandleDiagnostic(level, info);
32 
33   // Render the diagnostic message into a temporary buffer eagerly. We'll use
34   // this later as we print out the diagnostic to the terminal.
35   llvm::SmallString<100> outStr;
36   info.FormatDiagnostic(outStr);
37 
38   llvm::raw_svector_ostream DiagMessageStream(outStr);
39 
40   if (!prefix_.empty())
41     os_ << prefix_ << ": ";
42 
43   // We only emit diagnostics in contexts that lack valid source locations.
44   assert(!info.getLocation().isValid() &&
45       "Diagnostics with valid source location are not supported");
46 
47   Fortran::frontend::TextDiagnostic::PrintDiagnosticLevel(
48       os_, level, diagOpts_->ShowColors);
49   Fortran::frontend::TextDiagnostic::PrintDiagnosticMessage(os_,
50       /*IsSupplemental=*/level == clang::DiagnosticsEngine::Note,
51       DiagMessageStream.str(), diagOpts_->ShowColors);
52 
53   os_.flush();
54   return;
55 }
56