blob: b4be9307ea73d0d6ed7fa59826399d812060a6db [file] [log] [blame]
Bill Wendling37b1dde2007-06-07 09:34:54 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling37b1dde2007-06-07 09:34:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Nico Weberb5fc3c32008-08-05 23:33:20 +000014#include "clang/Driver/TextDiagnosticPrinter.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000015#include "clang/Basic/SourceManager.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000016#include "clang/Lex/Lexer.h"
Chris Lattner327984f2008-11-19 06:56:25 +000017#include "llvm/Support/raw_ostream.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattner23be0672008-11-19 06:51:40 +000019#include "llvm/ADT/SmallString.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000020using namespace clang;
21
Bill Wendling37b1dde2007-06-07 09:34:54 +000022void TextDiagnosticPrinter::
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000023PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +000024 if (Pos.isInvalid()) return;
25
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000026 Pos = Pos.getLogicalLoc();
Chris Lattnerdc5c0552007-07-20 16:37:10 +000027
Bill Wendling37b1dde2007-06-07 09:34:54 +000028 // Print out the other include frames first.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000029 PrintIncludeStack(Pos.getIncludeLoc());
30 unsigned LineNo = Pos.getLineNumber();
Bill Wendling37b1dde2007-06-07 09:34:54 +000031
Nate Begeman36c49272008-04-17 18:06:57 +000032 OS << "In file included from " << Pos.getSourceName()
Chris Lattner327984f2008-11-19 06:56:25 +000033 << ':' << LineNo << ":\n";
Bill Wendling37b1dde2007-06-07 09:34:54 +000034}
35
Bill Wendling37b1dde2007-06-07 09:34:54 +000036/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
37/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +000038void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattner8488c822008-11-18 07:04:44 +000039 const SourceManager& SourceMgr,
Chris Lattner177977ac2008-01-12 06:43:35 +000040 unsigned LineNo, unsigned FileID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000041 std::string &CaretLine,
Nuno Lopesd86aa932008-08-05 19:40:20 +000042 const std::string &SourceLine) {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000043 assert(CaretLine.size() == SourceLine.size() &&
44 "Expect a correspondence between source and caret line!");
Bill Wendling37b1dde2007-06-07 09:34:54 +000045 if (!R.isValid()) return;
46
Chris Lattner177977ac2008-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.
Bill Wendling37b1dde2007-06-07 09:34:54 +000051
Chris Lattner177977ac2008-01-12 06:43:35 +000052 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
53 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenekc8752012008-03-27 05:52:45 +000054 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattner177977ac2008-01-12 06:43:35 +000055 return; // No intersection.
Bill Wendling37b1dde2007-06-07 09:34:54 +000056
57 // Compute the column number of the start.
58 unsigned StartColNo = 0;
59 if (StartLineNo == LineNo) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000060 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Bill Wendling37b1dde2007-06-07 09:34:54 +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 Henriksen04c50bd2008-08-09 19:58:22 +000070 unsigned EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000071 if (EndLineNo == LineNo) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +000072 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Bill Wendling37b1dde2007-06-07 09:34:54 +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 Kremenek1daa3cf2007-12-12 22:39:36 +000077 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Bill Wendling37b1dde2007-06-07 09:34:54 +000078 } else {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000079 EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000080 }
81 }
82
83 // Pick the last non-whitespace column.
Nuno Lopesd86aa932008-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();
Bill Wendling37b1dde2007-06-07 09:34:54 +000090
91 // Fill the range with ~'s.
92 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd86aa932008-08-05 19:40:20 +000093 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000094 CaretLine[i] = '~';
Bill Wendling37b1dde2007-06-07 09:34:54 +000095}
96
Chris Lattner8488c822008-11-18 07:04:44 +000097void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
98 const DiagnosticInfo &Info) {
Chris Lattnerdc5c0552007-07-20 16:37:10 +000099 unsigned LineNo = 0, ColNo = 0;
Chris Lattner177977ac2008-01-12 06:43:35 +0000100 unsigned FileID = 0;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000101 const char *LineStart = 0, *LineEnd = 0;
Chris Lattner8488c822008-11-18 07:04:44 +0000102 const FullSourceLoc &Pos = Info.getLocation();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000103
104 if (Pos.isValid()) {
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000105 FullSourceLoc LPos = Pos.getLogicalLoc();
106 LineNo = LPos.getLineNumber();
Chris Lattner177977ac2008-01-12 06:43:35 +0000107 FileID = LPos.getLocation().getFileID();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000108
Bill Wendling37b1dde2007-06-07 09:34:54 +0000109 // First, if this diagnostic is not in the main file, print out the
110 // "included from" lines.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000111 if (LastWarningLoc != LPos.getIncludeLoc()) {
112 LastWarningLoc = LPos.getIncludeLoc();
113 PrintIncludeStack(LastWarningLoc);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000114 }
115
116 // Compute the column number. Rewind from the current position to the start
117 // of the line.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000118 ColNo = LPos.getColumnNumber();
119 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000120 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner3efff542008-01-25 00:01:10 +0000121
Bill Wendling37b1dde2007-06-07 09:34:54 +0000122 // Compute the line end. Scan forward from the error position to the end of
123 // the line.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000124 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000125 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000126 LineEnd = TokLogicalPtr;
127 while (LineEnd != BufEnd &&
128 *LineEnd != '\n' && *LineEnd != '\r')
Bill Wendling37b1dde2007-06-07 09:34:54 +0000129 ++LineEnd;
130
Chris Lattner327984f2008-11-19 06:56:25 +0000131 OS << Buffer->getBufferIdentifier() << ':' << LineNo << ':';
Nico Weberb5fc3c32008-08-05 23:33:20 +0000132 if (ColNo && ShowColumn)
Chris Lattner327984f2008-11-19 06:56:25 +0000133 OS << ColNo << ':';
134 OS << ' ';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000135 }
136
137 switch (Level) {
138 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman36c49272008-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;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000143 }
144
Chris Lattner23be0672008-11-19 06:51:40 +0000145 llvm::SmallString<100> OutStr;
146 Info.FormatDiagnostic(OutStr);
147 OS.write(OutStr.begin(), OutStr.size());
148 OS << '\n';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000149
Chris Lattner23be0672008-11-19 06:51:40 +0000150 if (CaretDiagnostics && Pos.isValid() &&
151 ((LastLoc != Pos) || Info.getNumRanges())) {
Steve Naroffa36992242008-02-08 22:06:17 +0000152 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
153 LastLoc = Pos;
154
Bill Wendling37b1dde2007-06-07 09:34:54 +0000155 // Get the line of the source file.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000156 std::string SourceLine(LineStart, LineEnd);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000157
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000158 // Create a line for the caret that is filled with spaces that is the same
Bill Wendling37b1dde2007-06-07 09:34:54 +0000159 // length as the line of source code.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000160 std::string CaretLine(LineEnd-LineStart, ' ');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000161
162 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner8488c822008-11-18 07:04:44 +0000163 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
164 HighlightRange(Info.getRange(i), Pos.getManager(), LineNo, FileID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000165 CaretLine, SourceLine);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000166
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000167 // Next, insert the caret itself.
168 if (ColNo-1 < CaretLine.size())
169 CaretLine[ColNo-1] = '^';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000170 else
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000171 CaretLine.push_back('^');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000172
173 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000174 // them to 8 characters and update the CaretLine to match.
Bill Wendling37b1dde2007-06-07 09:34:54 +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 Henriksen04c50bd2008-08-09 19:58:22 +0000188 // Insert spaces or ~'s into CaretLine.
189 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000190 }
191
Gordon Henriksen04c50bd2008-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);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000195
196 // Emit what we have computed.
Chris Lattner327984f2008-11-19 06:56:25 +0000197 OS << SourceLine << '\n';
198 OS << CaretLine << '\n';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000199 }
Chris Lattner327984f2008-11-19 06:56:25 +0000200
201 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000202}