clang API Documentation

TextDiagnostic.h
Go to the documentation of this file.
00001 //===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This is a utility class that provides support for textual pretty-printing of
00011 // diagnostics. It is used to implement the different code paths which require
00012 // such functionality in a consistent way.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
00017 #define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
00018 
00019 #include "clang/Frontend/DiagnosticRenderer.h"
00020 
00021 namespace clang {
00022 
00023 /// \brief Class to encapsulate the logic for formatting and printing a textual
00024 /// diagnostic message.
00025 ///
00026 /// This class provides an interface for building and emitting a textual
00027 /// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
00028 /// Hints, and code snippets. In the presence of macros this involves
00029 /// a recursive process, synthesizing notes for each macro expansion.
00030 ///
00031 /// The purpose of this class is to isolate the implementation of printing
00032 /// beautiful text diagnostics from any particular interfaces. The Clang
00033 /// DiagnosticClient is implemented through this class as is diagnostic
00034 /// printing coming out of libclang.
00035 class TextDiagnostic : public DiagnosticRenderer {
00036   raw_ostream &OS;
00037 
00038 public:
00039   TextDiagnostic(raw_ostream &OS,
00040                  const LangOptions &LangOpts,
00041                  DiagnosticOptions *DiagOpts);
00042 
00043   virtual ~TextDiagnostic();
00044   
00045   /// \brief Print the diagonstic level to a raw_ostream.
00046   ///
00047   /// This is a static helper that handles colorizing the level and formatting
00048   /// it into an arbitrary output stream. This is used internally by the
00049   /// TextDiagnostic emission code, but it can also be used directly by
00050   /// consumers that don't have a source manager or other state that the full
00051   /// TextDiagnostic logic requires.
00052   static void printDiagnosticLevel(raw_ostream &OS,
00053                                    DiagnosticsEngine::Level Level,
00054                                    bool ShowColors,
00055                                    bool CLFallbackMode = false);
00056 
00057   /// \brief Pretty-print a diagnostic message to a raw_ostream.
00058   ///
00059   /// This is a static helper to handle the line wrapping, colorizing, and
00060   /// rendering of a diagnostic message to a particular ostream. It is
00061   /// publicly visible so that clients which do not have sufficient state to
00062   /// build a complete TextDiagnostic object can still get consistent
00063   /// formatting of their diagnostic messages.
00064   ///
00065   /// \param OS Where the message is printed
00066   /// \param IsSupplemental true if this is a continuation note diagnostic
00067   /// \param Message The text actually printed
00068   /// \param CurrentColumn The starting column of the first line, accounting
00069   ///                      for any prefix.
00070   /// \param Columns The number of columns to use in line-wrapping, 0 disables
00071   ///                all line-wrapping.
00072   /// \param ShowColors Enable colorizing of the message.
00073   static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
00074                                      StringRef Message, unsigned CurrentColumn,
00075                                      unsigned Columns, bool ShowColors);
00076 
00077 protected:
00078   void emitDiagnosticMessage(SourceLocation Loc,PresumedLoc PLoc,
00079                              DiagnosticsEngine::Level Level,
00080                              StringRef Message,
00081                              ArrayRef<CharSourceRange> Ranges,
00082                              const SourceManager *SM,
00083                              DiagOrStoredDiag D) override;
00084 
00085   void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
00086                          DiagnosticsEngine::Level Level,
00087                          ArrayRef<CharSourceRange> Ranges,
00088                          const SourceManager &SM) override;
00089 
00090   void emitCodeContext(SourceLocation Loc,
00091                        DiagnosticsEngine::Level Level,
00092                        SmallVectorImpl<CharSourceRange>& Ranges,
00093                        ArrayRef<FixItHint> Hints,
00094                        const SourceManager &SM) override {
00095     emitSnippetAndCaret(Loc, Level, Ranges, Hints, SM);
00096   }
00097 
00098   void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc,
00099                            const SourceManager &SM) override;
00100 
00101   void emitImportLocation(SourceLocation Loc, PresumedLoc PLoc,
00102                           StringRef ModuleName,
00103                           const SourceManager &SM) override;
00104 
00105   void emitBuildingModuleLocation(SourceLocation Loc, PresumedLoc PLoc,
00106                                   StringRef ModuleName,
00107                                   const SourceManager &SM) override;
00108 
00109 private:
00110   void emitSnippetAndCaret(SourceLocation Loc, DiagnosticsEngine::Level Level,
00111                            SmallVectorImpl<CharSourceRange>& Ranges,
00112                            ArrayRef<FixItHint> Hints,
00113                            const SourceManager &SM);
00114 
00115   void emitSnippet(StringRef SourceLine);
00116 
00117   void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
00118 };
00119 
00120 } // end namespace clang
00121 
00122 #endif