//===--- 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"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/DiagnosticOptions.h"
#include "clang/Lex/Lexer.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/ADT/SmallString.h"
#include <algorithm>
using namespace clang;
static const enum raw_ostream::Colors noteColor =
raw_ostream::BLACK;
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;
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;
/// \brief Number of spaces to indent when word-wrapping.
const unsigned WordWrapIndentation = 6;
/// \brief When the source code line we want to print is too long for
/// the terminal, select the "interesting" region.
static void selectInterestingSourceRegion(std::string &SourceLine,
std::string &CaretLine,
std::string &FixItInsertionLine,
unsigned EndOfCaretToken,
unsigned Columns) {
unsigned MaxSize = std::max(SourceLine.size(),
std::max(CaretLine.size(),
FixItInsertionLine.size()));
if (MaxSize > SourceLine.size())
SourceLine.resize(MaxSize, ' ');
if (MaxSize > CaretLine.size())
CaretLine.resize(MaxSize, ' ');
if (!FixItInsertionLine.empty() && MaxSize > FixItInsertionLine.size())
FixItInsertionLine.resize(MaxSize, ' ');
// Find the slice that we need to display the full caret line
// correctly.
unsigned CaretStart = 0, CaretEnd = CaretLine.size();
for (; CaretStart != CaretEnd; ++CaretStart)
if (!isspace(CaretLine[CaretStart]))
break;
for (; CaretEnd != CaretStart; --CaretEnd)
if (!isspace(CaretLine[CaretEnd - 1]))
break;
// Make sure we don't chop the string shorter than the caret token
// itself.
if (CaretEnd < EndOfCaretToken)
CaretEnd = EndOfCaretToken;
// 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)
if (!isspace(FixItInsertionLine[FixItStart]))
break;
for (; FixItEnd != FixItStart; --FixItEnd)
if (!isspace(FixItInsertionLine[FixItEnd - 1]))
break;
if (FixItStart < CaretStart)
CaretStart = FixItStart;
if (FixItEnd > CaretEnd)
CaretEnd = FixItEnd;
}
// 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.
// If the end of the interesting region comes before we run out of
// space in the terminal, start at the beginning of the line.
if (Columns > 3 && CaretEnd < Columns - 3)
CaretStart = 0;
unsigned TargetColumns = Columns;
if (TargetColumns > 8)
TargetColumns -= 8; // Give us extra room for the ellipses.