blob: 463a3d7e8ed1cbb793fa0816868e486d37f7e32d [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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 if (Pos.isInvalid()) return;
36
37 Pos = SourceMgr.getLogicalLoc(Pos);
38
39 // Print out the other include frames first.
40 PrintIncludeStack(SourceMgr.getIncludeLoc(Pos));
41 unsigned LineNo = SourceMgr.getLineNumber(Pos);
42
43 std::cerr << "In file included from " << SourceMgr.getSourceName(Pos)
44 << ":" << 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 Lattner6fe8b272007-10-16 22:36:42 +000057 unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +000058 if (StartLineNo > LineNo) return; // No intersection.
59
Chris Lattner6fe8b272007-10-16 22:36:42 +000060 unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner6fe8b272007-10-16 22:36:42 +000066 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner6fe8b272007-10-16 22:36:42 +000078 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner761d76b2007-10-17 21:18:47 +000083 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Chris Lattner4b009652007-07-25 00:24:17 +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
Chris Lattner4478db92007-11-30 22:53:43 +0000100void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
101 Diagnostic::Level Level,
Chris Lattner4b009652007-07-25 00:24:17 +0000102 SourceLocation Pos,
103 diag::kind ID,
104 const std::string *Strs,
105 unsigned NumStrs,
106 const SourceRange *Ranges,
107 unsigned NumRanges) {
108 unsigned LineNo = 0, ColNo = 0;
109 const char *LineStart = 0, *LineEnd = 0;
110
111 if (Pos.isValid()) {
112 SourceLocation LPos = SourceMgr.getLogicalLoc(Pos);
113 LineNo = SourceMgr.getLineNumber(LPos);
114
115 // First, if this diagnostic is not in the main file, print out the
116 // "included from" lines.
117 if (LastWarningLoc != SourceMgr.getIncludeLoc(LPos)) {
118 LastWarningLoc = SourceMgr.getIncludeLoc(LPos);
119 PrintIncludeStack(LastWarningLoc);
120 }
121
122 // Compute the column number. Rewind from the current position to the start
123 // of the line.
124 ColNo = SourceMgr.getColumnNumber(LPos);
125 const char *TokLogicalPtr = SourceMgr.getCharacterData(LPos);
126 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
127
128 // Compute the line end. Scan forward from the error position to the end of
129 // the line.
130 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(LPos.getFileID());
131 const char *BufEnd = Buffer->getBufferEnd();
132 LineEnd = TokLogicalPtr;
133 while (LineEnd != BufEnd &&
134 *LineEnd != '\n' && *LineEnd != '\r')
135 ++LineEnd;
136
137 std::cerr << Buffer->getBufferIdentifier()
138 << ":" << LineNo << ":";
139 if (ColNo && !NoShowColumn)
140 std::cerr << ColNo << ":";
141 std::cerr << " ";
142 }
143
144 switch (Level) {
145 default: assert(0 && "Unknown diagnostic type!");
146 case Diagnostic::Note: std::cerr << "note: "; break;
147 case Diagnostic::Warning: std::cerr << "warning: "; break;
148 case Diagnostic::Error: std::cerr << "error: "; break;
149 case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000150 break;
151 }
152
Chris Lattner4478db92007-11-30 22:53:43 +0000153 std::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Chris Lattner4b009652007-07-25 00:24:17 +0000154
155 if (!NoCaretDiagnostics && Pos.isValid()) {
156 // Get the line of the source file.
157 std::string SourceLine(LineStart, LineEnd);
158
159 // Create a line for the carat that is filled with spaces that is the same
160 // length as the line of source code.
161 std::string CaratLine(LineEnd-LineStart, ' ');
162
163 // Highlight all of the characters covered by Ranges with ~ characters.
164 for (unsigned i = 0; i != NumRanges; ++i)
165 HighlightRange(Ranges[i], LineNo, CaratLine, SourceLine);
166
167 // Next, insert the carat itself.
168 if (ColNo-1 < CaratLine.size())
169 CaratLine[ColNo-1] = '^';
170 else
171 CaratLine.push_back('^');
172
173 // Scan the source line, looking for tabs. If we find any, manually expand
174 // them to 8 characters and update the CaratLine to match.
175 for (unsigned i = 0; i != SourceLine.size(); ++i) {
176 if (SourceLine[i] != '\t') continue;
177
178 // Replace this tab with at least one space.
179 SourceLine[i] = ' ';
180
181 // Compute the number of spaces we need to insert.
182 unsigned NumSpaces = ((i+8)&~7) - (i+1);
183 assert(NumSpaces < 8 && "Invalid computation of space amt");
184
185 // Insert spaces into the SourceLine.
186 SourceLine.insert(i+1, NumSpaces, ' ');
187
188 // Insert spaces or ~'s into CaratLine.
189 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
190 }
191
192 // Finally, remove any blank spaces from the end of CaratLine.
193 while (CaratLine[CaratLine.size()-1] == ' ')
194 CaratLine.erase(CaratLine.end()-1);
195
196 // Emit what we have computed.
197 std::cerr << SourceLine << "\n";
198 std::cerr << CaratLine << "\n";
199 }
200}