Skip to content

Commit de37211

Browse files
committed
repl: Fix source buffer handling.
1 parent 0a75f62 commit de37211

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

include/vast/repl/codegen.hpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ VAST_UNRELAX_WARNINGS
1818

1919
namespace vast::repl::codegen {
2020

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

23-
// TODO(Heno): return buffer
24-
std::string get_source(std::filesystem::path source);
25-
26-
owning_module_ref emit_module(const std::string &source, mcontext_t *ctx);
23+
owning_module_ref emit_module(const std::filesystem::path &source, mcontext_t *ctx);
2724

2825
} // namespace vast::repl::codegen

include/vast/repl/command.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ namespace vast::repl
3838

3939
void check_source(const state_t &state);
4040

41-
const std::string &get_source(const state_t &state);
41+
using maybe_memory_buffer = llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > >;
42+
43+
maybe_memory_buffer get_source_buffer(const state_t &state);
4244

4345
void check_and_emit_module(state_t &state);
4446

include/vast/repl/state.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
#include "vast/Tower/Tower.hpp"
66
#include "vast/repl/common.hpp"
77

8+
#include <filesystem>
9+
810
namespace vast::repl {
911

1012
struct state_t {
1113
explicit state_t(mcontext_t &ctx) : ctx(ctx) {}
1214

1315
bool exit = false;
1416

15-
std::optional< std::string > source;
17+
std::optional< std::filesystem::path > source;
1618

1719
mcontext_t &ctx;
1820
std::optional< tw::default_tower > tower;

tools/vast-repl/codegen.cpp

+3-14
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,10 @@ namespace vast::cc {
2121

2222
namespace vast::repl::codegen {
2323

24-
std::string slurp(std::ifstream& in) {
25-
std::ostringstream sstr;
26-
sstr << in.rdbuf();
27-
return sstr.str();
28-
}
29-
30-
std::unique_ptr< clang::ASTUnit > ast_from_source(const std::string &source) {
24+
std::unique_ptr< clang::ASTUnit > ast_from_source(string_ref source) {
3125
return clang::tooling::buildASTFromCode(source);
3226
}
3327

34-
std::string get_source(std::filesystem::path source) {
35-
std::ifstream in(source);
36-
return slurp(in);
37-
}
38-
3928
static void error_handler(void *user_data, const char *msg, bool get_crash_diag) {
4029
auto &diags = *static_cast< clang::DiagnosticsEngine* >(user_data);
4130

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

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

5443
auto comp = std::make_unique< cc::compiler_instance >();

tools/vast-repl/command.cpp

+21-11
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,30 @@ namespace vast::repl::cmd {
1111

1212
void check_source(const state_t &state) {
1313
if (!state.source.has_value()) {
14-
VAST_UNREACHABLE("error: missing source");
14+
VAST_ERROR("error: missing source");
1515
}
1616
}
1717

18-
const std::string &get_source(const state_t &state) {
18+
maybe_memory_buffer get_source_buffer(const state_t &state) {
1919
check_source(state);
20-
return state.source.value();
20+
21+
// Open the file using MemoryBuffer
22+
maybe_memory_buffer file_buffer = llvm::MemoryBuffer::getFile(state.source->c_str());
23+
24+
// Check if the file is opened successfully
25+
if (auto errorCode = file_buffer.getError()) {
26+
VAST_ERROR("error: missing source {}", errorCode.message());
27+
}
28+
29+
return file_buffer;
2130
}
2231

2332
void check_and_emit_module(state_t &state) {
2433
if (!state.tower) {
25-
const auto &source = get_source(state);
26-
auto mod = codegen::emit_module(source, &state.ctx);
27-
auto [t, _] = tw::default_tower::get(state.ctx, std::move(mod));
28-
state.tower = std::move(t);
34+
check_source(state);
35+
auto mod = codegen::emit_module(state.source.value(), &state.ctx);
36+
auto [t, _] = tw::default_tower::get(state.ctx, std::move(mod));
37+
state.tower = std::move(t);
2938
}
3039
}
3140

@@ -47,19 +56,20 @@ namespace vast::repl::cmd {
4756
// load command
4857
//
4958
void load::run(state_t &state) const {
50-
auto source = get_param< source_param >(params);
51-
state.source = codegen::get_source(source.path);
59+
state.source = get_param< source_param >(params).path;
5260
};
5361

5462
//
5563
// show command
5664
//
5765
void show_source(const state_t &state) {
58-
llvm::outs() << get_source(state) << "\n";
66+
auto buff = get_source_buffer(state);
67+
llvm::outs() << buff.get()->getBuffer() << "\n";
5968
}
6069

6170
void show_ast(const state_t &state) {
62-
auto unit = codegen::ast_from_source(get_source(state));
71+
auto buff = get_source_buffer(state);
72+
auto unit = codegen::ast_from_source(buff.get()->getBuffer());
6373
unit->getASTContext().getTranslationUnitDecl()->dump(llvm::outs());
6474
llvm::outs() << "\n";
6575
}

0 commit comments

Comments
 (0)