Skip to content

Commit

Permalink
Fix createCXString
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnimuc committed Jun 7, 2024
1 parent 4eabf46 commit cdb66fc
Showing 1 changed file with 32 additions and 35 deletions.
67 changes: 32 additions & 35 deletions lib/Interpreter/CXCppInterOp.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "clang-c/CXCppInterOp.h"
#include "Compatibility.h"
#include "CppInterOpInterpreter.h"
#include "clang/Interpreter/CppInterOp.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include <cstring>
#include <memory>
#include "clang-c/CXString.h"
#include <cstring>

CXString createCXString(const std::string& str) {
char* s = new char[str.length() + 1];
std::strcpy(s, str.c_str());

CXString createCXString(const std::string& S) {
CXString Str;
Str.data = s && s[0] == '\0' ? "" : s;
Str.private_flags = 1; // CXS_Malloc
if (S.empty()) {
Str.data = "";
Str.private_flags = 0; // CXS_Unmanaged
} else {
Str.data = strdup(S.c_str());
Str.private_flags = 1; // CXS_Malloc
}
return Str;
}

Expand All @@ -27,37 +27,33 @@ void clang_interpreter_dispose(CXInterpreter I) {
}

CXCompilerInstance clang_interpreter_getCompilerInstance(CXInterpreter I) {
return const_cast<clang::CompilerInstance*>(
static_cast<compat::Interpreter*>(I)->getCI());
auto* interp = static_cast<compat::Interpreter*>(I);
return const_cast<clang::CompilerInstance*>(interp->getCI());
}

LLVMOrcLLJITRef clang_interpreter_getExecutionEngine(CXInterpreter I) {
#ifdef USE_CLING
return nullptr;
#else
auto* interp = static_cast<compat::Interpreter*>(I);
return reinterpret_cast<LLVMOrcLLJITRef>(const_cast<llvm::orc::LLJIT*>(
static_cast<Cpp::Interpreter*>(I)
->getExecutionEngine())); // NOLINT(cppcoreguidelines-pro-type-const-cast,
// cppcoreguidelines-pro-type-reinterpret-cast)
interp->getExecutionEngine())); // NOLINT(*-cast)
#endif
}

void clang_interpreter_addSearchPath(CXInterpreter I, const char* dir,
bool isUser, bool prepend) {
static_cast<compat::Interpreter*>(I)
->getDynamicLibraryManager()
->addSearchPath(dir, isUser, prepend);
auto* interp = static_cast<compat::Interpreter*>(I);
interp->getDynamicLibraryManager()->addSearchPath(dir, isUser, prepend);
}

void clang_interpreter_addIncludePath(CXInterpreter I, const char* dir) {
static_cast<compat::Interpreter*>(I)->AddIncludePath(dir);
}

const char* clang_interpreter_getResourceDir(CXInterpreter I) {
return static_cast<compat::Interpreter*>(I)
->getCI()
->getHeaderSearchOpts()
.ResourceDir.c_str();
auto* interp = static_cast<compat::Interpreter*>(I);
return interp->getCI()->getHeaderSearchOpts().ResourceDir.c_str();
}

enum CXErrorCode clang_interpreter_declare(CXInterpreter I, const char* code,
Expand Down Expand Up @@ -122,31 +118,32 @@ enum CXErrorCode clang_interpreter_evaluate(CXInterpreter I, const char* code,

CXString clang_interpreter_lookupLibrary(CXInterpreter I,
const char* lib_name) {
return createCXString(static_cast<compat::Interpreter*>(I)
->getDynamicLibraryManager()
->lookupLibrary(lib_name));
auto* interp = static_cast<compat::Interpreter*>(I);
return createCXString(
interp->getDynamicLibraryManager()->lookupLibrary(lib_name).c_str());
}

CXInterpreter_CompilationResult
clang_interpreter_loadLibrary(CXInterpreter I, const char* lib_stem,
bool lookup) {
auto* interp = static_cast<compat::Interpreter*>(I);
return static_cast<CXInterpreter_CompilationResult>(
static_cast<compat::Interpreter*>(I)->loadLibrary(lib_stem, lookup));
interp->loadLibrary(lib_stem, lookup));
}

void clang_interpreter_unloadLibrary(CXInterpreter I, const char* lib_stem) {
static_cast<compat::Interpreter*>(I)
->getDynamicLibraryManager()
->unloadLibrary(lib_stem);
auto* interp = static_cast<compat::Interpreter*>(I);
interp->getDynamicLibraryManager()->unloadLibrary(lib_stem);
}

CXString clang_interpreter_searchLibrariesForSymbol(CXInterpreter I,
const char* mangled_name,
bool search_system) {
auto* interp = static_cast<compat::Interpreter*>(I);
return createCXString(
static_cast<compat::Interpreter*>(I)
->getDynamicLibraryManager()
->searchLibrariesForSymbol(mangled_name, search_system));
interp->getDynamicLibraryManager()
->searchLibrariesForSymbol(mangled_name, search_system)
.c_str());
}

namespace Cpp {
Expand All @@ -156,15 +153,15 @@ void DestructImpl(compat::Interpreter& interp, TCppObject_t This,

void clang_interpreter_destruct(CXInterpreter I, CXObject This, CXScope type,
bool withFree) {
Cpp::DestructImpl(*static_cast<compat::Interpreter*>(I), This, type,
withFree);
auto* interp = static_cast<compat::Interpreter*>(I);
Cpp::DestructImpl(*interp, This, type, withFree);
}

CXFuncAddr
clang_interpreter_getFunctionAddressFromMangledName(CXInterpreter I,
const char* mangled_name) {
auto FDAorErr = compat::getSymbolAddress(
*static_cast<compat::Interpreter*>(I), mangled_name);
auto* interp = static_cast<compat::Interpreter*>(I);
auto FDAorErr = compat::getSymbolAddress(*interp, mangled_name);
if (llvm::Error Err = FDAorErr.takeError())
llvm::consumeError(std::move(Err)); // nullptr if missing
else
Expand Down

0 comments on commit cdb66fc

Please sign in to comment.