1 //===--- TBAATest.cpp - Mixed TBAA 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 
10 #include "llvm/Analysis/AliasAnalysisEvaluator.h"
11 #include "llvm/Analysis/Passes.h"
12 #include "llvm/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/MDBuilder.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/LegacyPassManager.h"
18 #include "llvm/IR/Verifier.h"
19 #include "llvm/Support/CommandLine.h"
20 #include "gtest/gtest.h"
21 
22 namespace llvm {
23 namespace {
24 
25 class OldTBAATest : public testing::Test {
26 protected:
27   OldTBAATest() : M("MixedTBAATest", C), MD(C) {}
28 
29   LLVMContext C;
30   Module M;
31   MDBuilder MD;
32 };
33 
34 TEST_F(OldTBAATest, checkVerifierBehavior) {
35   // C++ unit test case to avoid going through the auto upgrade logic.
36 
37   FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), {});
38   auto *F = cast<Function>(M.getOrInsertFunction("f", FTy));
39   auto *BB = BasicBlock::Create(C, "entry", F);
40   auto *IntType = Type::getInt32Ty(C);
41   auto *PtrType = Type::getInt32PtrTy(C);
42   auto *SI = new StoreInst(ConstantInt::get(IntType, 42),
43                            ConstantPointerNull::get(PtrType), BB);
44   ReturnInst::Create(C, nullptr, BB);
45 
46   auto *RootMD = MD.createTBAARoot("Simple C/C++ TBAA");
47   auto *MD1 = MD.createTBAANode("omnipotent char", RootMD);
48   auto *MD2 = MD.createTBAANode("int", MD1);
49   SI->setMetadata(LLVMContext::MD_tbaa, MD2);
50 
51   SmallVector<char, 0> ErrorMsg;
52   raw_svector_ostream Outs(ErrorMsg);
53 
54   StringRef ExpectedFailureMsg(
55       "Old-style TBAA is no longer allowed, use struct-path TBAA instead");
56 
57   EXPECT_TRUE(verifyFunction(*F, &Outs));
58   EXPECT_TRUE(StringRef(ErrorMsg.begin(), ErrorMsg.size())
59                   .startswith(ExpectedFailureMsg));
60 }
61 
62 } // end anonymous namspace
63 } // end llvm namespace
64