blob: f37732c24165ab4d07030a61bb39d32056cd5405 [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) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000035 if (Pos.isInvalid()) return;
36
37 Pos = SourceMgr.getLogicalLoc(Pos);
38
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Print out the other include frames first.
Chris Lattner9dc1f532007-07-20 16:37:10 +000040 PrintIncludeStack(SourceMgr.getIncludeLoc(Pos));
Reid Spencer5f016e22007-07-11 17:01:13 +000041 unsigned LineNo = SourceMgr.getLineNumber(Pos);
42
Chris Lattner9dc1f532007-07-20 16:37:10 +000043 std::cerr << "In file included from " << SourceMgr.getSourceName(Pos)
Reid Spencer5f016e22007-07-11 17:01:13 +000044 << ":" << LineNo << ":\n";
45}
46
47/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
48/// any characters in LineNo that intersect the SourceRange.
49void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
50 unsigned LineNo,
51 std::string &CaratLine,
52 const std::string &SourceLine) {
53 assert(CaratLine.size() == SourceLine.size() &&
54 "Expect a correspondence between source and carat line!");
55 if (!R.isValid()) return;
56
Chris Lattner9dc1f532007-07-20 16:37:10 +000057 unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.Begin());
Reid Spencer5f016e22007-07-11 17:01:13 +000058 if (StartLineNo > LineNo) return; // No intersection.
59
Chris Lattner9dc1f532007-07-20 16:37:10 +000060 unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.End());
Reid Spencer5f016e22007-07-11 17:01:13 +000061 if (EndLineNo < LineNo) return; // No intersection.
62
63 // Compute the column number of the start.
64 unsigned StartColNo = 0;
65 if (StartLineNo == LineNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000066 StartColNo = SourceMgr.getLogicalColumnNumber(R.Begin());
Reid Spencer5f016e22007-07-11 17:01:13 +000067 if (StartColNo) --StartColNo; // Zero base the col #.
68 }
69
70 // Pick the first non-whitespace column.
71 while (StartColNo < SourceLine.size() &&
72 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
73 ++StartColNo;
74
75 // Compute the column number of the end.
76 unsigned EndColNo = CaratLine.size();
77 if (EndLineNo == LineNo) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000078 EndColNo = SourceMgr.getLogicalColumnNumber(R.End());
Reid Spencer5f016e22007-07-11 17:01:13 +000079 if (EndColNo) {
80 --EndColNo; // Zero base the col #.
81
82 // Add in the length of the token, so that we cover multi-char tokens.
83 EndColNo += GetTokenLength(R.End());
84 } else {
85 EndColNo = CaratLine.size();
86 }
87 }
88
89 // Pick the last non-whitespace column.
90 while (EndColNo-1 &&
91 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
92 --EndColNo;
93
94 // Fill the range with ~'s.
95 assert(StartColNo <= EndColNo && "Invalid range!");
96 for (unsigned i = StartColNo; i != EndColNo; ++i)
97 CaratLine[i] = '~';
98}
99
100/// GetTokenLength - Given the source location of a token, determine its length.
101/// This is a fully general function that uses a lexer to relex the token.
102unsigned TextDiagnosticPrinter::GetTokenLength(SourceLocation Loc) {
Chris Lattner20e6ccd2007-07-16 06:46:50 +0000103 // If this comes from a macro expansion, we really do want the macro name, not
104 // the token this macro expanded to.
105 Loc = SourceMgr.getLogicalLoc(Loc);
106 const char *StrData = SourceMgr.getCharacterData(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000107
Chris Lattner20e6ccd2007-07-16 06:46:50 +0000108 // TODO: this could be special cased for common tokens like identifiers, ')',
109 // etc to make this faster, if it mattered. This could use
110 // Lexer::isObviouslySimpleCharacter for example.
Reid Spencer5f016e22007-07-11 17:01:13 +0000111 unsigned FileID = Loc.getFileID();
112
113 // Create a lexer starting at the beginning of this token.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000114 Lexer TheLexer(SourceMgr.getBuffer(FileID), Loc,
115 *ThePreprocessor, StrData);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116
117 LexerToken TheTok;
118 TheLexer.LexRawToken(TheTok);
119
120 return TheTok.getLength();
121}
122
123void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
124 SourceLocation Pos,
125 diag::kind ID,
126 const std::string *Strs,
127 unsigned NumStrs,
128 const SourceRange *Ranges,
129 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000130 unsigned LineNo = 0, ColNo = 0;
131 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132
133 if (Pos.isValid()) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000134 SourceLocation LPos = SourceMgr.getLogicalLoc(Pos);
135 LineNo = SourceMgr.getLineNumber(LPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000136
137 // First, if this diagnostic is not in the main file, print out the
138 // "included from" lines.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000139 if (LastWarningLoc != SourceMgr.getIncludeLoc(LPos)) {
140 LastWarningLoc = SourceMgr.getIncludeLoc(LPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 PrintIncludeStack(LastWarningLoc);
142 }
143
144 // Compute the column number. Rewind from the current position to the start
145 // of the line.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000146 ColNo = SourceMgr.getColumnNumber(LPos);
147 const char *TokLogicalPtr = SourceMgr.getCharacterData(LPos);
148 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Reid Spencer5f016e22007-07-11 17:01:13 +0000149
150 // Compute the line end. Scan forward from the error position to the end of
151 // the line.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000152 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(LPos.getFileID());
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000154 LineEnd = TokLogicalPtr;
155 while (LineEnd != BufEnd &&
156 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 ++LineEnd;
158
159 std::cerr << Buffer->getBufferIdentifier()
160 << ":" << LineNo << ":";
161 if (ColNo && !NoShowColumn)
162 std::cerr << ColNo << ":";
163 std::cerr << " ";
164 }
165
166 switch (Level) {
167 default: assert(0 && "Unknown diagnostic type!");
168 case Diagnostic::Note: std::cerr << "note: "; break;
169 case Diagnostic::Warning: std::cerr << "warning: "; break;
170 case Diagnostic::Error: std::cerr << "error: "; break;
171 case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
172 case Diagnostic::Sorry: std::cerr << "sorry, unimplemented: ";
173 break;
174 }
175
176 std::cerr << FormatDiagnostic(Level, ID, Strs, NumStrs) << "\n";
177
178 if (!NoCaretDiagnostics && Pos.isValid()) {
179 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000180 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000181
182 // Create a line for the carat that is filled with spaces that is the same
183 // length as the line of source code.
184 std::string CaratLine(LineEnd-LineStart, ' ');
185
186 // Highlight all of the characters covered by Ranges with ~ characters.
187 for (unsigned i = 0; i != NumRanges; ++i)
188 HighlightRange(Ranges[i], LineNo, CaratLine, SourceLine);
189
190 // Next, insert the carat itself.
191 if (ColNo-1 < CaratLine.size())
192 CaratLine[ColNo-1] = '^';
193 else
194 CaratLine.push_back('^');
195
196 // Scan the source line, looking for tabs. If we find any, manually expand
197 // them to 8 characters and update the CaratLine to match.
198 for (unsigned i = 0; i != SourceLine.size(); ++i) {
199 if (SourceLine[i] != '\t') continue;
200
201 // Replace this tab with at least one space.
202 SourceLine[i] = ' ';
203
204 // Compute the number of spaces we need to insert.
205 unsigned NumSpaces = ((i+8)&~7) - (i+1);
206 assert(NumSpaces < 8 && "Invalid computation of space amt");
207
208 // Insert spaces into the SourceLine.
209 SourceLine.insert(i+1, NumSpaces, ' ');
210
211 // Insert spaces or ~'s into CaratLine.
212 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
213 }
214
215 // Finally, remove any blank spaces from the end of CaratLine.
216 while (CaratLine[CaratLine.size()-1] == ' ')
217 CaratLine.erase(CaratLine.end()-1);
218
219 // Emit what we have computed.
220 std::cerr << SourceLine << "\n";
221 std::cerr << CaratLine << "\n";
222 }
223}