[compiler-rt] r210959 - [Sanitizer] Merge AnsiColorDecorator and SanitizerCommonDecorator, use the latter in UBSan

Alexey Samsonov vonosmas at gmail.com
Fri Jun 13 16:46:38 PDT 2014


Author: samsonov
Date: Fri Jun 13 18:46:37 2014
New Revision: 210959

URL: http://llvm.org/viewvc/llvm-project?rev=210959&view=rev
Log:
[Sanitizer] Merge AnsiColorDecorator and SanitizerCommonDecorator, use the latter in UBSan

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_report_decorator.h
    compiler-rt/trunk/lib/ubsan/ubsan_diag.cc

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_report_decorator.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_report_decorator.h?rev=210959&r1=210958&r2=210959&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_report_decorator.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_report_decorator.h Fri Jun 13 18:46:37 2014
@@ -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

Modified: compiler-rt/trunk/lib/ubsan/ubsan_diag.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_diag.cc?rev=210959&r1=210958&r2=210959&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_diag.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_diag.cc Fri Jun 13 18:46:37 2014
@@ -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
 }
 
 /// 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 __
   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 __
     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 __
 
 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;
   }
 





More information about the llvm-commits mailing list