diff --git a/bolt/include/bolt/Passes/CallGraph.h b/bolt/include/bolt/Passes/CallGraph.h --- a/bolt/include/bolt/Passes/CallGraph.h +++ b/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 #include -#include #include #include @@ -160,31 +161,33 @@ }; template 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