blob: 1acd210898b32fe0e7c2bccbc5da84e39f710a17 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Bill Wendling and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
14#include "TextDiagnosticPrinter.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Lex/HeaderSearch.h"
18#include "clang/Lex/Lexer.h"
19#include "llvm/Support/CommandLine.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include <iostream>
22#include <string>
23using namespace clang;
24
25static llvm::cl::opt<bool>
26NoShowColumn("fno-show-column",
27 llvm::cl::desc("Do not include column number on diagnostics"));
28static llvm::cl::opt<bool>
29NoCaretDiagnostics("fno-caret-diagnostics",
30 llvm::cl::desc("Do not include source line and caret with"
31 " diagnostics"));
32
33void TextDiagnosticPrinter::
34PrintIncludeStack(SourceLocation Pos) {
35 unsigned FileID = Pos.getFileID();
36 if (FileID == 0) return;
37
38 // Print out the other include frames first.
39 PrintIncludeStack(SourceMgr.getIncludeLoc(FileID));
40
41 unsigned LineNo = SourceMgr.getLineNumber(Pos);
42
43 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(FileID);
44 std::cerr << "In file included from " << Buffer->getBufferIdentifier()
45 << ":" << LineNo << ":\n";
46}
47
48/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
49/// any characters in LineNo that intersect the SourceRange.
50void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
51 unsigned LineNo,
52 std::string &CaratLine,
53 const std::string &SourceLine) {
54 assert(CaratLine.size() == SourceLine.size() &&
55 "Expect a correspondence between source and carat line!");
56 if (!R.isValid()) return;
57
58 unsigned StartLineNo = SourceMgr.getLineNumber(R.Begin());
59 if (StartLineNo > LineNo) return; // No intersection.
60
61 unsigned EndLineNo = SourceMgr.getLineNumber(R.End());
62 if (EndLineNo < LineNo) return; // No intersection.
63
64 // Compute the column number of the start.
65 unsigned StartColNo = 0;
66 if (StartLineNo == LineNo) {
67 StartColNo = SourceMgr.getColumnNumber(R.Begin());
68 if (StartColNo) --StartColNo; // Zero base the col #.
69 }
70
71 // Pick the first non-whitespace column.
72 while (StartColNo < SourceLine.size() &&
73 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
74 ++StartColNo;
75
76 // Compute the column number of the end.
77 unsigned EndColNo = CaratLine.size();
78 if (EndLineNo == LineNo) {
79 EndColNo = SourceMgr.getColumnNumber(R.End());
80 if (EndColNo) {
81 --EndColNo; // Zero base the col #.
82
83 // Add in the length of the token, so that we cover multi-char tokens.
84 EndColNo += GetTokenLength(R.End());
85 } else {
86 EndColNo = CaratLine.size();
87 }
88 }
89
90 // Pick the last non-whitespace column.
91 while (EndColNo-1 &&
92 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
93 --EndColNo;
94
95 // Fill the range with ~'s.
96 assert(StartColNo <= EndColNo && "Invalid range!");
97 for (unsigned i = StartColNo; i != EndColNo; ++i)
98 CaratLine[i] = '~';
99}
100
101/// GetTokenLength - Given the source location of a token, determine its length.
102/// This is a fully general function that uses a lexer to relex the token.
103unsigned TextDiagnosticPrinter::GetTokenLength(SourceLocation Loc) {
104 const char *StrData =
105 SourceMgr.getCharacterData(SourceMgr.getLogicalLoc(Loc));
106
107 // Note, this could be special cased for common tokens like identifiers, ')',
108 // etc to make this faster, if it mattered.
109
110 unsigned FileID = Loc.getFileID();
111
112 // Create a lexer starting at the beginning of this token.
113 Lexer TheLexer(SourceMgr.getBuffer(FileID), FileID,
114 *ThePreprocessor, StrData);
115
116 LexerToken TheTok;
117 TheLexer.LexRawToken(TheTok);
118
119 return TheTok.getLength();
120}
121
122void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
123 SourceLocation Pos,
124 diag::kind ID,
125 const std::string *Strs,
126 unsigned NumStrs,
127 const SourceRange *Ranges,
128 unsigned NumRanges) {
129 unsigned LineNo = 0, FilePos = 0, FileID = 0, ColNo = 0;
130 unsigned LineStart = 0, LineEnd = 0;
131 const llvm::MemoryBuffer *Buffer = 0;
132
133 if (Pos.isValid()) {
134 LineNo = SourceMgr.getLineNumber(Pos);
135 FileID = SourceMgr.getLogicalLoc(Pos).getFileID();
136
137 // First, if this diagnostic is not in the main file, print out the
138 // "included from" lines.
139 if (LastWarningLoc != SourceMgr.getIncludeLoc(FileID)) {
140 LastWarningLoc = SourceMgr.getIncludeLoc(FileID);
141 PrintIncludeStack(LastWarningLoc);
142 }
143
144 // Compute the column number. Rewind from the current position to the start
145 // of the line.
146 ColNo = SourceMgr.getColumnNumber(Pos);
147 FilePos = SourceMgr.getSourceFilePos(Pos);
148 LineStart = FilePos-ColNo+1; // Column # is 1-based
149
150 // Compute the line end. Scan forward from the error position to the end of
151 // the line.
152 Buffer = SourceMgr.getBuffer(FileID);
153 const char *Buf = Buffer->getBufferStart();
154 const char *BufEnd = Buffer->getBufferEnd();
155 LineEnd = FilePos;
156 while (Buf+LineEnd != BufEnd &&
157 Buf[LineEnd] != '\n' && Buf[LineEnd] != '\r')
158 ++LineEnd;
159
160 std::cerr << Buffer->getBufferIdentifier()
161 << ":" << LineNo << ":";
162 if (ColNo && !NoShowColumn)
163 std::cerr << ColNo << ":";
164 std::cerr << " ";
165 }
166
167 switch (Level) {
168 default: assert(0 && "Unknown diagnostic type!");
169 case Diagnostic::Note: std::cerr << "note: "; break;
170 case Diagnostic::Warning: std::cerr << "warning: "; break;
171 case Diagnostic::Error: std::cerr << "error: "; break;
172 case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
173 case Diagnostic::Sorry: std::cerr << "sorry, unimplemented: ";
174 break;
175 }
176
177 std::cerr << FormatDiagnostic(Level, ID, Strs, NumStrs) << "\n";
178
179 if (!NoCaretDiagnostics && Pos.isValid()) {
180 // Get the line of the source file.
181 const char *Buf = Buffer->getBufferStart();
182 std::string SourceLine(Buf+LineStart, Buf+LineEnd);
183
184 // Create a line for the carat that is filled with spaces that is the same
185 // length as the line of source code.
186 std::string CaratLine(LineEnd-LineStart, ' ');
187
188 // Highlight all of the characters covered by Ranges with ~ characters.
189 for (unsigned i = 0; i != NumRanges; ++i)
190 HighlightRange(Ranges[i], LineNo, CaratLine, SourceLine);
191
192 // Next, insert the carat itself.
193 if (ColNo-1 < CaratLine.size())
194 CaratLine[ColNo-1] = '^';
195 else
196 CaratLine.push_back('^');
197
198 // Scan the source line, looking for tabs. If we find any, manually expand
199 // them to 8 characters and update the CaratLine to match.
200 for (unsigned i = 0; i != SourceLine.size(); ++i) {
201 if (SourceLine[i] != '\t') continue;
202
203 // Replace this tab with at least one space.
204 SourceLine[i] = ' ';
205
206 // Compute the number of spaces we need to insert.
207 unsigned NumSpaces = ((i+8)&~7) - (i+1);
208 assert(NumSpaces < 8 && "Invalid computation of space amt");
209
210 // Insert spaces into the SourceLine.
211 SourceLine.insert(i+1, NumSpaces, ' ');
212
213 // Insert spaces or ~'s into CaratLine.
214 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
215 }
216
217 // Finally, remove any blank spaces from the end of CaratLine.
218 while (CaratLine[CaratLine.size()-1] == ' ')
219 CaratLine.erase(CaratLine.end()-1);
220
221 // Emit what we have computed.
222 std::cerr << SourceLine << "\n";
223 std::cerr << CaratLine << "\n";
224 }
225}