* Add a new helper progress method
authorChris Lattner <sabre@nondot.org>
Wed, 31 Dec 2003 10:20:38 +0000 (10:20 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 31 Dec 2003 10:20:38 +0000 (10:20 +0000)
* Make sure that the user sees the 100% mark
* Don't bother printing out X.0%, just print out X%

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10672 91177308-0d34-0410-b5e6-96231b3b80d8

include/Support/SlowOperationInformer.h
include/llvm/Support/SlowOperationInformer.h
lib/Support/SlowOperationInformer.cpp

index 75c5796aaabc2e92953edb5000437007ec44c2fd..67edf7df42b53998e5c53f7e824992e00556e9ff 100644 (file)
@@ -32,6 +32,7 @@
 #define SUPPORT_SLOW_OPERATION_INFORMER_H
 
 #include <string>
+#include <cassert>
 
 namespace llvm {
   class SlowOperationInformer {
@@ -49,6 +50,15 @@ namespace llvm {
     /// along the operation is, given in 1/10ths of a percent (in other words,
     /// Amount should range from 0 to 1000).
     void progress(unsigned Amount);
+
+    /// progress - Same as the method above, but this performs the division for
+    /// you, and helps you avoid overflow if you are dealing with largish
+    /// numbers.
+    void progress(unsigned Current, unsigned Maximum) {
+      assert(Maximum != 0 &&
+             "Shouldn't be doing work if there is nothing to do!");
+      progress(Current*1000ULL/Maximum);
+    }
   };
 } // end namespace llvm
 
index 75c5796aaabc2e92953edb5000437007ec44c2fd..67edf7df42b53998e5c53f7e824992e00556e9ff 100644 (file)
@@ -32,6 +32,7 @@
 #define SUPPORT_SLOW_OPERATION_INFORMER_H
 
 #include <string>
+#include <cassert>
 
 namespace llvm {
   class SlowOperationInformer {
@@ -49,6 +50,15 @@ namespace llvm {
     /// along the operation is, given in 1/10ths of a percent (in other words,
     /// Amount should range from 0 to 1000).
     void progress(unsigned Amount);
+
+    /// progress - Same as the method above, but this performs the division for
+    /// you, and helps you avoid overflow if you are dealing with largish
+    /// numbers.
+    void progress(unsigned Current, unsigned Maximum) {
+      assert(Maximum != 0 &&
+             "Shouldn't be doing work if there is nothing to do!");
+      progress(Current*1000ULL/Maximum);
+    }
   };
 } // end namespace llvm
 
index 9dc2750f7698f899a448d28a528a4336895c7e92..9fd718e3060707a342546898e8d83ec3969a951f 100644 (file)
@@ -59,8 +59,12 @@ SlowOperationInformer::SlowOperationInformer(const std::string &Name)
 
 SlowOperationInformer::~SlowOperationInformer() {
   NestedSOI = false;
-  if (LastPrintAmount)
-    std::cout << "\n";
+  if (LastPrintAmount) {
+    // If we have printed something, make _sure_ we print the 100% amount, and
+    // also print a newline.
+    std::cout << std::string(LastPrintAmount, '\b') << "Progress "
+              << OperationName << ": 100%  \n";
+  }
 
   alarm(0);
   signal(SIGALRM, SIG_DFL);
@@ -87,8 +91,11 @@ void SlowOperationInformer::progress(unsigned Amount) {
   std::string ToPrint = std::string(LastPrintAmount, '\b');
 
   std::ostringstream OS;
-  OS << "Progress " << OperationName << ": " << Amount/10 << "." << Amount % 10
-     << "%";
+  OS << "Progress " << OperationName << ": " << Amount/10;
+  if (unsigned Rem = Amount % 10)
+    OS << "." << Rem << "%";
+  else
+    OS << "%  ";
 
   LastPrintAmount = OS.str().size();
   std::cout << ToPrint+OS.str() << std::flush;