diff --git a/lib/Interpreter/CXCppInterOp.cpp b/lib/Interpreter/CXCppInterOp.cpp index 63481a634..6c59170b7 100644 --- a/lib/Interpreter/CXCppInterOp.cpp +++ b/lib/Interpreter/CXCppInterOp.cpp @@ -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 -#include #include "clang-c/CXString.h" +#include -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; } @@ -27,26 +27,24 @@ void clang_interpreter_dispose(CXInterpreter I) { } CXCompilerInstance clang_interpreter_getCompilerInstance(CXInterpreter I) { - return const_cast( - static_cast(I)->getCI()); + auto* interp = static_cast(I); + return const_cast(interp->getCI()); } LLVMOrcLLJITRef clang_interpreter_getExecutionEngine(CXInterpreter I) { #ifdef USE_CLING return nullptr; #else + auto* interp = static_cast(I); return reinterpret_cast(const_cast( - static_cast(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(I) - ->getDynamicLibraryManager() - ->addSearchPath(dir, isUser, prepend); + auto* interp = static_cast(I); + interp->getDynamicLibraryManager()->addSearchPath(dir, isUser, prepend); } void clang_interpreter_addIncludePath(CXInterpreter I, const char* dir) { @@ -54,10 +52,8 @@ void clang_interpreter_addIncludePath(CXInterpreter I, const char* dir) { } const char* clang_interpreter_getResourceDir(CXInterpreter I) { - return static_cast(I) - ->getCI() - ->getHeaderSearchOpts() - .ResourceDir.c_str(); + auto* interp = static_cast(I); + return interp->getCI()->getHeaderSearchOpts().ResourceDir.c_str(); } enum CXErrorCode clang_interpreter_declare(CXInterpreter I, const char* code, @@ -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(I) - ->getDynamicLibraryManager() - ->lookupLibrary(lib_name)); + auto* interp = static_cast(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(I); return static_cast( - static_cast(I)->loadLibrary(lib_stem, lookup)); + interp->loadLibrary(lib_stem, lookup)); } void clang_interpreter_unloadLibrary(CXInterpreter I, const char* lib_stem) { - static_cast(I) - ->getDynamicLibraryManager() - ->unloadLibrary(lib_stem); + auto* interp = static_cast(I); + interp->getDynamicLibraryManager()->unloadLibrary(lib_stem); } CXString clang_interpreter_searchLibrariesForSymbol(CXInterpreter I, const char* mangled_name, bool search_system) { + auto* interp = static_cast(I); return createCXString( - static_cast(I) - ->getDynamicLibraryManager() - ->searchLibrariesForSymbol(mangled_name, search_system)); + interp->getDynamicLibraryManager() + ->searchLibrariesForSymbol(mangled_name, search_system) + .c_str()); } namespace Cpp { @@ -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(I), This, type, - withFree); + auto* interp = static_cast(I); + Cpp::DestructImpl(*interp, This, type, withFree); } CXFuncAddr clang_interpreter_getFunctionAddressFromMangledName(CXInterpreter I, const char* mangled_name) { - auto FDAorErr = compat::getSymbolAddress( - *static_cast(I), mangled_name); + auto* interp = static_cast(I); + auto FDAorErr = compat::getSymbolAddress(*interp, mangled_name); if (llvm::Error Err = FDAorErr.takeError()) llvm::consumeError(std::move(Err)); // nullptr if missing else