Skip to content

Commit

Permalink
fix-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnimuc committed Jun 5, 2024
1 parent 708cbf8 commit 4eabf46
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 33 deletions.
20 changes: 19 additions & 1 deletion include/clang-c/CXCppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,25 @@ typedef void* CXConstFunction;
CXJitCall clang_jitcall_makeFunctionCallable(CXInterpreter I,
CXConstFunction func);

intptr_t clang_jitcall_getVariableOffset(CXInterpreter I, CXScope var);

/**
* Inserts or replaces a symbol in the JIT with the one provided. This is useful
* for providing our own implementations of facilities such as printf.
*
* \param I The interpreter.
*
* \param linker_mangled_name The name of the symbol to be inserted or replaced.
*
* \param address The new address of the symbol.
*
* \returns true on failure.
*/
bool clang_jitcall_insertOrReplaceJitSymbol(CXInterpreter I,
const char* linker_mangled_name,
uint64_t address);

LLVM_CLANG_C_EXTERN_C_END

#endif // LLVM_CLANG_C_CXCPPINTEROP_H
// NOLINTEND()
// NOLINTEND()
69 changes: 43 additions & 26 deletions lib/Interpreter/CXCppInterOp.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "clang-c/CXCppInterOp.h"
#include "Compatibility.h"
#include "CppInterOpInterpreter.h"
#include "clang/Interpreter/CppInterOp.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include <cstring>
Expand All @@ -16,19 +17,6 @@ CXString createCXString(const std::string& str) {
return Str;

Check warning on line 17 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L15-L17

Added lines #L15 - L17 were not covered by tests
}

CXStringSet* createCXStringSet(const std::vector<std::string>& strs) {
CXStringSet* Set = new CXStringSet;
Set->Count = strs.size();
Set->Strings = new CXString[Set->Count];
for (unsigned int i = 0; i < Set->Count; ++i) {
char* Str = new char[strs[i].length() + 1];
std::strcpy(Str, strs[i].c_str());
Set->Strings[i].data = Str && Str[0] == '\0' ? "" : Str;
Set->Strings[i].private_flags = 1; // CXS_Malloc
}
return Set;
}

CXInterpreter clang_createInterpreter(const char* const* argv, int argc) {
auto I = std::make_unique<compat::Interpreter>(argc, argv);
return I.release();

Check warning on line 22 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L20-L22

Added lines #L20 - L22 were not covered by tests
Expand All @@ -40,12 +28,18 @@ void clang_interpreter_dispose(CXInterpreter I) {

CXCompilerInstance clang_interpreter_getCompilerInstance(CXInterpreter I) {

Check warning on line 29 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L29

Added line #L29 was not covered by tests
return const_cast<clang::CompilerInstance*>(
static_cast<compat::Interpreter*>(I)->getCompilerInstance());
static_cast<compat::Interpreter*>(I)->getCI());

Check warning on line 31 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L31

Added line #L31 was not covered by tests
}

LLVMOrcLLJITRef clang_interpreter_getExecutionEngine(CXInterpreter I) {

Check warning on line 34 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L34

Added line #L34 was not covered by tests
#ifdef USE_CLING
return nullptr;
#else
return reinterpret_cast<LLVMOrcLLJITRef>(const_cast<llvm::orc::LLJIT*>(
static_cast<compat::Interpreter*>(I)->getExecutionEngine()));
static_cast<Cpp::Interpreter*>(I)
->getExecutionEngine())); // NOLINT(cppcoreguidelines-pro-type-const-cast,

Check warning on line 40 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L40

Added line #L40 was not covered by tests
// cppcoreguidelines-pro-type-reinterpret-cast)
#endif
}

void clang_interpreter_addSearchPath(CXInterpreter I, const char* dir,

Check warning on line 45 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L45

Added line #L45 was not covered by tests
Expand All @@ -68,7 +62,7 @@ const char* clang_interpreter_getResourceDir(CXInterpreter I) {

enum CXErrorCode clang_interpreter_declare(CXInterpreter I, const char* code,

Check warning on line 63 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L63

Added line #L63 was not covered by tests
bool silent) {
auto interp = static_cast<compat::Interpreter*>(I);
auto* interp = static_cast<compat::Interpreter*>(I);
auto& diag = interp->getSema().getDiagnostics();

Check warning on line 66 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L65-L66

Added lines #L65 - L66 were not covered by tests

bool is_silent_old = diag.getSuppressAllDiagnostics();

Check warning on line 68 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L68

Added line #L68 was not covered by tests
Expand All @@ -79,16 +73,16 @@ enum CXErrorCode clang_interpreter_declare(CXInterpreter I, const char* code,

if (result)
return CXError_Failure;

Check warning on line 75 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L74-L75

Added lines #L74 - L75 were not covered by tests
else
return CXError_Success;

return CXError_Success;

Check warning on line 77 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L77

Added line #L77 was not covered by tests
}

enum CXErrorCode clang_interpreter_process(CXInterpreter I, const char* code) {
auto result = static_cast<compat::Interpreter*>(I)->process(code);
if (result)
return CXError_Failure;

Check warning on line 83 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L80-L83

Added lines #L80 - L83 were not covered by tests
else
return CXError_Success;

return CXError_Success;

Check warning on line 85 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L85

Added line #L85 was not covered by tests
}

CXValue clang_createValue() {

Check warning on line 88 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L88

Added line #L88 was not covered by tests
Expand All @@ -103,25 +97,27 @@ CXValue clang_createValue() {

void clang_value_dispose(CXValue V) {

Check warning on line 98 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L98

Added line #L98 was not covered by tests
#ifdef USE_CLING
delete static_cast<cling::Value*>(V);
delete static_cast<cling::Value*>(
V); // NOLINT(cppcoreguidelines-owning-memory)
#else
delete static_cast<clang::Value*>(V);
delete static_cast<clang::Value*>(
V); // NOLINT(cppcoreguidelines-owning-memory)

Check warning on line 104 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L103-L104

Added lines #L103 - L104 were not covered by tests
#endif // USE_CLING
}

enum CXErrorCode clang_interpreter_evaluate(CXInterpreter I, const char* code,

Check warning on line 108 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L108

Added line #L108 was not covered by tests
CXValue V) {
#ifdef USE_CLING
auto val = static_cast<cling::Value*>(V);
auto* val = static_cast<cling::Value*>(V);
#else
auto val = static_cast<clang::Value*>(V);
auto* val = static_cast<clang::Value*>(V);

Check warning on line 113 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L113

Added line #L113 was not covered by tests
#endif // USE_CLING

auto result = static_cast<compat::Interpreter*>(I)->evaluate(code, *val);
if (result)
return CXError_Failure;

Check warning on line 118 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L116-L118

Added lines #L116 - L118 were not covered by tests
else
return CXError_Success;

return CXError_Success;

Check warning on line 120 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L120

Added line #L120 was not covered by tests
}

CXString clang_interpreter_lookupLibrary(CXInterpreter I,

Check warning on line 123 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L123

Added line #L123 was not covered by tests
Expand Down Expand Up @@ -197,3 +193,24 @@ CXJitCall clang_jitcall_makeFunctionCallable(CXInterpreter I,
return reinterpret_cast<CXJitCall>(
std::make_unique<Cpp::JitCall>(J).release());

Check warning on line 194 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L194

Added line #L194 was not covered by tests
}

namespace Cpp {
intptr_t GetVariableOffsetImpl(compat::Interpreter& I, TCppScope_t var);
}

intptr_t clang_jitcall_getVariableOffset(CXInterpreter I, CXScope var) {
return Cpp::GetVariableOffsetImpl(*static_cast<compat::Interpreter*>(I), var);

Check warning on line 202 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L201-L202

Added lines #L201 - L202 were not covered by tests
}

namespace Cpp {
bool InsertOrReplaceJitSymbolImpl(compat::Interpreter& I,
const char* linker_mangled_name,
uint64_t address);
}

bool clang_jitcall_insertOrReplaceJitSymbol(CXInterpreter I,

Check warning on line 211 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L211

Added line #L211 was not covered by tests
const char* linker_mangled_name,
uint64_t address) {
return Cpp::InsertOrReplaceJitSymbolImpl(
*static_cast<compat::Interpreter*>(I), linker_mangled_name, address);

Check warning on line 215 in lib/Interpreter/CXCppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/Interpreter/CXCppInterOp.cpp#L214-L215

Added lines #L214 - L215 were not covered by tests
}
21 changes: 15 additions & 6 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,7 @@ namespace Cpp {
return 0;
}

intptr_t GetVariableOffset(TCppScope_t var)
{
intptr_t GetVariableOffsetImpl(compat::Interpreter& I, TCppScope_t var) {
if (!var)
return 0;

Expand All @@ -1177,7 +1176,7 @@ namespace Cpp {
compat::maybeMangleDeclName(GD, mangledName);
void* address = llvm::sys::DynamicLibrary::SearchForAddressOfSymbol(
mangledName.c_str());
auto &I = getInterp();

if (!address)
address = I.getAddressOfGlobal(GD);
if (!address) {
Expand Down Expand Up @@ -1227,6 +1226,10 @@ namespace Cpp {
return 0;
}

intptr_t GetVariableOffset(TCppScope_t var) {
return GetVariableOffsetImpl(getInterp(), var);
}

// Check if the Access Specifier of the variable matches the provided value.
bool CheckVariableAccess(TCppScope_t var, AccessSpecifier AS)
{
Expand Down Expand Up @@ -2765,8 +2768,9 @@ namespace Cpp {
return DLM->searchLibrariesForSymbol(mangled_name, search_system);
}

bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
uint64_t address) {
bool InsertOrReplaceJitSymbolImpl(compat::Interpreter& I,
const char* linker_mangled_name,
uint64_t address) {
// FIXME: This approach is problematic since we could replace a symbol
// whose address was already taken by clients.
//
Expand All @@ -2792,7 +2796,6 @@ namespace Cpp {
using namespace llvm;
using namespace llvm::orc;

auto& I = getInterp();
auto Symbol = compat::getSymbolAddress(I, linker_mangled_name);
llvm::orc::LLJIT& Jit = *compat::getExecutionEngine(I);
llvm::orc::ExecutionSession& ES = Jit.getExecutionSession();
Expand Down Expand Up @@ -2853,6 +2856,12 @@ namespace Cpp {
return false;
}

bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
uint64_t address) {
return InsertOrReplaceJitSymbolImpl(getInterp(), linker_mangled_name,
address);
}

std::string ObjToString(const char *type, void *obj) {
return getInterp().toString(type, obj);
}
Expand Down

0 comments on commit 4eabf46

Please sign in to comment.