Allow any cl::opt to use the method getPosition() to retrieve the option's
[oota-llvm.git] / lib / Support / Statistic.cpp
index b6c75c2adee49e5048bf8d27ead48b5f3144801a..7fa7547fee33368098a1095e62d6344b1e9b7570 100644 (file)
@@ -1,4 +1,11 @@
 //===-- Statistic.cpp - Easy way to expose stats information --------------===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file implements the 'Statistic' class, which is designed to be an easy
 // way to expose various success metrics from passes.  These statistics are
 
 #include "Support/Statistic.h"
 #include "Support/CommandLine.h"
-#include <iostream>
 #include <sstream>
+#include <iostream>
+#include <algorithm>
+using namespace llvm;
 
-bool DebugFlag;  // DebugFlag - Exported boolean set by the -debug option
+// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
+namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
 
 unsigned StatisticBase::NumStats = 0;
 
@@ -29,27 +39,23 @@ unsigned StatisticBase::NumStats = 0;
 static cl::opt<bool>
 Enabled("stats", cl::desc("Enable statistics output from program"));
 
-// -debug - Command line option to enable the DEBUG statements in the passes.
-static cl::opt<bool, true>
-Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
-      cl::location(DebugFlag));
-
 struct StatRecord {
   std::string Value;
   const char *Name, *Desc;
 
-  StatRecord(const std::string V, const char *N, const char *D)
+  StatRecord(const std::string &V, const char *N, const char *D)
     : Value(V), Name(N), Desc(D) {}
 
   bool operator<(const StatRecord &SR) const {
     return std::strcmp(Name, SR.Name) < 0;
   }
 
-  void print(unsigned ValFieldSize, unsigned NameFieldSize) {
-    std::cerr << std::string(ValFieldSize-Value.length(), ' ')
-              << Value << " " << Name
-              << std::string(NameFieldSize-std::strlen(Name), ' ')
-              << " - " << Desc << "\n";
+  void print(unsigned ValFieldSize, unsigned NameFieldSize,
+             std::ostream &OS) {
+    OS << std::string(ValFieldSize-Value.length(), ' ')
+       << Value << " " << Name
+       << std::string(NameFieldSize-std::strlen(Name), ' ')
+       << " - " << Desc << "\n";
   }
 };
 
@@ -67,29 +73,35 @@ void StatisticBase::destroy() const {
   }
 
   if (--NumStats == 0 && AccumStats) {
+    std::ostream *OutStream = GetLibSupportInfoOutputFile();
+
     // Figure out how long the biggest Value and Name fields are...
     unsigned MaxNameLen = 0, MaxValLen = 0;
     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i) {
-      MaxValLen = std::max(MaxValLen, (*AccumStats)[i].Value.length());
-      MaxNameLen = std::max(MaxNameLen, std::strlen((*AccumStats)[i].Name));
+      MaxValLen = std::max(MaxValLen, 
+                           (unsigned)(*AccumStats)[i].Value.length());
+      MaxNameLen = std::max(MaxNameLen, 
+                            (unsigned)std::strlen((*AccumStats)[i].Name));
     }
 
     // Sort the fields...
     std::stable_sort(AccumStats->begin(), AccumStats->end());
 
     // Print out the statistics header...
-    std::cerr << "===" << std::string(73, '-') << "===\n"
-              << "                          ... Statistics Collected ...\n"
-              << "===" << std::string(73, '-') << "===\n\n";
+    *OutStream << "===" << std::string(73, '-') << "===\n"
+               << "                          ... Statistics Collected ...\n"
+               << "===" << std::string(73, '-') << "===\n\n";
 
     // Print all of the statistics accumulated...
     for (unsigned i = 0, e = AccumStats->size(); i != e; ++i)
-      (*AccumStats)[i].print(MaxValLen, MaxNameLen);
+      (*AccumStats)[i].print(MaxValLen, MaxNameLen, *OutStream);
 
-    std::cerr << std::endl;  // Flush the output stream...
+    *OutStream << std::endl;  // Flush the output stream...
 
     // Free all accumulated statistics...
     delete AccumStats;
     AccumStats = 0;
+    if (OutStream != &std::cerr && OutStream != &std::cout)
+      delete OutStream;   // Close the file...
   }
 }