From c2155654d94cf655ee5473abe247c9d48f9a60c6 Mon Sep 17 00:00:00 2001 From: Gagandeep Singh Date: Fri, 21 Apr 2023 23:03:45 +0530 Subject: [PATCH] Respect --disable-main in global_stmts pass --- run_tests.py | 7 ++- src/libasr/pass/global_stmts_program.cpp | 22 ++++---- src/libasr/utils.h | 1 + src/lpython/semantics/python_ast_to_asr.cpp | 1 + .../reference/c-import_order_01-3ebf3c3.json | 13 +++++ .../c-import_order_01-3ebf3c3.stdout | 52 +++++++++++++++++++ tests/tests.toml | 5 ++ 7 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 tests/reference/c-import_order_01-3ebf3c3.json create mode 100644 tests/reference/c-import_order_01-3ebf3c3.stdout diff --git a/run_tests.py b/run_tests.py index 1e01b43656..63310c7510 100755 --- a/run_tests.py +++ b/run_tests.py @@ -28,6 +28,7 @@ def is_included(backend): wat = is_included("wat") run = is_included("run") run_with_dbg = is_included("run_with_dbg") + disable_main = is_included("disable_main") pass_ = test.get("pass", None) optimization_passes = ["flip_sign", "div_to_mul", "fma", "sign_from_value", "inline_function_calls", "loop_unroll", @@ -111,7 +112,11 @@ def is_included(backend): filename, update_reference, extra_args) if c: - run_test(filename, "c", "lpython --no-color --show-c {infile}", + if disable_main: + run_test(filename, "c", "lpython --no-color --disable-main --show-c {infile}", + filename, update_reference, extra_args) + else: + run_test(filename, "c", "lpython --no-color --show-c {infile}", filename, update_reference, extra_args) if wat: run_test(filename, "wat", "lpython --no-color --show-wat {infile}", diff --git a/src/libasr/pass/global_stmts_program.cpp b/src/libasr/pass/global_stmts_program.cpp index 1e3d1c1331..3c63dde962 100644 --- a/src/libasr/pass/global_stmts_program.cpp +++ b/src/libasr/pass/global_stmts_program.cpp @@ -30,7 +30,7 @@ void pass_wrap_global_stmts_into_program(Allocator &al, bool call_main_program = unit.n_items > 0; pass_wrap_global_stmts_into_function(al, unit, pass_options); pass_wrap_global_syms_into_module(al, unit, pass_options); - if( call_main_program ) { + if( call_main_program && !pass_options.disable_main ) { // Call `_lpython_main_program` function ASR::Module_t *mod = ASR::down_cast( unit.m_global_scope->get_symbol("_global_symbols")); @@ -55,15 +55,17 @@ void pass_wrap_global_stmts_into_program(Allocator &al, prog_dep.push_back(al, s2c(al, "_global_symbols")); } - ASR::asr_t *prog = ASR::make_Program_t( - al, unit.base.base.loc, - /* a_symtab */ current_scope, - /* a_name */ s2c(al, prog_name), - prog_dep.p, - prog_dep.n, - /* a_body */ prog_body.p, - /* n_body */ prog_body.n); - unit.m_global_scope->add_symbol(prog_name, ASR::down_cast(prog)); + if( !pass_options.disable_main ) { + ASR::asr_t *prog = ASR::make_Program_t( + al, unit.base.base.loc, + /* a_symtab */ current_scope, + /* a_name */ s2c(al, prog_name), + prog_dep.p, + prog_dep.n, + /* a_body */ prog_body.p, + /* n_body */ prog_body.n); + unit.m_global_scope->add_symbol(prog_name, ASR::down_cast(prog)); + } } } // namespace LCompilers diff --git a/src/libasr/utils.h b/src/libasr/utils.h index 908eed9659..ee71c94162 100644 --- a/src/libasr/utils.h +++ b/src/libasr/utils.h @@ -82,6 +82,7 @@ namespace LCompilers { bool fast = false; // is fast flag enabled. bool verbose = false; // For developer debugging bool pass_cumulative = false; // Apply passes cumulatively + bool disable_main = false; }; } diff --git a/src/lpython/semantics/python_ast_to_asr.cpp b/src/lpython/semantics/python_ast_to_asr.cpp index 145d8b2732..72347ba64a 100644 --- a/src/lpython/semantics/python_ast_to_asr.cpp +++ b/src/lpython/semantics/python_ast_to_asr.cpp @@ -6728,6 +6728,7 @@ Result python_ast_to_asr(Allocator &al, LocationManager // If it is a main module, turn it into a program // Note: we can modify this behavior for interactive mode later LCompilers::PassOptions pass_options; + pass_options.disable_main = compiler_options.disable_main; if (compiler_options.disable_main) { if (tu->n_items > 0) { diagnostics.add(diag::Diagnostic( diff --git a/tests/reference/c-import_order_01-3ebf3c3.json b/tests/reference/c-import_order_01-3ebf3c3.json new file mode 100644 index 0000000000..5477786c56 --- /dev/null +++ b/tests/reference/c-import_order_01-3ebf3c3.json @@ -0,0 +1,13 @@ +{ + "basename": "c-import_order_01-3ebf3c3", + "cmd": "lpython --no-color --disable-main --show-c {infile}", + "infile": "tests/../integration_tests/import_order_01.py", + "infile_hash": "b6f67090973a5fce0778dbdd86fdb12272a8da5bbae2f50051df34e5", + "outfile": null, + "outfile_hash": null, + "stdout": "c-import_order_01-3ebf3c3.stdout", + "stdout_hash": "d844d0d693ba8a9867f29b17de6f66c99f2096b7a14efeb4c5d698d2", + "stderr": null, + "stderr_hash": null, + "returncode": 0 +} \ No newline at end of file diff --git a/tests/reference/c-import_order_01-3ebf3c3.stdout b/tests/reference/c-import_order_01-3ebf3c3.stdout new file mode 100644 index 0000000000..0e7c567af1 --- /dev/null +++ b/tests/reference/c-import_order_01-3ebf3c3.stdout @@ -0,0 +1,52 @@ +#include + +#include +#include +#include +#include +#include + +#define ASSERT(cond) \ + { \ + if (!(cond)) { \ + printf("%s%s", "ASSERT failed: ", __FILE__); \ + printf("%s%s", "\nfunction ", __func__); \ + printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \ + printf("%s%s", #cond, "\n"); \ + exit(1); \ + } \ + } +#define ASSERT_MSG(cond, msg) \ + { \ + if (!(cond)) { \ + printf("%s%s", "ASSERT failed: ", __FILE__); \ + printf("%s%s", "\nfunction ", __func__); \ + printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \ + printf("%s%s", #cond, "\n"); \ + printf("%s", "ERROR MESSAGE:\n"); \ + printf("%s%s", msg, "\n"); \ + exit(1); \ + } \ + } + + +struct dimension_descriptor +{ + int32_t lower_bound, length; +}; + +// Implementations +int32_t f() +{ + int32_t _lpython_return_variable; + _lpython_return_variable = 42; + return _lpython_return_variable; +} + +void main1() +{ + int32_t a; + a = f(); + printf("%d\n", a); +} + diff --git a/tests/tests.toml b/tests/tests.toml index ceec27f5a8..549370df82 100644 --- a/tests/tests.toml +++ b/tests/tests.toml @@ -201,6 +201,11 @@ asr = true filename = "../integration_tests/test_import_02.py" c = true +[[test]] +filename = "../integration_tests/import_order_01.py" +c = true +disable_main = true + [[test]] filename = "../integration_tests/vec_01.py" asr = true