blob: 76809d7dea9021528c39f6ae44ba19f44cab5ffa [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 Lattner311ff022007-10-16 22:36:42 +000057 unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +000058 if (StartLineNo > LineNo) return; // No intersection.
59
Chris Lattner311ff022007-10-16 22:36:42 +000060 unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.getEnd());
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 Lattner311ff022007-10-16 22:36:42 +000066 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
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 Lattner311ff022007-10-16 22:36:42 +000078 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
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.
Chris Lattner311ff022007-10-16 22:36:42 +000083 EndColNo += GetTokenLength(R.getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +000084 } 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);
Chris Lattner2b65a9d2007-10-17 20:53:57 +0000107 const char *BufEnd = SourceMgr.getBufferData(Loc.getFileID()).second;
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
Chris Lattner2b65a9d2007-10-17 20:53:57 +0000113 // Create a langops struct and enable trigraphs. This is sufficient for
114 // measuring tokens.
115 LangOptions LangOpts;
116 LangOpts.Trigraphs = true;
117
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 // Create a lexer starting at the beginning of this token.
Chris Lattner2b65a9d2007-10-17 20:53:57 +0000119 Lexer TheLexer(Loc, LangOpts, StrData, BufEnd);
Chris Lattnerd2177732007-07-20 16:59:19 +0000120 Token TheTok;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 TheLexer.LexRawToken(TheTok);
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 return TheTok.getLength();
123}
124
125void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
126 SourceLocation Pos,
127 diag::kind ID,
128 const std::string *Strs,
129 unsigned NumStrs,
130 const SourceRange *Ranges,
131 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000132 unsigned LineNo = 0, ColNo = 0;
133 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000134
135 if (Pos.isValid()) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000136 SourceLocation LPos = SourceMgr.getLogicalLoc(Pos);
137 LineNo = SourceMgr.getLineNumber(LPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000138
139 // First, if this diagnostic is not in the main file, print out the
140 // "included from" lines.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000141 if (LastWarningLoc != SourceMgr.getIncludeLoc(LPos)) {
142 LastWarningLoc = SourceMgr.getIncludeLoc(LPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 PrintIncludeStack(LastWarningLoc);
144 }
145
146 // Compute the column number. Rewind from the current position to the start
147 // of the line.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000148 ColNo = SourceMgr.getColumnNumber(LPos);
149 const char *TokLogicalPtr = SourceMgr.getCharacterData(LPos);
150 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Reid Spencer5f016e22007-07-11 17:01:13 +0000151
152 // Compute the line end. Scan forward from the error position to the end of
153 // the line.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000154 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(LPos.getFileID());
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000156 LineEnd = TokLogicalPtr;
157 while (LineEnd != BufEnd &&
158 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 ++LineEnd;
160
161 std::cerr << Buffer->getBufferIdentifier()
162 << ":" << LineNo << ":";
163 if (ColNo && !NoShowColumn)
164 std::cerr << ColNo << ":";
165 std::cerr << " ";
166 }
167
168 switch (Level) {
169 default: assert(0 && "Unknown diagnostic type!");
170 case Diagnostic::Note: std::cerr << "note: "; break;
171 case Diagnostic::Warning: std::cerr << "warning: "; break;
172 case Diagnostic::Error: std::cerr << "error: "; break;
173 case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 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.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000181 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000182
183 // Create a line for the carat that is filled with spaces that is the same
184 // length as the line of source code.
185 std::string CaratLine(LineEnd-LineStart, ' ');
186
187 // Highlight all of the characters covered by Ranges with ~ characters.
188 for (unsigned i = 0; i != NumRanges; ++i)
189 HighlightRange(Ranges[i], LineNo, CaratLine, SourceLine);
190
191 // Next, insert the carat itself.
192 if (ColNo-1 < CaratLine.size())
193 CaratLine[ColNo-1] = '^';
194 else
195 CaratLine.push_back('^');
196
197 // Scan the source line, looking for tabs. If we find any, manually expand
198 // them to 8 characters and update the CaratLine to match.
199 for (unsigned i = 0; i != SourceLine.size(); ++i) {
200 if (SourceLine[i] != '\t') continue;
201
202 // Replace this tab with at least one space.
203 SourceLine[i] = ' ';
204
205 // Compute the number of spaces we need to insert.
206 unsigned NumSpaces = ((i+8)&~7) - (i+1);
207 assert(NumSpaces < 8 && "Invalid computation of space amt");
208
209 // Insert spaces into the SourceLine.
210 SourceLine.insert(i+1, NumSpaces, ' ');
211
212 // Insert spaces or ~'s into CaratLine.
213 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
214 }
215
216 // Finally, remove any blank spaces from the end of CaratLine.
217 while (CaratLine[CaratLine.size()-1] == ' ')
218 CaratLine.erase(CaratLine.end()-1);
219
220 // Emit what we have computed.
221 std::cerr << SourceLine << "\n";
222 std::cerr << CaratLine << "\n";
223 }
224}