blob: 5d06ebea2d14c2fb8948660f283805f24d9d63c8 [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"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include <string>
22using namespace clang;
23
24static llvm::cl::opt<bool>
25NoShowColumn("fno-show-column",
26 llvm::cl::desc("Do not include column number on diagnostics"));
27static llvm::cl::opt<bool>
28NoCaretDiagnostics("fno-caret-diagnostics",
29 llvm::cl::desc("Do not include source line and caret with"
30 " diagnostics"));
31
32void TextDiagnosticPrinter::
Ted Kremenek9c728dc2007-12-12 22:39:36 +000033PrintIncludeStack(FullSourceLoc Pos) {
Chris Lattner9dc1f532007-07-20 16:37:10 +000034 if (Pos.isInvalid()) return;
35
Ted Kremenek9c728dc2007-12-12 22:39:36 +000036 Pos = Pos.getLogicalLoc();
Chris Lattner9dc1f532007-07-20 16:37:10 +000037
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Print out the other include frames first.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000039 PrintIncludeStack(Pos.getIncludeLoc());
40 unsigned LineNo = Pos.getLineNumber();
Reid Spencer5f016e22007-07-11 17:01:13 +000041
Nate Begeman165b9542008-04-17 18:06:57 +000042 OS << "In file included from " << Pos.getSourceName()
43 << ":" << LineNo << ":\n";
Reid Spencer5f016e22007-07-11 17:01:13 +000044}
45
46/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
47/// any characters in LineNo that intersect the SourceRange.
Ted Kremenek7a9d49f2007-12-11 21:27:55 +000048void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnere41b7cd2008-01-12 06:43:35 +000049 SourceManager& SourceMgr,
50 unsigned LineNo, unsigned FileID,
Reid Spencer5f016e22007-07-11 17:01:13 +000051 std::string &CaratLine,
Ted Kremenek2eefd862007-12-11 22:57:35 +000052 const std::string &SourceLine) {
Reid Spencer5f016e22007-07-11 17:01:13 +000053 assert(CaratLine.size() == SourceLine.size() &&
54 "Expect a correspondence between source and carat line!");
55 if (!R.isValid()) return;
56
Chris Lattnere41b7cd2008-01-12 06:43:35 +000057 SourceLocation LogicalStart = SourceMgr.getLogicalLoc(R.getBegin());
58 unsigned StartLineNo = SourceMgr.getLineNumber(LogicalStart);
59 if (StartLineNo > LineNo || LogicalStart.getFileID() != FileID)
60 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000061
Chris Lattnere41b7cd2008-01-12 06:43:35 +000062 SourceLocation LogicalEnd = SourceMgr.getLogicalLoc(R.getEnd());
63 unsigned EndLineNo = SourceMgr.getLineNumber(LogicalEnd);
Ted Kremenek2b70af42008-03-27 05:52:45 +000064 if (EndLineNo < LineNo || LogicalEnd.getFileID() != FileID)
Chris Lattnere41b7cd2008-01-12 06:43:35 +000065 return; // No intersection.
Reid Spencer5f016e22007-07-11 17:01:13 +000066
67 // Compute the column number of the start.
68 unsigned StartColNo = 0;
69 if (StartLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000070 StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin());
Reid Spencer5f016e22007-07-11 17:01:13 +000071 if (StartColNo) --StartColNo; // Zero base the col #.
72 }
73
74 // Pick the first non-whitespace column.
75 while (StartColNo < SourceLine.size() &&
76 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
77 ++StartColNo;
78
79 // Compute the column number of the end.
80 unsigned EndColNo = CaratLine.size();
81 if (EndLineNo == LineNo) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +000082 EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd());
Reid Spencer5f016e22007-07-11 17:01:13 +000083 if (EndColNo) {
84 --EndColNo; // Zero base the col #.
85
86 // Add in the length of the token, so that we cover multi-char tokens.
Ted Kremenek9c728dc2007-12-12 22:39:36 +000087 EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr);
Reid Spencer5f016e22007-07-11 17:01:13 +000088 } else {
89 EndColNo = CaratLine.size();
90 }
91 }
92
93 // Pick the last non-whitespace column.
94 while (EndColNo-1 &&
95 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
96 --EndColNo;
97
98 // Fill the range with ~'s.
99 assert(StartColNo <= EndColNo && "Invalid range!");
100 for (unsigned i = StartColNo; i != EndColNo; ++i)
101 CaratLine[i] = '~';
102}
103
Chris Lattner07506182007-11-30 22:53:43 +0000104void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
105 Diagnostic::Level Level,
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000106 FullSourceLoc Pos,
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 diag::kind ID,
108 const std::string *Strs,
109 unsigned NumStrs,
110 const SourceRange *Ranges,
111 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000112 unsigned LineNo = 0, ColNo = 0;
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000113 unsigned FileID = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000114 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000115
116 if (Pos.isValid()) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000117 FullSourceLoc LPos = Pos.getLogicalLoc();
118 LineNo = LPos.getLineNumber();
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000119 FileID = LPos.getLocation().getFileID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000120
121 // First, if this diagnostic is not in the main file, print out the
122 // "included from" lines.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000123 if (LastWarningLoc != LPos.getIncludeLoc()) {
124 LastWarningLoc = LPos.getIncludeLoc();
125 PrintIncludeStack(LastWarningLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127
128 // Compute the column number. Rewind from the current position to the start
129 // of the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000130 ColNo = LPos.getColumnNumber();
131 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000132 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner0cbc2152008-01-25 00:01:10 +0000133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // Compute the line end. Scan forward from the error position to the end of
135 // the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000136 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000138 LineEnd = TokLogicalPtr;
139 while (LineEnd != BufEnd &&
140 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 ++LineEnd;
142
Nate Begeman165b9542008-04-17 18:06:57 +0000143 OS << Buffer->getBufferIdentifier() << ":" << LineNo << ":";
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 if (ColNo && !NoShowColumn)
Nate Begeman165b9542008-04-17 18:06:57 +0000145 OS << ColNo << ":";
146 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
148
149 switch (Level) {
150 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman165b9542008-04-17 18:06:57 +0000151 case Diagnostic::Note: OS << "note: "; break;
152 case Diagnostic::Warning: OS << "warning: "; break;
153 case Diagnostic::Error: OS << "error: "; break;
154 case Diagnostic::Fatal: OS << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 break;
156 }
157
Nate Begeman165b9542008-04-17 18:06:57 +0000158 OS << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000159
Steve Naroff58de0262008-02-11 22:17:33 +0000160 if (!NoCaretDiagnostics && Pos.isValid() && ((LastLoc != Pos) || Ranges)) {
Steve Naroffefe7f362008-02-08 22:06:17 +0000161 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
162 LastLoc = Pos;
163
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000165 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000166
167 // Create a line for the carat that is filled with spaces that is the same
168 // length as the line of source code.
169 std::string CaratLine(LineEnd-LineStart, ' ');
170
171 // Highlight all of the characters covered by Ranges with ~ characters.
172 for (unsigned i = 0; i != NumRanges; ++i)
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000173 HighlightRange(Ranges[i], Pos.getManager(), LineNo, FileID,
174 CaratLine, SourceLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000175
176 // Next, insert the carat itself.
177 if (ColNo-1 < CaratLine.size())
178 CaratLine[ColNo-1] = '^';
179 else
180 CaratLine.push_back('^');
181
182 // Scan the source line, looking for tabs. If we find any, manually expand
183 // them to 8 characters and update the CaratLine to match.
184 for (unsigned i = 0; i != SourceLine.size(); ++i) {
185 if (SourceLine[i] != '\t') continue;
186
187 // Replace this tab with at least one space.
188 SourceLine[i] = ' ';
189
190 // Compute the number of spaces we need to insert.
191 unsigned NumSpaces = ((i+8)&~7) - (i+1);
192 assert(NumSpaces < 8 && "Invalid computation of space amt");
193
194 // Insert spaces into the SourceLine.
195 SourceLine.insert(i+1, NumSpaces, ' ');
196
197 // Insert spaces or ~'s into CaratLine.
198 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
199 }
200
201 // Finally, remove any blank spaces from the end of CaratLine.
202 while (CaratLine[CaratLine.size()-1] == ' ')
203 CaratLine.erase(CaratLine.end()-1);
204
205 // Emit what we have computed.
Nate Begeman165b9542008-04-17 18:06:57 +0000206 OS << SourceLine << "\n";
207 OS << CaratLine << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 }
209}