blob: d2eaacaace1b06ed1434dbe617c13afa39a965b6 [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::
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000034PrintIncludeStack(SourceLocation Pos, SourceManager& SourceMgr) {
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.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000040 PrintIncludeStack(SourceMgr.getIncludeLoc(Pos),SourceMgr);
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.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000049void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
50 SourceManager& SourceMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +000051 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
Chris Lattner311ff022007-10-16 22:36:42 +000058 unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +000059 if (StartLineNo > LineNo) return; // No intersection.
60
Chris Lattner311ff022007-10-16 22:36:42 +000061 unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +000062 if (EndLineNo < LineNo) return; // No intersection.
63
64 // Compute the column number of the start.
65 unsigned StartColNo = 0;
66 if (StartLineNo == LineNo) {
Chris Lattner311ff022007-10-16 22:36:42 +000067 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +000068 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) {
Chris Lattner311ff022007-10-16 22:36:42 +000079 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +000080 if (EndColNo) {
81 --EndColNo; // Zero base the col #.
82
83 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattner9a611942007-10-17 21:18:47 +000084 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +000085 } 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
Chris Lattner07506182007-11-30 22:53:43 +0000101void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
102 Diagnostic::Level Level,
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 SourceLocation Pos,
104 diag::kind ID,
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000105 SourceManager& SourceMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 const std::string *Strs,
107 unsigned NumStrs,
108 const SourceRange *Ranges,
109 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000110 unsigned LineNo = 0, ColNo = 0;
111 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112
113 if (Pos.isValid()) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000114 SourceLocation LPos = SourceMgr.getLogicalLoc(Pos);
115 LineNo = SourceMgr.getLineNumber(LPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000116
117 // First, if this diagnostic is not in the main file, print out the
118 // "included from" lines.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000119 if (LastWarningLoc != SourceMgr.getIncludeLoc(LPos)) {
120 LastWarningLoc = SourceMgr.getIncludeLoc(LPos);
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000121 PrintIncludeStack(LastWarningLoc,SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123
124 // Compute the column number. Rewind from the current position to the start
125 // of the line.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000126 ColNo = SourceMgr.getColumnNumber(LPos);
127 const char *TokLogicalPtr = SourceMgr.getCharacterData(LPos);
128 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Reid Spencer5f016e22007-07-11 17:01:13 +0000129
130 // Compute the line end. Scan forward from the error position to the end of
131 // the line.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000132 const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(LPos.getFileID());
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000134 LineEnd = TokLogicalPtr;
135 while (LineEnd != BufEnd &&
136 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ++LineEnd;
138
139 std::cerr << Buffer->getBufferIdentifier()
140 << ":" << LineNo << ":";
141 if (ColNo && !NoShowColumn)
142 std::cerr << ColNo << ":";
143 std::cerr << " ";
144 }
145
146 switch (Level) {
147 default: assert(0 && "Unknown diagnostic type!");
148 case Diagnostic::Note: std::cerr << "note: "; break;
149 case Diagnostic::Warning: std::cerr << "warning: "; break;
150 case Diagnostic::Error: std::cerr << "error: "; break;
151 case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 break;
153 }
154
Chris Lattner07506182007-11-30 22:53:43 +0000155 std::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000156
157 if (!NoCaretDiagnostics && Pos.isValid()) {
158 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000159 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000160
161 // Create a line for the carat that is filled with spaces that is the same
162 // length as the line of source code.
163 std::string CaratLine(LineEnd-LineStart, ' ');
164
165 // Highlight all of the characters covered by Ranges with ~ characters.
166 for (unsigned i = 0; i != NumRanges; ++i)
Ted Kremenek7a9d49f2007-12-11 21:27:55 +0000167 HighlightRange(Ranges[i], SourceMgr, LineNo, CaratLine, SourceLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168
169 // Next, insert the carat itself.
170 if (ColNo-1 < CaratLine.size())
171 CaratLine[ColNo-1] = '^';
172 else
173 CaratLine.push_back('^');
174
175 // Scan the source line, looking for tabs. If we find any, manually expand
176 // them to 8 characters and update the CaratLine to match.
177 for (unsigned i = 0; i != SourceLine.size(); ++i) {
178 if (SourceLine[i] != '\t') continue;
179
180 // Replace this tab with at least one space.
181 SourceLine[i] = ' ';
182
183 // Compute the number of spaces we need to insert.
184 unsigned NumSpaces = ((i+8)&~7) - (i+1);
185 assert(NumSpaces < 8 && "Invalid computation of space amt");
186
187 // Insert spaces into the SourceLine.
188 SourceLine.insert(i+1, NumSpaces, ' ');
189
190 // Insert spaces or ~'s into CaratLine.
191 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
192 }
193
194 // Finally, remove any blank spaces from the end of CaratLine.
195 while (CaratLine[CaratLine.size()-1] == ' ')
196 CaratLine.erase(CaratLine.end()-1);
197
198 // Emit what we have computed.
199 std::cerr << SourceLine << "\n";
200 std::cerr << CaratLine << "\n";
201 }
202}