blob: 598270b016ba66c0c6326617303b6df5f96010f7 [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 Kremenek9c728dc2007-12-12 22:39:36 +000034PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000035 if (Pos.isInvalid()) return;
36
Ted Kremenek9c728dc2007-12-12 22:39:36 +000037 Pos = Pos.getLogicalLoc();
Chris Lattner9dc1f532007-07-20 16:37:10 +000038
Reid Spencer5f016e22007-07-11 17:01:13 +000039 // Print out the other include frames first.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000040 PrintIncludeStack(Pos.getIncludeLoc());
41 unsigned LineNo = Pos.getLineNumber();
Reid Spencer5f016e22007-07-11 17:01:13 +000042
Ted Kremenek9c728dc2007-12-12 22:39:36 +000043 std::cerr << "In file included from " << Pos.getSourceName()
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,
Ted Kremenek9c728dc2007-12-12 22:39:36 +000050 SourceManager& SourceMgr,
Reid Spencer5f016e22007-07-11 17:01:13 +000051 unsigned LineNo,
52 std::string &CaratLine,
Ted Kremenek2eefd862007-12-11 22:57:35 +000053 const std::string &SourceLine) {
Reid Spencer5f016e22007-07-11 17:01:13 +000054 assert(CaratLine.size() == SourceLine.size() &&
55 "Expect a correspondence between source and carat line!");
56 if (!R.isValid()) return;
57
Ted Kremenek9c728dc2007-12-12 22:39:36 +000058 unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +000059 if (StartLineNo > LineNo) return; // No intersection.
60
Ted Kremenek9c728dc2007-12-12 22:39:36 +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) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +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) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +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.
Ted Kremenek9c728dc2007-12-12 22:39:36 +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,
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000103 FullSourceLoc Pos,
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 diag::kind ID,
105 const std::string *Strs,
106 unsigned NumStrs,
107 const SourceRange *Ranges,
108 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000109 unsigned LineNo = 0, ColNo = 0;
110 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000111
112 if (Pos.isValid()) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000113 FullSourceLoc LPos = Pos.getLogicalLoc();
114 LineNo = LPos.getLineNumber();
Reid Spencer5f016e22007-07-11 17:01:13 +0000115
116 // First, if this diagnostic is not in the main file, print out the
117 // "included from" lines.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000118 if (LastWarningLoc != LPos.getIncludeLoc()) {
119 LastWarningLoc = LPos.getIncludeLoc();
120 PrintIncludeStack(LastWarningLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 }
122
123 // Compute the column number. Rewind from the current position to the start
124 // of the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000125 ColNo = LPos.getColumnNumber();
126 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000127 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Reid Spencer5f016e22007-07-11 17:01:13 +0000128
129 // Compute the line end. Scan forward from the error position to the end of
130 // the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000131 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000133 LineEnd = TokLogicalPtr;
134 while (LineEnd != BufEnd &&
135 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ++LineEnd;
137
138 std::cerr << Buffer->getBufferIdentifier()
139 << ":" << LineNo << ":";
140 if (ColNo && !NoShowColumn)
141 std::cerr << ColNo << ":";
142 std::cerr << " ";
143 }
144
145 switch (Level) {
146 default: assert(0 && "Unknown diagnostic type!");
147 case Diagnostic::Note: std::cerr << "note: "; break;
148 case Diagnostic::Warning: std::cerr << "warning: "; break;
149 case Diagnostic::Error: std::cerr << "error: "; break;
150 case Diagnostic::Fatal: std::cerr << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 break;
152 }
153
Chris Lattner07506182007-11-30 22:53:43 +0000154 std::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000155
156 if (!NoCaretDiagnostics && Pos.isValid()) {
157 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000158 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159
160 // Create a line for the carat that is filled with spaces that is the same
161 // length as the line of source code.
162 std::string CaratLine(LineEnd-LineStart, ' ');
163
164 // Highlight all of the characters covered by Ranges with ~ characters.
165 for (unsigned i = 0; i != NumRanges; ++i)
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000166 HighlightRange(Ranges[i], Pos.getManager(),LineNo, CaratLine, SourceLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167
168 // Next, insert the carat itself.
169 if (ColNo-1 < CaratLine.size())
170 CaratLine[ColNo-1] = '^';
171 else
172 CaratLine.push_back('^');
173
174 // Scan the source line, looking for tabs. If we find any, manually expand
175 // them to 8 characters and update the CaratLine to match.
176 for (unsigned i = 0; i != SourceLine.size(); ++i) {
177 if (SourceLine[i] != '\t') continue;
178
179 // Replace this tab with at least one space.
180 SourceLine[i] = ' ';
181
182 // Compute the number of spaces we need to insert.
183 unsigned NumSpaces = ((i+8)&~7) - (i+1);
184 assert(NumSpaces < 8 && "Invalid computation of space amt");
185
186 // Insert spaces into the SourceLine.
187 SourceLine.insert(i+1, NumSpaces, ' ');
188
189 // Insert spaces or ~'s into CaratLine.
190 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
191 }
192
193 // Finally, remove any blank spaces from the end of CaratLine.
194 while (CaratLine[CaratLine.size()-1] == ' ')
195 CaratLine.erase(CaratLine.end()-1);
196
197 // Emit what we have computed.
198 std::cerr << SourceLine << "\n";
199 std::cerr << CaratLine << "\n";
200 }
201}