From cfafa48475598d053570569c19817cb124885d93 Mon Sep 17 00:00:00 2001 From: Shreyas Atre Date: Fri, 5 Jan 2024 00:41:01 +0530 Subject: [PATCH] [Symbol Names] Workaround: Mangle names for macOS - Prepend `_` before looking for symbols - Currenty the interface is not unified and solutions like a global prefix would work better. Signed-off-by: Shreyas Atre --- lib/Interpreter/Compatibility.h | 9 +++++++++ lib/Interpreter/CppInterOp.cpp | 4 ++++ .../CppInterOp/FunctionReflectionTest.cpp | 17 +---------------- unittests/CppInterOp/InterpreterTest.cpp | 4 ---- unittests/CppInterOp/JitTest.cpp | 4 ---- unittests/CppInterOp/ScopeReflectionTest.cpp | 18 ++---------------- unittests/CppInterOp/TypeReflectionTest.cpp | 4 ---- 7 files changed, 16 insertions(+), 44 deletions(-) diff --git a/lib/Interpreter/Compatibility.h b/lib/Interpreter/Compatibility.h index b84579c47..98ace1811 100644 --- a/lib/Interpreter/Compatibility.h +++ b/lib/Interpreter/Compatibility.h @@ -211,15 +211,24 @@ getSymbolAddress(clang::Interpreter& I, llvm::StringRef IRName) { inline llvm::Expected getSymbolAddress(clang::Interpreter& I, clang::GlobalDecl GD) { std::string MangledName; +#if __APPLE__ + return getSymbolAddress(I, llvm::StringRef("_" + MangledName)); +#else compat::maybeMangleDeclName(GD, MangledName); return getSymbolAddress(I, llvm::StringRef(MangledName)); +#endif } inline llvm::Expected getSymbolAddressFromLinkerName(const clang::Interpreter& I, llvm::StringRef LinkerName) { #if CLANG_VERSION_MAJOR >= 14 +#if __APPLE__ + /// https://github.com/llvm/llvm-project/blob/0f8615f4dc568f4d7cbf73580eef3e78f64f3bd0/llvm/lib/ExecutionEngine/Orc/ExecutionUtils.cpp#L252-L259 + auto AddrOrErr = I.getSymbolAddressFromLinkerName(("_" + LinkerName).str()); +#else auto AddrOrErr = I.getSymbolAddressFromLinkerName(LinkerName); +#endif if (llvm::Error Err = AddrOrErr.takeError()) return std::move(Err); return AddrOrErr->getValue(); diff --git a/lib/Interpreter/CppInterOp.cpp b/lib/Interpreter/CppInterOp.cpp index 9a0c21e44..d3fb01725 100644 --- a/lib/Interpreter/CppInterOp.cpp +++ b/lib/Interpreter/CppInterOp.cpp @@ -2661,7 +2661,11 @@ namespace Cpp { // Let's inject it. SymbolMap::iterator It; llvm::orc::SymbolMap InjectedSymbols; +#if __APPLE__ + auto Name = ES.intern("_" + std::string(linker_mangled_name)); +#else auto Name = ES.intern(linker_mangled_name); +#endif InjectedSymbols[Name] = #if CLANG_VERSION_MAJOR < 17 JITEvaluatedSymbol(address, diff --git a/unittests/CppInterOp/FunctionReflectionTest.cpp b/unittests/CppInterOp/FunctionReflectionTest.cpp index 96be337e6..3fcb3533f 100644 --- a/unittests/CppInterOp/FunctionReflectionTest.cpp +++ b/unittests/CppInterOp/FunctionReflectionTest.cpp @@ -592,11 +592,7 @@ TEST(FunctionReflectionTest, IsVirtualMethod) { EXPECT_FALSE(Cpp::IsVirtualMethod(Decls[0])); } -#ifdef __APPLE__ -TEST(FunctionReflectionTest, DISABLED_JitCallAdvanced) { -#else TEST(FunctionReflectionTest, JitCallAdvanced) { -#endif std::vector Decls; std::string code = R"( typedef struct _name { @@ -617,11 +613,8 @@ TEST(FunctionReflectionTest, JitCallAdvanced) { Cpp::Destruct(object, Decls[1]); } -#ifdef __APPLE__ -TEST(FunctionReflectionTest, DISABLED_GetFunctionCallWrapper) { -#else + TEST(FunctionReflectionTest, GetFunctionCallWrapper) { -#endif std::vector Decls; std::string code = R"( int f1(int i) { return i * i; } @@ -784,11 +777,7 @@ TEST(FunctionReflectionTest, GetFunctionArgDefault) { EXPECT_EQ(Cpp::GetFunctionArgDefault(Decls[1], 2), "34126"); } -#ifdef __APPLE__ -TEST(FunctionReflectionTest, DISABLED_Construct) { -#else TEST(FunctionReflectionTest, Construct) { -#endif Cpp::CreateInterpreter(); Interp->declare(R"( @@ -822,11 +811,7 @@ TEST(FunctionReflectionTest, Construct) { EXPECT_EQ(output, "Constructor Executed"); } -#ifdef __APPLE__ -TEST(FunctionReflectionTest, DISABLED_Destruct) { -#else TEST(FunctionReflectionTest, Destruct) { -#endif Cpp::CreateInterpreter(); Interp->declare(R"( diff --git a/unittests/CppInterOp/InterpreterTest.cpp b/unittests/CppInterOp/InterpreterTest.cpp index e64cb8d7c..5c30108b2 100644 --- a/unittests/CppInterOp/InterpreterTest.cpp +++ b/unittests/CppInterOp/InterpreterTest.cpp @@ -49,11 +49,7 @@ TEST(InterpreterTest, Evaluate) { EXPECT_FALSE(HadError) ; } -#ifdef __APPLE__ //Fails for Cling Tests -TEST(InterpreterTest, DISABLED_Process) { -#else TEST(InterpreterTest, Process) { -#endif Cpp::CreateInterpreter(); EXPECT_TRUE(Cpp::Process("") == 0); EXPECT_TRUE(Cpp::Process("int a = 12;") == 0); diff --git a/unittests/CppInterOp/JitTest.cpp b/unittests/CppInterOp/JitTest.cpp index 93edd04a9..9cc1bc38a 100644 --- a/unittests/CppInterOp/JitTest.cpp +++ b/unittests/CppInterOp/JitTest.cpp @@ -11,11 +11,7 @@ static int printf_jit(const char* format, ...) { return 0; } -#ifdef __APPLE__ -TEST(JitTest, DISABLED_InsertOrReplaceJitSymbol) { -#else TEST(JitTest, InsertOrReplaceJitSymbol) { -#endif std::vector Decls; std::string code = R"( extern "C" int printf(const char*,...); diff --git a/unittests/CppInterOp/ScopeReflectionTest.cpp b/unittests/CppInterOp/ScopeReflectionTest.cpp index af88724d5..7549cdaef 100644 --- a/unittests/CppInterOp/ScopeReflectionTest.cpp +++ b/unittests/CppInterOp/ScopeReflectionTest.cpp @@ -99,11 +99,8 @@ TEST(ScopeReflectionTest, SizeOf) { EXPECT_EQ(Cpp::SizeOf(Decls[7]), (size_t)16); } -#ifdef __APPLE__ -TEST(ScopeReflectionTest, DISABLED_IsBuiltin) { -#else + TEST(ScopeReflectionTest, IsBuiltin) { -#endif // static std::set g_builtins = // {"bool", "char", "signed char", "unsigned char", "wchar_t", "short", "unsigned short", // "int", "unsigned int", "long", "unsigned long", "long long", "unsigned long long", @@ -433,11 +430,7 @@ TEST(ScopeReflectionTest, GetScopefromCompleteName) { EXPECT_EQ(Cpp::GetQualifiedName(Cpp::GetScopeFromCompleteName("N1::N2::C::S")), "N1::N2::C::S"); } -#ifdef __APPLE__ -TEST(ScopeReflectionTest, DISABLED_GetNamed) { -#else TEST(ScopeReflectionTest, GetNamed) { -#endif std::string code = R"(namespace N1 { namespace N2 { class C { @@ -761,11 +754,7 @@ TEST(ScopeReflectionTest, InstantiateNNTPClassTemplate) { /*type_size*/ args1.size())); } -#ifdef __APPLE__ -TEST(ScopeReflectionTest, DISABLED_InstantiateTemplateFunctionFromString) { -#else TEST(ScopeReflectionTest, InstantiateTemplateFunctionFromString) { -#endif Cpp::CreateInterpreter(); std::string code = R"(#include )"; Interp->process(code); @@ -905,11 +894,8 @@ TEST(ScopeReflectionTest, GetClassTemplateInstantiationArgs) { EXPECT_TRUE(instance_types.size() == 0); } -#ifdef __APPLE__ -TEST(ScopeReflectionTest, DISABLED_IncludeVector) { -#else + TEST(ScopeReflectionTest, IncludeVector) { -#endif std::string code = R"( #include #include diff --git a/unittests/CppInterOp/TypeReflectionTest.cpp b/unittests/CppInterOp/TypeReflectionTest.cpp index 48c863e6f..34db92306 100644 --- a/unittests/CppInterOp/TypeReflectionTest.cpp +++ b/unittests/CppInterOp/TypeReflectionTest.cpp @@ -522,11 +522,7 @@ TEST(TypeReflectionTest, IsPODType) { EXPECT_FALSE(Cpp::IsPODType(0)); } -#ifdef __APPLE__ -TEST(TypeReflectionTest, DISABLED_IsSmartPtrType) { -#else TEST(TypeReflectionTest, IsSmartPtrType) { -#endif Cpp::CreateInterpreter(); Interp->declare(R"(