Skip to content

Commit 3964653

Browse files
committed
Add more helper functions
Added: - CreateInterpreter - GetSema - Process - ObjToString
1 parent e04d404 commit 3964653

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

include/clang/Interpreter/InterOp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,25 @@ namespace InterOp {
151151
CallFuncWrapper_t GetFunctionCallWrapper(TInterp_t interp,
152152
TCppFunction_t func);
153153

154+
TInterp_t CreateInterpreter();
155+
156+
TCppSema_t GetSema(TInterp_t interp);
157+
154158
void AddSearchPath(TInterp_t interp, const char *dir, bool isUser = true,
155159
bool prepend = false);
156160

157161
void AddIncludePath(TInterp_t interp, const char *dir);
158162

159163
TCppIndex_t Declare(TInterp_t interp, const char *code, bool silent = false);
160164

165+
void Process(TInterp_t interp, const char *code);
166+
161167
const std::string LookupLibrary(TInterp_t interp, const char *lib_name);
162168

163169
bool LoadLibrary(TInterp_t interp, const char *lib_path, bool lookup = true);
164170

171+
std::string ObjToString(TInterp_t interp, const char *type, void *obj);
172+
165173
TCppScope_t InstantiateClassTemplate(TInterp_t interp, const char *tmpl_name);
166174

167175
std::vector<std::string> GetAllCppNames(TCppScope_t scope);

lib/Interpreter/InterOp.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "clang/AST/GlobalDecl.h"
1919
#include "clang/AST/Mangle.h"
2020
#include "clang/AST/RecordLayout.h"
21+
#include "clang/Basic/Version.h"
22+
#include "clang/Config/config.h"
2123
#include "clang/Frontend/CompilerInstance.h"
2224
#include "clang/Sema/Sema.h"
2325
#include "clang/Sema/Lookup.h"
@@ -1828,6 +1830,50 @@ namespace InterOp {
18281830
return 0;
18291831
}
18301832

1833+
namespace {
1834+
1835+
std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
1836+
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
1837+
}
1838+
1839+
std::string MakeResourcesPath() {
1840+
// Dir is bin/ or lib/, depending on where BinaryPath is.
1841+
void *MainAddr = (void *)(intptr_t)GetExecutablePath;
1842+
std::string BinaryPath = GetExecutablePath(/*Argv0=*/nullptr, MainAddr);
1843+
1844+
// build/tools/clang/unittests/Interpreter/Executable -> build/
1845+
llvm::StringRef Dir = llvm::sys::path::parent_path(BinaryPath);
1846+
1847+
Dir = llvm::sys::path::parent_path(Dir);
1848+
Dir = llvm::sys::path::parent_path(Dir);
1849+
Dir = llvm::sys::path::parent_path(Dir);
1850+
Dir = llvm::sys::path::parent_path(Dir);
1851+
//Dir = sys::path::parent_path(Dir);
1852+
llvm::SmallString<128> P(Dir);
1853+
llvm::sys::path::append(P, llvm::Twine("lib") + CLANG_LIBDIR_SUFFIX,
1854+
"clang", CLANG_VERSION_STRING);
1855+
1856+
return std::string(P.str());
1857+
}
1858+
1859+
}
1860+
1861+
TInterp_t CreateInterpreter() {
1862+
std::string MainExecutableName =
1863+
llvm::sys::fs::getMainExecutable(nullptr, nullptr);
1864+
std::string ResourceDir = MakeResourcesPath();
1865+
std::vector<const char *> ClingArgv = {"-resource-dir", ResourceDir.c_str(),
1866+
"-std=c++14"};
1867+
ClingArgv.insert(ClingArgv.begin(), MainExecutableName.c_str());
1868+
return (TInterp_t) new cling::Interpreter(ClingArgv.size(), &ClingArgv[0]);
1869+
}
1870+
1871+
TCppSema_t GetSema(TInterp_t interp) {
1872+
auto* I = (cling::Interpreter*)interp;
1873+
1874+
return (TCppSema_t) &I->getSema();
1875+
}
1876+
18311877
void AddSearchPath(TInterp_t interp, const char *dir, bool isUser,
18321878
bool prepend) {
18331879
auto* I = (cling::Interpreter*)interp;
@@ -1870,6 +1916,13 @@ namespace InterOp {
18701916

18711917
return I->declare(code);
18721918
}
1919+
1920+
void Process(TInterp_t interp, const char *code) {
1921+
auto* I = (cling::Interpreter*)interp;
1922+
1923+
I->process(code);
1924+
}
1925+
18731926
const std::string LookupLibrary(TInterp_t interp, const char *lib_name) {
18741927
auto* I = (cling::Interpreter*)interp;
18751928

@@ -1884,6 +1937,11 @@ namespace InterOp {
18841937
return res == cling::Interpreter::kSuccess;
18851938
}
18861939

1940+
std::string ObjToString(TInterp_t interp, const char *type, void *obj) {
1941+
auto* I = (cling::Interpreter*)interp;
1942+
return I->toString(type, obj);
1943+
}
1944+
18871945
TCppScope_t InstantiateClassTemplate(TInterp_t interp, const char *tmpl_name) {
18881946
auto* I = (cling::Interpreter*)interp;
18891947

0 commit comments

Comments
 (0)