blob: 1f98ef91901d4a77ae09ee7d6bfe4174053bd9c8 [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
Nico Weber0e13eaa2008-08-05 23:33:20 +000014#include "clang/Driver/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 Lattner4b009652007-07-25 00:24:17 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerbe8e5a42008-11-19 06:51:40 +000019#include "llvm/ADT/SmallString.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Chris Lattner4b009652007-07-25 00:24:17 +000022void TextDiagnosticPrinter::
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000023PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner4b009652007-07-25 00:24:17 +000024 if (Pos.isInvalid()) return;
25
Chris Lattner18c8dc02009-01-16 07:36:28 +000026 Pos = Pos.getInstantiationLoc();
Chris Lattner4b009652007-07-25 00:24:17 +000027
28 // Print out the other include frames first.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000029 PrintIncludeStack(Pos.getIncludeLoc());
30 unsigned LineNo = Pos.getLineNumber();
Chris Lattner4b009652007-07-25 00:24:17 +000031
Nate Begeman01d74272008-04-17 18:06:57 +000032 OS << "In file included from " << Pos.getSourceName()
Chris Lattner92a33532008-11-19 06:56:25 +000033 << ':' << LineNo << ":\n";
Chris Lattner4b009652007-07-25 00:24:17 +000034}
35
36/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
37/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekb3ee1932007-12-11 21:27:55 +000038void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattner6948ae62008-11-18 07:04:44 +000039 const SourceManager& SourceMgr,
Chris Lattnera0030d22008-01-12 06:43:35 +000040 unsigned LineNo, unsigned FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000041 std::string &CaretLine,
Nuno Lopesd0e162c2008-08-05 19:40:20 +000042 const std::string &SourceLine) {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000043 assert(CaretLine.size() == SourceLine.size() &&
44 "Expect a correspondence between source and caret line!");
Chris Lattner4b009652007-07-25 00:24:17 +000045 if (!R.isValid()) return;
46
Chris Lattner18c8dc02009-01-16 07:36:28 +000047 SourceLocation InstantiationStart =
48 SourceMgr.getInstantiationLoc(R.getBegin());
49 unsigned StartLineNo = SourceMgr.getLineNumber(InstantiationStart);
50 if (StartLineNo > LineNo || InstantiationStart.getFileID() != FileID)
Chris Lattnera0030d22008-01-12 06:43:35 +000051 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000052
Chris Lattner18c8dc02009-01-16 07:36:28 +000053 SourceLocation InstantiationEnd = SourceMgr.getInstantiationLoc(R.getEnd());
54 unsigned EndLineNo = SourceMgr.getLineNumber(InstantiationEnd);
55 if (EndLineNo < LineNo || InstantiationEnd.getFileID() != FileID)
Chris Lattnera0030d22008-01-12 06:43:35 +000056 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000057
58 // Compute the column number of the start.
59 unsigned StartColNo = 0;
60 if (StartLineNo == LineNo) {
Chris Lattner18c8dc02009-01-16 07:36:28 +000061 StartColNo = SourceMgr.getInstantiationColumnNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +000062 if (StartColNo) --StartColNo; // Zero base the col #.
63 }
64
65 // Pick the first non-whitespace column.
66 while (StartColNo < SourceLine.size() &&
67 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
68 ++StartColNo;
69
70 // Compute the column number of the end.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000071 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000072 if (EndLineNo == LineNo) {
Chris Lattner18c8dc02009-01-16 07:36:28 +000073 EndColNo = SourceMgr.getInstantiationColumnNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +000074 if (EndColNo) {
75 --EndColNo; // Zero base the col #.
76
77 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000078 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +000079 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000080 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000081 }
82 }
83
84 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-08-05 19:40:20 +000085 if (EndColNo <= SourceLine.size())
86 while (EndColNo-1 &&
87 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
88 --EndColNo;
89 else
90 EndColNo = SourceLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000091
92 // Fill the range with ~'s.
93 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +000094 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000095 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +000096}
97
Chris Lattner6948ae62008-11-18 07:04:44 +000098void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
99 const DiagnosticInfo &Info) {
Chris Lattner4b009652007-07-25 00:24:17 +0000100 unsigned LineNo = 0, ColNo = 0;
Chris Lattnera0030d22008-01-12 06:43:35 +0000101 unsigned FileID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000102 const char *LineStart = 0, *LineEnd = 0;
Chris Lattner6948ae62008-11-18 07:04:44 +0000103 const FullSourceLoc &Pos = Info.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000104
105 if (Pos.isValid()) {
Chris Lattner18c8dc02009-01-16 07:36:28 +0000106 FullSourceLoc LPos = Pos.getInstantiationLoc();
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000107 LineNo = LPos.getLineNumber();
Chris Lattnera0030d22008-01-12 06:43:35 +0000108 FileID = LPos.getLocation().getFileID();
Chris Lattner4b009652007-07-25 00:24:17 +0000109
110 // First, if this diagnostic is not in the main file, print out the
111 // "included from" lines.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000112 if (LastWarningLoc != LPos.getIncludeLoc()) {
113 LastWarningLoc = LPos.getIncludeLoc();
114 PrintIncludeStack(LastWarningLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000115 }
116
117 // Compute the column number. Rewind from the current position to the start
118 // of the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000119 ColNo = LPos.getColumnNumber();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000120 const char *TokInstantiationPtr = LPos.getCharacterData();
121 LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based
Chris Lattner83343342008-01-25 00:01:10 +0000122
Chris Lattner4b009652007-07-25 00:24:17 +0000123 // Compute the line end. Scan forward from the error position to the end of
124 // the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000125 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000126 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000127 LineEnd = TokInstantiationPtr;
Chris Lattner4b009652007-07-25 00:24:17 +0000128 while (LineEnd != BufEnd &&
129 *LineEnd != '\n' && *LineEnd != '\r')
130 ++LineEnd;
131
Chris Lattner92a33532008-11-19 06:56:25 +0000132 OS << Buffer->getBufferIdentifier() << ':' << LineNo << ':';
Nico Weber0e13eaa2008-08-05 23:33:20 +0000133 if (ColNo && ShowColumn)
Chris Lattner92a33532008-11-19 06:56:25 +0000134 OS << ColNo << ':';
135 OS << ' ';
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
137
138 switch (Level) {
139 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman01d74272008-04-17 18:06:57 +0000140 case Diagnostic::Note: OS << "note: "; break;
141 case Diagnostic::Warning: OS << "warning: "; break;
142 case Diagnostic::Error: OS << "error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000143 }
144
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000145 llvm::SmallString<100> OutStr;
146 Info.FormatDiagnostic(OutStr);
147 OS.write(OutStr.begin(), OutStr.size());
148 OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000149
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000150 if (CaretDiagnostics && Pos.isValid() &&
151 ((LastLoc != Pos) || Info.getNumRanges())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000152 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
153 LastLoc = Pos;
154
Chris Lattner4b009652007-07-25 00:24:17 +0000155 // Get the line of the source file.
156 std::string SourceLine(LineStart, LineEnd);
157
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000158 // Create a line for the caret that is filled with spaces that is the same
Chris Lattner4b009652007-07-25 00:24:17 +0000159 // length as the line of source code.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000160 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000161
162 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner6948ae62008-11-18 07:04:44 +0000163 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
164 HighlightRange(Info.getRange(i), Pos.getManager(), LineNo, FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000165 CaretLine, SourceLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000166
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000167 // Next, insert the caret itself.
168 if (ColNo-1 < CaretLine.size())
169 CaretLine[ColNo-1] = '^';
Chris Lattner4b009652007-07-25 00:24:17 +0000170 else
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000171 CaretLine.push_back('^');
Chris Lattner4b009652007-07-25 00:24:17 +0000172
173 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000174 // them to 8 characters and update the CaretLine to match.
Chris Lattner4b009652007-07-25 00:24:17 +0000175 for (unsigned i = 0; i != SourceLine.size(); ++i) {
176 if (SourceLine[i] != '\t') continue;
177
178 // Replace this tab with at least one space.
179 SourceLine[i] = ' ';
180
181 // Compute the number of spaces we need to insert.
182 unsigned NumSpaces = ((i+8)&~7) - (i+1);
183 assert(NumSpaces < 8 && "Invalid computation of space amt");
184
185 // Insert spaces into the SourceLine.
186 SourceLine.insert(i+1, NumSpaces, ' ');
187
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000188 // Insert spaces or ~'s into CaretLine.
189 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000190 }
191
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000192 // Finally, remove any blank spaces from the end of CaretLine.
193 while (CaretLine[CaretLine.size()-1] == ' ')
194 CaretLine.erase(CaretLine.end()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000195
196 // Emit what we have computed.
Chris Lattner92a33532008-11-19 06:56:25 +0000197 OS << SourceLine << '\n';
198 OS << CaretLine << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000199 }
Chris Lattner92a33532008-11-19 06:56:25 +0000200
201 OS.flush();
Chris Lattner4b009652007-07-25 00:24:17 +0000202}