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