#include "clang/Frontend/TextDiagnostic.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Preprocessor.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/ConvertUTF.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Locale.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <optional>
using namespace clang;
static const enum raw_ostream::Colors noteColor = raw_ostream::CYAN;
static const enum raw_ostream::Colors remarkColor =
raw_ostream::BLUE;
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 templateColor =
raw_ostream::CYAN;
static const enum raw_ostream::Colors errorColor = raw_ostream::RED;
static const enum raw_ostream::Colors fatalColor = raw_ostream::RED;
static const enum raw_ostream::Colors savedColor =
raw_ostream::SAVEDCOLOR;
static constexpr raw_ostream::Colors CommentColor = raw_ostream::YELLOW;
static constexpr raw_ostream::Colors LiteralColor = raw_ostream::GREEN;
static constexpr raw_ostream::Colors KeywordColor = raw_ostream::BLUE;
static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str,
bool &Normal, bool Bold) {
while (true) {
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);
}
Normal = !Normal;
}
}
const unsigned WordWrapIndentation = 6;
static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) {
int bytes = 0;
while (0<i) {
if (SourceLine[--i]=='\t')
break;
++bytes;
}
return bytes;
}
static std::pair<SmallString<16>, bool>
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);
}
const unsigned char *Begin = SourceLine.bytes_begin() + *I;
if (*Begin < 0x80 && llvm::sys::locale::isPrint(*Begin)) {
++(*I);
return std::make_pair(SmallString<16>(Begin, Begin + 1), true);
}
unsigned CharSize = llvm::getNumBytesForUTF8(*Begin);
const unsigned char *End = Begin + CharSize;
if (End <= SourceLine.bytes_end() && llvm::isLegalUTF8Sequence(Begin, End)) {
llvm::UTF32 C;
llvm::UTF32 *CPtr = &C;
unsigned char const *OriginalBegin = Begin;
llvm::ConversionResult Res = llvm::ConvertUTF8toUTF32(
&Begin, End, &CPtr, CPtr + 1, llvm::strictConversion);
(void)Res;
assert(Res == llvm::conversionOK);
assert(OriginalBegin < Begin);
assert(unsigned(Begin - OriginalBegin) == CharSize);
(*I) += (Begin - OriginalBegin);
if (llvm::sys::locale::isPrint(C))
return std::make_pair(SmallString<16>(OriginalBegin, End), true);
SmallString<16> Str("<U+>");
while (C) {
Str.insert(Str.begin() + 3, llvm::hexdigit(C % 16));
C /= 16;
}
while (Str.size() < 8)
Str.insert(Str.begin() + 3, llvm::hexdigit(0));
return std::make_pair(Str, false);
}
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);
}
static void expandTabs(std::string &SourceLine, unsigned TabStop) {
size_t I = SourceLine.size();
while (I > 0) {
I--;
if (SourceLine[I] != '\t')
continue;
size_t TmpI = I;
auto [Str, Printable] =
printableTextForNextCharacter(SourceLine, &TmpI, TabStop);
SourceLine.replace(I, 1, Str.c_str());
}
}
static void genColumnByteMapping(StringRef SourceLine, unsigned TabStop,
SmallVectorImpl<int> &BytesOut,
SmallVectorImpl<int> &ColumnsOut) {
assert(BytesOut.empty());
assert(ColumnsOut.empty());
if (SourceLine.empty()) {
BytesOut.resize(1u, 0);
ColumnsOut.resize(1u, 0);
return;
}
ColumnsOut.resize(SourceLine.size() + 1, -1);
int Columns = 0;
size_t I = 0;
while (I < SourceLine.size()) {
ColumnsOut[I] = Columns;
BytesOut.resize(Columns + 1, -1);
BytesOut.back() = I;
auto [Str, Printable] =
printableTextForNextCharacter(SourceLine, &I, TabStop);
Columns += llvm::sys::locale::columnWidth(Str);
}
ColumnsOut.back() = Columns;
BytesOut.resize(Columns + 1, -1);
BytesOut.back() = I;
}
namespace {
struct SourceColumnMap {
SourceColumnMap(StringRef SourceLine, unsigned TabStop)
: m_SourceLine(SourceLine) {
genColumnByteMapping(SourceLine, TabStop, m_columnToByte, m_byteToColumn);
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(); }
int byteToColumn(int n) const {
assert(0<=n && n<static_cast<int>(m_byteToColumn.size()));
return m_byteToColumn[n];
}
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];
}
int columnToByte(int n) const {
assert(0<=n && n<static_cast<int>(m_columnToByte.size()));
return m_columnToByte[n];
}
int startOfNextColumn(int N) const {
assert(0 <= N && N < static_cast<int>(m_byteToColumn.size() - 1));
while (byteToColumn(++N) == -1) {}
return N;
}
int startOfPreviousColumn(int N) const {
assert(0 < N && N < static_cast<int>(m_byteToColumn.size()));
while (byteToColumn(--N) == -1) {}
return N;
}
StringRef getSourceLine() const {
return m_SourceLine;
}
private:
const std::string m_SourceLine;
SmallVector<int,200> m_byteToColumn;
SmallVector<int,200> m_columnToByte;
};
}
static void selectInterestingSourceRegion(std::string &SourceLine,
std::string &CaretLine,
std::string &FixItInsertionLine,
unsigned Columns,
const SourceColumnMap &map) {
unsigned CaretColumns = CaretLine.size();
unsigned FixItColumns = llvm::sys::locale::columnWidth(FixItInsertionLine);
unsigned MaxColumns = std::max(static_cast<unsigned>(map.columns()),
std::max(CaretColumns, FixItColumns));
if (MaxColumns <= Columns)
return;
assert(llvm::none_of(CaretLine, [](char c) { return c < ' ' || '~' < c; }));
unsigned CaretStart = 0, CaretEnd = CaretLine.size();
for (; CaretStart != CaretEnd; ++CaretStart)
if (!isWhitespace(CaretLine[CaretStart]))
break;
for (; CaretEnd != CaretStart; --CaretEnd)
if (!isWhitespace(CaretLine[CaretEnd - 1]))
break;
if (!FixItInsertionLine.empty()) {
unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size();
for (; FixItStart != FixItEnd; ++FixItStart)
if (!isWhitespace(FixItInsertionLine[FixItStart]))
break;
for (; FixItEnd != FixItStart; --FixItEnd)
if (!isWhitespace(FixItInsertionLine[FixItEnd - 1]))
break;
unsigned FixItStartCol = FixItStart;
unsigned FixItEndCol
= llvm::sys::locale::columnWidth(FixItInsertionLine.substr(0, FixItEnd));
CaretStart = std::min(FixItStartCol, CaretStart);
CaretEnd = std::max(FixItEndCol, CaretEnd);
}
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");
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);
unsigned TargetColumns = Columns;
if (TargetColumns > ellipses_space+CaretColumnsOutsideSource)
TargetColumns -= ellipses_space+CaretColumnsOutsideSource;
while (SourceStart>0 || SourceEnd<SourceLine.size()) {
bool ExpandedRegion = false;
if (SourceStart>0) {
unsigned NewStart = map.startOfPreviousColumn(SourceStart);
while (NewStart && isWhitespace(SourceLine[NewStart]))
NewStart = map.startOfPreviousColumn(NewStart);
while (NewStart) {
unsigned Prev = map.startOfPreviousColumn(NewStart);
if (isWhitespace(SourceLine[Prev]))
break;
NewStart = Prev;
}
assert(map.byteToColumn(NewStart) != -1);
unsigned NewColumns = map.byteToColumn(SourceEnd) -
map.byteToColumn(NewStart);
if (NewColumns <= TargetColumns) {
SourceStart = NewStart;
ExpandedRegion = true;
}
}
if (SourceEnd<SourceLine.size()) {
unsigned NewEnd = map.startOfNextColumn(SourceEnd);
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
NewEnd = map.startOfNextColumn(NewEnd);
while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd]))
NewEnd = map.startOfNextColumn(NewEnd);
assert(map.byteToColumn(NewEnd) != -1);
unsigned NewColumns = map.byteToColumn(NewEnd) -
map.byteToColumn(SourceStart);
if (NewColumns <= TargetColumns) {
SourceEnd = NewEnd;
ExpandedRegion = true;
}
}
if (!ExpandedRegion)
break;
}
CaretStart = map.byteToColumn(SourceStart);
CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource;
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;
assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns);
if (BackColumnsRemoved > strlen(back_ellipse))
SourceLine.replace(SourceEnd, std::string::npos, back_ellipse);
if (FrontColumnsRemoved+ColumnsKept <= Columns)
return;
if (FrontColumnsRemoved > strlen(front_ellipse)) {
SourceLine.replace(0, SourceStart, front_ellipse);
CaretLine.replace(0, CaretStart, front_space);
if (!FixItInsertionLine.empty())
FixItInsertionLine.replace(0, CaretStart, front_space);
}
}
static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) {
while (Idx < Length && isWhitespace(Str[Idx]))
++Idx;
return Idx;
}
static inline char findMatchingPunctuation(char c) {
switch (c) {
case '\'': return '\'';
case '`': return '\'';
case '"': return '"';
case '(': return ')';
case '[': return ']';
case '{': return '}';
default: break;
}
return 0;
}
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 (End == Str.size())
return End;
char EndPunct = findMatchingPunctuation(Str[Start]);
if (!EndPunct) {
while (End < Length && !isWhitespace(Str[End]))
++End;
return End;
}
SmallString<16> PunctuationEndStack;
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;
}
while (End < Length && !isWhitespace(Str[End]))
++End;
unsigned PunctWordLength = End - Start;
if (
Column + PunctWordLength <= Columns ||
PunctWordLength < Columns/3)
return End;
return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns);
}
static bool printWordWrapped(raw_ostream &OS, StringRef Str, unsigned Columns,
unsigned Column, bool Bold) {
const unsigned Length = std::min(Str.find('\n'), Str.size());
bool TextNormal = true;
bool Wrapped = false;
for (unsigned WordStart = 0, WordEnd; WordStart < Length;
WordStart = WordEnd) {
WordStart = skipWhitespace(WordStart, Str, Length);
if (WordStart == Length)
break;
WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns);
unsigned WordLength = WordEnd - WordStart;
if (Column + WordLength < Columns) {
if (WordStart) {
OS << ' ';
Column += 1;
}
applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
TextNormal, Bold);
Column += WordLength;
continue;
}
OS << '\n';
OS.indent(WordWrapIndentation);
applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength),
TextNormal, Bold);
Column = WordWrapIndentation + WordLength;
Wrapped = true;
}
applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold);
assert(TextNormal && "Text highlighted at end of diagnostic message.");
return Wrapped;
}
TextDiagnostic::TextDiagnostic(raw_ostream &OS, const LangOptions &LangOpts,
DiagnosticOptions *DiagOpts,
const Preprocessor *PP)
: DiagnosticRenderer(LangOpts, DiagOpts), OS(OS), PP(PP) {}
TextDiagnostic::~TextDiagnostic() {}
void TextDiagnostic::emitDiagnosticMessage(
FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level,
StringRef Message, ArrayRef<clang::CharSourceRange> Ranges,
DiagOrStoredDiag D) {
uint64_t StartOfLocationInfo = OS.tell();
if (Loc.isValid())
emitDiagnosticLoc(Loc, PLoc, Level, Ranges);
if (DiagOpts->ShowColors)
OS.resetColor();
if (DiagOpts->ShowLevel)
printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
printDiagnosticMessage(OS,
Level == DiagnosticsEngine::Note,
Message, OS.tell() - StartOfLocationInfo,
DiagOpts->MessageLength, DiagOpts->ShowColors);
}
void
TextDiagnostic::printDiagnosticLevel(raw_ostream &OS,
DiagnosticsEngine::Level Level,
bool ShowColors) {
if (ShowColors) {
switch (Level) {
case DiagnosticsEngine::Ignored:
llvm_unreachable("Invalid diagnostic type");
case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break;
case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break;
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");
case DiagnosticsEngine::Note: OS << "note: "; break;
case DiagnosticsEngine::Remark: OS << "remark: "; break;
case DiagnosticsEngine::Warning: OS << "warning: "; break;
case DiagnosticsEngine::Error: OS << "error: "; break;
case DiagnosticsEngine::Fatal: OS << "fatal error: "; break;
}
if (ShowColors)
OS.resetColor();
}
void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS,
bool IsSupplemental,
StringRef Message,
unsigned CurrentColumn,
unsigned Columns, bool ShowColors) {
bool Bold = false;
if (ShowColors && !IsSupplemental) {
OS.changeColor(savedColor, true);
Bold = true;
}
if (Columns)
printWordWrapped(OS, Message, Columns, CurrentColumn, Bold);
else {
bool Normal = true;
applyTemplateHighlighting(OS, Message, Normal, Bold);
assert(Normal && "Formatting should have returned to normal");
}
if (ShowColors)
OS.resetColor();
OS << '\n';
}
void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) {
#ifdef _WIN32
SmallString<4096> TmpFilename;
#endif
if (DiagOpts->AbsolutePath) {
auto File = SM.getFileManager().getOptionalFileRef(Filename);
if (File) {
#ifdef _WIN32
TmpFilename = File->getName();
llvm::sys::fs::make_absolute(TmpFilename);
llvm::sys::path::native(TmpFilename);
llvm::sys::path::remove_dots(TmpFilename, true);
Filename = StringRef(TmpFilename.data(), TmpFilename.size());
#else
Filename = SM.getFileManager().getCanonicalName(*File);
#endif
}
}
OS << Filename;
}
void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc,
DiagnosticsEngine::Level Level,
ArrayRef<CharSourceRange> Ranges) {
if (PLoc.isInvalid()) {
if (FileID FID = Loc.getFileID(); FID.isValid()) {
if (OptionalFileEntryRef FE = Loc.getFileEntryRef()) {
emitFilename(FE->getName(), Loc.getManager());
OS << ": ";
}
}
return;
}
unsigned LineNo = PLoc.getLine();
if (!DiagOpts->ShowLocation)
return;
if (DiagOpts->ShowColors)
OS.changeColor(savedColor, true);
emitFilename(PLoc.getFilename(), Loc.getManager());
switch (DiagOpts->getFormat()) {
case DiagnosticOptions::SARIF:
case DiagnosticOptions::Clang:
if (DiagOpts->ShowLine)
OS << ':' << LineNo;
break;
case DiagnosticOptions::MSVC: OS << '(' << LineNo; break;
case DiagnosticOptions::Vi: OS << " +" << LineNo; break;
}
if (DiagOpts->ShowColumn)
if (unsigned ColNo = PLoc.getColumn()) {
if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) {
OS << ',';
if (LangOpts.MSCompatibilityVersion &&
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012))
ColNo--;
} else
OS << ':';
OS << ColNo;
}
switch (DiagOpts->getFormat()) {
case DiagnosticOptions::SARIF:
case DiagnosticOptions::Clang:
case DiagnosticOptions::Vi: OS << ':'; break;
case DiagnosticOptions::MSVC:
OS << ')';
if (LangOpts.MSCompatibilityVersion &&
!LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
OS << ' ';
OS << ':';
break;
}
if (DiagOpts->ShowSourceRanges && !Ranges.empty()) {
FileID CaretFileID = Loc.getExpansionLoc().getFileID();
bool PrintedRange = false;
const SourceManager &SM = Loc.getManager();
for (const auto &R : Ranges) {
if (!R.isValid())
continue;
SourceLocation B = SM.getExpansionLoc(R.getBegin());
CharSourceRange ERange = SM.getExpansionRange(R.getEnd());
SourceLocation E = ERange.getEnd();
if (SM.getFileID(B) != CaretFileID || SM.getFileID(E) != CaretFileID)
continue;
unsigned TokSize = 0;
if (ERange.isTokenRange())
TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts);
FullSourceLoc BF(B, SM), EF(E, SM);
OS << '{'
<< BF.getLineNumber() << ':' << BF.getColumnNumber() << '-'
<< EF.getLineNumber() << ':' << (EF.getColumnNumber() + TokSize)
<< '}';
PrintedRange = true;
}
if (PrintedRange)
OS << ':';
}
OS << ' ';
}
void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) {
if (DiagOpts->ShowLocation && PLoc.isValid()) {
OS << "In file included from ";
emitFilename(PLoc.getFilename(), Loc.getManager());
OS << ':' << PLoc.getLine() << ":\n";
} else
OS << "In included file:\n";
}
void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc,
StringRef ModuleName) {
if (DiagOpts->ShowLocation && PLoc.isValid())
OS << "In module '" << ModuleName << "' imported from "
<< PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
else
OS << "In module '" << ModuleName << "':\n";
}
void TextDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc,
PresumedLoc PLoc,
StringRef ModuleName) {
if (DiagOpts->ShowLocation && PLoc.isValid())
OS << "While building module '" << ModuleName << "' imported from "
<< PLoc.getFilename() << ':' << PLoc.getLine() << ":\n";
else
OS << "While building module '" << ModuleName << "':\n";
}
static std::optional<std::pair<unsigned, unsigned>>
findLinesForRange(const CharSourceRange &R, FileID FID,
const SourceManager &SM) {
if (!R.isValid())
return std::nullopt;
SourceLocation Begin = R.getBegin();
SourceLocation End = R.getEnd();
if (SM.getFileID(Begin) != FID || SM.getFileID(End) != FID)
return std::nullopt;
return std::make_pair(SM.getExpansionLineNumber(Begin),
SM.getExpansionLineNumber(End));
}
static std::pair<unsigned, unsigned>
maybeAddRange(std::pair<unsigned, unsigned> A, std::pair<unsigned, unsigned> B,
unsigned MaxRange) {
unsigned Slack = MaxRange - (A.second - A.first + 1);
if (Slack == 0)
return A;
unsigned Min = std::min(A.first, B.first);
unsigned Max = std::max(A.second, B.second);
if (Max - Min + 1 <= MaxRange)
return {Min, Max};
if ((B.first > A.first && B.first - A.first + 1 > MaxRange) ||
(B.second < A.second && A.second - B.second + 1 > MaxRange))
return A;
A.second = std::min(A.second + (Slack + 1) / 2, Max);
Slack = MaxRange - (A.second - A.first + 1);
A.first = std::max(Min + Slack, A.first) - Slack;
A.second = std::min(A.first + MaxRange - 1, Max);
return A;
}
struct LineRange {
unsigned LineNo;
unsigned StartCol;
unsigned EndCol;
};
static void highlightRange(const LineRange &R, const SourceColumnMap &Map,
std::string &CaretLine) {
unsigned StartColNo = R.StartCol;
while (StartColNo < Map.getSourceLine().size() &&
(Map.getSourceLine()[StartColNo] == ' ' ||
Map.getSourceLine()[StartColNo] == '\t'))
StartColNo = Map.startOfNextColumn(StartColNo);
unsigned EndColNo =
std::min(static_cast<size_t>(R.EndCol), Map.getSourceLine().size());
while (EndColNo && (Map.getSourceLine()[EndColNo - 1] == ' ' ||
Map.getSourceLine()[EndColNo - 1] == '\t'))
EndColNo = Map.startOfPreviousColumn(EndColNo);
if (StartColNo > EndColNo)
return;
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(FileID FID,
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 (const auto &H : Hints) {
if (H.CodeToInsert.empty())
continue;
std::pair<FileID, unsigned> HintLocInfo =
SM.getDecomposedExpansionLoc(H.RemoveRange.getBegin());
if (FID == HintLocInfo.first &&
LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) &&
StringRef(H.CodeToInsert).find_first_of("\n\r") == StringRef::npos) {
unsigned HintByteOffset =
SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1;
assert(HintByteOffset < static_cast<unsigned>(map.bytes()) + 1);
unsigned HintCol = map.byteToContainingColumn(HintByteOffset);
if (HintCol < PrevHintEndCol)
HintCol = PrevHintEndCol + 1;
unsigned NewFixItLineSize = FixItInsertionLine.size() +
(HintCol - PrevHintEndCol) +
H.CodeToInsert.size();
if (NewFixItLineSize > FixItInsertionLine.size())
FixItInsertionLine.resize(NewFixItLineSize, ' ');
std::copy(H.CodeToInsert.begin(), H.CodeToInsert.end(),
FixItInsertionLine.end() - H.CodeToInsert.size());
PrevHintEndCol = HintCol + llvm::sys::locale::columnWidth(H.CodeToInsert);
}
}
expandTabs(FixItInsertionLine, DiagOpts->TabStop);
return FixItInsertionLine;
}
static unsigned getNumDisplayWidth(unsigned N) {
unsigned L = 1u, M = 10u;
while (M <= N && ++L != std::numeric_limits<unsigned>::digits10 + 1)
M *= 10u;
return L;
}
static SmallVector<LineRange>
prepareAndFilterRanges(const SmallVectorImpl<CharSourceRange> &Ranges,
const SourceManager &SM,
const std::pair<unsigned, unsigned> &Lines, FileID FID,
const LangOptions &LangOpts) {
SmallVector<LineRange> LineRanges;
for (const CharSourceRange &R : Ranges) {
if (R.isInvalid())
continue;
SourceLocation Begin = R.getBegin();
SourceLocation End = R.getEnd();
unsigned StartLineNo = SM.getExpansionLineNumber(Begin);
if (StartLineNo > Lines.second || SM.getFileID(Begin) != FID)
continue;
unsigned EndLineNo = SM.getExpansionLineNumber(End);
if (EndLineNo < Lines.first || SM.getFileID(End) != FID)
continue;
unsigned StartColumn = SM.getExpansionColumnNumber(Begin);
unsigned EndColumn = SM.getExpansionColumnNumber(End);
if (R.isTokenRange())
EndColumn += Lexer::MeasureTokenLength(End, SM, LangOpts);
if (StartLineNo == EndLineNo) {
LineRanges.push_back({StartLineNo, StartColumn - 1, EndColumn - 1});
continue;
}
LineRanges.push_back({StartLineNo, StartColumn - 1, ~0u});
for (unsigned S = StartLineNo + 1; S != EndLineNo; ++S)
LineRanges.push_back({S, 0, ~0u});
LineRanges.push_back({EndLineNo, 0, EndColumn - 1});
}
return LineRanges;
}
static std::unique_ptr<llvm::SmallVector<TextDiagnostic::StyleRange>[]>
highlightLines(StringRef FileData, unsigned StartLineNumber,
unsigned EndLineNumber, const Preprocessor *PP,
const LangOptions &LangOpts, bool ShowColors, FileID FID,
const SourceManager &SM) {
assert(StartLineNumber <= EndLineNumber);
auto SnippetRanges =
std::make_unique<SmallVector<TextDiagnostic::StyleRange>[]>(
EndLineNumber - StartLineNumber + 1);
if (!PP || !ShowColors)
return SnippetRanges;
if (PP->getIdentifierTable().getExternalIdentifierLookup())
return SnippetRanges;
auto Buff = llvm::MemoryBuffer::getMemBuffer(FileData);
Lexer L{FID, *Buff, SM, LangOpts};
L.SetKeepWhitespaceMode(true);
const char *FirstLineStart =
FileData.data() +
SM.getDecomposedLoc(SM.translateLineCol(FID, StartLineNumber, 1)).second;
if (const char *CheckPoint = PP->getCheckPoint(FID, FirstLineStart)) {
assert(CheckPoint >= Buff->getBufferStart() &&
CheckPoint <= Buff->getBufferEnd());
assert(CheckPoint <= FirstLineStart);
size_t Offset = CheckPoint - Buff->getBufferStart();
L.seek(Offset, false);
}
auto appendStyle =
[PP, &LangOpts](SmallVector<TextDiagnostic::StyleRange> &Vec,
const Token &T, unsigned Start, unsigned Length) -> void {
if (T.is(tok::raw_identifier)) {
StringRef RawIdent = T.getRawIdentifier();
if (llvm::StringSwitch<bool>(RawIdent)
.Case("true", true)
.Case("false", true)
.Case("nullptr", true)
.Case("__func__", true)
.Case("__objc_yes__", true)
.Case("__objc_no__", true)
.Case("__null", true)
.Case("__FUNCDNAME__", true)
.Case("__FUNCSIG__", true)
.Case("__FUNCTION__", true)
.Case("__FUNCSIG__", true)
.Default(false)) {
Vec.emplace_back(Start, Start + Length, LiteralColor);
} else {
const IdentifierInfo *II = PP->getIdentifierInfo(RawIdent);
assert(II);
if (II->isKeyword(LangOpts))
Vec.emplace_back(Start, Start + Length, KeywordColor);
}
} else if (tok::isLiteral(T.getKind())) {
Vec.emplace_back(Start, Start + Length, LiteralColor);
} else {
assert(T.is(tok::comment));
Vec.emplace_back(Start, Start + Length, CommentColor);
}
};
bool Stop = false;
while (!Stop) {
Token T;
Stop = L.LexFromRawLexer(T);
if (T.is(tok::unknown))
continue;
if (!T.is(tok::raw_identifier) && !T.is(tok::comment) &&
!tok::isLiteral(T.getKind()))
continue;
bool Invalid = false;
unsigned TokenEndLine = SM.getSpellingLineNumber(T.getEndLoc(), &Invalid);
if (Invalid || TokenEndLine < StartLineNumber)
continue;
assert(TokenEndLine >= StartLineNumber);
unsigned TokenStartLine =
SM.getSpellingLineNumber(T.getLocation(), &Invalid);
if (Invalid)
continue;
if (TokenStartLine > EndLineNumber)
break;
unsigned StartCol =
SM.getSpellingColumnNumber(T.getLocation(), &Invalid) - 1;
if (Invalid)
continue;
if (TokenStartLine == TokenEndLine) {
SmallVector<TextDiagnostic::StyleRange> &LineRanges =
SnippetRanges[TokenStartLine - StartLineNumber];
appendStyle(LineRanges, T, StartCol, T.getLength());
continue;
}
assert((TokenEndLine - TokenStartLine) >= 1);
unsigned EndCol = SM.getSpellingColumnNumber(T.getEndLoc(), &Invalid) - 1;
if (Invalid)
continue;
std::string Spelling = Lexer::getSpelling(T, SM, LangOpts);
unsigned L = TokenStartLine;
unsigned LineLength = 0;
for (unsigned I = 0; I <= Spelling.size(); ++I) {
if (I == Spelling.size() || isVerticalWhitespace(Spelling[I])) {
SmallVector<TextDiagnostic::StyleRange> &LineRanges =
SnippetRanges[L - StartLineNumber];
if (L >= StartLineNumber) {
if (L == TokenStartLine)
appendStyle(LineRanges, T, StartCol, LineLength);
else if (L == TokenEndLine)
appendStyle(LineRanges, T, 0, EndCol);
else
appendStyle(LineRanges, T, 0, LineLength);
}
++L;
if (L > EndLineNumber)
break;
LineLength = 0;
continue;
}
++LineLength;
}
}
return SnippetRanges;
}
void TextDiagnostic::emitSnippetAndCaret(
FullSourceLoc Loc, DiagnosticsEngine::Level Level,
SmallVectorImpl<CharSourceRange> &Ranges, ArrayRef<FixItHint> Hints) {
assert(Loc.isValid() && "must have a valid source location here");
assert(Loc.isFileID() && "must have a file location here");
if (!DiagOpts->ShowCarets)
return;
if (Loc == LastLoc && Ranges.empty() && Hints.empty() &&
(LastLevel != DiagnosticsEngine::Note || Level == LastLevel))
return;
FileID FID = Loc.getFileID();
const SourceManager &SM = Loc.getManager();
bool Invalid = false;
StringRef BufData = Loc.getBufferData(&Invalid);
if (Invalid)
return;
const char *BufStart = BufData.data();
const char *BufEnd = BufStart + BufData.size();
unsigned CaretLineNo = Loc.getLineNumber();
unsigned CaretColNo = Loc.getColumnNumber();
static const size_t MaxLineLengthToPrint = 4096;
if (CaretColNo > MaxLineLengthToPrint)
return;
const unsigned MaxLines = DiagOpts->SnippetLineLimit;
std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
for (const auto &I : Ranges) {
if (auto OptionalRange = findLinesForRange(I, FID, SM))
Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);
DisplayLineNo =
std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin()));
}
unsigned MaxLineNoDisplayWidth =
DiagOpts->ShowLineNumbers
? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines))
: 0;
auto indentForLineNumbers = [&] {
if (MaxLineNoDisplayWidth > 0)
OS.indent(MaxLineNoDisplayWidth + 2) << "| ";
};
std::unique_ptr<SmallVector<StyleRange>[]> SourceStyles =
highlightLines(BufData, Lines.first, Lines.second, PP, LangOpts,
DiagOpts->ShowColors, FID, SM);
SmallVector<LineRange> LineRanges =
prepareAndFilterRanges(Ranges, SM, Lines, FID, LangOpts);
for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1;
++LineNo, ++DisplayLineNo) {
const char *LineStart =
BufStart +
SM.getDecomposedLoc(SM.translateLineCol(FID, LineNo, 1)).second;
if (LineStart == BufEnd)
break;
const char *LineEnd = LineStart;
while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd)
++LineEnd;
if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint)
return;
std::string SourceLine(LineStart, LineEnd);
while (!SourceLine.empty() && SourceLine.back() == '\0' &&
(LineNo != CaretLineNo || SourceLine.size() > CaretColNo))
SourceLine.pop_back();
const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop);
std::string CaretLine;
for (const auto &LR : LineRanges) {
if (LR.LineNo == LineNo)
highlightRange(LR, sourceColMap, CaretLine);
}
if (CaretLineNo == LineNo) {
size_t Col = sourceColMap.byteToContainingColumn(CaretColNo - 1);
CaretLine.resize(std::max(Col + 1, CaretLine.size()), ' ');
CaretLine[Col] = '^';
}
std::string FixItInsertionLine = buildFixItInsertionLine(
FID, LineNo, sourceColMap, Hints, SM, DiagOpts.get());
unsigned Columns = DiagOpts->MessageLength;
if (Columns)
selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine,
Columns, sourceColMap);
if (DiagOpts->ShowSourceRanges && !SourceLine.empty()) {
SourceLine = ' ' + SourceLine;
CaretLine = ' ' + CaretLine;
}
emitSnippet(SourceLine, MaxLineNoDisplayWidth, LineNo, DisplayLineNo,
SourceStyles[LineNo - Lines.first]);
if (!CaretLine.empty()) {
indentForLineNumbers();
if (DiagOpts->ShowColors)
OS.changeColor(caretColor, true);
OS << CaretLine << '\n';
if (DiagOpts->ShowColors)
OS.resetColor();
}
if (!FixItInsertionLine.empty()) {
indentForLineNumbers();
if (DiagOpts->ShowColors)
OS.changeColor(fixitColor, false);
if (DiagOpts->ShowSourceRanges)
OS << ' ';
OS << FixItInsertionLine << '\n';
if (DiagOpts->ShowColors)
OS.resetColor();
}
}
emitParseableFixits(Hints, SM);
}
void TextDiagnostic::emitSnippet(StringRef SourceLine,
unsigned MaxLineNoDisplayWidth,
unsigned LineNo, unsigned DisplayLineNo,
ArrayRef<StyleRange> Styles) {
if (MaxLineNoDisplayWidth > 0) {
unsigned LineNoDisplayWidth = getNumDisplayWidth(DisplayLineNo);
OS.indent(MaxLineNoDisplayWidth - LineNoDisplayWidth + 1)
<< DisplayLineNo << " | ";
}
bool PrintReversed = false;
std::optional<llvm::raw_ostream::Colors> CurrentColor;
size_t I = 0;
while (I < SourceLine.size()) {
auto [Str, WasPrintable] =
printableTextForNextCharacter(SourceLine, &I, DiagOpts->TabStop);
if (DiagOpts->ShowColors) {
if (WasPrintable == PrintReversed) {
PrintReversed = !PrintReversed;
if (PrintReversed)
OS.reverseColor();
else {
OS.resetColor();
CurrentColor = std::nullopt;
}
}
const auto *CharStyle = llvm::find_if(Styles, [I](const StyleRange &R) {
return (R.Start < I && R.End >= I);
});
if (CharStyle != Styles.end()) {
if (!CurrentColor ||
(CurrentColor && *CurrentColor != CharStyle->Color)) {
OS.changeColor(CharStyle->Color, false);
CurrentColor = CharStyle->Color;
}
} else if (CurrentColor) {
OS.resetColor();
CurrentColor = std::nullopt;
}
}
OS << Str;
}
if (DiagOpts->ShowColors)
OS.resetColor();
OS << '\n';
}
void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints,
const SourceManager &SM) {
if (!DiagOpts->ShowParseableFixits)
return;
for (const auto &H : Hints) {
if (H.RemoveRange.isInvalid() || H.RemoveRange.getBegin().isMacroID() ||
H.RemoveRange.getEnd().isMacroID())
return;
}
for (const auto &H : Hints) {
SourceLocation BLoc = H.RemoveRange.getBegin();
SourceLocation ELoc = H.RemoveRange.getEnd();
std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc);
std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc);
if (H.RemoveRange.isTokenRange())
EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts);
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(H.CodeToInsert);
OS << "\"\n";
}
}