blob: 4080b748ad5a818a18f8ef7ba0410e10521a0709 [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 Lattner10aaf532009-01-17 08:45:21 +000039 const SourceManager &SourceMgr,
40 unsigned LineNo, FileID FID,
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);
Chris Lattner10aaf532009-01-17 08:45:21 +000050 if (StartLineNo > LineNo ||
51 SourceMgr.getCanonicalFileID(InstantiationStart) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000052 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000053
Chris Lattner18c8dc02009-01-16 07:36:28 +000054 SourceLocation InstantiationEnd = SourceMgr.getInstantiationLoc(R.getEnd());
55 unsigned EndLineNo = SourceMgr.getLineNumber(InstantiationEnd);
Chris Lattner10aaf532009-01-17 08:45:21 +000056 if (EndLineNo < LineNo ||
57 SourceMgr.getCanonicalFileID(InstantiationEnd) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000058 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000059
60 // Compute the column number of the start.
61 unsigned StartColNo = 0;
62 if (StartLineNo == LineNo) {
Chris Lattner18c8dc02009-01-16 07:36:28 +000063 StartColNo = SourceMgr.getInstantiationColumnNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +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 Henriksenf0a835c2008-08-09 19:58:22 +000073 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000074 if (EndLineNo == LineNo) {
Chris Lattner18c8dc02009-01-16 07:36:28 +000075 EndColNo = SourceMgr.getInstantiationColumnNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +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 Kremenekd7f64cd2007-12-12 22:39:36 +000080 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +000081 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000082 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000083 }
84 }
85
86 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-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();
Chris Lattner4b009652007-07-25 00:24:17 +000093
94 // Fill the range with ~'s.
95 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +000096 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000097 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +000098}
99
Chris Lattner6948ae62008-11-18 07:04:44 +0000100void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
101 const DiagnosticInfo &Info) {
Chris Lattner4b009652007-07-25 00:24:17 +0000102 unsigned LineNo = 0, ColNo = 0;
Chris Lattner10aaf532009-01-17 08:45:21 +0000103 FileID FID;
Chris Lattner4b009652007-07-25 00:24:17 +0000104 const char *LineStart = 0, *LineEnd = 0;
Chris Lattner6948ae62008-11-18 07:04:44 +0000105 const FullSourceLoc &Pos = Info.getLocation();
Chris Lattner4b009652007-07-25 00:24:17 +0000106
107 if (Pos.isValid()) {
Chris Lattner18c8dc02009-01-16 07:36:28 +0000108 FullSourceLoc LPos = Pos.getInstantiationLoc();
Chris Lattner10aaf532009-01-17 08:45:21 +0000109 FID = LPos.getFileID();
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000110 LineNo = LPos.getLineNumber();
Chris Lattner4b009652007-07-25 00:24:17 +0000111
112 // First, if this diagnostic is not in the main file, print out the
113 // "included from" lines.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000114 if (LastWarningLoc != LPos.getIncludeLoc()) {
115 LastWarningLoc = LPos.getIncludeLoc();
116 PrintIncludeStack(LastWarningLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000117 }
118
119 // Compute the column number. Rewind from the current position to the start
120 // of the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000121 ColNo = LPos.getColumnNumber();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000122 const char *TokInstantiationPtr = LPos.getCharacterData();
123 LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based
Chris Lattner83343342008-01-25 00:01:10 +0000124
Chris Lattner4b009652007-07-25 00:24:17 +0000125 // Compute the line end. Scan forward from the error position to the end of
126 // the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000127 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000128 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner18c8dc02009-01-16 07:36:28 +0000129 LineEnd = TokInstantiationPtr;
Chris Lattner4b009652007-07-25 00:24:17 +0000130 while (LineEnd != BufEnd &&
131 *LineEnd != '\n' && *LineEnd != '\r')
132 ++LineEnd;
133
Chris Lattner92a33532008-11-19 06:56:25 +0000134 OS << Buffer->getBufferIdentifier() << ':' << LineNo << ':';
Nico Weber0e13eaa2008-08-05 23:33:20 +0000135 if (ColNo && ShowColumn)
Chris Lattner92a33532008-11-19 06:56:25 +0000136 OS << ColNo << ':';
137 OS << ' ';
Chris Lattner4b009652007-07-25 00:24:17 +0000138 }
139
140 switch (Level) {
141 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman01d74272008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
146
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000147 llvm::SmallString<100> OutStr;
148 Info.FormatDiagnostic(OutStr);
149 OS.write(OutStr.begin(), OutStr.size());
150 OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000151
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000152 if (CaretDiagnostics && Pos.isValid() &&
153 ((LastLoc != Pos) || Info.getNumRanges())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000154 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
155 LastLoc = Pos;
156
Chris Lattner4b009652007-07-25 00:24:17 +0000157 // Get the line of the source file.
158 std::string SourceLine(LineStart, LineEnd);
159
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000160 // Create a line for the caret that is filled with spaces that is the same
Chris Lattner4b009652007-07-25 00:24:17 +0000161 // length as the line of source code.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000162 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000163
164 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner6948ae62008-11-18 07:04:44 +0000165 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
Chris Lattner10aaf532009-01-17 08:45:21 +0000166 HighlightRange(Info.getRange(i), Pos.getManager(), LineNo, FID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000167 CaretLine, SourceLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000168
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000169 // Next, insert the caret itself.
170 if (ColNo-1 < CaretLine.size())
171 CaretLine[ColNo-1] = '^';
Chris Lattner4b009652007-07-25 00:24:17 +0000172 else
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000173 CaretLine.push_back('^');
Chris Lattner4b009652007-07-25 00:24:17 +0000174
175 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000176 // them to 8 characters and update the CaretLine to match.
Chris Lattner4b009652007-07-25 00:24:17 +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 Henriksenf0a835c2008-08-09 19:58:22 +0000190 // Insert spaces or ~'s into CaretLine.
191 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000192 }
193
Gordon Henriksenf0a835c2008-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);
Chris Lattner4b009652007-07-25 00:24:17 +0000197
198 // Emit what we have computed.
Chris Lattner92a33532008-11-19 06:56:25 +0000199 OS << SourceLine << '\n';
200 OS << CaretLine << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000201 }
Chris Lattner92a33532008-11-19 06:56:25 +0000202
203 OS.flush();
Chris Lattner4b009652007-07-25 00:24:17 +0000204}