CPlusPlusLanguageTest.cpp
9.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//===-- CPlusPlusLanguageTest.cpp -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
#include "Plugins/Language/CPlusPlus/CPlusPlusNameParser.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace lldb_private;
TEST(CPlusPlusLanguage, MethodNameParsing) {
struct TestCase {
std::string input;
std::string context, basename, arguments, qualifiers, scope_qualified_name;
};
TestCase test_cases[] = {
{"main(int, char *[]) ", "", "main", "(int, char *[])", "", "main"},
{"foo::bar(baz) const", "foo", "bar", "(baz)", "const", "foo::bar"},
{"foo::~bar(baz)", "foo", "~bar", "(baz)", "", "foo::~bar"},
{"a::b::c::d(e,f)", "a::b::c", "d", "(e,f)", "", "a::b::c::d"},
{"void f(int)", "", "f", "(int)", "", "f"},
// Operators
{"std::basic_ostream<char, std::char_traits<char> >& "
"std::operator<<<std::char_traits<char> >"
"(std::basic_ostream<char, std::char_traits<char> >&, char const*)",
"std", "operator<<<std::char_traits<char> >",
"(std::basic_ostream<char, std::char_traits<char> >&, char const*)", "",
"std::operator<<<std::char_traits<char> >"},
{"operator delete[](void*, clang::ASTContext const&, unsigned long)", "",
"operator delete[]", "(void*, clang::ASTContext const&, unsigned long)",
"", "operator delete[]"},
{"llvm::Optional<clang::PostInitializer>::operator bool() const",
"llvm::Optional<clang::PostInitializer>", "operator bool", "()", "const",
"llvm::Optional<clang::PostInitializer>::operator bool"},
{"(anonymous namespace)::FactManager::operator[](unsigned short)",
"(anonymous namespace)::FactManager", "operator[]", "(unsigned short)",
"", "(anonymous namespace)::FactManager::operator[]"},
{"const int& std::map<int, pair<short, int>>::operator[](short) const",
"std::map<int, pair<short, int>>", "operator[]", "(short)", "const",
"std::map<int, pair<short, int>>::operator[]"},
{"CompareInsn::operator()(llvm::StringRef, InsnMatchEntry const&)",
"CompareInsn", "operator()", "(llvm::StringRef, InsnMatchEntry const&)",
"", "CompareInsn::operator()"},
{"llvm::Optional<llvm::MCFixupKind>::operator*() const &",
"llvm::Optional<llvm::MCFixupKind>", "operator*", "()", "const &",
"llvm::Optional<llvm::MCFixupKind>::operator*"},
// Internal classes
{"operator<<(Cls, Cls)::Subclass::function()",
"operator<<(Cls, Cls)::Subclass", "function", "()", "",
"operator<<(Cls, Cls)::Subclass::function"},
{"SAEC::checkFunction(context&) const::CallBack::CallBack(int)",
"SAEC::checkFunction(context&) const::CallBack", "CallBack", "(int)", "",
"SAEC::checkFunction(context&) const::CallBack::CallBack"},
// Anonymous namespace
{"XX::(anonymous namespace)::anon_class::anon_func() const",
"XX::(anonymous namespace)::anon_class", "anon_func", "()", "const",
"XX::(anonymous namespace)::anon_class::anon_func"},
// Lambda
{"main::{lambda()#1}::operator()() const::{lambda()#1}::operator()() const",
"main::{lambda()#1}::operator()() const::{lambda()#1}", "operator()", "()", "const",
"main::{lambda()#1}::operator()() const::{lambda()#1}::operator()"},
// Function pointers
{"string (*f(vector<int>&&))(float)", "", "f", "(vector<int>&&)", "",
"f"},
{"void (*&std::_Any_data::_M_access<void (*)()>())()", "std::_Any_data",
"_M_access<void (*)()>", "()", "",
"std::_Any_data::_M_access<void (*)()>"},
{"void (*(*(*(*(*(*(*(* const&func1(int))())())())())())())())()", "",
"func1", "(int)", "", "func1"},
// Decltype
{"decltype(nullptr)&& std::forward<decltype(nullptr)>"
"(std::remove_reference<decltype(nullptr)>::type&)",
"std", "forward<decltype(nullptr)>",
"(std::remove_reference<decltype(nullptr)>::type&)", "",
"std::forward<decltype(nullptr)>"},
// Templates
{"void llvm::PM<llvm::Module, llvm::AM<llvm::Module>>::"
"addPass<llvm::VP>(llvm::VP)",
"llvm::PM<llvm::Module, llvm::AM<llvm::Module>>", "addPass<llvm::VP>",
"(llvm::VP)", "",
"llvm::PM<llvm::Module, llvm::AM<llvm::Module>>::"
"addPass<llvm::VP>"},
{"void std::vector<Class, std::allocator<Class> >"
"::_M_emplace_back_aux<Class const&>(Class const&)",
"std::vector<Class, std::allocator<Class> >",
"_M_emplace_back_aux<Class const&>", "(Class const&)", "",
"std::vector<Class, std::allocator<Class> >::"
"_M_emplace_back_aux<Class const&>"},
{"unsigned long llvm::countTrailingOnes<unsigned int>"
"(unsigned int, llvm::ZeroBehavior)",
"llvm", "countTrailingOnes<unsigned int>",
"(unsigned int, llvm::ZeroBehavior)", "",
"llvm::countTrailingOnes<unsigned int>"},
{"std::enable_if<(10u)<(64), bool>::type llvm::isUInt<10u>(unsigned "
"long)",
"llvm", "isUInt<10u>", "(unsigned long)", "", "llvm::isUInt<10u>"},
{"f<A<operator<(X,Y)::Subclass>, sizeof(B)<sizeof(C)>()", "",
"f<A<operator<(X,Y)::Subclass>, sizeof(B)<sizeof(C)>", "()", "",
"f<A<operator<(X,Y)::Subclass>, sizeof(B)<sizeof(C)>"}};
for (const auto &test : test_cases) {
CPlusPlusLanguage::MethodName method(ConstString(test.input));
EXPECT_TRUE(method.IsValid()) << test.input;
if (method.IsValid()) {
EXPECT_EQ(test.context, method.GetContext().str());
EXPECT_EQ(test.basename, method.GetBasename().str());
EXPECT_EQ(test.arguments, method.GetArguments().str());
EXPECT_EQ(test.qualifiers, method.GetQualifiers().str());
EXPECT_EQ(test.scope_qualified_name, method.GetScopeQualifiedName());
}
}
}
TEST(CPlusPlusLanguage, ExtractContextAndIdentifier) {
struct TestCase {
std::string input;
std::string context, basename;
};
TestCase test_cases[] = {
{"main", "", "main"},
{"main ", "", "main"},
{"foo01::bar", "foo01", "bar"},
{"foo::~bar", "foo", "~bar"},
{"std::vector<int>::push_back", "std::vector<int>", "push_back"},
{"operator<<(Cls, Cls)::Subclass::function",
"operator<<(Cls, Cls)::Subclass", "function"},
{"std::vector<Class, std::allocator<Class>>"
"::_M_emplace_back_aux<Class const&>",
"std::vector<Class, std::allocator<Class>>",
"_M_emplace_back_aux<Class const&>"},
{"`anonymous namespace'::foo", "`anonymous namespace'", "foo"},
{"`operator<<A>'::`2'::B<0>::operator>", "`operator<<A>'::`2'::B<0>",
"operator>"},
{"`anonymous namespace'::S::<<::__l2::Foo",
"`anonymous namespace'::S::<<::__l2", "Foo"},
// These cases are idiosyncratic in how clang generates debug info for
// names when we have template parameters. They are not valid C++ names
// but if we fix this we need to support them for older compilers.
{"A::operator><A::B>", "A", "operator><A::B>"},
{"operator><A::B>", "", "operator><A::B>"},
{"A::operator<<A::B>", "A", "operator<<A::B>"},
{"operator<<A::B>", "", "operator<<A::B>"},
{"A::operator<<<A::B>", "A", "operator<<<A::B>"},
{"operator<<<A::B>", "", "operator<<<A::B>"},
};
llvm::StringRef context, basename;
for (const auto &test : test_cases) {
EXPECT_TRUE(CPlusPlusLanguage::ExtractContextAndIdentifier(
test.input.c_str(), context, basename));
EXPECT_EQ(test.context, context.str());
EXPECT_EQ(test.basename, basename.str());
}
EXPECT_FALSE(CPlusPlusLanguage::ExtractContextAndIdentifier("void", context,
basename));
EXPECT_FALSE(
CPlusPlusLanguage::ExtractContextAndIdentifier("321", context, basename));
EXPECT_FALSE(
CPlusPlusLanguage::ExtractContextAndIdentifier("", context, basename));
EXPECT_FALSE(CPlusPlusLanguage::ExtractContextAndIdentifier(
"selector:", context, basename));
EXPECT_FALSE(CPlusPlusLanguage::ExtractContextAndIdentifier(
"selector:otherField:", context, basename));
EXPECT_FALSE(CPlusPlusLanguage::ExtractContextAndIdentifier(
"abc::", context, basename));
EXPECT_FALSE(CPlusPlusLanguage::ExtractContextAndIdentifier(
"f<A<B><C>>", context, basename));
// We expect these cases to fail until we turn on C++2a
EXPECT_FALSE(CPlusPlusLanguage::ExtractContextAndIdentifier(
"A::operator<=><A::B>", context, basename));
EXPECT_FALSE(CPlusPlusLanguage::ExtractContextAndIdentifier(
"operator<=><A::B>", context, basename));
}
static std::set<std::string> FindAlternate(llvm::StringRef Name) {
std::set<ConstString> Results;
uint32_t Count = CPlusPlusLanguage::FindAlternateFunctionManglings(
ConstString(Name), Results);
EXPECT_EQ(Count, Results.size());
std::set<std::string> Strings;
for (ConstString Str : Results)
Strings.insert(std::string(Str.GetStringRef()));
return Strings;
}
TEST(CPlusPlusLanguage, FindAlternateFunctionManglings) {
using namespace testing;
EXPECT_THAT(FindAlternate("_ZN1A1fEv"),
UnorderedElementsAre("_ZNK1A1fEv", "_ZLN1A1fEv"));
EXPECT_THAT(FindAlternate("_ZN1A1fEa"), Contains("_ZN1A1fEc"));
EXPECT_THAT(FindAlternate("_ZN1A1fEx"), Contains("_ZN1A1fEl"));
EXPECT_THAT(FindAlternate("_ZN1A1fEy"), Contains("_ZN1A1fEm"));
EXPECT_THAT(FindAlternate("_ZN1A1fEai"), Contains("_ZN1A1fEci"));
EXPECT_THAT(FindAlternate("_ZN1AC1Ev"), Contains("_ZN1AC2Ev"));
EXPECT_THAT(FindAlternate("_ZN1AD1Ev"), Contains("_ZN1AD2Ev"));
EXPECT_THAT(FindAlternate("_bogus"), IsEmpty());
}
TEST(CPlusPlusLanguage, CPlusPlusNameParser) {
// Don't crash.
CPlusPlusNameParser((const char *)nullptr);
}