-#if 0
-#define t(x) (1 << (unsigned)Type::x)
-#define SignedIntegralTypes (t(SByteTyID) | t(ShortTyID) | \
- t(IntTyID) | t(LongTyID))
-static long UnsignedIntegralTypes = t(UByteTyID) | t(UShortTyID) |
- t(UIntTyID) | t(ULongTyID);
-static const long FloatingPointTypes = t(FloatTyID) | t(DoubleTyID);
-
-static const long IntegralTypes = SignedIntegralTypes | UnsignedIntegralTypes;
-
-static long ValidTypes[Type::FirstDerivedTyID] = {
- [(unsigned)Instruction::UnaryOps::Not] t(BoolTyID),
- //[Instruction::UnaryOps::Add] = IntegralTypes,
- // [Instruction::Sub] = IntegralTypes,
-};
-#undef t
-#endif
-
-// CheckFailed - A check failed, so print out the condition and the message that
-// failed. This provides a nice place to put a breakpoint if you want to see
-// why something is not correct.
-//
-static inline void CheckFailed(const char *Cond, const std::string &Message,
- const Value *V1 = 0, const Value *V2 = 0) {
- std::cerr << Message << "\n";
- if (V1) { V1->dump(); std::cerr << "\n"; }
- if (V2) { V2->dump(); std::cerr << "\n"; }
+namespace { // Anonymous namespace for class
+
+ 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?
+
+ DominatorSet *DS; // Dominator set, caution can be null!
+
+ Verifier() : Broken(false), RealPass(true), AbortBroken(true), DS(0) {}
+ Verifier(bool AB) : Broken(false), RealPass(true), AbortBroken(AB), DS(0) {}
+ Verifier(DominatorSet &ds)
+ : Broken(false), RealPass(false), AbortBroken(false), DS(&ds) {}
+
+
+ bool doInitialization(Module &M) {
+ verifySymbolTable(M.getSymbolTable());
+
+ // If this is a real pass, in a pass manager, we must abort before
+ // returning back to the pass manager, or else the pass manager may try to
+ // run other passes on the broken module.
+ //
+ if (RealPass)
+ abortIfBroken();
+ return false;
+ }
+
+ bool runOnFunction(Function &F) {
+ // Get dominator information if we are being run by PassManager
+ if (RealPass) DS = &getAnalysis<DominatorSet>();
+ visit(F);
+
+ // If this is a real pass, in a pass manager, we must abort before
+ // returning back to the pass manager, or else the pass manager may try to
+ // run other passes on the broken module.
+ //
+ if (RealPass)
+ abortIfBroken();
+
+ return false;
+ }
+
+ bool doFinalization(Module &M) {
+ // Scan through, checking all of the external function's linkage now...
+ for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
+ if (I->isExternal() && I->hasInternalLinkage())
+ CheckFailed("Function Declaration has Internal Linkage!", I);
+
+ // If the module is broken, abort at this time.
+ abortIfBroken();
+ return false;
+ }
+
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.setPreservesAll();
+ if (RealPass)
+ AU.addRequired<DominatorSet>();
+ }
+
+ // 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();
+ }
+ }
+
+ // Verification methods...
+ void verifySymbolTable(SymbolTable *ST);
+ void visitFunction(Function &F);
+ void visitBasicBlock(BasicBlock &BB);
+ void visitPHINode(PHINode &PN);
+ void visitBinaryOperator(BinaryOperator &B);
+ void visitShiftInst(ShiftInst &SI);
+ void visitCallInst(CallInst &CI);
+ void visitGetElementPtrInst(GetElementPtrInst &GEP);
+ void visitLoadInst(LoadInst &LI);
+ void visitStoreInst(StoreInst &SI);
+ void visitInstruction(Instruction &I);
+ void visitTerminatorInst(TerminatorInst &I);
+ void visitReturnInst(ReturnInst &RI);
+
+ // CheckFailed - A check failed, so print out the condition and the message
+ // that failed. This provides a nice place to put a breakpoint if you want
+ // to see why something is not correct.
+ //
+ inline 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";
+ if (V1) std::cerr << *V1 << "\n";
+ if (V2) std::cerr << *V2 << "\n";
+ if (V3) std::cerr << *V3 << "\n";
+ if (V4) std::cerr << *V4 << "\n";
+ Broken = true;
+ }
+ };
+
+ RegisterPass<Verifier> X("verify", "Module Verifier");