xref: /llvm-project/flang/include/flang/Frontend/TextDiagnosticPrinter.h (revision 12da8ef0e318cf1e05c1380de7b98bc5cfa51f42)
1 //===--- TextDiagnosticPrinter.h - Text Diagnostic Client -------*- C++ -*-===//
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 is a concrete diagnostic client. In terminals that support it, the
10 // diagnostics are pretty-printed (colors + bold). The printing/flushing
11 // happens in HandleDiagnostics (usually called at the point when the
12 // diagnostic is generated).
13 //
14 //===----------------------------------------------------------------------===//
15 //
16 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #ifndef FORTRAN_FRONTEND_TEXTDIAGNOSTICPRINTER_H
21 #define FORTRAN_FRONTEND_TEXTDIAGNOSTICPRINTER_H
22 
23 #include "clang/Basic/Diagnostic.h"
24 #include "llvm/ADT/IntrusiveRefCntPtr.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 namespace clang {
28 class DiagnosticOptions;
29 class DiagnosticsEngine;
30 } // namespace clang
31 
32 using llvm::IntrusiveRefCntPtr;
33 using llvm::raw_ostream;
34 
35 namespace Fortran::frontend {
36 class TextDiagnostic;
37 
38 class TextDiagnosticPrinter : public clang::DiagnosticConsumer {
39   raw_ostream &os;
40   llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagOpts;
41 
42   /// A string to prefix to error messages.
43   std::string prefix;
44 
45 public:
46   TextDiagnosticPrinter(raw_ostream &os, clang::DiagnosticOptions *diags);
47   ~TextDiagnosticPrinter() override;
48 
49   /// Set the diagnostic printer prefix string, which will be printed at the
50   /// start of any diagnostics. If empty, no prefix string is used.
setPrefix(std::string value)51   void setPrefix(std::string value) { prefix = std::move(value); }
52 
53   void HandleDiagnostic(clang::DiagnosticsEngine::Level level,
54                         const clang::Diagnostic &info) override;
55 
56   void printLocForRemarks(llvm::raw_svector_ostream &diagMessageStream,
57                           llvm::StringRef &diagMsg);
58 };
59 
60 } // namespace Fortran::frontend
61 
62 #endif
63