blob: 6be362399e9473855f2e22826fb55ffb6cb617c7 [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 Lattner2d89c562009-02-04 01:06:56 +000048 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin);
Chris Lattner836774b2009-01-27 07:57:44 +000049 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000050 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000051
Chris Lattner2d89c562009-02-04 01:06:56 +000052 unsigned EndLineNo = SM.getInstantiationLineNumber(End);
Chris Lattner836774b2009-01-27 07:57:44 +000053 if (EndLineNo < LineNo || SM.getFileID(End) != FID)
Chris Lattnera0030d22008-01-12 06:43:35 +000054 return; // No intersection.
Chris Lattner4b009652007-07-25 00:24:17 +000055
56 // Compute the column number of the start.
57 unsigned StartColNo = 0;
58 if (StartLineNo == LineNo) {
Chris Lattnere79fc852009-02-04 00:55:58 +000059 StartColNo = SM.getInstantiationColumnNumber(Begin);
Chris Lattner4b009652007-07-25 00:24:17 +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 Henriksenf0a835c2008-08-09 19:58:22 +000069 unsigned EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000070 if (EndLineNo == LineNo) {
Chris Lattnere79fc852009-02-04 00:55:58 +000071 EndColNo = SM.getInstantiationColumnNumber(End);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner836774b2009-01-27 07:57:44 +000076 EndColNo += Lexer::MeasureTokenLength(End, SM);
Chris Lattner4b009652007-07-25 00:24:17 +000077 } else {
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000078 EndColNo = CaretLine.size();
Chris Lattner4b009652007-07-25 00:24:17 +000079 }
80 }
81
82 // Pick the last non-whitespace column.
Nuno Lopesd0e162c2008-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();
Chris Lattner4b009652007-07-25 00:24:17 +000089
90 // Fill the range with ~'s.
91 assert(StartColNo <= EndColNo && "Invalid range!");
Nuno Lopesd0e162c2008-08-05 19:40:20 +000092 for (unsigned i = StartColNo; i < EndColNo; ++i)
Gordon Henriksenf0a835c2008-08-09 19:58:22 +000093 CaretLine[i] = '~';
Chris Lattner4b009652007-07-25 00:24:17 +000094}
95
Chris Lattner6948ae62008-11-18 07:04:44 +000096void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
97 const DiagnosticInfo &Info) {
Chris Lattner836774b2009-01-27 07:57:44 +000098 unsigned ColNo = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000099
Chris Lattner836774b2009-01-27 07:57:44 +0000100 // If the location is specified, print out a file/line/col and include trace
101 // if enabled.
102 if (Info.getLocation().isValid()) {
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000103 const SourceManager &SM = Info.getLocation().getManager();
Chris Lattner836774b2009-01-27 07:57:44 +0000104 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
105 unsigned LineNo = PLoc.getLine();
Chris Lattner4b009652007-07-25 00:24:17 +0000106
107 // First, if this diagnostic is not in the main file, print out the
108 // "included from" lines.
Chris Lattner836774b2009-01-27 07:57:44 +0000109 if (LastWarningLoc != PLoc.getIncludeLoc()) {
110 LastWarningLoc = PLoc.getIncludeLoc();
111 PrintIncludeStack(LastWarningLoc, SM);
Chris Lattner4b009652007-07-25 00:24:17 +0000112 }
113
Chris Lattner836774b2009-01-27 07:57:44 +0000114 // Compute the column number.
115 ColNo = PLoc.getColumn();
Chris Lattner68c1e192009-01-30 17:41:53 +0000116 if (ShowLocation) {
117 OS << PLoc.getFilename() << ':' << LineNo << ':';
118 if (ColNo && ShowColumn)
119 OS << ColNo << ':';
120 OS << ' ';
121 }
Chris Lattner4b009652007-07-25 00:24:17 +0000122 }
123
124 switch (Level) {
Chris Lattner95cb5502009-02-06 03:57:44 +0000125 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
Nate Begeman01d74272008-04-17 18:06:57 +0000126 case Diagnostic::Note: OS << "note: "; break;
127 case Diagnostic::Warning: OS << "warning: "; break;
128 case Diagnostic::Error: OS << "error: "; break;
Chris Lattner95cb5502009-02-06 03:57:44 +0000129 case Diagnostic::Fatal: OS << "fatal error: "; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000130 }
131
Chris Lattnerbe8e5a42008-11-19 06:51:40 +0000132 llvm::SmallString<100> OutStr;
133 Info.FormatDiagnostic(OutStr);
134 OS.write(OutStr.begin(), OutStr.size());
135 OS << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000136
Chris Lattner836774b2009-01-27 07:57:44 +0000137 // If caret diagnostics are enabled and we have location, we want to emit the
138 // caret. However, we only do this if the location moved from the last
139 // diagnostic, or if the diagnostic has ranges. We don't want to emit the
140 // same caret multiple times if one loc has multiple diagnostics.
141 if (CaretDiagnostics && Info.getLocation().isValid() &&
142 ((LastLoc != Info.getLocation()) || Info.getNumRanges())) {
Steve Naroffb268d2a2008-02-08 22:06:17 +0000143 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage.
Chris Lattner836774b2009-01-27 07:57:44 +0000144 LastLoc = Info.getLocation();
145
146 // Inspect the actual instantiation point of the diagnostic, we don't care
147 // about presumed locations anymore.
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000148 FullSourceLoc ILoc = Info.getLocation().getInstantiationLoc();
149
Chris Lattner836774b2009-01-27 07:57:44 +0000150 // Rewind from the current position to the start of the line.
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000151 const char *TokInstantiationPtr = ILoc.getCharacterData();
Chris Lattner836774b2009-01-27 07:57:44 +0000152 const char *LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based.
153
154 // Compute the line end. Scan forward from the error position to the end of
155 // the line.
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000156 const char *BufEnd = ILoc.getBufferData().second;
Chris Lattner836774b2009-01-27 07:57:44 +0000157 const char *LineEnd = TokInstantiationPtr;
158 while (LineEnd != BufEnd &&
159 *LineEnd != '\n' && *LineEnd != '\r')
160 ++LineEnd;
161
162 // Copy the line of code into an std::string for ease of manipulation.
Chris Lattner4b009652007-07-25 00:24:17 +0000163 std::string SourceLine(LineStart, LineEnd);
164
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000165 // Create a line for the caret that is filled with spaces that is the same
Chris Lattner4b009652007-07-25 00:24:17 +0000166 // length as the line of source code.
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000167 std::string CaretLine(LineEnd-LineStart, ' ');
Chris Lattner836774b2009-01-27 07:57:44 +0000168
Chris Lattner4b009652007-07-25 00:24:17 +0000169 // Highlight all of the characters covered by Ranges with ~ characters.
Chris Lattner6948ae62008-11-18 07:04:44 +0000170 for (unsigned i = 0; i != Info.getNumRanges(); ++i)
Chris Lattner2d89c562009-02-04 01:06:56 +0000171 HighlightRange(Info.getRange(i), ILoc.getManager(),
172 ILoc.getInstantiationLineNumber(),
Ted Kremenekdd62ea62009-01-28 20:47:47 +0000173 ILoc.getFileID(), CaretLine, SourceLine);
Chris Lattner4b009652007-07-25 00:24:17 +0000174
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000175 // Next, insert the caret itself.
176 if (ColNo-1 < CaretLine.size())
177 CaretLine[ColNo-1] = '^';
Chris Lattner4b009652007-07-25 00:24:17 +0000178 else
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000179 CaretLine.push_back('^');
Chris Lattner4b009652007-07-25 00:24:17 +0000180
181 // Scan the source line, looking for tabs. If we find any, manually expand
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000182 // them to 8 characters and update the CaretLine to match.
Chris Lattner4b009652007-07-25 00:24:17 +0000183 for (unsigned i = 0; i != SourceLine.size(); ++i) {
184 if (SourceLine[i] != '\t') continue;
185
186 // Replace this tab with at least one space.
187 SourceLine[i] = ' ';
188
189 // Compute the number of spaces we need to insert.
190 unsigned NumSpaces = ((i+8)&~7) - (i+1);
191 assert(NumSpaces < 8 && "Invalid computation of space amt");
192
193 // Insert spaces into the SourceLine.
194 SourceLine.insert(i+1, NumSpaces, ' ');
195
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000196 // Insert spaces or ~'s into CaretLine.
197 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' ');
Chris Lattner4b009652007-07-25 00:24:17 +0000198 }
199
Gordon Henriksenf0a835c2008-08-09 19:58:22 +0000200 // Finally, remove any blank spaces from the end of CaretLine.
201 while (CaretLine[CaretLine.size()-1] == ' ')
202 CaretLine.erase(CaretLine.end()-1);
Chris Lattner4b009652007-07-25 00:24:17 +0000203
204 // Emit what we have computed.
Chris Lattner92a33532008-11-19 06:56:25 +0000205 OS << SourceLine << '\n';
206 OS << CaretLine << '\n';
Chris Lattner4b009652007-07-25 00:24:17 +0000207 }
Chris Lattner92a33532008-11-19 06:56:25 +0000208
209 OS.flush();
Chris Lattner4b009652007-07-25 00:24:17 +0000210}