blob: 71a5e401075b3281b9d7fcaac5a21f4ac2356232 [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"
Bill Wendling37b1dde2007-06-07 09:34:54 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattner23be0672008-11-19 06:51:40 +000019#include "llvm/ADT/SmallString.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000020using namespace clang;
21
Bill Wendling37b1dde2007-06-07 09:34:54 +000022void TextDiagnosticPrinter::
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000023PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +000024 if (Pos.isInvalid()) return;
25
Chris Lattner8a425862009-01-16 07:36:28 +000026 Pos = Pos.getInstantiationLoc();
Chris Lattnerdc5c0552007-07-20 16:37:10 +000027
Bill Wendling37b1dde2007-06-07 09:34:54 +000028 // Print out the other include frames first.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000029 PrintIncludeStack(Pos.getIncludeLoc());
30 unsigned LineNo = Pos.getLineNumber();
Bill Wendling37b1dde2007-06-07 09:34:54 +000031
Nate Begeman36c49272008-04-17 18:06:57 +000032 OS << "In file included from " << Pos.getSourceName()
Chris Lattner327984f2008-11-19 06:56:25 +000033 << ':' << LineNo << ":\n";
Bill Wendling37b1dde2007-06-07 09:34:54 +000034}
35
Bill Wendling37b1dde2007-06-07 09:34:54 +000036/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
37/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +000038void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattner71dc14b2009-01-17 08:45:21 +000039 const SourceManager &SourceMgr,
40 unsigned LineNo, FileID FID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000041 std::string &CaretLine,
Nuno Lopesd86aa932008-08-05 19:40:20 +000042 const std::string &SourceLine) {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000043 assert(CaretLine.size() == SourceLine.size() &&
44 "Expect a correspondence between source and caret line!");
Bill Wendling37b1dde2007-06-07 09:34:54 +000045 if (!R.isValid()) return;
46
Chris Lattner8a425862009-01-16 07:36:28 +000047 SourceLocation InstantiationStart =
48 SourceMgr.getInstantiationLoc(R.getBegin());
49 unsigned StartLineNo = SourceMgr.getLineNumber(InstantiationStart);
Chris Lattner71dc14b2009-01-17 08:45:21 +000050 if (StartLineNo > LineNo ||
Chris Lattnercbc35ecb2009-01-19 07:46:45 +000051 SourceMgr.getFileID(InstantiationStart) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000052 return; // No intersection.
Bill Wendling37b1dde2007-06-07 09:34:54 +000053
Chris Lattner8a425862009-01-16 07:36:28 +000054 SourceLocation InstantiationEnd = SourceMgr.getInstantiationLoc(R.getEnd());
55 unsigned EndLineNo = SourceMgr.getLineNumber(InstantiationEnd);
Chris Lattner71dc14b2009-01-17 08:45:21 +000056 if (EndLineNo < LineNo ||
Chris Lattnercbc35ecb2009-01-19 07:46:45 +000057 SourceMgr.getFileID(InstantiationEnd) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000058 return; // No intersection.
Bill Wendling37b1dde2007-06-07 09:34:54 +000059
60 // Compute the column number of the start.
61 unsigned StartColNo = 0;
62 if (StartLineNo == LineNo) {
Chris Lattner8a425862009-01-16 07:36:28 +000063 StartColNo = SourceMgr.getInstantiationColumnNumber(R.getBegin());
Bill Wendling37b1dde2007-06-07 09:34:54 +000064 if (StartColNo) --StartColNo; // Zero base the col #.
65 }
66
67 // Pick the first non-whitespace column.
68 while (StartColNo < SourceLine.size() &&
69 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
70 ++StartColNo;
71
72 // Compute the column number of the end.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000073 unsigned EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000074 if (EndLineNo == LineNo) {
Chris Lattner8a425862009-01-16 07:36:28 +000075 EndColNo = SourceMgr.getInstantiationColumnNumber(R.getEnd());
Bill Wendling37b1dde2007-06-07 09:34:54 +000076 if (EndColNo) {
77 --EndColNo; // Zero base the col #.
78
79 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000080 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Bill Wendling37b1dde2007-06-07 09:34:54 +000081 } else {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000082 EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000083 }
84 }
85
86 // Pick the last non-whitespace column.
Nuno Lopesd86aa932008-08-05 19:40:20 +000087 if (EndColNo <= SourceLine.size())
88 while (EndColNo-1 &&
89 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
90 --EndColNo;
91 else
92 EndColNo = SourceLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000093
94 // Fill the range with ~'s.
95 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd86aa932008-08-05 19:40:20 +000096 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000097 CaretLine[i] = '~';
Bill Wendling37b1dde2007-06-07 09:34:54 +000098}
99
Chris Lattner8488c822008-11-18 07:04:44 +0000100void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
101 const DiagnosticInfo &Info) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000102 unsigned LineNo = 0, ColNo = 0;
Chris Lattner71dc14b2009-01-17 08:45:21 +0000103 FileID FID;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000104 const char *LineStart = 0, *LineEnd = 0;
Chris Lattner8488c822008-11-18 07:04:44 +0000105 const FullSourceLoc &Pos = Info.getLocation();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000106
107 if (Pos.isValid()) {
Chris Lattner8a425862009-01-16 07:36:28 +0000108 FullSourceLoc LPos = Pos.getInstantiationLoc();
Chris Lattner71dc14b2009-01-17 08:45:21 +0000109 FID = LPos.getFileID();
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000110 LineNo = LPos.getLineNumber();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000111
Bill Wendling37b1dde2007-06-07 09:34:54 +0000112 // First, if this diagnostic is not in the main file, print out the
113 // "included from" lines.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000114 if (LastWarningLoc != LPos.getIncludeLoc()) {
115 LastWarningLoc = LPos.getIncludeLoc();
116 PrintIncludeStack(LastWarningLoc);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000117 }
118
119 // Compute the column number. Rewind from the current position to the start
120 // of the line.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000121 ColNo = LPos.getColumnNumber();
Chris Lattner8a425862009-01-16 07:36:28 +0000122 const char *TokInstantiationPtr = LPos.getCharacterData();
123 LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based
Chris Lattner3efff542008-01-25 00:01:10 +0000124
Bill Wendling37b1dde2007-06-07 09:34:54 +0000125 // Compute the line end. Scan forward from the error position to the end of
126 // the line.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000127 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000128 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner8a425862009-01-16 07:36:28 +0000129 LineEnd = TokInstantiationPtr;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000130 while (LineEnd != BufEnd &&
131 *LineEnd != '\n' && *LineEnd != '\r')
Bill Wendling37b1dde2007-06-07 09:34:54 +0000132 ++LineEnd;
133
Chris Lattner327984f2008-11-19 06:56:25 +0000134 OS << Buffer->getBufferIdentifier() << ':' << LineNo << ':';
Nico Weberb5fc3c32008-08-05 23:33:20 +0000135 if (ColNo && ShowColumn)
Chris Lattner327984f2008-11-19 06:56:25 +0000136 OS << ColNo << ':';
137 OS << ' ';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000138 }
139
140 switch (Level) {
141 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman36c49272008-04-17 18:06:57 +0000142 case Diagnostic::Note: OS << "note: "; break;
143 case Diagnostic::Warning: OS << "warning: "; break;
144 case Diagnostic::Error: OS << "error: "; break;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000145 }
146
Chris Lattner23be0672008-11-19 06:51:40 +0000147 llvm::SmallString<100> OutStr;
148 Info.FormatDiagnostic(OutStr);
149 OS.write(OutStr.begin(), OutStr.size());
150 OS << '\n';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000151
Chris Lattner23be0672008-11-19 06:51:40 +0000152 if (CaretDiagnostics && Pos.isValid() &&
153 ((LastLoc != Pos) || Info.getNumRanges())) {
Steve Naroffa36992242008-02-08 22:06:17 +0000154 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
155 LastLoc = Pos;
156
Bill Wendling37b1dde2007-06-07 09:34:54 +0000157 // Get the line of the source file.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000158 std::string SourceLine(LineStart, LineEnd);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000159
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000160 // Create a line for the caret that is filled with spaces that is the same
Bill Wendling37b1dde2007-06-07 09:34:54 +0000161 // length as the line of source code.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000162 std::string CaretLine(LineEnd-LineStart, ' ');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000163
164 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner8488c822008-11-18 07:04:44 +0000165 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
Chris Lattner71dc14b2009-01-17 08:45:21 +0000166 HighlightRange(Info.getRange(i), Pos.getManager(), LineNo, FID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000167 CaretLine, SourceLine);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000168
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000169 // Next, insert the caret itself.
170 if (ColNo-1 < CaretLine.size())
171 CaretLine[ColNo-1] = '^';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000172 else
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000173 CaretLine.push_back('^');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000174
175 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000176 // them to 8 characters and update the CaretLine to match.
Bill Wendling37b1dde2007-06-07 09:34:54 +0000177 for (unsigned i = 0; i != SourceLine.size(); ++i) {
178 if (SourceLine[i] != '\t') continue;
179
180 // Replace this tab with at least one space.
181 SourceLine[i] = ' ';
182
183 // Compute the number of spaces we need to insert.
184 unsigned NumSpaces = ((i+8)&~7) - (i+1);
185 assert(NumSpaces < 8 && "Invalid computation of space amt");
186
187 // Insert spaces into the SourceLine.
188 SourceLine.insert(i+1, NumSpaces, ' ');
189
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000190 // Insert spaces or ~'s into CaretLine.
191 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000192 }
193
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000194 // Finally, remove any blank spaces from the end of CaretLine.
195 while (CaretLine[CaretLine.size()-1] == ' ')
196 CaretLine.erase(CaretLine.end()-1);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000197
198 // Emit what we have computed.
Chris Lattner327984f2008-11-19 06:56:25 +0000199 OS << SourceLine << '\n';
200 OS << CaretLine << '\n';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000201 }
Chris Lattner327984f2008-11-19 06:56:25 +0000202
203 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000204}