[llvm-commits] CVS: llvm/lib/VMCore/Verifier.cpp

Chris Lattner lattner at cs.uiuc.edu
Fri Apr 2 10:25:48 PST 2004


Changes in directory llvm/lib/VMCore:

Verifier.cpp updated: 1.89 -> 1.90

---
Log message:

Make the verifier API more complete and useful.

Patch contributed by Reid Spencer


---
Diffs of the changes:  (+47 -19)

Index: llvm/lib/VMCore/Verifier.cpp
diff -u llvm/lib/VMCore/Verifier.cpp:1.89 llvm/lib/VMCore/Verifier.cpp:1.90
--- llvm/lib/VMCore/Verifier.cpp:1.89	Sun Mar 28 18:29:36 2004
+++ llvm/lib/VMCore/Verifier.cpp	Fri Apr  2 09:45:08 2004
@@ -55,6 +55,7 @@
 #include "llvm/Support/InstVisitor.h"
 #include "Support/STLExtras.h"
 #include <algorithm>
+#include <sstream>
 using namespace llvm;
 
 namespace {  // Anonymous namespace for class
@@ -62,14 +63,25 @@
   struct Verifier : public FunctionPass, InstVisitor<Verifier> {
     bool Broken;          // Is this module found to be broken?
     bool RealPass;        // Are we not being run by a PassManager?
-    bool AbortBroken;     // If broken, should it or should it not abort?
+    VerifierFailureAction action;
+    			  // What to do if verification fails.
     Module *Mod;          // Module we are verifying right now
     DominatorSet *DS;     // Dominator set, caution can be null!
+    std::stringstream msgs;  // A stringstream to collect messages
 
-    Verifier() : Broken(false), RealPass(true), AbortBroken(true), DS(0) {}
-    Verifier(bool AB) : Broken(false), RealPass(true), AbortBroken(AB), DS(0) {}
+    Verifier() 
+	: Broken(false), RealPass(true), action(AbortProcessAction),
+          DS(0), msgs( std::ios_base::app | std::ios_base::out ) {}
+    Verifier( VerifierFailureAction ctn )
+	: Broken(false), RealPass(true), action(ctn), DS(0), 
+          msgs( std::ios_base::app | std::ios_base::out ) {}
+    Verifier(bool AB ) 
+	: Broken(false), RealPass(true), 
+          action( AB ? AbortProcessAction : PrintMessageAction), DS(0), 
+	  msgs( std::ios_base::app | std::ios_base::out ) {}
     Verifier(DominatorSet &ds) 
-      : Broken(false), RealPass(false), AbortBroken(false), DS(&ds) {}
+      : Broken(false), RealPass(false), action(PrintMessageAction),
+        DS(&ds), msgs( std::ios_base::app | std::ios_base::out ) {}
 
 
     bool doInitialization(Module &M) {
@@ -120,10 +132,26 @@
     /// abortIfBroken - If the module is broken and we are supposed to abort on
     /// this condition, do so.
     ///
-    void abortIfBroken() const {
-      if (Broken && AbortBroken) {
-        std::cerr << "Broken module found, compilation aborted!\n";
-        abort();
+    void abortIfBroken() {
+      if (Broken)
+      {
+        msgs << "Broken module found, ";
+	switch (action)
+	{
+	  case AbortProcessAction:
+	    msgs << "compilation aborted!\n";
+	    std::cerr << msgs.str();
+	    abort();
+	  case ThrowExceptionAction:
+	    msgs << "verification terminated.\n";
+	    throw msgs.str();
+	  case PrintMessageAction:
+	    msgs << "verification continues.\n";
+	    std::cerr << msgs.str();
+	    break;
+	  case ReturnStatusAction:
+	    break;
+	}
       }
     }
 
@@ -154,12 +182,12 @@
     void WriteValue(const Value *V) {
       if (!V) return;
       if (isa<Instruction>(V)) {
-        std::cerr << *V;
+        msgs << *V;
       } else if (const Type *Ty = dyn_cast<Type>(V)) {
-        WriteTypeSymbolic(std::cerr, Ty, Mod);
+        WriteTypeSymbolic(msgs, Ty, Mod);
       } else {
-        WriteAsOperand (std::cerr, V, true, true, Mod);
-        std::cerr << "\n";
+        WriteAsOperand (msgs, V, true, true, Mod);
+        msgs << "\n";
       }
     }
 
@@ -170,7 +198,7 @@
     void CheckFailed(const std::string &Message,
                      const Value *V1 = 0, const Value *V2 = 0,
                      const Value *V3 = 0, const Value *V4 = 0) {
-      std::cerr << Message << "\n";
+      msgs << Message << "\n";
       WriteValue(V1);
       WriteValue(V2);
       WriteValue(V3);
@@ -623,18 +651,18 @@
 //  Implement the public interfaces to this file...
 //===----------------------------------------------------------------------===//
 
-FunctionPass *llvm::createVerifierPass() {
-  return new Verifier();
+FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
+  return new Verifier(action);
 }
 
 
 // verifyFunction - Create 
-bool llvm::verifyFunction(const Function &f) {
+bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
   Function &F = const_cast<Function&>(f);
   assert(!F.isExternal() && "Cannot verify external functions");
   
   FunctionPassManager FPM(new ExistingModuleProvider(F.getParent()));
-  Verifier *V = new Verifier();
+  Verifier *V = new Verifier(action);
   FPM.add(V);
   FPM.run(F);
   return V->Broken;
@@ -643,9 +671,9 @@
 /// verifyModule - Check a module for errors, printing messages on stderr.
 /// Return true if the module is corrupt.
 ///
-bool llvm::verifyModule(const Module &M) {
+bool llvm::verifyModule(const Module &M, VerifierFailureAction action) {
   PassManager PM;
-  Verifier *V = new Verifier();
+  Verifier *V = new Verifier(action);
   PM.add(V);
   PM.run((Module&)M);
   return V->Broken;





More information about the llvm-commits mailing list