blob: 17646b48e23dd5c15a633ca28e5c45f8941f074b [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"
Bill Wendling37b1dde2007-06-07 09:34:54 +000016#include "clang/Basic/SourceManager.h"
Chandler Carrutha3028852011-10-15 23:43:53 +000017#include "clang/Frontend/TextDiagnostic.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000018#include "clang/Lex/Lexer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "llvm/ADT/SmallString.h"
20#include "llvm/Support/ErrorHandling.h"
Chris Lattner327984f2008-11-19 06:56:25 +000021#include "llvm/Support/raw_ostream.h"
Douglas Gregor87f95b02009-02-26 21:00:50 +000022#include <algorithm>
Bill Wendling37b1dde2007-06-07 09:34:54 +000023using namespace clang;
24
Chris Lattner0e62c1c2011-07-23 10:55:15 +000025TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
Douglas Gregor811db4e2012-10-23 22:26:28 +000026 DiagnosticOptions *diags,
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000027 bool _OwnsOutputStream)
Douglas Gregor811db4e2012-10-23 22:26:28 +000028 : OS(os), DiagOpts(diags),
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000029 OwnsOutputStream(_OwnsOutputStream) {
30}
31
32TextDiagnosticPrinter::~TextDiagnosticPrinter() {
33 if (OwnsOutputStream)
34 delete &OS;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000035}
36
Chandler Carruth3eb8b542011-10-16 02:57:39 +000037void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
38 const Preprocessor *PP) {
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +000039 // Build the TextDiagnostic utility.
Douglas Gregor811db4e2012-10-23 22:26:28 +000040 TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts));
Chandler Carruth3eb8b542011-10-16 02:57:39 +000041}
42
43void TextDiagnosticPrinter::EndSourceFile() {
David Blaikie3875a822014-07-19 01:06:45 +000044 TextDiag.reset();
Chandler Carruth3eb8b542011-10-16 02:57:39 +000045}
46
Chandler Carruthcf259a42011-09-26 01:30:09 +000047/// \brief Print any diagnostic option information to a raw_ostream.
48///
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: " [...]".
52static void printDiagnosticOptions(raw_ostream &OS,
Chandler Carruth60740842011-09-26 00:44:09 +000053 DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +000054 const Diagnostic &Info,
Chandler Carruth60740842011-09-26 00:44:09 +000055 const DiagnosticOptions &DiagOpts) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000056 bool Started = false;
Chandler Carruth60740842011-09-26 00:44:09 +000057 if (DiagOpts.ShowOptionNames) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000058 // Handle special cases for non-warnings early.
59 if (Info.getID() == diag::fatal_too_many_errors) {
60 OS << " [-ferror-limit=]";
61 return;
62 }
63
Daniel Dunbaraa111382011-09-29 01:01:08 +000064 // 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.
68
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.
Chandler Carruth60740842011-09-26 00:44:09 +000073 if (Level == DiagnosticsEngine::Error &&
Daniel Dunbaraa111382011-09-29 01:01:08 +000074 DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) &&
75 !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) {
76 OS << " [-Werror";
77 Started = true;
Chandler Carruth0059a9b2011-09-26 01:21:58 +000078 }
79
Chandler Carruth60740842011-09-26 00:44:09 +000080 StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
81 if (!Opt.empty()) {
Diego Novillo829b1702014-04-16 16:54:24 +000082 OS << (Started ? "," : " [")
Alp Toker2724ec32014-06-22 10:08:06 +000083 << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt;
Alp Toker4ea0d232014-07-09 01:37:24 +000084 StringRef OptValue = Info.getDiags()->getFlagValue();
Diego Novillo9f239972014-04-21 23:16:03 +000085 if (!OptValue.empty())
86 OS << "=" << OptValue;
Chandler Carruth0059a9b2011-09-26 01:21:58 +000087 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +000088 }
89 }
Chandler Carruth60740842011-09-26 00:44:09 +000090
Chandler Carruth0059a9b2011-09-26 01:21:58 +000091 // If the user wants to see category information, include it too.
92 if (DiagOpts.ShowCategories) {
93 unsigned DiagCategory =
94 DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
Chandler Carruth60740842011-09-26 00:44:09 +000095 if (DiagCategory) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +000096 OS << (Started ? "," : " [");
Benjamin Kramere2125d82011-09-26 02:14:13 +000097 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +000098 if (DiagOpts.ShowCategories == 1)
Benjamin Kramere2125d82011-09-26 02:14:13 +000099 OS << DiagCategory;
Chandler Carruth60740842011-09-26 00:44:09 +0000100 else {
101 assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
102 OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
103 }
104 }
Chandler Carruth60740842011-09-26 00:44:09 +0000105 }
Benjamin Kramere2125d82011-09-26 02:14:13 +0000106 if (Started)
107 OS << ']';
Chandler Carruth60740842011-09-26 00:44:09 +0000108}
109
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000110void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000111 const Diagnostic &Info) {
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000112 // Default implementation (Warnings/errors count).
113 DiagnosticConsumer::HandleDiagnostic(Level, Info);
114
Chandler Carruth2be992d2011-09-26 11:25:30 +0000115 // Render the diagnostic message into a temporary buffer eagerly. We'll use
116 // this later as we print out the diagnostic to the terminal.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000117 SmallString<100> OutStr;
Chandler Carruth2be992d2011-09-26 11:25:30 +0000118 Info.FormatDiagnostic(OutStr);
119
120 llvm::raw_svector_ostream DiagMessageStream(OutStr);
Chandler Carruth2be992d2011-09-26 11:25:30 +0000121 printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
122
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000123 // Keeps track of the starting position of the location
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000124 // 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();
128
129 if (!Prefix.empty())
130 OS << Prefix << ": ";
131
Chandler Carruth2be992d2011-09-26 11:25:30 +0000132 // Use a dedicated, simpler path for diagnostics without a valid location.
Chandler Carruth577372e2011-09-26 11:38:46 +0000133 // 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.
Chandler Carruth2be992d2011-09-26 11:25:30 +0000136 if (!Info.getLocation().isValid()) {
Hans Wennborgf4aee182013-09-24 00:08:55 +0000137 TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors,
138 DiagOpts->CLFallbackMode);
Chandler Carruth76576db2011-10-15 22:57:29 +0000139 TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(),
140 OS.tell() - StartOfLocationInfo,
141 DiagOpts->MessageLength,
142 DiagOpts->ShowColors);
Chandler Carruth2be992d2011-09-26 11:25:30 +0000143 OS.flush();
144 return;
Chandler Carruth09c65ce2011-09-26 00:26:47 +0000145 }
146
Chandler Carruth577372e2011-09-26 11:38:46 +0000147 // Assert that the rest of our infrastructure is setup properly.
Chandler Carruth577372e2011-09-26 11:38:46 +0000148 assert(DiagOpts && "Unexpected diagnostic without options set");
149 assert(Info.hasSourceManager() &&
150 "Unexpected diagnostic with no source manager");
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000151 assert(TextDiag && "Unexpected diagnostic outside source file processing");
Chandler Carruth2be992d2011-09-26 11:25:30 +0000152
Chandler Carruth3eb8b542011-10-16 02:57:39 +0000153 TextDiag->emitDiagnostic(Info.getLocation(), Level, DiagMessageStream.str(),
154 Info.getRanges(),
Alexander Kornienkod3b4e082014-05-22 19:56:11 +0000155 Info.getFixItHints(),
Argyrios Kyrtzidisb16ff5d2012-05-10 05:03:45 +0000156 &Info.getSourceManager());
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000157
Chris Lattner327984f2008-11-19 06:56:25 +0000158 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000159}