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