VerifierTest.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //===- llvm/unittest/IR/VerifierTest.cpp - Verifier unit tests ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/IR/Verifier.h"
  10. #include "llvm/IR/Constants.h"
  11. #include "llvm/IR/DerivedTypes.h"
  12. #include "llvm/IR/Function.h"
  13. #include "llvm/IR/GlobalAlias.h"
  14. #include "llvm/IR/GlobalVariable.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/LLVMContext.h"
  17. #include "llvm/IR/Module.h"
  18. #include "gtest/gtest.h"
  19. namespace llvm {
  20. namespace {
  21. TEST(VerifierTest, Branch_i1) {
  22. LLVMContext &C = getGlobalContext();
  23. Module M("M", C);
  24. FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
  25. Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
  26. BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
  27. BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
  28. ReturnInst::Create(C, Exit);
  29. // To avoid triggering an assertion in BranchInst::Create, we first create
  30. // a branch with an 'i1' condition ...
  31. Constant *False = ConstantInt::getFalse(C);
  32. BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry);
  33. // ... then use setOperand to redirect it to a value of different type.
  34. Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0);
  35. BI->setOperand(0, Zero32);
  36. EXPECT_TRUE(verifyFunction(*F));
  37. }
  38. TEST(VerifierTest, InvalidRetAttribute) {
  39. LLVMContext &C = getGlobalContext();
  40. Module M("M", C);
  41. FunctionType *FTy = FunctionType::get(Type::getInt32Ty(C), /*isVarArg=*/false);
  42. Function *F = cast<Function>(M.getOrInsertFunction("foo", FTy));
  43. AttributeSet AS = F->getAttributes();
  44. F->setAttributes(AS.addAttribute(C, AttributeSet::ReturnIndex,
  45. Attribute::UWTable));
  46. std::string Error;
  47. raw_string_ostream ErrorOS(Error);
  48. EXPECT_TRUE(verifyModule(M, &ErrorOS));
  49. EXPECT_TRUE(StringRef(ErrorOS.str()).startswith(
  50. "Attribute 'uwtable' only applies to functions!"));
  51. }
  52. }
  53. }