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