blob: f8f6b3599b325385a8addcb899b15141425eb512 [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/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Lex/HeaderSearch.h"
18#include "clang/Lex/Lexer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include <string>
21using namespace clang;
22
Chris Lattner4b009652007-07-25 00:24:17 +000023void TextDiagnosticPrinter::
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000024PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner4b009652007-07-25 00:24:17 +000025 if (Pos.isInvalid()) return;
26
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000027 Pos = Pos.getLogicalLoc();
Chris Lattner4b009652007-07-25 00:24:17 +000028
29 // Print out the other include frames first.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000030 PrintIncludeStack(Pos.getIncludeLoc());
31 unsigned LineNo = Pos.getLineNumber();
Chris Lattner4b009652007-07-25 00:24:17 +000032
Nate Begeman01d74272008-04-17 18:06:57 +000033 OS << "In file included from " << Pos.getSourceName()
34 << ":" << LineNo << ":\n";
Chris Lattner4b009652007-07-25 00:24:17 +000035}
36
37/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
38/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekb3ee1932007-12-11 21:27:55 +000039void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnera0030d22008-01-12 06:43:35 +000040 SourceManager& SourceMgr,
41 unsigned LineNo, unsigned FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000042 std::string &CaretLine,
Nuno Lopesd0e162c2008-08-05 19:40:20 +000043 const std::string &SourceLine) {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000044 assert(CaretLine.size() == SourceLine.size() &&
45 "Expect a correspondence between source and caret line!");
Chris Lattner4b009652007-07-25 00:24:17 +000046 if (!R.isValid()) return;
47
Chris Lattnera0030d22008-01-12 06:43:35 +000048 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(R.getBegin());
49 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
50 if (StartLineNo > LineNo || LogicalStart.getFileID() != FileID)
51 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000052
Chris Lattnera0030d22008-01-12 06:43:35 +000053 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
54 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek1840dbd2008-03-27 05:52:45 +000055 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnera0030d22008-01-12 06:43:35 +000056 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000057
58 // Compute the column number of the start.
59 unsigned StartColNo = 0;
60 if (StartLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000061 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +000062 if (StartColNo) --StartColNo; // Zero base the col #.
63 }
64
65 // Pick the first non-whitespace column.
66 while (StartColNo < SourceLine.size() &&
67 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
68 ++StartColNo;
69
70 // Compute the column number of the end.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000071 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000072 if (EndLineNo == LineNo) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000073 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +000074 if (EndColNo) {
75 --EndColNo; // Zero base the col #.
76
77 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +000078 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +000079 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000080 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000081 }
82 }
83
84 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-08-05 19:40:20 +000085 if (EndColNo <= SourceLine.size())
86 while (EndColNo-1 &&
87 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
88 --EndColNo;
89 else
90 EndColNo = SourceLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000091
92 // Fill the range with ~'s.
93 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +000094 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000095 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +000096}
97
Chris Lattner4478db92007-11-30 22:53:43 +000098void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
99 Diagnostic::Level Level,
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000100 FullSourceLoc Pos,
Chris Lattner4b009652007-07-25 00:24:17 +0000101 diag::kind ID,
102 const std::string *Strs,
103 unsigned NumStrs,
104 const SourceRange *Ranges,
105 unsigned NumRanges) {
106 unsigned LineNo = 0, ColNo = 0;
Chris Lattnera0030d22008-01-12 06:43:35 +0000107 unsigned FileID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000108 const char *LineStart = 0, *LineEnd = 0;
109
110 if (Pos.isValid()) {
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000111 FullSourceLoc LPos = Pos.getLogicalLoc();
112 LineNo = LPos.getLineNumber();
Chris Lattnera0030d22008-01-12 06:43:35 +0000113 FileID = LPos.getLocation().getFileID();
Chris Lattner4b009652007-07-25 00:24:17 +0000114
115 // First, if this diagnostic is not in the main file, print out the
116 // "included from" lines.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000117 if (LastWarningLoc != LPos.getIncludeLoc()) {
118 LastWarningLoc = LPos.getIncludeLoc();
119 PrintIncludeStack(LastWarningLoc);
Chris Lattner4b009652007-07-25 00:24:17 +0000120 }
121
122 // Compute the column number. Rewind from the current position to the start
123 // of the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000124 ColNo = LPos.getColumnNumber();
125 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner4b009652007-07-25 00:24:17 +0000126 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner83343342008-01-25 00:01:10 +0000127
Chris Lattner4b009652007-07-25 00:24:17 +0000128 // Compute the line end. Scan forward from the error position to the end of
129 // the line.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000130 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000131 const char *BufEnd = Buffer->getBufferEnd();
132 LineEnd = TokLogicalPtr;
133 while (LineEnd != BufEnd &&
134 *LineEnd != '\n' && *LineEnd != '\r')
135 ++LineEnd;
136
Nate Begeman01d74272008-04-17 18:06:57 +0000137 OS << Buffer->getBufferIdentifier() << ":" << LineNo << ":";
Nico Weber0e13eaa2008-08-05 23:33:20 +0000138 if (ColNo && ShowColumn)
Nate Begeman01d74272008-04-17 18:06:57 +0000139 OS << ColNo << ":";
140 OS << " ";
Chris Lattner4b009652007-07-25 00:24:17 +0000141 }
142
143 switch (Level) {
144 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman01d74272008-04-17 18:06:57 +0000145 case Diagnostic::Note: OS << "note: "; break;
146 case Diagnostic::Warning: OS << "warning: "; break;
147 case Diagnostic::Error: OS << "error: "; break;
148 case Diagnostic::Fatal: OS << "fatal error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000149 break;
150 }
151
Nate Begeman01d74272008-04-17 18:06:57 +0000152 OS << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000153
Nico Weber0e13eaa2008-08-05 23:33:20 +0000154 if (CaretDiagnostics && Pos.isValid() && ((LastLoc != Pos) || Ranges)) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000155 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
156 LastLoc = Pos;
157
Chris Lattner4b009652007-07-25 00:24:17 +0000158 // Get the line of the source file.
159 std::string SourceLine(LineStart, LineEnd);
160
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000161 // Create a line for the caret that is filled with spaces that is the same
Chris Lattner4b009652007-07-25 00:24:17 +0000162 // length as the line of source code.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000163 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000164
165 // Highlight all of the characters covered by Ranges with ~ characters.
166 for (unsigned i = 0; i != NumRanges; ++i)
Chris Lattnera0030d22008-01-12 06:43:35 +0000167 HighlightRange(Ranges[i], Pos.getManager(), LineNo, FileID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000168 CaretLine, SourceLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000169
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000170 // Next, insert the caret itself.
171 if (ColNo-1 < CaretLine.size())
172 CaretLine[ColNo-1] = '^';
Chris Lattner4b009652007-07-25 00:24:17 +0000173 else
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000174 CaretLine.push_back('^');
Chris Lattner4b009652007-07-25 00:24:17 +0000175
176 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000177 // them to 8 characters and update the CaretLine to match.
Chris Lattner4b009652007-07-25 00:24:17 +0000178 for (unsigned i = 0; i != SourceLine.size(); ++i) {
179 if (SourceLine[i] != '\t') continue;
180
181 // Replace this tab with at least one space.
182 SourceLine[i] = ' ';
183
184 // Compute the number of spaces we need to insert.
185 unsigned NumSpaces = ((i+8)&~7) - (i+1);
186 assert(NumSpaces < 8 && "Invalid computation of space amt");
187
188 // Insert spaces into the SourceLine.
189 SourceLine.insert(i+1, NumSpaces, ' ');
190
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000191 // Insert spaces or ~'s into CaretLine.
192 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000193 }
194
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000195 // Finally, remove any blank spaces from the end of CaretLine.
196 while (CaretLine[CaretLine.size()-1] == ' ')
197 CaretLine.erase(CaretLine.end()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000198
199 // Emit what we have computed.
Nate Begeman01d74272008-04-17 18:06:57 +0000200 OS << SourceLine << "\n";
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000201 OS << CaretLine << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000202 }
203}