[llvm] [CFGPrinter] Added command line option to change DOT font (PR #148403)

PysKa Ratzinger via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 12 15:42:34 PDT 2025


https://github.com/PysKa-Ratzinger created https://github.com/llvm/llvm-project/pull/148403

This patch introduces the ability to change the fontname used in the DOT graphs of the view-cfg and dot-cfg passes.

This was tested by compiling the bitcode for a simple main.cpp with `clang++ -O0 -emit-llvm ./main.cpp -c -o main.bc`

```c++
int main() { return 0; }
```

And generating 2 CFG graphs, one without the new argument (to make sure the original behavior is maintained), and another with the new argument, which resulted in the following:

```dot
# Generated with opt -p dot-cfg ./main.bc
digraph "CFG for 'main' function" {
	label="CFG for 'main' function";

	Node0x557a6e235010 [shape=record,color="#b70d28ff", style=filled, fillcolor="#b70d2870" fontname="Courier",label="{entry:\l|  %retval = alloca i32, align 4\l  store i32 0, ptr %retval, align 4\l  ret i32 0\l}"];
}
```

```dot
# Generated with opt -p dot-cfg ./main.bc -cfg-fontname=Arial
digraph "CFG for 'main' function" {
	label="CFG for 'main' function";

	Node0x55c9e8286010 [shape=record,color="#b70d28ff", style=filled, fillcolor="#b70d2870" fontname="Arial",label="{entry:\l|  %retval = alloca i32, align 4\l  store i32 0, ptr %retval, align 4\l  ret i32 0\l}"];
}
```

>From 1109d3ca85b69f399375c3e18f7392218503a0b3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ricardo=20Catal=C3=A3o?= <ricardojorge512 at hotmail.com>
Date: Sat, 12 Jul 2025 23:40:27 +0100
Subject: [PATCH] [CFGPrinter] Added command line option to change DOT font

This patch introduces the ability to change the fontname used in the DOT
graphs of the view-cfg and dot-cfg passes.
---
 llvm/include/llvm/Analysis/CFGPrinter.h | 12 ++++++++++--
 llvm/lib/Analysis/CFGPrinter.cpp        |  5 +++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Analysis/CFGPrinter.h b/llvm/include/llvm/Analysis/CFGPrinter.h
index ec26da87eb916..8955ada815ed0 100644
--- a/llvm/include/llvm/Analysis/CFGPrinter.h
+++ b/llvm/include/llvm/Analysis/CFGPrinter.h
@@ -27,11 +27,14 @@
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/ProfDataUtils.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/DOTGraphTraits.h"
 #include "llvm/Support/FormatVariadic.h"
 
 namespace llvm {
+LLVM_ABI extern cl::opt<std::string> CFGFontName;
+
 class ModuleSlotTracker;
 
 template <class GraphType> struct GraphTraits;
@@ -321,9 +324,14 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
                                 ? (getHeatColor(0))
                                 : (getHeatColor(1));
 
+    std::string FontName = "Courier";
+    if (!CFGFontName.empty()) {
+      FontName = CFGFontName;
+    }
+
     std::string Attrs = "color=\"" + EdgeColor + "ff\", style=filled," +
-                        " fillcolor=\"" + Color + "70\"" +
-                        " fontname=\"Courier\"";
+                        " fillcolor=\"" + Color + "70\"" + " fontname=\"" +
+                        FontName + "\"";
     return Attrs;
   }
   LLVM_ABI bool isNodeHidden(const BasicBlock *Node,
diff --git a/llvm/lib/Analysis/CFGPrinter.cpp b/llvm/lib/Analysis/CFGPrinter.cpp
index 38aad849755be..d7f411ddbf9a4 100644
--- a/llvm/lib/Analysis/CFGPrinter.cpp
+++ b/llvm/lib/Analysis/CFGPrinter.cpp
@@ -26,6 +26,11 @@
 
 using namespace llvm;
 
+cl::opt<std::string>
+    llvm::CFGFontName("cfg-fontname", cl::Hidden,
+                      cl::desc("The name of a font to use for the DOT"
+                               " file (defaults to Courier)"));
+
 static cl::opt<std::string>
     CFGFuncName("cfg-func-name", cl::Hidden,
                 cl::desc("The name of a function (or its substring)"



More information about the llvm-commits mailing list