blob: 3b8ec10ec81c7323b19764b8275142a2c4f95c89 [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 Lattner4b009652007-07-25 00:24:17 +000017#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018using namespace clang;
19
Chris Lattner4b009652007-07-25 00:24:17 +000020void TextDiagnosticPrinter::
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000021PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner4b009652007-07-25 00:24:17 +000022 if (Pos.isInvalid()) return;
23
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000024 Pos = Pos.getLogicalLoc();
Chris Lattner4b009652007-07-25 00:24:17 +000025
26 // Print out the other include frames first.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000027 PrintIncludeStack(Pos.getIncludeLoc());
28 unsigned LineNo = Pos.getLineNumber();
Chris Lattner4b009652007-07-25 00:24:17 +000029
Nate Begeman01d74272008-04-17 18:06:57 +000030 OS << "In file included from " << Pos.getSourceName()
31 << ":" << LineNo << ":\n";
Chris Lattner4b009652007-07-25 00:24:17 +000032}
33
34/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
35/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekb3ee1932007-12-11 21:27:55 +000036void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattner6948ae62008-11-18 07:04:44 +000037 const SourceManager& SourceMgr,
Chris Lattnera0030d22008-01-12 06:43:35 +000038 unsigned LineNo, unsigned FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000039 std::string &CaretLine,
Nuno Lopesd0e162c2008-08-05 19:40:20 +000040 const std::string &SourceLine) {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000041 assert(CaretLine.size() == SourceLine.size() &&
42 "Expect a correspondence between source and caret line!");
Chris Lattner4b009652007-07-25 00:24:17 +000043 if (!R.isValid()) return;
44
Chris Lattnera0030d22008-01-12 06:43:35 +000045 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(R.getBegin());
46 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
47 if (StartLineNo > LineNo || LogicalStart.getFileID() != FileID)
48 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000049
Chris Lattnera0030d22008-01-12 06:43:35 +000050 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
51 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek1840dbd2008-03-27 05:52:45 +000052 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnera0030d22008-01-12 06:43:35 +000053 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000054
55 // Compute the column number of the start.
56 unsigned StartColNo = 0;
57 if (StartLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000058 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +000059 if (StartColNo) --StartColNo; // Zero base the col #.
60 }
61
62 // Pick the first non-whitespace column.
63 while (StartColNo < SourceLine.size() &&
64 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
65 ++StartColNo;
66
67 // Compute the column number of the end.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000068 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000069 if (EndLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000070 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +000071 if (EndColNo) {
72 --EndColNo; // Zero base the col #.
73
74 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000075 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +000076 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000077 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000078 }
79 }
80
81 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-08-05 19:40:20 +000082 if (EndColNo <= SourceLine.size())
83 while (EndColNo-1 &&
84 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
85 --EndColNo;
86 else
87 EndColNo = SourceLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000088
89 // Fill the range with ~'s.
90 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +000091 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000092 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +000093}
94
Chris Lattner6948ae62008-11-18 07:04:44 +000095void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
96 const DiagnosticInfo &Info) {
Chris Lattner4b009652007-07-25 00:24:17 +000097 unsigned LineNo = 0, ColNo = 0;
Chris Lattnera0030d22008-01-12 06:43:35 +000098 unsigned FileID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000099 const char *LineStart = 0, *LineEnd = 0;
Chris Lattner6948ae62008-11-18 07:04:44 +0000100 const FullSourceLoc &Pos = Info.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000101
102 if (Pos.isValid()) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000103 FullSourceLoc LPos = Pos.getLogicalLoc();
104 LineNo = LPos.getLineNumber();
Chris Lattnera0030d22008-01-12 06:43:35 +0000105 FileID = LPos.getLocation().getFileID();
Chris Lattner4b009652007-07-25 00:24:17 +0000106
107 // First, if this diagnostic is not in the main file, print out the
108 // "included from" lines.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000109 if (LastWarningLoc != LPos.getIncludeLoc()) {
110 LastWarningLoc = LPos.getIncludeLoc();
111 PrintIncludeStack(LastWarningLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000112 }
113
114 // Compute the column number. Rewind from the current position to the start
115 // of the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000116 ColNo = LPos.getColumnNumber();
117 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner4b009652007-07-25 00:24:17 +0000118 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner83343342008-01-25 00:01:10 +0000119
Chris Lattner4b009652007-07-25 00:24:17 +0000120 // Compute the line end. Scan forward from the error position to the end of
121 // the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000122 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000123 const char *BufEnd = Buffer->getBufferEnd();
124 LineEnd = TokLogicalPtr;
125 while (LineEnd != BufEnd &&
126 *LineEnd != '\n' && *LineEnd != '\r')
127 ++LineEnd;
128
Nate Begeman01d74272008-04-17 18:06:57 +0000129 OS << Buffer->getBufferIdentifier() << ":" << LineNo << ":";
Nico Weber0e13eaa2008-08-05 23:33:20 +0000130 if (ColNo && ShowColumn)
Nate Begeman01d74272008-04-17 18:06:57 +0000131 OS << ColNo << ":";
132 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000133 }
134
135 switch (Level) {
136 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman01d74272008-04-17 18:06:57 +0000137 case Diagnostic::Note: OS << "note: "; break;
138 case Diagnostic::Warning: OS << "warning: "; break;
139 case Diagnostic::Error: OS << "error: "; break;
140 case Diagnostic::Fatal: OS << "fatal error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000141 break;
142 }
143
Chris Lattner6948ae62008-11-18 07:04:44 +0000144 OS << FormatDiagnostic(Info) << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000145
Chris Lattner6948ae62008-11-18 07:04:44 +0000146 if (CaretDiagnostics && Pos.isValid() && ((LastLoc != Pos) ||
147 Info.getNumRanges())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000148 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
149 LastLoc = Pos;
150
Chris Lattner4b009652007-07-25 00:24:17 +0000151 // Get the line of the source file.
152 std::string SourceLine(LineStart, LineEnd);
153
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000154 // Create a line for the caret that is filled with spaces that is the same
Chris Lattner4b009652007-07-25 00:24:17 +0000155 // length as the line of source code.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000156 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000157
158 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner6948ae62008-11-18 07:04:44 +0000159 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
160 HighlightRange(Info.getRange(i), Pos.getManager(), LineNo, FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000161 CaretLine, SourceLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000162
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000163 // Next, insert the caret itself.
164 if (ColNo-1 < CaretLine.size())
165 CaretLine[ColNo-1] = '^';
Chris Lattner4b009652007-07-25 00:24:17 +0000166 else
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000167 CaretLine.push_back('^');
Chris Lattner4b009652007-07-25 00:24:17 +0000168
169 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000170 // them to 8 characters and update the CaretLine to match.
Chris Lattner4b009652007-07-25 00:24:17 +0000171 for (unsigned i = 0; i != SourceLine.size(); ++i) {
172 if (SourceLine[i] != '\t') continue;
173
174 // Replace this tab with at least one space.
175 SourceLine[i] = ' ';
176
177 // Compute the number of spaces we need to insert.
178 unsigned NumSpaces = ((i+8)&~7) - (i+1);
179 assert(NumSpaces < 8 && "Invalid computation of space amt");
180
181 // Insert spaces into the SourceLine.
182 SourceLine.insert(i+1, NumSpaces, ' ');
183
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000184 // Insert spaces or ~'s into CaretLine.
185 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000186 }
187
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000188 // Finally, remove any blank spaces from the end of CaretLine.
189 while (CaretLine[CaretLine.size()-1] == ' ')
190 CaretLine.erase(CaretLine.end()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000191
192 // Emit what we have computed.
Nate Begeman01d74272008-04-17 18:06:57 +0000193 OS << SourceLine << "\n";
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000194 OS << CaretLine << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000195 }
196}