1 //===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- 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 // A utility class that provides support for textual pretty-printing of
10 // diagnostics. Based on clang::TextDiagnostic (this is a trimmed version).
11 //
12 // TODO: If expanding, consider sharing the implementation with Clang.
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_FLANG_FRONTEND_TEXTDIAGNOSTIC_H
16 #define LLVM_FLANG_FRONTEND_TEXTDIAGNOSTIC_H
17 
18 #include "clang/Basic/Diagnostic.h"
19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
20 
21 namespace Fortran::frontend {
22 
23 /// Class to encapsulate the logic for formatting and printing a textual
24 /// diagnostic message.
25 ///
26 /// The purpose of this class is to isolate the implementation of printing
27 /// beautiful text diagnostics from any particular interfaces. Currently only
28 /// simple diagnostics that lack source location information are supported (e.g.
29 /// Flang driver errors).
30 ///
31 /// In the future we can extend this class (akin to Clang) to support more
32 /// complex diagnostics that would include macro backtraces, caret diagnostics,
33 /// FixIt Hints and code snippets.
34 ///
35 class TextDiagnostic {
36 public:
37   TextDiagnostic();
38 
39   ~TextDiagnostic();
40 
41   /// Print the diagnostic level to a llvm::raw_ostream.
42   ///
43   /// This is a static helper that handles colorizing the level and formatting
44   /// it into an arbitrary output stream.
45   ///
46   /// \param os Where the message is printed
47   /// \param level The diagnostic level (e.g. error or warning)
48   /// \param showColors Enable colorizing of the message.
49   static void PrintDiagnosticLevel(llvm::raw_ostream &os,
50       clang::DiagnosticsEngine::Level level, bool showColors);
51 
52   /// Pretty-print a diagnostic message to a llvm::raw_ostream.
53   ///
54   /// This is a static helper to handle the colorizing and rendering diagnostic
55   /// message to a particular ostream. In the future we can
56   /// extend it to support e.g. line wrapping. It is
57   /// publicly visible as at this stage we don't require any state data to
58   /// print a diagnostic.
59   ///
60   /// \param os Where the message is printed
61   /// \param isSupplemental true if this is a continuation note diagnostic
62   /// \param message The text actually printed
63   /// \param showColors Enable colorizing of the message.
64   static void PrintDiagnosticMessage(llvm::raw_ostream &os, bool isSupplemental,
65       llvm::StringRef message, bool showColors);
66 };
67 
68 } // namespace Fortran::frontend
69 
70 #endif
71