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

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 12 15:43:24 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: PysKa Ratzinger (PysKa-Ratzinger)

<details>
<summary>Changes</summary>

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}"];
}
```

---
Full diff: https://github.com/llvm/llvm-project/pull/148403.diff


2 Files Affected:

- (modified) llvm/include/llvm/Analysis/CFGPrinter.h (+10-2) 
- (modified) llvm/lib/Analysis/CFGPrinter.cpp (+5) 


``````````diff
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)"

``````````

</details>


https://github.com/llvm/llvm-project/pull/148403


More information about the llvm-commits mailing list