blob: 007dfae5a7b587636e1a61ab011e49f5c338debc [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar68952de2009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticPrinter.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Lex/Lexer.h"
Chris Lattner92a33532008-11-19 06:56:25 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattnerbe8e5a42008-11-19 06:51:40 +000018#include "llvm/ADT/SmallString.h"
Douglas Gregor3bb30002009-02-26 21:00:50 +000019#include <algorithm>
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Douglas Gregora4eb3e72009-05-01 21:53:04 +000022/// \brief Number of spaces to indent when word-wrapping.
23const unsigned WordWrapIndentation = 6;
24
Chris Lattner4b009652007-07-25 00:24:17 +000025void TextDiagnosticPrinter::
Chris Lattner836774b2009-01-27 07:57:44 +000026PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
27 if (Loc.isInvalid()) return;
Chris Lattner4b009652007-07-25 00:24:17 +000028
Chris Lattner836774b2009-01-27 07:57:44 +000029 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +000030
31 // Print out the other include frames first.
Chris Lattner836774b2009-01-27 07:57:44 +000032 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Chris Lattnerfd0739e2009-04-21 03:57:54 +000033
34 if (ShowLocation)
35 OS << "In file included from " << PLoc.getFilename()
36 << ':' << PLoc.getLine() << ":\n";
37 else
38 OS << "In included file:\n";
Chris Lattner4b009652007-07-25 00:24:17 +000039}
40
41/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
42/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekb3ee1932007-12-11 21:27:55 +000043void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattner836774b2009-01-27 07:57:44 +000044 const SourceManager &SM,
Chris Lattner10aaf532009-01-17 08:45:21 +000045 unsigned LineNo, FileID FID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000046 std::string &CaretLine,
Nuno Lopesd0e162c2008-08-05 19:40:20 +000047 const std::string &SourceLine) {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000048 assert(CaretLine.size() == SourceLine.size() &&
49 "Expect a correspondence between source and caret line!");
Chris Lattner4b009652007-07-25 00:24:17 +000050 if (!R.isValid()) return;
51
Chris Lattner836774b2009-01-27 07:57:44 +000052 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
53 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
54
Chris Lattnere357b112009-02-17 05:19:10 +000055 // If the End location and the start location are the same and are a macro
56 // location, then the range was something that came from a macro expansion
57 // or _Pragma. If this is an object-like macro, the best we can do is to
58 // highlight the range. If this is a function-like macro, we'd also like to
59 // highlight the arguments.
60 if (Begin == End && R.getEnd().isMacroID())
61 End = SM.getInstantiationRange(R.getEnd()).second;
62
Chris Lattner2d89c562009-02-04 01:06:56 +000063 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattner836774b2009-01-27 07:57:44 +000064 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000065 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000066
Chris Lattner2d89c562009-02-04 01:06:56 +000067 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattner836774b2009-01-27 07:57:44 +000068 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000069 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000070
71 // Compute the column number of the start.
72 unsigned StartColNo = 0;
73 if (StartLineNo == LineNo) {
Chris Lattnere79fc852009-02-04 00:55:58 +000074 StartColNo = SM.getInstantiationColumnNumber(Begin);
Chris Lattner4b009652007-07-25 00:24:17 +000075 if (StartColNo) --StartColNo; // Zero base the col #.
76 }
77
78 // Pick the first non-whitespace column.
79 while (StartColNo < SourceLine.size() &&
80 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
81 ++StartColNo;
82
83 // Compute the column number of the end.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000084 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000085 if (EndLineNo == LineNo) {
Chris Lattnere79fc852009-02-04 00:55:58 +000086 EndColNo = SM.getInstantiationColumnNumber(End);
Chris Lattner4b009652007-07-25 00:24:17 +000087 if (EndColNo) {
88 --EndColNo; // Zero base the col #.
89
90 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattnere1be6022009-04-14 23:22:57 +000091 EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts);
Chris Lattner4b009652007-07-25 00:24:17 +000092 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000093 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000094 }
95 }
96
97 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-08-05 19:40:20 +000098 if (EndColNo <= SourceLine.size())
99 while (EndColNo-1 &&
100 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
101 --EndColNo;
102 else
103 EndColNo = SourceLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +0000104
105 // Fill the range with ~'s.
106 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +0000107 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000108 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +0000109}
110
Chris Lattnerec52b7d2009-02-20 00:18:51 +0000111void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Chris Lattner3272e922009-02-20 00:25:28 +0000112 SourceRange *Ranges,
Chris Lattnerec52b7d2009-02-20 00:18:51 +0000113 unsigned NumRanges,
Douglas Gregor3bb30002009-02-26 21:00:50 +0000114 SourceManager &SM,
115 const CodeModificationHint *Hints,
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000116 unsigned NumHints,
117 unsigned AvoidColumn) {
Chris Lattner37f9ad22009-02-17 08:44:50 +0000118 assert(!Loc.isInvalid() && "must have a valid source location here");
119
Chris Lattner3b29a182009-02-17 07:54:55 +0000120 // We always emit diagnostics about the instantiation points, not the spelling
121 // points. This more closely correlates to what the user writes.
Chris Lattner37f9ad22009-02-17 08:44:50 +0000122 if (!Loc.isFileID()) {
Chris Lattner459da5d2009-02-18 18:50:45 +0000123 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000124 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, AvoidColumn);
Chris Lattner37f9ad22009-02-17 08:44:50 +0000125
Chris Lattner3272e922009-02-20 00:25:28 +0000126 // Map the location through the macro.
Chris Lattner37f9ad22009-02-17 08:44:50 +0000127 Loc = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(Loc));
Chris Lattner3272e922009-02-20 00:25:28 +0000128
129 // Map the ranges.
130 for (unsigned i = 0; i != NumRanges; ++i) {
131 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd();
132 if (S.isMacroID())
133 S = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(S));
134 if (E.isMacroID())
135 E = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(E));
136 Ranges[i] = SourceRange(S, E);
137 }
Chris Lattner37f9ad22009-02-17 08:44:50 +0000138
Chris Lattnerfd0739e2009-04-21 03:57:54 +0000139 if (ShowLocation) {
140 // Emit the file/line/column that this expansion came from.
141 OS << SM.getBufferName(Loc) << ':' << SM.getInstantiationLineNumber(Loc)
142 << ':';
143 if (ShowColumn)
144 OS << SM.getInstantiationColumnNumber(Loc) << ':';
145 OS << ' ';
146 }
147 OS << "note: instantiated from:\n";
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000148 AvoidColumn = 0;
Chris Lattner37f9ad22009-02-17 08:44:50 +0000149 }
Chris Lattner34e6c262009-02-17 07:51:53 +0000150
151 // Decompose the location into a FID/Offset pair.
152 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
153 FileID FID = LocInfo.first;
154 unsigned FileOffset = LocInfo.second;
155
156 // Get information about the buffer it points into.
157 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID);
158 const char *BufStart = BufferInfo.first;
Chris Lattner34e6c262009-02-17 07:51:53 +0000159
160 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
Chris Lattnerc1303fb2009-02-17 07:38:37 +0000161
162 // Rewind from the current position to the start of the line.
Chris Lattner34e6c262009-02-17 07:51:53 +0000163 const char *TokPtr = BufStart+FileOffset;
164 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
165
Chris Lattnerc1303fb2009-02-17 07:38:37 +0000166
167 // Compute the line end. Scan forward from the error position to the end of
168 // the line.
Chris Lattner34e6c262009-02-17 07:51:53 +0000169 const char *LineEnd = TokPtr;
Chris Lattnerd9e72412009-03-08 08:11:22 +0000170 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0')
Chris Lattnerc1303fb2009-02-17 07:38:37 +0000171 ++LineEnd;
172
173 // Copy the line of code into an std::string for ease of manipulation.
174 std::string SourceLine(LineStart, LineEnd);
175
176 // Create a line for the caret that is filled with spaces that is the same
177 // length as the line of source code.
178 std::string CaretLine(LineEnd-LineStart, ' ');
179
180 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattnerec52b7d2009-02-20 00:18:51 +0000181 if (NumRanges) {
Chris Lattner34e6c262009-02-17 07:51:53 +0000182 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
183
Chris Lattnerec52b7d2009-02-20 00:18:51 +0000184 for (unsigned i = 0, e = NumRanges; i != e; ++i)
185 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine);
Chris Lattner34e6c262009-02-17 07:51:53 +0000186 }
Chris Lattnerc1303fb2009-02-17 07:38:37 +0000187
188 // Next, insert the caret itself.
189 if (ColNo-1 < CaretLine.size())
190 CaretLine[ColNo-1] = '^';
191 else
192 CaretLine.push_back('^');
193
194 // Scan the source line, looking for tabs. If we find any, manually expand
195 // them to 8 characters and update the CaretLine to match.
196 for (unsigned i = 0; i != SourceLine.size(); ++i) {
197 if (SourceLine[i] != '\t') continue;
198
199 // Replace this tab with at least one space.
200 SourceLine[i] = ' ';
201
202 // Compute the number of spaces we need to insert.
203 unsigned NumSpaces = ((i+8)&~7) - (i+1);
204 assert(NumSpaces < 8 && "Invalid computation of space amt");
205
206 // Insert spaces into the SourceLine.
207 SourceLine.insert(i+1, NumSpaces, ' ');
208
209 // Insert spaces or ~'s into CaretLine.
210 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
211 }
212
213 // Finally, remove any blank spaces from the end of CaretLine.
214 while (CaretLine[CaretLine.size()-1] == ' ')
215 CaretLine.erase(CaretLine.end()-1);
216
Chris Lattner404ba8e2009-04-28 22:33:16 +0000217 // If we are in -fdiagnostics-print-source-range-info mode, we are trying to
218 // produce easily machine parsable output. Add a space before the source line
219 // and the caret to make it trivial to tell the main diagnostic line from what
220 // the user is intended to see.
221 if (PrintRangeInfo) {
222 SourceLine = ' ' + SourceLine;
223 CaretLine = ' ' + CaretLine;
224 }
225
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000226 // AvoidColumn tells us which column we should avoid when printing
227 // the source line. If the source line would start at or near that
228 // column, add another line of whitespace before printing the source
229 // line. Otherwise, the source line and the diagnostic text can get
230 // jumbled together.
231 unsigned StartCol = 0;
232 for (unsigned N = SourceLine.size(); StartCol != N; ++StartCol)
233 if (!isspace(SourceLine[StartCol]))
234 break;
235
236 if (StartCol != SourceLine.size() &&
237 abs((int)StartCol - (int)AvoidColumn) <= 2)
238 OS << '\n';
Chris Lattner404ba8e2009-04-28 22:33:16 +0000239
Chris Lattnerc1303fb2009-02-17 07:38:37 +0000240 // Emit what we have computed.
241 OS << SourceLine << '\n';
242 OS << CaretLine << '\n';
Douglas Gregor3bb30002009-02-26 21:00:50 +0000243
Chris Lattner041bd732009-04-19 07:44:08 +0000244 if (NumHints && PrintFixItInfo) {
Douglas Gregor3bb30002009-02-26 21:00:50 +0000245 std::string InsertionLine;
Chris Lattner041bd732009-04-19 07:44:08 +0000246 for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints;
Douglas Gregor3bb30002009-02-26 21:00:50 +0000247 Hint != LastHint; ++Hint) {
248 if (Hint->InsertionLoc.isValid()) {
249 // We have an insertion hint. Determine whether the inserted
250 // code is on the same line as the caret.
251 std::pair<FileID, unsigned> HintLocInfo
Chris Lattner9ccffa72009-03-02 20:58:48 +0000252 = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc);
Douglas Gregor3bb30002009-02-26 21:00:50 +0000253 if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) ==
254 SM.getLineNumber(FID, FileOffset)) {
255 // Insert the new code into the line just below the code
256 // that the user wrote.
257 unsigned HintColNo
258 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
259 unsigned LastColumnModified
260 = HintColNo - 1 + Hint->CodeToInsert.size();
261 if (LastColumnModified > InsertionLine.size())
262 InsertionLine.resize(LastColumnModified, ' ');
263 std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(),
264 InsertionLine.begin() + HintColNo - 1);
265 }
266 }
267 }
268
Chris Lattner404ba8e2009-04-28 22:33:16 +0000269 if (!InsertionLine.empty()) {
270 if (PrintRangeInfo)
271 OS << ' ';
Douglas Gregor3bb30002009-02-26 21:00:50 +0000272 OS << InsertionLine << '\n';
Chris Lattner404ba8e2009-04-28 22:33:16 +0000273 }
Douglas Gregor3bb30002009-02-26 21:00:50 +0000274 }
Chris Lattnerc1303fb2009-02-17 07:38:37 +0000275}
276
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000277/// \brief Skip over whitespace in the string, starting at the given
278/// index.
279///
280/// \returns The index of the first non-whitespace character that is
281/// greater than or equal to Idx or, if no such character exists,
282/// returns the end of the string.
283static unsigned skipWhitespace(unsigned Idx,
284 const llvm::SmallVectorImpl<char> &Str,
285 unsigned Length) {
286 while (Idx < Length && isspace(Str[Idx]))
287 ++Idx;
288 return Idx;
289}
290
291/// \brief If the given character is the start of some kind of
292/// balanced punctuation (e.g., quotes or parentheses), return the
293/// character that will terminate the punctuation.
294///
295/// \returns The ending punctuation character, if any, or the NULL
296/// character if the input character does not start any punctuation.
297static inline char findMatchingPunctuation(char c) {
298 switch (c) {
299 case '\'': return '\'';
300 case '`': return '\'';
301 case '"': return '"';
302 case '(': return ')';
303 case '[': return ']';
304 case '{': return '}';
305 default: break;
306 }
307
308 return 0;
309}
310
311/// \brief Find the end of the word starting at the given offset
312/// within a string.
313///
314/// \returns the index pointing one character past the end of the
315/// word.
316unsigned findEndOfWord(unsigned Start,
317 const llvm::SmallVectorImpl<char> &Str,
318 unsigned Length, unsigned Column,
319 unsigned Columns) {
320 unsigned End = Start + 1;
321
322 // Determine if the start of the string is actually opening
323 // punctuation, e.g., a quote or parentheses.
324 char EndPunct = findMatchingPunctuation(Str[Start]);
325 if (!EndPunct) {
326 // This is a normal word. Just find the first space character.
327 while (End < Length && !isspace(Str[End]))
328 ++End;
329 return End;
330 }
331
332 // We have the start of a balanced punctuation sequence (quotes,
333 // parentheses, etc.). Determine the full sequence is.
334 llvm::SmallVector<char, 16> PunctuationEndStack;
335 PunctuationEndStack.push_back(EndPunct);
336 while (End < Length && !PunctuationEndStack.empty()) {
337 if (Str[End] == PunctuationEndStack.back())
338 PunctuationEndStack.pop_back();
339 else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
340 PunctuationEndStack.push_back(SubEndPunct);
341
342 ++End;
343 }
344
345 // Find the first space character after the punctuation ended.
346 while (End < Length && !isspace(Str[End]))
347 ++End;
348
349 unsigned PunctWordLength = End - Start;
350 if (// If the word fits on this line
351 Column + PunctWordLength <= Columns ||
352 // ... or the word is "short enough" to take up the next line
353 // without too much ugly white space
354 PunctWordLength < Columns/3)
355 return End; // Take the whole thing as a single "word".
356
357 // The whole quoted/parenthesized string is too long to print as a
358 // single "word". Instead, find the "word" that starts just after
359 // the punctuation and use that end-point instead. This will recurse
360 // until it finds something small enough to consider a word.
361 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
362}
363
364/// \brief Print the given string to a stream, word-wrapping it to
365/// some number of columns in the process.
366///
367/// \brief OS the stream to which the word-wrapping string will be
368/// emitted.
369///
370/// \brief Str the string to word-wrap and output.
371///
372/// \brief Columns the number of columns to word-wrap to.
373///
374/// \brief Column the column number at which the first character of \p
375/// Str will be printed. This will be non-zero when part of the first
376/// line has already been printed.
377///
378/// \brief Indentation the number of spaces to indent any lines beyond
379/// the first line.
380///
381/// \returns true if word-wrapping was required, or false if the
382/// string fit on the first line.
383static bool PrintWordWrapped(llvm::raw_ostream &OS,
384 const llvm::SmallVectorImpl<char> &Str,
385 unsigned Columns,
386 unsigned Column = 0,
387 unsigned Indentation = WordWrapIndentation) {
388 unsigned Length = Str.size();
389
390 // If there is a newline in this message somewhere, find that
391 // newline and split the message into the part before the newline
392 // (which will be word-wrapped) and the part from the newline one
393 // (which will be emitted unchanged).
394 for (unsigned I = 0; I != Length; ++I)
395 if (Str[I] == '\n') {
396 Length = I;
397 break;
398 }
399
400 // The string used to indent each line.
401 llvm::SmallString<16> IndentStr;
402 IndentStr.assign(Indentation, ' ');
403 bool Wrapped = false;
404 for (unsigned WordStart = 0, WordEnd; WordStart < Length;
405 WordStart = WordEnd) {
406 // Find the beginning of the next word.
407 WordStart = skipWhitespace(WordStart, Str, Length);
408 if (WordStart == Length)
409 break;
410
411 // Find the end of this word.
412 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
413
414 // Does this word fit on the current line?
415 unsigned WordLength = WordEnd - WordStart;
416 if (Column + WordLength < Columns) {
417 // This word fits on the current line; print it there.
418 if (WordStart) {
419 OS << ' ';
420 Column += 1;
421 }
422 OS.write(&Str[WordStart], WordLength);
423 Column += WordLength;
424 continue;
425 }
426
427 // This word does not fit on the current line, so wrap to the next
428 // line.
429 OS << '\n' << IndentStr.begin();
430 OS.write(&Str[WordStart], WordLength);
431 Column = Indentation + WordLength;
432 Wrapped = true;
433 }
434
435 if (Length == Str.size())
436 return Wrapped; // We're done.
437
438 // There is a newline in the message, followed by something that
439 // will not be word-wrapped. Print that.
440 OS.write(&Str[Length], Str.size() - Length);
441 return true;
442}
Chris Lattnerc1303fb2009-02-17 07:38:37 +0000443
Chris Lattner6948ae62008-11-18 07:04:44 +0000444void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
445 const DiagnosticInfo &Info) {
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000446 // Keeps track of the the starting position of the location
447 // information (e.g., "foo.c:10:4:") that precedes the error
448 // message. We use this information to determine how long the
449 // file+line+column number prefix is.
450 uint64_t StartOfLocationInfo = OS.tell();
451
Chris Lattner836774b2009-01-27 07:57:44 +0000452 // If the location is specified, print out a file/line/col and include trace
453 // if enabled.
454 if (Info.getLocation().isValid()) {
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000455 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattner836774b2009-01-27 07:57:44 +0000456 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
457 unsigned LineNo = PLoc.getLine();
Chris Lattner4b009652007-07-25 00:24:17 +0000458
459 // First, if this diagnostic is not in the main file, print out the
460 // "included from" lines.
Chris Lattner836774b2009-01-27 07:57:44 +0000461 if (LastWarningLoc != PLoc.getIncludeLoc()) {
462 LastWarningLoc = PLoc.getIncludeLoc();
463 PrintIncludeStack(LastWarningLoc, SM);
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000464 StartOfLocationInfo = OS.tell();
Chris Lattner4b009652007-07-25 00:24:17 +0000465 }
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000466
Chris Lattner836774b2009-01-27 07:57:44 +0000467 // Compute the column number.
Chris Lattner68c1e192009-01-30 17:41:53 +0000468 if (ShowLocation) {
469 OS << PLoc.getFilename() << ':' << LineNo << ':';
Chris Lattnerf0b28562009-02-17 07:34:34 +0000470 if (ShowColumn)
471 if (unsigned ColNo = PLoc.getColumn())
472 OS << ColNo << ':';
Chris Lattner695a4f52009-03-13 01:08:23 +0000473
474 if (PrintRangeInfo && Info.getNumRanges()) {
475 FileID CaretFileID =
476 SM.getFileID(SM.getInstantiationLoc(Info.getLocation()));
477 bool PrintedRange = false;
478
479 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) {
Chris Lattner94780632009-04-19 22:24:10 +0000480 // Ignore invalid ranges.
481 if (!Info.getRange(i).isValid()) continue;
482
Chris Lattner695a4f52009-03-13 01:08:23 +0000483 SourceLocation B = Info.getRange(i).getBegin();
484 SourceLocation E = Info.getRange(i).getEnd();
485 std::pair<FileID, unsigned> BInfo=SM.getDecomposedInstantiationLoc(B);
486
487 E = SM.getInstantiationLoc(E);
488 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
489
490 // If the start or end of the range is in another file, just discard
491 // it.
492 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
493 continue;
494
495 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattnere1be6022009-04-14 23:22:57 +0000496 unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts);
Chris Lattner695a4f52009-03-13 01:08:23 +0000497
498 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
499 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
500 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
501 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}';
502 PrintedRange = true;
503 }
504
505 if (PrintedRange)
506 OS << ':';
507 }
Chris Lattner68c1e192009-01-30 17:41:53 +0000508 OS << ' ';
509 }
Chris Lattner4b009652007-07-25 00:24:17 +0000510 }
511
512 switch (Level) {
Chris Lattner95cb5502009-02-06 03:57:44 +0000513 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman01d74272008-04-17 18:06:57 +0000514 case Diagnostic::Note: OS << "note: "; break;
515 case Diagnostic::Warning: OS << "warning: "; break;
516 case Diagnostic::Error: OS << "error: "; break;
Chris Lattner95cb5502009-02-06 03:57:44 +0000517 case Diagnostic::Fatal: OS << "fatal error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000518 }
519
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000520 llvm::SmallString<100> OutStr;
521 Info.FormatDiagnostic(OutStr);
Chris Lattnera96ec3b2009-04-16 05:44:38 +0000522
523 if (PrintDiagnosticOption)
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000524 if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) {
525 OutStr += " [-W";
526 OutStr += Opt;
527 OutStr += ']';
528 }
Chris Lattnera96ec3b2009-04-16 05:44:38 +0000529
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000530 bool WordWrapped = false;
531 if (MessageLength) {
532 // We will be word-wrapping the error message, so compute the
533 // column number where we currently are (after printing the
534 // location information).
535 unsigned Column = OS.tell() - StartOfLocationInfo;
536 WordWrapped = PrintWordWrapped(OS, OutStr, MessageLength, Column);
537 } else {
538 OS.write(OutStr.begin(), OutStr.size());
539 }
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000540 OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000541
Douglas Gregor56d25a72009-03-10 20:44:00 +0000542 // If caret diagnostics are enabled and we have location, we want to
543 // emit the caret. However, we only do this if the location moved
544 // from the last diagnostic, if the last diagnostic was a note that
545 // was part of a different warning or error diagnostic, or if the
546 // diagnostic has ranges. We don't want to emit the same caret
547 // multiple times if one loc has multiple diagnostics.
Chris Lattner836774b2009-01-27 07:57:44 +0000548 if (CaretDiagnostics && Info.getLocation().isValid() &&
Douglas Gregor3bb30002009-02-26 21:00:50 +0000549 ((LastLoc != Info.getLocation()) || Info.getNumRanges() ||
Douglas Gregor56d25a72009-03-10 20:44:00 +0000550 (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) ||
Douglas Gregor3bb30002009-02-26 21:00:50 +0000551 Info.getNumCodeModificationHints())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000552 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattner836774b2009-01-27 07:57:44 +0000553 LastLoc = Info.getLocation();
Douglas Gregor56d25a72009-03-10 20:44:00 +0000554 LastCaretDiagnosticWasNote = (Level == Diagnostic::Note);
Chris Lattner836774b2009-01-27 07:57:44 +0000555
Chris Lattnerec52b7d2009-02-20 00:18:51 +0000556 // Get the ranges into a local array we can hack on.
Douglas Gregor3bb30002009-02-26 21:00:50 +0000557 SourceRange Ranges[20];
Chris Lattnerec52b7d2009-02-20 00:18:51 +0000558 unsigned NumRanges = Info.getNumRanges();
Douglas Gregor3bb30002009-02-26 21:00:50 +0000559 assert(NumRanges < 20 && "Out of space");
Chris Lattnerec52b7d2009-02-20 00:18:51 +0000560 for (unsigned i = 0; i != NumRanges; ++i)
561 Ranges[i] = Info.getRange(i);
562
Douglas Gregor3bb30002009-02-26 21:00:50 +0000563 unsigned NumHints = Info.getNumCodeModificationHints();
564 for (unsigned idx = 0; idx < NumHints; ++idx) {
565 const CodeModificationHint &Hint = Info.getCodeModificationHint(idx);
566 if (Hint.RemoveRange.isValid()) {
567 assert(NumRanges < 20 && "Out of space");
568 Ranges[NumRanges++] = Hint.RemoveRange;
569 }
570 }
571
572 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
573 Info.getCodeModificationHints(),
Douglas Gregora4eb3e72009-05-01 21:53:04 +0000574 Info.getNumCodeModificationHints(),
575 WordWrapped? WordWrapIndentation : 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000576 }
Chris Lattner92a33532008-11-19 06:56:25 +0000577
578 OS.flush();
Chris Lattner4b009652007-07-25 00:24:17 +0000579}