Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,18 +517,28 @@ std::string GetCompleteName(TCppType_t klass) {
auto& C = getSema().getASTContext();
auto* D = (Decl*)klass;

PrintingPolicy Policy = C.getPrintingPolicy();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be a good idea to improve the to-string printing consistency by modifying the default printing policy at CreateIntepreter time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the intention is to; not keep modifying the PrintingPolicy, it may not be possible. We need different amounts of information at different times, using different APIs. Where we will have to modify the PrintingPolicy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we open an issue to track this idea?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened an issue at #662

Policy.SuppressUnwrittenScope = true;
Policy.SuppressScope = true;
Policy.AnonymousTagLocations = false;
Policy.SuppressTemplateArgsInCXXConstructors = false;
Policy.SuppressDefaultTemplateArgs = false;
Policy.AlwaysIncludeTypeForTemplateArgument = true;

if (auto* ND = llvm::dyn_cast_or_null<NamedDecl>(D)) {
if (auto* TD = llvm::dyn_cast<TagDecl>(ND)) {
std::string type_name;
QualType QT = C.getTagDeclType(TD);
PrintingPolicy Policy = C.getPrintingPolicy();
Policy.SuppressUnwrittenScope = true;
Policy.SuppressScope = true;
Policy.AnonymousTagLocations = false;
QT.getAsStringInternal(type_name, Policy);

return type_name;
}
if (auto* FD = llvm::dyn_cast<FunctionDecl>(ND)) {
std::string func_name;
llvm::raw_string_ostream name_stream(func_name);
FD->getNameForDiagnostic(name_stream, Policy, false);
name_stream.flush();
return func_name;
}

return ND->getNameAsString();
}
Expand Down Expand Up @@ -3635,7 +3645,11 @@ std::string GetFunctionArgDefault(TCppFunction_t func,
if (PI->hasDefaultArg()) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Expr* DefaultArgExpr = const_cast<Expr*>(PI->getDefaultArg());
Expr* DefaultArgExpr = nullptr;
if (PI->hasUninstantiatedDefaultArg())
DefaultArgExpr = PI->getUninstantiatedDefaultArg();
else
DefaultArgExpr = PI->getDefaultArg();
DefaultArgExpr->printPretty(OS, nullptr, PrintingPolicy(LangOptions()));

// FIXME: Floats are printed in clang with the precision of their underlying
Expand Down
23 changes: 23 additions & 0 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,15 @@ TEST(FunctionReflectionTest, GetFunctionArgDefault) {
template<class A>
void get_size(long k, A, char ch = 'a', double l = 0.0) {}

template<typename T>
struct Other {};

template <typename T, typename S = Other<T>>
struct MyStruct {
T t;
S s;
void fn(T t, S s = S()) {}
};
)";

GetAllTopLevelDecls(code, Decls);
Expand All @@ -2102,6 +2111,20 @@ TEST(FunctionReflectionTest, GetFunctionArgDefault) {
EXPECT_EQ(Cpp::GetFunctionArgDefault(Decls[4], 1), "");
EXPECT_EQ(Cpp::GetFunctionArgDefault(Decls[4], 2), "\'a\'");
EXPECT_EQ(Cpp::GetFunctionArgDefault(Decls[4], 3), "0.");

ASTContext& C = Interp->getCI()->getASTContext();
Cpp::TemplateArgInfo template_args[1] = {C.IntTy.getAsOpaquePtr()};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

  Cpp::TemplateArgInfo template_args[1] = {C.IntTy.getAsOpaquePtr()};
  ^

Cpp::TCppScope_t my_struct =
Cpp::InstantiateTemplate(Decls[6], template_args, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay]

      Cpp::InstantiateTemplate(Decls[6], template_args, 1);
                                         ^

EXPECT_TRUE(my_struct);

std::vector<Cpp::TCppFunction_t> fns =
Cpp::GetFunctionsUsingName(my_struct, "fn");
EXPECT_EQ(fns.size(), 1);

Cpp::TCppScope_t fn = fns[0];
EXPECT_EQ(Cpp::GetFunctionArgDefault(fn, 0), "");
EXPECT_EQ(Cpp::GetFunctionArgDefault(fn, 1), "S()");
}

TEST(FunctionReflectionTest, Construct) {
Expand Down
11 changes: 11 additions & 0 deletions unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@ TEST(ScopeReflectionTest, GetCompleteName) {
A<int> a;

enum { enum1 };

template<typename T1, typename T2>
void fn(T1 t1, T2 t2) {}
)";
GetAllTopLevelDecls(code, Decls);

Expand All @@ -350,7 +353,15 @@ TEST(ScopeReflectionTest, GetCompleteName) {
Cpp::GetVariableType(
Decls[9]))), "A<int>");
EXPECT_EQ(Cpp::GetCompleteName(Decls[10]), "(unnamed)");
EXPECT_EQ(Cpp::GetCompleteName(Decls[11]), "fn");
EXPECT_EQ(Cpp::GetCompleteName(nullptr), "<unnamed>");

ASTContext& C = Interp->getCI()->getASTContext();
Cpp::TemplateArgInfo template_args[2] = {C.IntTy.getAsOpaquePtr(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

  Cpp::TemplateArgInfo template_args[2] = {C.IntTy.getAsOpaquePtr(),
  ^

C.DoubleTy.getAsOpaquePtr()};
Cpp::TCppScope_t fn = Cpp::InstantiateTemplate(Decls[11], template_args, 2);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not implicitly decay an array into a pointer; consider using gsl::array_view or an explicit cast instead [cppcoreguidelines-pro-bounds-array-to-pointer-decay]

  Cpp::TCppScope_t fn = Cpp::InstantiateTemplate(Decls[11], template_args, 2);
                                                            ^

EXPECT_TRUE(fn);
EXPECT_EQ(Cpp::GetCompleteName(fn), "fn<int, double>");
}

TEST(ScopeReflectionTest, GetQualifiedName) {
Expand Down
Loading