blob: 5188f4dfa1105106d796c2189614f495f8bc1b65 [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/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Lex/HeaderSearch.h"
18#include "clang/Lex/Lexer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include <string>
21using namespace clang;
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023void TextDiagnosticPrinter::
Ted Kremenek9c728dc2007-12-12 22:39:36 +000024PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000025 if (Pos.isInvalid()) return;
26
Ted Kremenek9c728dc2007-12-12 22:39:36 +000027 Pos = Pos.getLogicalLoc();
Chris Lattner9dc1f532007-07-20 16:37:10 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029 // Print out the other include frames first.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000030 PrintIncludeStack(Pos.getIncludeLoc());
31 unsigned LineNo = Pos.getLineNumber();
Reid Spencer5f016e22007-07-11 17:01:13 +000032
Nate Begeman165b9542008-04-17 18:06:57 +000033 OS << "In file included from " << Pos.getSourceName()
34 << ":" << LineNo << ":\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000035}
36
37/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
38/// any characters in LineNo that intersect the SourceRange.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000039void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnere41b7cd2008-01-12 06:43:35 +000040 SourceManager& SourceMgr,
41 unsigned LineNo, unsigned FileID,
Reid Spencer5f016e22007-07-11 17:01:13 +000042 std::string &CaratLine,
Nuno Lopesdb825682008-08-05 19:40:20 +000043 const std::string &SourceLine) {
Reid Spencer5f016e22007-07-11 17:01:13 +000044 assert(CaratLine.size() == SourceLine.size() &&
45 "Expect a correspondence between source and carat line!");
46 if (!R.isValid()) return;
47
Chris Lattnere41b7cd2008-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.
Reid Spencer5f016e22007-07-11 17:01:13 +000052
Chris Lattnere41b7cd2008-01-12 06:43:35 +000053 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
54 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek2b70af42008-03-27 05:52:45 +000055 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000056 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000057
58 // Compute the column number of the start.
59 unsigned StartColNo = 0;
60 if (StartLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000061 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +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.
71 unsigned EndColNo = CaratLine.size();
72 if (EndLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000073 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +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 Kremenek9c728dc2007-12-12 22:39:36 +000078 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +000079 } else {
80 EndColNo = CaratLine.size();
81 }
82 }
83
84 // Pick the last non-whitespace column.
Nuno Lopesdb825682008-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();
Reid Spencer5f016e22007-07-11 17:01:13 +000091
92 // Fill the range with ~'s.
93 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesdb825682008-08-05 19:40:20 +000094 for (unsigned i = StartColNo; i < EndColNo; ++i)
Reid Spencer5f016e22007-07-11 17:01:13 +000095 CaratLine[i] = '~';
96}
97
Chris Lattner07506182007-11-30 22:53:43 +000098void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
99 Diagnostic::Level Level,
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000100 FullSourceLoc Pos,
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 diag::kind ID,
102 const std::string *Strs,
103 unsigned NumStrs,
104 const SourceRange *Ranges,
105 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000106 unsigned LineNo = 0, ColNo = 0;
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000107 unsigned FileID = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000108 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109
110 if (Pos.isValid()) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000111 FullSourceLoc LPos = Pos.getLogicalLoc();
112 LineNo = LPos.getLineNumber();
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000113 FileID = LPos.getLocation().getFileID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000114
115 // First, if this diagnostic is not in the main file, print out the
116 // "included from" lines.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000117 if (LastWarningLoc != LPos.getIncludeLoc()) {
118 LastWarningLoc = LPos.getIncludeLoc();
119 PrintIncludeStack(LastWarningLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 }
121
122 // Compute the column number. Rewind from the current position to the start
123 // of the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000124 ColNo = LPos.getColumnNumber();
125 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000126 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner0cbc2152008-01-25 00:01:10 +0000127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 // Compute the line end. Scan forward from the error position to the end of
129 // the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000130 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000132 LineEnd = TokLogicalPtr;
133 while (LineEnd != BufEnd &&
134 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 ++LineEnd;
136
Nate Begeman165b9542008-04-17 18:06:57 +0000137 OS << Buffer->getBufferIdentifier() << ":" << LineNo << ":";
Nico Weberfd54ebc2008-08-05 23:33:20 +0000138 if (ColNo && ShowColumn)
Nate Begeman165b9542008-04-17 18:06:57 +0000139 OS << ColNo << ":";
140 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
142
143 switch (Level) {
144 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman165b9542008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 break;
150 }
151
Nate Begeman165b9542008-04-17 18:06:57 +0000152 OS << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000153
Nico Weberfd54ebc2008-08-05 23:33:20 +0000154 if (CaretDiagnostics && Pos.isValid() && ((LastLoc != Pos) || Ranges)) {
Steve Naroffefe7f362008-02-08 22:06:17 +0000155 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
156 LastLoc = Pos;
157
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000159 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160
161 // Create a line for the carat that is filled with spaces that is the same
162 // length as the line of source code.
163 std::string CaratLine(LineEnd-LineStart, ' ');
164
165 // Highlight all of the characters covered by Ranges with ~ characters.
166 for (unsigned i = 0; i != NumRanges; ++i)
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000167 HighlightRange(Ranges[i], Pos.getManager(), LineNo, FileID,
168 CaratLine, SourceLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169
170 // Next, insert the carat itself.
171 if (ColNo-1 < CaratLine.size())
172 CaratLine[ColNo-1] = '^';
173 else
174 CaratLine.push_back('^');
175
176 // Scan the source line, looking for tabs. If we find any, manually expand
177 // them to 8 characters and update the CaratLine to match.
178 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
191 // Insert spaces or ~'s into CaratLine.
192 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
193 }
194
195 // Finally, remove any blank spaces from the end of CaratLine.
196 while (CaratLine[CaratLine.size()-1] == ' ')
197 CaratLine.erase(CaratLine.end()-1);
198
199 // Emit what we have computed.
Nate Begeman165b9542008-04-17 18:06:57 +0000200 OS << SourceLine << "\n";
201 OS << CaratLine << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 }
203}