Skip to content

Revert "Fix bugs related to printing types (#661)" #673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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: 6 additions & 20 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,28 +517,18 @@ std::string GetCompleteName(TCppType_t klass) {
auto& C = getSema().getASTContext();
auto* D = (Decl*)klass;

PrintingPolicy Policy = C.getPrintingPolicy();
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 @@ -3645,11 +3635,7 @@ std::string GetFunctionArgDefault(TCppFunction_t func,
if (PI->hasDefaultArg()) {
std::string Result;
llvm::raw_string_ostream OS(Result);
Expr* DefaultArgExpr = nullptr;
if (PI->hasUninstantiatedDefaultArg())
DefaultArgExpr = PI->getUninstantiatedDefaultArg();
else
DefaultArgExpr = PI->getDefaultArg();
Expr* DefaultArgExpr = const_cast<Expr*>(PI->getDefaultArg());
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: redundant explicit casting to the same type 'Expr *' as the sub-expression, remove this casting [readability-redundant-casting]

Suggested change
Expr* DefaultArgExpr = const_cast<Expr*>(PI->getDefaultArg());
Expr* DefaultArgExpr = PI->getDefaultArg();
Additional context

llvm/include/clang/AST/Decl.h:1824: source type originates from the invocation of this method

  Expr *getDefaultArg();
        ^

DefaultArgExpr->printPretty(OS, nullptr, PrintingPolicy(LangOptions()));

// FIXME: Floats are printed in clang with the precision of their underlying
Expand Down
23 changes: 0 additions & 23 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2078,15 +2078,6 @@ 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 @@ -2111,20 +2102,6 @@ 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()};
Cpp::TCppScope_t my_struct =
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: 0 additions & 11 deletions unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,6 @@ TEST(ScopeReflectionTest, GetCompleteName) {
A<int> a;

enum { enum1 };

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

Expand All @@ -353,15 +350,7 @@ 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(),
C.DoubleTy.getAsOpaquePtr()};
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