clang  19.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/raw_ostream.h"
20 
21 namespace clang {
22 
23 /// 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.
36  raw_ostream &OS;
37  const Preprocessor *PP;
38 
39 public:
40  TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
41  DiagnosticOptions *DiagOpts, const Preprocessor *PP = nullptr);
42 
43  ~TextDiagnostic() override;
44 
45  struct StyleRange {
46  unsigned Start;
47  unsigned End;
48  enum llvm::raw_ostream::Colors Color;
49  StyleRange(unsigned S, unsigned E, enum llvm::raw_ostream::Colors C)
50  : Start(S), End(E), Color(C){};
51  };
52 
53  /// Print the diagonstic level to a raw_ostream.
54  ///
55  /// This is a static helper that handles colorizing the level and formatting
56  /// it into an arbitrary output stream. This is used internally by the
57  /// TextDiagnostic emission code, but it can also be used directly by
58  /// consumers that don't have a source manager or other state that the full
59  /// TextDiagnostic logic requires.
60  static void printDiagnosticLevel(raw_ostream &OS,
62  bool ShowColors);
63 
64  /// Pretty-print a diagnostic message to a raw_ostream.
65  ///
66  /// This is a static helper to handle the line wrapping, colorizing, and
67  /// rendering of a diagnostic message to a particular ostream. It is
68  /// publicly visible so that clients which do not have sufficient state to
69  /// build a complete TextDiagnostic object can still get consistent
70  /// formatting of their diagnostic messages.
71  ///
72  /// \param OS Where the message is printed
73  /// \param IsSupplemental true if this is a continuation note diagnostic
74  /// \param Message The text actually printed
75  /// \param CurrentColumn The starting column of the first line, accounting
76  /// for any prefix.
77  /// \param Columns The number of columns to use in line-wrapping, 0 disables
78  /// all line-wrapping.
79  /// \param ShowColors Enable colorizing of the message.
80  static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental,
81  StringRef Message, unsigned CurrentColumn,
82  unsigned Columns, bool ShowColors);
83 
84 protected:
86  DiagnosticsEngine::Level Level, StringRef Message,
88  DiagOrStoredDiag D) override;
89 
92  ArrayRef<CharSourceRange> Ranges) override;
93 
96  ArrayRef<FixItHint> Hints) override {
97  emitSnippetAndCaret(Loc, Level, Ranges, Hints);
98  }
99 
100  void emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) override;
101 
103  StringRef ModuleName) override;
104 
106  StringRef ModuleName) override;
107 
108 private:
109  void emitFilename(StringRef Filename, const SourceManager &SM);
110 
111  void emitSnippetAndCaret(FullSourceLoc Loc, DiagnosticsEngine::Level Level,
113  ArrayRef<FixItHint> Hints);
114 
115  void emitSnippet(StringRef SourceLine, unsigned MaxLineNoDisplayWidth,
116  unsigned LineNo, unsigned DisplayLineNo,
117  ArrayRef<StyleRange> Styles);
118 
119  void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM);
120 };
121 
122 } // end namespace clang
123 
124 #endif
#define SM(sm)
Definition: Cuda.cpp:83
StringRef Filename
Definition: Format.cpp:2976
bool ShowColors
Definition: Logger.cpp:29
SourceLocation Loc
Definition: SemaObjC.cpp:755
Options for controlling the compiler diagnostics engine.
Class to encapsulate the logic for formatting a diagnostic message.
const LangOptions & LangOpts
IntrusiveRefCntPtr< DiagnosticOptions > DiagOpts
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:196
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...
Definition: LangOptions.h:482
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
Represents an unpacked "presumed" location which can be presented to the user.
This class handles loading and caching of source files into memory.
Class to encapsulate the logic for formatting and printing a textual diagnostic message.
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
static void printDiagnosticLevel(raw_ostream &OS, DiagnosticsEngine::Level Level, bool ShowColors)
Print the diagonstic level to a raw_ostream.
TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts, DiagnosticOptions *DiagOpts, const Preprocessor *PP=nullptr)
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)