Clang Project

clang_source_code/include/clang/Frontend/TextDiagnostic.h
1//===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This is a utility class that provides support for textual pretty-printing of
10// diagnostics. It is used to implement the different code paths which require
11// such functionality in a consistent way.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
16#define LLVM_CLANG_FRONTEND_TEXTDIAGNOSTIC_H
17
18#include "clang/Frontend/DiagnosticRenderer.h"
19
20namespace clang {
21
22/// Class to encapsulate the logic for formatting and printing a textual
23/// diagnostic message.
24///
25/// This class provides an interface for building and emitting a textual
26/// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
27/// Hints, and code snippets. In the presence of macros this involves
28/// a recursive process, synthesizing notes for each macro expansion.
29///
30/// The purpose of this class is to isolate the implementation of printing
31/// beautiful text diagnostics from any particular interfaces. The Clang
32/// DiagnosticClient is implemented through this class as is diagnostic
33/// printing coming out of libclang.
34class TextDiagnostic : public DiagnosticRenderer {
35  raw_ostream &OS;
36
37public:
38  TextDiagnostic(raw_ostream &OS,
39                 const LangOptions &LangOpts,
40                 DiagnosticOptions *DiagOpts);
41
42  ~TextDiagnostic() override;
43
44  /// Print the diagonstic level to a raw_ostream.
45  ///
46  /// This is a static helper that handles colorizing the level and formatting
47  /// it into an arbitrary output stream. This is used internally by the
48  /// TextDiagnostic emission code, but it can also be used directly by
49  /// consumers that don't have a source manager or other state that the full
50  /// TextDiagnostic logic requires.
51  static void printDiagnosticLevel(raw_ostream &OS,
52                                   DiagnosticsEngine::Level Level,
53                                   bool ShowColors,
54                                   bool CLFallbackMode = false);
55
56  /// Pretty-print a diagnostic message to a raw_ostream.
57  ///
58  /// This is a static helper to handle the line wrapping, colorizing, and
59  /// rendering of a diagnostic message to a particular ostream. It is
60  /// publicly visible so that clients which do not have sufficient state to
61  /// build a complete TextDiagnostic object can still get consistent
62  /// formatting of their diagnostic messages.
63  ///
64  /// \param OS Where the message is printed
65  /// \param IsSupplemental true if this is a continuation note diagnostic
66  /// \param Message The text actually printed
67  /// \param CurrentColumn The starting column of the first line, accounting
68  ///                      for any prefix.
69  /// \param Columns The number of columns to use in line-wrapping, 0 disables
70  ///                all line-wrapping.
71  /// \param ShowColors Enable colorizing of the message.
72  static void printDiagnosticMessage(raw_ostream &OSbool IsSupplemental,
73                                     StringRef Messageunsigned CurrentColumn,
74                                     unsigned Columnsbool ShowColors);
75
76protected:
77  void emitDiagnosticMessage(FullSourceLoc LocPresumedLoc PLoc,
78                             DiagnosticsEngine::Level LevelStringRef Message,
79                             ArrayRef<CharSourceRangeRanges,
80                             DiagOrStoredDiag D) override;
81
82  void emitDiagnosticLoc(FullSourceLoc LocPresumedLoc PLoc,
83                         DiagnosticsEngine::Level Level,
84                         ArrayRef<CharSourceRangeRanges) override;
85
86  void emitCodeContext(FullSourceLoc LocDiagnosticsEngine::Level Level,
87                       SmallVectorImpl<CharSourceRange> &Ranges,
88                       ArrayRef<FixItHintHints) override {
89    emitSnippetAndCaret(Loc, Level, Ranges, Hints);
90  }
91
92  void emitIncludeLocation(FullSourceLoc LocPresumedLoc PLoc) override;
93
94  void emitImportLocation(FullSourceLoc LocPresumedLoc PLoc,
95                          StringRef ModuleName) override;
96
97  void emitBuildingModuleLocation(FullSourceLoc LocPresumedLoc PLoc,
98                                  StringRef ModuleName) override;
99
100private:
101  void emitFilename(StringRef Filenameconst SourceManager &SM);
102
103  void emitSnippetAndCaret(FullSourceLoc LocDiagnosticsEngine::Level Level,
104                           SmallVectorImpl<CharSourceRange> &Ranges,
105                           ArrayRef<FixItHintHints);
106
107  void emitSnippet(StringRef SourceLine);
108
109  void emitParseableFixits(ArrayRef<FixItHintHintsconst SourceManager &SM);
110};
111
112// end namespace clang
113
114#endif
115
clang::TextDiagnostic::OS
clang::TextDiagnostic::printDiagnosticLevel
clang::TextDiagnostic::printDiagnosticMessage
clang::TextDiagnostic::emitDiagnosticMessage
clang::TextDiagnostic::emitDiagnosticLoc
clang::TextDiagnostic::emitCodeContext
clang::TextDiagnostic::emitIncludeLocation
clang::TextDiagnostic::emitImportLocation
clang::TextDiagnostic::emitBuildingModuleLocation
clang::TextDiagnostic::emitFilename
clang::TextDiagnostic::emitSnippetAndCaret
clang::TextDiagnostic::emitSnippet
clang::TextDiagnostic::emitParseableFixits