Skip to content

Commit 004a1af

Browse files
committed
Write to a file if -o provided --show-c option
1 parent 3c47c14 commit 004a1af

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,9 @@ integration_tests/array_02_decl.c
218218
integration_tests/expr_12
219219
integration_tests/expr_12.c
220220
integration_tests/expr_07_generated.c
221+
integration_tests/expr_07.c
222+
expr_07.c
223+
tests/c_interop1.c
224+
tests/expr7.c
225+
tests/loop1.c
226+
tests/loop2.c

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ macro(RUN)
8787
add_custom_command(
8888
OUTPUT ${name}.c
8989
COMMAND lpython --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
90-
--custom-c-postprocessor ${CMAKE_CURRENT_SOURCE_DIR}/${custom_c_postprocessor} > ${name}.c
90+
--custom-c-postprocessor ${CMAKE_CURRENT_SOURCE_DIR}/${custom_c_postprocessor} -o ${name}.c
9191
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
9292
VERBATIM)
9393
else ()
9494
add_custom_command(
9595
OUTPUT ${name}.c
96-
COMMAND lpython --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py > ${name}.c
96+
COMMAND lpython --show-c ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py -o ${name}.c
9797
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${name}.py
9898
VERBATIM)
9999
endif()

integration_tests/test_c_custom_postprocessor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
filename = sys.argv[1]
66

7-
f = open(filename).read()
7+
file = open(filename, "r")
8+
f = file.read()
9+
file.close()
810

9-
open(filename, "w").write(f)
11+
file = open(filename, "w")
12+
file.write(f)
13+
file.close()

src/bin/lpython.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,34 @@ int emit_c(const std::string &infile,
295295
LFORTRAN_ASSERT(diagnostics.has_error())
296296
return 3;
297297
}
298+
299+
std::string output_file;
300+
bool print_to_std = true;
301+
if( !compiler_options.arg_o.empty() ) {
302+
output_file = compiler_options.arg_o;
303+
print_to_std = false;
304+
} else {
305+
output_file = remove_extension(infile) + ".c";
306+
}
307+
std::string ccode_file = output_file;
308+
std::ofstream ccode_out(ccode_file);
309+
ccode_out << res.result;
310+
ccode_out.close();
298311
if( !compiler_options.c_postprocessor_script.empty() ) {
299-
std::string ccode_file = remove_extension(infile) + "_generated.c";
300-
std::ofstream ccode_out(ccode_file);
301-
ccode_out << res.result;
302312
std::string command = "python " + compiler_options.c_postprocessor_script + " " + ccode_file;
303313
system(command.c_str());
304-
ccode_out.close();
305-
std::ifstream ccode_in(ccode_file);
306-
std::string modified_result = "";
307-
while( std::getline(ccode_in, modified_result) ) {
308-
std::cout << modified_result << "\n";
314+
if( print_to_std ) {
315+
std::ifstream ccode_in(ccode_file);
316+
std::string modified_result = "";
317+
while( std::getline(ccode_in, modified_result) ) {
318+
std::cout << modified_result << "\n";
319+
}
320+
ccode_in.close();
309321
}
310-
ccode_in.close();
311322
} else {
312-
std::cout << res.result;
323+
if( print_to_std ) {
324+
std::cout << res.result;
325+
}
313326
}
314327
return 0;
315328
}

0 commit comments

Comments
 (0)