Skip to content

Commit

Permalink
repl: Fix source buffer handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlauko committed Nov 16, 2023
1 parent 0a75f62 commit de37211
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
7 changes: 2 additions & 5 deletions include/vast/repl/codegen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ VAST_UNRELAX_WARNINGS

namespace vast::repl::codegen {

std::unique_ptr< clang::ASTUnit > ast_from_source(const std::string &source);
std::unique_ptr< clang::ASTUnit > ast_from_source(string_ref source);

// TODO(Heno): return buffer
std::string get_source(std::filesystem::path source);

owning_module_ref emit_module(const std::string &source, mcontext_t *ctx);
owning_module_ref emit_module(const std::filesystem::path &source, mcontext_t *ctx);

} // namespace vast::repl::codegen
4 changes: 3 additions & 1 deletion include/vast/repl/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ namespace vast::repl

void check_source(const state_t &state);

const std::string &get_source(const state_t &state);
using maybe_memory_buffer = llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > >;

maybe_memory_buffer get_source_buffer(const state_t &state);

void check_and_emit_module(state_t &state);

Expand Down
4 changes: 3 additions & 1 deletion include/vast/repl/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
#include "vast/Tower/Tower.hpp"
#include "vast/repl/common.hpp"

#include <filesystem>

namespace vast::repl {

struct state_t {
explicit state_t(mcontext_t &ctx) : ctx(ctx) {}

bool exit = false;

std::optional< std::string > source;
std::optional< std::filesystem::path > source;

mcontext_t &ctx;
std::optional< tw::default_tower > tower;
Expand Down
17 changes: 3 additions & 14 deletions tools/vast-repl/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,10 @@ namespace vast::cc {

namespace vast::repl::codegen {

std::string slurp(std::ifstream& in) {
std::ostringstream sstr;
sstr << in.rdbuf();
return sstr.str();
}

std::unique_ptr< clang::ASTUnit > ast_from_source(const std::string &source) {
std::unique_ptr< clang::ASTUnit > ast_from_source(string_ref source) {
return clang::tooling::buildASTFromCode(source);
}

std::string get_source(std::filesystem::path source) {
std::ifstream in(source);
return slurp(in);
}

static void error_handler(void *user_data, const char *msg, bool get_crash_diag) {
auto &diags = *static_cast< clang::DiagnosticsEngine* >(user_data);

Expand All @@ -46,9 +35,9 @@ namespace vast::repl::codegen {
llvm::sys::RunInterruptHandlers();
}

owning_module_ref emit_module(const std::string &/* source */, mcontext_t */* mctx */) {
owning_module_ref emit_module(const std::filesystem::path &source, mcontext_t */* mctx */) {
// TODO setup args from repl state
const char *ccargs = {""};
std::vector< const char * > ccargs = { source.c_str() };
vast::cc::buffered_diagnostics diags(ccargs);

auto comp = std::make_unique< cc::compiler_instance >();
Expand Down
32 changes: 21 additions & 11 deletions tools/vast-repl/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,30 @@ namespace vast::repl::cmd {

void check_source(const state_t &state) {
if (!state.source.has_value()) {
VAST_UNREACHABLE("error: missing source");
VAST_ERROR("error: missing source");
}
}

const std::string &get_source(const state_t &state) {
maybe_memory_buffer get_source_buffer(const state_t &state) {
check_source(state);
return state.source.value();

// Open the file using MemoryBuffer
maybe_memory_buffer file_buffer = llvm::MemoryBuffer::getFile(state.source->c_str());

// Check if the file is opened successfully
if (auto errorCode = file_buffer.getError()) {
VAST_ERROR("error: missing source {}", errorCode.message());
}

return file_buffer;
}

void check_and_emit_module(state_t &state) {
if (!state.tower) {
const auto &source = get_source(state);
auto mod = codegen::emit_module(source, &state.ctx);
auto [t, _] = tw::default_tower::get(state.ctx, std::move(mod));
state.tower = std::move(t);
check_source(state);
auto mod = codegen::emit_module(state.source.value(), &state.ctx);
auto [t, _] = tw::default_tower::get(state.ctx, std::move(mod));
state.tower = std::move(t);
}
}

Expand All @@ -47,19 +56,20 @@ namespace vast::repl::cmd {
// load command
//
void load::run(state_t &state) const {
auto source = get_param< source_param >(params);
state.source = codegen::get_source(source.path);
state.source = get_param< source_param >(params).path;
};

//
// show command
//
void show_source(const state_t &state) {
llvm::outs() << get_source(state) << "\n";
auto buff = get_source_buffer(state);
llvm::outs() << buff.get()->getBuffer() << "\n";
}

void show_ast(const state_t &state) {
auto unit = codegen::ast_from_source(get_source(state));
auto buff = get_source_buffer(state);
auto unit = codegen::ast_from_source(buff.get()->getBuffer());
unit->getASTContext().getTranslationUnitDecl()->dump(llvm::outs());
llvm::outs() << "\n";
}
Expand Down

0 comments on commit de37211

Please sign in to comment.