Skip to content

Commit

Permalink
[Symbol Names] Workaround: Mangle names for macOS
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
SAtacker committed Jan 4, 2024
1 parent d294b07 commit cfafa48
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 44 deletions.
9 changes: 9 additions & 0 deletions lib/Interpreter/Compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,24 @@ getSymbolAddress(clang::Interpreter& I, llvm::StringRef IRName) {
inline llvm::Expected<llvm::JITTargetAddress>
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<llvm::JITTargetAddress>
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();
Expand Down
4 changes: 4 additions & 0 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 1 addition & 16 deletions unittests/CppInterOp/FunctionReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Decl*> Decls;
std::string code = R"(
typedef struct _name {
Expand All @@ -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<Decl*> Decls;
std::string code = R"(
int f1(int i) { return i * i; }
Expand Down Expand Up @@ -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"(
Expand Down Expand Up @@ -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"(
Expand Down
4 changes: 0 additions & 4 deletions unittests/CppInterOp/InterpreterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions unittests/CppInterOp/JitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Decl*> Decls;
std::string code = R"(
extern "C" int printf(const char*,...);
Expand Down
18 changes: 2 additions & 16 deletions unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> g_builtins =
// {"bool", "char", "signed char", "unsigned char", "wchar_t", "short", "unsigned short",
// "int", "unsigned int", "long", "unsigned long", "long long", "unsigned long long",
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 <memory>)";
Interp->process(code);
Expand Down Expand Up @@ -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 <vector>
#include <iostream>
Expand Down
4 changes: 0 additions & 4 deletions unittests/CppInterOp/TypeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"(
Expand Down

0 comments on commit cfafa48

Please sign in to comment.