blob: b5226f5131e2c1a151bc1e4b52735022397dfda3 [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
Nico Weberb5fc3c32008-08-05 23:33:20 +000014#include "clang/Driver/TextDiagnosticPrinter.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000015#include "clang/Basic/SourceManager.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000016#include "clang/Lex/Lexer.h"
Chris Lattner327984f2008-11-19 06:56:25 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattner23be0672008-11-19 06:51:40 +000018#include "llvm/ADT/SmallString.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000019using namespace clang;
20
Bill Wendling37b1dde2007-06-07 09:34:54 +000021void TextDiagnosticPrinter::
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000022PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
23 if (Loc.isInvalid()) return;
Chris Lattnerdc5c0552007-07-20 16:37:10 +000024
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000025 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattnerdc5c0552007-07-20 16:37:10 +000026
Bill Wendling37b1dde2007-06-07 09:34:54 +000027 // Print out the other include frames first.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000028 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Bill Wendling37b1dde2007-06-07 09:34:54 +000029
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000030 OS << "In file included from " << PLoc.getFilename()
31 << ':' << PLoc.getLine() << ":\n";
Bill Wendling37b1dde2007-06-07 09:34:54 +000032}
33
Bill Wendling37b1dde2007-06-07 09:34:54 +000034/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
35/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +000036void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000037 const SourceManager &SM,
Chris Lattner71dc14b2009-01-17 08:45:21 +000038 unsigned LineNo, FileID FID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000039 std::string &CaretLine,
Nuno Lopesd86aa932008-08-05 19:40:20 +000040 const std::string &SourceLine) {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000041 assert(CaretLine.size() == SourceLine.size() &&
42 "Expect a correspondence between source and caret line!");
Bill Wendling37b1dde2007-06-07 09:34:54 +000043 if (!R.isValid()) return;
44
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000045 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
46 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
47
Chris Lattnerb42700f2009-02-17 05:19:10 +000048 // If the End location and the start location are the same and are a macro
49 // location, then the range was something that came from a macro expansion
50 // or _Pragma. If this is an object-like macro, the best we can do is to
51 // highlight the range. If this is a function-like macro, we'd also like to
52 // highlight the arguments.
53 if (Begin == End && R.getEnd().isMacroID())
54 End = SM.getInstantiationRange(R.getEnd()).second;
55
Chris Lattner88ea93e2009-02-04 01:06:56 +000056 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000057 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000058 return; // No intersection.
Bill Wendling37b1dde2007-06-07 09:34:54 +000059
Chris Lattner88ea93e2009-02-04 01:06:56 +000060 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000061 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000062 return; // No intersection.
Bill Wendling37b1dde2007-06-07 09:34:54 +000063
64 // Compute the column number of the start.
65 unsigned StartColNo = 0;
66 if (StartLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +000067 StartColNo = SM.getInstantiationColumnNumber(Begin);
Bill Wendling37b1dde2007-06-07 09:34:54 +000068 if (StartColNo) --StartColNo; // Zero base the col #.
69 }
70
71 // Pick the first non-whitespace column.
72 while (StartColNo < SourceLine.size() &&
73 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
74 ++StartColNo;
75
76 // Compute the column number of the end.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000077 unsigned EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000078 if (EndLineNo == LineNo) {
Chris Lattnere4ad4172009-02-04 00:55:58 +000079 EndColNo = SM.getInstantiationColumnNumber(End);
Bill Wendling37b1dde2007-06-07 09:34:54 +000080 if (EndColNo) {
81 --EndColNo; // Zero base the col #.
82
83 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000084 EndColNo += Lexer::MeasureTokenLength(End, SM);
Bill Wendling37b1dde2007-06-07 09:34:54 +000085 } else {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000086 EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000087 }
88 }
89
90 // Pick the last non-whitespace column.
Nuno Lopesd86aa932008-08-05 19:40:20 +000091 if (EndColNo <= SourceLine.size())
92 while (EndColNo-1 &&
93 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
94 --EndColNo;
95 else
96 EndColNo = SourceLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000097
98 // Fill the range with ~'s.
99 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd86aa932008-08-05 19:40:20 +0000100 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000101 CaretLine[i] = '~';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000102}
103
Chris Lattner1973d842009-02-20 00:18:51 +0000104void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc,
Chris Lattner464ceb42009-02-20 00:25:28 +0000105 SourceRange *Ranges,
Chris Lattner1973d842009-02-20 00:18:51 +0000106 unsigned NumRanges,
Chris Lattnerd22603c2009-02-17 07:51:53 +0000107 SourceManager &SM) {
Chris Lattner7b60a162009-02-17 08:44:50 +0000108 assert(!Loc.isInvalid() && "must have a valid source location here");
109
Chris Lattner5abfe972009-02-17 07:54:55 +0000110 // We always emit diagnostics about the instantiation points, not the spelling
111 // points. This more closely correlates to what the user writes.
Chris Lattner7b60a162009-02-17 08:44:50 +0000112 if (!Loc.isFileID()) {
Chris Lattner24eb28b2009-02-18 18:50:45 +0000113 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first;
Chris Lattner1973d842009-02-20 00:18:51 +0000114 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM);
Chris Lattner7b60a162009-02-17 08:44:50 +0000115
Chris Lattner464ceb42009-02-20 00:25:28 +0000116 // Map the location through the macro.
Chris Lattner7b60a162009-02-17 08:44:50 +0000117 Loc = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(Loc));
Chris Lattner464ceb42009-02-20 00:25:28 +0000118
119 // Map the ranges.
120 for (unsigned i = 0; i != NumRanges; ++i) {
121 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd();
122 if (S.isMacroID())
123 S = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(S));
124 if (E.isMacroID())
125 E = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(E));
126 Ranges[i] = SourceRange(S, E);
127 }
Chris Lattner7b60a162009-02-17 08:44:50 +0000128
129 // Emit the file/line/column that this expansion came from.
130 OS << SM.getBufferName(Loc) << ':' << SM.getInstantiationLineNumber(Loc)
131 << ':';
132 if (ShowColumn)
133 OS << SM.getInstantiationColumnNumber(Loc) << ':';
134 OS << " note: instantiated from:\n";
135 }
Chris Lattnerd22603c2009-02-17 07:51:53 +0000136
137 // Decompose the location into a FID/Offset pair.
138 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
139 FileID FID = LocInfo.first;
140 unsigned FileOffset = LocInfo.second;
141
142 // Get information about the buffer it points into.
143 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID);
144 const char *BufStart = BufferInfo.first;
145 const char *BufEnd = BufferInfo.second;
146
147 unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
Chris Lattner2cf68c12009-02-17 07:38:37 +0000148
149 // Rewind from the current position to the start of the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000150 const char *TokPtr = BufStart+FileOffset;
151 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
152
Chris Lattner2cf68c12009-02-17 07:38:37 +0000153
154 // Compute the line end. Scan forward from the error position to the end of
155 // the line.
Chris Lattnerd22603c2009-02-17 07:51:53 +0000156 const char *LineEnd = TokPtr;
Chris Lattner2cf68c12009-02-17 07:38:37 +0000157 while (LineEnd != BufEnd &&
158 *LineEnd != '\n' && *LineEnd != '\r')
159 ++LineEnd;
160
161 // Copy the line of code into an std::string for ease of manipulation.
162 std::string SourceLine(LineStart, LineEnd);
163
164 // Create a line for the caret that is filled with spaces that is the same
165 // length as the line of source code.
166 std::string CaretLine(LineEnd-LineStart, ' ');
167
168 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner1973d842009-02-20 00:18:51 +0000169 if (NumRanges) {
Chris Lattnerd22603c2009-02-17 07:51:53 +0000170 unsigned LineNo = SM.getLineNumber(FID, FileOffset);
171
Chris Lattner1973d842009-02-20 00:18:51 +0000172 for (unsigned i = 0, e = NumRanges; i != e; ++i)
173 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine);
Chris Lattnerd22603c2009-02-17 07:51:53 +0000174 }
Chris Lattner2cf68c12009-02-17 07:38:37 +0000175
176 // Next, insert the caret itself.
177 if (ColNo-1 < CaretLine.size())
178 CaretLine[ColNo-1] = '^';
179 else
180 CaretLine.push_back('^');
181
182 // Scan the source line, looking for tabs. If we find any, manually expand
183 // them to 8 characters and update the CaretLine to match.
184 for (unsigned i = 0; i != SourceLine.size(); ++i) {
185 if (SourceLine[i] != '\t') continue;
186
187 // Replace this tab with at least one space.
188 SourceLine[i] = ' ';
189
190 // Compute the number of spaces we need to insert.
191 unsigned NumSpaces = ((i+8)&~7) - (i+1);
192 assert(NumSpaces < 8 && "Invalid computation of space amt");
193
194 // Insert spaces into the SourceLine.
195 SourceLine.insert(i+1, NumSpaces, ' ');
196
197 // Insert spaces or ~'s into CaretLine.
198 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
199 }
200
201 // Finally, remove any blank spaces from the end of CaretLine.
202 while (CaretLine[CaretLine.size()-1] == ' ')
203 CaretLine.erase(CaretLine.end()-1);
204
205 // Emit what we have computed.
206 OS << SourceLine << '\n';
207 OS << CaretLine << '\n';
208}
209
210
Chris Lattner8488c822008-11-18 07:04:44 +0000211void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
212 const DiagnosticInfo &Info) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000213 // If the location is specified, print out a file/line/col and include trace
214 // if enabled.
215 if (Info.getLocation().isValid()) {
Ted Kremenek52418322009-01-28 20:47:47 +0000216 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000217 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
218 unsigned LineNo = PLoc.getLine();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000219
Bill Wendling37b1dde2007-06-07 09:34:54 +0000220 // First, if this diagnostic is not in the main file, print out the
221 // "included from" lines.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000222 if (LastWarningLoc != PLoc.getIncludeLoc()) {
223 LastWarningLoc = PLoc.getIncludeLoc();
224 PrintIncludeStack(LastWarningLoc, SM);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000225 }
226
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000227 // Compute the column number.
Chris Lattner19586772009-01-30 17:41:53 +0000228 if (ShowLocation) {
229 OS << PLoc.getFilename() << ':' << LineNo << ':';
Chris Lattner11a2a432009-02-17 07:34:34 +0000230 if (ShowColumn)
231 if (unsigned ColNo = PLoc.getColumn())
232 OS << ColNo << ':';
Chris Lattner19586772009-01-30 17:41:53 +0000233 OS << ' ';
234 }
Bill Wendling37b1dde2007-06-07 09:34:54 +0000235 }
236
237 switch (Level) {
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000238 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman36c49272008-04-17 18:06:57 +0000239 case Diagnostic::Note: OS << "note: "; break;
240 case Diagnostic::Warning: OS << "warning: "; break;
241 case Diagnostic::Error: OS << "error: "; break;
Chris Lattnerb05f49e2009-02-06 03:57:44 +0000242 case Diagnostic::Fatal: OS << "fatal error: "; break;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000243 }
244
Chris Lattner23be0672008-11-19 06:51:40 +0000245 llvm::SmallString<100> OutStr;
246 Info.FormatDiagnostic(OutStr);
247 OS.write(OutStr.begin(), OutStr.size());
248 OS << '\n';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000249
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000250 // If caret diagnostics are enabled and we have location, we want to emit the
251 // caret. However, we only do this if the location moved from the last
252 // diagnostic, or if the diagnostic has ranges. We don't want to emit the
253 // same caret multiple times if one loc has multiple diagnostics.
254 if (CaretDiagnostics && Info.getLocation().isValid() &&
255 ((LastLoc != Info.getLocation()) || Info.getNumRanges())) {
Steve Naroffa36992242008-02-08 22:06:17 +0000256 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000257 LastLoc = Info.getLocation();
258
Chris Lattner1973d842009-02-20 00:18:51 +0000259 // Get the ranges into a local array we can hack on.
260 SourceRange Ranges[10];
261 unsigned NumRanges = Info.getNumRanges();
262 assert(NumRanges < 10 && "Out of space");
263 for (unsigned i = 0; i != NumRanges; ++i)
264 Ranges[i] = Info.getRange(i);
265
266 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager());
Bill Wendling37b1dde2007-06-07 09:34:54 +0000267 }
Chris Lattner327984f2008-11-19 06:56:25 +0000268
269 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000270}