Skip to content

Commit 50f7e29

Browse files
fix GetFunctionArgDefault to handle uninstantiated args
1 parent 19507f6 commit 50f7e29

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/CppInterOp/CppInterOp.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3645,7 +3645,11 @@ std::string GetFunctionArgDefault(TCppFunction_t func,
36453645
if (PI->hasDefaultArg()) {
36463646
std::string Result;
36473647
llvm::raw_string_ostream OS(Result);
3648-
Expr* DefaultArgExpr = const_cast<Expr*>(PI->getDefaultArg());
3648+
Expr* DefaultArgExpr = nullptr;
3649+
if (PI->hasUninstantiatedDefaultArg())
3650+
DefaultArgExpr = PI->getUninstantiatedDefaultArg();
3651+
else
3652+
DefaultArgExpr = PI->getDefaultArg();
36493653
DefaultArgExpr->printPretty(OS, nullptr, PrintingPolicy(LangOptions()));
36503654

36513655
// FIXME: Floats are printed in clang with the precision of their underlying

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,15 @@ TEST(FunctionReflectionTest, GetFunctionArgDefault) {
20782078
template<class A>
20792079
void get_size(long k, A, char ch = 'a', double l = 0.0) {}
20802080
2081+
template<typename T>
2082+
struct Other {};
2083+
2084+
template <typename T, typename S = Other<T>>
2085+
struct MyStruct {
2086+
T t;
2087+
S s;
2088+
void fn(T t, S s = S()) {}
2089+
};
20812090
)";
20822091

20832092
GetAllTopLevelDecls(code, Decls);
@@ -2102,6 +2111,20 @@ TEST(FunctionReflectionTest, GetFunctionArgDefault) {
21022111
EXPECT_EQ(Cpp::GetFunctionArgDefault(Decls[4], 1), "");
21032112
EXPECT_EQ(Cpp::GetFunctionArgDefault(Decls[4], 2), "\'a\'");
21042113
EXPECT_EQ(Cpp::GetFunctionArgDefault(Decls[4], 3), "0.");
2114+
2115+
ASTContext& C = Interp->getCI()->getASTContext();
2116+
Cpp::TemplateArgInfo template_args[1] = {C.IntTy.getAsOpaquePtr()};
2117+
Cpp::TCppScope_t my_struct =
2118+
Cpp::InstantiateTemplate(Decls[6], template_args, 1);
2119+
EXPECT_TRUE(my_struct);
2120+
2121+
std::vector<Cpp::TCppFunction_t> fns =
2122+
Cpp::GetFunctionsUsingName(my_struct, "fn");
2123+
EXPECT_EQ(fns.size(), 1);
2124+
2125+
Cpp::TCppScope_t fn = fns[0];
2126+
EXPECT_EQ(Cpp::GetFunctionArgDefault(fn, 0), "");
2127+
EXPECT_EQ(Cpp::GetFunctionArgDefault(fn, 1), "S()");
21052128
}
21062129

21072130
TEST(FunctionReflectionTest, Construct) {

0 commit comments

Comments
 (0)