Skip to content

Commit

Permalink
Fix build paths. Fix inspect to work with CppInterOp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-penev committed Nov 19, 2023
1 parent 9b353e7 commit ad5f1b4
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
26 changes: 20 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")

set(XEUS_CPP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)

if ("${CMAKE_INSTALL_INCLUDEDIR}" STREQUAL "")
set(CMAKE_INSTALL_INCLUDEDIR "include")
endif()
if ("${CMAKE_INSTALL_LIBDIR}" STREQUAL "")
set(CMAKE_INSTALL_LIBDIR "lib")
endif()
if ("${CMAKE_INSTALL_BINDIR}" STREQUAL "")
set(CMAKE_INSTALL_BINDIR "bin")
endif()

# Versionning
# ===========

Expand Down Expand Up @@ -198,7 +208,8 @@ include(CheckCXXCompilerFlag)

string(TOUPPER "${CMAKE_BUILD_TYPE}" U_CMAKE_BUILD_TYPE)

set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib; ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
list(REMOVE_DUPLICATES CMAKE_INSTALL_RPATH)

macro(xeus_cpp_set_common_options target_name)
if (MSVC)
Expand Down Expand Up @@ -287,7 +298,7 @@ macro(xeus_cpp_create_target target_name linkage output_name)
set(XEUS_CPP_XEUS_TARGET xeus-static)
endif ()

target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl)
target_link_libraries(${target_name} PUBLIC ${XEUS_CPP_XEUS_TARGET} clangCppInterOp pugixml argparse::argparse xtl Python::Python)
if (WIN32 OR CYGWIN)
#
elseif (APPLE)
Expand Down Expand Up @@ -390,17 +401,20 @@ set(XEUS_CPP_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NA
if (XEUS_CPP_BUILD_SHARED)
install(TARGETS ${XEUS_CPP_TARGETS}
EXPORT ${PROJECT_NAME}-targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp)
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_BINDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/xeus-cpp)

# Makes the project importable from the build directory
export(EXPORT ${PROJECT_NAME}-targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
endif ()

# Install xcpp
if ("${CMAKE_VENV_PATH}" STREQUAL "")
set(CMAKE_VENV_PATH "${CMAKE_INSTALL_PREFIX}")
endif()
if (XEUS_CPP_BUILD_EXECUTABLE)
install(TARGETS xcpp
RUNTIME DESTINATION ${CMAKE_VENV_PATH}/bin)
Expand Down
2 changes: 1 addition & 1 deletion include/xeus-cpp/xinterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <vector>

//#include <clang/Interpreter/Interpreter.h>
#include "clang/Interpreter/CppInterOp.h"
#include "clang/Interpreter/CppInterOp.h" // from CppInterOp package

#include <nlohmann/json.hpp>

Expand Down
63 changes: 48 additions & 15 deletions src/xinspect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#ifndef XEUS_CPP_INSPECT_HPP
#define XEUS_CPP_INSPECT_HPP

#include <filesystem>
#include <fstream>
#include <string>


#include <pugixml.hpp>

#include <xtl/xsystem.hpp>
Expand All @@ -23,8 +23,11 @@
#include "xdemangle.hpp"
#include "xparser.hpp"

#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
//#include "llvm/Support/FileSystem.h"
//#include "llvm/Support/Path.h"

//#include "clang/Interpreter/CppInterOp.h"


namespace xcpp
{
Expand Down Expand Up @@ -81,27 +84,57 @@ namespace xcpp
}
};

std::string find_type(const std::string& expression, clang::Interpreter& interpreter)

std::string find_type_slow(const std::string& expression) {
static unsigned long long var_count = 0;

if (auto type = Cpp::GetType(expression))
return Cpp::GetQualifiedName(type);

// Here we might need to deal with integral types such as 3.14.

std::string id = "__Xeus_GetType_" + std::to_string(var_count++);
std::string using_clause = "using " + id + " = __typeof__(" + expression + ");\n";

if (!Cpp::Declare(using_clause.c_str(), /*silent=*/false)) {
Cpp::TCppScope_t lookup = Cpp::GetNamed(id, 0);
Cpp::TCppType_t lookup_ty = Cpp::GetTypeFromScope(lookup);
return Cpp::GetQualifiedCompleteName(Cpp::GetCanonicalType(lookup_ty));
}
return "";
}
/*
std::string find_type(const std::string& expression)
{
auto PTU = interpreter.Parse(expression + ";");
if (llvm::Error Err = PTU.takeError()) {
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
return "";
}
clang::Decl *D = *PTU->TUPart->decls_begin();
if (!llvm::isa<clang::TopLevelStmtDecl>(D))
return "";
clang::Decl *D = *PTU->TUPart->decls_begin();
if (!llvm::isa<clang::TopLevelStmtDecl>(D))
return "";
clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt());
clang::Expr *E = llvm::cast<clang::Expr>(llvm::cast<clang::TopLevelStmtDecl>(D)->getStmt());
clang::QualType QT = E->getType();
clang::QualType QT = E->getType();
return QT.getAsString();
}

*/
static nl::json read_tagconfs(const char* path)
{
nl::json result = nl::json::array();
for (auto &entry: std::filesystem::directory_iterator(path)) {
if (entry.path().extension() != ".json")
continue;
std::ifstream i(entry.path());
nl::json json_entry;
i >> json_entry;
result.emplace_back(std::move(json_entry));
}
return result;
/*
std::error_code EC;
for (llvm::sys::fs::directory_iterator File(path, EC), FileEnd;
File != FileEnd && !EC; File.increment(EC)) {
Expand All @@ -115,19 +148,19 @@ namespace xcpp
result.emplace_back(std::move(entry));
}
return result;
*/
}

std::pair<bool, std::smatch> is_inspect_request(const std::string code, std::regex re)
std::pair<bool, std::smatch> is_inspect_request(const std::string code, std::regex re)
{
std::smatch inspect;
if (std::regex_search(code, inspect, re)){
return std::make_pair(true, inspect);
}
return std::make_pair(false, inspect);

}

void inspect(const std::string& code, nl::json& kernel_res, clang::Interpreter& interpreter)
void inspect(const std::string& code, nl::json& kernel_res)
{
std::string tagconf_dir = XCPP_TAGCONFS_DIR;
std::string tagfiles_dir = XCPP_TAGFILES_DIR;
Expand All @@ -150,7 +183,7 @@ namespace xcpp
// Method or variable of class found (xxxx.yyyy)
if (std::regex_search(to_inspect, method, std::regex(R"((.*)\.(\w*)$)")))
{
std::string typename_ = find_type(method[1], interpreter);
std::string typename_ = find_type_slow(method[1]);

if (!typename_.empty())
{
Expand Down Expand Up @@ -184,7 +217,7 @@ namespace xcpp
}
else
{
std::string typename_ = find_type(to_inspect, interpreter);
std::string typename_ = find_type_slow(to_inspect);
find_string = (typename_.empty()) ? to_inspect : typename_;
}

Expand Down
14 changes: 7 additions & 7 deletions src/xinterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "xeus-cpp/xmagics.hpp"

#include "xinput.hpp"
// #include "xinspect.hpp"
#include "xinspect.hpp"
// #include "xmagics/executable.hpp"
// #include "xmagics/execution.hpp"
#include "xmagics/os.hpp"
Expand Down Expand Up @@ -124,12 +124,12 @@ namespace xcpp
// Attempt normal evaluation
try
{
std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)";
std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)");
std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)";
std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)");
auto inspect_request = is_inspect_request(code, re);
if (inspect_request.first)
inspect(inspect_request.second[0], kernel_res, *m_interpreter);
inspect(inspect_request.second[0], kernel_res);

Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
compilation_result = Cpp::Process(code.c_str());
Expand All @@ -148,7 +148,7 @@ namespace xcpp
errorlevel = 1;
ename = "Error :";
}

if (compilation_result)
{
errorlevel = 1;
Expand Down Expand Up @@ -227,7 +227,7 @@ namespace xcpp
auto inspect_request = is_inspect_request(code.substr(0, cursor_pos), re);
if (inspect_request.first)
{
inspect(inspect_request.second[0], kernel_res, *m_interpreter);
inspect(inspect_request.second[0], kernel_res);
}
return kernel_res;
}
Expand Down

0 comments on commit ad5f1b4

Please sign in to comment.