1 //===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_
17 #define LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_
19 #include "clang/Frontend/DiagnosticRenderer.h"
21 struct SourceColumnMap;
25 /// \brief Class to encapsulate the logic for formatting and printing a textual
26 /// diagnostic message.
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.
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.
37 class TextDiagnostic : public DiagnosticRenderer {
41 TextDiagnostic(raw_ostream &OS,
42 const LangOptions &LangOpts,
43 DiagnosticOptions *DiagOpts);
45 virtual ~TextDiagnostic();
47 /// \brief Print the diagonstic level to a raw_ostream.
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,
58 /// \brief Pretty-print a diagnostic message to a raw_ostream.
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.
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
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,
77 unsigned CurrentColumn, unsigned Columns,
81 virtual void emitDiagnosticMessage(SourceLocation Loc,PresumedLoc PLoc,
82 DiagnosticsEngine::Level Level,
84 ArrayRef<CharSourceRange> Ranges,
85 const SourceManager *SM,
88 virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
89 DiagnosticsEngine::Level Level,
90 ArrayRef<CharSourceRange> Ranges,
91 const SourceManager &SM);
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);
101 virtual void emitBasicNote(StringRef Message);
103 virtual void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc,
104 const SourceManager &SM);
107 void emitSnippetAndCaret(SourceLocation Loc, DiagnosticsEngine::Level Level,
108 SmallVectorImpl<CharSourceRange>& Ranges,
109 ArrayRef<FixItHint> Hints,
110 const SourceManager &SM);
112 void emitSnippet(StringRef SourceLine);
114 void highlightRange(const CharSourceRange &R,
115 unsigned LineNo, FileID FID,
116 const SourceColumnMap &map,
117 std::string &CaretLine,
118 const SourceManager &SM);
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);
127 } // end namespace clang