TextDiagnostic.h revision e9a3b76ba589a8a884e978273beaed0d97cf9861
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 21struct SourceColumnMap; 22 23namespace clang { 24 25/// \brief Class to encapsulate the logic for formatting and printing a textual 26/// diagnostic message. 27/// 28/// This class provides an interface for building and emitting a textual 29/// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt 30/// Hints, and code snippets. In the presence of macros this involves 31/// a recursive process, synthesizing notes for each macro expansion. 32/// 33/// The purpose of this class is to isolate the implementation of printing 34/// beautiful text diagnostics from any particular interfaces. The Clang 35/// DiagnosticClient is implemented through this class as is diagnostic 36/// printing coming out of libclang. 37class TextDiagnostic : public DiagnosticRenderer { 38 raw_ostream &OS; 39 40public: 41 TextDiagnostic(raw_ostream &OS, 42 const SourceManager &SM, 43 const LangOptions &LangOpts, 44 const DiagnosticOptions &DiagOpts); 45 46 virtual ~TextDiagnostic(); 47 48 /// \brief Print the diagonstic level to a raw_ostream. 49 /// 50 /// This is a static helper that handles colorizing the level and formatting 51 /// it into an arbitrary output stream. This is used internally by the 52 /// TextDiagnostic emission code, but it can also be used directly by 53 /// consumers that don't have a source manager or other state that the full 54 /// TextDiagnostic logic requires. 55 static void printDiagnosticLevel(raw_ostream &OS, 56 DiagnosticsEngine::Level Level, 57 bool ShowColors); 58 59 /// \brief Pretty-print a diagnostic message to a raw_ostream. 60 /// 61 /// This is a static helper to handle the line wrapping, colorizing, and 62 /// rendering of a diagnostic message to a particular ostream. It is 63 /// publically visible so that clients which do not have sufficient state to 64 /// build a complete TextDiagnostic object can still get consistent 65 /// formatting of their diagnostic messages. 66 /// 67 /// \param OS Where the message is printed 68 /// \param Level Used to colorizing the message 69 /// \param Message The text actually printed 70 /// \param CurrentColumn The starting column of the first line, accounting 71 /// for any prefix. 72 /// \param Columns The number of columns to use in line-wrapping, 0 disables 73 /// all line-wrapping. 74 /// \param ShowColors Enable colorizing of the message. 75 static void printDiagnosticMessage(raw_ostream &OS, 76 DiagnosticsEngine::Level Level, 77 StringRef Message, 78 unsigned CurrentColumn, unsigned Columns, 79 bool ShowColors); 80 81protected: 82 virtual void emitDiagnosticMessage(SourceLocation Loc,PresumedLoc PLoc, 83 DiagnosticsEngine::Level Level, 84 StringRef Message, 85 ArrayRef<CharSourceRange> Ranges, 86 DiagOrStoredDiag D); 87 88 virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, 89 DiagnosticsEngine::Level Level, 90 ArrayRef<CharSourceRange> Ranges); 91 92 virtual void emitCodeContext(SourceLocation Loc, 93 DiagnosticsEngine::Level Level, 94 SmallVectorImpl<CharSourceRange>& Ranges, 95 ArrayRef<FixItHint> Hints) { 96 emitSnippetAndCaret(Loc, Level, Ranges, Hints); 97 } 98 99 virtual void emitBasicNote(StringRef Message); 100 101 virtual void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc); 102 103private: 104 void emitSnippetAndCaret(SourceLocation Loc, DiagnosticsEngine::Level Level, 105 SmallVectorImpl<CharSourceRange>& Ranges, 106 ArrayRef<FixItHint> Hints); 107 108 void emitSnippet(StringRef SourceLine); 109 110 void highlightRange(const CharSourceRange &R, 111 unsigned LineNo, FileID FID, 112 const SourceColumnMap &map, 113 std::string &CaretLine); 114 115 std::string buildFixItInsertionLine(unsigned LineNo, 116 const SourceColumnMap &map, 117 ArrayRef<FixItHint> Hints); 118 void emitParseableFixits(ArrayRef<FixItHint> Hints); 119}; 120 121} // end namespace clang 122 123#endif 124