clang 22.0.0git
TextDiagnostic.h
Go to the documentation of this file.
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
19#include "llvm/Support/FormattedStream.h"
20
21namespace clang {
22
23using llvm::formatted_raw_ostream;
24
25/// Class to encapsulate the logic for formatting and printing a textual
26/// diagnostic message.
27///
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.
32///
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.
38 formatted_raw_ostream OS;
39 const Preprocessor *PP;
40
41public:
42 TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
43 DiagnosticOptions &DiagOpts, const Preprocessor *PP = nullptr);
44
45 ~TextDiagnostic() override;
46
47 struct StyleRange {
48 unsigned Start;
49 unsigned End;
50 enum llvm::raw_ostream::Colors Color;
51 StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C)
52 : Start(S), End(E), Color(C) {};
53 };
54
55 /// Print the diagonstic level to a raw_ostream.
56 ///
57 /// This is a static helper that handles colorizing the level and formatting
58 /// it into an arbitrary output stream. This is used internally by the
59 /// TextDiagnostic emission code, but it can also be used directly by
60 /// consumers that don't have a source manager or other state that the full
61 /// TextDiagnostic logic requires.
62 static void printDiagnosticLevel(raw_ostream &OS,
64 bool ShowColors);
65
66 /// Pretty-print a diagnostic message to a raw_ostream.
67 ///
68 /// This is a static helper to handle the line wrapping, colorizing, and
69 /// rendering of a diagnostic message to a particular ostream. It is
70 /// publicly visible so that clients which do not have sufficient state to
71 /// build a complete TextDiagnostic object can still get consistent
72 /// formatting of their diagnostic messages.
73 ///
74 /// \param OS Where the message is printed
75 /// \param IsSupplemental true if this is a continuation note diagnostic
76 /// \param Message The text actually printed
77 /// \param CurrentColumn The starting column of the first line, accounting
78 /// for any prefix.
79 /// \param Columns The number of columns to use in line-wrapping, 0 disables
80 /// all line-wrapping.
81 /// \param ShowColors Enable colorizing of the message.
82 static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
83 StringRef Message, unsigned CurrentColumn,
84 unsigned Columns, bool ShowColors);
85
86protected:
88 DiagnosticsEngine::Level Level, StringRef Message,
90 DiagOrStoredDiag D) override;
91
94 ArrayRef<CharSourceRange> Ranges) override;
95
98 ArrayRef<FixItHint> Hints) override {
99 emitSnippetAndCaret(Loc, Level, Ranges, Hints);
100 }
101
102 void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
103
105 StringRef ModuleName) override;
106
108 StringRef ModuleName) override;
109
110private:
111 void emitFilename(StringRef Filename, const SourceManager &SM);
112
113 void emitSnippetAndCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
115 ArrayRef<FixItHint> Hints);
116
117 void emitSnippet(StringRef SourceLine, unsigned MaxLineNoDisplayWidth,
118 unsigned LineNo, unsigned DisplayLineNo,
119 ArrayRef<StyleRange> Styles);
120
121 void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
122};
123
124} // end namespace clang
125
126#endif
#define SM(sm)
Options for controlling the compiler diagnostics engine.
const LangOptions & LangOpts
DiagnosticOptions & DiagOpts
DiagnosticRenderer(const LangOptions &LangOpts, DiagnosticOptions &DiagOpts)
Level
The level of the diagnostic, after it has been through mapping.
Definition Diagnostic.h:237
A SourceLocation and its associated SourceManager.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Represents an unpacked "presumed" location which can be presented to the user.
This class handles loading and caching of source files into memory.
void emitCodeContext(FullSourceLoc Loc, DiagnosticsEngine::Level Level, SmallVectorImpl< CharSourceRange > &Ranges, ArrayRef< FixItHint > Hints) override
static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental, StringRef Message, unsigned CurrentColumn, unsigned Columns, bool ShowColors)
Pretty-print a diagnostic message to a raw_ostream.
void emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override
TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, DiagnosticOptions &DiagOpts, const Preprocessor *PP=nullptr)
static void printDiagnosticLevel(raw_ostream &OS, DiagnosticsEngine::Level Level, bool ShowColors)
Print the diagonstic level to a raw_ostream.
void emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, ArrayRef< CharSourceRange > Ranges) override
Print out the file/line/column information and include trace.
void emitDiagnosticMessage(FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, StringRef Message, ArrayRef< CharSourceRange > Ranges, DiagOrStoredDiag D) override
void emitBuildingModuleLocation(FullSourceLoc Loc, PresumedLoc PLoc, StringRef ModuleName) override
The JSON file list parser is used to communicate input to InstallAPI.
llvm::PointerUnion< const Diagnostic *, const StoredDiagnostic * > DiagOrStoredDiag
enum llvm::raw_ostream::Colors Color
StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C)