@@ -11,21 +11,30 @@ namespace vast::repl::cmd {
11
11
12
12
void check_source (const state_t &state) {
13
13
if (!state.source .has_value ()) {
14
- VAST_UNREACHABLE (" error: missing source" );
14
+ VAST_ERROR (" error: missing source" );
15
15
}
16
16
}
17
17
18
- const std::string & get_source (const state_t &state) {
18
+ maybe_memory_buffer get_source_buffer (const state_t &state) {
19
19
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;
21
30
}
22
31
23
32
void check_and_emit_module (state_t &state) {
24
33
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);
29
38
}
30
39
}
31
40
@@ -47,19 +56,20 @@ namespace vast::repl::cmd {
47
56
// load command
48
57
//
49
58
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 ;
52
60
};
53
61
54
62
//
55
63
// show command
56
64
//
57
65
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 " ;
59
68
}
60
69
61
70
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 ());
63
73
unit->getASTContext ().getTranslationUnitDecl ()->dump (llvm::outs ());
64
74
llvm::outs () << " \n " ;
65
75
}
0 commit comments