blob: 16c7645862da039e1cc711e2eec67a295896904d [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,
Nuno Lopesdb825682008-08-05 19:40:20 +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.
Nuno Lopesdb825682008-08-05 19:40:20 +000094 if (EndColNo <= SourceLine.size())
95 while (EndColNo-1 &&
96 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
97 --EndColNo;
98 else
99 EndColNo = SourceLine.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000100
101 // Fill the range with ~'s.
102 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesdb825682008-08-05 19:40:20 +0000103 for (unsigned i = StartColNo; i < EndColNo; ++i)
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 CaratLine[i] = '~';
105}
106
Chris Lattner07506182007-11-30 22:53:43 +0000107void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags,
108 Diagnostic::Level Level,
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000109 FullSourceLoc Pos,
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 diag::kind ID,
111 const std::string *Strs,
112 unsigned NumStrs,
113 const SourceRange *Ranges,
114 unsigned NumRanges) {
Chris Lattner9dc1f532007-07-20 16:37:10 +0000115 unsigned LineNo = 0, ColNo = 0;
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000116 unsigned FileID = 0;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000117 const char *LineStart = 0, *LineEnd = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000118
119 if (Pos.isValid()) {
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000120 FullSourceLoc LPos = Pos.getLogicalLoc();
121 LineNo = LPos.getLineNumber();
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000122 FileID = LPos.getLocation().getFileID();
Reid Spencer5f016e22007-07-11 17:01:13 +0000123
124 // First, if this diagnostic is not in the main file, print out the
125 // "included from" lines.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000126 if (LastWarningLoc != LPos.getIncludeLoc()) {
127 LastWarningLoc = LPos.getIncludeLoc();
128 PrintIncludeStack(LastWarningLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
130
131 // Compute the column number. Rewind from the current position to the start
132 // of the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000133 ColNo = LPos.getColumnNumber();
134 const char *TokLogicalPtr = LPos.getCharacterData();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000135 LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based
Chris Lattner0cbc2152008-01-25 00:01:10 +0000136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 // Compute the line end. Scan forward from the error position to the end of
138 // the line.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000139 const llvm::MemoryBuffer *Buffer = LPos.getBuffer();
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 const char *BufEnd = Buffer->getBufferEnd();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000141 LineEnd = TokLogicalPtr;
142 while (LineEnd != BufEnd &&
143 *LineEnd != '\n' && *LineEnd != '\r')
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 ++LineEnd;
145
Nate Begeman165b9542008-04-17 18:06:57 +0000146 OS << Buffer->getBufferIdentifier() << ":" << LineNo << ":";
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 if (ColNo && !NoShowColumn)
Nate Begeman165b9542008-04-17 18:06:57 +0000148 OS << ColNo << ":";
149 OS << " ";
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 }
151
152 switch (Level) {
153 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman165b9542008-04-17 18:06:57 +0000154 case Diagnostic::Note: OS << "note: "; break;
155 case Diagnostic::Warning: OS << "warning: "; break;
156 case Diagnostic::Error: OS << "error: "; break;
157 case Diagnostic::Fatal: OS << "fatal error: "; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 break;
159 }
160
Nate Begeman165b9542008-04-17 18:06:57 +0000161 OS << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000162
Steve Naroff58de0262008-02-11 22:17:33 +0000163 if (!NoCaretDiagnostics && Pos.isValid() && ((LastLoc != Pos) || Ranges)) {
Steve Naroffefe7f362008-02-08 22:06:17 +0000164 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
165 LastLoc = Pos;
166
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 // Get the line of the source file.
Chris Lattner9dc1f532007-07-20 16:37:10 +0000168 std::string SourceLine(LineStart, LineEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169
170 // Create a line for the carat that is filled with spaces that is the same
171 // length as the line of source code.
172 std::string CaratLine(LineEnd-LineStart, ' ');
173
174 // Highlight all of the characters covered by Ranges with ~ characters.
175 for (unsigned i = 0; i != NumRanges; ++i)
Chris Lattnere41b7cd2008-01-12 06:43:35 +0000176 HighlightRange(Ranges[i], Pos.getManager(), LineNo, FileID,
177 CaratLine, SourceLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178
179 // Next, insert the carat itself.
180 if (ColNo-1 < CaratLine.size())
181 CaratLine[ColNo-1] = '^';
182 else
183 CaratLine.push_back('^');
184
185 // Scan the source line, looking for tabs. If we find any, manually expand
186 // them to 8 characters and update the CaratLine to match.
187 for (unsigned i = 0; i != SourceLine.size(); ++i) {
188 if (SourceLine[i] != '\t') continue;
189
190 // Replace this tab with at least one space.
191 SourceLine[i] = ' ';
192
193 // Compute the number of spaces we need to insert.
194 unsigned NumSpaces = ((i+8)&~7) - (i+1);
195 assert(NumSpaces < 8 && "Invalid computation of space amt");
196
197 // Insert spaces into the SourceLine.
198 SourceLine.insert(i+1, NumSpaces, ' ');
199
200 // Insert spaces or ~'s into CaratLine.
201 CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' ');
202 }
203
204 // Finally, remove any blank spaces from the end of CaratLine.
205 while (CaratLine[CaratLine.size()-1] == ' ')
206 CaratLine.erase(CaratLine.end()-1);
207
208 // Emit what we have computed.
Nate Begeman165b9542008-04-17 18:06:57 +0000209 OS << SourceLine << "\n";
210 OS << CaratLine << "\n";
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 }
212}