6
6
#include " clang/Interpreter/CppInterOp.h"
7
7
#include " clang/Sema/Sema.h"
8
8
9
+ #include " clang-c/CXCppInterOp.h"
10
+
9
11
#include " gtest/gtest.h"
10
12
11
13
#include < string>
@@ -105,6 +107,20 @@ TEST(FunctionReflectionTest, GetClassMethods) {
105
107
std::vector<Cpp::TCppFunction_t> methods5;
106
108
Cpp::GetClassMethods (nullptr , methods5);
107
109
EXPECT_EQ (methods5.size (), 0 );
110
+
111
+ // C API
112
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
113
+ auto C_API_SHIM = [&](Cpp::TCppFunction_t method) {
114
+ auto Str = clang_getFunctionSignature (
115
+ make_scope (static_cast <clang::Decl*>(method), I));
116
+ auto Res = std::string (get_c_string (Str));
117
+ dispose_string (Str);
118
+ return Res;
119
+ };
120
+ EXPECT_EQ (C_API_SHIM (methods0[0 ]), " int A::f1(int a, int b)" );
121
+ // Clean up resources
122
+ clang_Interpreter_takeInterpreterAsPtr (I);
123
+ clang_Interpreter_dispose (I);
108
124
}
109
125
110
126
TEST (FunctionReflectionTest, ConstructorInGetClassMethods) {
@@ -161,6 +177,15 @@ TEST(FunctionReflectionTest, HasDefaultConstructor) {
161
177
EXPECT_TRUE (Cpp::HasDefaultConstructor (Decls[0 ]));
162
178
EXPECT_TRUE (Cpp::HasDefaultConstructor (Decls[1 ]));
163
179
EXPECT_FALSE (Cpp::HasDefaultConstructor (Decls[3 ]));
180
+
181
+ // C API
182
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
183
+ EXPECT_TRUE (clang_hasDefaultConstructor (make_scope (Decls[0 ], I)));
184
+ EXPECT_TRUE (clang_hasDefaultConstructor (make_scope (Decls[1 ], I)));
185
+ EXPECT_FALSE (clang_hasDefaultConstructor (make_scope (Decls[3 ], I)));
186
+ // Clean up resources
187
+ clang_Interpreter_takeInterpreterAsPtr (I);
188
+ clang_Interpreter_dispose (I);
164
189
}
165
190
166
191
TEST (FunctionReflectionTest, GetDestructor) {
@@ -189,6 +214,14 @@ TEST(FunctionReflectionTest, GetDestructor) {
189
214
EXPECT_TRUE (DeletedDtor);
190
215
EXPECT_TRUE (Cpp::IsFunctionDeleted (DeletedDtor));
191
216
EXPECT_FALSE (Cpp::GetDestructor (Decls[3 ]));
217
+
218
+ // C API
219
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
220
+ EXPECT_TRUE (clang_getDestructor (make_scope (Decls[0 ], I)).data [0 ]);
221
+ EXPECT_TRUE (clang_getDestructor (make_scope (Decls[1 ], I)).data [0 ]);
222
+ // Clean up resources
223
+ clang_Interpreter_takeInterpreterAsPtr (I);
224
+ clang_Interpreter_dispose (I);
192
225
}
193
226
194
227
TEST (FunctionReflectionTest, GetFunctionsUsingName) {
@@ -524,6 +557,17 @@ TEST(FunctionReflectionTest, IsTemplatedFunction) {
524
557
EXPECT_FALSE (Cpp::IsTemplatedFunction (Decls[3 ]));
525
558
EXPECT_FALSE (Cpp::IsTemplatedFunction (SubDeclsC1[1 ]));
526
559
EXPECT_TRUE (Cpp::IsTemplatedFunction (SubDeclsC1[2 ]));
560
+
561
+ // C API
562
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
563
+ EXPECT_FALSE (clang_isTemplatedFunction (make_scope (Decls[0 ], I)));
564
+ EXPECT_TRUE (clang_isTemplatedFunction (make_scope (Decls[1 ], I)));
565
+ EXPECT_FALSE (clang_isTemplatedFunction (make_scope (Decls[3 ], I)));
566
+ EXPECT_FALSE (clang_isTemplatedFunction (make_scope (SubDeclsC1[1 ], I)));
567
+ EXPECT_TRUE (clang_isTemplatedFunction (make_scope (SubDeclsC1[2 ], I)));
568
+ // Clean up resources
569
+ clang_Interpreter_takeInterpreterAsPtr (I);
570
+ clang_Interpreter_dispose (I);
527
571
}
528
572
529
573
TEST (FunctionReflectionTest, ExistsFunctionTemplate) {
@@ -544,6 +588,14 @@ TEST(FunctionReflectionTest, ExistsFunctionTemplate) {
544
588
EXPECT_TRUE (Cpp::ExistsFunctionTemplate (" f" , 0 ));
545
589
EXPECT_TRUE (Cpp::ExistsFunctionTemplate (" f" , Decls[1 ]));
546
590
EXPECT_FALSE (Cpp::ExistsFunctionTemplate (" f" , Decls[2 ]));
591
+
592
+ // C API
593
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
594
+ EXPECT_TRUE (clang_existsFunctionTemplate (" f" , make_scope (Decls[1 ], I)));
595
+ EXPECT_FALSE (clang_existsFunctionTemplate (" f" , make_scope (Decls[2 ], I)));
596
+ // Clean up resources
597
+ clang_Interpreter_takeInterpreterAsPtr (I);
598
+ clang_Interpreter_dispose (I);
547
599
}
548
600
549
601
TEST (FunctionReflectionTest, InstantiateTemplateFunctionFromString) {
@@ -1119,6 +1171,17 @@ TEST(FunctionReflectionTest, JitCallAdvanced) {
1119
1171
EXPECT_TRUE (object) << " Failed to call the ctor." ;
1120
1172
// Building a wrapper with a typedef decl must be possible.
1121
1173
Cpp::Destruct (object, Decls[1 ]);
1174
+
1175
+ // C API
1176
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
1177
+ auto S = clang_getDefaultConstructor (make_scope (Decls[0 ], I));
1178
+ void * object_c = nullptr ;
1179
+ clang_invoke (S, &object_c, nullptr , 0 , nullptr );
1180
+ EXPECT_TRUE (object_c) << " Failed to call the ctor." ;
1181
+ clang_destruct (object_c, make_scope (Decls[1 ], I), true );
1182
+ // Clean up resources
1183
+ clang_Interpreter_takeInterpreterAsPtr (I);
1184
+ clang_Interpreter_dispose (I);
1122
1185
}
1123
1186
1124
1187
@@ -1473,6 +1536,23 @@ TEST(FunctionReflectionTest, Construct) {
1473
1536
Cpp::Deallocate (scope, where);
1474
1537
output = testing::internal::GetCapturedStdout ();
1475
1538
EXPECT_EQ (output, " Constructor Executed" );
1539
+ output.clear ();
1540
+
1541
+ // C API
1542
+ testing::internal::CaptureStdout ();
1543
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
1544
+ auto scope_c = make_scope (static_cast <clang::Decl*>(scope), I);
1545
+ auto object_c = clang_construct (scope_c, nullptr );
1546
+ EXPECT_TRUE (object_c != nullptr );
1547
+ output = testing::internal::GetCapturedStdout ();
1548
+ EXPECT_EQ (output, " Constructor Executed" );
1549
+ output.clear ();
1550
+ auto * dummy = clang_allocate (8 );
1551
+ EXPECT_TRUE (dummy);
1552
+ clang_deallocate (dummy);
1553
+ // Clean up resources
1554
+ clang_Interpreter_takeInterpreterAsPtr (I);
1555
+ clang_Interpreter_dispose (I);
1476
1556
}
1477
1557
1478
1558
TEST (FunctionReflectionTest, Destruct) {
@@ -1516,4 +1596,17 @@ TEST(FunctionReflectionTest, Destruct) {
1516
1596
Cpp::Deallocate (scope, object);
1517
1597
output = testing::internal::GetCapturedStdout ();
1518
1598
EXPECT_EQ (output, " Destructor Executed" );
1599
+
1600
+ // C API
1601
+ testing::internal::CaptureStdout ();
1602
+ auto * I = clang_createInterpreterFromRawPtr (Cpp::GetInterpreter ());
1603
+ auto scope_c = make_scope (static_cast <clang::Decl*>(scope), I);
1604
+ auto object_c = clang_construct (scope_c, nullptr );
1605
+ clang_destruct (object_c, scope_c, true );
1606
+ output = testing::internal::GetCapturedStdout ();
1607
+ EXPECT_EQ (output, " Destructor Executed" );
1608
+ output.clear ();
1609
+ // Clean up resources
1610
+ clang_Interpreter_takeInterpreterAsPtr (I);
1611
+ clang_Interpreter_dispose (I);
1519
1612
}
0 commit comments