Home | History | Annotate | Download | only in Frontend
      1 //===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This is a utility class that provides support for textual pretty-printing of
     11 // diagnostics. It is used to implement the different code paths which require
     12 // such functionality in a consistent way.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_
     17 #define LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_
     18 
     19 #include "clang/Frontend/DiagnosticRenderer.h"
     20 
     21 namespace clang {
     22 
     23 /// \brief Class to encapsulate the logic for formatting and printing a textual
     24 /// diagnostic message.
     25 ///
     26 /// This class provides an interface for building and emitting a textual
     27 /// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
     28 /// Hints, and code snippets. In the presence of macros this involves
     29 /// a recursive process, synthesizing notes for each macro expansion.
     30 ///
     31 /// The purpose of this class is to isolate the implementation of printing
     32 /// beautiful text diagnostics from any particular interfaces. The Clang
     33 /// DiagnosticClient is implemented through this class as is diagnostic
     34 /// printing coming out of libclang.
     35 class TextDiagnostic : public DiagnosticRenderer {
     36   raw_ostream &OS;
     37 
     38 public:
     39   TextDiagnostic(raw_ostream &OS,
     40                  const LangOptions &LangOpts,
     41                  DiagnosticOptions *DiagOpts);
     42 
     43   virtual ~TextDiagnostic();
     44 
     45   /// \brief Print the diagonstic level to a raw_ostream.
     46   ///
     47   /// This is a static helper that handles colorizing the level and formatting
     48   /// it into an arbitrary output stream. This is used internally by the
     49   /// TextDiagnostic emission code, but it can also be used directly by
     50   /// consumers that don't have a source manager or other state that the full
     51   /// TextDiagnostic logic requires.
     52   static void printDiagnosticLevel(raw_ostream &OS,
     53                                    DiagnosticsEngine::Level Level,
     54                                    bool ShowColors);
     55 
     56   /// \brief Pretty-print a diagnostic message to a raw_ostream.
     57   ///
     58   /// This is a static helper to handle the line wrapping, colorizing, and
     59   /// rendering of a diagnostic message to a particular ostream. It is
     60   /// publicly visible so that clients which do not have sufficient state to
     61   /// build a complete TextDiagnostic object can still get consistent
     62   /// formatting of their diagnostic messages.
     63   ///
     64   /// \param OS Where the message is printed
     65   /// \param Level Used to colorizing the message
     66   /// \param Message The text actually printed
     67   /// \param CurrentColumn The starting column of the first line, accounting
     68   ///                      for any prefix.
     69   /// \param Columns The number of columns to use in line-wrapping, 0 disables
     70   ///                all line-wrapping.
     71   /// \param ShowColors Enable colorizing of the message.
     72   static void printDiagnosticMessage(raw_ostream &OS,
     73                                      DiagnosticsEngine::Level Level,
     74                                      StringRef Message,
     75                                      unsigned CurrentColumn, unsigned Columns,
     76                                      bool ShowColors);
     77 
     78 protected:
     79   virtual void emitDiagnosticMessage(SourceLocation Loc,PresumedLoc PLoc,
     80                                      DiagnosticsEngine::Level Level,
     81                                      StringRef Message,
     82                                      ArrayRef<CharSourceRange> Ranges,
     83                                      const SourceManager *SM,
     84                                      DiagOrStoredDiag D);
     85 
     86   virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
     87                                  DiagnosticsEngine::Level Level,
     88                                  ArrayRef<CharSourceRange> Ranges,
     89                                  const SourceManager &SM);
     90 
     91   virtual void emitCodeContext(SourceLocation Loc,
     92                                DiagnosticsEngine::Level Level,
     93                                SmallVectorImpl<CharSourceRange>& Ranges,
     94                                ArrayRef<FixItHint> Hints,
     95                                const SourceManager &SM) {
     96     emitSnippetAndCaret(Loc, Level, Ranges, Hints, SM);
     97   }
     98 
     99   virtual void emitBasicNote(StringRef Message);
    100 
    101   virtual void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc,
    102                                    const SourceManager &SM);
    103 
    104   virtual void emitImportLocation(SourceLocation Loc, PresumedLoc PLoc,
    105                                   StringRef ModuleName,
    106                                   const SourceManager &SM);
    107 
    108   virtual void emitBuildingModuleLocation(SourceLocation Loc, PresumedLoc PLoc,
    109                                           StringRef ModuleName,
    110                                           const SourceManager &SM);
    111 
    112 private:
    113   void emitSnippetAndCaret(SourceLocation Loc, DiagnosticsEngine::Level Level,
    114                            SmallVectorImpl<CharSourceRange>& Ranges,
    115                            ArrayRef<FixItHint> Hints,
    116                            const SourceManager &SM);
    117 
    118   void emitSnippet(StringRef SourceLine);
    119 
    120   void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
    121 };
    122 
    123 } // end namespace clang
    124 
    125 #endif
    126