[PATCH] D126826: [BOLT][NFC] Replacing stdio functions with raw_ostream functions

Huan Nguyen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 1 17:29:22 PDT 2022


nhuhuan updated this revision to Diff 433597.
nhuhuan added a comment.

Prune unnecessary include, restrict use of auto keyword, format floating-point values.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126826/new/

https://reviews.llvm.org/D126826

Files:
  bolt/include/bolt/Passes/CallGraph.h


Index: bolt/include/bolt/Passes/CallGraph.h
===================================================================
--- bolt/include/bolt/Passes/CallGraph.h
+++ bolt/include/bolt/Passes/CallGraph.h
@@ -9,9 +9,10 @@
 #ifndef BOLT_PASSES_CALLGRAPH_H
 #define BOLT_PASSES_CALLGRAPH_H
 
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/raw_ostream.h"
 #include <cassert>
 #include <cstdint>
-#include <cstdio>
 #include <unordered_set>
 #include <vector>
 
@@ -160,31 +161,33 @@
 };
 
 template <class L> void CallGraph::printDot(char *FileName, L GetLabel) const {
-  FILE *File = fopen(FileName, "wt");
-  if (!File)
+  std::error_code EC;
+  raw_fd_ostream OS(std::string(FileName), EC, sys::fs::OF_None);
+  if (EC)
     return;
 
-  fprintf(File, "digraph g {\n");
-  for (NodeId F = 0; F < Nodes.size(); F++) {
+  OS << "digraph g {\n";
+  for (NodeId F = 0; F < Nodes.size(); ++F) {
     if (Nodes[F].samples() == 0)
       continue;
-    fprintf(File, "f%lu [label=\"%s\\nsamples=%u\\nsize=%u\"];\n", F,
-            GetLabel(F), Nodes[F].samples(), Nodes[F].size());
+    OS << "f" << (unsigned long)F << " [label=\"" << GetLabel(F)
+       << "\\nsamples=" << (unsigned int)(Nodes[F].samples())
+       << "\\nsize=" << (unsigned int)(Nodes[F].size()) << "\"];\n";
   }
-  for (NodeId F = 0; F < Nodes.size(); F++) {
+
+  for (NodeId F = 0; F < Nodes.size(); ++F) {
     if (Nodes[F].samples() == 0)
       continue;
     for (NodeId Dst : Nodes[F].successors()) {
       ArcConstIterator Arc = findArc(F, Dst);
-      fprintf(
-          File,
-          "f%lu -> f%u [label=\"normWgt=%.3lf,weight=%.0lf,callOffset=%.1lf\"];"
-          "\n",
-          F, Dst, Arc->normalizedWeight(), Arc->weight(), Arc->avgCallOffset());
+      OS << "f" << (unsigned long)F << " -> f" << (unsigned)Dst
+         << " [label=\"normWgt=" << format("%.3lf", Arc->normalizedWeight())
+         << ",weight=" << format("%.0lf", Arc->weight())
+         << ",callOffset=" << format("%.1lf", Arc->avgCallOffset()) << "\"];\n";
     }
   }
-  fprintf(File, "}\n");
-  fclose(File);
+  OS << "}\n";
+  OS.close();
 }
 
 } // namespace bolt


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126826.433597.patch
Type: text/x-patch
Size: 2135 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220602/793aecfb/attachment.bin>


More information about the llvm-commits mailing list