[Sanitizer] Merge AnsiColorDecorator and SanitizerCommonDecorator, use the latter in UBSan

llvm-svn: 210959
This commit is contained in:
Alexey Samsonov 2014-06-13 23:46:37 +00:00
parent 4ad03dc355
commit e287ef847a
2 changed files with 23 additions and 18 deletions

View File

@ -20,12 +20,16 @@
#include "sanitizer_common.h"
namespace __sanitizer {
class AnsiColorDecorator {
class SanitizerCommonDecorator {
// FIXME: This is not portable. It assumes the special strings are printed to
// stdout, which is not the case on Windows (see SetConsoleTextAttribute()).
public:
explicit AnsiColorDecorator(bool use_ansi_colors) : ansi_(use_ansi_colors) { }
SanitizerCommonDecorator() : ansi_(ColorizeReports()) {}
const char *Bold() const { return ansi_ ? "\033[1m" : ""; }
const char *Default() const { return ansi_ ? "\033[1m\033[0m" : ""; }
const char *Warning() { return Red(); }
const char *EndWarning() { return Default(); }
protected:
const char *Black() const { return ansi_ ? "\033[1m\033[30m" : ""; }
const char *Red() const { return ansi_ ? "\033[1m\033[31m" : ""; }
const char *Green() const { return ansi_ ? "\033[1m\033[32m" : ""; }
@ -34,19 +38,10 @@ class AnsiColorDecorator {
const char *Magenta() const { return ansi_ ? "\033[1m\033[35m" : ""; }
const char *Cyan() const { return ansi_ ? "\033[1m\033[36m" : ""; }
const char *White() const { return ansi_ ? "\033[1m\033[37m" : ""; }
const char *Default() const { return ansi_ ? "\033[1m\033[0m" : ""; }
private:
bool ansi_;
};
class SanitizerCommonDecorator: protected AnsiColorDecorator {
public:
SanitizerCommonDecorator()
: __sanitizer::AnsiColorDecorator(ColorizeReports()) { }
const char *Warning() { return Red(); }
const char *EndWarning() { return Default(); }
};
} // namespace __sanitizer
#endif // SANITIZER_REPORT_DECORATOR_H

View File

@ -38,6 +38,17 @@ static void InitializeSanitizerCommon() {
initialized = true;
}
namespace {
class Decorator : public SanitizerCommonDecorator {
public:
Decorator() : SanitizerCommonDecorator() {}
const char *Highlight() const { return Green(); }
const char *EndHighlight() const { return Default(); }
const char *Note() const { return Black(); }
const char *EndNote() const { return Default(); }
};
}
Location __ubsan::getCallerLocation(uptr CallerLoc) {
if (!CallerLoc)
return Location();
@ -183,8 +194,7 @@ static Range *upperBound(MemoryLocation Loc, Range *Ranges,
}
/// Render a snippet of the address space near a location.
static void renderMemorySnippet(const __sanitizer::AnsiColorDecorator &Decor,
MemoryLocation Loc,
static void renderMemorySnippet(const Decorator &Decor, MemoryLocation Loc,
Range *Ranges, unsigned NumRanges,
const Diag::Arg *Args) {
const unsigned BytesToShow = 32;
@ -211,7 +221,7 @@ static void renderMemorySnippet(const __sanitizer::AnsiColorDecorator &Decor,
Printf("\n");
// Emit highlights.
Printf(Decor.Green());
Printf(Decor.Highlight());
Range *InRange = upperBound(Min, Ranges, NumRanges);
for (uptr P = Min; P != Max; ++P) {
char Pad = ' ', Byte = ' ';
@ -226,7 +236,7 @@ static void renderMemorySnippet(const __sanitizer::AnsiColorDecorator &Decor,
char Buffer[] = { Pad, Pad, P == Loc ? '^' : Byte, Byte, 0 };
Printf((P % 8 == 0) ? Buffer : &Buffer[1]);
}
Printf("%s\n", Decor.Default());
Printf("%s\n", Decor.EndHighlight());
// Go over the line again, and print names for the ranges.
InRange = 0;
@ -265,7 +275,7 @@ static void renderMemorySnippet(const __sanitizer::AnsiColorDecorator &Decor,
Diag::~Diag() {
InitializeSanitizerCommon();
__sanitizer::AnsiColorDecorator Decor(ColorizeReports());
Decorator Decor;
SpinMutexLock l(&CommonSanitizerReportMutex);
Printf(Decor.Bold());
@ -274,11 +284,11 @@ Diag::~Diag() {
switch (Level) {
case DL_Error:
Printf("%s runtime error: %s%s",
Decor.Red(), Decor.Default(), Decor.Bold());
Decor.Warning(), Decor.EndWarning(), Decor.Bold());
break;
case DL_Note:
Printf("%s note: %s", Decor.Black(), Decor.Default());
Printf("%s note: %s", Decor.Note(), Decor.EndNote());
break;
}