blob: 1d61f85c33707ff50e31f32f4d807540e5505bd4 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This diagnostic client prints out their diagnostic messages.
11//
12//===----------------------------------------------------------------------===//
13
Nico Weber0e13eaa2008-08-05 23:33:20 +000014#include "clang/Driver/TextDiagnosticPrinter.h"
Chris Lattner4b009652007-07-25 00:24:17 +000015#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Lex/Lexer.h"
Chris Lattner92a33532008-11-19 06:56:25 +000017#include "llvm/Support/raw_ostream.h"
Chris Lattnerbe8e5a42008-11-19 06:51:40 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019using namespace clang;
20
Chris Lattner4b009652007-07-25 00:24:17 +000021void TextDiagnosticPrinter::
Chris Lattner836774b2009-01-27 07:57:44 +000022PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) {
23 if (Loc.isInvalid()) return;
Chris Lattner4b009652007-07-25 00:24:17 +000024
Chris Lattner836774b2009-01-27 07:57:44 +000025 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +000026
27 // Print out the other include frames first.
Chris Lattner836774b2009-01-27 07:57:44 +000028 PrintIncludeStack(PLoc.getIncludeLoc(), SM);
Chris Lattner4b009652007-07-25 00:24:17 +000029
Chris Lattner836774b2009-01-27 07:57:44 +000030 OS << "In file included from " << PLoc.getFilename()
31 << ':' << PLoc.getLine() << ":\n";
Chris Lattner4b009652007-07-25 00:24:17 +000032}
33
34/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s)
35/// any characters in LineNo that intersect the SourceRange.
Ted Kremenekb3ee1932007-12-11 21:27:55 +000036void TextDiagnosticPrinter::HighlightRange(const SourceRange &R,
Chris Lattner836774b2009-01-27 07:57:44 +000037 const SourceManager &SM,
Chris Lattner10aaf532009-01-17 08:45:21 +000038 unsigned LineNo, FileID FID,
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000039 std::string &CaretLine,
Nuno Lopesd0e162c2008-08-05 19:40:20 +000040 const std::string &SourceLine) {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000041 assert(CaretLine.size() == SourceLine.size() &&
42 "Expect a correspondence between source and caret line!");
Chris Lattner4b009652007-07-25 00:24:17 +000043 if (!R.isValid()) return;
44
Chris Lattner836774b2009-01-27 07:57:44 +000045 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin());
46 SourceLocation End = SM.getInstantiationLoc(R.getEnd());
47
Chris Lattnere357b112009-02-17 05:19:10 +000048 // If the End location and the start location are the same and are a macro
49 // location, then the range was something that came from a macro expansion
50 // or _Pragma. If this is an object-like macro, the best we can do is to
51 // highlight the range. If this is a function-like macro, we'd also like to
52 // highlight the arguments.
53 if (Begin == End && R.getEnd().isMacroID())
54 End = SM.getInstantiationRange(R.getEnd()).second;
55
Chris Lattner2d89c562009-02-04 01:06:56 +000056 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattner836774b2009-01-27 07:57:44 +000057 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000058 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000059
Chris Lattner2d89c562009-02-04 01:06:56 +000060 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattner836774b2009-01-27 07:57:44 +000061 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000062 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000063
64 // Compute the column number of the start.
65 unsigned StartColNo = 0;
66 if (StartLineNo == LineNo) {
Chris Lattnere79fc852009-02-04 00:55:58 +000067 StartColNo = SM.getInstantiationColumnNumber(Begin);
Chris Lattner4b009652007-07-25 00:24:17 +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.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000077 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000078 if (EndLineNo == LineNo) {
Chris Lattnere79fc852009-02-04 00:55:58 +000079 EndColNo = SM.getInstantiationColumnNumber(End);
Chris Lattner4b009652007-07-25 00:24:17 +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.
Chris Lattner836774b2009-01-27 07:57:44 +000084 EndColNo += Lexer::MeasureTokenLength(End, SM);
Chris Lattner4b009652007-07-25 00:24:17 +000085 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000086 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000087 }
88 }
89
90 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-08-05 19:40:20 +000091 if (EndColNo <= SourceLine.size())
92 while (EndColNo-1 &&
93 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t'))
94 --EndColNo;
95 else
96 EndColNo = SourceLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000097
98 // Fill the range with ~'s.
99 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +0000100 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000101 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +0000102}
103
Chris Lattner6948ae62008-11-18 07:04:44 +0000104void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
105 const DiagnosticInfo &Info) {
Chris Lattner836774b2009-01-27 07:57:44 +0000106 // If the location is specified, print out a file/line/col and include trace
107 // if enabled.
108 if (Info.getLocation().isValid()) {
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000109 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattner836774b2009-01-27 07:57:44 +0000110 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
111 unsigned LineNo = PLoc.getLine();
Chris Lattner4b009652007-07-25 00:24:17 +0000112
113 // First, if this diagnostic is not in the main file, print out the
114 // "included from" lines.
Chris Lattner836774b2009-01-27 07:57:44 +0000115 if (LastWarningLoc != PLoc.getIncludeLoc()) {
116 LastWarningLoc = PLoc.getIncludeLoc();
117 PrintIncludeStack(LastWarningLoc, SM);
Chris Lattner4b009652007-07-25 00:24:17 +0000118 }
119
Chris Lattner836774b2009-01-27 07:57:44 +0000120 // Compute the column number.
Chris Lattner68c1e192009-01-30 17:41:53 +0000121 if (ShowLocation) {
122 OS << PLoc.getFilename() << ':' << LineNo << ':';
Chris Lattnerf0b28562009-02-17 07:34:34 +0000123 if (ShowColumn)
124 if (unsigned ColNo = PLoc.getColumn())
125 OS << ColNo << ':';
Chris Lattner68c1e192009-01-30 17:41:53 +0000126 OS << ' ';
127 }
Chris Lattner4b009652007-07-25 00:24:17 +0000128 }
129
130 switch (Level) {
Chris Lattner95cb5502009-02-06 03:57:44 +0000131 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman01d74272008-04-17 18:06:57 +0000132 case Diagnostic::Note: OS << "note: "; break;
133 case Diagnostic::Warning: OS << "warning: "; break;
134 case Diagnostic::Error: OS << "error: "; break;
Chris Lattner95cb5502009-02-06 03:57:44 +0000135 case Diagnostic::Fatal: OS << "fatal error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000136 }
137
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000138 llvm::SmallString<100> OutStr;
139 Info.FormatDiagnostic(OutStr);
140 OS.write(OutStr.begin(), OutStr.size());
141 OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000142
Chris Lattner836774b2009-01-27 07:57:44 +0000143 // If caret diagnostics are enabled and we have location, we want to emit the
144 // caret. However, we only do this if the location moved from the last
145 // diagnostic, or if the diagnostic has ranges. We don't want to emit the
146 // same caret multiple times if one loc has multiple diagnostics.
147 if (CaretDiagnostics && Info.getLocation().isValid() &&
148 ((LastLoc != Info.getLocation()) || Info.getNumRanges())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000149 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattner836774b2009-01-27 07:57:44 +0000150 LastLoc = Info.getLocation();
151
152 // Inspect the actual instantiation point of the diagnostic, we don't care
153 // about presumed locations anymore.
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000154 FullSourceLoc ILoc = Info.getLocation().getInstantiationLoc();
155
Chris Lattnerf0b28562009-02-17 07:34:34 +0000156 unsigned ColNo = ILoc.getInstantiationColumnNumber();
157
Chris Lattner836774b2009-01-27 07:57:44 +0000158 // Rewind from the current position to the start of the line.
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000159 const char *TokInstantiationPtr = ILoc.getCharacterData();
Chris Lattner836774b2009-01-27 07:57:44 +0000160 const char *LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based.
161
162 // Compute the line end. Scan forward from the error position to the end of
163 // the line.
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000164 const char *BufEnd = ILoc.getBufferData().second;
Chris Lattner836774b2009-01-27 07:57:44 +0000165 const char *LineEnd = TokInstantiationPtr;
166 while (LineEnd != BufEnd &&
167 *LineEnd != '\n' && *LineEnd != '\r')
168 ++LineEnd;
169
170 // Copy the line of code into an std::string for ease of manipulation.
Chris Lattner4b009652007-07-25 00:24:17 +0000171 std::string SourceLine(LineStart, LineEnd);
172
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000173 // Create a line for the caret that is filled with spaces that is the same
Chris Lattner4b009652007-07-25 00:24:17 +0000174 // length as the line of source code.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000175 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattner836774b2009-01-27 07:57:44 +0000176
Chris Lattner4b009652007-07-25 00:24:17 +0000177 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner6948ae62008-11-18 07:04:44 +0000178 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
Chris Lattner2d89c562009-02-04 01:06:56 +0000179 HighlightRange(Info.getRange(i), ILoc.getManager(),
180 ILoc.getInstantiationLineNumber(),
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000181 ILoc.getFileID(), CaretLine, SourceLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000182
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000183 // Next, insert the caret itself.
184 if (ColNo-1 < CaretLine.size())
185 CaretLine[ColNo-1] = '^';
Chris Lattner4b009652007-07-25 00:24:17 +0000186 else
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000187 CaretLine.push_back('^');
Chris Lattner4b009652007-07-25 00:24:17 +0000188
189 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000190 // them to 8 characters and update the CaretLine to match.
Chris Lattner4b009652007-07-25 00:24:17 +0000191 for (unsigned i = 0; i != SourceLine.size(); ++i) {
192 if (SourceLine[i] != '\t') continue;
193
194 // Replace this tab with at least one space.
195 SourceLine[i] = ' ';
196
197 // Compute the number of spaces we need to insert.
198 unsigned NumSpaces = ((i+8)&~7) - (i+1);
199 assert(NumSpaces < 8 && "Invalid computation of space amt");
200
201 // Insert spaces into the SourceLine.
202 SourceLine.insert(i+1, NumSpaces, ' ');
203
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000204 // Insert spaces or ~'s into CaretLine.
205 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000206 }
207
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000208 // Finally, remove any blank spaces from the end of CaretLine.
209 while (CaretLine[CaretLine.size()-1] == ' ')
210 CaretLine.erase(CaretLine.end()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000211
212 // Emit what we have computed.
Chris Lattner92a33532008-11-19 06:56:25 +0000213 OS << SourceLine << '\n';
214 OS << CaretLine << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000215 }
Chris Lattner92a33532008-11-19 06:56:25 +0000216
217 OS.flush();
Chris Lattner4b009652007-07-25 00:24:17 +0000218}