[cfe-commits] r72855 - in /cfe/trunk: include/clang/Frontend/TextDiagnosticPrinter.h lib/Frontend/TextDiagnosticPrinter.cpp tools/clang-cc/clang-cc.cpp
Torok Edwin
edwintorok at gmail.com
Thu Jun 4 00:18:23 PDT 2009
Author: edwin
Date: Thu Jun 4 02:18:23 2009
New Revision: 72855
URL: http://llvm.org/viewvc/llvm-project?rev=72855&view=rev
Log:
Add ANSI color support for clang.
Modified:
cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
cfe/trunk/tools/clang-cc/clang-cc.cpp
Modified: cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h?rev=72855&r1=72854&r2=72855&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h (original)
+++ cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h Thu Jun 4 02:18:23 2009
@@ -40,6 +40,7 @@
bool PrintDiagnosticOption;
bool PrintFixItInfo;
unsigned MessageLength;
+ bool UseColors;
public:
TextDiagnosticPrinter(llvm::raw_ostream &os,
@@ -48,14 +49,16 @@
bool printRangeInfo = true,
bool printDiagnosticOption = true,
bool printFixItInfo = true,
- unsigned messageLength = 0)
+ unsigned messageLength = 0,
+ bool useColors = false)
: OS(os), LangOpts(0),
LastCaretDiagnosticWasNote(false), ShowColumn(showColumn),
CaretDiagnostics(caretDiagnistics), ShowLocation(showLocation),
PrintRangeInfo(printRangeInfo),
PrintDiagnosticOption(printDiagnosticOption),
PrintFixItInfo(printFixItInfo),
- MessageLength(messageLength) {}
+ MessageLength(messageLength),
+ UseColors(useColors) {}
void setLangOptions(const LangOptions *LO) {
LangOpts = LO;
Modified: cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp?rev=72855&r1=72854&r2=72855&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp Thu Jun 4 02:18:23 2009
@@ -20,6 +20,20 @@
#include <algorithm>
using namespace clang;
+static const enum llvm::raw_ostream::Colors noteColor =
+ llvm::raw_ostream::BLACK;
+static const enum llvm::raw_ostream::Colors fixitColor =
+ llvm::raw_ostream::GREEN;
+static const enum llvm::raw_ostream::Colors caretColor =
+ llvm::raw_ostream::GREEN;
+static const enum llvm::raw_ostream::Colors warningColor =
+ llvm::raw_ostream::MAGENTA;
+static const enum llvm::raw_ostream::Colors errorColor = llvm::raw_ostream::RED;
+static const enum llvm::raw_ostream::Colors fatalColor = llvm::raw_ostream::RED;
+// used for changing only the bold attribute
+static const enum llvm::raw_ostream::Colors savedColor =
+ llvm::raw_ostream::SAVEDCOLOR;
+
/// \brief Number of spaces to indent when word-wrapping.
const unsigned WordWrapIndentation = 6;
@@ -396,12 +410,22 @@
// Emit what we have computed.
OS << SourceLine << '\n';
+
+ if (UseColors)
+ OS.changeColor(caretColor, true);
OS << CaretLine << '\n';
+ if (UseColors)
+ OS.resetColor();
if (!FixItInsertionLine.empty()) {
+ if (UseColors)
+ // Print fixit line in color
+ OS.changeColor(fixitColor, false);
if (PrintRangeInfo)
OS << ' ';
OS << FixItInsertionLine << '\n';
+ if (UseColors)
+ OS.resetColor();
}
}
@@ -598,6 +622,8 @@
// Compute the column number.
if (ShowLocation) {
+ if (UseColors)
+ OS.changeColor(savedColor, true);
OS << PLoc.getFilename() << ':' << LineNo << ':';
if (ShowColumn)
if (unsigned ColNo = PLoc.getColumn())
@@ -638,6 +664,19 @@
OS << ':';
}
OS << ' ';
+ if (UseColors)
+ OS.resetColor();
+ }
+ }
+
+ if (UseColors) {
+ // Print diagnostic category in bold and color
+ switch (Level) {
+ case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
+ case Diagnostic::Note: OS.changeColor(noteColor, true); break;
+ case Diagnostic::Warning: OS.changeColor(warningColor, true); break;
+ case Diagnostic::Error: OS.changeColor(errorColor, true); break;
+ case Diagnostic::Fatal: OS.changeColor(fatalColor, true); break;
}
}
@@ -648,7 +687,10 @@
case Diagnostic::Error: OS << "error: "; break;
case Diagnostic::Fatal: OS << "fatal error: "; break;
}
-
+
+ if (UseColors)
+ OS.resetColor();
+
llvm::SmallString<100> OutStr;
Info.FormatDiagnostic(OutStr);
@@ -659,6 +701,16 @@
OutStr += ']';
}
+ if (UseColors) {
+ // Print warnings, errors and fatal errors in bold, no color
+ switch (Level) {
+ case Diagnostic::Warning: OS.changeColor(savedColor, true); break;
+ case Diagnostic::Error: OS.changeColor(savedColor, true); break;
+ case Diagnostic::Fatal: OS.changeColor(savedColor, true); break;
+ default: break; //don't bold notes
+ }
+ }
+
if (MessageLength) {
// We will be word-wrapping the error message, so compute the
// column number where we currently are (after printing the
@@ -669,6 +721,8 @@
OS.write(OutStr.begin(), OutStr.size());
}
OS << '\n';
+ if (UseColors)
+ OS.resetColor();
// If caret diagnostics are enabled and we have location, we want to
// emit the caret. However, we only do this if the location moved
Modified: cfe/trunk/tools/clang-cc/clang-cc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/clang-cc.cpp?rev=72855&r1=72854&r2=72855&view=diff
==============================================================================
--- cfe/trunk/tools/clang-cc/clang-cc.cpp (original)
+++ cfe/trunk/tools/clang-cc/clang-cc.cpp Thu Jun 4 02:18:23 2009
@@ -328,6 +328,11 @@
"within N columns or fewer, when possible."),
llvm::cl::value_desc("N"));
+static llvm::cl::opt<bool>
+NoColorDiagnostic("fno-color-diagnostic",
+ llvm::cl::desc("Don't use colors when showing diagnostics "
+ "(automatically turned off if output is not a "
+ "terminal)."));
//===----------------------------------------------------------------------===//
// C++ Visualization.
//===----------------------------------------------------------------------===//
@@ -2150,6 +2155,10 @@
if (MessageLength.getNumOccurrences() == 0)
MessageLength.setValue(llvm::sys::Process::StandardErrColumns());
+ if (!NoColorDiagnostic) {
+ NoColorDiagnostic.setValue(!llvm::sys::Process::StandardErrHasColors());
+ }
+
DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(),
!NoShowColumn,
!NoCaretDiagnostics,
@@ -2157,7 +2166,8 @@
PrintSourceRangeInfo,
PrintDiagnosticOption,
!NoDiagnosticsFixIt,
- MessageLength));
+ MessageLength,
+ !NoColorDiagnostic));
} else {
DiagClient.reset(CreateHTMLDiagnosticClient(HTMLDiag));
}
More information about the cfe-commits
mailing list