1db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===//
2db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//
3db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//                     The LLVM Compiler Infrastructure
4db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//
5db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// This file is distributed under the University of Illinois Open Source
6db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// License. See LICENSE.TXT for details.
7db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//
8db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//===----------------------------------------------------------------------===//
9db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//
10db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// This is a utility class that provides support for textual pretty-printing of
11db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// diagnostics. It is used to implement the different code paths which require
12db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// such functionality in a consistent way.
13db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//
14db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//===----------------------------------------------------------------------===//
15db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
16db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth#ifndef LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_
17db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth#define LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_
18db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
192898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek#include "clang/Frontend/DiagnosticRenderer.h"
20db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
21db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruthnamespace clang {
22db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
23db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// \brief Class to encapsulate the logic for formatting and printing a textual
24db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// diagnostic message.
25db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth///
26db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// This class provides an interface for building and emitting a textual
27db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
28db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// Hints, and code snippets. In the presence of macros this involves
29db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// a recursive process, synthesizing notes for each macro expansion.
30db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth///
31db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// The purpose of this class is to isolate the implementation of printing
32db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// beautiful text diagnostics from any particular interfaces. The Clang
33db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// DiagnosticClient is implemented through this class as is diagnostic
34db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// printing coming out of libclang.
352898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenekclass TextDiagnostic : public DiagnosticRenderer {
36db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  raw_ostream &OS;
37db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
38db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruthpublic:
39db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  TextDiagnostic(raw_ostream &OS,
40db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth                 const LangOptions &LangOpts,
4102c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor                 DiagnosticOptions *DiagOpts);
42db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
432898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek  virtual ~TextDiagnostic();
442898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek
45db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \brief Print the diagonstic level to a raw_ostream.
46db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  ///
47db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// This is a static helper that handles colorizing the level and formatting
48db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// it into an arbitrary output stream. This is used internally by the
49db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// TextDiagnostic emission code, but it can also be used directly by
50db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// consumers that don't have a source manager or other state that the full
51db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// TextDiagnostic logic requires.
52db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  static void printDiagnosticLevel(raw_ostream &OS,
53db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth                                   DiagnosticsEngine::Level Level,
54db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth                                   bool ShowColors);
55db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
56db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \brief Pretty-print a diagnostic message to a raw_ostream.
57db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  ///
58db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// This is a static helper to handle the line wrapping, colorizing, and
59db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// rendering of a diagnostic message to a particular ostream. It is
6048d798ce32447607144db70a484cdb99c1180663Benjamin Kramer  /// publicly visible so that clients which do not have sufficient state to
61db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// build a complete TextDiagnostic object can still get consistent
62db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// formatting of their diagnostic messages.
63db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  ///
64db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \param OS Where the message is printed
65db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \param Level Used to colorizing the message
66db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \param Message The text actually printed
67db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \param CurrentColumn The starting column of the first line, accounting
68db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  ///                      for any prefix.
69db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \param Columns The number of columns to use in line-wrapping, 0 disables
70db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  ///                all line-wrapping.
71db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  /// \param ShowColors Enable colorizing of the message.
72db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth  static void printDiagnosticMessage(raw_ostream &OS,
73db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth                                     DiagnosticsEngine::Level Level,
74db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth                                     StringRef Message,
75db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth                                     unsigned CurrentColumn, unsigned Columns,
76db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth                                     bool ShowColors);
77db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
782898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenekprotected:
792898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek  virtual void emitDiagnosticMessage(SourceLocation Loc,PresumedLoc PLoc,
802898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek                                     DiagnosticsEngine::Level Level,
812898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek                                     StringRef Message,
822898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek                                     ArrayRef<CharSourceRange> Ranges,
8316afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                                     const SourceManager *SM,
848be51eab5ad34515d2a40dcdc8558128ca1800adTed Kremenek                                     DiagOrStoredDiag D);
852898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek
862898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek  virtual void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
872898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek                                 DiagnosticsEngine::Level Level,
8816afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                                 ArrayRef<CharSourceRange> Ranges,
8916afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                                 const SourceManager &SM);
902898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek
912898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek  virtual void emitCodeContext(SourceLocation Loc,
922898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek                               DiagnosticsEngine::Level Level,
932898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek                               SmallVectorImpl<CharSourceRange>& Ranges,
9416afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                               ArrayRef<FixItHint> Hints,
9516afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                               const SourceManager &SM) {
9616afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis    emitSnippetAndCaret(Loc, Level, Ranges, Hints, SM);
972898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek  }
982898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek
992898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek  virtual void emitBasicNote(StringRef Message);
1002898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek
10116afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis  virtual void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc,
10216afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                                   const SourceManager &SM);
1032898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek
1046c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor  virtual void emitImportLocation(SourceLocation Loc, PresumedLoc PLoc,
1056c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor                                  StringRef ModuleName,
1066c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor                                  const SourceManager &SM);
1076c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor
108830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor  virtual void emitBuildingModuleLocation(SourceLocation Loc, PresumedLoc PLoc,
109830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor                                          StringRef ModuleName,
110830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor                                          const SourceManager &SM);
111830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor
112db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruthprivate:
1134ba55659982bf45eedc9d32851815e5c4b6d8460Chandler Carruth  void emitSnippetAndCaret(SourceLocation Loc, DiagnosticsEngine::Level Level,
1146ddd8871c07ab161742e8ed37d3ef121ac79fa59Chandler Carruth                           SmallVectorImpl<CharSourceRange>& Ranges,
11516afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                           ArrayRef<FixItHint> Hints,
11616afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis                           const SourceManager &SM);
1176ddd8871c07ab161742e8ed37d3ef121ac79fa59Chandler Carruth
1186749dd50869281f9b49ad5960e25288c15a71cacSeth Cantrell  void emitSnippet(StringRef SourceLine);
1196749dd50869281f9b49ad5960e25288c15a71cacSeth Cantrell
12016afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis  void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
121db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth};
122db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
123db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth} // end namespace clang
124db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth
125db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth#endif
126