blob: 012c3abf946605f6e66600506fc570a77828a2e4 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Ted Kremenekbdd30c22008-01-14 16:44:48 +000021#include "llvm/Support/Streams.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#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 Kremenekbdd30c22008-01-14 16:44:48 +000043 llvm::cerr << "In file included from " << Pos.getSourceName()
44 << ":" << LineNo << ":\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
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,
Chris Lattnere41b7cd2008-01-12 06:43:35 +000050 SourceManager& SourceMgr,
51 unsigned LineNo, unsigned FileID,
Reid Spencer5f016e22007-07-11 17:01:13 +000052 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
Chris Lattnere41b7cd2008-01-12 06:43:35 +000058 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(R.getBegin());
59 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
60 if (StartLineNo > LineNo || LogicalStart.getFileID() != FileID)
61 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000062
Chris Lattnere41b7cd2008-01-12 06:43:35 +000063 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
64 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek2b70af42008-03-27 05:52:45 +000065 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000066 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000067
68 // Compute the column number of the start.
69 unsigned StartColNo = 0;
70 if (StartLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000071 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +000072 if (StartColNo) --StartColNo; // Zero base the col #.
73 }
74
75 // Pick the first non-whitespace column.
76 while (StartColNo < SourceLine.size() &&
77 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
78 ++StartColNo;
79
80 // Compute the column number of the end.
81 unsigned EndColNo = CaratLine.size();
82 if (EndLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000083 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +000084 if (EndColNo) {
85 --EndColNo; // Zero base the col #.
86
87 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000088 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +000089 } else {
90 EndColNo = CaratLine.size();
91 }
92 }
93
94 // Pick the last non-whitespace column.
95 while (EndColNo-1 &&
96 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
97 --EndColNo;
98
99 // Fill the range with ~'s.
100 assert(StartColNo <= EndColNo && "Invalid range!");
101 for (unsigned i = StartColNo; i != EndColNo; ++i)
102 CaratLine[i] = '~';
103}
104
Chris Lattner07506182007-11-30 22:53:43 +0000105void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
106 Diagnostic::Level Level,
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000107 FullSourceLoc Pos,
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 diag::kind ID,
109 const std::string *Strs,
110 unsigned NumStrs,
111 const SourceRange *Ranges,
112 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000113 unsigned LineNo = 0, ColNo = 0;
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000114 unsigned FileID = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000115 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000116
117 if (Pos.isValid()) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000118 FullSourceLoc LPos = Pos.getLogicalLoc();
119 LineNo = LPos.getLineNumber();
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000120 FileID = LPos.getLocation().getFileID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000121
122 // First, if this diagnostic is not in the main file, print out the
123 // "included from" lines.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000124 if (LastWarningLoc != LPos.getIncludeLoc()) {
125 LastWarningLoc = LPos.getIncludeLoc();
126 PrintIncludeStack(LastWarningLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 }
128
129 // Compute the column number. Rewind from the current position to the start
130 // of the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000131 ColNo = LPos.getColumnNumber();
132 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000133 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner0cbc2152008-01-25 00:01:10 +0000134
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 // Compute the line end. Scan forward from the error position to the end of
136 // the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000137 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000138 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000139 LineEnd = TokLogicalPtr;
140 while (LineEnd != BufEnd &&
141 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 ++LineEnd;
143
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000144 llvm::cerr << Buffer->getBufferIdentifier()
145 << ":" << LineNo << ":";
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 if (ColNo && !NoShowColumn)
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000147 llvm::cerr << ColNo << ":";
148 llvm::cerr << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150
151 switch (Level) {
152 default: assert(0 && "Unknown diagnostic type!");
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000153 case Diagnostic::Note: llvm::cerr << "note: "; break;
154 case Diagnostic::Warning: llvm::cerr << "warning: "; break;
155 case Diagnostic::Error: llvm::cerr << "error: "; break;
156 case Diagnostic::Fatal: llvm::cerr << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 break;
158 }
159
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000160 llvm::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000161
Steve Naroff58de0262008-02-11 22:17:33 +0000162 if (!NoCaretDiagnostics && Pos.isValid() && ((LastLoc != Pos) || Ranges)) {
Steve Naroffefe7f362008-02-08 22:06:17 +0000163 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
164 LastLoc = Pos;
165
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000167 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168
169 // Create a line for the carat that is filled with spaces that is the same
170 // length as the line of source code.
171 std::string CaratLine(LineEnd-LineStart, ' ');
172
173 // Highlight all of the characters covered by Ranges with ~ characters.
174 for (unsigned i = 0; i != NumRanges; ++i)
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000175 HighlightRange(Ranges[i], Pos.getManager(), LineNo, FileID,
176 CaratLine, SourceLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000177
178 // Next, insert the carat itself.
179 if (ColNo-1 < CaratLine.size())
180 CaratLine[ColNo-1] = '^';
181 else
182 CaratLine.push_back('^');
183
184 // Scan the source line, looking for tabs. If we find any, manually expand
185 // them to 8 characters and update the CaratLine to match.
186 for (unsigned i = 0; i != SourceLine.size(); ++i) {
187 if (SourceLine[i] != '\t') continue;
188
189 // Replace this tab with at least one space.
190 SourceLine[i] = ' ';
191
192 // Compute the number of spaces we need to insert.
193 unsigned NumSpaces = ((i+8)&~7) - (i+1);
194 assert(NumSpaces < 8 && "Invalid computation of space amt");
195
196 // Insert spaces into the SourceLine.
197 SourceLine.insert(i+1, NumSpaces, ' ');
198
199 // Insert spaces or ~'s into CaratLine.
200 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
201 }
202
203 // Finally, remove any blank spaces from the end of CaratLine.
204 while (CaratLine[CaratLine.size()-1] == ' ')
205 CaratLine.erase(CaratLine.end()-1);
206
207 // Emit what we have computed.
Ted Kremenekbdd30c22008-01-14 16:44:48 +0000208 llvm::cerr << SourceLine << "\n";
209 llvm::cerr << CaratLine << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 }
211}