Skip to content

Commit ff4b716

Browse files
authored
Merge pull request #1734 from czgdp1807/disable_main_1
Respect ``--disable-main`` in global_stmts pass
2 parents 5148356 + c215565 commit ff4b716

File tree

7 files changed

+90
-11
lines changed

7 files changed

+90
-11
lines changed

run_tests.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def is_included(backend):
2828
wat = is_included("wat")
2929
run = is_included("run")
3030
run_with_dbg = is_included("run_with_dbg")
31+
disable_main = is_included("disable_main")
3132
pass_ = test.get("pass", None)
3233
optimization_passes = ["flip_sign", "div_to_mul", "fma", "sign_from_value",
3334
"inline_function_calls", "loop_unroll",
@@ -111,7 +112,11 @@ def is_included(backend):
111112
filename, update_reference, extra_args)
112113

113114
if c:
114-
run_test(filename, "c", "lpython --no-color --show-c {infile}",
115+
if disable_main:
116+
run_test(filename, "c", "lpython --no-color --disable-main --show-c {infile}",
117+
filename, update_reference, extra_args)
118+
else:
119+
run_test(filename, "c", "lpython --no-color --show-c {infile}",
115120
filename, update_reference, extra_args)
116121
if wat:
117122
run_test(filename, "wat", "lpython --no-color --show-wat {infile}",

src/libasr/pass/global_stmts_program.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void pass_wrap_global_stmts_into_program(Allocator &al,
3030
bool call_main_program = unit.n_items > 0;
3131
pass_wrap_global_stmts_into_function(al, unit, pass_options);
3232
pass_wrap_global_syms_into_module(al, unit, pass_options);
33-
if( call_main_program ) {
33+
if( call_main_program && !pass_options.disable_main ) {
3434
// Call `_lpython_main_program` function
3535
ASR::Module_t *mod = ASR::down_cast<ASR::Module_t>(
3636
unit.m_global_scope->get_symbol("_global_symbols"));
@@ -55,15 +55,17 @@ void pass_wrap_global_stmts_into_program(Allocator &al,
5555
prog_dep.push_back(al, s2c(al, "_global_symbols"));
5656
}
5757

58-
ASR::asr_t *prog = ASR::make_Program_t(
59-
al, unit.base.base.loc,
60-
/* a_symtab */ current_scope,
61-
/* a_name */ s2c(al, prog_name),
62-
prog_dep.p,
63-
prog_dep.n,
64-
/* a_body */ prog_body.p,
65-
/* n_body */ prog_body.n);
66-
unit.m_global_scope->add_symbol(prog_name, ASR::down_cast<ASR::symbol_t>(prog));
58+
if( !pass_options.disable_main ) {
59+
ASR::asr_t *prog = ASR::make_Program_t(
60+
al, unit.base.base.loc,
61+
/* a_symtab */ current_scope,
62+
/* a_name */ s2c(al, prog_name),
63+
prog_dep.p,
64+
prog_dep.n,
65+
/* a_body */ prog_body.p,
66+
/* n_body */ prog_body.n);
67+
unit.m_global_scope->add_symbol(prog_name, ASR::down_cast<ASR::symbol_t>(prog));
68+
}
6769
}
6870

6971
} // namespace LCompilers

src/libasr/utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace LCompilers {
8282
bool fast = false; // is fast flag enabled.
8383
bool verbose = false; // For developer debugging
8484
bool pass_cumulative = false; // Apply passes cumulatively
85+
bool disable_main = false;
8586
};
8687

8788
}

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6728,6 +6728,7 @@ Result<ASR::TranslationUnit_t*> python_ast_to_asr(Allocator &al, LocationManager
67286728
// If it is a main module, turn it into a program
67296729
// Note: we can modify this behavior for interactive mode later
67306730
LCompilers::PassOptions pass_options;
6731+
pass_options.disable_main = compiler_options.disable_main;
67316732
if (compiler_options.disable_main) {
67326733
if (tu->n_items > 0) {
67336734
diagnostics.add(diag::Diagnostic(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"basename": "c-import_order_01-3ebf3c3",
3+
"cmd": "lpython --no-color --disable-main --show-c {infile}",
4+
"infile": "tests/../integration_tests/import_order_01.py",
5+
"infile_hash": "b6f67090973a5fce0778dbdd86fdb12272a8da5bbae2f50051df34e5",
6+
"outfile": null,
7+
"outfile_hash": null,
8+
"stdout": "c-import_order_01-3ebf3c3.stdout",
9+
"stdout_hash": "d844d0d693ba8a9867f29b17de6f66c99f2096b7a14efeb4c5d698d2",
10+
"stderr": null,
11+
"stderr_hash": null,
12+
"returncode": 0
13+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <inttypes.h>
2+
3+
#include <stdlib.h>
4+
#include <stdbool.h>
5+
#include <stdio.h>
6+
#include <string.h>
7+
#include <lfortran_intrinsics.h>
8+
9+
#define ASSERT(cond) \
10+
{ \
11+
if (!(cond)) { \
12+
printf("%s%s", "ASSERT failed: ", __FILE__); \
13+
printf("%s%s", "\nfunction ", __func__); \
14+
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
15+
printf("%s%s", #cond, "\n"); \
16+
exit(1); \
17+
} \
18+
}
19+
#define ASSERT_MSG(cond, msg) \
20+
{ \
21+
if (!(cond)) { \
22+
printf("%s%s", "ASSERT failed: ", __FILE__); \
23+
printf("%s%s", "\nfunction ", __func__); \
24+
printf("%s%d%s", "(), line number ", __LINE__, " at \n"); \
25+
printf("%s%s", #cond, "\n"); \
26+
printf("%s", "ERROR MESSAGE:\n"); \
27+
printf("%s%s", msg, "\n"); \
28+
exit(1); \
29+
} \
30+
}
31+
32+
33+
struct dimension_descriptor
34+
{
35+
int32_t lower_bound, length;
36+
};
37+
38+
// Implementations
39+
int32_t f()
40+
{
41+
int32_t _lpython_return_variable;
42+
_lpython_return_variable = 42;
43+
return _lpython_return_variable;
44+
}
45+
46+
void main1()
47+
{
48+
int32_t a;
49+
a = f();
50+
printf("%d\n", a);
51+
}
52+

tests/tests.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ asr = true
201201
filename = "../integration_tests/test_import_02.py"
202202
c = true
203203

204+
[[test]]
205+
filename = "../integration_tests/import_order_01.py"
206+
c = true
207+
disable_main = true
208+
204209
[[test]]
205210
filename = "../integration_tests/vec_01.py"
206211
asr = true

0 commit comments

Comments
 (0)