blob: 7d3c4c23ffd6a9b06c895321a135ec0a455f8a96 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000014#include "clang/Frontend/TextDiagnosticPrinter.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000015#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/Lex/Lexer.h"
Chris Lattnera03a5b52008-11-19 06:56:25 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattnerf4c83962008-11-19 06:51:40 +000018#include "llvm/ADT/SmallString.h"
Douglas Gregor4b2d3f72009-02-26 21:00:50 +000019#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000020using namespace clang;
21
Reid Spencer5f016e22007-07-11 17:01:13 +000022void TextDiagnosticPrinter::
Chris Lattnerb9c3f962009-01-27 07:57:44 +000023PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
24 if (Loc.isInvalid()) return;
Chris Lattner9dc1f532007-07-20 16:37:10 +000025
Chris Lattnerb9c3f962009-01-27 07:57:44 +000026 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattner9dc1f532007-07-20 16:37:10 +000027
Reid Spencer5f016e22007-07-11 17:01:13 +000028 // Print out the other include frames first.
Chris Lattnerb9c3f962009-01-27 07:57:44 +000029 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Reid Spencer5f016e22007-07-11 17:01:13 +000030
Chris Lattnerb9c3f962009-01-27 07:57:44 +000031 OS << "In file included from " << PLoc.getFilename()
32 << ':' << PLoc.getLine() << ":\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
35/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
36/// any characters in LineNo that intersect the SourceRange.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000037void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnerb9c3f962009-01-27 07:57:44 +000038 const SourceManager &SM,
Chris Lattner3b4d5e92009-01-17 08:45:21 +000039 unsigned LineNo, FileID FID,
Gordon Henriksenaad69532008-08-09 19:58:22 +000040 std::string &CaretLine,
Nuno Lopesdb825682008-08-05 19:40:20 +000041 const std::string &SourceLine) {
Gordon Henriksenaad69532008-08-09 19:58:22 +000042 assert(CaretLine.size() == SourceLine.size() &&
43 "Expect a correspondence between source and caret line!");
Reid Spencer5f016e22007-07-11 17:01:13 +000044 if (!R.isValid()) return;
45
Chris Lattnerb9c3f962009-01-27 07:57:44 +000046 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
47 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
48
Chris Lattner34837a52009-02-17 05:19:10 +000049 // If the End location and the start location are the same and are a macro
50 // location, then the range was something that came from a macro expansion
51 // or _Pragma. If this is an object-like macro, the best we can do is to
52 // highlight the range. If this is a function-like macro, we'd also like to
53 // highlight the arguments.
54 if (Begin == End && R.getEnd().isMacroID())
55 End = SM.getInstantiationRange(R.getEnd()).second;
56
Chris Lattner30fc9332009-02-04 01:06:56 +000057 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattnerb9c3f962009-01-27 07:57:44 +000058 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000059 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000060
Chris Lattner30fc9332009-02-04 01:06:56 +000061 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattnerb9c3f962009-01-27 07:57:44 +000062 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000063 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000064
65 // Compute the column number of the start.
66 unsigned StartColNo = 0;
67 if (StartLineNo == LineNo) {
Chris Lattner7da5aea2009-02-04 00:55:58 +000068 StartColNo = SM.getInstantiationColumnNumber(Begin);
Reid Spencer5f016e22007-07-11 17:01:13 +000069 if (StartColNo) --StartColNo; // Zero base the col #.
70 }
71
72 // Pick the first non-whitespace column.
73 while (StartColNo < SourceLine.size() &&
74 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
75 ++StartColNo;
76
77 // Compute the column number of the end.
Gordon Henriksenaad69532008-08-09 19:58:22 +000078 unsigned EndColNo = CaretLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +000079 if (EndLineNo == LineNo) {
Chris Lattner7da5aea2009-02-04 00:55:58 +000080 EndColNo = SM.getInstantiationColumnNumber(End);
Reid Spencer5f016e22007-07-11 17:01:13 +000081 if (EndColNo) {
82 --EndColNo; // Zero base the col #.
83
84 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +000085 EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts);
Reid Spencer5f016e22007-07-11 17:01:13 +000086 } else {
Gordon Henriksenaad69532008-08-09 19:58:22 +000087 EndColNo = CaretLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +000088 }
89 }
90
91 // Pick the last non-whitespace column.
Nuno Lopesdb825682008-08-05 19:40:20 +000092 if (EndColNo <= SourceLine.size())
93 while (EndColNo-1 &&
94 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
95 --EndColNo;
96 else
97 EndColNo = SourceLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +000098
99 // Fill the range with ~'s.
100 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesdb825682008-08-05 19:40:20 +0000101 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenaad69532008-08-09 19:58:22 +0000102 CaretLine[i] = '~';
Reid Spencer5f016e22007-07-11 17:01:13 +0000103}
104
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000105void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Chris Lattner676f0242009-02-20 00:25:28 +0000106 SourceRange *Ranges,
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000107 unsigned NumRanges,
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000108 SourceManager &SM,
109 const CodeModificationHint *Hints,
110 unsigned NumHints) {
Chris Lattner55dcef02009-02-17 08:44:50 +0000111 assert(!Loc.isInvalid() && "must have a valid source location here");
112
Chris Lattner49a48eb2009-02-17 07:54:55 +0000113 // We always emit diagnostics about the instantiation points, not the spelling
114 // points. This more closely correlates to what the user writes.
Chris Lattner55dcef02009-02-17 08:44:50 +0000115 if (!Loc.isFileID()) {
Chris Lattner609b3ab2009-02-18 18:50:45 +0000116 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000117 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM);
Chris Lattner55dcef02009-02-17 08:44:50 +0000118
Chris Lattner676f0242009-02-20 00:25:28 +0000119 // Map the location through the macro.
Chris Lattner55dcef02009-02-17 08:44:50 +0000120 Loc = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(Loc));
Chris Lattner676f0242009-02-20 00:25:28 +0000121
122 // Map the ranges.
123 for (unsigned i = 0; i != NumRanges; ++i) {
124 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd();
125 if (S.isMacroID())
126 S = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(S));
127 if (E.isMacroID())
128 E = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(E));
129 Ranges[i] = SourceRange(S, E);
130 }
Chris Lattner55dcef02009-02-17 08:44:50 +0000131
132 // Emit the file/line/column that this expansion came from.
133 OS << SM.getBufferName(Loc) << ':' << SM.getInstantiationLineNumber(Loc)
134 << ':';
135 if (ShowColumn)
136 OS << SM.getInstantiationColumnNumber(Loc) << ':';
137 OS << " note: instantiated from:\n";
138 }
Chris Lattnerb88af812009-02-17 07:51:53 +0000139
140 // Decompose the location into a FID/Offset pair.
141 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
142 FileID FID = LocInfo.first;
143 unsigned FileOffset = LocInfo.second;
144
145 // Get information about the buffer it points into.
146 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID);
147 const char *BufStart = BufferInfo.first;
Chris Lattnerb88af812009-02-17 07:51:53 +0000148
149 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
Chris Lattner94f55782009-02-17 07:38:37 +0000150
151 // Rewind from the current position to the start of the line.
Chris Lattnerb88af812009-02-17 07:51:53 +0000152 const char *TokPtr = BufStart+FileOffset;
153 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
154
Chris Lattner94f55782009-02-17 07:38:37 +0000155
156 // Compute the line end. Scan forward from the error position to the end of
157 // the line.
Chris Lattnerb88af812009-02-17 07:51:53 +0000158 const char *LineEnd = TokPtr;
Chris Lattnercd1148b2009-03-08 08:11:22 +0000159 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0')
Chris Lattner94f55782009-02-17 07:38:37 +0000160 ++LineEnd;
161
162 // Copy the line of code into an std::string for ease of manipulation.
163 std::string SourceLine(LineStart, LineEnd);
164
165 // Create a line for the caret that is filled with spaces that is the same
166 // length as the line of source code.
167 std::string CaretLine(LineEnd-LineStart, ' ');
168
169 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000170 if (NumRanges) {
Chris Lattnerb88af812009-02-17 07:51:53 +0000171 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
172
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000173 for (unsigned i = 0, e = NumRanges; i != e; ++i)
174 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine);
Chris Lattnerb88af812009-02-17 07:51:53 +0000175 }
Chris Lattner94f55782009-02-17 07:38:37 +0000176
177 // Next, insert the caret itself.
178 if (ColNo-1 < CaretLine.size())
179 CaretLine[ColNo-1] = '^';
180 else
181 CaretLine.push_back('^');
182
183 // Scan the source line, looking for tabs. If we find any, manually expand
184 // them to 8 characters and update the CaretLine to match.
185 for (unsigned i = 0; i != SourceLine.size(); ++i) {
186 if (SourceLine[i] != '\t') continue;
187
188 // Replace this tab with at least one space.
189 SourceLine[i] = ' ';
190
191 // Compute the number of spaces we need to insert.
192 unsigned NumSpaces = ((i+8)&~7) - (i+1);
193 assert(NumSpaces < 8 && "Invalid computation of space amt");
194
195 // Insert spaces into the SourceLine.
196 SourceLine.insert(i+1, NumSpaces, ' ');
197
198 // Insert spaces or ~'s into CaretLine.
199 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
200 }
201
202 // Finally, remove any blank spaces from the end of CaretLine.
203 while (CaretLine[CaretLine.size()-1] == ' ')
204 CaretLine.erase(CaretLine.end()-1);
205
206 // Emit what we have computed.
207 OS << SourceLine << '\n';
208 OS << CaretLine << '\n';
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000209
Chris Lattneraa5bf2e2009-04-19 07:44:08 +0000210 if (NumHints && PrintFixItInfo) {
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000211 std::string InsertionLine;
Chris Lattneraa5bf2e2009-04-19 07:44:08 +0000212 for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints;
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000213 Hint != LastHint; ++Hint) {
214 if (Hint->InsertionLoc.isValid()) {
215 // We have an insertion hint. Determine whether the inserted
216 // code is on the same line as the caret.
217 std::pair<FileID, unsigned> HintLocInfo
Chris Lattner7b5b5b42009-03-02 20:58:48 +0000218 = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc);
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000219 if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) ==
220 SM.getLineNumber(FID, FileOffset)) {
221 // Insert the new code into the line just below the code
222 // that the user wrote.
223 unsigned HintColNo
224 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second);
225 unsigned LastColumnModified
226 = HintColNo - 1 + Hint->CodeToInsert.size();
227 if (LastColumnModified > InsertionLine.size())
228 InsertionLine.resize(LastColumnModified, ' ');
229 std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(),
230 InsertionLine.begin() + HintColNo - 1);
231 }
232 }
233 }
234
235 if (!InsertionLine.empty())
236 OS << InsertionLine << '\n';
237 }
Chris Lattner94f55782009-02-17 07:38:37 +0000238}
239
240
Chris Lattner0a14eee2008-11-18 07:04:44 +0000241void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
242 const DiagnosticInfo &Info) {
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000243 // If the location is specified, print out a file/line/col and include trace
244 // if enabled.
245 if (Info.getLocation().isValid()) {
Ted Kremenek05f39572009-01-28 20:47:47 +0000246 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000247 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
248 unsigned LineNo = PLoc.getLine();
Reid Spencer5f016e22007-07-11 17:01:13 +0000249
250 // First, if this diagnostic is not in the main file, print out the
251 // "included from" lines.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000252 if (LastWarningLoc != PLoc.getIncludeLoc()) {
253 LastWarningLoc = PLoc.getIncludeLoc();
254 PrintIncludeStack(LastWarningLoc, SM);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255 }
256
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000257 // Compute the column number.
Chris Lattnerb8bf65e2009-01-30 17:41:53 +0000258 if (ShowLocation) {
259 OS << PLoc.getFilename() << ':' << LineNo << ':';
Chris Lattner8f7b3962009-02-17 07:34:34 +0000260 if (ShowColumn)
261 if (unsigned ColNo = PLoc.getColumn())
262 OS << ColNo << ':';
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000263
264 if (PrintRangeInfo && Info.getNumRanges()) {
265 FileID CaretFileID =
266 SM.getFileID(SM.getInstantiationLoc(Info.getLocation()));
267 bool PrintedRange = false;
268
269 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) {
270 SourceLocation B = Info.getRange(i).getBegin();
271 SourceLocation E = Info.getRange(i).getEnd();
272 std::pair<FileID, unsigned> BInfo=SM.getDecomposedInstantiationLoc(B);
273
274 E = SM.getInstantiationLoc(E);
275 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
276
277 // If the start or end of the range is in another file, just discard
278 // it.
279 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
280 continue;
281
282 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner2c78b872009-04-14 23:22:57 +0000283 unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts);
Chris Lattner1fbee5d2009-03-13 01:08:23 +0000284
285 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
286 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
287 << SM.getLineNumber(EInfo.first, EInfo.second) << ':'
288 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}';
289 PrintedRange = true;
290 }
291
292 if (PrintedRange)
293 OS << ':';
294 }
Chris Lattnerb8bf65e2009-01-30 17:41:53 +0000295 OS << ' ';
296 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 }
298
299 switch (Level) {
Chris Lattner41327582009-02-06 03:57:44 +0000300 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman165b9542008-04-17 18:06:57 +0000301 case Diagnostic::Note: OS << "note: "; break;
302 case Diagnostic::Warning: OS << "warning: "; break;
303 case Diagnostic::Error: OS << "error: "; break;
Chris Lattner41327582009-02-06 03:57:44 +0000304 case Diagnostic::Fatal: OS << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 }
306
Chris Lattnerf4c83962008-11-19 06:51:40 +0000307 llvm::SmallString<100> OutStr;
308 Info.FormatDiagnostic(OutStr);
309 OS.write(OutStr.begin(), OutStr.size());
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000310
311 if (PrintDiagnosticOption)
312 if (const char *Option = Diagnostic::getWarningOptionForDiag(Info.getID()))
Chris Lattner19cbb442009-04-16 05:52:14 +0000313 OS << " [-W" << Option << ']';
Chris Lattnerd51d74a2009-04-16 05:44:38 +0000314
Chris Lattnerf4c83962008-11-19 06:51:40 +0000315 OS << '\n';
Reid Spencer5f016e22007-07-11 17:01:13 +0000316
Douglas Gregordf667e72009-03-10 20:44:00 +0000317 // If caret diagnostics are enabled and we have location, we want to
318 // emit the caret. However, we only do this if the location moved
319 // from the last diagnostic, if the last diagnostic was a note that
320 // was part of a different warning or error diagnostic, or if the
321 // diagnostic has ranges. We don't want to emit the same caret
322 // multiple times if one loc has multiple diagnostics.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000323 if (CaretDiagnostics && Info.getLocation().isValid() &&
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000324 ((LastLoc != Info.getLocation()) || Info.getNumRanges() ||
Douglas Gregordf667e72009-03-10 20:44:00 +0000325 (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) ||
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000326 Info.getNumCodeModificationHints())) {
Steve Naroffefe7f362008-02-08 22:06:17 +0000327 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000328 LastLoc = Info.getLocation();
Douglas Gregordf667e72009-03-10 20:44:00 +0000329 LastCaretDiagnosticWasNote = (Level == Diagnostic::Note);
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000330
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000331 // Get the ranges into a local array we can hack on.
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000332 SourceRange Ranges[20];
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000333 unsigned NumRanges = Info.getNumRanges();
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000334 assert(NumRanges < 20 && "Out of space");
Chris Lattnerebbbb1b2009-02-20 00:18:51 +0000335 for (unsigned i = 0; i != NumRanges; ++i)
336 Ranges[i] = Info.getRange(i);
337
Douglas Gregor4b2d3f72009-02-26 21:00:50 +0000338 unsigned NumHints = Info.getNumCodeModificationHints();
339 for (unsigned idx = 0; idx < NumHints; ++idx) {
340 const CodeModificationHint &Hint = Info.getCodeModificationHint(idx);
341 if (Hint.RemoveRange.isValid()) {
342 assert(NumRanges < 20 && "Out of space");
343 Ranges[NumRanges++] = Hint.RemoveRange;
344 }
345 }
346
347 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
348 Info.getCodeModificationHints(),
349 Info.getNumCodeModificationHints());
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 }
Chris Lattnera03a5b52008-11-19 06:56:25 +0000351
352 OS.flush();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353}