Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:55

0001 //===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This is a utility class that provides support for textual pretty-printing of
0010 // diagnostics. It is used to implement the different code paths which require
0011 // such functionality in a consistent way.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
0016 #define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
0017 
0018 #include "clang/Frontend/DiagnosticRenderer.h"
0019 #include "llvm/Support/raw_ostream.h"
0020 
0021 namespace clang {
0022 
0023 /// Class to encapsulate the logic for formatting and printing a textual
0024 /// diagnostic message.
0025 ///
0026 /// This class provides an interface for building and emitting a textual
0027 /// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
0028 /// Hints, and code snippets. In the presence of macros this involves
0029 /// a recursive process, synthesizing notes for each macro expansion.
0030 ///
0031 /// The purpose of this class is to isolate the implementation of printing
0032 /// beautiful text diagnostics from any particular interfaces. The Clang
0033 /// DiagnosticClient is implemented through this class as is diagnostic
0034 /// printing coming out of libclang.
0035 class TextDiagnostic : public DiagnosticRenderer {
0036   raw_ostream &OS;
0037   const Preprocessor *PP;
0038 
0039 public:
0040   TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
0041                  DiagnosticOptions *DiagOpts, const Preprocessor *PP = nullptr);
0042 
0043   ~TextDiagnostic() override;
0044 
0045   struct StyleRange {
0046     unsigned Start;
0047     unsigned End;
0048     enum llvm::raw_ostream::Colors Color;
0049     StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C)
0050         : Start(S), End(E), Color(C){};
0051   };
0052 
0053   /// Print the diagonstic level to a raw_ostream.
0054   ///
0055   /// This is a static helper that handles colorizing the level and formatting
0056   /// it into an arbitrary output stream. This is used internally by the
0057   /// TextDiagnostic emission code, but it can also be used directly by
0058   /// consumers that don't have a source manager or other state that the full
0059   /// TextDiagnostic logic requires.
0060   static void printDiagnosticLevel(raw_ostream &OS,
0061                                    DiagnosticsEngine::Level Level,
0062                                    bool ShowColors);
0063 
0064   /// Pretty-print a diagnostic message to a raw_ostream.
0065   ///
0066   /// This is a static helper to handle the line wrapping, colorizing, and
0067   /// rendering of a diagnostic message to a particular ostream. It is
0068   /// publicly visible so that clients which do not have sufficient state to
0069   /// build a complete TextDiagnostic object can still get consistent
0070   /// formatting of their diagnostic messages.
0071   ///
0072   /// \param OS Where the message is printed
0073   /// \param IsSupplemental true if this is a continuation note diagnostic
0074   /// \param Message The text actually printed
0075   /// \param CurrentColumn The starting column of the first line, accounting
0076   ///                      for any prefix.
0077   /// \param Columns The number of columns to use in line-wrapping, 0 disables
0078   ///                all line-wrapping.
0079   /// \param ShowColors Enable colorizing of the message.
0080   static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
0081                                      StringRef Message, unsigned CurrentColumn,
0082                                      unsigned Columns, bool ShowColors);
0083 
0084 protected:
0085   void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
0086                              DiagnosticsEngine::Level Level, StringRef Message,
0087                              ArrayRef<CharSourceRange> Ranges,
0088                              DiagOrStoredDiag D) override;
0089 
0090   void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
0091                          DiagnosticsEngine::Level Level,
0092                          ArrayRef<CharSourceRange> Ranges) override;
0093 
0094   void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
0095                        SmallVectorImpl<CharSourceRange> &Ranges,
0096                        ArrayRef<FixItHint> Hints) override {
0097     emitSnippetAndCaret(Loc, Level, Ranges, Hints);
0098   }
0099 
0100   void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
0101 
0102   void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
0103                           StringRef ModuleName) override;
0104 
0105   void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
0106                                   StringRef ModuleName) override;
0107 
0108 private:
0109   void emitFilename(StringRef Filename, const SourceManager &SM);
0110 
0111   void emitSnippetAndCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
0112                            SmallVectorImpl<CharSourceRange> &Ranges,
0113                            ArrayRef<FixItHint> Hints);
0114 
0115   void emitSnippet(StringRef SourceLine, unsigned MaxLineNoDisplayWidth,
0116                    unsigned LineNo, unsigned DisplayLineNo,
0117                    ArrayRef<StyleRange> Styles);
0118 
0119   void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
0120 };
0121 
0122 } // end namespace clang
0123 
0124 #endif