blob: b5784b9fc013c6c13547a4d8c98bbf45ffeaa7ed [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"
Axel Naumann63fbaed2011-01-27 10:55:51 +000015#include "clang/Basic/FileManager.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000016#include "clang/Basic/SourceManager.h"
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000017#include "clang/Frontend/DiagnosticOptions.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000018#include "clang/Lex/Lexer.h"
Chris Lattnerc45529b2009-05-05 22:03:18 +000019#include "llvm/Support/MemoryBuffer.h"
Chris Lattner327984f2008-11-19 06:56:25 +000020#include "llvm/Support/raw_ostream.h"
David Blaikie79000202011-09-23 05:57:42 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner23be0672008-11-19 06:51:40 +000022#include "llvm/ADT/SmallString.h"
Douglas Gregor87f95b02009-02-26 21:00:50 +000023#include <algorithm>
Bill Wendling37b1dde2007-06-07 09:34:54 +000024using namespace clang;
25
Chris Lattner0e62c1c2011-07-23 10:55:15 +000026static const enum raw_ostream::Colors noteColor =
27 raw_ostream::BLACK;
28static const enum raw_ostream::Colors fixitColor =
29 raw_ostream::GREEN;
30static const enum raw_ostream::Colors caretColor =
31 raw_ostream::GREEN;
32static const enum raw_ostream::Colors warningColor =
33 raw_ostream::MAGENTA;
34static const enum raw_ostream::Colors errorColor = raw_ostream::RED;
35static const enum raw_ostream::Colors fatalColor = raw_ostream::RED;
Daniel Dunbaraa7d55a2010-02-25 03:23:40 +000036// Used for changing only the bold attribute.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000037static const enum raw_ostream::Colors savedColor =
38 raw_ostream::SAVEDCOLOR;
Torok Edwinc91b6e02009-06-04 07:18:23 +000039
Douglas Gregor48185532009-05-01 21:53:04 +000040/// \brief Number of spaces to indent when word-wrapping.
41const unsigned WordWrapIndentation = 6;
42
Chris Lattner0e62c1c2011-07-23 10:55:15 +000043TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000044 const DiagnosticOptions &diags,
45 bool _OwnsOutputStream)
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000046 : OS(os), LangOpts(0), DiagOpts(&diags),
Daniel Dunbard0c3bb02009-11-11 09:38:24 +000047 LastCaretDiagnosticWasNote(0),
48 OwnsOutputStream(_OwnsOutputStream) {
49}
50
51TextDiagnosticPrinter::~TextDiagnosticPrinter() {
52 if (OwnsOutputStream)
53 delete &OS;
Daniel Dunbarc2e6a472009-11-04 06:24:30 +000054}
55
Chandler Carruth153a7bb2011-08-31 23:59:19 +000056/// \brief Helper to recursivly walk up the include stack and print each layer
57/// on the way back down.
58static void PrintIncludeStackRecursively(raw_ostream &OS,
59 const SourceManager &SM,
60 SourceLocation Loc,
61 bool ShowLocation) {
62 if (Loc.isInvalid())
63 return;
Chris Lattnerdc5c0552007-07-20 16:37:10 +000064
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000065 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Douglas Gregor453b0122010-11-12 07:15:47 +000066 if (PLoc.isInvalid())
67 return;
Chris Lattner92b29b2f2009-04-21 03:57:54 +000068
Chandler Carruth153a7bb2011-08-31 23:59:19 +000069 // Print out the other include frames first.
70 PrintIncludeStackRecursively(OS, SM, PLoc.getIncludeLoc(), ShowLocation);
71
72 if (ShowLocation)
Chris Lattner92b29b2f2009-04-21 03:57:54 +000073 OS << "In file included from " << PLoc.getFilename()
74 << ':' << PLoc.getLine() << ":\n";
75 else
76 OS << "In included file:\n";
Bill Wendling37b1dde2007-06-07 09:34:54 +000077}
78
Chandler Carruth153a7bb2011-08-31 23:59:19 +000079/// \brief Prints an include stack when appropriate for a particular diagnostic
80/// level and location.
81///
82/// This routine handles all the logic of suppressing particular include stacks
83/// (such as those for notes) and duplicate include stacks when repeated
84/// warnings occur within the same file. It also handles the logic of
85/// customizing the formatting and display of the include stack.
86///
87/// \param Level The diagnostic level of the message this stack pertains to.
88/// \param Loc The include location of the current file (not the diagnostic
89/// location).
David Blaikie9c902b52011-09-25 23:23:43 +000090void TextDiagnosticPrinter::PrintIncludeStack(DiagnosticsEngine::Level Level,
Chandler Carruth153a7bb2011-08-31 23:59:19 +000091 SourceLocation Loc,
92 const SourceManager &SM) {
93 // Skip redundant include stacks altogether.
94 if (LastWarningLoc == Loc)
95 return;
96 LastWarningLoc = Loc;
97
David Blaikie9c902b52011-09-25 23:23:43 +000098 if (!DiagOpts->ShowNoteIncludeStack && Level == DiagnosticsEngine::Note)
Chandler Carruth153a7bb2011-08-31 23:59:19 +000099 return;
100
101 PrintIncludeStackRecursively(OS, SM, Loc, DiagOpts->ShowLocation);
102}
103
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000104/// \brief When the source code line we want to print is too long for
105/// the terminal, select the "interesting" region.
106static void SelectInterestingSourceRegion(std::string &SourceLine,
107 std::string &CaretLine,
108 std::string &FixItInsertionLine,
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000109 unsigned EndOfCaretToken,
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000110 unsigned Columns) {
Douglas Gregoraef00222010-04-16 00:23:51 +0000111 unsigned MaxSize = std::max(SourceLine.size(),
112 std::max(CaretLine.size(),
113 FixItInsertionLine.size()));
114 if (MaxSize > SourceLine.size())
115 SourceLine.resize(MaxSize, ' ');
116 if (MaxSize > CaretLine.size())
117 CaretLine.resize(MaxSize, ' ');
118 if (!FixItInsertionLine.empty() && MaxSize > FixItInsertionLine.size())
119 FixItInsertionLine.resize(MaxSize, ' ');
120
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000121 // Find the slice that we need to display the full caret line
122 // correctly.
123 unsigned CaretStart = 0, CaretEnd = CaretLine.size();
124 for (; CaretStart != CaretEnd; ++CaretStart)
125 if (!isspace(CaretLine[CaretStart]))
126 break;
127
128 for (; CaretEnd != CaretStart; --CaretEnd)
129 if (!isspace(CaretLine[CaretEnd - 1]))
130 break;
Douglas Gregor2d69cd22009-05-04 06:27:32 +0000131
132 // Make sure we don't chop the string shorter than the caret token
133 // itself.
134 if (CaretEnd < EndOfCaretToken)
135 CaretEnd = EndOfCaretToken;
136
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000137 // If we have a fix-it line, make sure the slice includes all of the
138 // fix-it information.
139 if (!FixItInsertionLine.empty()) {
140 unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
141 for (; FixItStart != FixItEnd; ++FixItStart)
142 if (!isspace(FixItInsertionLine[FixItStart]))
143 break;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000144
Douglas Gregorb8c6d5d2009-05-03 04:33:32 +0000145 for (; FixItEnd != FixItStart; --FixItEnd)
146 if (!isspace(FixItInsertionLine[FixItEnd - 1]))
147 break;
148
149 if (FixItStart < CaretStart)
150 CaretStart = FixItStart;
151 if (FixItEnd > CaretEnd)
152 CaretEnd = FixItEnd;
153 }
154
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000155 // CaretLine[CaretStart, CaretEnd) contains all of the interesting
156 // parts of the caret line. While this slice is smaller than the
157 // number of columns we have, try to grow the slice to encompass
158 // more context.
159
160 // If the end of the interesting region comes before we run out of
161 // space in the terminal, start at the beginning of the line.
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000162 if (Columns > 3 && CaretEnd < Columns - 3)
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000163 CaretStart = 0;
164
Douglas Gregor12c3a5c2009-05-15 18:05:24 +0000165 unsigned TargetColumns = Columns;
166 if (TargetColumns > 8)
167 TargetColumns -= 8; // Give us extra room for the ellipses.
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000168 unsigned SourceLength = SourceLine.size();
Douglas Gregor006fd382009-05-04 06:45:38 +0000169 while ((CaretEnd - CaretStart) < TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000170 bool ExpandedRegion = false;
171 // Move the start of the interesting region left until we've
172 // pulled in something else interesting.
Douglas Gregor006fd382009-05-04 06:45:38 +0000173 if (CaretStart == 1)
174 CaretStart = 0;
175 else if (CaretStart > 1) {
176 unsigned NewStart = CaretStart - 1;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000177
Douglas Gregor006fd382009-05-04 06:45:38 +0000178 // Skip over any whitespace we see here; we're looking for
179 // another bit of interesting text.
180 while (NewStart && isspace(SourceLine[NewStart]))
181 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000182
Douglas Gregor006fd382009-05-04 06:45:38 +0000183 // Skip over this bit of "interesting" text.
184 while (NewStart && !isspace(SourceLine[NewStart]))
185 --NewStart;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000186
Douglas Gregor006fd382009-05-04 06:45:38 +0000187 // Move up to the non-whitespace character we just saw.
188 if (NewStart)
189 ++NewStart;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000190
191 // If we're still within our limit, update the starting
192 // position within the source/caret line.
Douglas Gregor006fd382009-05-04 06:45:38 +0000193 if (CaretEnd - NewStart <= TargetColumns) {
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000194 CaretStart = NewStart;
195 ExpandedRegion = true;
196 }
197 }
198
199 // Move the end of the interesting region right until we've
200 // pulled in something else interesting.
Daniel Dunbar6f949912009-05-03 23:04:40 +0000201 if (CaretEnd != SourceLength) {
Daniel Dunbar28a24fd2009-10-19 09:11:21 +0000202 assert(CaretEnd < SourceLength && "Unexpected caret position!");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000203 unsigned NewEnd = CaretEnd;
204
205 // Skip over any whitespace we see here; we're looking for
206 // another bit of interesting text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000207 while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000208 ++NewEnd;
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000209
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000210 // Skip over this bit of "interesting" text.
Douglas Gregor8e35e2d2009-05-18 22:09:16 +0000211 while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1]))
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000212 ++NewEnd;
213
214 if (NewEnd - CaretStart <= TargetColumns) {
215 CaretEnd = NewEnd;
216 ExpandedRegion = true;
217 }
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000218 }
Daniel Dunbar6f949912009-05-03 23:04:40 +0000219
220 if (!ExpandedRegion)
221 break;
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000222 }
223
224 // [CaretStart, CaretEnd) is the slice we want. Update the various
225 // output lines to show only this slice, with two-space padding
226 // before the lines so that it looks nicer.
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000227 if (CaretEnd < SourceLine.size())
228 SourceLine.replace(CaretEnd, std::string::npos, "...");
Douglas Gregorcb516622009-05-03 15:24:25 +0000229 if (CaretEnd < CaretLine.size())
230 CaretLine.erase(CaretEnd, std::string::npos);
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000231 if (FixItInsertionLine.size() > CaretEnd)
232 FixItInsertionLine.erase(CaretEnd, std::string::npos);
Daniel Dunbar49f0e802009-09-07 23:07:56 +0000233
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000234 if (CaretStart > 2) {
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000235 SourceLine.replace(0, CaretStart, " ...");
236 CaretLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000237 if (FixItInsertionLine.size() >= CaretStart)
Douglas Gregor54c6a3b2009-05-03 04:12:51 +0000238 FixItInsertionLine.replace(0, CaretStart, " ");
Douglas Gregorcf7b2af2009-05-01 23:32:58 +0000239 }
240}
241
Chandler Carruth64d376a2011-07-14 08:20:31 +0000242/// Look through spelling locations for a macro argument expansion, and
Chandler Carruth402bb382011-07-07 23:56:36 +0000243/// if found skip to it so that we can trace the argument rather than the macros
Chandler Carruth64d376a2011-07-14 08:20:31 +0000244/// in which that argument is used. If no macro argument expansion is found,
Chandler Carruth402bb382011-07-07 23:56:36 +0000245/// don't skip anything and return the starting location.
Chandler Carruth64d376a2011-07-14 08:20:31 +0000246static SourceLocation skipToMacroArgExpansion(const SourceManager &SM,
Chandler Carruth402bb382011-07-07 23:56:36 +0000247 SourceLocation StartLoc) {
248 for (SourceLocation L = StartLoc; L.isMacroID();
249 L = SM.getImmediateSpellingLoc(L)) {
Chandler Carruthaa631532011-07-26 03:03:00 +0000250 if (SM.isMacroArgExpansion(L))
Chandler Carruth402bb382011-07-07 23:56:36 +0000251 return L;
252 }
253
254 // Otherwise just return initial location, there's nothing to skip.
255 return StartLoc;
256}
257
258/// Gets the location of the immediate macro caller, one level up the stack
259/// toward the initial macro typed into the source.
260static SourceLocation getImmediateMacroCallerLoc(const SourceManager &SM,
261 SourceLocation Loc) {
262 if (!Loc.isMacroID()) return Loc;
263
264 // When we have the location of (part of) an expanded parameter, its spelling
265 // location points to the argument as typed into the macro call, and
266 // therefore is used to locate the macro caller.
Chandler Carruthaa631532011-07-26 03:03:00 +0000267 if (SM.isMacroArgExpansion(Loc))
Chandler Carruth402bb382011-07-07 23:56:36 +0000268 return SM.getImmediateSpellingLoc(Loc);
269
270 // Otherwise, the caller of the macro is located where this macro is
Chandler Carruth64d376a2011-07-14 08:20:31 +0000271 // expanded (while the spelling is part of the macro definition).
Chandler Carruthca757582011-07-25 20:52:21 +0000272 return SM.getImmediateExpansionRange(Loc).first;
Chandler Carruth402bb382011-07-07 23:56:36 +0000273}
274
275/// Gets the location of the immediate macro callee, one level down the stack
276/// toward the leaf macro.
277static SourceLocation getImmediateMacroCalleeLoc(const SourceManager &SM,
278 SourceLocation Loc) {
279 if (!Loc.isMacroID()) return Loc;
280
281 // When we have the location of (part of) an expanded parameter, its
Chandler Carruth64d376a2011-07-14 08:20:31 +0000282 // expansion location points to the unexpanded paramater reference within
Chandler Carruth402bb382011-07-07 23:56:36 +0000283 // the macro definition (or callee).
Chandler Carruthaa631532011-07-26 03:03:00 +0000284 if (SM.isMacroArgExpansion(Loc))
Chandler Carruthca757582011-07-25 20:52:21 +0000285 return SM.getImmediateExpansionRange(Loc).first;
Chandler Carruth402bb382011-07-07 23:56:36 +0000286
287 // Otherwise, the callee of the macro is located where this location was
288 // spelled inside the macro definition.
289 return SM.getImmediateSpellingLoc(Loc);
290}
291
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000292namespace {
293
Chandler Carruth84f36192011-09-25 22:54:56 +0000294/// \brief Class to encapsulate the logic for formatting and printing a textual
295/// diagnostic message.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000296///
Chandler Carruth84f36192011-09-25 22:54:56 +0000297/// This class provides an interface for building and emitting a textual
298/// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt
299/// Hints, and code snippets. In the presence of macros this involves
300/// a recursive process, synthesizing notes for each macro expansion.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000301///
Chandler Carruth84f36192011-09-25 22:54:56 +0000302/// The purpose of this class is to isolate the implementation of printing
303/// beautiful text diagnostics from any particular interfaces. The Clang
304/// DiagnosticClient is implemented through this class as is diagnostic
305/// printing coming out of libclang.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000306///
Chandler Carruth84f36192011-09-25 22:54:56 +0000307/// A brief worklist:
308/// FIXME: Sink the printing of the diagnostic message itself into this class.
309/// FIXME: Sink the printing of the include stack into this class.
310/// FIXME: Remove the TextDiagnosticPrinter as an input.
311/// FIXME: Sink the recursive printing of template instantiations into this
312/// class.
313class TextDiagnostic {
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000314 TextDiagnosticPrinter &Printer;
315 raw_ostream &OS;
316 const SourceManager &SM;
317 const LangOptions &LangOpts;
318 const DiagnosticOptions &DiagOpts;
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000319
320public:
Chandler Carruth84f36192011-09-25 22:54:56 +0000321 TextDiagnostic(TextDiagnosticPrinter &Printer,
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000322 raw_ostream &OS,
323 const SourceManager &SM,
324 const LangOptions &LangOpts,
Chandler Carruth3236f0d2011-09-25 22:31:58 +0000325 const DiagnosticOptions &DiagOpts)
326 : Printer(Printer), OS(OS), SM(SM), LangOpts(LangOpts), DiagOpts(DiagOpts) {
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000327 }
328
Chandler Carruth09c74642011-10-15 01:21:55 +0000329 /// \brief Emit the caret and underlining text.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000330 ///
331 /// Walks up the macro expansion stack printing the code snippet, caret,
332 /// underlines and FixItHint display as appropriate at each level. Walk is
333 /// accomplished by calling itself recursively.
334 ///
Chandler Carruth09c74642011-10-15 01:21:55 +0000335 /// FIXME: Remove macro expansion from this routine, it shouldn't be tied to
336 /// caret diagnostics.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000337 /// FIXME: Break up massive function into logical units.
338 ///
339 /// \param Loc The location for this caret.
340 /// \param Ranges The underlined ranges for this code snippet.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000341 /// \param Hints The FixIt hints active for this diagnostic.
Chandler Carruthcb8f82a2011-09-25 22:27:52 +0000342 /// \param MacroSkipEnd The depth to stop skipping macro expansions.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000343 /// \param OnMacroInst The current depth of the macro expansion stack.
Chandler Carruth09c74642011-10-15 01:21:55 +0000344 void EmitCaret(SourceLocation Loc,
Chandler Carruth773757a2011-09-07 01:47:09 +0000345 SmallVectorImpl<CharSourceRange>& Ranges,
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000346 ArrayRef<FixItHint> Hints,
Chandler Carruthcb8f82a2011-09-25 22:27:52 +0000347 unsigned &MacroDepth,
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000348 unsigned OnMacroInst = 0) {
349 assert(!Loc.isInvalid() && "must have a valid source location here");
350
Chandler Carruth9d229f32011-09-25 06:59:38 +0000351 // If this is a file source location, directly emit the source snippet and
Chandler Carruthcb8f82a2011-09-25 22:27:52 +0000352 // caret line. Also record the macro depth reached.
353 if (Loc.isFileID()) {
354 assert(MacroDepth == 0 && "We shouldn't hit a leaf node twice!");
355 MacroDepth = OnMacroInst;
356 EmitSnippetAndCaret(Loc, Ranges, Hints);
357 return;
358 }
Chandler Carruth9d229f32011-09-25 06:59:38 +0000359 // Otherwise recurse through each macro expansion layer.
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000360
Chandler Carruth9d229f32011-09-25 06:59:38 +0000361 // When processing macros, skip over the expansions leading up to
362 // a macro argument, and trace the argument's expansion stack instead.
363 Loc = skipToMacroArgExpansion(SM, Loc);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000364
Chandler Carruth9d229f32011-09-25 06:59:38 +0000365 SourceLocation OneLevelUp = getImmediateMacroCallerLoc(SM, Loc);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000366
Chandler Carruth9d229f32011-09-25 06:59:38 +0000367 // FIXME: Map ranges?
Chandler Carruth09c74642011-10-15 01:21:55 +0000368 EmitCaret(OneLevelUp, Ranges, Hints, MacroDepth, OnMacroInst + 1);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000369
Chandler Carruth9d229f32011-09-25 06:59:38 +0000370 // Map the location.
371 Loc = getImmediateMacroCalleeLoc(SM, Loc);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000372
Chandler Carruthcb8f82a2011-09-25 22:27:52 +0000373 unsigned MacroSkipStart = 0, MacroSkipEnd = 0;
374 if (MacroDepth > DiagOpts.MacroBacktraceLimit) {
375 MacroSkipStart = DiagOpts.MacroBacktraceLimit / 2 +
376 DiagOpts.MacroBacktraceLimit % 2;
377 MacroSkipEnd = MacroDepth - DiagOpts.MacroBacktraceLimit / 2;
378 }
379
380 // Whether to suppress printing this macro expansion.
381 bool Suppressed = (OnMacroInst >= MacroSkipStart &&
382 OnMacroInst < MacroSkipEnd);
383
Chandler Carruth9d229f32011-09-25 06:59:38 +0000384 // Map the ranges.
385 for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(),
386 E = Ranges.end();
387 I != E; ++I) {
388 SourceLocation Start = I->getBegin(), End = I->getEnd();
389 if (Start.isMacroID())
390 I->setBegin(getImmediateMacroCalleeLoc(SM, Start));
391 if (End.isMacroID())
392 I->setEnd(getImmediateMacroCalleeLoc(SM, End));
393 }
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000394
Chandler Carruth9d229f32011-09-25 06:59:38 +0000395 if (!Suppressed) {
396 // Don't print recursive expansion notes from an expansion note.
397 Loc = SM.getSpellingLoc(Loc);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000398
Chandler Carruth9d229f32011-09-25 06:59:38 +0000399 // Get the pretty name, according to #line directives etc.
400 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
401 if (PLoc.isInvalid())
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000402 return;
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000403
Chandler Carruth9d229f32011-09-25 06:59:38 +0000404 // If this diagnostic is not in the main file, print out the
405 // "included from" lines.
David Blaikie9c902b52011-09-25 23:23:43 +0000406 Printer.PrintIncludeStack(DiagnosticsEngine::Note, PLoc.getIncludeLoc(),
407 SM);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000408
Chandler Carruth9d229f32011-09-25 06:59:38 +0000409 if (DiagOpts.ShowLocation) {
410 // Emit the file/line/column that this expansion came from.
411 OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':';
412 if (DiagOpts.ShowColumn)
413 OS << PLoc.getColumn() << ':';
414 OS << ' ';
415 }
416 OS << "note: expanded from:\n";
417
418 EmitSnippetAndCaret(Loc, Ranges, ArrayRef<FixItHint>());
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000419 return;
420 }
421
Chandler Carruth9d229f32011-09-25 06:59:38 +0000422 if (OnMacroInst == MacroSkipStart) {
423 // Tell the user that we've skipped contexts.
424 OS << "note: (skipping " << (MacroSkipEnd - MacroSkipStart)
425 << " expansions in backtrace; use -fmacro-backtrace-limit=0 to see "
426 "all)\n";
427 }
428 }
429
430 /// \brief Emit a code snippet and caret line.
431 ///
432 /// This routine emits a single line's code snippet and caret line..
433 ///
434 /// \param Loc The location for the caret.
435 /// \param Ranges The underlined ranges for this code snippet.
436 /// \param Hints The FixIt hints active for this diagnostic.
437 void EmitSnippetAndCaret(SourceLocation Loc,
438 SmallVectorImpl<CharSourceRange>& Ranges,
439 ArrayRef<FixItHint> Hints) {
440 assert(!Loc.isInvalid() && "must have a valid source location here");
441 assert(Loc.isFileID() && "must have a file location here");
442
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000443 // Decompose the location into a FID/Offset pair.
444 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
445 FileID FID = LocInfo.first;
446 unsigned FileOffset = LocInfo.second;
447
448 // Get information about the buffer it points into.
449 bool Invalid = false;
450 const char *BufStart = SM.getBufferData(FID, &Invalid).data();
451 if (Invalid)
452 return;
453
Chandler Carruth0f1006a2011-09-07 05:01:10 +0000454 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000455 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
456 unsigned CaretEndColNo
457 = ColNo + Lexer::MeasureTokenLength(Loc, SM, LangOpts);
458
459 // Rewind from the current position to the start of the line.
460 const char *TokPtr = BufStart+FileOffset;
461 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
462
463
464 // Compute the line end. Scan forward from the error position to the end of
465 // the line.
466 const char *LineEnd = TokPtr;
467 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0')
468 ++LineEnd;
469
470 // FIXME: This shouldn't be necessary, but the CaretEndColNo can extend past
471 // the source line length as currently being computed. See
472 // test/Misc/message-length.c.
473 CaretEndColNo = std::min(CaretEndColNo, unsigned(LineEnd - LineStart));
474
475 // Copy the line of code into an std::string for ease of manipulation.
476 std::string SourceLine(LineStart, LineEnd);
477
478 // Create a line for the caret that is filled with spaces that is the same
479 // length as the line of source code.
480 std::string CaretLine(LineEnd-LineStart, ' ');
481
482 // Highlight all of the characters covered by Ranges with ~ characters.
Chandler Carruth0f1006a2011-09-07 05:01:10 +0000483 for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(),
484 E = Ranges.end();
485 I != E; ++I)
Chandler Carruth97798302011-09-07 07:02:31 +0000486 HighlightRange(*I, LineNo, FID, SourceLine, CaretLine);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000487
488 // Next, insert the caret itself.
489 if (ColNo-1 < CaretLine.size())
490 CaretLine[ColNo-1] = '^';
491 else
492 CaretLine.push_back('^');
493
Chandler Carruthe79ddf82011-09-07 05:36:50 +0000494 ExpandTabs(SourceLine, CaretLine);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000495
496 // If we are in -fdiagnostics-print-source-range-info mode, we are trying
497 // to produce easily machine parsable output. Add a space before the
498 // source line and the caret to make it trivial to tell the main diagnostic
499 // line from what the user is intended to see.
500 if (DiagOpts.ShowSourceRanges) {
501 SourceLine = ' ' + SourceLine;
502 CaretLine = ' ' + CaretLine;
503 }
504
Chandler Carruth0f1006a2011-09-07 05:01:10 +0000505 std::string FixItInsertionLine = BuildFixItInsertionLine(LineNo,
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000506 LineStart, LineEnd,
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000507 Hints);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000508
509 // If the source line is too long for our terminal, select only the
510 // "interesting" source region within that line.
Chandler Carruth3236f0d2011-09-25 22:31:58 +0000511 unsigned Columns = DiagOpts.MessageLength;
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000512 if (Columns && SourceLine.size() > Columns)
513 SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
514 CaretEndColNo, Columns);
515
516 // Finally, remove any blank spaces from the end of CaretLine.
517 while (CaretLine[CaretLine.size()-1] == ' ')
518 CaretLine.erase(CaretLine.end()-1);
519
520 // Emit what we have computed.
521 OS << SourceLine << '\n';
522
523 if (DiagOpts.ShowColors)
524 OS.changeColor(caretColor, true);
525 OS << CaretLine << '\n';
526 if (DiagOpts.ShowColors)
527 OS.resetColor();
528
529 if (!FixItInsertionLine.empty()) {
530 if (DiagOpts.ShowColors)
531 // Print fixit line in color
532 OS.changeColor(fixitColor, false);
533 if (DiagOpts.ShowSourceRanges)
534 OS << ' ';
535 OS << FixItInsertionLine << '\n';
536 if (DiagOpts.ShowColors)
537 OS.resetColor();
538 }
539
Chandler Carruthd3e83672011-09-02 06:30:30 +0000540 // Print out any parseable fixit information requested by the options.
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000541 EmitParseableFixits(Hints);
Chandler Carruthd3e83672011-09-02 06:30:30 +0000542 }
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000543
Chandler Carruthd3e83672011-09-02 06:30:30 +0000544private:
Chandler Carruth97798302011-09-07 07:02:31 +0000545 /// \brief Highlight a SourceRange (with ~'s) for any characters on LineNo.
546 void HighlightRange(const CharSourceRange &R,
547 unsigned LineNo, FileID FID,
548 const std::string &SourceLine,
549 std::string &CaretLine) {
550 assert(CaretLine.size() == SourceLine.size() &&
551 "Expect a correspondence between source and caret line!");
552 if (!R.isValid()) return;
553
554 SourceLocation Begin = SM.getExpansionLoc(R.getBegin());
555 SourceLocation End = SM.getExpansionLoc(R.getEnd());
556
557 // If the End location and the start location are the same and are a macro
558 // location, then the range was something that came from a macro expansion
559 // or _Pragma. If this is an object-like macro, the best we can do is to
560 // highlight the range. If this is a function-like macro, we'd also like to
561 // highlight the arguments.
562 if (Begin == End && R.getEnd().isMacroID())
563 End = SM.getExpansionRange(R.getEnd()).second;
564
565 unsigned StartLineNo = SM.getExpansionLineNumber(Begin);
566 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
567 return; // No intersection.
568
569 unsigned EndLineNo = SM.getExpansionLineNumber(End);
570 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
571 return; // No intersection.
572
573 // Compute the column number of the start.
574 unsigned StartColNo = 0;
575 if (StartLineNo == LineNo) {
576 StartColNo = SM.getExpansionColumnNumber(Begin);
577 if (StartColNo) --StartColNo; // Zero base the col #.
578 }
579
580 // Compute the column number of the end.
581 unsigned EndColNo = CaretLine.size();
582 if (EndLineNo == LineNo) {
583 EndColNo = SM.getExpansionColumnNumber(End);
584 if (EndColNo) {
585 --EndColNo; // Zero base the col #.
586
587 // Add in the length of the token, so that we cover multi-char tokens if
588 // this is a token range.
589 if (R.isTokenRange())
590 EndColNo += Lexer::MeasureTokenLength(End, SM, LangOpts);
591 } else {
592 EndColNo = CaretLine.size();
593 }
594 }
595
596 assert(StartColNo <= EndColNo && "Invalid range!");
597
598 // Check that a token range does not highlight only whitespace.
599 if (R.isTokenRange()) {
600 // Pick the first non-whitespace column.
601 while (StartColNo < SourceLine.size() &&
602 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
603 ++StartColNo;
604
605 // Pick the last non-whitespace column.
606 if (EndColNo > SourceLine.size())
607 EndColNo = SourceLine.size();
608 while (EndColNo-1 &&
609 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
610 --EndColNo;
611
612 // If the start/end passed each other, then we are trying to highlight a
613 // range that just exists in whitespace, which must be some sort of other
614 // bug.
615 assert(StartColNo <= EndColNo && "Trying to highlight whitespace??");
616 }
617
618 // Fill the range with ~'s.
619 for (unsigned i = StartColNo; i < EndColNo; ++i)
620 CaretLine[i] = '~';
621 }
622
Chandler Carruth0f1006a2011-09-07 05:01:10 +0000623 std::string BuildFixItInsertionLine(unsigned LineNo,
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000624 const char *LineStart,
625 const char *LineEnd,
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000626 ArrayRef<FixItHint> Hints) {
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000627 std::string FixItInsertionLine;
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000628 if (Hints.empty() || !DiagOpts.ShowFixits)
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000629 return FixItInsertionLine;
630
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000631 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
632 I != E; ++I) {
633 if (!I->CodeToInsert.empty()) {
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000634 // We have an insertion hint. Determine whether the inserted
635 // code is on the same line as the caret.
636 std::pair<FileID, unsigned> HintLocInfo
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000637 = SM.getDecomposedExpansionLoc(I->RemoveRange.getBegin());
Chandler Carruth0f1006a2011-09-07 05:01:10 +0000638 if (LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second)) {
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000639 // Insert the new code into the line just below the code
640 // that the user wrote.
641 unsigned HintColNo
642 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
643 unsigned LastColumnModified
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000644 = HintColNo - 1 + I->CodeToInsert.size();
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000645 if (LastColumnModified > FixItInsertionLine.size())
646 FixItInsertionLine.resize(LastColumnModified, ' ');
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000647 std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(),
Chandler Carruth20bfaa0f2011-09-06 22:01:04 +0000648 FixItInsertionLine.begin() + HintColNo - 1);
649 } else {
650 FixItInsertionLine.clear();
651 break;
652 }
653 }
654 }
655
656 if (FixItInsertionLine.empty())
657 return FixItInsertionLine;
658
659 // Now that we have the entire fixit line, expand the tabs in it.
660 // Since we don't want to insert spaces in the middle of a word,
661 // find each word and the column it should line up with and insert
662 // spaces until they match.
663 unsigned FixItPos = 0;
664 unsigned LinePos = 0;
665 unsigned TabExpandedCol = 0;
666 unsigned LineLength = LineEnd - LineStart;
667
668 while (FixItPos < FixItInsertionLine.size() && LinePos < LineLength) {
669 // Find the next word in the FixIt line.
670 while (FixItPos < FixItInsertionLine.size() &&
671 FixItInsertionLine[FixItPos] == ' ')
672 ++FixItPos;
673 unsigned CharDistance = FixItPos - TabExpandedCol;
674
675 // Walk forward in the source line, keeping track of
676 // the tab-expanded column.
677 for (unsigned I = 0; I < CharDistance; ++I, ++LinePos)
678 if (LinePos >= LineLength || LineStart[LinePos] != '\t')
679 ++TabExpandedCol;
680 else
681 TabExpandedCol =
682 (TabExpandedCol/DiagOpts.TabStop + 1) * DiagOpts.TabStop;
683
684 // Adjust the fixit line to match this column.
685 FixItInsertionLine.insert(FixItPos, TabExpandedCol-FixItPos, ' ');
686 FixItPos = TabExpandedCol;
687
688 // Walk to the end of the word.
689 while (FixItPos < FixItInsertionLine.size() &&
690 FixItInsertionLine[FixItPos] != ' ')
691 ++FixItPos;
692 }
693
694 return FixItInsertionLine;
695 }
696
Chandler Carruthe79ddf82011-09-07 05:36:50 +0000697 void ExpandTabs(std::string &SourceLine, std::string &CaretLine) {
698 // Scan the source line, looking for tabs. If we find any, manually expand
699 // them to spaces and update the CaretLine to match.
700 for (unsigned i = 0; i != SourceLine.size(); ++i) {
701 if (SourceLine[i] != '\t') continue;
702
703 // Replace this tab with at least one space.
704 SourceLine[i] = ' ';
705
706 // Compute the number of spaces we need to insert.
707 unsigned TabStop = DiagOpts.TabStop;
708 assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
709 "Invalid -ftabstop value");
710 unsigned NumSpaces = ((i+TabStop)/TabStop * TabStop) - (i+1);
711 assert(NumSpaces < TabStop && "Invalid computation of space amt");
712
713 // Insert spaces into the SourceLine.
714 SourceLine.insert(i+1, NumSpaces, ' ');
715
716 // Insert spaces or ~'s into CaretLine.
717 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
718 }
719 }
720
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000721 void EmitParseableFixits(ArrayRef<FixItHint> Hints) {
Chandler Carruthd3e83672011-09-02 06:30:30 +0000722 if (!DiagOpts.ShowParseableFixits)
723 return;
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000724
Chandler Carruthd3e83672011-09-02 06:30:30 +0000725 // We follow FixItRewriter's example in not (yet) handling
726 // fix-its in macros.
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000727 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
728 I != E; ++I) {
729 if (I->RemoveRange.isInvalid() ||
730 I->RemoveRange.getBegin().isMacroID() ||
731 I->RemoveRange.getEnd().isMacroID())
Chandler Carruthd3e83672011-09-02 06:30:30 +0000732 return;
733 }
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000734
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000735 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
736 I != E; ++I) {
737 SourceLocation BLoc = I->RemoveRange.getBegin();
738 SourceLocation ELoc = I->RemoveRange.getEnd();
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000739
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000740 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
741 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000742
Chandler Carruthd3e83672011-09-02 06:30:30 +0000743 // Adjust for token ranges.
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000744 if (I->RemoveRange.isTokenRange())
745 EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000746
Chandler Carruthd3e83672011-09-02 06:30:30 +0000747 // We specifically do not do word-wrapping or tab-expansion here,
748 // because this is supposed to be easy to parse.
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000749 PresumedLoc PLoc = SM.getPresumedLoc(BLoc);
Chandler Carruthd3e83672011-09-02 06:30:30 +0000750 if (PLoc.isInvalid())
751 break;
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000752
Chandler Carruthd3e83672011-09-02 06:30:30 +0000753 OS << "fix-it:\"";
Chandler Carruth935574d2011-09-06 22:34:33 +0000754 OS.write_escaped(PLoc.getFilename());
Chandler Carruthd3e83672011-09-02 06:30:30 +0000755 OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second)
756 << ':' << SM.getColumnNumber(BInfo.first, BInfo.second)
757 << '-' << SM.getLineNumber(EInfo.first, EInfo.second)
758 << ':' << SM.getColumnNumber(EInfo.first, EInfo.second)
759 << "}:\"";
Chandler Carruth1f28e6c2011-09-06 22:31:44 +0000760 OS.write_escaped(I->CodeToInsert);
Chandler Carruthd3e83672011-09-02 06:30:30 +0000761 OS << "\"\n";
Chandler Carrutheef47ba2011-08-31 23:59:23 +0000762 }
763 }
764};
765
766} // end namespace
767
Chandler Carruth402bb382011-07-07 23:56:36 +0000768/// Get the presumed location of a diagnostic message. This computes the
769/// presumed location for the top of any macro backtrace when present.
770static PresumedLoc getDiagnosticPresumedLoc(const SourceManager &SM,
771 SourceLocation Loc) {
772 // This is a condensed form of the algorithm used by EmitCaretDiagnostic to
773 // walk to the top of the macro call stack.
774 while (Loc.isMacroID()) {
Chandler Carruth64d376a2011-07-14 08:20:31 +0000775 Loc = skipToMacroArgExpansion(SM, Loc);
Chandler Carruth402bb382011-07-07 23:56:36 +0000776 Loc = getImmediateMacroCallerLoc(SM, Loc);
777 }
778
779 return SM.getPresumedLoc(Loc);
780}
781
Chandler Carruth3d45b232011-09-07 08:05:58 +0000782/// \brief Print out the file/line/column information and include trace.
783///
784/// This method handlen the emission of the diagnostic location information.
785/// This includes extracting as much location information as is present for the
786/// diagnostic and printing it, as well as any include stack or source ranges
787/// necessary.
Chandler Carruth342327ca2011-10-15 10:48:19 +0000788void TextDiagnosticPrinter::EmitDiagnosticLoc(SourceLocation Loc,
789 PresumedLoc PLoc,
790 DiagnosticsEngine::Level Level,
791 ArrayRef<CharSourceRange> Ranges,
792 const SourceManager &SM) {
Chandler Carruth3d45b232011-09-07 08:05:58 +0000793 if (PLoc.isInvalid()) {
794 // At least print the file name if available:
Chandler Carruth342327ca2011-10-15 10:48:19 +0000795 FileID FID = SM.getFileID(Loc);
Chandler Carruth3d45b232011-09-07 08:05:58 +0000796 if (!FID.isInvalid()) {
797 const FileEntry* FE = SM.getFileEntryForID(FID);
798 if (FE && FE->getName()) {
799 OS << FE->getName();
800 if (FE->getDevice() == 0 && FE->getInode() == 0
801 && FE->getFileMode() == 0) {
802 // in PCH is a guess, but a good one:
803 OS << " (in PCH)";
804 }
805 OS << ": ";
806 }
807 }
808 return;
809 }
810 unsigned LineNo = PLoc.getLine();
811
812 if (!DiagOpts->ShowLocation)
813 return;
814
815 if (DiagOpts->ShowColors)
816 OS.changeColor(savedColor, true);
817
818 OS << PLoc.getFilename();
819 switch (DiagOpts->Format) {
820 case DiagnosticOptions::Clang: OS << ':' << LineNo; break;
821 case DiagnosticOptions::Msvc: OS << '(' << LineNo; break;
822 case DiagnosticOptions::Vi: OS << " +" << LineNo; break;
823 }
824
825 if (DiagOpts->ShowColumn)
826 // Compute the column number.
827 if (unsigned ColNo = PLoc.getColumn()) {
828 if (DiagOpts->Format == DiagnosticOptions::Msvc) {
829 OS << ',';
830 ColNo--;
831 } else
832 OS << ':';
833 OS << ColNo;
834 }
835 switch (DiagOpts->Format) {
836 case DiagnosticOptions::Clang:
837 case DiagnosticOptions::Vi: OS << ':'; break;
838 case DiagnosticOptions::Msvc: OS << ") : "; break;
839 }
840
Chandler Carruth342327ca2011-10-15 10:48:19 +0000841 if (DiagOpts->ShowSourceRanges && !Ranges.empty()) {
Chandler Carruth3d45b232011-09-07 08:05:58 +0000842 FileID CaretFileID =
Chandler Carruth342327ca2011-10-15 10:48:19 +0000843 SM.getFileID(SM.getExpansionLoc(Loc));
Chandler Carruth3d45b232011-09-07 08:05:58 +0000844 bool PrintedRange = false;
845
Chandler Carruth342327ca2011-10-15 10:48:19 +0000846 for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(),
847 RE = Ranges.end();
848 RI != RE; ++RI) {
Chandler Carruth3d45b232011-09-07 08:05:58 +0000849 // Ignore invalid ranges.
Chandler Carruth342327ca2011-10-15 10:48:19 +0000850 if (!RI->isValid()) continue;
Chandler Carruth3d45b232011-09-07 08:05:58 +0000851
Chandler Carruth342327ca2011-10-15 10:48:19 +0000852 SourceLocation B = SM.getExpansionLoc(RI->getBegin());
853 SourceLocation E = SM.getExpansionLoc(RI->getEnd());
Chandler Carruth3d45b232011-09-07 08:05:58 +0000854
855 // If the End location and the start location are the same and are a
856 // macro location, then the range was something that came from a
857 // macro expansion or _Pragma. If this is an object-like macro, the
858 // best we can do is to highlight the range. If this is a
859 // function-like macro, we'd also like to highlight the arguments.
Chandler Carruth342327ca2011-10-15 10:48:19 +0000860 if (B == E && RI->getEnd().isMacroID())
861 E = SM.getExpansionRange(RI->getEnd()).second;
Chandler Carruth3d45b232011-09-07 08:05:58 +0000862
863 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
864 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
865
866 // If the start or end of the range is in another file, just discard
867 // it.
868 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
869 continue;
870
871 // Add in the length of the token, so that we cover multi-char
872 // tokens.
873 unsigned TokSize = 0;
Chandler Carruth342327ca2011-10-15 10:48:19 +0000874 if (RI->isTokenRange())
Chandler Carruth3d45b232011-09-07 08:05:58 +0000875 TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts);
876
877 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
878 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
879 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
880 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize)
881 << '}';
882 PrintedRange = true;
883 }
884
885 if (PrintedRange)
886 OS << ':';
887 }
888 OS << ' ';
889}
890
Chandler Carruthcf259a42011-09-26 01:30:09 +0000891/// \brief Print the diagonstic level to a raw_ostream.
892///
893/// Handles colorizing the level and formatting.
894static void printDiagnosticLevel(raw_ostream &OS,
Chandler Carruth02160582011-09-25 23:51:01 +0000895 DiagnosticsEngine::Level Level,
896 bool ShowColors) {
897 if (ShowColors) {
898 // Print diagnostic category in bold and color
899 switch (Level) {
900 case DiagnosticsEngine::Ignored:
901 llvm_unreachable("Invalid diagnostic type");
902 case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break;
903 case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break;
904 case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break;
905 case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break;
906 }
907 }
908
909 switch (Level) {
910 case DiagnosticsEngine::Ignored: llvm_unreachable("Invalid diagnostic type");
911 case DiagnosticsEngine::Note: OS << "note: "; break;
912 case DiagnosticsEngine::Warning: OS << "warning: "; break;
913 case DiagnosticsEngine::Error: OS << "error: "; break;
914 case DiagnosticsEngine::Fatal: OS << "fatal error: "; break;
915 }
916
917 if (ShowColors)
918 OS.resetColor();
919}
920
Chandler Carruthcf259a42011-09-26 01:30:09 +0000921/// \brief Print the diagnostic name to a raw_ostream.
922///
923/// This prints the diagnostic name to a raw_ostream if it has one. It formats
924/// the name according to the expected diagnostic message formatting:
925/// " [diagnostic_name_here]"
926static void printDiagnosticName(raw_ostream &OS, const Diagnostic &Info) {
Chandler Carruthb59f9cb92011-09-26 00:37:30 +0000927 if (!DiagnosticIDs::isBuiltinNote(Info.getID()))
928 OS << " [" << DiagnosticIDs::getName(Info.getID()) << "]";
929}
930
Chandler Carruthcf259a42011-09-26 01:30:09 +0000931/// \brief Print any diagnostic option information to a raw_ostream.
932///
933/// This implements all of the logic for adding diagnostic options to a message
934/// (via OS). Each relevant option is comma separated and all are enclosed in
935/// the standard bracketing: " [...]".
936static void printDiagnosticOptions(raw_ostream &OS,
Chandler Carruth60740842011-09-26 00:44:09 +0000937 DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +0000938 const Diagnostic &Info,
Chandler Carruth60740842011-09-26 00:44:09 +0000939 const DiagnosticOptions &DiagOpts) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +0000940 bool Started = false;
Chandler Carruth60740842011-09-26 00:44:09 +0000941 if (DiagOpts.ShowOptionNames) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +0000942 // Handle special cases for non-warnings early.
943 if (Info.getID() == diag::fatal_too_many_errors) {
944 OS << " [-ferror-limit=]";
945 return;
946 }
947
Daniel Dunbaraa111382011-09-29 01:01:08 +0000948 // The code below is somewhat fragile because we are essentially trying to
949 // report to the user what happened by inferring what the diagnostic engine
950 // did. Eventually it might make more sense to have the diagnostic engine
951 // include some "why" information in the diagnostic.
952
953 // If this is a warning which has been mapped to an error by the user (as
954 // inferred by checking whether the default mapping is to an error) then
955 // flag it as such. Note that diagnostics could also have been mapped by a
956 // pragma, but we don't currently have a way to distinguish this.
Chandler Carruth60740842011-09-26 00:44:09 +0000957 if (Level == DiagnosticsEngine::Error &&
Daniel Dunbaraa111382011-09-29 01:01:08 +0000958 DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) &&
959 !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) {
960 OS << " [-Werror";
961 Started = true;
Chandler Carruth0059a9b2011-09-26 01:21:58 +0000962 }
963
964 // If the diagnostic is an extension diagnostic and not enabled by default
965 // then it must have been turned on with -pedantic.
966 bool EnabledByDefault;
967 if (DiagnosticIDs::isBuiltinExtensionDiag(Info.getID(),
968 EnabledByDefault) &&
969 !EnabledByDefault) {
970 OS << (Started ? "," : " [") << "-pedantic";
971 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +0000972 }
973
974 StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
975 if (!Opt.empty()) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +0000976 OS << (Started ? "," : " [") << "-W" << Opt;
977 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +0000978 }
979 }
Chandler Carruth60740842011-09-26 00:44:09 +0000980
Chandler Carruth0059a9b2011-09-26 01:21:58 +0000981 // If the user wants to see category information, include it too.
982 if (DiagOpts.ShowCategories) {
983 unsigned DiagCategory =
984 DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
Chandler Carruth60740842011-09-26 00:44:09 +0000985 if (DiagCategory) {
Chandler Carruth0059a9b2011-09-26 01:21:58 +0000986 OS << (Started ? "," : " [");
Benjamin Kramere2125d82011-09-26 02:14:13 +0000987 Started = true;
Chandler Carruth60740842011-09-26 00:44:09 +0000988 if (DiagOpts.ShowCategories == 1)
Benjamin Kramere2125d82011-09-26 02:14:13 +0000989 OS << DiagCategory;
Chandler Carruth60740842011-09-26 00:44:09 +0000990 else {
991 assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
992 OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
993 }
994 }
Chandler Carruth60740842011-09-26 00:44:09 +0000995 }
Benjamin Kramere2125d82011-09-26 02:14:13 +0000996 if (Started)
997 OS << ']';
Chandler Carruth60740842011-09-26 00:44:09 +0000998}
999
Chandler Carruthbae3c7c2011-09-26 10:58:00 +00001000/// \brief Skip over whitespace in the string, starting at the given
1001/// index.
1002///
1003/// \returns The index of the first non-whitespace character that is
1004/// greater than or equal to Idx or, if no such character exists,
1005/// returns the end of the string.
Chandler Carruthc0fe5852011-09-26 11:19:35 +00001006static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) {
Chandler Carruthbae3c7c2011-09-26 10:58:00 +00001007 while (Idx < Length && isspace(Str[Idx]))
1008 ++Idx;
1009 return Idx;
1010}
1011
1012/// \brief If the given character is the start of some kind of
1013/// balanced punctuation (e.g., quotes or parentheses), return the
1014/// character that will terminate the punctuation.
1015///
1016/// \returns The ending punctuation character, if any, or the NULL
1017/// character if the input character does not start any punctuation.
1018static inline char findMatchingPunctuation(char c) {
1019 switch (c) {
1020 case '\'': return '\'';
1021 case '`': return '\'';
1022 case '"': return '"';
1023 case '(': return ')';
1024 case '[': return ']';
1025 case '{': return '}';
1026 default: break;
1027 }
1028
1029 return 0;
1030}
1031
1032/// \brief Find the end of the word starting at the given offset
1033/// within a string.
1034///
1035/// \returns the index pointing one character past the end of the
1036/// word.
Chandler Carruthc0fe5852011-09-26 11:19:35 +00001037static unsigned findEndOfWord(unsigned Start, StringRef Str,
Chandler Carruthbae3c7c2011-09-26 10:58:00 +00001038 unsigned Length, unsigned Column,
1039 unsigned Columns) {
1040 assert(Start < Str.size() && "Invalid start position!");
1041 unsigned End = Start + 1;
1042
1043 // If we are already at the end of the string, take that as the word.
1044 if (End == Str.size())
1045 return End;
1046
1047 // Determine if the start of the string is actually opening
1048 // punctuation, e.g., a quote or parentheses.
1049 char EndPunct = findMatchingPunctuation(Str[Start]);
1050 if (!EndPunct) {
1051 // This is a normal word. Just find the first space character.
1052 while (End < Length && !isspace(Str[End]))
1053 ++End;
1054 return End;
1055 }
1056
1057 // We have the start of a balanced punctuation sequence (quotes,
1058 // parentheses, etc.). Determine the full sequence is.
1059 llvm::SmallString<16> PunctuationEndStack;
1060 PunctuationEndStack.push_back(EndPunct);
1061 while (End < Length && !PunctuationEndStack.empty()) {
1062 if (Str[End] == PunctuationEndStack.back())
1063 PunctuationEndStack.pop_back();
1064 else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
1065 PunctuationEndStack.push_back(SubEndPunct);
1066
1067 ++End;
1068 }
1069
1070 // Find the first space character after the punctuation ended.
1071 while (End < Length && !isspace(Str[End]))
1072 ++End;
1073
1074 unsigned PunctWordLength = End - Start;
1075 if (// If the word fits on this line
1076 Column + PunctWordLength <= Columns ||
1077 // ... or the word is "short enough" to take up the next line
1078 // without too much ugly white space
1079 PunctWordLength < Columns/3)
1080 return End; // Take the whole thing as a single "word".
1081
1082 // The whole quoted/parenthesized string is too long to print as a
1083 // single "word". Instead, find the "word" that starts just after
1084 // the punctuation and use that end-point instead. This will recurse
1085 // until it finds something small enough to consider a word.
1086 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
1087}
1088
Chandler Carruthb1e536b2011-09-26 01:44:27 +00001089/// \brief Print the given string to a stream, word-wrapping it to
1090/// some number of columns in the process.
1091///
1092/// \param OS the stream to which the word-wrapping string will be
1093/// emitted.
1094/// \param Str the string to word-wrap and output.
1095/// \param Columns the number of columns to word-wrap to.
1096/// \param Column the column number at which the first character of \p
1097/// Str will be printed. This will be non-zero when part of the first
1098/// line has already been printed.
1099/// \param Indentation the number of spaces to indent any lines beyond
1100/// the first line.
1101/// \returns true if word-wrapping was required, or false if the
1102/// string fit on the first line.
Chandler Carruthc0fe5852011-09-26 11:19:35 +00001103static bool printWordWrapped(raw_ostream &OS, StringRef Str,
Chandler Carruthb1e536b2011-09-26 01:44:27 +00001104 unsigned Columns,
1105 unsigned Column = 0,
1106 unsigned Indentation = WordWrapIndentation) {
Chandler Carruth374eaa92011-09-26 16:43:25 +00001107 const unsigned Length = std::min(Str.find('\n'), Str.size());
Chandler Carruthb1e536b2011-09-26 01:44:27 +00001108
1109 // The string used to indent each line.
1110 llvm::SmallString<16> IndentStr;
1111 IndentStr.assign(Indentation, ' ');
1112 bool Wrapped = false;
1113 for (unsigned WordStart = 0, WordEnd; WordStart < Length;
1114 WordStart = WordEnd) {
1115 // Find the beginning of the next word.
1116 WordStart = skipWhitespace(WordStart, Str, Length);
1117 if (WordStart == Length)
1118 break;
1119
1120 // Find the end of this word.
1121 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
1122
1123 // Does this word fit on the current line?
1124 unsigned WordLength = WordEnd - WordStart;
1125 if (Column + WordLength < Columns) {
1126 // This word fits on the current line; print it there.
1127 if (WordStart) {
1128 OS << ' ';
1129 Column += 1;
1130 }
Chandler Carruthc0fe5852011-09-26 11:19:35 +00001131 OS << Str.substr(WordStart, WordLength);
Chandler Carruthb1e536b2011-09-26 01:44:27 +00001132 Column += WordLength;
1133 continue;
1134 }
1135
1136 // This word does not fit on the current line, so wrap to the next
1137 // line.
1138 OS << '\n';
1139 OS.write(&IndentStr[0], Indentation);
Chandler Carruthc0fe5852011-09-26 11:19:35 +00001140 OS << Str.substr(WordStart, WordLength);
Chandler Carruthb1e536b2011-09-26 01:44:27 +00001141 Column = Indentation + WordLength;
1142 Wrapped = true;
1143 }
1144
Chandler Carruth374eaa92011-09-26 16:43:25 +00001145 // Append any remaning text from the message with its existing formatting.
1146 OS << Str.substr(Length);
1147
Chandler Carruthd16b42e2011-09-26 01:44:29 +00001148 return Wrapped;
Chandler Carruthb1e536b2011-09-26 01:44:27 +00001149}
1150
Chandler Carruthc0fe5852011-09-26 11:19:35 +00001151static void printDiagnosticMessage(raw_ostream &OS,
1152 DiagnosticsEngine::Level Level,
1153 StringRef Message,
1154 unsigned CurrentColumn, unsigned Columns,
1155 bool ShowColors) {
1156 if (ShowColors) {
1157 // Print warnings, errors and fatal errors in bold, no color
1158 switch (Level) {
1159 case DiagnosticsEngine::Warning: OS.changeColor(savedColor, true); break;
1160 case DiagnosticsEngine::Error: OS.changeColor(savedColor, true); break;
1161 case DiagnosticsEngine::Fatal: OS.changeColor(savedColor, true); break;
1162 default: break; //don't bold notes
1163 }
1164 }
1165
1166 if (Columns)
1167 printWordWrapped(OS, Message, Columns, CurrentColumn);
1168 else
1169 OS << Message;
1170
1171 if (ShowColors)
1172 OS.resetColor();
1173 OS << '\n';
1174}
1175
Chandler Carruth09c65ce2011-09-26 00:26:47 +00001176void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
David Blaikieb5784322011-09-26 01:18:08 +00001177 const Diagnostic &Info) {
Chandler Carruth09c65ce2011-09-26 00:26:47 +00001178 // Default implementation (Warnings/errors count).
1179 DiagnosticConsumer::HandleDiagnostic(Level, Info);
1180
Chandler Carruth2be992d2011-09-26 11:25:30 +00001181 // Render the diagnostic message into a temporary buffer eagerly. We'll use
1182 // this later as we print out the diagnostic to the terminal.
1183 llvm::SmallString<100> OutStr;
1184 Info.FormatDiagnostic(OutStr);
1185
1186 llvm::raw_svector_ostream DiagMessageStream(OutStr);
1187 if (DiagOpts->ShowNames)
1188 printDiagnosticName(DiagMessageStream, Info);
1189 printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
1190
1191
Chandler Carruth09c65ce2011-09-26 00:26:47 +00001192 // Keeps track of the the starting position of the location
1193 // information (e.g., "foo.c:10:4:") that precedes the error
1194 // message. We use this information to determine how long the
1195 // file+line+column number prefix is.
1196 uint64_t StartOfLocationInfo = OS.tell();
1197
1198 if (!Prefix.empty())
1199 OS << Prefix << ": ";
1200
Chandler Carruth2be992d2011-09-26 11:25:30 +00001201 // Use a dedicated, simpler path for diagnostics without a valid location.
Chandler Carruth577372e2011-09-26 11:38:46 +00001202 // This is important as if the location is missing, we may be emitting
1203 // diagnostics in a context that lacks language options, a source manager, or
1204 // other infrastructure necessary when emitting more rich diagnostics.
Chandler Carruth2be992d2011-09-26 11:25:30 +00001205 if (!Info.getLocation().isValid()) {
1206 printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
1207 printDiagnosticMessage(OS, Level, DiagMessageStream.str(),
1208 OS.tell() - StartOfLocationInfo,
1209 DiagOpts->MessageLength, DiagOpts->ShowColors);
1210 OS.flush();
1211 return;
Chandler Carruth09c65ce2011-09-26 00:26:47 +00001212 }
1213
Chandler Carruth577372e2011-09-26 11:38:46 +00001214 // Assert that the rest of our infrastructure is setup properly.
1215 assert(LangOpts && "Unexpected diagnostic outside source file processing");
1216 assert(DiagOpts && "Unexpected diagnostic without options set");
1217 assert(Info.hasSourceManager() &&
1218 "Unexpected diagnostic with no source manager");
Chandler Carruth2be992d2011-09-26 11:25:30 +00001219 const SourceManager &SM = Info.getSourceManager();
Chandler Carruth577372e2011-09-26 11:38:46 +00001220 TextDiagnostic TextDiag(*this, OS, SM, *LangOpts, *DiagOpts);
1221
Chandler Carruth2be992d2011-09-26 11:25:30 +00001222 PresumedLoc PLoc = getDiagnosticPresumedLoc(SM, Info.getLocation());
1223
1224 // First, if this diagnostic is not in the main file, print out the
1225 // "included from" lines.
1226 PrintIncludeStack(Level, PLoc.getIncludeLoc(), SM);
1227 StartOfLocationInfo = OS.tell();
1228
1229 // Next emit the location of this particular diagnostic.
Chandler Carruth342327ca2011-10-15 10:48:19 +00001230 EmitDiagnosticLoc(Info.getLocation(), PLoc, Level, Info.getRanges(), SM);
Chandler Carruth2be992d2011-09-26 11:25:30 +00001231
1232 if (DiagOpts->ShowColors)
1233 OS.resetColor();
1234
Chandler Carruthcf259a42011-09-26 01:30:09 +00001235 printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
Chandler Carruthc0fe5852011-09-26 11:19:35 +00001236 printDiagnosticMessage(OS, Level, DiagMessageStream.str(),
1237 OS.tell() - StartOfLocationInfo,
1238 DiagOpts->MessageLength, DiagOpts->ShowColors);
Daniel Dunbar49f0e802009-09-07 23:07:56 +00001239
Douglas Gregor79cf6032009-03-10 20:44:00 +00001240 // If caret diagnostics are enabled and we have location, we want to
1241 // emit the caret. However, we only do this if the location moved
1242 // from the last diagnostic, if the last diagnostic was a note that
1243 // was part of a different warning or error diagnostic, or if the
1244 // diagnostic has ranges. We don't want to emit the same caret
1245 // multiple times if one loc has multiple diagnostics.
Chandler Carruth2be992d2011-09-26 11:25:30 +00001246 if (DiagOpts->ShowCarets &&
Daniel Dunbar49f0e802009-09-07 23:07:56 +00001247 ((LastLoc != Info.getLocation()) || Info.getNumRanges() ||
David Blaikie9c902b52011-09-25 23:23:43 +00001248 (LastCaretDiagnosticWasNote && Level != DiagnosticsEngine::Note) ||
Douglas Gregora771f462010-03-31 17:46:05 +00001249 Info.getNumFixItHints())) {
Steve Naroffa36992242008-02-08 22:06:17 +00001250 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00001251 LastLoc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
David Blaikie9c902b52011-09-25 23:23:43 +00001252 LastCaretDiagnosticWasNote = (Level == DiagnosticsEngine::Note);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +00001253
Chris Lattner1973d842009-02-20 00:18:51 +00001254 // Get the ranges into a local array we can hack on.
Chandler Carruth773757a2011-09-07 01:47:09 +00001255 SmallVector<CharSourceRange, 20> Ranges;
1256 Ranges.reserve(Info.getNumRanges());
1257 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i)
1258 Ranges.push_back(Info.getRange(i));
Daniel Dunbar49f0e802009-09-07 23:07:56 +00001259
Chandler Carruth773757a2011-09-07 01:47:09 +00001260 for (unsigned i = 0, e = Info.getNumFixItHints(); i != e; ++i) {
Chris Lattnered8b6b72010-06-18 22:45:06 +00001261 const FixItHint &Hint = Info.getFixItHint(i);
Chandler Carruth773757a2011-09-07 01:47:09 +00001262 if (Hint.RemoveRange.isValid())
1263 Ranges.push_back(Hint.RemoveRange);
Douglas Gregor87f95b02009-02-26 21:00:50 +00001264 }
1265
Chandler Carruth74e2c352011-09-25 23:01:05 +00001266 unsigned MacroDepth = 0;
Chandler Carruth09c74642011-10-15 01:21:55 +00001267 TextDiag.EmitCaret(LastLoc, Ranges,
1268 llvm::makeArrayRef(Info.getFixItHints(),
1269 Info.getNumFixItHints()),
1270 MacroDepth);
Bill Wendling37b1dde2007-06-07 09:34:54 +00001271 }
Daniel Dunbar49f0e802009-09-07 23:07:56 +00001272
Chris Lattner327984f2008-11-19 06:56:25 +00001273 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +00001274}
Douglas Gregord0e9e3a2011-09-29 00:38:00 +00001275
1276DiagnosticConsumer *
1277TextDiagnosticPrinter::clone(DiagnosticsEngine &Diags) const {
1278 return new TextDiagnosticPrinter(OS, *DiagOpts, /*OwnsOutputStream=*/false);
1279}