blob: 1d34abfd421c8537a831adf8a5bf73ccc2a2d292 [file] [log] [blame]
Bill Wendling37b1dde2007-06-07 09:34:54 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling37b1dde2007-06-07 09:34:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar51adf582009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticPrinter.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000015#include "clang/Basic/DiagnosticOptions.h"
Axel Naumann63fbaed2011-01-27 10:55:51 +000016#include "clang/Basic/FileManager.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000017#include "clang/Basic/SourceManager.h"
Chandler Carrutha3028852011-10-15 23:43:53 +000018#include "clang/Frontend/TextDiagnostic.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000019#include "clang/Lex/Lexer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc45529b2009-05-05 22:03:18 +000022#include "llvm/Support/MemoryBuffer.h"
Chris Lattner327984f2008-11-19 06:56:25 +000023#include "llvm/Support/raw_ostream.h"
Douglas Gregor87f95b02009-02-26 21:00:50 +000024#include <algorithm>
Bill Wendling37b1dde2007-06-07 09:34:54 +000025using namespace clang;
26
Chris Lattner0e62c1c2011-07-23 10:55:15 +000027TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
Douglas Gregor811db4e2012-10-23 22:26:28 +000028 DiagnosticOptions *diags,
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000029 bool _OwnsOutputStream)
Douglas Gregor811db4e2012-10-23 22:26:28 +000030 : OS(os), DiagOpts(diags),
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000031 OwnsOutputStream(_OwnsOutputStream) {
32}
33
34TextDiagnosticPrinter::~TextDiagnosticPrinter() {
35 if (OwnsOutputStream)
36 delete &OS;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000037}
38
Chandler Carruth3eb8b542011-10-16 02:57:39 +000039void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
40 const Preprocessor *PP) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +000041 // Build the TextDiagnostic utility.
Douglas Gregor811db4e2012-10-23 22:26:28 +000042 TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts));
Chandler Carruth3eb8b542011-10-16 02:57:39 +000043}
44
45void TextDiagnosticPrinter::EndSourceFile() {
Craig Topper49a27902014-05-22 04:46:25 +000046 TextDiag.reset(nullptr);
Chandler Carruth3eb8b542011-10-16 02:57:39 +000047}
48
Chandler Carruthcf259a42011-09-26 01:30:09 +000049/// \brief Print any diagnostic option information to a raw_ostream.
50///
51/// This implements all of the logic for adding diagnostic options to a message
52/// (via OS). Each relevant option is comma separated and all are enclosed in
53/// the standard bracketing: " [...]".
54static void printDiagnosticOptions(raw_ostream &OS,
Chandler Carruth60740842011-09-26 00:44:09 +000055 DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +000056 const Diagnostic &Info,
Chandler Carruth60740842011-09-26 00:44:09 +000057 const DiagnosticOptions &DiagOpts) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000058 bool Started = false;
Chandler Carruth60740842011-09-26 00:44:09 +000059 if (DiagOpts.ShowOptionNames) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000060 // Handle special cases for non-warnings early.
61 if (Info.getID() == diag::fatal_too_many_errors) {
62 OS << " [-ferror-limit=]";
63 return;
64 }
65
Daniel Dunbaraa111382011-09-29 01:01:08 +000066 // The code below is somewhat fragile because we are essentially trying to
67 // report to the user what happened by inferring what the diagnostic engine
68 // did. Eventually it might make more sense to have the diagnostic engine
69 // include some "why" information in the diagnostic.
70
71 // If this is a warning which has been mapped to an error by the user (as
72 // inferred by checking whether the default mapping is to an error) then
73 // flag it as such. Note that diagnostics could also have been mapped by a
74 // pragma, but we don't currently have a way to distinguish this.
Chandler Carruth60740842011-09-26 00:44:09 +000075 if (Level == DiagnosticsEngine::Error &&
Daniel Dunbaraa111382011-09-29 01:01:08 +000076 DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) &&
77 !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) {
78 OS << " [-Werror";
79 Started = true;
Chandler Carruth0059a9b2011-09-26 01:21:58 +000080 }
81
Chandler Carruth60740842011-09-26 00:44:09 +000082 StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
83 if (!Opt.empty()) {
Diego Novillo829b1702014-04-16 16:54:24 +000084 OS << (Started ? "," : " [")
Alp Toker2724ec32014-06-22 10:08:06 +000085 << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt;
Diego Novillo9f239972014-04-21 23:16:03 +000086 StringRef OptValue = Info.getDiags()->getFlagNameValue();
87 if (!OptValue.empty())
88 OS << "=" << OptValue;
Chandler Carruth0059a9b2011-09-26 01:21:58 +000089 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +000090 }
91 }
Chandler Carruth60740842011-09-26 00:44:09 +000092
Chandler Carruth0059a9b2011-09-26 01:21:58 +000093 // If the user wants to see category information, include it too.
94 if (DiagOpts.ShowCategories) {
95 unsigned DiagCategory =
96 DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
Chandler Carruth60740842011-09-26 00:44:09 +000097 if (DiagCategory) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000098 OS << (Started ? "," : " [");
Benjamin Kramere2125d82011-09-26 02:14:13 +000099 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +0000100 if (DiagOpts.ShowCategories == 1)
Benjamin Kramere2125d82011-09-26 02:14:13 +0000101 OS << DiagCategory;
Chandler Carruth60740842011-09-26 00:44:09 +0000102 else {
103 assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
104 OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
105 }
106 }
Chandler Carruth60740842011-09-26 00:44:09 +0000107 }
Benjamin Kramere2125d82011-09-26 02:14:13 +0000108 if (Started)
109 OS << ']';
Chandler Carruth60740842011-09-26 00:44:09 +0000110}
111
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000112void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000113 const Diagnostic &Info) {
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000114 // Default implementation (Warnings/errors count).
115 DiagnosticConsumer::HandleDiagnostic(Level, Info);
116
Chandler Carruth2be992d2011-09-26 11:25:30 +0000117 // Render the diagnostic message into a temporary buffer eagerly. We'll use
118 // this later as we print out the diagnostic to the terminal.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000119 SmallString<100> OutStr;
Chandler Carruth2be992d2011-09-26 11:25:30 +0000120 Info.FormatDiagnostic(OutStr);
121
122 llvm::raw_svector_ostream DiagMessageStream(OutStr);
Chandler Carruth2be992d2011-09-26 11:25:30 +0000123 printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
124
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000125 // Keeps track of the starting position of the location
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000126 // information (e.g., "foo.c:10:4:") that precedes the error
127 // message. We use this information to determine how long the
128 // file+line+column number prefix is.
129 uint64_t StartOfLocationInfo = OS.tell();
130
131 if (!Prefix.empty())
132 OS << Prefix << ": ";
133
Chandler Carruth2be992d2011-09-26 11:25:30 +0000134 // Use a dedicated, simpler path for diagnostics without a valid location.
Chandler Carruth577372e2011-09-26 11:38:46 +0000135 // This is important as if the location is missing, we may be emitting
136 // diagnostics in a context that lacks language options, a source manager, or
137 // other infrastructure necessary when emitting more rich diagnostics.
Chandler Carruth2be992d2011-09-26 11:25:30 +0000138 if (!Info.getLocation().isValid()) {
Hans Wennborgf4aee182013-09-24 00:08:55 +0000139 TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors,
140 DiagOpts->CLFallbackMode);
Chandler Carruth76576db2011-10-15 22:57:29 +0000141 TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(),
142 OS.tell() - StartOfLocationInfo,
143 DiagOpts->MessageLength,
144 DiagOpts->ShowColors);
Chandler Carruth2be992d2011-09-26 11:25:30 +0000145 OS.flush();
146 return;
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000147 }
148
Chandler Carruth577372e2011-09-26 11:38:46 +0000149 // Assert that the rest of our infrastructure is setup properly.
Chandler Carruth577372e2011-09-26 11:38:46 +0000150 assert(DiagOpts && "Unexpected diagnostic without options set");
151 assert(Info.hasSourceManager() &&
152 "Unexpected diagnostic with no source manager");
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000153 assert(TextDiag && "Unexpected diagnostic outside source file processing");
Chandler Carruth2be992d2011-09-26 11:25:30 +0000154
Chandler Carruth3eb8b542011-10-16 02:57:39 +0000155 TextDiag->emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(),
156 Info.getRanges(),
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000157 Info.getFixItHints(),
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000158 &Info.getSourceManager());
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000159
Chris Lattner327984f2008-11-19 06:56:25 +0000160 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000161}