Skip to content

Commit bcce36f

Browse files
committed
Add some test examples
1 parent 1e61b09 commit bcce36f

File tree

2 files changed

+87
-31
lines changed

2 files changed

+87
-31
lines changed

lib/Interpreter/CXCppInterOp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ clang_interpreter_getFunctionAddressFromMangledName(CXInterpreter I,
196196
return nullptr;
197197
}
198198

199-
static inline CXQualType
200-
makeCXQualType(const CXInterpreterImpl* I, const clang::QualType Ty,
201-
const CXTypeKind K = CXType_Unexposed) {
199+
static inline CXQualType makeCXQualType(const CXInterpreterImpl* I,
200+
const clang::QualType Ty,
201+
const CXTypeKind K = CXType_Unexposed) {
202202
assert(I && "Invalid interpreter");
203203
return CXQualType{K, Ty.getAsOpaquePtr(), static_cast<const void*>(I)};
204204
}

unittests/CppInterOp/EnumReflectionTest.cpp

Lines changed: 84 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "Utils.h"
22

33
#include "clang/AST/ASTContext.h"
4-
#include "clang/Interpreter/CppInterOp.h"
54
#include "clang/Frontend/CompilerInstance.h"
5+
#include "clang/Interpreter/CppInterOp.h"
66
#include "clang/Sema/Sema.h"
7+
#include "clang-c/CXCppInterOp.h"
78

89
#include "gtest/gtest.h"
910

@@ -12,7 +13,7 @@ using namespace llvm;
1213
using namespace clang;
1314

1415
TEST(ScopeReflectionTest, IsEnumScope) {
15-
std::vector<Decl *> Decls, SubDecls;
16+
std::vector<Decl*> Decls, SubDecls;
1617
std::string code = R"(
1718
enum Switch {
1819
OFF,
@@ -31,10 +32,22 @@ TEST(ScopeReflectionTest, IsEnumScope) {
3132
EXPECT_FALSE(Cpp::IsEnumScope(Decls[2]));
3233
EXPECT_FALSE(Cpp::IsEnumScope(SubDecls[0]));
3334
EXPECT_FALSE(Cpp::IsEnumScope(SubDecls[1]));
35+
36+
// C API
37+
auto I = Cpp::GetInterpreter();
38+
EXPECT_TRUE(clang_scope_isEnumScope(CXScope{CXScope_Unexposed, Decls[0], I}));
39+
EXPECT_FALSE(
40+
clang_scope_isEnumScope(CXScope{CXScope_Unexposed, Decls[1], I}));
41+
EXPECT_FALSE(
42+
clang_scope_isEnumScope(CXScope{CXScope_Unexposed, Decls[2], I}));
43+
EXPECT_FALSE(
44+
clang_scope_isEnumScope(CXScope{CXScope_Unexposed, SubDecls[0], I}));
45+
EXPECT_FALSE(
46+
clang_scope_isEnumScope(CXScope{CXScope_Unexposed, SubDecls[1], I}));
3447
}
3548

3649
TEST(ScopeReflectionTest, IsEnumConstant) {
37-
std::vector<Decl *> Decls, SubDecls;
50+
std::vector<Decl*> Decls, SubDecls;
3851
std::string code = R"(
3952
enum Switch {
4053
OFF,
@@ -53,11 +66,24 @@ TEST(ScopeReflectionTest, IsEnumConstant) {
5366
EXPECT_FALSE(Cpp::IsEnumConstant(Decls[2]));
5467
EXPECT_TRUE(Cpp::IsEnumConstant(SubDecls[0]));
5568
EXPECT_TRUE(Cpp::IsEnumConstant(SubDecls[1]));
69+
70+
// C API
71+
auto I = Cpp::GetInterpreter();
72+
EXPECT_FALSE(
73+
clang_scope_isEnumConstant(CXScope{CXScope_Unexposed, Decls[0], I}));
74+
EXPECT_FALSE(
75+
clang_scope_isEnumConstant(CXScope{CXScope_Unexposed, Decls[1], I}));
76+
EXPECT_FALSE(
77+
clang_scope_isEnumConstant(CXScope{CXScope_Unexposed, Decls[2], I}));
78+
EXPECT_TRUE(
79+
clang_scope_isEnumConstant(CXScope{CXScope_Unexposed, SubDecls[0], I}));
80+
EXPECT_TRUE(
81+
clang_scope_isEnumConstant(CXScope{CXScope_Unexposed, SubDecls[1], I}));
5682
}
5783

5884
TEST(EnumReflectionTest, IsEnumType) {
59-
std::vector<Decl *> Decls;
60-
std::string code = R"(
85+
std::vector<Decl*> Decls;
86+
std::string code = R"(
6187
enum class E {
6288
a,
6389
b
@@ -81,10 +107,25 @@ TEST(EnumReflectionTest, IsEnumType) {
81107
EXPECT_TRUE(Cpp::IsEnumType(Cpp::GetVariableType(Decls[3])));
82108
EXPECT_TRUE(Cpp::IsEnumType(Cpp::GetVariableType(Decls[4])));
83109
EXPECT_TRUE(Cpp::IsEnumType(Cpp::GetVariableType(Decls[5])));
110+
111+
// C API
112+
auto I = Cpp::GetInterpreter();
113+
auto VarTy2 =
114+
clang_scope_getVariableType(CXScope{CXScope_Unexposed, Decls[2], I});
115+
auto VarTy3 =
116+
clang_scope_getVariableType(CXScope{CXScope_Unexposed, Decls[3], I});
117+
auto VarTy4 =
118+
clang_scope_getVariableType(CXScope{CXScope_Unexposed, Decls[4], I});
119+
auto VarTy5 =
120+
clang_scope_getVariableType(CXScope{CXScope_Unexposed, Decls[5], I});
121+
EXPECT_TRUE(clang_qualtype_isEnumType(VarTy2));
122+
EXPECT_TRUE(clang_qualtype_isEnumType(VarTy3));
123+
EXPECT_TRUE(clang_qualtype_isEnumType(VarTy4));
124+
EXPECT_TRUE(clang_qualtype_isEnumType(VarTy5));
84125
}
85126

86127
TEST(EnumReflectionTest, GetIntegerTypeFromEnumScope) {
87-
std::vector<Decl *> Decls;
128+
std::vector<Decl*> Decls;
88129
std::string code = R"(
89130
enum Switch : bool {
90131
OFF,
@@ -120,21 +161,27 @@ TEST(EnumReflectionTest, GetIntegerTypeFromEnumScope) {
120161

121162
GetAllTopLevelDecls(code, Decls);
122163

123-
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[0])), "bool");
124-
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[1])), "char");
125-
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[2])), "int");
126-
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[3])), "long long");
164+
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[0])),
165+
"bool");
166+
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[1])),
167+
"char");
168+
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[2])),
169+
"int");
170+
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[3])),
171+
"long long");
127172
#ifdef _WIN32
128173
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[4])),
129174
"int");
130175
#else
131-
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[4])), "unsigned int");
176+
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[4])),
177+
"unsigned int");
132178
#endif
133-
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[5])),"NULL TYPE");
179+
EXPECT_EQ(Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumScope(Decls[5])),
180+
"NULL TYPE");
134181
}
135182

136183
TEST(EnumReflectionTest, GetIntegerTypeFromEnumType) {
137-
std::vector<Decl *> Decls;
184+
std::vector<Decl*> Decls;
138185
std::string code = R"(
139186
enum Switch : bool {
140187
OFF,
@@ -176,11 +223,13 @@ TEST(EnumReflectionTest, GetIntegerTypeFromEnumType) {
176223

177224
GetAllTopLevelDecls(code, Decls);
178225

179-
auto get_int_type_from_enum_var = [](Decl *D) {
180-
return Cpp::GetTypeAsString(Cpp::GetIntegerTypeFromEnumType(Cpp::GetVariableType(D)));
226+
auto get_int_type_from_enum_var = [](Decl* D) {
227+
return Cpp::GetTypeAsString(
228+
Cpp::GetIntegerTypeFromEnumType(Cpp::GetVariableType(D)));
181229
};
182230

183-
EXPECT_EQ(get_int_type_from_enum_var(Decls[5]), "NULL TYPE"); // When a nullptr is returned by GetVariableType()
231+
EXPECT_EQ(get_int_type_from_enum_var(Decls[5]),
232+
"NULL TYPE"); // When a nullptr is returned by GetVariableType()
184233
EXPECT_EQ(get_int_type_from_enum_var(Decls[6]), "bool");
185234
EXPECT_EQ(get_int_type_from_enum_var(Decls[7]), "char");
186235
EXPECT_EQ(get_int_type_from_enum_var(Decls[8]), "int");
@@ -190,11 +239,12 @@ TEST(EnumReflectionTest, GetIntegerTypeFromEnumType) {
190239
#else
191240
EXPECT_EQ(get_int_type_from_enum_var(Decls[10]), "unsigned int");
192241
#endif
193-
EXPECT_EQ(get_int_type_from_enum_var(Decls[11]), "NULL TYPE"); // When a non Enum Type variable is used
242+
EXPECT_EQ(get_int_type_from_enum_var(Decls[11]),
243+
"NULL TYPE"); // When a non Enum Type variable is used
194244
}
195245

196246
TEST(EnumReflectionTest, GetEnumConstants) {
197-
std::vector<Decl *> Decls;
247+
std::vector<Decl*> Decls;
198248
std::string code = R"(
199249
enum ZeroEnum {
200250
};
@@ -238,7 +288,7 @@ TEST(EnumReflectionTest, GetEnumConstants) {
238288
}
239289

240290
TEST(EnumReflectionTest, GetEnumConstantType) {
241-
std::vector<Decl *> Decls;
291+
std::vector<Decl*> Decls;
242292
std::string code = R"(
243293
enum Enum0 {
244294
Constant0 = 0
@@ -269,7 +319,7 @@ TEST(EnumReflectionTest, GetEnumConstantType) {
269319
}
270320

271321
TEST(EnumReflectionTest, GetEnumConstantValue) {
272-
std::vector<Decl *> Decls;
322+
std::vector<Decl*> Decls;
273323
std::string code = R"(
274324
enum Counter {
275325
Zero = 0,
@@ -293,7 +343,8 @@ TEST(EnumReflectionTest, GetEnumConstantValue) {
293343
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[4]), 54);
294344
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[5]), -10);
295345
EXPECT_EQ(Cpp::GetEnumConstantValue(EnumConstants[6]), -9);
296-
EXPECT_EQ(Cpp::GetEnumConstantValue(Decls[1]), 0); // Checking value of non enum constant
346+
EXPECT_EQ(Cpp::GetEnumConstantValue(Decls[1]),
347+
0); // Checking value of non enum constant
297348
}
298349

299350
TEST(EnumReflectionTest, GetEnums) {
@@ -345,16 +396,21 @@ TEST(EnumReflectionTest, GetEnums) {
345396
Cpp::TCppScope_t myClass_scope = Cpp::GetScope("myClass", 0);
346397
Cpp::TCppScope_t unsupported_scope = Cpp::GetScope("myVariable", 0);
347398

348-
Cpp::GetEnums(globalscope,enumNames1);
349-
Cpp::GetEnums(Animals_scope,enumNames2);
399+
Cpp::GetEnums(globalscope, enumNames1);
400+
Cpp::GetEnums(Animals_scope, enumNames2);
350401
Cpp::GetEnums(myClass_scope, enumNames3);
351402
Cpp::GetEnums(unsupported_scope, enumNames4);
352403

353404
// Check if the enum names are correctly retrieved
354-
EXPECT_TRUE(std::find(enumNames1.begin(), enumNames1.end(), "Color") != enumNames1.end());
355-
EXPECT_TRUE(std::find(enumNames1.begin(), enumNames1.end(), "Days") != enumNames1.end());
356-
EXPECT_TRUE(std::find(enumNames2.begin(), enumNames2.end(), "AnimalType") != enumNames2.end());
357-
EXPECT_TRUE(std::find(enumNames2.begin(), enumNames2.end(), "Months") != enumNames2.end());
358-
EXPECT_TRUE(std::find(enumNames3.begin(), enumNames3.end(), "Color") != enumNames3.end());
405+
EXPECT_TRUE(std::find(enumNames1.begin(), enumNames1.end(), "Color") !=
406+
enumNames1.end());
407+
EXPECT_TRUE(std::find(enumNames1.begin(), enumNames1.end(), "Days") !=
408+
enumNames1.end());
409+
EXPECT_TRUE(std::find(enumNames2.begin(), enumNames2.end(), "AnimalType") !=
410+
enumNames2.end());
411+
EXPECT_TRUE(std::find(enumNames2.begin(), enumNames2.end(), "Months") !=
412+
enumNames2.end());
413+
EXPECT_TRUE(std::find(enumNames3.begin(), enumNames3.end(), "Color") !=
414+
enumNames3.end());
359415
EXPECT_TRUE(enumNames4.empty());
360416
}

0 commit comments

Comments
 (0)