2011-10-16 07:43:53 +08:00
|
|
|
//===--- TextDiagnostic.cpp - Text Diagnostic Pretty-Printing -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/TextDiagnostic.h"
|
2013-02-09 06:30:41 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2012-10-24 06:26:28 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
2011-10-16 07:43:53 +08:00
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-04-18 10:44:46 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2013-01-30 20:06:08 +08:00
|
|
|
#include "llvm/Support/ConvertUTF.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/Locale.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-10-16 07:43:53 +08:00
|
|
|
#include <algorithm>
|
2012-04-18 10:44:46 +08:00
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static const enum raw_ostream::Colors noteColor =
|
|
|
|
raw_ostream::BLACK;
|
2014-02-28 17:11:08 +08:00
|
|
|
static const enum raw_ostream::Colors remarkColor =
|
|
|
|
raw_ostream::BLUE;
|
2011-10-16 07:43:53 +08:00
|
|
|
static const enum raw_ostream::Colors fixitColor =
|
|
|
|
raw_ostream::GREEN;
|
|
|
|
static const enum raw_ostream::Colors caretColor =
|
|
|
|
raw_ostream::GREEN;
|
|
|
|
static const enum raw_ostream::Colors warningColor =
|
|
|
|
raw_ostream::MAGENTA;
|
2012-06-27 02:18:47 +08:00
|
|
|
static const enum raw_ostream::Colors templateColor =
|
|
|
|
raw_ostream::CYAN;
|
2011-10-16 07:43:53 +08:00
|
|
|
static const enum raw_ostream::Colors errorColor = raw_ostream::RED;
|
|
|
|
static const enum raw_ostream::Colors fatalColor = raw_ostream::RED;
|
|
|
|
// Used for changing only the bold attribute.
|
|
|
|
static const enum raw_ostream::Colors savedColor =
|
|
|
|
raw_ostream::SAVEDCOLOR;
|
|
|
|
|
2012-06-27 02:18:47 +08:00
|
|
|
/// \brief Add highlights to differences in template strings.
|
|
|
|
static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str,
|
2012-06-29 06:39:03 +08:00
|
|
|
bool &Normal, bool Bold) {
|
2012-10-19 04:09:54 +08:00
|
|
|
while (1) {
|
|
|
|
size_t Pos = Str.find(ToggleHighlight);
|
|
|
|
OS << Str.slice(0, Pos);
|
|
|
|
if (Pos == StringRef::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Str = Str.substr(Pos + 1);
|
|
|
|
if (Normal)
|
|
|
|
OS.changeColor(templateColor, true);
|
|
|
|
else {
|
|
|
|
OS.resetColor();
|
|
|
|
if (Bold)
|
|
|
|
OS.changeColor(savedColor, true);
|
2012-06-27 02:18:47 +08:00
|
|
|
}
|
2012-10-19 04:09:54 +08:00
|
|
|
Normal = !Normal;
|
|
|
|
}
|
2012-06-27 02:18:47 +08:00
|
|
|
}
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
/// \brief Number of spaces to indent when word-wrapping.
|
|
|
|
const unsigned WordWrapIndentation = 6;
|
|
|
|
|
2012-05-01 22:34:11 +08:00
|
|
|
static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) {
|
2012-04-18 10:44:46 +08:00
|
|
|
int bytes = 0;
|
|
|
|
while (0<i) {
|
|
|
|
if (SourceLine[--i]=='\t')
|
|
|
|
break;
|
|
|
|
++bytes;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief returns a printable representation of first item from input range
|
|
|
|
///
|
|
|
|
/// This function returns a printable representation of the next item in a line
|
|
|
|
/// of source. If the next byte begins a valid and printable character, that
|
|
|
|
/// character is returned along with 'true'.
|
|
|
|
///
|
|
|
|
/// Otherwise, if the next byte begins a valid, but unprintable character, a
|
|
|
|
/// printable, escaped representation of the character is returned, along with
|
|
|
|
/// 'false'. Otherwise a printable, escaped representation of the next byte
|
|
|
|
/// is returned along with 'false'.
|
|
|
|
///
|
|
|
|
/// \note The index is updated to be used with a subsequent call to
|
|
|
|
/// printableTextForNextCharacter.
|
|
|
|
///
|
|
|
|
/// \param SourceLine The line of source
|
|
|
|
/// \param i Pointer to byte index,
|
|
|
|
/// \param TabStop used to expand tabs
|
2012-09-27 18:16:10 +08:00
|
|
|
/// \return pair(printable text, 'true' iff original text was printable)
|
2012-04-18 10:44:46 +08:00
|
|
|
///
|
2012-05-01 22:34:11 +08:00
|
|
|
static std::pair<SmallString<16>, bool>
|
2012-04-18 10:44:46 +08:00
|
|
|
printableTextForNextCharacter(StringRef SourceLine, size_t *i,
|
|
|
|
unsigned TabStop) {
|
|
|
|
assert(i && "i must not be null");
|
|
|
|
assert(*i<SourceLine.size() && "must point to a valid index");
|
|
|
|
|
|
|
|
if (SourceLine[*i]=='\t') {
|
|
|
|
assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop &&
|
|
|
|
"Invalid -ftabstop value");
|
|
|
|
unsigned col = bytesSincePreviousTabOrLineBegin(SourceLine, *i);
|
|
|
|
unsigned NumSpaces = TabStop - col%TabStop;
|
|
|
|
assert(0 < NumSpaces && NumSpaces <= TabStop
|
|
|
|
&& "Invalid computation of space amt");
|
|
|
|
++(*i);
|
|
|
|
|
|
|
|
SmallString<16> expandedTab;
|
|
|
|
expandedTab.assign(NumSpaces, ' ');
|
|
|
|
return std::make_pair(expandedTab, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char const *begin, *end;
|
|
|
|
begin = reinterpret_cast<unsigned char const *>(&*(SourceLine.begin() + *i));
|
2012-10-30 14:13:50 +08:00
|
|
|
end = begin + (SourceLine.size() - *i);
|
2012-04-18 10:44:46 +08:00
|
|
|
|
|
|
|
if (isLegalUTF8Sequence(begin, end)) {
|
|
|
|
UTF32 c;
|
|
|
|
UTF32 *cptr = &c;
|
|
|
|
unsigned char const *original_begin = begin;
|
2012-10-30 14:13:52 +08:00
|
|
|
unsigned char const *cp_end = begin+getNumBytesForUTF8(SourceLine[*i]);
|
2012-04-18 10:44:46 +08:00
|
|
|
|
|
|
|
ConversionResult res = ConvertUTF8toUTF32(&begin, cp_end, &cptr, cptr+1,
|
|
|
|
strictConversion);
|
2012-04-19 01:25:16 +08:00
|
|
|
(void)res;
|
2012-04-18 10:44:46 +08:00
|
|
|
assert(conversionOK==res);
|
|
|
|
assert(0 < begin-original_begin
|
|
|
|
&& "we must be further along in the string now");
|
|
|
|
*i += begin-original_begin;
|
|
|
|
|
|
|
|
if (!llvm::sys::locale::isPrint(c)) {
|
|
|
|
// If next character is valid UTF-8, but not printable
|
|
|
|
SmallString<16> expandedCP("<U+>");
|
|
|
|
while (c) {
|
|
|
|
expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(c%16));
|
|
|
|
c/=16;
|
|
|
|
}
|
|
|
|
while (expandedCP.size() < 8)
|
|
|
|
expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(0));
|
|
|
|
return std::make_pair(expandedCP, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If next character is valid UTF-8, and printable
|
|
|
|
return std::make_pair(SmallString<16>(original_begin, cp_end), true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// If next byte is not valid UTF-8 (and therefore not printable)
|
|
|
|
SmallString<16> expandedByte("<XX>");
|
|
|
|
unsigned char byte = SourceLine[*i];
|
|
|
|
expandedByte[1] = llvm::hexdigit(byte / 16);
|
|
|
|
expandedByte[2] = llvm::hexdigit(byte % 16);
|
|
|
|
++(*i);
|
|
|
|
return std::make_pair(expandedByte, false);
|
|
|
|
}
|
|
|
|
|
2012-05-01 22:34:11 +08:00
|
|
|
static void expandTabs(std::string &SourceLine, unsigned TabStop) {
|
2012-04-18 10:44:46 +08:00
|
|
|
size_t i = SourceLine.size();
|
|
|
|
while (i>0) {
|
|
|
|
i--;
|
|
|
|
if (SourceLine[i]!='\t')
|
|
|
|
continue;
|
|
|
|
size_t tmp_i = i;
|
|
|
|
std::pair<SmallString<16>,bool> res
|
|
|
|
= printableTextForNextCharacter(SourceLine, &tmp_i, TabStop);
|
|
|
|
SourceLine.replace(i, 1, res.first.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function takes a raw source line and produces a mapping from the bytes
|
|
|
|
/// of the printable representation of the line to the columns those printable
|
|
|
|
/// characters will appear at (numbering the first column as 0).
|
|
|
|
///
|
2014-12-20 16:51:22 +08:00
|
|
|
/// If a byte 'i' corresponds to multiple columns (e.g. the byte contains a tab
|
2012-07-23 16:59:39 +08:00
|
|
|
/// character) then the array will map that byte to the first column the
|
2012-04-18 10:44:46 +08:00
|
|
|
/// tab appears at and the next value in the map will have been incremented
|
|
|
|
/// more than once.
|
|
|
|
///
|
|
|
|
/// If a byte is the first in a sequence of bytes that together map to a single
|
|
|
|
/// entity in the output, then the array will map that byte to the appropriate
|
|
|
|
/// column while the subsequent bytes will be -1.
|
|
|
|
///
|
|
|
|
/// The last element in the array does not correspond to any byte in the input
|
|
|
|
/// and instead is the number of columns needed to display the source
|
|
|
|
///
|
|
|
|
/// example: (given a tabstop of 8)
|
|
|
|
///
|
|
|
|
/// "a \t \u3042" -> {0,1,2,8,9,-1,-1,11}
|
|
|
|
///
|
2012-06-22 13:33:23 +08:00
|
|
|
/// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to
|
2012-04-18 10:44:46 +08:00
|
|
|
/// display)
|
2012-05-01 22:34:11 +08:00
|
|
|
static void byteToColumn(StringRef SourceLine, unsigned TabStop,
|
|
|
|
SmallVectorImpl<int> &out) {
|
2012-04-18 10:44:46 +08:00
|
|
|
out.clear();
|
|
|
|
|
|
|
|
if (SourceLine.empty()) {
|
|
|
|
out.resize(1u,0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.resize(SourceLine.size()+1, -1);
|
|
|
|
|
|
|
|
int columns = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
while (i<SourceLine.size()) {
|
|
|
|
out[i] = columns;
|
|
|
|
std::pair<SmallString<16>,bool> res
|
|
|
|
= printableTextForNextCharacter(SourceLine, &i, TabStop);
|
|
|
|
columns += llvm::sys::locale::columnWidth(res.first);
|
|
|
|
}
|
|
|
|
out.back() = columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function takes a raw source line and produces a mapping from columns
|
|
|
|
/// to the byte of the source line that produced the character displaying at
|
|
|
|
/// that column. This is the inverse of the mapping produced by byteToColumn()
|
|
|
|
///
|
|
|
|
/// The last element in the array is the number of bytes in the source string
|
|
|
|
///
|
|
|
|
/// example: (given a tabstop of 8)
|
|
|
|
///
|
|
|
|
/// "a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7}
|
|
|
|
///
|
2012-06-22 13:33:23 +08:00
|
|
|
/// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to
|
2012-04-18 10:44:46 +08:00
|
|
|
/// display)
|
2012-05-01 22:34:11 +08:00
|
|
|
static void columnToByte(StringRef SourceLine, unsigned TabStop,
|
2012-04-18 10:44:46 +08:00
|
|
|
SmallVectorImpl<int> &out) {
|
|
|
|
out.clear();
|
|
|
|
|
|
|
|
if (SourceLine.empty()) {
|
|
|
|
out.resize(1u, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int columns = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
while (i<SourceLine.size()) {
|
|
|
|
out.resize(columns+1, -1);
|
|
|
|
out.back() = i;
|
|
|
|
std::pair<SmallString<16>,bool> res
|
|
|
|
= printableTextForNextCharacter(SourceLine, &i, TabStop);
|
|
|
|
columns += llvm::sys::locale::columnWidth(res.first);
|
|
|
|
}
|
|
|
|
out.resize(columns+1, -1);
|
|
|
|
out.back() = i;
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:58:01 +08:00
|
|
|
namespace {
|
2012-04-18 10:44:46 +08:00
|
|
|
struct SourceColumnMap {
|
|
|
|
SourceColumnMap(StringRef SourceLine, unsigned TabStop)
|
|
|
|
: m_SourceLine(SourceLine) {
|
|
|
|
|
|
|
|
::byteToColumn(SourceLine, TabStop, m_byteToColumn);
|
|
|
|
::columnToByte(SourceLine, TabStop, m_columnToByte);
|
|
|
|
|
|
|
|
assert(m_byteToColumn.size()==SourceLine.size()+1);
|
|
|
|
assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size());
|
|
|
|
assert(m_byteToColumn.size()
|
|
|
|
== static_cast<unsigned>(m_columnToByte.back()+1));
|
|
|
|
assert(static_cast<unsigned>(m_byteToColumn.back()+1)
|
|
|
|
== m_columnToByte.size());
|
|
|
|
}
|
|
|
|
int columns() const { return m_byteToColumn.back(); }
|
|
|
|
int bytes() const { return m_columnToByte.back(); }
|
2012-09-14 02:37:50 +08:00
|
|
|
|
|
|
|
/// \brief Map a byte to the column which it is at the start of, or return -1
|
|
|
|
/// if it is not at the start of a column (for a UTF-8 trailing byte).
|
2012-04-18 10:44:46 +08:00
|
|
|
int byteToColumn(int n) const {
|
|
|
|
assert(0<=n && n<static_cast<int>(m_byteToColumn.size()));
|
|
|
|
return m_byteToColumn[n];
|
|
|
|
}
|
2012-09-14 02:37:50 +08:00
|
|
|
|
|
|
|
/// \brief Map a byte to the first column which contains it.
|
|
|
|
int byteToContainingColumn(int N) const {
|
|
|
|
assert(0 <= N && N < static_cast<int>(m_byteToColumn.size()));
|
|
|
|
while (m_byteToColumn[N] == -1)
|
|
|
|
--N;
|
|
|
|
return m_byteToColumn[N];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Map a column to the byte which starts the column, or return -1 if
|
|
|
|
/// the column the second or subsequent column of an expanded tab or similar
|
|
|
|
/// multi-column entity.
|
2012-04-18 10:44:46 +08:00
|
|
|
int columnToByte(int n) const {
|
|
|
|
assert(0<=n && n<static_cast<int>(m_columnToByte.size()));
|
|
|
|
return m_columnToByte[n];
|
|
|
|
}
|
2012-09-14 02:37:50 +08:00
|
|
|
|
|
|
|
/// \brief Map from a byte index to the next byte which starts a column.
|
|
|
|
int startOfNextColumn(int N) const {
|
2015-01-08 21:19:07 +08:00
|
|
|
assert(0 <= N && N < static_cast<int>(m_byteToColumn.size() - 1));
|
2012-09-14 02:37:50 +08:00
|
|
|
while (byteToColumn(++N) == -1) {}
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Map from a byte index to the previous byte which starts a column.
|
|
|
|
int startOfPreviousColumn(int N) const {
|
2015-01-08 21:19:07 +08:00
|
|
|
assert(0 < N && N < static_cast<int>(m_byteToColumn.size()));
|
2012-11-04 05:21:14 +08:00
|
|
|
while (byteToColumn(--N) == -1) {}
|
2012-09-14 02:37:50 +08:00
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
2012-04-18 10:44:46 +08:00
|
|
|
StringRef getSourceLine() const {
|
|
|
|
return m_SourceLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const std::string m_SourceLine;
|
|
|
|
SmallVector<int,200> m_byteToColumn;
|
|
|
|
SmallVector<int,200> m_columnToByte;
|
|
|
|
};
|
2012-12-02 04:58:01 +08:00
|
|
|
} // end anonymous namespace
|
2012-04-18 10:44:46 +08:00
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
/// \brief When the source code line we want to print is too long for
|
|
|
|
/// the terminal, select the "interesting" region.
|
2011-10-16 07:54:09 +08:00
|
|
|
static void selectInterestingSourceRegion(std::string &SourceLine,
|
2011-10-16 07:43:53 +08:00
|
|
|
std::string &CaretLine,
|
|
|
|
std::string &FixItInsertionLine,
|
2012-04-18 10:44:46 +08:00
|
|
|
unsigned Columns,
|
|
|
|
const SourceColumnMap &map) {
|
2015-01-08 21:19:07 +08:00
|
|
|
unsigned CaretColumns = CaretLine.size();
|
|
|
|
unsigned FixItColumns = llvm::sys::locale::columnWidth(FixItInsertionLine);
|
|
|
|
unsigned MaxColumns = std::max(static_cast<unsigned>(map.columns()),
|
|
|
|
std::max(CaretColumns, FixItColumns));
|
2012-04-18 10:44:46 +08:00
|
|
|
// if the number of columns is less than the desired number we're done
|
|
|
|
if (MaxColumns <= Columns)
|
|
|
|
return;
|
|
|
|
|
2013-06-08 01:16:01 +08:00
|
|
|
// No special characters are allowed in CaretLine.
|
2012-04-18 10:44:46 +08:00
|
|
|
assert(CaretLine.end() ==
|
|
|
|
std::find_if(CaretLine.begin(), CaretLine.end(),
|
2014-03-01 22:48:57 +08:00
|
|
|
[](char c) { return c < ' ' || '~' < c; }));
|
2012-04-18 10:44:46 +08:00
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
// Find the slice that we need to display the full caret line
|
|
|
|
// correctly.
|
|
|
|
unsigned CaretStart = 0, CaretEnd = CaretLine.size();
|
|
|
|
for (; CaretStart != CaretEnd; ++CaretStart)
|
2013-02-09 06:30:41 +08:00
|
|
|
if (!isWhitespace(CaretLine[CaretStart]))
|
2011-10-16 07:43:53 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
for (; CaretEnd != CaretStart; --CaretEnd)
|
2013-02-09 06:30:41 +08:00
|
|
|
if (!isWhitespace(CaretLine[CaretEnd - 1]))
|
2011-10-16 07:43:53 +08:00
|
|
|
break;
|
|
|
|
|
2012-04-18 10:44:46 +08:00
|
|
|
// caret has already been inserted into CaretLine so the above whitespace
|
|
|
|
// check is guaranteed to include the caret
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// If we have a fix-it line, make sure the slice includes all of the
|
|
|
|
// fix-it information.
|
|
|
|
if (!FixItInsertionLine.empty()) {
|
|
|
|
unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
|
|
|
|
for (; FixItStart != FixItEnd; ++FixItStart)
|
2013-02-09 06:30:41 +08:00
|
|
|
if (!isWhitespace(FixItInsertionLine[FixItStart]))
|
2011-10-16 07:43:53 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
for (; FixItEnd != FixItStart; --FixItEnd)
|
2013-02-09 06:30:41 +08:00
|
|
|
if (!isWhitespace(FixItInsertionLine[FixItEnd - 1]))
|
2011-10-16 07:43:53 +08:00
|
|
|
break;
|
|
|
|
|
2013-06-08 01:16:01 +08:00
|
|
|
// We can safely use the byte offset FixItStart as the column offset
|
|
|
|
// because the characters up until FixItStart are all ASCII whitespace
|
|
|
|
// characters.
|
|
|
|
unsigned FixItStartCol = FixItStart;
|
|
|
|
unsigned FixItEndCol
|
|
|
|
= llvm::sys::locale::columnWidth(FixItInsertionLine.substr(0, FixItEnd));
|
|
|
|
|
|
|
|
CaretStart = std::min(FixItStartCol, CaretStart);
|
|
|
|
CaretEnd = std::max(FixItEndCol, CaretEnd);
|
2011-10-16 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-24 13:14:44 +08:00
|
|
|
// CaretEnd may have been set at the middle of a character
|
|
|
|
// If it's not at a character's first column then advance it past the current
|
|
|
|
// character.
|
|
|
|
while (static_cast<int>(CaretEnd) < map.columns() &&
|
|
|
|
-1 == map.columnToByte(CaretEnd))
|
|
|
|
++CaretEnd;
|
|
|
|
|
|
|
|
assert((static_cast<int>(CaretStart) > map.columns() ||
|
|
|
|
-1!=map.columnToByte(CaretStart)) &&
|
|
|
|
"CaretStart must not point to a column in the middle of a source"
|
|
|
|
" line character");
|
|
|
|
assert((static_cast<int>(CaretEnd) > map.columns() ||
|
|
|
|
-1!=map.columnToByte(CaretEnd)) &&
|
|
|
|
"CaretEnd must not point to a column in the middle of a source line"
|
|
|
|
" character");
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
// CaretLine[CaretStart, CaretEnd) contains all of the interesting
|
|
|
|
// parts of the caret line. While this slice is smaller than the
|
|
|
|
// number of columns we have, try to grow the slice to encompass
|
|
|
|
// more context.
|
|
|
|
|
2012-04-18 10:44:46 +08:00
|
|
|
unsigned SourceStart = map.columnToByte(std::min<unsigned>(CaretStart,
|
|
|
|
map.columns()));
|
|
|
|
unsigned SourceEnd = map.columnToByte(std::min<unsigned>(CaretEnd,
|
|
|
|
map.columns()));
|
|
|
|
|
|
|
|
unsigned CaretColumnsOutsideSource = CaretEnd-CaretStart
|
|
|
|
- (map.byteToColumn(SourceEnd)-map.byteToColumn(SourceStart));
|
|
|
|
|
|
|
|
char const *front_ellipse = " ...";
|
|
|
|
char const *front_space = " ";
|
|
|
|
char const *back_ellipse = "...";
|
|
|
|
unsigned ellipses_space = strlen(front_ellipse) + strlen(back_ellipse);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
unsigned TargetColumns = Columns;
|
2012-04-18 10:44:46 +08:00
|
|
|
// Give us extra room for the ellipses
|
|
|
|
// and any of the caret line that extends past the source
|
|
|
|
if (TargetColumns > ellipses_space+CaretColumnsOutsideSource)
|
|
|
|
TargetColumns -= ellipses_space+CaretColumnsOutsideSource;
|
|
|
|
|
|
|
|
while (SourceStart>0 || SourceEnd<SourceLine.size()) {
|
2011-10-16 07:43:53 +08:00
|
|
|
bool ExpandedRegion = false;
|
2012-04-18 10:44:46 +08:00
|
|
|
|
|
|
|
if (SourceStart>0) {
|
2012-11-04 05:21:17 +08:00
|
|
|
unsigned NewStart = map.startOfPreviousColumn(SourceStart);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// Skip over any whitespace we see here; we're looking for
|
|
|
|
// another bit of interesting text.
|
2012-09-14 02:37:50 +08:00
|
|
|
// FIXME: Detect non-ASCII whitespace characters too.
|
2013-02-09 06:30:41 +08:00
|
|
|
while (NewStart && isWhitespace(SourceLine[NewStart]))
|
2012-09-14 02:37:50 +08:00
|
|
|
NewStart = map.startOfPreviousColumn(NewStart);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// Skip over this bit of "interesting" text.
|
2012-09-14 02:37:50 +08:00
|
|
|
while (NewStart) {
|
|
|
|
unsigned Prev = map.startOfPreviousColumn(NewStart);
|
2013-02-09 06:30:41 +08:00
|
|
|
if (isWhitespace(SourceLine[Prev]))
|
2012-09-14 02:37:50 +08:00
|
|
|
break;
|
|
|
|
NewStart = Prev;
|
|
|
|
}
|
2011-10-16 07:43:53 +08:00
|
|
|
|
2012-09-14 02:37:50 +08:00
|
|
|
assert(map.byteToColumn(NewStart) != -1);
|
2012-04-18 10:44:46 +08:00
|
|
|
unsigned NewColumns = map.byteToColumn(SourceEnd) -
|
|
|
|
map.byteToColumn(NewStart);
|
|
|
|
if (NewColumns <= TargetColumns) {
|
|
|
|
SourceStart = NewStart;
|
2011-10-16 07:43:53 +08:00
|
|
|
ExpandedRegion = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-18 10:44:46 +08:00
|
|
|
if (SourceEnd<SourceLine.size()) {
|
2012-11-04 05:21:17 +08:00
|
|
|
unsigned NewEnd = map.startOfNextColumn(SourceEnd);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// Skip over any whitespace we see here; we're looking for
|
|
|
|
// another bit of interesting text.
|
2012-09-14 02:37:50 +08:00
|
|
|
// FIXME: Detect non-ASCII whitespace characters too.
|
2013-02-09 06:30:41 +08:00
|
|
|
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
|
2012-09-14 02:37:50 +08:00
|
|
|
NewEnd = map.startOfNextColumn(NewEnd);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// Skip over this bit of "interesting" text.
|
2013-02-09 06:30:41 +08:00
|
|
|
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
|
2012-09-14 02:37:50 +08:00
|
|
|
NewEnd = map.startOfNextColumn(NewEnd);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
2012-09-14 02:37:50 +08:00
|
|
|
assert(map.byteToColumn(NewEnd) != -1);
|
2012-04-18 10:44:46 +08:00
|
|
|
unsigned NewColumns = map.byteToColumn(NewEnd) -
|
|
|
|
map.byteToColumn(SourceStart);
|
|
|
|
if (NewColumns <= TargetColumns) {
|
|
|
|
SourceEnd = NewEnd;
|
2011-10-16 07:43:53 +08:00
|
|
|
ExpandedRegion = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ExpandedRegion)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-18 10:44:46 +08:00
|
|
|
CaretStart = map.byteToColumn(SourceStart);
|
|
|
|
CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
// [CaretStart, CaretEnd) is the slice we want. Update the various
|
|
|
|
// output lines to show only this slice, with two-space padding
|
|
|
|
// before the lines so that it looks nicer.
|
2012-04-18 10:44:46 +08:00
|
|
|
|
|
|
|
assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 &&
|
|
|
|
SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1);
|
|
|
|
assert(SourceStart <= SourceEnd);
|
|
|
|
assert(CaretStart <= CaretEnd);
|
|
|
|
|
|
|
|
unsigned BackColumnsRemoved
|
|
|
|
= map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd);
|
|
|
|
unsigned FrontColumnsRemoved = CaretStart;
|
|
|
|
unsigned ColumnsKept = CaretEnd-CaretStart;
|
|
|
|
|
|
|
|
// We checked up front that the line needed truncation
|
|
|
|
assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns);
|
|
|
|
|
2014-12-20 16:51:22 +08:00
|
|
|
// The line needs some truncation, and we'd prefer to keep the front
|
2012-04-18 10:44:46 +08:00
|
|
|
// if possible, so remove the back
|
2012-11-04 07:56:43 +08:00
|
|
|
if (BackColumnsRemoved > strlen(back_ellipse))
|
2012-04-18 10:44:46 +08:00
|
|
|
SourceLine.replace(SourceEnd, std::string::npos, back_ellipse);
|
|
|
|
|
|
|
|
// If that's enough then we're done
|
|
|
|
if (FrontColumnsRemoved+ColumnsKept <= Columns)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Otherwise remove the front as well
|
2012-11-04 07:56:43 +08:00
|
|
|
if (FrontColumnsRemoved > strlen(front_ellipse)) {
|
2012-04-18 10:44:46 +08:00
|
|
|
SourceLine.replace(0, SourceStart, front_ellipse);
|
|
|
|
CaretLine.replace(0, CaretStart, front_space);
|
|
|
|
if (!FixItInsertionLine.empty())
|
|
|
|
FixItInsertionLine.replace(0, CaretStart, front_space);
|
2011-10-16 07:43:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Skip over whitespace in the string, starting at the given
|
|
|
|
/// index.
|
|
|
|
///
|
|
|
|
/// \returns The index of the first non-whitespace character that is
|
|
|
|
/// greater than or equal to Idx or, if no such character exists,
|
|
|
|
/// returns the end of the string.
|
|
|
|
static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) {
|
2013-02-09 06:30:41 +08:00
|
|
|
while (Idx < Length && isWhitespace(Str[Idx]))
|
2011-10-16 07:43:53 +08:00
|
|
|
++Idx;
|
|
|
|
return Idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief If the given character is the start of some kind of
|
|
|
|
/// balanced punctuation (e.g., quotes or parentheses), return the
|
|
|
|
/// character that will terminate the punctuation.
|
|
|
|
///
|
|
|
|
/// \returns The ending punctuation character, if any, or the NULL
|
|
|
|
/// character if the input character does not start any punctuation.
|
|
|
|
static inline char findMatchingPunctuation(char c) {
|
|
|
|
switch (c) {
|
|
|
|
case '\'': return '\'';
|
|
|
|
case '`': return '\'';
|
|
|
|
case '"': return '"';
|
|
|
|
case '(': return ')';
|
|
|
|
case '[': return ']';
|
|
|
|
case '{': return '}';
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Find the end of the word starting at the given offset
|
|
|
|
/// within a string.
|
|
|
|
///
|
|
|
|
/// \returns the index pointing one character past the end of the
|
|
|
|
/// word.
|
|
|
|
static unsigned findEndOfWord(unsigned Start, StringRef Str,
|
|
|
|
unsigned Length, unsigned Column,
|
|
|
|
unsigned Columns) {
|
|
|
|
assert(Start < Str.size() && "Invalid start position!");
|
|
|
|
unsigned End = Start + 1;
|
|
|
|
|
|
|
|
// If we are already at the end of the string, take that as the word.
|
|
|
|
if (End == Str.size())
|
|
|
|
return End;
|
|
|
|
|
|
|
|
// Determine if the start of the string is actually opening
|
|
|
|
// punctuation, e.g., a quote or parentheses.
|
|
|
|
char EndPunct = findMatchingPunctuation(Str[Start]);
|
|
|
|
if (!EndPunct) {
|
|
|
|
// This is a normal word. Just find the first space character.
|
2013-02-09 06:30:41 +08:00
|
|
|
while (End < Length && !isWhitespace(Str[End]))
|
2011-10-16 07:43:53 +08:00
|
|
|
++End;
|
|
|
|
return End;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have the start of a balanced punctuation sequence (quotes,
|
|
|
|
// parentheses, etc.). Determine the full sequence is.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<16> PunctuationEndStack;
|
2011-10-16 07:43:53 +08:00
|
|
|
PunctuationEndStack.push_back(EndPunct);
|
|
|
|
while (End < Length && !PunctuationEndStack.empty()) {
|
|
|
|
if (Str[End] == PunctuationEndStack.back())
|
|
|
|
PunctuationEndStack.pop_back();
|
|
|
|
else if (char SubEndPunct = findMatchingPunctuation(Str[End]))
|
|
|
|
PunctuationEndStack.push_back(SubEndPunct);
|
|
|
|
|
|
|
|
++End;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first space character after the punctuation ended.
|
2013-02-09 06:30:41 +08:00
|
|
|
while (End < Length && !isWhitespace(Str[End]))
|
2011-10-16 07:43:53 +08:00
|
|
|
++End;
|
|
|
|
|
|
|
|
unsigned PunctWordLength = End - Start;
|
|
|
|
if (// If the word fits on this line
|
|
|
|
Column + PunctWordLength <= Columns ||
|
|
|
|
// ... or the word is "short enough" to take up the next line
|
|
|
|
// without too much ugly white space
|
|
|
|
PunctWordLength < Columns/3)
|
|
|
|
return End; // Take the whole thing as a single "word".
|
|
|
|
|
|
|
|
// The whole quoted/parenthesized string is too long to print as a
|
|
|
|
// single "word". Instead, find the "word" that starts just after
|
|
|
|
// the punctuation and use that end-point instead. This will recurse
|
|
|
|
// until it finds something small enough to consider a word.
|
|
|
|
return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Print the given string to a stream, word-wrapping it to
|
|
|
|
/// some number of columns in the process.
|
|
|
|
///
|
|
|
|
/// \param OS the stream to which the word-wrapping string will be
|
|
|
|
/// emitted.
|
|
|
|
/// \param Str the string to word-wrap and output.
|
|
|
|
/// \param Columns the number of columns to word-wrap to.
|
|
|
|
/// \param Column the column number at which the first character of \p
|
|
|
|
/// Str will be printed. This will be non-zero when part of the first
|
|
|
|
/// line has already been printed.
|
2012-06-29 06:39:03 +08:00
|
|
|
/// \param Bold if the current text should be bold
|
2011-10-16 07:43:53 +08:00
|
|
|
/// \param Indentation the number of spaces to indent any lines beyond
|
|
|
|
/// the first line.
|
|
|
|
/// \returns true if word-wrapping was required, or false if the
|
|
|
|
/// string fit on the first line.
|
|
|
|
static bool printWordWrapped(raw_ostream &OS, StringRef Str,
|
|
|
|
unsigned Columns,
|
|
|
|
unsigned Column = 0,
|
2012-06-29 06:39:03 +08:00
|
|
|
bool Bold = false,
|
2011-10-16 07:43:53 +08:00
|
|
|
unsigned Indentation = WordWrapIndentation) {
|
|
|
|
const unsigned Length = std::min(Str.find('\n'), Str.size());
|
2012-06-27 02:18:47 +08:00
|
|
|
bool TextNormal = true;
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// The string used to indent each line.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<16> IndentStr;
|
2011-10-16 07:43:53 +08:00
|
|
|
IndentStr.assign(Indentation, ' ');
|
|
|
|
bool Wrapped = false;
|
|
|
|
for (unsigned WordStart = 0, WordEnd; WordStart < Length;
|
|
|
|
WordStart = WordEnd) {
|
|
|
|
// Find the beginning of the next word.
|
|
|
|
WordStart = skipWhitespace(WordStart, Str, Length);
|
|
|
|
if (WordStart == Length)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Find the end of this word.
|
|
|
|
WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
|
|
|
|
|
|
|
|
// Does this word fit on the current line?
|
|
|
|
unsigned WordLength = WordEnd - WordStart;
|
|
|
|
if (Column + WordLength < Columns) {
|
|
|
|
// This word fits on the current line; print it there.
|
|
|
|
if (WordStart) {
|
|
|
|
OS << ' ';
|
|
|
|
Column += 1;
|
|
|
|
}
|
2012-06-27 02:18:47 +08:00
|
|
|
applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
|
2012-06-29 06:39:03 +08:00
|
|
|
TextNormal, Bold);
|
2011-10-16 07:43:53 +08:00
|
|
|
Column += WordLength;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This word does not fit on the current line, so wrap to the next
|
|
|
|
// line.
|
|
|
|
OS << '\n';
|
|
|
|
OS.write(&IndentStr[0], Indentation);
|
2012-06-27 02:18:47 +08:00
|
|
|
applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
|
2012-06-29 06:39:03 +08:00
|
|
|
TextNormal, Bold);
|
2011-10-16 07:43:53 +08:00
|
|
|
Column = Indentation + WordLength;
|
|
|
|
Wrapped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append any remaning text from the message with its existing formatting.
|
2012-06-29 06:39:03 +08:00
|
|
|
applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold);
|
2012-06-27 02:18:47 +08:00
|
|
|
|
|
|
|
assert(TextNormal && "Text highlighted at end of diagnostic message.");
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
return Wrapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextDiagnostic::TextDiagnostic(raw_ostream &OS,
|
|
|
|
const LangOptions &LangOpts,
|
2012-10-24 06:26:28 +08:00
|
|
|
DiagnosticOptions *DiagOpts)
|
2012-05-10 13:03:45 +08:00
|
|
|
: DiagnosticRenderer(LangOpts, DiagOpts), OS(OS) {}
|
2011-10-16 07:43:53 +08:00
|
|
|
|
2015-10-20 21:23:58 +08:00
|
|
|
TextDiagnostic::~TextDiagnostic() {}
|
2011-10-16 07:43:53 +08:00
|
|
|
|
2011-12-17 13:26:04 +08:00
|
|
|
void
|
|
|
|
TextDiagnostic::emitDiagnosticMessage(SourceLocation Loc,
|
|
|
|
PresumedLoc PLoc,
|
|
|
|
DiagnosticsEngine::Level Level,
|
|
|
|
StringRef Message,
|
|
|
|
ArrayRef<clang::CharSourceRange> Ranges,
|
2012-05-10 13:03:45 +08:00
|
|
|
const SourceManager *SM,
|
2012-02-14 10:46:00 +08:00
|
|
|
DiagOrStoredDiag D) {
|
2011-10-16 07:43:53 +08:00
|
|
|
uint64_t StartOfLocationInfo = OS.tell();
|
|
|
|
|
2011-12-17 13:26:04 +08:00
|
|
|
// Emit the location of this particular diagnostic.
|
2012-05-10 13:03:45 +08:00
|
|
|
if (Loc.isValid())
|
|
|
|
emitDiagnosticLoc(Loc, PLoc, Level, Ranges, *SM);
|
2011-12-17 13:26:04 +08:00
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors)
|
2011-10-16 07:43:53 +08:00
|
|
|
OS.resetColor();
|
2011-12-17 13:26:04 +08:00
|
|
|
|
2013-09-24 08:08:55 +08:00
|
|
|
printDiagnosticLevel(OS, Level, DiagOpts->ShowColors,
|
|
|
|
DiagOpts->CLFallbackMode);
|
2014-06-22 07:31:52 +08:00
|
|
|
printDiagnosticMessage(OS,
|
|
|
|
/*IsSupplemental*/ Level == DiagnosticsEngine::Note,
|
|
|
|
Message, OS.tell() - StartOfLocationInfo,
|
2012-10-24 06:26:28 +08:00
|
|
|
DiagOpts->MessageLength, DiagOpts->ShowColors);
|
2011-10-16 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
2011-10-16 07:48:02 +08:00
|
|
|
/*static*/ void
|
|
|
|
TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
|
|
|
|
DiagnosticsEngine::Level Level,
|
2013-09-24 08:08:55 +08:00
|
|
|
bool ShowColors,
|
|
|
|
bool CLFallbackMode) {
|
2011-10-16 07:48:02 +08:00
|
|
|
if (ShowColors) {
|
|
|
|
// Print diagnostic category in bold and color
|
|
|
|
switch (Level) {
|
|
|
|
case DiagnosticsEngine::Ignored:
|
|
|
|
llvm_unreachable("Invalid diagnostic type");
|
|
|
|
case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break;
|
2014-02-28 17:11:08 +08:00
|
|
|
case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break;
|
2011-10-16 07:48:02 +08:00
|
|
|
case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break;
|
|
|
|
case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break;
|
|
|
|
case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (Level) {
|
|
|
|
case DiagnosticsEngine::Ignored:
|
|
|
|
llvm_unreachable("Invalid diagnostic type");
|
2013-09-24 08:08:55 +08:00
|
|
|
case DiagnosticsEngine::Note: OS << "note"; break;
|
2014-02-28 17:11:08 +08:00
|
|
|
case DiagnosticsEngine::Remark: OS << "remark"; break;
|
2013-09-24 08:08:55 +08:00
|
|
|
case DiagnosticsEngine::Warning: OS << "warning"; break;
|
|
|
|
case DiagnosticsEngine::Error: OS << "error"; break;
|
|
|
|
case DiagnosticsEngine::Fatal: OS << "fatal error"; break;
|
2011-10-16 07:48:02 +08:00
|
|
|
}
|
|
|
|
|
2013-09-24 08:08:55 +08:00
|
|
|
// In clang-cl /fallback mode, print diagnostics as "error(clang):". This
|
|
|
|
// makes it more clear whether a message is coming from clang or cl.exe,
|
|
|
|
// and it prevents MSBuild from concluding that the build failed just because
|
|
|
|
// there is an "error:" in the output.
|
|
|
|
if (CLFallbackMode)
|
|
|
|
OS << "(clang)";
|
|
|
|
|
|
|
|
OS << ": ";
|
|
|
|
|
2011-10-16 07:48:02 +08:00
|
|
|
if (ShowColors)
|
|
|
|
OS.resetColor();
|
|
|
|
}
|
|
|
|
|
2014-06-22 07:31:52 +08:00
|
|
|
/*static*/
|
|
|
|
void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
|
|
|
|
bool IsSupplemental,
|
|
|
|
StringRef Message,
|
|
|
|
unsigned CurrentColumn,
|
|
|
|
unsigned Columns, bool ShowColors) {
|
2012-06-29 06:39:03 +08:00
|
|
|
bool Bold = false;
|
2014-06-22 07:31:52 +08:00
|
|
|
if (ShowColors && !IsSupplemental) {
|
|
|
|
// Print primary diagnostic messages in bold and without color, to visually
|
|
|
|
// indicate the transition from continuation notes and other output.
|
|
|
|
OS.changeColor(savedColor, true);
|
|
|
|
Bold = true;
|
2011-10-16 07:48:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Columns)
|
2012-06-29 06:39:03 +08:00
|
|
|
printWordWrapped(OS, Message, Columns, CurrentColumn, Bold);
|
2012-06-29 05:46:07 +08:00
|
|
|
else {
|
|
|
|
bool Normal = true;
|
2012-06-29 06:39:03 +08:00
|
|
|
applyTemplateHighlighting(OS, Message, Normal, Bold);
|
2012-06-29 05:46:07 +08:00
|
|
|
assert(Normal && "Formatting should have returned to normal");
|
|
|
|
}
|
2011-10-16 07:48:02 +08:00
|
|
|
|
|
|
|
if (ShowColors)
|
|
|
|
OS.resetColor();
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Print out the file/line/column information and include trace.
|
|
|
|
///
|
|
|
|
/// This method handlen the emission of the diagnostic location information.
|
|
|
|
/// This includes extracting as much location information as is present for
|
|
|
|
/// the diagnostic and printing it, as well as any include stack or source
|
|
|
|
/// ranges necessary.
|
2011-10-16 07:54:09 +08:00
|
|
|
void TextDiagnostic::emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc,
|
2011-10-16 07:48:02 +08:00
|
|
|
DiagnosticsEngine::Level Level,
|
2012-05-10 13:03:45 +08:00
|
|
|
ArrayRef<CharSourceRange> Ranges,
|
|
|
|
const SourceManager &SM) {
|
2011-10-16 07:48:02 +08:00
|
|
|
if (PLoc.isInvalid()) {
|
|
|
|
// At least print the file name if available:
|
|
|
|
FileID FID = SM.getFileID(Loc);
|
2015-10-03 18:46:20 +08:00
|
|
|
if (FID.isValid()) {
|
2011-10-16 07:48:02 +08:00
|
|
|
const FileEntry* FE = SM.getFileEntryForID(FID);
|
2014-02-28 01:23:33 +08:00
|
|
|
if (FE && FE->isValid()) {
|
2011-10-16 07:48:02 +08:00
|
|
|
OS << FE->getName();
|
2013-08-02 05:42:11 +08:00
|
|
|
if (FE->isInPCH())
|
2011-10-16 07:48:02 +08:00
|
|
|
OS << " (in PCH)";
|
|
|
|
OS << ": ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned LineNo = PLoc.getLine();
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (!DiagOpts->ShowLocation)
|
2011-10-16 07:48:02 +08:00
|
|
|
return;
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors)
|
2011-10-16 07:48:02 +08:00
|
|
|
OS.changeColor(savedColor, true);
|
|
|
|
|
|
|
|
OS << PLoc.getFilename();
|
2012-10-24 07:11:23 +08:00
|
|
|
switch (DiagOpts->getFormat()) {
|
2011-10-16 07:48:02 +08:00
|
|
|
case DiagnosticOptions::Clang: OS << ':' << LineNo; break;
|
2015-02-03 03:30:52 +08:00
|
|
|
case DiagnosticOptions::MSVC: OS << '(' << LineNo; break;
|
2011-10-16 07:48:02 +08:00
|
|
|
case DiagnosticOptions::Vi: OS << " +" << LineNo; break;
|
|
|
|
}
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColumn)
|
2011-10-16 07:48:02 +08:00
|
|
|
// Compute the column number.
|
|
|
|
if (unsigned ColNo = PLoc.getColumn()) {
|
2015-02-03 03:30:52 +08:00
|
|
|
if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) {
|
2011-10-16 07:48:02 +08:00
|
|
|
OS << ',';
|
2014-03-07 08:23:36 +08:00
|
|
|
// Visual Studio 2010 or earlier expects column number to be off by one
|
2014-07-16 11:13:50 +08:00
|
|
|
if (LangOpts.MSCompatibilityVersion &&
|
2015-05-11 11:57:49 +08:00
|
|
|
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012))
|
2014-03-07 08:23:36 +08:00
|
|
|
ColNo--;
|
2011-10-16 07:48:02 +08:00
|
|
|
} else
|
|
|
|
OS << ':';
|
|
|
|
OS << ColNo;
|
|
|
|
}
|
2012-10-24 07:11:23 +08:00
|
|
|
switch (DiagOpts->getFormat()) {
|
2011-10-16 07:48:02 +08:00
|
|
|
case DiagnosticOptions::Clang:
|
|
|
|
case DiagnosticOptions::Vi: OS << ':'; break;
|
2016-03-24 06:57:55 +08:00
|
|
|
case DiagnosticOptions::MSVC:
|
|
|
|
// MSVC2013 and before print 'file(4) : error'. MSVC2015 gets rid of the
|
|
|
|
// space and prints 'file(4): error'.
|
|
|
|
OS << ')';
|
|
|
|
if (LangOpts.MSCompatibilityVersion &&
|
|
|
|
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
|
|
|
|
OS << ' ';
|
|
|
|
OS << ": ";
|
|
|
|
break;
|
2011-10-16 07:48:02 +08:00
|
|
|
}
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowSourceRanges && !Ranges.empty()) {
|
2011-10-16 07:48:02 +08:00
|
|
|
FileID CaretFileID =
|
|
|
|
SM.getFileID(SM.getExpansionLoc(Loc));
|
|
|
|
bool PrintedRange = false;
|
|
|
|
|
|
|
|
for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(),
|
|
|
|
RE = Ranges.end();
|
|
|
|
RI != RE; ++RI) {
|
|
|
|
// Ignore invalid ranges.
|
|
|
|
if (!RI->isValid()) continue;
|
|
|
|
|
|
|
|
SourceLocation B = SM.getExpansionLoc(RI->getBegin());
|
|
|
|
SourceLocation E = SM.getExpansionLoc(RI->getEnd());
|
|
|
|
|
|
|
|
// If the End location and the start location are the same and are a
|
|
|
|
// macro location, then the range was something that came from a
|
|
|
|
// macro expansion or _Pragma. If this is an object-like macro, the
|
|
|
|
// best we can do is to highlight the range. If this is a
|
|
|
|
// function-like macro, we'd also like to highlight the arguments.
|
|
|
|
if (B == E && RI->getEnd().isMacroID())
|
|
|
|
E = SM.getExpansionRange(RI->getEnd()).second;
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B);
|
|
|
|
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E);
|
|
|
|
|
|
|
|
// If the start or end of the range is in another file, just discard
|
|
|
|
// it.
|
|
|
|
if (BInfo.first != CaretFileID || EInfo.first != CaretFileID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Add in the length of the token, so that we cover multi-char
|
|
|
|
// tokens.
|
|
|
|
unsigned TokSize = 0;
|
|
|
|
if (RI->isTokenRange())
|
|
|
|
TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts);
|
|
|
|
|
|
|
|
OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':'
|
|
|
|
<< SM.getColumnNumber(BInfo.first, BInfo.second) << '-'
|
|
|
|
<< SM.getLineNumber(EInfo.first, EInfo.second) << ':'
|
|
|
|
<< (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize)
|
|
|
|
<< '}';
|
|
|
|
PrintedRange = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PrintedRange)
|
|
|
|
OS << ':';
|
|
|
|
}
|
|
|
|
OS << ' ';
|
|
|
|
}
|
|
|
|
|
2011-12-17 13:26:04 +08:00
|
|
|
void TextDiagnostic::emitIncludeLocation(SourceLocation Loc,
|
2012-05-10 13:03:45 +08:00
|
|
|
PresumedLoc PLoc,
|
|
|
|
const SourceManager &SM) {
|
2016-04-29 02:26:32 +08:00
|
|
|
if (DiagOpts->ShowLocation && PLoc.isValid())
|
2011-12-17 13:26:04 +08:00
|
|
|
OS << "In file included from " << PLoc.getFilename() << ':'
|
|
|
|
<< PLoc.getLine() << ":\n";
|
|
|
|
else
|
|
|
|
OS << "In included file:\n";
|
2011-10-16 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
2012-12-01 05:58:49 +08:00
|
|
|
void TextDiagnostic::emitImportLocation(SourceLocation Loc, PresumedLoc PLoc,
|
|
|
|
StringRef ModuleName,
|
|
|
|
const SourceManager &SM) {
|
2016-04-29 02:26:32 +08:00
|
|
|
if (DiagOpts->ShowLocation && PLoc.isValid())
|
2012-12-01 05:58:49 +08:00
|
|
|
OS << "In module '" << ModuleName << "' imported from "
|
|
|
|
<< PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
|
|
|
|
else
|
2015-08-11 08:05:21 +08:00
|
|
|
OS << "In module '" << ModuleName << "':\n";
|
2012-12-01 05:58:49 +08:00
|
|
|
}
|
|
|
|
|
2012-12-01 02:38:50 +08:00
|
|
|
void TextDiagnostic::emitBuildingModuleLocation(SourceLocation Loc,
|
|
|
|
PresumedLoc PLoc,
|
|
|
|
StringRef ModuleName,
|
|
|
|
const SourceManager &SM) {
|
2016-04-29 02:26:32 +08:00
|
|
|
if (DiagOpts->ShowLocation && PLoc.isValid())
|
2012-12-01 02:38:50 +08:00
|
|
|
OS << "While building module '" << ModuleName << "' imported from "
|
|
|
|
<< PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
|
|
|
|
else
|
|
|
|
OS << "While building module '" << ModuleName << "':\n";
|
|
|
|
}
|
|
|
|
|
2012-12-02 04:58:01 +08:00
|
|
|
/// \brief Highlight a SourceRange (with ~'s) for any characters on LineNo.
|
|
|
|
static void highlightRange(const CharSourceRange &R,
|
|
|
|
unsigned LineNo, FileID FID,
|
|
|
|
const SourceColumnMap &map,
|
|
|
|
std::string &CaretLine,
|
|
|
|
const SourceManager &SM,
|
|
|
|
const LangOptions &LangOpts) {
|
|
|
|
if (!R.isValid()) return;
|
|
|
|
|
|
|
|
SourceLocation Begin = R.getBegin();
|
|
|
|
SourceLocation End = R.getEnd();
|
|
|
|
|
|
|
|
unsigned StartLineNo = SM.getExpansionLineNumber(Begin);
|
|
|
|
if (StartLineNo > LineNo || SM.getFileID(Begin) != FID)
|
|
|
|
return; // No intersection.
|
|
|
|
|
|
|
|
unsigned EndLineNo = SM.getExpansionLineNumber(End);
|
|
|
|
if (EndLineNo < LineNo || SM.getFileID(End) != FID)
|
|
|
|
return; // No intersection.
|
|
|
|
|
|
|
|
// Compute the column number of the start.
|
|
|
|
unsigned StartColNo = 0;
|
|
|
|
if (StartLineNo == LineNo) {
|
|
|
|
StartColNo = SM.getExpansionColumnNumber(Begin);
|
|
|
|
if (StartColNo) --StartColNo; // Zero base the col #.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the column number of the end.
|
|
|
|
unsigned EndColNo = map.getSourceLine().size();
|
|
|
|
if (EndLineNo == LineNo) {
|
|
|
|
EndColNo = SM.getExpansionColumnNumber(End);
|
|
|
|
if (EndColNo) {
|
|
|
|
--EndColNo; // Zero base the col #.
|
|
|
|
|
|
|
|
// Add in the length of the token, so that we cover multi-char tokens if
|
|
|
|
// this is a token range.
|
|
|
|
if (R.isTokenRange())
|
|
|
|
EndColNo += Lexer::MeasureTokenLength(End, SM, LangOpts);
|
|
|
|
} else {
|
|
|
|
EndColNo = CaretLine.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StartColNo <= EndColNo && "Invalid range!");
|
|
|
|
|
|
|
|
// Check that a token range does not highlight only whitespace.
|
|
|
|
if (R.isTokenRange()) {
|
|
|
|
// Pick the first non-whitespace column.
|
|
|
|
while (StartColNo < map.getSourceLine().size() &&
|
|
|
|
(map.getSourceLine()[StartColNo] == ' ' ||
|
|
|
|
map.getSourceLine()[StartColNo] == '\t'))
|
|
|
|
StartColNo = map.startOfNextColumn(StartColNo);
|
|
|
|
|
|
|
|
// Pick the last non-whitespace column.
|
|
|
|
if (EndColNo > map.getSourceLine().size())
|
|
|
|
EndColNo = map.getSourceLine().size();
|
2013-03-16 07:09:37 +08:00
|
|
|
while (EndColNo &&
|
2012-12-02 04:58:01 +08:00
|
|
|
(map.getSourceLine()[EndColNo-1] == ' ' ||
|
|
|
|
map.getSourceLine()[EndColNo-1] == '\t'))
|
|
|
|
EndColNo = map.startOfPreviousColumn(EndColNo);
|
|
|
|
|
|
|
|
// If the start/end passed each other, then we are trying to highlight a
|
|
|
|
// range that just exists in whitespace, which must be some sort of other
|
|
|
|
// bug.
|
|
|
|
assert(StartColNo <= EndColNo && "Trying to highlight whitespace??");
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(StartColNo <= map.getSourceLine().size() && "Invalid range!");
|
|
|
|
assert(EndColNo <= map.getSourceLine().size() && "Invalid range!");
|
|
|
|
|
|
|
|
// Fill the range with ~'s.
|
|
|
|
StartColNo = map.byteToContainingColumn(StartColNo);
|
|
|
|
EndColNo = map.byteToContainingColumn(EndColNo);
|
|
|
|
|
|
|
|
assert(StartColNo <= EndColNo && "Invalid range!");
|
|
|
|
if (CaretLine.size() < EndColNo)
|
|
|
|
CaretLine.resize(EndColNo,' ');
|
|
|
|
std::fill(CaretLine.begin()+StartColNo,CaretLine.begin()+EndColNo,'~');
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string buildFixItInsertionLine(unsigned LineNo,
|
|
|
|
const SourceColumnMap &map,
|
|
|
|
ArrayRef<FixItHint> Hints,
|
|
|
|
const SourceManager &SM,
|
|
|
|
const DiagnosticOptions *DiagOpts) {
|
|
|
|
std::string FixItInsertionLine;
|
|
|
|
if (Hints.empty() || !DiagOpts->ShowFixits)
|
|
|
|
return FixItInsertionLine;
|
|
|
|
unsigned PrevHintEndCol = 0;
|
|
|
|
|
|
|
|
for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!I->CodeToInsert.empty()) {
|
|
|
|
// We have an insertion hint. Determine whether the inserted
|
|
|
|
// code contains no newlines and is on the same line as the caret.
|
|
|
|
std::pair<FileID, unsigned> HintLocInfo
|
|
|
|
= SM.getDecomposedExpansionLoc(I->RemoveRange.getBegin());
|
|
|
|
if (LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) &&
|
|
|
|
StringRef(I->CodeToInsert).find_first_of("\n\r") == StringRef::npos) {
|
|
|
|
// Insert the new code into the line just below the code
|
|
|
|
// that the user wrote.
|
|
|
|
// Note: When modifying this function, be very careful about what is a
|
|
|
|
// "column" (printed width, platform-dependent) and what is a
|
|
|
|
// "byte offset" (SourceManager "column").
|
|
|
|
unsigned HintByteOffset
|
|
|
|
= SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1;
|
|
|
|
|
|
|
|
// The hint must start inside the source or right at the end
|
|
|
|
assert(HintByteOffset < static_cast<unsigned>(map.bytes())+1);
|
|
|
|
unsigned HintCol = map.byteToContainingColumn(HintByteOffset);
|
|
|
|
|
|
|
|
// If we inserted a long previous hint, push this one forwards, and add
|
|
|
|
// an extra space to show that this is not part of the previous
|
|
|
|
// completion. This is sort of the best we can do when two hints appear
|
|
|
|
// to overlap.
|
|
|
|
//
|
|
|
|
// Note that if this hint is located immediately after the previous
|
|
|
|
// hint, no space will be added, since the location is more important.
|
|
|
|
if (HintCol < PrevHintEndCol)
|
|
|
|
HintCol = PrevHintEndCol + 1;
|
|
|
|
|
|
|
|
// This should NOT use HintByteOffset, because the source might have
|
|
|
|
// Unicode characters in earlier columns.
|
2013-06-08 01:16:01 +08:00
|
|
|
unsigned NewFixItLineSize = FixItInsertionLine.size() +
|
|
|
|
(HintCol - PrevHintEndCol) + I->CodeToInsert.size();
|
|
|
|
if (NewFixItLineSize > FixItInsertionLine.size())
|
|
|
|
FixItInsertionLine.resize(NewFixItLineSize, ' ');
|
2012-12-02 04:58:01 +08:00
|
|
|
|
|
|
|
std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(),
|
2013-06-08 01:16:01 +08:00
|
|
|
FixItInsertionLine.end() - I->CodeToInsert.size());
|
2012-12-02 04:58:01 +08:00
|
|
|
|
2013-06-08 01:16:01 +08:00
|
|
|
PrevHintEndCol =
|
|
|
|
HintCol + llvm::sys::locale::columnWidth(I->CodeToInsert);
|
2012-12-02 04:58:01 +08:00
|
|
|
} else {
|
|
|
|
FixItInsertionLine.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expandTabs(FixItInsertionLine, DiagOpts->TabStop);
|
|
|
|
|
|
|
|
return FixItInsertionLine;
|
|
|
|
}
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
/// \brief Emit a code snippet and caret line.
|
|
|
|
///
|
|
|
|
/// This routine emits a single line's code snippet and caret line..
|
|
|
|
///
|
|
|
|
/// \param Loc The location for the caret.
|
|
|
|
/// \param Ranges The underlined ranges for this code snippet.
|
|
|
|
/// \param Hints The FixIt hints active for this diagnostic.
|
2011-10-16 07:54:09 +08:00
|
|
|
void TextDiagnostic::emitSnippetAndCaret(
|
2011-10-16 15:20:28 +08:00
|
|
|
SourceLocation Loc, DiagnosticsEngine::Level Level,
|
2011-10-16 07:43:53 +08:00
|
|
|
SmallVectorImpl<CharSourceRange>& Ranges,
|
2012-05-10 13:03:45 +08:00
|
|
|
ArrayRef<FixItHint> Hints,
|
|
|
|
const SourceManager &SM) {
|
2015-10-03 13:15:57 +08:00
|
|
|
assert(Loc.isValid() && "must have a valid source location here");
|
2011-10-16 07:43:53 +08:00
|
|
|
assert(Loc.isFileID() && "must have a file location here");
|
|
|
|
|
2011-10-16 15:20:28 +08:00
|
|
|
// If caret diagnostics are enabled and we have location, we want to
|
|
|
|
// emit the caret. However, we only do this if the location moved
|
|
|
|
// from the last diagnostic, if the last diagnostic was a note that
|
|
|
|
// was part of a different warning or error diagnostic, or if the
|
|
|
|
// diagnostic has ranges. We don't want to emit the same caret
|
|
|
|
// multiple times if one loc has multiple diagnostics.
|
2012-10-24 06:26:28 +08:00
|
|
|
if (!DiagOpts->ShowCarets)
|
2011-10-16 15:20:28 +08:00
|
|
|
return;
|
|
|
|
if (Loc == LastLoc && Ranges.empty() && Hints.empty() &&
|
|
|
|
(LastLevel != DiagnosticsEngine::Note || Level == LastLevel))
|
|
|
|
return;
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
// Decompose the location into a FID/Offset pair.
|
|
|
|
std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
|
|
|
|
FileID FID = LocInfo.first;
|
|
|
|
unsigned FileOffset = LocInfo.second;
|
|
|
|
|
|
|
|
// Get information about the buffer it points into.
|
|
|
|
bool Invalid = false;
|
2016-02-18 06:37:45 +08:00
|
|
|
StringRef BufData = SM.getBufferData(FID, &Invalid);
|
2011-10-16 07:43:53 +08:00
|
|
|
if (Invalid)
|
|
|
|
return;
|
|
|
|
|
2016-02-18 06:37:45 +08:00
|
|
|
const char *BufStart = BufData.data();
|
|
|
|
const char *BufEnd = BufStart + BufData.size();
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
unsigned LineNo = SM.getLineNumber(FID, FileOffset);
|
|
|
|
unsigned ColNo = SM.getColumnNumber(FID, FileOffset);
|
2013-01-31 05:41:07 +08:00
|
|
|
|
|
|
|
// Arbitrarily stop showing snippets when the line is too long.
|
2013-04-23 22:42:47 +08:00
|
|
|
static const size_t MaxLineLengthToPrint = 4096;
|
2013-01-31 06:14:15 +08:00
|
|
|
if (ColNo > MaxLineLengthToPrint)
|
2013-01-31 05:41:07 +08:00
|
|
|
return;
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// Rewind from the current position to the start of the line.
|
|
|
|
const char *TokPtr = BufStart+FileOffset;
|
|
|
|
const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based.
|
|
|
|
|
|
|
|
// Compute the line end. Scan forward from the error position to the end of
|
|
|
|
// the line.
|
|
|
|
const char *LineEnd = TokPtr;
|
2016-02-18 06:37:45 +08:00
|
|
|
while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd)
|
2011-10-16 07:43:53 +08:00
|
|
|
++LineEnd;
|
|
|
|
|
2013-01-31 05:41:07 +08:00
|
|
|
// Arbitrarily stop showing snippets when the line is too long.
|
2013-04-23 22:42:47 +08:00
|
|
|
if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint)
|
2013-01-31 05:41:07 +08:00
|
|
|
return;
|
|
|
|
|
2016-02-18 06:37:45 +08:00
|
|
|
// Trim trailing null-bytes.
|
|
|
|
StringRef Line(LineStart, LineEnd - LineStart);
|
|
|
|
while (Line.size() > ColNo && Line.back() == '\0')
|
|
|
|
Line = Line.drop_back();
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
// Copy the line of code into an std::string for ease of manipulation.
|
2016-02-18 06:37:45 +08:00
|
|
|
std::string SourceLine(Line.begin(), Line.end());
|
2011-10-16 07:43:53 +08:00
|
|
|
|
2015-01-08 21:19:07 +08:00
|
|
|
// Build the byte to column map.
|
2012-10-24 06:26:28 +08:00
|
|
|
const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop);
|
2012-04-18 10:44:46 +08:00
|
|
|
|
2015-01-08 21:19:07 +08:00
|
|
|
// Create a line for the caret that is filled with spaces that is the same
|
|
|
|
// number of columns as the line of source code.
|
|
|
|
std::string CaretLine(sourceColMap.columns(), ' ');
|
|
|
|
|
2011-10-16 07:43:53 +08:00
|
|
|
// Highlight all of the characters covered by Ranges with ~ characters.
|
|
|
|
for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(),
|
|
|
|
E = Ranges.end();
|
|
|
|
I != E; ++I)
|
2012-12-02 04:58:01 +08:00
|
|
|
highlightRange(*I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// Next, insert the caret itself.
|
2012-09-14 02:37:50 +08:00
|
|
|
ColNo = sourceColMap.byteToContainingColumn(ColNo-1);
|
2012-04-18 10:44:46 +08:00
|
|
|
if (CaretLine.size()<ColNo+1)
|
|
|
|
CaretLine.resize(ColNo+1, ' ');
|
|
|
|
CaretLine[ColNo] = '^';
|
2012-04-18 04:06:03 +08:00
|
|
|
|
2012-04-18 10:44:46 +08:00
|
|
|
std::string FixItInsertionLine = buildFixItInsertionLine(LineNo,
|
|
|
|
sourceColMap,
|
2012-12-02 04:58:01 +08:00
|
|
|
Hints, SM,
|
2014-07-05 11:08:06 +08:00
|
|
|
DiagOpts.get());
|
2012-04-18 10:44:46 +08:00
|
|
|
|
|
|
|
// If the source line is too long for our terminal, select only the
|
|
|
|
// "interesting" source region within that line.
|
2012-10-24 06:26:28 +08:00
|
|
|
unsigned Columns = DiagOpts->MessageLength;
|
2012-04-18 10:44:46 +08:00
|
|
|
if (Columns)
|
|
|
|
selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
|
|
|
|
Columns, sourceColMap);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
|
|
|
// If we are in -fdiagnostics-print-source-range-info mode, we are trying
|
|
|
|
// to produce easily machine parsable output. Add a space before the
|
|
|
|
// source line and the caret to make it trivial to tell the main diagnostic
|
|
|
|
// line from what the user is intended to see.
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowSourceRanges) {
|
2011-10-16 07:43:53 +08:00
|
|
|
SourceLine = ' ' + SourceLine;
|
|
|
|
CaretLine = ' ' + CaretLine;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, remove any blank spaces from the end of CaretLine.
|
|
|
|
while (CaretLine[CaretLine.size()-1] == ' ')
|
|
|
|
CaretLine.erase(CaretLine.end()-1);
|
|
|
|
|
|
|
|
// Emit what we have computed.
|
2012-04-18 10:44:46 +08:00
|
|
|
emitSnippet(SourceLine);
|
2011-10-16 07:43:53 +08:00
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors)
|
2011-10-16 07:43:53 +08:00
|
|
|
OS.changeColor(caretColor, true);
|
|
|
|
OS << CaretLine << '\n';
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors)
|
2011-10-16 07:43:53 +08:00
|
|
|
OS.resetColor();
|
|
|
|
|
|
|
|
if (!FixItInsertionLine.empty()) {
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors)
|
2011-10-16 07:43:53 +08:00
|
|
|
// Print fixit line in color
|
|
|
|
OS.changeColor(fixitColor, false);
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowSourceRanges)
|
2011-10-16 07:43:53 +08:00
|
|
|
OS << ' ';
|
|
|
|
OS << FixItInsertionLine << '\n';
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors)
|
2011-10-16 07:43:53 +08:00
|
|
|
OS.resetColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out any parseable fixit information requested by the options.
|
2012-05-10 13:03:45 +08:00
|
|
|
emitParseableFixits(Hints, SM);
|
2011-10-16 07:43:53 +08:00
|
|
|
}
|
|
|
|
|
2012-05-01 22:34:11 +08:00
|
|
|
void TextDiagnostic::emitSnippet(StringRef line) {
|
2012-04-18 10:44:46 +08:00
|
|
|
if (line.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
std::string to_print;
|
|
|
|
bool print_reversed = false;
|
|
|
|
|
|
|
|
while (i<line.size()) {
|
|
|
|
std::pair<SmallString<16>,bool> res
|
2012-10-24 06:26:28 +08:00
|
|
|
= printableTextForNextCharacter(line, &i, DiagOpts->TabStop);
|
2012-04-18 10:44:46 +08:00
|
|
|
bool was_printable = res.second;
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors && was_printable == print_reversed) {
|
2012-04-18 10:44:46 +08:00
|
|
|
if (print_reversed)
|
|
|
|
OS.reverseColor();
|
|
|
|
OS << to_print;
|
|
|
|
to_print.clear();
|
2012-10-24 06:26:28 +08:00
|
|
|
if (DiagOpts->ShowColors)
|
2012-04-18 10:44:46 +08:00
|
|
|
OS.resetColor();
|
|
|
|
}
|
|
|
|
|
|
|
|
print_reversed = !was_printable;
|
|
|
|
to_print += res.first.str();
|
|
|
|
}
|
|
|
|
|
2012-10-24 06:26:28 +08:00
|
|
|
if (print_reversed && DiagOpts->ShowColors)
|
2012-04-18 10:44:46 +08:00
|
|
|
OS.reverseColor();
|
|
|
|
OS << to_print;
|
2012-10-24 06:26:28 +08:00
|
|
|
if (print_reversed && DiagOpts->ShowColors)
|
2012-04-18 10:44:46 +08:00
|
|
|
OS.resetColor();
|
|
|
|
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2012-05-10 13:03:45 +08:00
|
|
|
void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints,
|
|
|
|
const SourceManager &SM) {
|
2012-10-24 06:26:28 +08:00
|
|
|
if (!DiagOpts->ShowParseableFixits)
|
2011-10-16 07:43:53 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// We follow FixItRewriter's example in not (yet) handling
|
|
|
|
// fix-its in macros.
|
|
|
|
for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->RemoveRange.isInvalid() ||
|
|
|
|
I->RemoveRange.getBegin().isMacroID() ||
|
|
|
|
I->RemoveRange.getEnd().isMacroID())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
SourceLocation BLoc = I->RemoveRange.getBegin();
|
|
|
|
SourceLocation ELoc = I->RemoveRange.getEnd();
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
|
|
|
|
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
|
|
|
|
|
|
|
|
// Adjust for token ranges.
|
|
|
|
if (I->RemoveRange.isTokenRange())
|
|
|
|
EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
|
|
|
|
|
|
|
|
// We specifically do not do word-wrapping or tab-expansion here,
|
|
|
|
// because this is supposed to be easy to parse.
|
|
|
|
PresumedLoc PLoc = SM.getPresumedLoc(BLoc);
|
|
|
|
if (PLoc.isInvalid())
|
|
|
|
break;
|
|
|
|
|
|
|
|
OS << "fix-it:\"";
|
|
|
|
OS.write_escaped(PLoc.getFilename());
|
|
|
|
OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second)
|
|
|
|
<< ':' << SM.getColumnNumber(BInfo.first, BInfo.second)
|
|
|
|
<< '-' << SM.getLineNumber(EInfo.first, EInfo.second)
|
|
|
|
<< ':' << SM.getColumnNumber(EInfo.first, EInfo.second)
|
|
|
|
<< "}:\"";
|
|
|
|
OS.write_escaped(I->CodeToInsert);
|
|
|
|
OS << "\"\n";
|
|
|
|
}
|
|
|
|
}
|