From 55c24f680c3d9d16327241211ec9f5a4782bb3fc Mon Sep 17 00:00:00 2001 From: Alexey Samsonov Date: Fri, 13 Jun 2014 23:46:37 +0000 Subject: [PATCH] [Sanitizer] Merge AnsiColorDecorator and SanitizerCommonDecorator, use the latter in UBSan git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@210959 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/sanitizer_common/sanitizer_report_decorator.h | 17 ++++++---------- lib/ubsan/ubsan_diag.cc | 24 ++++++++++++++++------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/sanitizer_common/sanitizer_report_decorator.h b/lib/sanitizer_common/sanitizer_report_decorator.h index 6e5b0ed32..86536aa19 100644 --- a/lib/sanitizer_common/sanitizer_report_decorator.h +++ b/lib/sanitizer_common/sanitizer_report_decorator.h @@ -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 diff --git a/lib/ubsan/ubsan_diag.cc b/lib/ubsan/ubsan_diag.cc index ba31f4caa..fb5cd4b0a 100644 --- a/lib/ubsan/ubsan_diag.cc +++ b/lib/ubsan/ubsan_diag.cc @@ -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; } -- 2.11.4.GIT