blob: b4be9307ea73d0d6ed7fa59826399d812060a6db [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
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000026 Pos = Pos.getLogicalLoc();
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 Lattnera0030d22008-01-12 06:43:35 +000047 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(R.getBegin());
48 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
49 if (StartLineNo > LineNo || LogicalStart.getFileID() != FileID)
50 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000051
Chris Lattnera0030d22008-01-12 06:43:35 +000052 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
53 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek1840dbd2008-03-27 05:52:45 +000054 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnera0030d22008-01-12 06:43:35 +000055 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000056
57 // Compute the column number of the start.
58 unsigned StartColNo = 0;
59 if (StartLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000060 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +000061 if (StartColNo) --StartColNo; // Zero base the col #.
62 }
63
64 // Pick the first non-whitespace column.
65 while (StartColNo < SourceLine.size() &&
66 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
67 ++StartColNo;
68
69 // Compute the column number of the end.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000070 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000071 if (EndLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000072 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +000073 if (EndColNo) {
74 --EndColNo; // Zero base the col #.
75
76 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000077 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +000078 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000079 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000080 }
81 }
82
83 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-08-05 19:40:20 +000084 if (EndColNo <= SourceLine.size())
85 while (EndColNo-1 &&
86 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
87 --EndColNo;
88 else
89 EndColNo = SourceLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000090
91 // Fill the range with ~'s.
92 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +000093 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000094 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +000095}
96
Chris Lattner6948ae62008-11-18 07:04:44 +000097void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
98 const DiagnosticInfo &Info) {
Chris Lattner4b009652007-07-25 00:24:17 +000099 unsigned LineNo = 0, ColNo = 0;
Chris Lattnera0030d22008-01-12 06:43:35 +0000100 unsigned FileID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000101 const char *LineStart = 0, *LineEnd = 0;
Chris Lattner6948ae62008-11-18 07:04:44 +0000102 const FullSourceLoc &Pos = Info.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000103
104 if (Pos.isValid()) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000105 FullSourceLoc LPos = Pos.getLogicalLoc();
106 LineNo = LPos.getLineNumber();
Chris Lattnera0030d22008-01-12 06:43:35 +0000107 FileID = LPos.getLocation().getFileID();
Chris Lattner4b009652007-07-25 00:24:17 +0000108
109 // First, if this diagnostic is not in the main file, print out the
110 // "included from" lines.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000111 if (LastWarningLoc != LPos.getIncludeLoc()) {
112 LastWarningLoc = LPos.getIncludeLoc();
113 PrintIncludeStack(LastWarningLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 }
115
116 // Compute the column number. Rewind from the current position to the start
117 // of the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000118 ColNo = LPos.getColumnNumber();
119 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner4b009652007-07-25 00:24:17 +0000120 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner83343342008-01-25 00:01:10 +0000121
Chris Lattner4b009652007-07-25 00:24:17 +0000122 // Compute the line end. Scan forward from the error position to the end of
123 // the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000124 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000125 const char *BufEnd = Buffer->getBufferEnd();
126 LineEnd = TokLogicalPtr;
127 while (LineEnd != BufEnd &&
128 *LineEnd != '\n' && *LineEnd != '\r')
129 ++LineEnd;
130
Chris Lattner92a33532008-11-19 06:56:25 +0000131 OS << Buffer->getBufferIdentifier() << ':' << LineNo << ':';
Nico Weber0e13eaa2008-08-05 23:33:20 +0000132 if (ColNo && ShowColumn)
Chris Lattner92a33532008-11-19 06:56:25 +0000133 OS << ColNo << ':';
134 OS << ' ';
Chris Lattner4b009652007-07-25 00:24:17 +0000135 }
136
137 switch (Level) {
138 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman01d74272008-04-17 18:06:57 +0000139 case Diagnostic::Note: OS << "note: "; break;
140 case Diagnostic::Warning: OS << "warning: "; break;
141 case Diagnostic::Error: OS << "error: "; break;
142 case Diagnostic::Fatal: OS << "fatal 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}