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 LangOptions &LangOpts, 43 const DiagnosticOptions &DiagOpts); 44 45 virtual ~TextDiagnostic(); 46 47 /// \brief Print the diagonstic level to a raw_ostream. 48 /// 49 /// This is a static helper that handles colorizing the level and formatting 50 /// it into an arbitrary output stream. This is used internally by the 51 /// TextDiagnostic emission code, but it can also be used directly by 52 /// consumers that don't have a source manager or other state that the full 53 /// TextDiagnostic logic requires. 54 static void printDiagnosticLevel(raw_ostream &OS, 55 DiagnosticsEngine::Level Level, 56 bool ShowColors); 57 58 /// \brief Pretty-print a diagnostic message to a raw_ostream. 59 /// 60 /// This is a static helper to handle the line wrapping, colorizing, and 61 /// rendering of a diagnostic message to a particular ostream. It is 62 /// publicly visible so that clients which do not have sufficient state to 63 /// build a complete TextDiagnostic object can still get consistent 64 /// formatting of their diagnostic messages. 65 /// 66 /// \param OS Where the message is printed 67 /// \param Level Used to colorizing the message 68 /// \param Message The text actually printed 69 /// \param CurrentColumn The starting column of the first line, accounting 70 /// for any prefix. 71 /// \param Columns The number of columns to use in line-wrapping, 0 disables 72 /// all line-wrapping. 73 /// \param ShowColors Enable colorizing of the message. 74 static void printDiagnosticMessage(raw_ostream &OS, 75 DiagnosticsEngine::Level Level, 76 StringRef Message, 77 unsigned CurrentColumn, unsigned Columns, 78 bool ShowColors); 79 80protected: 81 virtual void emitDiagnosticMessage(SourceLocation Loc,PresumedLoc PLoc, 82 DiagnosticsEngine::Level Level, 83 StringRef Message, 84 ArrayRef<CharSourceRange> Ranges, 85 const SourceManager *SM, 86 DiagOrStoredDiag D); 87 88 virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, 89 DiagnosticsEngine::Level Level, 90 ArrayRef<CharSourceRange> Ranges, 91 const SourceManager &SM); 92 93 virtual void emitCodeContext(SourceLocation Loc, 94 DiagnosticsEngine::Level Level, 95 SmallVectorImpl<CharSourceRange>& Ranges, 96 ArrayRef<FixItHint> Hints, 97 const SourceManager &SM) { 98 emitSnippetAndCaret(Loc, Level, Ranges, Hints, SM); 99 } 100 101 virtual void emitBasicNote(StringRef Message); 102 103 virtual void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc, 104 const SourceManager &SM); 105 106private: 107 void emitSnippetAndCaret(SourceLocation Loc, DiagnosticsEngine::Level Level, 108 SmallVectorImpl<CharSourceRange>& Ranges, 109 ArrayRef<FixItHint> Hints, 110 const SourceManager &SM); 111 112 void emitSnippet(StringRef SourceLine); 113 114 void highlightRange(const CharSourceRange &R, 115 unsigned LineNo, FileID FID, 116 const SourceColumnMap &map, 117 std::string &CaretLine, 118 const SourceManager &SM); 119 120 std::string buildFixItInsertionLine(unsigned LineNo, 121 const SourceColumnMap &map, 122 ArrayRef<FixItHint> Hints, 123 const SourceManager &SM); 124 void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM); 125}; 126 127} // end namespace clang 128 129#endif 130