Skip to content

Commit

Permalink
[llvm-c][MC] Expose color printing via LLVMSetDisasmOptions (llvm#112980
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jroelofs authored Oct 18, 2024
1 parent f4c6088 commit 709116c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion llvm/include/llvm-c/Disassembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/MC/MCDisassembler/Disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

0 comments on commit 709116c

Please sign in to comment.