Skip to content

Commit 50e21bd

Browse files
committed
LLVMInstructionList: Add containsInstruction() method
1 parent 073d30a commit 50e21bd

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/engine/internal/llvm/llvminstructionlist.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ LLVMInstruction *LLVMInstructionList::last() const
1717
return m_last.get();
1818
}
1919

20+
bool LLVMInstructionList::containsInstruction(LLVMInstruction *ins) const
21+
{
22+
LLVMInstruction *ptr = m_first.get();
23+
24+
while (ptr) {
25+
if (ptr == ins)
26+
return true;
27+
28+
ptr = ptr->next;
29+
}
30+
31+
return false;
32+
}
33+
2034
void LLVMInstructionList::addInstruction(std::shared_ptr<LLVMInstruction> ins)
2135
{
2236
if (!(m_first && m_last)) {

src/engine/internal/llvm/llvminstructionlist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class LLVMInstructionList
1818
LLVMInstruction *first() const;
1919
LLVMInstruction *last() const;
2020

21+
bool containsInstruction(LLVMInstruction *ins) const;
2122
void addInstruction(std::shared_ptr<LLVMInstruction> ins);
2223

2324
private:

test/llvm/llvminstructionlist_test.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,99 @@ TEST(LLVMInstructionListTest, EmptyList_Last)
1717
ASSERT_EQ(list.last(), nullptr);
1818
}
1919

20+
TEST(LLVMInstructionListTest, EmptyList_ContainsNull)
21+
{
22+
LLVMInstructionList list;
23+
ASSERT_FALSE(list.containsInstruction(nullptr));
24+
}
25+
26+
TEST(LLVMInstructionListTest, EmptyList_Contains)
27+
{
28+
LLVMInstructionList list;
29+
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
30+
ASSERT_FALSE(list.containsInstruction(ins.get()));
31+
}
32+
33+
TEST(LLVMInstructionListTest, SingleInstructionList_ContainsExistent)
34+
{
35+
LLVMInstructionList list;
36+
37+
auto ins1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
38+
list.addInstruction(ins1);
39+
40+
ASSERT_TRUE(list.containsInstruction(ins1.get()));
41+
}
42+
43+
TEST(LLVMInstructionListTest, SingleInstructionList_ContainsNonExistent)
44+
{
45+
LLVMInstructionList list;
46+
47+
auto ins1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
48+
list.addInstruction(ins1);
49+
50+
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
51+
ASSERT_FALSE(list.containsInstruction(ins.get()));
52+
}
53+
54+
TEST(LLVMInstructionListTest, SingleInstructionList_ContainsNull)
55+
{
56+
LLVMInstructionList list;
57+
58+
auto ins1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
59+
list.addInstruction(ins1);
60+
61+
ASSERT_FALSE(list.containsInstruction(nullptr));
62+
}
63+
64+
TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsExistent)
65+
{
66+
LLVMInstructionList list;
67+
68+
auto ins1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
69+
list.addInstruction(ins1);
70+
71+
auto ins2 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
72+
list.addInstruction(ins2);
73+
74+
auto ins3 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
75+
list.addInstruction(ins3);
76+
77+
ASSERT_TRUE(list.containsInstruction(ins2.get()));
78+
}
79+
80+
TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsNonExistent)
81+
{
82+
LLVMInstructionList list;
83+
84+
auto ins1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
85+
list.addInstruction(ins1);
86+
87+
auto ins2 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
88+
list.addInstruction(ins2);
89+
90+
auto ins3 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
91+
list.addInstruction(ins3);
92+
93+
auto ins = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
94+
ASSERT_FALSE(list.containsInstruction(ins.get()));
95+
}
96+
97+
TEST(LLVMInstructionListTest, MultipleInstructionList_ContainsNull)
98+
{
99+
LLVMInstructionList list;
100+
101+
auto ins1 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
102+
list.addInstruction(ins1);
103+
104+
auto ins2 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
105+
list.addInstruction(ins2);
106+
107+
auto ins3 = std::make_shared<LLVMInstruction>(LLVMInstruction::Type::FunctionCall, nullptr, false);
108+
list.addInstruction(ins3);
109+
110+
ASSERT_FALSE(list.containsInstruction(nullptr));
111+
}
112+
20113
TEST(LLVMInstructionListTest, AddSingleInstruction_First)
21114
{
22115
LLVMInstructionList list;

0 commit comments

Comments
 (0)