blob: f1a36ba503439293cdba3b9fc35daab8ba1096c6 [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) {
Chris Lattner20e6ccd2007-07-16 06:46:50 +0000104 // If this comes from a macro expansion, we really do want the macro name, not
105 // the token this macro expanded to.
106 Loc = SourceMgr.getLogicalLoc(Loc);
107 const char *StrData = SourceMgr.getCharacterData(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000108
Chris Lattner20e6ccd2007-07-16 06:46:50 +0000109 // TODO: this could be special cased for common tokens like identifiers, ')',
110 // etc to make this faster, if it mattered. This could use
111 // Lexer::isObviouslySimpleCharacter for example.
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 unsigned FileID = Loc.getFileID();
113
114 // Create a lexer starting at the beginning of this token.
115 Lexer TheLexer(SourceMgr.getBuffer(FileID), FileID,
116 *ThePreprocessor, StrData);
117
118 LexerToken TheTok;
119 TheLexer.LexRawToken(TheTok);
120
121 return TheTok.getLength();
122}
123
124void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
125 SourceLocation Pos,
126 diag::kind ID,
127 const std::string *Strs,
128 unsigned NumStrs,
129 const SourceRange *Ranges,
130 unsigned NumRanges) {
131 unsigned LineNo = 0, FilePos = 0, FileID = 0, ColNo = 0;
132 unsigned LineStart = 0, LineEnd = 0;
133 const llvm::MemoryBuffer *Buffer = 0;
134
135 if (Pos.isValid()) {
136 LineNo = SourceMgr.getLineNumber(Pos);
137 FileID = SourceMgr.getLogicalLoc(Pos).getFileID();
138
139 // First, if this diagnostic is not in the main file, print out the
140 // "included from" lines.
141 if (LastWarningLoc != SourceMgr.getIncludeLoc(FileID)) {
142 LastWarningLoc = SourceMgr.getIncludeLoc(FileID);
143 PrintIncludeStack(LastWarningLoc);
144 }
145
146 // Compute the column number. Rewind from the current position to the start
147 // of the line.
148 ColNo = SourceMgr.getColumnNumber(Pos);
149 FilePos = SourceMgr.getSourceFilePos(Pos);
150 LineStart = FilePos-ColNo+1; // Column # is 1-based
151
152 // Compute the line end. Scan forward from the error position to the end of
153 // the line.
154 Buffer = SourceMgr.getBuffer(FileID);
155 const char *Buf = Buffer->getBufferStart();
156 const char *BufEnd = Buffer->getBufferEnd();
157 LineEnd = FilePos;
158 while (Buf+LineEnd != BufEnd &&
159 Buf[LineEnd] != '\n' && Buf[LineEnd] != '\r')
160 ++LineEnd;
161
162 std::cerr << Buffer->getBufferIdentifier()
163 << ":" << LineNo << ":";
164 if (ColNo && !NoShowColumn)
165 std::cerr << ColNo << ":";
166 std::cerr << " ";
167 }
168
169 switch (Level) {
170 default: assert(0 && "Unknown diagnostic type!");
171 case Diagnostic::Note: std::cerr << "note: "; break;
172 case Diagnostic::Warning: std::cerr << "warning: "; break;
173 case Diagnostic::Error: std::cerr << "error: "; break;
174 case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
175 case Diagnostic::Sorry: std::cerr << "sorry, unimplemented: ";
176 break;
177 }
178
179 std::cerr << FormatDiagnostic(Level, ID, Strs, NumStrs) << "\n";
180
181 if (!NoCaretDiagnostics && Pos.isValid()) {
182 // Get the line of the source file.
183 const char *Buf = Buffer->getBufferStart();
184 std::string SourceLine(Buf+LineStart, Buf+LineEnd);
185
186 // Create a line for the carat that is filled with spaces that is the same
187 // length as the line of source code.
188 std::string CaratLine(LineEnd-LineStart, ' ');
189
190 // Highlight all of the characters covered by Ranges with ~ characters.
191 for (unsigned i = 0; i != NumRanges; ++i)
192 HighlightRange(Ranges[i], LineNo, CaratLine, SourceLine);
193
194 // Next, insert the carat itself.
195 if (ColNo-1 < CaratLine.size())
196 CaratLine[ColNo-1] = '^';
197 else
198 CaratLine.push_back('^');
199
200 // Scan the source line, looking for tabs. If we find any, manually expand
201 // them to 8 characters and update the CaratLine to match.
202 for (unsigned i = 0; i != SourceLine.size(); ++i) {
203 if (SourceLine[i] != '\t') continue;
204
205 // Replace this tab with at least one space.
206 SourceLine[i] = ' ';
207
208 // Compute the number of spaces we need to insert.
209 unsigned NumSpaces = ((i+8)&~7) - (i+1);
210 assert(NumSpaces < 8 && "Invalid computation of space amt");
211
212 // Insert spaces into the SourceLine.
213 SourceLine.insert(i+1, NumSpaces, ' ');
214
215 // Insert spaces or ~'s into CaratLine.
216 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
217 }
218
219 // Finally, remove any blank spaces from the end of CaratLine.
220 while (CaratLine[CaratLine.size()-1] == ' ')
221 CaratLine.erase(CaratLine.end()-1);
222
223 // Emit what we have computed.
224 std::cerr << SourceLine << "\n";
225 std::cerr << CaratLine << "\n";
226 }
227}