Skip to content

Errors from imported files #1294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from b import j
print(j)
5 changes: 4 additions & 1 deletion examples/expr2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from a import i

def main0():
x: i32
x = (2+3)*5
x = i
x = 2.14
print(x)

main0()
Expand Down
19 changes: 19 additions & 0 deletions expr2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# from tmp import hi
from tmp2 import hi2

def main0():
x: i32
x = (2+3)*5
print(x)
hi2()
# y: i32 = 3.1
# # hi()
# p: i32 = 4.5
# # hi2()
# print(y, p)

main0()

# Not implemented yet in LPython:
#if __name__ == "__main__":
# main()
34 changes: 18 additions & 16 deletions src/bin/lpython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ int emit_asr(const std::string &infile,

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -206,7 +206,7 @@ int emit_cpp(const std::string &infile,

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -246,7 +246,7 @@ int emit_c(const std::string &infile,

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -286,7 +286,7 @@ int emit_wat(const std::string &infile,

diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -329,7 +329,7 @@ int get_symbols (const std::string &infile,
if (r1.ok) {
LFortran::LPython::AST::ast_t* ast = r1.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
x = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
x = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
if (!x.ok) {
std::cout << "{}\n";
return 0;
Expand Down Expand Up @@ -421,7 +421,7 @@ int get_errors (const std::string &infile,
if (r1.ok) {
LFortran::LPython::AST::ast_t* ast = r1.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
}
std::vector<LFortran::LPython::error_highlight> diag_lists;
LFortran::LPython::error_highlight h;
Expand Down Expand Up @@ -532,7 +532,7 @@ int emit_llvm(const std::string &infile,
LFortran::LPython::AST::ast_t* ast = r.result;
diagnostics.diagnostics.clear();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -572,6 +572,7 @@ int compile_python_to_object_file(
auto file_reading_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("File reading", std::chrono::duration<double, std::milli>(file_reading_end - file_reading_start).count()));
lm.init_simple(input);
std::cout << "lpython.cpp: " << lm.files[0].in_filename << std::endl;
auto parsing_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::LPython::AST::ast_t*> r = parse_python_file(
al, runtime_library_dir, infile, diagnostics, compiler_options.new_parser);
Expand All @@ -588,11 +589,12 @@ int compile_python_to_object_file(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options,
!(arg_c && compiler_options.disable_main), infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options,
!(arg_c && compiler_options.disable_main), infile, input);

auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cout << "input: \n" << input << std::endl;
std::cerr << diagnostics.render(input, lm, compiler_options);
if (!r1.ok) {
LFORTRAN_ASSERT(diagnostics.has_error())
Expand Down Expand Up @@ -672,7 +674,7 @@ int compile_to_binary_wasm(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -737,7 +739,7 @@ int compile_to_binary_x86(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -802,7 +804,7 @@ int compile_to_binary_wasm_to_x86(
diagnostics.diagnostics.clear();
auto ast_to_asr_start = std::chrono::high_resolution_clock::now();
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, diagnostics, compiler_options, true, infile);
r1 = LFortran::LPython::python_ast_to_asr(al, *ast, lm, diagnostics, compiler_options, true, infile, input);
auto ast_to_asr_end = std::chrono::high_resolution_clock::now();
times.push_back(std::make_pair("AST to ASR", std::chrono::duration<double, std::milli>(ast_to_asr_end - ast_to_asr_start).count()));
std::cerr << diagnostics.render(input, lm, compiler_options);
Expand Down Expand Up @@ -1067,7 +1069,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_asr_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input", input);
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
out += LFortran::pickle(*asr.result, compiler_options.use_colors, compiler_options.indent,
Expand All @@ -1085,7 +1087,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wat_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input", input);
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
LFortran::Result<LFortran::Vec<uint8_t>>
Expand All @@ -1112,7 +1114,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_cpp_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input", input);
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
auto res = LFortran::asr_to_cpp(al, *asr.result, diagnostics,
Expand Down Expand Up @@ -1150,7 +1152,7 @@ EMSCRIPTEN_KEEPALIVE char* emit_wasm_from_source(char *input) {
if (ast.ok) {
auto casted_ast = (LFortran::LPython::AST::ast_t*)ast.result;
LFortran::Result<LFortran::ASR::TranslationUnit_t*>
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input");
asr = LFortran::LPython::python_ast_to_asr(al, *casted_ast, diagnostics, compiler_options, true, "input", input);
out = diagnostics.render(input, lm, compiler_options);
if (asr.ok) {
LFortran::Result<LFortran::Vec<uint8_t>>
Expand Down
6 changes: 3 additions & 3 deletions src/libasr/codegen/asr_to_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,9 @@ class ASRToLLVMVisitor : public ASR::BaseVisitor<ASRToLLVMVisitor>
void debug_get_line_column(const uint32_t &loc_first,
uint32_t &line, uint32_t &column) {
LocationManager lm;
lm.in_filename = infile;
lm.init_simple(LFortran::read_file(infile));
lm.pos_to_linecol(lm.output_to_input_pos(loc_first, false), line, column);
lm.lm.in_filename = infile;
lm.lm.init_simple(LFortran::read_file(infile));
lm.lm.pos_to_linecol(lm.lm.output_to_input_pos(loc_first, false), line, column);
}

template <typename T>
Expand Down
18 changes: 9 additions & 9 deletions src/libasr/diagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ std::string get_line(std::string str, int n)

void populate_span(diag::Span &s, const LocationManager &lm,
const std::string &input) {
lm.pos_to_linecol(lm.output_to_input_pos(s.loc.first, false),
s.first_line, s.first_column);
lm.pos_to_linecol(lm.output_to_input_pos(s.loc.last, true),
s.last_line, s.last_column);
s.filename = lm.in_filename;
lm.pos_to_linecol(s.loc.first,
s.first_line, s.first_column, s.filename);
lm.pos_to_linecol(s.loc.last,
s.last_line, s.last_column, s.filename);
// s.filename = lm.in_filename;
for (uint32_t i = s.first_line; i <= s.last_line; i++) {
s.source_code.push_back(get_line(input, i));
}
Expand Down Expand Up @@ -178,7 +178,7 @@ std::string render_diagnostic_human(const Diagnostic &d, bool use_colors) {
std::string type_color = "";
switch (d.level) {
case (Level::Error):
primary_color = red_bold;
primary_color = red_bold;
type_color = primary_color;
switch (d.stage) {
case (Stage::CPreprocessor):
Expand All @@ -205,17 +205,17 @@ std::string render_diagnostic_human(const Diagnostic &d, bool use_colors) {
}
break;
case (Level::Warning):
primary_color = yellow_bold;
primary_color = yellow_bold;
type_color = primary_color;
message_type = "warning";
break;
case (Level::Note):
primary_color = bold;
primary_color = bold;
type_color = primary_color;
message_type = "note";
break;
case (Level::Help):
primary_color = bold;
primary_color = bold;
type_color = primary_color;
message_type = "help";
break;
Expand Down
Loading