[llvm] r327108 - [Support] Move syntax highlighting into support

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 9 01:56:24 PST 2018


Author: jdevlieghere
Date: Fri Mar  9 01:56:24 2018
New Revision: 327108

URL: http://llvm.org/viewvc/llvm-project?rev=327108&view=rev
Log:
[Support] Move syntax highlighting into support

Move the DWARF syntax highlighting into support. This has several
advantages, most notably that this makes the WithColor RAII wrapper
available outside libDebugInfo. Furthermore, several projects all have
their own code for handling colored output. This provides a place to
centralize it.

Differential revision: https://reviews.llvm.org/D44215

Added:
    llvm/trunk/include/llvm/Support/WithColor.h
    llvm/trunk/lib/Support/WithColor.cpp
Removed:
    llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.cpp
    llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.h
Modified:
    llvm/trunk/lib/DebugInfo/DWARF/CMakeLists.txt
    llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp
    llvm/trunk/lib/DebugInfo/DWARF/DWARFDie.cpp
    llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp
    llvm/trunk/lib/DebugInfo/DWARF/DWARFVerifier.cpp
    llvm/trunk/lib/Support/CMakeLists.txt

Added: llvm/trunk/include/llvm/Support/WithColor.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/WithColor.h?rev=327108&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Support/WithColor.h (added)
+++ llvm/trunk/include/llvm/Support/WithColor.h Fri Mar  9 01:56:24 2018
@@ -0,0 +1,48 @@
+//===- WithColor.h ----------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_WITHCOLOR_H
+#define LLVM_SUPPORT_WITHCOLOR_H
+
+namespace llvm {
+
+class raw_ostream;
+
+// Symbolic names for various syntax elements.
+enum class HighlightColor {
+  Address,
+  String,
+  Tag,
+  Attribute,
+  Enumerator,
+  Macro,
+  Error,
+  Warning,
+  Note
+};
+
+/// An RAII object that temporarily switches an output stream to a specific
+/// color.
+class WithColor {
+  raw_ostream &OS;
+  /// Determine whether colors should be displayed.
+  bool colorsEnabled(raw_ostream &OS);
+
+public:
+  /// To be used like this: WithColor(OS, HighlightColor::String) << "text";
+  WithColor(raw_ostream &OS, HighlightColor S);
+  ~WithColor();
+
+  raw_ostream &get() { return OS; }
+  operator raw_ostream &() { return OS; }
+};
+
+} // end namespace llvm
+
+#endif // LLVM_LIB_DEBUGINFO_WITHCOLOR_H

Modified: llvm/trunk/lib/DebugInfo/DWARF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/CMakeLists.txt?rev=327108&r1=327107&r2=327108&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/CMakeLists.txt (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/CMakeLists.txt Fri Mar  9 01:56:24 2018
@@ -24,7 +24,6 @@ add_llvm_library(LLVMDebugInfoDWARF
   DWARFUnitIndex.cpp
   DWARFUnit.cpp
   DWARFVerifier.cpp
-  SyntaxHighlighting.cpp
 
   ADDITIONAL_HEADER_DIRS
   ${LLVM_MAIN_INCLUDE_DIR}/llvm/DebugInfo/DWARF

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp?rev=327108&r1=327107&r2=327108&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFDebugMacro.cpp Fri Mar  9 01:56:24 2018
@@ -8,14 +8,13 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
-#include "SyntaxHighlighting.h"
 #include "llvm/BinaryFormat/Dwarf.h"
+#include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cstdint>
 
 using namespace llvm;
 using namespace dwarf;
-using namespace syntax;
 
 void DWARFDebugMacro::dump(raw_ostream &OS) const {
   unsigned IndLevel = 0;
@@ -29,7 +28,7 @@ void DWARFDebugMacro::dump(raw_ostream &
       OS << "  ";
     IndLevel += (E.Type == DW_MACINFO_start_file);
 
-    WithColor(OS, syntax::Macro).get() << MacinfoString(E.Type);
+    WithColor(OS, HighlightColor::Macro).get() << MacinfoString(E.Type);
     switch (E.Type) {
     default:
       // Got a corrupted ".debug_macinfo" section (invalid macinfo type).

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFDie.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFDie.cpp?rev=327108&r1=327107&r2=327108&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFDie.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFDie.cpp Fri Mar  9 01:56:24 2018
@@ -8,7 +8,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
-#include "SyntaxHighlighting.h"
 #include "llvm/ADT/None.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/StringRef.h"
@@ -23,6 +22,7 @@
 #include "llvm/Support/DataExtractor.h"
 #include "llvm/Support/Format.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <cassert>
@@ -34,7 +34,6 @@
 using namespace llvm;
 using namespace dwarf;
 using namespace object;
-using namespace syntax;
 
 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
   OS << " (";
@@ -191,9 +190,10 @@ static void dumpAttribute(raw_ostream &O
   OS.indent(Indent + 2);
   auto attrString = AttributeString(Attr);
   if (!attrString.empty())
-    WithColor(OS, syntax::Attribute) << attrString;
+    WithColor(OS, HighlightColor::Attribute) << attrString;
   else
-    WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
+    WithColor(OS, HighlightColor::Attribute).get()
+        << format("DW_AT_Unknown_%x", Attr);
 
   if (DumpOpts.Verbose || DumpOpts.ShowForm) {
     auto formString = FormEncodingString(Form);
@@ -214,9 +214,9 @@ static void dumpAttribute(raw_ostream &O
 
   StringRef Name;
   std::string File;
-  auto Color = syntax::Enumerator;
+  auto Color = HighlightColor::Enumerator;
   if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
-    Color = syntax::String;
+    Color = HighlightColor::String;
     if (const auto *LT = U->getContext().getLineTableForUnit(U))
       if (LT->getFileNameByIndex(
               formValue.getAsUnsignedConstant().getValue(),
@@ -459,16 +459,17 @@ void DWARFDie::dump(raw_ostream &OS, uns
   if (debug_info_data.isValidOffset(offset)) {
     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
     if (DumpOpts.ShowAddresses)
-      WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
+      WithColor(OS, HighlightColor::Address).get()
+          << format("\n0x%8.8x: ", Offset);
 
     if (abbrCode) {
       auto AbbrevDecl = getAbbreviationDeclarationPtr();
       if (AbbrevDecl) {
         auto tagString = TagString(getTag());
         if (!tagString.empty())
-          WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
+          WithColor(OS, HighlightColor::Tag).get().indent(Indent) << tagString;
         else
-          WithColor(OS, syntax::Tag).get().indent(Indent)
+          WithColor(OS, HighlightColor::Tag).get().indent(Indent)
               << format("DW_TAG_Unknown_%x", getTag());
 
         if (DumpOpts.Verbose)

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp?rev=327108&r1=327107&r2=327108&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFFormValue.cpp Fri Mar  9 01:56:24 2018
@@ -8,7 +8,6 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
-#include "SyntaxHighlighting.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/None.h"
 #include "llvm/ADT/Optional.h"
@@ -19,6 +18,7 @@
 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Format.h"
+#include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cinttypes>
 #include <cstdint>
@@ -26,7 +26,6 @@
 
 using namespace llvm;
 using namespace dwarf;
-using namespace syntax;
 
 static const DWARFFormValue::FormClass DWARF5FormClasses[] = {
     DWARFFormValue::FC_Unknown,  // 0x0
@@ -421,8 +420,9 @@ bool DWARFFormValue::extractValue(const
 void DWARFFormValue::dump(raw_ostream &OS, DIDumpOptions DumpOpts) const {
   uint64_t UValue = Value.uval;
   bool CURelativeOffset = false;
-  raw_ostream &AddrOS =
-      DumpOpts.ShowAddresses ? WithColor(OS, syntax::Address).get() : nulls();
+  raw_ostream &AddrOS = DumpOpts.ShowAddresses
+                            ? WithColor(OS, HighlightColor::Address).get()
+                            : nulls();
   switch (Form) {
   case DW_FORM_addr:
     AddrOS << format("0x%016" PRIx64, UValue);
@@ -584,7 +584,7 @@ void DWARFFormValue::dump(raw_ostream &O
   if (CURelativeOffset) {
     if (DumpOpts.Verbose)
       OS << " => {";
-    WithColor(OS, syntax::Address).get()
+    WithColor(OS, HighlightColor::Address).get()
         << format("0x%8.8" PRIx64, UValue + (U ? U->getOffset() : 0));
     if (DumpOpts.Verbose)
       OS << "}";
@@ -594,7 +594,7 @@ void DWARFFormValue::dump(raw_ostream &O
 void DWARFFormValue::dumpString(raw_ostream &OS) const {
   Optional<const char *> DbgStr = getAsCString();
   if (DbgStr.hasValue()) {
-    auto COS = WithColor(OS, syntax::String);
+    auto COS = WithColor(OS, HighlightColor::String);
     COS.get() << '"';
     COS.get().write_escaped(DbgStr.getValue());
     COS.get() << '"';

Modified: llvm/trunk/lib/DebugInfo/DWARF/DWARFVerifier.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/DWARFVerifier.cpp?rev=327108&r1=327107&r2=327108&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/DWARFVerifier.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/DWARFVerifier.cpp Fri Mar  9 01:56:24 2018
@@ -7,8 +7,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "SyntaxHighlighting.h"
 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
+#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
@@ -16,8 +16,8 @@
 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
-#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
 #include "llvm/Support/FormatVariadic.h"
+#include "llvm/Support/WithColor.h"
 #include "llvm/Support/raw_ostream.h"
 #include <map>
 #include <set>
@@ -26,7 +26,6 @@
 using namespace llvm;
 using namespace dwarf;
 using namespace object;
-using namespace syntax;
 
 DWARFVerifier::DieRangeInfo::address_range_iterator
 DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) {
@@ -885,13 +884,13 @@ bool DWARFVerifier::handleAccelTables()
 }
 
 raw_ostream &DWARFVerifier::error() const {
-  return WithColor(OS, syntax::Error).get() << "error: ";
+  return WithColor(OS, HighlightColor::Error).get() << "error: ";
 }
 
 raw_ostream &DWARFVerifier::warn() const {
-  return WithColor(OS, syntax::Warning).get() << "warning: ";
+  return WithColor(OS, HighlightColor::Warning).get() << "warning: ";
 }
 
 raw_ostream &DWARFVerifier::note() const {
-  return WithColor(OS, syntax::Note).get() << "note: ";
+  return WithColor(OS, HighlightColor::Note).get() << "note: ";
 }

Removed: llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.cpp?rev=327107&view=auto
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.cpp (removed)
@@ -1,49 +0,0 @@
-//===- SyntaxHighlighting.cpp ---------------------------------------------===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#include "SyntaxHighlighting.h"
-#include "llvm/Support/CommandLine.h"
-#include "llvm/Support/raw_ostream.h"
-
-using namespace llvm;
-using namespace dwarf;
-using namespace syntax;
-
-static cl::opt<cl::boolOrDefault>
-    UseColor("color",
-             cl::desc("use colored syntax highlighting (default=autodetect)"),
-             cl::init(cl::BOU_UNSET));
-
-bool WithColor::colorsEnabled(raw_ostream &OS) {
-  if (UseColor == cl::BOU_UNSET)
-    return OS.has_colors();
-  return UseColor == cl::BOU_TRUE;
-}
-
-WithColor::WithColor(raw_ostream &OS, enum HighlightColor Type) : OS(OS) {
-  // Detect color from terminal type unless the user passed the --color option.
-  if (colorsEnabled(OS)) {
-    switch (Type) {
-    case Address:    OS.changeColor(raw_ostream::YELLOW);         break;
-    case String:     OS.changeColor(raw_ostream::GREEN);          break;
-    case Tag:        OS.changeColor(raw_ostream::BLUE);           break;
-    case Attribute:  OS.changeColor(raw_ostream::CYAN);           break;
-    case Enumerator: OS.changeColor(raw_ostream::MAGENTA);        break;
-    case Macro:      OS.changeColor(raw_ostream::RED);            break;
-    case Error:      OS.changeColor(raw_ostream::RED, true);      break;
-    case Warning:    OS.changeColor(raw_ostream::MAGENTA, true);  break;
-    case Note:       OS.changeColor(raw_ostream::BLACK, true);    break;
-    }
-  }
-}
-
-WithColor::~WithColor() {
-  if (colorsEnabled(OS))
-    OS.resetColor();
-}

Removed: llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.h?rev=327107&view=auto
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.h (original)
+++ llvm/trunk/lib/DebugInfo/DWARF/SyntaxHighlighting.h (removed)
@@ -1,54 +0,0 @@
-//===- SyntaxHighlighting.h -------------------------------------*- C++ -*-===//
-//
-//                     The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_LIB_DEBUGINFO_SYNTAXHIGHLIGHTING_H
-#define LLVM_LIB_DEBUGINFO_SYNTAXHIGHLIGHTING_H
-
-namespace llvm {
-
-class raw_ostream;
-
-namespace dwarf {
-namespace syntax {
-
-// Symbolic names for various syntax elements.
-enum HighlightColor {
-  Address,
-  String,
-  Tag,
-  Attribute,
-  Enumerator,
-  Macro,
-  Error,
-  Warning,
-  Note
-};
-
-/// An RAII object that temporarily switches an output stream to a
-/// specific color.
-class WithColor {
-  raw_ostream &OS;
-  /// Determine whether colors should be displayed.
-  bool colorsEnabled(raw_ostream &OS);
-
-public:
-  /// To be used like this: WithColor(OS, syntax::String) << "text";
-  WithColor(raw_ostream &OS, enum HighlightColor Type);
-  ~WithColor();
-
-  raw_ostream &get() { return OS; }
-  operator raw_ostream &() { return OS; }
-};
-
-} // end namespace syntax
-} // end namespace dwarf
-
-} // end namespace llvm
-
-#endif // LLVM_LIB_DEBUGINFO_SYNTAXHIGHLIGHTING_H

Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=327108&r1=327107&r2=327108&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Fri Mar  9 01:56:24 2018
@@ -120,6 +120,7 @@ add_llvm_library(LLVMSupport
   Twine.cpp
   Unicode.cpp
   UnicodeCaseFold.cpp
+  WithColor.cpp
   YAMLParser.cpp
   YAMLTraits.cpp
   raw_os_ostream.cpp

Added: llvm/trunk/lib/Support/WithColor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/WithColor.cpp?rev=327108&view=auto
==============================================================================
--- llvm/trunk/lib/Support/WithColor.cpp (added)
+++ llvm/trunk/lib/Support/WithColor.cpp Fri Mar  9 01:56:24 2018
@@ -0,0 +1,65 @@
+//===- WithColor.cpp ------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/WithColor.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace llvm;
+
+static cl::opt<cl::boolOrDefault>
+    UseColor("color",
+             cl::desc("use colored syntax highlighting (default=autodetect)"),
+             cl::init(cl::BOU_UNSET));
+
+bool WithColor::colorsEnabled(raw_ostream &OS) {
+  if (UseColor == cl::BOU_UNSET)
+    return OS.has_colors();
+  return UseColor == cl::BOU_TRUE;
+}
+
+WithColor::WithColor(raw_ostream &OS, HighlightColor Color) : OS(OS) {
+  // Detect color from terminal type unless the user passed the --color option.
+  if (colorsEnabled(OS)) {
+    switch (Color) {
+    case HighlightColor::Address:
+      OS.changeColor(raw_ostream::YELLOW);
+      break;
+    case HighlightColor::String:
+      OS.changeColor(raw_ostream::GREEN);
+      break;
+    case HighlightColor::Tag:
+      OS.changeColor(raw_ostream::BLUE);
+      break;
+    case HighlightColor::Attribute:
+      OS.changeColor(raw_ostream::CYAN);
+      break;
+    case HighlightColor::Enumerator:
+      OS.changeColor(raw_ostream::MAGENTA);
+      break;
+    case HighlightColor::Macro:
+      OS.changeColor(raw_ostream::RED);
+      break;
+    case HighlightColor::Error:
+      OS.changeColor(raw_ostream::RED, true);
+      break;
+    case HighlightColor::Warning:
+      OS.changeColor(raw_ostream::MAGENTA, true);
+      break;
+    case HighlightColor::Note:
+      OS.changeColor(raw_ostream::BLACK, true);
+      break;
+    }
+  }
+}
+
+WithColor::~WithColor() {
+  if (colorsEnabled(OS))
+    OS.resetColor();
+}




More information about the llvm-commits mailing list