blob: e0faf478d1a5bc15892279dd8d7c90eec8f32ce2 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018using namespace clang;
19
Reid Spencer5f016e22007-07-11 17:01:13 +000020void TextDiagnosticPrinter::
Ted Kremenek9c728dc2007-12-12 22:39:36 +000021PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000022 if (Pos.isInvalid()) return;
23
Ted Kremenek9c728dc2007-12-12 22:39:36 +000024 Pos = Pos.getLogicalLoc();
Chris Lattner9dc1f532007-07-20 16:37:10 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026 // Print out the other include frames first.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000027 PrintIncludeStack(Pos.getIncludeLoc());
28 unsigned LineNo = Pos.getLineNumber();
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Nate Begeman165b9542008-04-17 18:06:57 +000030 OS << "In file included from " << Pos.getSourceName()
31 << ":" << LineNo << ":\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000032}
33
34/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
35/// any characters in LineNo that intersect the SourceRange.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000036void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnere41b7cd2008-01-12 06:43:35 +000037 SourceManager& SourceMgr,
38 unsigned LineNo, unsigned FileID,
Gordon Henriksenaad69532008-08-09 19:58:22 +000039 std::string &CaretLine,
Nuno Lopesdb825682008-08-05 19:40:20 +000040 const std::string &SourceLine) {
Gordon Henriksenaad69532008-08-09 19:58:22 +000041 assert(CaretLine.size() == SourceLine.size() &&
42 "Expect a correspondence between source and caret line!");
Reid Spencer5f016e22007-07-11 17:01:13 +000043 if (!R.isValid()) return;
44
Chris Lattnere41b7cd2008-01-12 06:43:35 +000045 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(R.getBegin());
46 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
47 if (StartLineNo > LineNo || LogicalStart.getFileID() != FileID)
48 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000049
Chris Lattnere41b7cd2008-01-12 06:43:35 +000050 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
51 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek2b70af42008-03-27 05:52:45 +000052 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000053 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000054
55 // Compute the column number of the start.
56 unsigned StartColNo = 0;
57 if (StartLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000058 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +000059 if (StartColNo) --StartColNo; // Zero base the col #.
60 }
61
62 // Pick the first non-whitespace column.
63 while (StartColNo < SourceLine.size() &&
64 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
65 ++StartColNo;
66
67 // Compute the column number of the end.
Gordon Henriksenaad69532008-08-09 19:58:22 +000068 unsigned EndColNo = CaretLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +000069 if (EndLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000070 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +000071 if (EndColNo) {
72 --EndColNo; // Zero base the col #.
73
74 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000075 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +000076 } else {
Gordon Henriksenaad69532008-08-09 19:58:22 +000077 EndColNo = CaretLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +000078 }
79 }
80
81 // Pick the last non-whitespace column.
Nuno Lopesdb825682008-08-05 19:40:20 +000082 if (EndColNo <= SourceLine.size())
83 while (EndColNo-1 &&
84 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
85 --EndColNo;
86 else
87 EndColNo = SourceLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +000088
89 // Fill the range with ~'s.
90 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesdb825682008-08-05 19:40:20 +000091 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenaad69532008-08-09 19:58:22 +000092 CaretLine[i] = '~';
Reid Spencer5f016e22007-07-11 17:01:13 +000093}
94
Chris Lattner07506182007-11-30 22:53:43 +000095void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
96 Diagnostic::Level Level,
Ted Kremenek9c728dc2007-12-12 22:39:36 +000097 FullSourceLoc Pos,
Reid Spencer5f016e22007-07-11 17:01:13 +000098 diag::kind ID,
99 const std::string *Strs,
100 unsigned NumStrs,
101 const SourceRange *Ranges,
102 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000103 unsigned LineNo = 0, ColNo = 0;
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000104 unsigned FileID = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000105 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106
107 if (Pos.isValid()) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000108 FullSourceLoc LPos = Pos.getLogicalLoc();
109 LineNo = LPos.getLineNumber();
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000110 FileID = LPos.getLocation().getFileID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000111
112 // First, if this diagnostic is not in the main file, print out the
113 // "included from" lines.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000114 if (LastWarningLoc != LPos.getIncludeLoc()) {
115 LastWarningLoc = LPos.getIncludeLoc();
116 PrintIncludeStack(LastWarningLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 }
118
119 // Compute the column number. Rewind from the current position to the start
120 // of the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000121 ColNo = LPos.getColumnNumber();
122 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000123 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner0cbc2152008-01-25 00:01:10 +0000124
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 // Compute the line end. Scan forward from the error position to the end of
126 // the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000127 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000129 LineEnd = TokLogicalPtr;
130 while (LineEnd != BufEnd &&
131 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 ++LineEnd;
133
Nate Begeman165b9542008-04-17 18:06:57 +0000134 OS << Buffer->getBufferIdentifier() << ":" << LineNo << ":";
Nico Weberfd54ebc2008-08-05 23:33:20 +0000135 if (ColNo && ShowColumn)
Nate Begeman165b9542008-04-17 18:06:57 +0000136 OS << ColNo << ":";
137 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 }
139
140 switch (Level) {
141 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman165b9542008-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;
145 case Diagnostic::Fatal: OS << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 break;
147 }
148
Nate Begeman165b9542008-04-17 18:06:57 +0000149 OS << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000150
Nico Weberfd54ebc2008-08-05 23:33:20 +0000151 if (CaretDiagnostics && Pos.isValid() && ((LastLoc != Pos) || Ranges)) {
Steve Naroffefe7f362008-02-08 22:06:17 +0000152 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
153 LastLoc = Pos;
154
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000156 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000157
Gordon Henriksenaad69532008-08-09 19:58:22 +0000158 // Create a line for the caret that is filled with spaces that is the same
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // length as the line of source code.
Gordon Henriksenaad69532008-08-09 19:58:22 +0000160 std::string CaretLine(LineEnd-LineStart, ' ');
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
162 // Highlight all of the characters covered by Ranges with ~ characters.
163 for (unsigned i = 0; i != NumRanges; ++i)
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000164 HighlightRange(Ranges[i], Pos.getManager(), LineNo, FileID,
Gordon Henriksenaad69532008-08-09 19:58:22 +0000165 CaretLine, SourceLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
Gordon Henriksenaad69532008-08-09 19:58:22 +0000167 // Next, insert the caret itself.
168 if (ColNo-1 < CaretLine.size())
169 CaretLine[ColNo-1] = '^';
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 else
Gordon Henriksenaad69532008-08-09 19:58:22 +0000171 CaretLine.push_back('^');
Reid Spencer5f016e22007-07-11 17:01:13 +0000172
173 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenaad69532008-08-09 19:58:22 +0000174 // them to 8 characters and update the CaretLine to match.
Reid Spencer5f016e22007-07-11 17:01:13 +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 Henriksenaad69532008-08-09 19:58:22 +0000188 // Insert spaces or ~'s into CaretLine.
189 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 }
191
Gordon Henriksenaad69532008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000195
196 // Emit what we have computed.
Nate Begeman165b9542008-04-17 18:06:57 +0000197 OS << SourceLine << "\n";
Gordon Henriksenaad69532008-08-09 19:58:22 +0000198 OS << CaretLine << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 }
200}