1//===- SpecialCaseList.cpp - Unit tests for SpecialCaseList ---------------===//
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/IR/Function.h"
11#include "llvm/IR/LLVMContext.h"
12#include "llvm/IR/Module.h"
13#include "llvm/Support/MemoryBuffer.h"
14#include "llvm/Transforms/Utils/SpecialCaseList.h"
15#include "gtest/gtest.h"
16
17using namespace llvm;
18
19namespace {
20
21class SpecialCaseListTest : public ::testing::Test {
22protected:
23  Function *makeFunction(StringRef Name, Module &M) {
24    return Function::Create(FunctionType::get(Type::getVoidTy(Ctx), false),
25                            GlobalValue::ExternalLinkage,
26                            Name,
27                            &M);
28  }
29
30  GlobalVariable *makeGlobal(StringRef Name, StringRef StructName, Module &M) {
31    StructType *ST =
32        StructType::create(StructName, Type::getInt32Ty(Ctx), (Type*)0);
33    return new GlobalVariable(
34        M, ST, false, GlobalValue::ExternalLinkage, 0, Name);
35  }
36
37  SpecialCaseList *makeSpecialCaseList(StringRef List) {
38    OwningPtr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(List));
39    return new SpecialCaseList(MB.get());
40  }
41
42  LLVMContext Ctx;
43};
44
45TEST_F(SpecialCaseListTest, ModuleIsIn) {
46  Module M("hello", Ctx);
47  Function *F = makeFunction("foo", M);
48  GlobalVariable *GV = makeGlobal("bar", "t", M);
49
50  OwningPtr<SpecialCaseList> SCL(makeSpecialCaseList("# This is a comment.\n"
51                                                     "\n"
52                                                     "src:hello\n"));
53  EXPECT_TRUE(SCL->isIn(M));
54  EXPECT_TRUE(SCL->isIn(*F));
55  EXPECT_TRUE(SCL->isIn(*GV));
56
57  SCL.reset(makeSpecialCaseList("src:he*o\n"));
58  EXPECT_TRUE(SCL->isIn(M));
59  EXPECT_TRUE(SCL->isIn(*F));
60  EXPECT_TRUE(SCL->isIn(*GV));
61
62  SCL.reset(makeSpecialCaseList("src:hi\n"));
63  EXPECT_FALSE(SCL->isIn(M));
64  EXPECT_FALSE(SCL->isIn(*F));
65  EXPECT_FALSE(SCL->isIn(*GV));
66}
67
68TEST_F(SpecialCaseListTest, FunctionIsIn) {
69  Module M("hello", Ctx);
70  Function *Foo = makeFunction("foo", M);
71  Function *Bar = makeFunction("bar", M);
72
73  OwningPtr<SpecialCaseList> SCL(makeSpecialCaseList("fun:foo\n"));
74  EXPECT_TRUE(SCL->isIn(*Foo));
75  EXPECT_FALSE(SCL->isIn(*Bar));
76
77  SCL.reset(makeSpecialCaseList("fun:b*\n"));
78  EXPECT_FALSE(SCL->isIn(*Foo));
79  EXPECT_TRUE(SCL->isIn(*Bar));
80
81  SCL.reset(makeSpecialCaseList("fun:f*\n"
82                                "fun:bar\n"));
83  EXPECT_TRUE(SCL->isIn(*Foo));
84  EXPECT_TRUE(SCL->isIn(*Bar));
85
86  SCL.reset(makeSpecialCaseList("fun:foo=functional\n"));
87  EXPECT_TRUE(SCL->isIn(*Foo, "functional"));
88  StringRef Category;
89  EXPECT_TRUE(SCL->findCategory(*Foo, Category));
90  EXPECT_EQ("functional", Category);
91  EXPECT_FALSE(SCL->isIn(*Bar, "functional"));
92}
93
94TEST_F(SpecialCaseListTest, GlobalIsIn) {
95  Module M("hello", Ctx);
96  GlobalVariable *Foo = makeGlobal("foo", "t1", M);
97  GlobalVariable *Bar = makeGlobal("bar", "t2", M);
98
99  OwningPtr<SpecialCaseList> SCL(makeSpecialCaseList("global:foo\n"));
100  EXPECT_TRUE(SCL->isIn(*Foo));
101  EXPECT_FALSE(SCL->isIn(*Bar));
102  EXPECT_FALSE(SCL->isIn(*Foo, "init"));
103  EXPECT_FALSE(SCL->isIn(*Bar, "init"));
104
105  SCL.reset(makeSpecialCaseList("global:foo=init\n"));
106  EXPECT_FALSE(SCL->isIn(*Foo));
107  EXPECT_FALSE(SCL->isIn(*Bar));
108  EXPECT_TRUE(SCL->isIn(*Foo, "init"));
109  EXPECT_FALSE(SCL->isIn(*Bar, "init"));
110
111  SCL.reset(makeSpecialCaseList("global-init:foo\n"));
112  EXPECT_FALSE(SCL->isIn(*Foo));
113  EXPECT_FALSE(SCL->isIn(*Bar));
114  EXPECT_TRUE(SCL->isIn(*Foo, "init"));
115  EXPECT_FALSE(SCL->isIn(*Bar, "init"));
116
117  SCL.reset(makeSpecialCaseList("type:t2=init\n"));
118  EXPECT_FALSE(SCL->isIn(*Foo));
119  EXPECT_FALSE(SCL->isIn(*Bar));
120  EXPECT_FALSE(SCL->isIn(*Foo, "init"));
121  EXPECT_TRUE(SCL->isIn(*Bar, "init"));
122
123  SCL.reset(makeSpecialCaseList("global-init-type:t2\n"));
124  EXPECT_FALSE(SCL->isIn(*Foo));
125  EXPECT_FALSE(SCL->isIn(*Bar));
126  EXPECT_FALSE(SCL->isIn(*Foo, "init"));
127  EXPECT_TRUE(SCL->isIn(*Bar, "init"));
128
129  SCL.reset(makeSpecialCaseList("src:hello=init\n"));
130  EXPECT_FALSE(SCL->isIn(*Foo));
131  EXPECT_FALSE(SCL->isIn(*Bar));
132  EXPECT_TRUE(SCL->isIn(*Foo, "init"));
133  EXPECT_TRUE(SCL->isIn(*Bar, "init"));
134
135  SCL.reset(makeSpecialCaseList("global-init-src:hello\n"));
136  EXPECT_FALSE(SCL->isIn(*Foo));
137  EXPECT_FALSE(SCL->isIn(*Bar));
138  EXPECT_TRUE(SCL->isIn(*Foo, "init"));
139  EXPECT_TRUE(SCL->isIn(*Bar, "init"));
140}
141
142TEST_F(SpecialCaseListTest, Substring) {
143  Module M("othello", Ctx);
144  Function *F = makeFunction("tomfoolery", M);
145  GlobalVariable *GV = makeGlobal("bartender", "t", M);
146
147  OwningPtr<SpecialCaseList> SCL(makeSpecialCaseList("src:hello\n"
148                                                     "fun:foo\n"
149                                                     "global:bar\n"));
150  EXPECT_FALSE(SCL->isIn(M));
151  EXPECT_FALSE(SCL->isIn(*F));
152  EXPECT_FALSE(SCL->isIn(*GV));
153
154  SCL.reset(makeSpecialCaseList("fun:*foo*\n"));
155  EXPECT_TRUE(SCL->isIn(*F));
156}
157
158}
159