diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2019-01-19 10:01:25 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2019-01-19 10:01:25 +0000 |
commit | d8e91e46262bc44006913e6796843909f1ac7bcd (patch) | |
tree | 7d0c143d9b38190e0fa0180805389da22cd834c5 /lib/Support/SourceMgr.cpp | |
parent | b7eb8e35e481a74962664b63dfb09483b200209a (diff) |
Notes
Diffstat (limited to 'lib/Support/SourceMgr.cpp')
-rw-r--r-- | lib/Support/SourceMgr.cpp | 124 |
1 files changed, 56 insertions, 68 deletions
diff --git a/lib/Support/SourceMgr.cpp b/lib/Support/SourceMgr.cpp index d8fde7fa8990..a55ad881d012 100644 --- a/lib/Support/SourceMgr.cpp +++ b/lib/Support/SourceMgr.cpp @@ -24,6 +24,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/SMLoc.h" +#include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" #include <algorithm> #include <cassert> @@ -269,7 +270,7 @@ SMDiagnostic::SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, : SM(&sm), Loc(L), Filename(FN), LineNo(Line), ColumnNo(Col), Kind(Kind), Message(Msg), LineContents(LineStr), Ranges(Ranges.vec()), FixIts(Hints.begin(), Hints.end()) { - llvm::sort(FixIts.begin(), FixIts.end()); + llvm::sort(FixIts); } static void buildFixItLine(std::string &CaretLine, std::string &FixItLine, @@ -345,12 +346,18 @@ static void buildFixItLine(std::string &CaretLine, std::string &FixItLine, static void printSourceLine(raw_ostream &S, StringRef LineContents) { // Print out the source line one character at a time, so we can expand tabs. for (unsigned i = 0, e = LineContents.size(), OutCol = 0; i != e; ++i) { - if (LineContents[i] != '\t') { - S << LineContents[i]; - ++OutCol; - continue; + size_t NextTab = LineContents.find('\t', i); + // If there were no tabs left, print the rest, we are done. + if (NextTab == StringRef::npos) { + S << LineContents.drop_front(i); + break; } + // Otherwise, print from i to NextTab. + S << LineContents.slice(i, NextTab); + OutCol += NextTab - i; + i = NextTab; + // If we have a tab, emit at least one space, then round up to 8 columns. do { S << ' '; @@ -364,65 +371,48 @@ static bool isNonASCII(char c) { return c & 0x80; } -void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, - bool ShowKindLabel) const { - // Display colors only if OS supports colors. - ShowColors &= S.has_colors(); - - if (ShowColors) - S.changeColor(raw_ostream::SAVEDCOLOR, true); +void SMDiagnostic::print(const char *ProgName, raw_ostream &OS, + bool ShowColors, bool ShowKindLabel) const { + { + WithColor S(OS, raw_ostream::SAVEDCOLOR, true, false, !ShowColors); - if (ProgName && ProgName[0]) - S << ProgName << ": "; + if (ProgName && ProgName[0]) + S << ProgName << ": "; - if (!Filename.empty()) { - if (Filename == "-") - S << "<stdin>"; - else - S << Filename; + if (!Filename.empty()) { + if (Filename == "-") + S << "<stdin>"; + else + S << Filename; - if (LineNo != -1) { - S << ':' << LineNo; - if (ColumnNo != -1) - S << ':' << (ColumnNo+1); + if (LineNo != -1) { + S << ':' << LineNo; + if (ColumnNo != -1) + S << ':' << (ColumnNo + 1); + } + S << ": "; } - S << ": "; } if (ShowKindLabel) { switch (Kind) { case SourceMgr::DK_Error: - if (ShowColors) - S.changeColor(raw_ostream::RED, true); - S << "error: "; + WithColor::error(OS, "", !ShowColors); break; case SourceMgr::DK_Warning: - if (ShowColors) - S.changeColor(raw_ostream::MAGENTA, true); - S << "warning: "; + WithColor::warning(OS, "", !ShowColors); break; case SourceMgr::DK_Note: - if (ShowColors) - S.changeColor(raw_ostream::BLACK, true); - S << "note: "; + WithColor::note(OS, "", !ShowColors); break; case SourceMgr::DK_Remark: - if (ShowColors) - S.changeColor(raw_ostream::BLUE, true); - S << "remark: "; + WithColor::remark(OS, "", !ShowColors); break; } - - if (ShowColors) { - S.resetColor(); - S.changeColor(raw_ostream::SAVEDCOLOR, true); - } } - S << Message << '\n'; - - if (ShowColors) - S.resetColor(); + WithColor(OS, raw_ostream::SAVEDCOLOR, true, false, !ShowColors) + << Message << '\n'; if (LineNo == -1 || ColumnNo == -1) return; @@ -433,7 +423,7 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, // expanding them later, and bail out rather than show incorrect ranges and // misaligned fixits for any other odd characters. if (find_if(LineContents, isNonASCII) != LineContents.end()) { - printSourceLine(S, LineContents); + printSourceLine(OS, LineContents); return; } size_t NumColumns = LineContents.size(); @@ -467,29 +457,27 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, // least. CaretLine.erase(CaretLine.find_last_not_of(' ')+1); - printSourceLine(S, LineContents); + printSourceLine(OS, LineContents); - if (ShowColors) - S.changeColor(raw_ostream::GREEN, true); + { + WithColor S(OS, raw_ostream::GREEN, true, false, !ShowColors); - // Print out the caret line, matching tabs in the source line. - for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) { - if (i >= LineContents.size() || LineContents[i] != '\t') { - S << CaretLine[i]; - ++OutCol; - continue; - } + // Print out the caret line, matching tabs in the source line. + for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) { + if (i >= LineContents.size() || LineContents[i] != '\t') { + S << CaretLine[i]; + ++OutCol; + continue; + } - // Okay, we have a tab. Insert the appropriate number of characters. - do { - S << CaretLine[i]; - ++OutCol; - } while ((OutCol % TabStop) != 0); + // Okay, we have a tab. Insert the appropriate number of characters. + do { + S << CaretLine[i]; + ++OutCol; + } while ((OutCol % TabStop) != 0); + } + S << '\n'; } - S << '\n'; - - if (ShowColors) - S.resetColor(); // Print out the replacement line, matching tabs in the source line. if (FixItInsertionLine.empty()) @@ -497,14 +485,14 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, for (size_t i = 0, e = FixItInsertionLine.size(), OutCol = 0; i < e; ++i) { if (i >= LineContents.size() || LineContents[i] != '\t') { - S << FixItInsertionLine[i]; + OS << FixItInsertionLine[i]; ++OutCol; continue; } // Okay, we have a tab. Insert the appropriate number of characters. do { - S << FixItInsertionLine[i]; + OS << FixItInsertionLine[i]; // FIXME: This is trying not to break up replacements, but then to re-sync // with the tabs between replacements. This will fail, though, if two // fix-it replacements are exactly adjacent, or if a fix-it contains a @@ -515,5 +503,5 @@ void SMDiagnostic::print(const char *ProgName, raw_ostream &S, bool ShowColors, ++OutCol; } while (((OutCol % TabStop) != 0) && i != e); } - S << '\n'; + OS << '\n'; } |