blob: b19d61c160f298bc6df6999d182fab66956da71a [file] [log] [blame]
Bill Wendling37b1dde2007-06-07 09:34:54 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling37b1dde2007-06-07 09:34:54 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Nico Weberb5fc3c32008-08-05 23:33:20 +000014#include "clang/Driver/TextDiagnosticPrinter.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000015#include "clang/Basic/SourceManager.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000016#include "clang/Lex/Lexer.h"
Chris Lattner327984f2008-11-19 06:56:25 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattner23be0672008-11-19 06:51:40 +000018#include "llvm/ADT/SmallString.h"
Bill Wendling37b1dde2007-06-07 09:34:54 +000019using namespace clang;
20
Bill Wendling37b1dde2007-06-07 09:34:54 +000021void TextDiagnosticPrinter::
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000022PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
23 if (Loc.isInvalid()) return;
Chris Lattnerdc5c0552007-07-20 16:37:10 +000024
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000025 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattnerdc5c0552007-07-20 16:37:10 +000026
Bill Wendling37b1dde2007-06-07 09:34:54 +000027 // Print out the other include frames first.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000028 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Bill Wendling37b1dde2007-06-07 09:34:54 +000029
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000030 OS << "In file included from " << PLoc.getFilename()
31 << ':' << PLoc.getLine() << ":\n";
Bill Wendling37b1dde2007-06-07 09:34:54 +000032}
33
Bill Wendling37b1dde2007-06-07 09:34:54 +000034/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
35/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekd4e5fba2007-12-11 21:27:55 +000036void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000037 const SourceManager &SM,
Chris Lattner71dc14b2009-01-17 08:45:21 +000038 unsigned LineNo, FileID FID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000039 std::string &CaretLine,
Nuno Lopesd86aa932008-08-05 19:40:20 +000040 const std::string &SourceLine) {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000041 assert(CaretLine.size() == SourceLine.size() &&
42 "Expect a correspondence between source and caret line!");
Bill Wendling37b1dde2007-06-07 09:34:54 +000043 if (!R.isValid()) return;
44
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000045 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
46 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
47
48 unsigned StartLineNo = SM.getLineNumber(Begin);
49 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000050 return; // No intersection.
Bill Wendling37b1dde2007-06-07 09:34:54 +000051
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000052 unsigned EndLineNo = SM.getLineNumber(End);
53 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattner177977ac2008-01-12 06:43:35 +000054 return; // No intersection.
Bill Wendling37b1dde2007-06-07 09:34:54 +000055
56 // Compute the column number of the start.
57 unsigned StartColNo = 0;
58 if (StartLineNo == LineNo) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000059 StartColNo = SM.getColumnNumber(Begin);
Bill Wendling37b1dde2007-06-07 09:34:54 +000060 if (StartColNo) --StartColNo; // Zero base the col #.
61 }
62
63 // Pick the first non-whitespace column.
64 while (StartColNo < SourceLine.size() &&
65 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t'))
66 ++StartColNo;
67
68 // Compute the column number of the end.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000069 unsigned EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000070 if (EndLineNo == LineNo) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000071 EndColNo = SM.getColumnNumber(End);
Bill Wendling37b1dde2007-06-07 09:34:54 +000072 if (EndColNo) {
73 --EndColNo; // Zero base the col #.
74
75 // Add in the length of the token, so that we cover multi-char tokens.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000076 EndColNo += Lexer::MeasureTokenLength(End, SM);
Bill Wendling37b1dde2007-06-07 09:34:54 +000077 } else {
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000078 EndColNo = CaretLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000079 }
80 }
81
82 // Pick the last non-whitespace column.
Nuno Lopesd86aa932008-08-05 19:40:20 +000083 if (EndColNo <= SourceLine.size())
84 while (EndColNo-1 &&
85 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
86 --EndColNo;
87 else
88 EndColNo = SourceLine.size();
Bill Wendling37b1dde2007-06-07 09:34:54 +000089
90 // Fill the range with ~'s.
91 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd86aa932008-08-05 19:40:20 +000092 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksen04c50bd2008-08-09 19:58:22 +000093 CaretLine[i] = '~';
Bill Wendling37b1dde2007-06-07 09:34:54 +000094}
95
Chris Lattner8488c822008-11-18 07:04:44 +000096void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
97 const DiagnosticInfo &Info) {
Chris Lattnerf1ca7d32009-01-27 07:57:44 +000098 const SourceManager &SM = Info.getLocation().getManager();
99 unsigned ColNo = 0;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000100
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000101 // If the location is specified, print out a file/line/col and include trace
102 // if enabled.
103 if (Info.getLocation().isValid()) {
104 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
105 unsigned LineNo = PLoc.getLine();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000106
Bill Wendling37b1dde2007-06-07 09:34:54 +0000107 // First, if this diagnostic is not in the main file, print out the
108 // "included from" lines.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000109 if (LastWarningLoc != PLoc.getIncludeLoc()) {
110 LastWarningLoc = PLoc.getIncludeLoc();
111 PrintIncludeStack(LastWarningLoc, SM);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000112 }
113
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000114 // Compute the column number.
115 ColNo = PLoc.getColumn();
116 OS << PLoc.getFilename() << ':' << LineNo << ':';
Nico Weberb5fc3c32008-08-05 23:33:20 +0000117 if (ColNo && ShowColumn)
Chris Lattner327984f2008-11-19 06:56:25 +0000118 OS << ColNo << ':';
119 OS << ' ';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000120 }
121
122 switch (Level) {
123 default: assert(0 && "Unknown diagnostic type!");
Nate Begeman36c49272008-04-17 18:06:57 +0000124 case Diagnostic::Note: OS << "note: "; break;
125 case Diagnostic::Warning: OS << "warning: "; break;
126 case Diagnostic::Error: OS << "error: "; break;
Bill Wendling37b1dde2007-06-07 09:34:54 +0000127 }
128
Chris Lattner23be0672008-11-19 06:51:40 +0000129 llvm::SmallString<100> OutStr;
130 Info.FormatDiagnostic(OutStr);
131 OS.write(OutStr.begin(), OutStr.size());
132 OS << '\n';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000133
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000134 // If caret diagnostics are enabled and we have location, we want to emit the
135 // caret. However, we only do this if the location moved from the last
136 // diagnostic, or if the diagnostic has ranges. We don't want to emit the
137 // same caret multiple times if one loc has multiple diagnostics.
138 if (CaretDiagnostics && Info.getLocation().isValid() &&
139 ((LastLoc != Info.getLocation()) || Info.getNumRanges())) {
Steve Naroffa36992242008-02-08 22:06:17 +0000140 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000141 LastLoc = Info.getLocation();
142
143 // Inspect the actual instantiation point of the diagnostic, we don't care
144 // about presumed locations anymore.
145 SourceLocation ILoc = SM.getInstantiationLoc(Info.getLocation());
Steve Naroffa36992242008-02-08 22:06:17 +0000146
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000147 // Get the file and line that we want to highlight. We only draw ranges
148 // that intersect this.
149 FileID ILocFID = SM.getFileID(ILoc);
150 unsigned LineNo = SM.getLineNumber(ILoc);
151
152 // Get the line of the source file. Scan from the location backward and
153 // forward to find the start/end of the line.
154
155 // Rewind from the current position to the start of the line.
156 const char *TokInstantiationPtr = SM.getCharacterData(ILoc);
157 const char *LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based.
158
159 // Compute the line end. Scan forward from the error position to the end of
160 // the line.
161 const char *BufEnd = SM.getBufferData(ILocFID).second;
162 const char *LineEnd = TokInstantiationPtr;
163 while (LineEnd != BufEnd &&
164 *LineEnd != '\n' && *LineEnd != '\r')
165 ++LineEnd;
166
167 // Copy the line of code into an std::string for ease of manipulation.
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000168 std::string SourceLine(LineStart, LineEnd);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000169
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000170 // Create a line for the caret that is filled with spaces that is the same
Bill Wendling37b1dde2007-06-07 09:34:54 +0000171 // length as the line of source code.
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000172 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000173
Bill Wendling37b1dde2007-06-07 09:34:54 +0000174 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner8488c822008-11-18 07:04:44 +0000175 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
Chris Lattnerf1ca7d32009-01-27 07:57:44 +0000176 HighlightRange(Info.getRange(i), SM, LineNo, ILocFID,
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000177 CaretLine, SourceLine);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000178
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000179 // Next, insert the caret itself.
180 if (ColNo-1 < CaretLine.size())
181 CaretLine[ColNo-1] = '^';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000182 else
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000183 CaretLine.push_back('^');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000184
185 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000186 // them to 8 characters and update the CaretLine to match.
Bill Wendling37b1dde2007-06-07 09:34:54 +0000187 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
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000200 // Insert spaces or ~'s into CaretLine.
201 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Bill Wendling37b1dde2007-06-07 09:34:54 +0000202 }
203
Gordon Henriksen04c50bd2008-08-09 19:58:22 +0000204 // Finally, remove any blank spaces from the end of CaretLine.
205 while (CaretLine[CaretLine.size()-1] == ' ')
206 CaretLine.erase(CaretLine.end()-1);
Bill Wendling37b1dde2007-06-07 09:34:54 +0000207
208 // Emit what we have computed.
Chris Lattner327984f2008-11-19 06:56:25 +0000209 OS << SourceLine << '\n';
210 OS << CaretLine << '\n';
Bill Wendling37b1dde2007-06-07 09:34:54 +0000211 }
Chris Lattner327984f2008-11-19 06:56:25 +0000212
213 OS.flush();
Bill Wendling37b1dde2007-06-07 09:34:54 +0000214}