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_TEXTDIAGNOSTIC_H
17 #define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
19 #include "clang/Frontend/DiagnosticRenderer.h"
23 /// \brief Class to encapsulate the logic for formatting and printing a textual
24 /// diagnostic message.
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.
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.
35 class TextDiagnostic : public DiagnosticRenderer {
39 TextDiagnostic(raw_ostream &OS,
40 const LangOptions &LangOpts,
41 DiagnosticOptions *DiagOpts);
43 ~TextDiagnostic() override;
45 /// \brief Print the diagonstic level to a raw_ostream.
47 /// This is a static helper that handles colorizing the level and formatting
48 /// it into an arbitrary output stream. This is used internally by the
49 /// TextDiagnostic emission code, but it can also be used directly by
50 /// consumers that don't have a source manager or other state that the full
51 /// TextDiagnostic logic requires.
52 static void printDiagnosticLevel(raw_ostream &OS,
53 DiagnosticsEngine::Level Level,
55 bool CLFallbackMode = false);
57 /// \brief Pretty-print a diagnostic message to a raw_ostream.
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 /// publicly 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.
65 /// \param OS Where the message is printed
66 /// \param IsSupplemental true if this is a continuation note diagnostic
67 /// \param Message The text actually printed
68 /// \param CurrentColumn The starting column of the first line, accounting
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, bool IsSupplemental,
74 StringRef Message, unsigned CurrentColumn,
75 unsigned Columns, bool ShowColors);
78 void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc,
79 DiagnosticsEngine::Level Level, StringRef Message,
80 ArrayRef<CharSourceRange> Ranges,
81 DiagOrStoredDiag D) override;
83 void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
84 DiagnosticsEngine::Level Level,
85 ArrayRef<CharSourceRange> Ranges) override;
87 void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
88 SmallVectorImpl<CharSourceRange> &Ranges,
89 ArrayRef<FixItHint> Hints) override {
90 emitSnippetAndCaret(Loc, Level, Ranges, Hints);
93 void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
95 void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
96 StringRef ModuleName) override;
98 void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc,
99 StringRef ModuleName) override;
102 void emitFilename(StringRef Filename, const SourceManager &SM);
104 void emitSnippetAndCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
105 SmallVectorImpl<CharSourceRange> &Ranges,
106 ArrayRef<FixItHint> Hints);
108 void emitSnippet(StringRef SourceLine);
110 void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
113 } // end namespace clang