From 77eadd1d2b033e3a52d5924c1b1fd0902f036f07 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Fri, 20 Dec 2024 15:41:34 +0530 Subject: [PATCH 1/2] fix `MakeFunctionCallable` for dealing with public typedef to private type --- lib/Interpreter/CppInterOp.cpp | 13 +++-- .../CppInterOp/FunctionReflectionTest.cpp | 53 ++++++++++++++++++- 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index eaef806be..d285ef9a1 100644 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -1600,7 +1600,12 @@ namespace Cpp { PrintingPolicy Policy) { //TODO: Implement cling desugaring from utils::AST // cling::utils::Transform::GetPartiallyDesugaredType() - QT = QT.getDesugaredType(C); + if (!QT->isTypedefNameType() || QT->isBuiltinType()) + QT = QT.getDesugaredType(C); +#if CLANG_VERSION_MAJOR > 16 + Policy.SuppressElaboration = true; +#endif + Policy.FullyQualifiedName = true; QT.getAsStringInternal(type_name, Policy); } @@ -1652,13 +1657,13 @@ namespace Cpp { return; } else if (QT->isPointerType()) { isPointer = true; - QT = cast(QT)->getPointeeType(); + QT = cast(QT.getCanonicalType())->getPointeeType(); } else if (QT->isReferenceType()) { if (QT->isRValueReferenceType()) refType = kRValueReference; else refType = kLValueReference; - QT = cast(QT)->getPointeeType(); + QT = cast(QT.getCanonicalType())->getPointeeType(); } // Fall through for the array type to deal with reference/pointer ro array // type. @@ -1911,7 +1916,7 @@ namespace Cpp { make_narg_ctor_with_return(FD, N, class_name, buf, indent_level); return; } - QualType QT = FD->getReturnType().getCanonicalType(); + QualType QT = FD->getReturnType(); if (QT->isVoidType()) { std::ostringstream typedefbuf; std::ostringstream callbuf; diff --git a/unittests/CppInterOp/FunctionReflectionTest.cpp b/unittests/CppInterOp/FunctionReflectionTest.cpp index bd1483dea..efc1ca815 100644 --- a/unittests/CppInterOp/FunctionReflectionTest.cpp +++ b/unittests/CppInterOp/FunctionReflectionTest.cpp @@ -1,8 +1,9 @@ #include "Utils.h" #include "clang/AST/ASTContext.h" -#include "clang/Interpreter/CppInterOp.h" +#include "clang/Basic/Version.h" #include "clang/Frontend/CompilerInstance.h" +#include "clang/Interpreter/CppInterOp.h" #include "clang/Sema/Sema.h" #include "gtest/gtest.h" @@ -974,6 +975,56 @@ TEST(FunctionReflectionTest, GetFunctionCallWrapper) { FCI_Add.Invoke(&result, {args, /*args_size=*/2}); EXPECT_EQ(result, a + b); + + // call with pointers + Interp->process(R"( + void set_5(int *out) { + *out = 5; + } + )"); + + Cpp::TCppScope_t set_5 = Cpp::GetNamed("set_5"); + EXPECT_TRUE(set_5); + + Cpp::JitCall set_5_f = Cpp::MakeFunctionCallable(set_5); + EXPECT_EQ(set_5_f.getKind(), Cpp::JitCall::kGenericCall); + + int* bp = &b; + void* set_5_args[1] = {(void*)&bp}; + set_5_f.Invoke(nullptr, {set_5_args, 1}); + EXPECT_EQ(b, 5); + +#if CLANG_VERSION_MAJOR > 16 + // typedef resolution testing + // supported for clang version >16 only + Interp->process(R"( + class TypedefToPrivateClass { + private: + class PC { + public: + int m_val = 4; + }; + + public: + typedef PC PP; + static PP f() { return PC(); } + }; + )"); + + Cpp::TCppScope_t TypedefToPrivateClass = + Cpp::GetNamed("TypedefToPrivateClass"); + EXPECT_TRUE(TypedefToPrivateClass); + + Cpp::TCppScope_t f = Cpp::GetNamed("f", TypedefToPrivateClass); + EXPECT_TRUE(f); + + Cpp::JitCall FCI_f = Cpp::MakeFunctionCallable(f); + EXPECT_EQ(FCI_f.getKind(), Cpp::JitCall::kGenericCall); + + void* res = nullptr; + FCI_f.Invoke(&res, {nullptr, 0}); + EXPECT_TRUE(res); +#endif } TEST(FunctionReflectionTest, IsConstMethod) { From d1ffcd8e30f4e56a01807805c85473ed9ed60106 Mon Sep 17 00:00:00 2001 From: Vipul Cariappa Date: Wed, 8 Jan 2025 09:34:13 +0530 Subject: [PATCH 2/2] Disable c-style pointers checks in clang-tidy for unittests --- unittests/.clang-tidy | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unittests/.clang-tidy b/unittests/.clang-tidy index 6b19485e3..f59809139 100644 --- a/unittests/.clang-tidy +++ b/unittests/.clang-tidy @@ -4,5 +4,8 @@ Checks: > -cppcoreguidelines-macro-usage, -cppcoreguidelines-pro-type-cstyle-cast, -cppcoreguidelines-avoid-non-const-global-variables, + -cppcoreguidelines-avoid-c-arrays, + -cppcoreguidelines-pro-bounds-array-to-pointer-decay, -performance-unnecessary-value-param, -performance-no-int-to-ptr, + -bugprone-multi-level-implicit-pointer-conversion,