blob: cfbe8a7f927df89c987cc4e84f5e26de9557de96 [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 Lattnerbe8e5a42008-11-19 06:51:40 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattner4b009652007-07-25 00:24:17 +000021void TextDiagnosticPrinter::
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000022PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner4b009652007-07-25 00:24:17 +000023 if (Pos.isInvalid()) return;
24
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000025 Pos = Pos.getLogicalLoc();
Chris Lattner4b009652007-07-25 00:24:17 +000026
27 // Print out the other include frames first.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000028 PrintIncludeStack(Pos.getIncludeLoc());
29 unsigned LineNo = Pos.getLineNumber();
Chris Lattner4b009652007-07-25 00:24:17 +000030
Nate Begeman01d74272008-04-17 18:06:57 +000031 OS << "In file included from " << Pos.getSourceName()
32 << ":" << LineNo << ":\n";
Chris Lattner4b009652007-07-25 00:24:17 +000033}
34
35/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
36/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekb3ee1932007-12-11 21:27:55 +000037void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattner6948ae62008-11-18 07:04:44 +000038 const SourceManager& SourceMgr,
Chris Lattnera0030d22008-01-12 06:43:35 +000039 unsigned LineNo, unsigned FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000040 std::string &CaretLine,
Nuno Lopesd0e162c2008-08-05 19:40:20 +000041 const std::string &SourceLine) {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000042 assert(CaretLine.size() == SourceLine.size() &&
43 "Expect a correspondence between source and caret line!");
Chris Lattner4b009652007-07-25 00:24:17 +000044 if (!R.isValid()) return;
45
Chris Lattnera0030d22008-01-12 06:43:35 +000046 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(R.getBegin());
47 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
48 if (StartLineNo > LineNo || LogicalStart.getFileID() != FileID)
49 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000050
Chris Lattnera0030d22008-01-12 06:43:35 +000051 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
52 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek1840dbd2008-03-27 05:52:45 +000053 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnera0030d22008-01-12 06:43:35 +000054 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000055
56 // Compute the column number of the start.
57 unsigned StartColNo = 0;
58 if (StartLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000059 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +000060 if (StartColNo) --StartColNo; // Zero base the col #.
61 }
62
63 // Pick the first non-whitespace column.
64 while (StartColNo < SourceLine.size() &&
65 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
66 ++StartColNo;
67
68 // Compute the column number of the end.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000069 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000070 if (EndLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000071 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +000072 if (EndColNo) {
73 --EndColNo; // Zero base the col #.
74
75 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000076 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +000077 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000078 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000079 }
80 }
81
82 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-08-05 19:40:20 +000083 if (EndColNo <= SourceLine.size())
84 while (EndColNo-1 &&
85 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
86 --EndColNo;
87 else
88 EndColNo = SourceLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000089
90 // Fill the range with ~'s.
91 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +000092 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000093 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +000094}
95
Chris Lattner6948ae62008-11-18 07:04:44 +000096void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
97 const DiagnosticInfo &Info) {
Chris Lattner4b009652007-07-25 00:24:17 +000098 unsigned LineNo = 0, ColNo = 0;
Chris Lattnera0030d22008-01-12 06:43:35 +000099 unsigned FileID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000100 const char *LineStart = 0, *LineEnd = 0;
Chris Lattner6948ae62008-11-18 07:04:44 +0000101 const FullSourceLoc &Pos = Info.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000102
103 if (Pos.isValid()) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000104 FullSourceLoc LPos = Pos.getLogicalLoc();
105 LineNo = LPos.getLineNumber();
Chris Lattnera0030d22008-01-12 06:43:35 +0000106 FileID = LPos.getLocation().getFileID();
Chris Lattner4b009652007-07-25 00:24:17 +0000107
108 // First, if this diagnostic is not in the main file, print out the
109 // "included from" lines.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000110 if (LastWarningLoc != LPos.getIncludeLoc()) {
111 LastWarningLoc = LPos.getIncludeLoc();
112 PrintIncludeStack(LastWarningLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000113 }
114
115 // Compute the column number. Rewind from the current position to the start
116 // of the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000117 ColNo = LPos.getColumnNumber();
118 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner4b009652007-07-25 00:24:17 +0000119 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner83343342008-01-25 00:01:10 +0000120
Chris Lattner4b009652007-07-25 00:24:17 +0000121 // Compute the line end. Scan forward from the error position to the end of
122 // the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000123 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000124 const char *BufEnd = Buffer->getBufferEnd();
125 LineEnd = TokLogicalPtr;
126 while (LineEnd != BufEnd &&
127 *LineEnd != '\n' && *LineEnd != '\r')
128 ++LineEnd;
129
Nate Begeman01d74272008-04-17 18:06:57 +0000130 OS << Buffer->getBufferIdentifier() << ":" << LineNo << ":";
Nico Weber0e13eaa2008-08-05 23:33:20 +0000131 if (ColNo && ShowColumn)
Nate Begeman01d74272008-04-17 18:06:57 +0000132 OS << ColNo << ":";
133 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000134 }
135
136 switch (Level) {
137 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman01d74272008-04-17 18:06:57 +0000138 case Diagnostic::Note: OS << "note: "; break;
139 case Diagnostic::Warning: OS << "warning: "; break;
140 case Diagnostic::Error: OS << "error: "; break;
141 case Diagnostic::Fatal: OS << "fatal error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000142 }
143
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000144 llvm::SmallString<100> OutStr;
145 Info.FormatDiagnostic(OutStr);
146 OS.write(OutStr.begin(), OutStr.size());
147 OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000148
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000149 if (CaretDiagnostics && Pos.isValid() &&
150 ((LastLoc != Pos) || Info.getNumRanges())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000151 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
152 LastLoc = Pos;
153
Chris Lattner4b009652007-07-25 00:24:17 +0000154 // Get the line of the source file.
155 std::string SourceLine(LineStart, LineEnd);
156
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000157 // Create a line for the caret that is filled with spaces that is the same
Chris Lattner4b009652007-07-25 00:24:17 +0000158 // length as the line of source code.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000159 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000160
161 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner6948ae62008-11-18 07:04:44 +0000162 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
163 HighlightRange(Info.getRange(i), Pos.getManager(), LineNo, FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000164 CaretLine, SourceLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000165
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000166 // Next, insert the caret itself.
167 if (ColNo-1 < CaretLine.size())
168 CaretLine[ColNo-1] = '^';
Chris Lattner4b009652007-07-25 00:24:17 +0000169 else
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000170 CaretLine.push_back('^');
Chris Lattner4b009652007-07-25 00:24:17 +0000171
172 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000173 // them to 8 characters and update the CaretLine to match.
Chris Lattner4b009652007-07-25 00:24:17 +0000174 for (unsigned i = 0; i != SourceLine.size(); ++i) {
175 if (SourceLine[i] != '\t') continue;
176
177 // Replace this tab with at least one space.
178 SourceLine[i] = ' ';
179
180 // Compute the number of spaces we need to insert.
181 unsigned NumSpaces = ((i+8)&~7) - (i+1);
182 assert(NumSpaces < 8 && "Invalid computation of space amt");
183
184 // Insert spaces into the SourceLine.
185 SourceLine.insert(i+1, NumSpaces, ' ');
186
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000187 // Insert spaces or ~'s into CaretLine.
188 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000189 }
190
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000191 // Finally, remove any blank spaces from the end of CaretLine.
192 while (CaretLine[CaretLine.size()-1] == ' ')
193 CaretLine.erase(CaretLine.end()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000194
195 // Emit what we have computed.
Nate Begeman01d74272008-04-17 18:06:57 +0000196 OS << SourceLine << "\n";
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000197 OS << CaretLine << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199}