1 //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
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 diagnostic client prints out their diagnostic messages.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/TextDiagnosticPrinter.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Frontend/TextDiagnostic.h"
18 #include "clang/Lex/Lexer.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace clang;
25 TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
26 DiagnosticOptions *diags,
27 bool _OwnsOutputStream)
28 : OS(os), DiagOpts(diags),
29 OwnsOutputStream(_OwnsOutputStream) {
32 TextDiagnosticPrinter::~TextDiagnosticPrinter() {
37 void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
38 const Preprocessor *PP) {
39 // Build the TextDiagnostic utility.
40 TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts));
43 void TextDiagnosticPrinter::EndSourceFile() {
47 /// \brief Print any diagnostic option information to a raw_ostream.
49 /// This implements all of the logic for adding diagnostic options to a message
50 /// (via OS). Each relevant option is comma separated and all are enclosed in
51 /// the standard bracketing: " [...]".
52 static void printDiagnosticOptions(raw_ostream &OS,
53 DiagnosticsEngine::Level Level,
54 const Diagnostic &Info,
55 const DiagnosticOptions &DiagOpts) {
57 if (DiagOpts.ShowOptionNames) {
58 // Handle special cases for non-warnings early.
59 if (Info.getID() == diag::fatal_too_many_errors) {
60 OS << " [-ferror-limit=]";
64 // The code below is somewhat fragile because we are essentially trying to
65 // report to the user what happened by inferring what the diagnostic engine
66 // did. Eventually it might make more sense to have the diagnostic engine
67 // include some "why" information in the diagnostic.
69 // If this is a warning which has been mapped to an error by the user (as
70 // inferred by checking whether the default mapping is to an error) then
71 // flag it as such. Note that diagnostics could also have been mapped by a
72 // pragma, but we don't currently have a way to distinguish this.
73 if (Level == DiagnosticsEngine::Error &&
74 DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) &&
75 !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) {
80 StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
82 OS << (Started ? "," : " [")
83 << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt;
84 StringRef OptValue = Info.getDiags()->getFlagValue();
85 if (!OptValue.empty())
86 OS << "=" << OptValue;
91 // If the user wants to see category information, include it too.
92 if (DiagOpts.ShowCategories) {
93 unsigned DiagCategory =
94 DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
96 OS << (Started ? "," : " [");
98 if (DiagOpts.ShowCategories == 1)
101 assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
102 OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
110 void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
111 const Diagnostic &Info) {
112 // Default implementation (Warnings/errors count).
113 DiagnosticConsumer::HandleDiagnostic(Level, Info);
115 // Render the diagnostic message into a temporary buffer eagerly. We'll use
116 // this later as we print out the diagnostic to the terminal.
117 SmallString<100> OutStr;
118 Info.FormatDiagnostic(OutStr);
120 llvm::raw_svector_ostream DiagMessageStream(OutStr);
121 printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
123 // Keeps track of the starting position of the location
124 // information (e.g., "foo.c:10:4:") that precedes the error
125 // message. We use this information to determine how long the
126 // file+line+column number prefix is.
127 uint64_t StartOfLocationInfo = OS.tell();
130 OS << Prefix << ": ";
132 // Use a dedicated, simpler path for diagnostics without a valid location.
133 // This is important as if the location is missing, we may be emitting
134 // diagnostics in a context that lacks language options, a source manager, or
135 // other infrastructure necessary when emitting more rich diagnostics.
136 if (!Info.getLocation().isValid()) {
137 TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors,
138 DiagOpts->CLFallbackMode);
139 TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(),
140 OS.tell() - StartOfLocationInfo,
141 DiagOpts->MessageLength,
142 DiagOpts->ShowColors);
147 // Assert that the rest of our infrastructure is setup properly.
148 assert(DiagOpts && "Unexpected diagnostic without options set");
149 assert(Info.hasSourceManager() &&
150 "Unexpected diagnostic with no source manager");
151 assert(TextDiag && "Unexpected diagnostic outside source file processing");
153 TextDiag->emitDiagnostic(
154 FullSourceLoc(Info.getLocation(), Info.getSourceManager()), Level,
155 DiagMessageStream.str(), Info.getRanges(), Info.getFixItHints());