Add new check of return value type matching ret instruction values types
authorChris Lattner <sabre@nondot.org>
Fri, 12 Apr 2002 18:20:49 +0000 (18:20 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 12 Apr 2002 18:20:49 +0000 (18:20 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2230 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/Verifier.cpp

index 77a362903c1b4695f76bfc9dc2bb7f9783541a9e..4d7596d8509a8b330f991c22a1cbf51c0cbd3d9a 100644 (file)
@@ -28,6 +28,8 @@
 //  . Verify that arrays and structures have fixed elements: No unsized arrays.
 //  * It is illegal to specify a name for a void value.
 //  * It is illegal to have a internal function that is just a declaration
+//  * It is illegal to have a ret instruction that returns a value that does not
+//    agree with the function return value type.
 //  . All other things that are tested by asserts spread about the code...
 //
 //===----------------------------------------------------------------------===//
@@ -39,6 +41,7 @@
 #include "llvm/BasicBlock.h"
 #include "llvm/Type.h"
 #include "llvm/iPHINode.h"
+#include "llvm/iTerminators.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/Support/CFG.h"
 #include "Support/STLExtras.h"
@@ -69,8 +72,8 @@ static long ValidTypes[Type::FirstDerivedTyID] = {
 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"; }
+  if (V1) { std::cerr << V1 << "\n"; }
+  if (V2) { std::cerr << V2 << "\n"; }
 }
 
 // Assert - We know that cond should be true, if not print an error message.
@@ -90,6 +93,18 @@ static bool verifyInstruction(const Instruction *I) {
   Assert1(!isa<TerminatorInst>(I),
           "Terminator instruction found embedded in basic block!\n", I);
 
+  if (isa<ReturnInst>(I)) {
+    const Function *F = I->getParent()->getParent();
+    if (I->getNumOperands() == 0)
+      Assert1(F->getReturnType() == Type::VoidTy,
+              "Function returns no value, but ret instruction found that does!",
+              I);
+    else
+      Assert2(F->getReturnType() == I->getOperand(0)->getType(),
+              "Function return type does not match operand "
+              "type of return inst!", I, F->getReturnType());
+  }
+
   // Check that all uses of the instruction, if they are instructions
   // themselves, actually have parent basic blocks.
   //