[llvm] 709116c - [llvm-c][MC] Expose color printing via LLVMSetDisasmOptions (#112980)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 15:31:00 PDT 2024


Author: Jon Roelofs
Date: 2024-10-18T15:30:57-07:00
New Revision: 709116cb76803bdb897d191ef2d96ae19846ed81

URL: https://github.com/llvm/llvm-project/commit/709116cb76803bdb897d191ef2d96ae19846ed81
DIFF: https://github.com/llvm/llvm-project/commit/709116cb76803bdb897d191ef2d96ae19846ed81.diff

LOG: [llvm-c][MC] Expose color printing via LLVMSetDisasmOptions (#112980)

Added: 
    

Modified: 
    llvm/include/llvm-c/Disassembler.h
    llvm/lib/MC/MCDisassembler/Disassembler.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm-c/Disassembler.h b/llvm/include/llvm-c/Disassembler.h
index b1cb35da6687a8..4bc6b04dd6eaf7 100644
--- a/llvm/include/llvm-c/Disassembler.h
+++ b/llvm/include/llvm-c/Disassembler.h
@@ -79,8 +79,10 @@ int LLVMSetDisasmOptions(LLVMDisasmContextRef DC, uint64_t Options);
 #define LLVMDisassembler_Option_AsmPrinterVariant 4
 /* The option to set comment on instructions */
 #define LLVMDisassembler_Option_SetInstrComments 8
-  /* The option to print latency information alongside instructions */
+/* The option to print latency information alongside instructions */
 #define LLVMDisassembler_Option_PrintLatency 16
+/* The option to print in color */
+#define LLVMDisassembler_Option_Color 32
 
 /**
  * Dispose of a disassembler context.

diff  --git a/llvm/lib/MC/MCDisassembler/Disassembler.cpp b/llvm/lib/MC/MCDisassembler/Disassembler.cpp
index 5e5a163c290244..f5d6c6bb561871 100644
--- a/llvm/lib/MC/MCDisassembler/Disassembler.cpp
+++ b/llvm/lib/MC/MCDisassembler/Disassembler.cpp
@@ -277,6 +277,12 @@ size_t LLVMDisasmInstruction(LLVMDisasmContextRef DCR, uint8_t *Bytes,
     SmallVector<char, 64> InsnStr;
     raw_svector_ostream OS(InsnStr);
     formatted_raw_ostream FormattedOS(OS);
+
+    if (DC->getOptions() & LLVMDisassembler_Option_Color) {
+      FormattedOS.enable_colors(true);
+      IP->setUseColor(true);
+    }
+
     IP->printInst(&Inst, PC, AnnotationsStr, *DC->getSubtargetInfo(),
                   FormattedOS);
 
@@ -343,5 +349,10 @@ int LLVMSetDisasmOptions(LLVMDisasmContextRef DCR, uint64_t Options){
     DC->addOptions(LLVMDisassembler_Option_PrintLatency);
     Options &= ~LLVMDisassembler_Option_PrintLatency;
   }
+  if (Options & LLVMDisassembler_Option_Color) {
+    LLVMDisasmContext *DC = static_cast<LLVMDisasmContext *>(DCR);
+    DC->addOptions(LLVMDisassembler_Option_Color);
+    Options &= ~LLVMDisassembler_Option_Color;
+  }
   return (Options == 0);
 }


        


More information about the llvm-commits mailing list