-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathidentity_string.cpp
More file actions
222 lines (188 loc) · 7.31 KB
/
identity_string.cpp
File metadata and controls
222 lines (188 loc) · 7.31 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
217
218
219
220
221
222
// semmle-extractor-options: -std=c++17
template<typename T>
void check_type(const char* expected);
template<typename TFunc>
void check_func(TFunc func, const char* expected);
template<typename TVar>
void check_var(TVar var, const char* expected);
struct S
{
enum NestedEnum
{
Blah,
Bluh
};
int i;
float f;
};
struct T
{
bool b;
};
enum E
{
One,
Two,
Three
};
void checks()
{
// Primitive types
check_type<char>("char");
check_type<unsigned char>("unsigned char");
check_type<signed char>("signed char");
check_type<signed short>("short");
check_type<short>("short");
check_type<unsigned short>("unsigned short");
check_type<int>("int");
check_type<signed int>("int");
check_type<unsigned int>("unsigned int");
check_type<long>("long");
check_type<signed long>("long");
check_type<unsigned long>("unsigned long");
check_type<long long>("long long");
check_type<signed long long>("long long");
check_type<unsigned long long>("unsigned long long");
check_type<float>("float");
check_type<double>("double");
check_type<long double>("long double");
check_type<bool>("bool");
check_type<wchar_t>("wchar_t");
check_type<char16_t>("char16_t");
check_type<char32_t>("char32_t");
check_type<void>("void");
check_type<decltype(nullptr)>("decltype(nullptr)");
check_type<const char>("char const");
check_type<volatile short>("short volatile");
check_type<const volatile int>("int const volatile");
check_type<volatile long const>("long const volatile");
// Pointers and references
check_type<int*>("int*");
check_type<int**>("int**");
check_type<int&>("int&");
check_type<int&&>("int&&");
check_type<int**&&>("int**&&");
// Qualifiers
check_type<const int*>("int const*");
check_type<int* const>("int* const");
check_type<volatile float const* volatile>("float const volatile* volatile");
check_type<volatile char32_t &&>("char32_t volatile&&");
// Arrays
check_type<int[]>("int[]");
check_type<int[10]>("int[10]");
check_type<int[5][10]>("int[5][10]");
check_type<int[][7]>("int[][7]");
// Functions
check_type<int(float)>("int(float)");
check_type<int(*)(float, double)>("int(*)(float, double)");
check_type<int(**)(float, double, ...)>("int(**)(float, double, ...)");
check_type<int(&)(float, double)>("int(&)(float, double)");
check_type<int(*&)(float, double)>("int(*&)(float, double)");
check_type<auto(float, double) -> int>("int(float, double)");
check_type<auto (*)(float) -> auto (*)(double) -> int>("int(*(*)(float))(double)");
check_type<int(*(*)(float))(double)>("int(*(*)(float))(double)");
check_type<int(*(float))(double)>("int(*(float))(double)");
// Pointers-to-member
check_type<int S::*>("int S::*");
check_type<const int S::* volatile>("int const S::* volatile");
check_type<float* (*)(double)>("float*(*)(double)");
check_type<float* (S::*)(double)>("float* (S::*)(double)");
check_type<float* (S::*)(double) const>("float* (S::*)(double) const");
typedef int (S::* PMF_S)(float) volatile;
typedef PMF_S(T::* PMF_T)(double) const;
check_type<PMF_T>("int (S::* (T::*)(double) const)(float) volatile");
check_type<int (S::*(T::*)(double) const)(float) volatile>("int (S::* (T::*)(double) const)(float) volatile");
check_type<int S::* T::*>("int S::* T::*");
check_type<const bool(S::* volatile)[10]>("bool const (S::* volatile)[10]");
check_type<const bool S::* volatile[10]>("bool const S::* volatile[10]");
// Complicated stuff
typedef int Int10[10];
check_type<int(*)[10]>("int(*)[10]");
check_type<Int10*>("int(*)[10]");
typedef int FuncA(float, double);
typedef FuncA* FuncB(wchar_t);
check_type<FuncB*&>("int(*(*&)(wchar_t))(float, double)");
check_type<int(*(*&)(wchar_t))(float, double)>("int(*(*&)(wchar_t))(float, double)");
typedef const int CI;
typedef volatile CI VCI;
typedef volatile int VI;
typedef const VI CVI;
check_type<CI>("int const");
check_type<VCI>("int const volatile");
check_type<VI>("int volatile");
check_type<CVI>("int const volatile");
check_type<const CI>("int const");
typedef int AI[10];
typedef const AI CAI;
check_type<AI>("int[10]");
check_type<CAI>("int const[10]");
check_type<int const[10]>("int const[10]");
check_type<E>("E");
check_type<S::NestedEnum>("S::NestedEnum");
}
int globalVariable;
int globalFunc(float x);
int overloadedGlobalFunc(float x);
float overloadedGlobalFunc(int x);
template<typename T>
T variableTemplate;
auto vt = &variableTemplate<bool>;
namespace Outer {
namespace Inner {
template<typename T, typename U>
int globalFunctionTemplate(T t, U u) {
class LocalClass {
public:
T x;
void MemberFuncOfLocalClass() { }
};
check_func(&LocalClass::MemberFuncOfLocalClass, "void (int Outer::Inner::globalFunctionTemplate<long, double>(long, double))::LocalClass::MemberFuncOfLocalClass()");
{
class LocalClassInBlock {
public:
U x;
void MemberFuncOfLocalClassInBlock() { }
};
check_func(&LocalClassInBlock::MemberFuncOfLocalClassInBlock, "void (int Outer::Inner::globalFunctionTemplate<long, double>(long, double))::LocalClassInBlock::MemberFuncOfLocalClassInBlock()");
}
auto l = [](int x) {
struct LocalClassInLambda {
void MemberFuncOfLocalClassInLambda() { }
};
check_func(&LocalClassInLambda::MemberFuncOfLocalClassInLambda, "void (int (int Outer::Inner::globalFunctionTemplate<long, double>(long, double))::(lambda [] type at line ?, col. ?)::operator()(int) const)::LocalClassInLambda::MemberFuncOfLocalClassInLambda()");
return x;
};
return 0;
}
}
}
struct GlobalStruct {
int f;
static float s[156];
GlobalStruct MemberFunc(wchar_t);
const GlobalStruct ConstMemberFunc(char) const;
GlobalStruct volatile VolatileMemberFunc(char16_t) volatile;
volatile GlobalStruct const ConstVolatileMemberFunc(char32_t) volatile const;
static bool& StaticMemberFunc(float);
};
template<typename T, typename U>
struct ClassTemplate {
T f;
U g;
};
void sym_checks() {
check_func(globalFunc, "int globalFunc(float)");
check_func(static_cast<int (&)(float)>(overloadedGlobalFunc), "int overloadedGlobalFunc(float)");
check_func(static_cast<float (&)(int)>(overloadedGlobalFunc), "float overloadedGlobalFunc(int)");
check_func(Outer::Inner::globalFunctionTemplate<long, double>, "int Outer::Inner::globalFunctionTemplate<long, double>(long, double)");
check_func(GlobalStruct::StaticMemberFunc, "bool& GlobalStruct::StaticMemberFunc(float)");
check_func(&GlobalStruct::MemberFunc, "GlobalStruct GlobalStruct::MemberFunc(wchar_t)");
check_func(&GlobalStruct::ConstMemberFunc, "GlobalStruct const GlobalStruct::ConstMemberFunc(char) const");
check_func(&GlobalStruct::VolatileMemberFunc, "GlobalStruct volatile GlobalStruct::VolatileMemberFunc(char16_t) volatile");
check_func(&GlobalStruct::ConstVolatileMemberFunc, "GlobalStruct const volatile GlobalStruct::ConstVolatileMemberFunc(char32_t) const volatile");
check_var(globalVariable, "int globalVariable");
check_var(variableTemplate<long double>, "long double variableTemplate<long double>");
check_var(&GlobalStruct::f, "int GlobalStruct::f");
check_var(GlobalStruct::s, "float GlobalStruct::s[156]");
check_var(&ClassTemplate<short, signed char>::g, "signed char ClassTemplate<short, signed char>::g");
}