Skip to content

Commit d2a5640

Browse files
authored
Merge pull request #1401 from Smit-create/test_global_c
C: Fix strings allocation/declaration
2 parents 595139b + 89dd629 commit d2a5640

File tree

8 files changed

+27
-18
lines changed

8 files changed

+27
-18
lines changed

integration_tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ RUN(NAME test_generics_01 LABELS cpython llvm c)
339339
RUN(NAME test_cmath LABELS cpython llvm c)
340340
RUN(NAME test_complex LABELS cpython llvm c)
341341
RUN(NAME test_max_min LABELS cpython llvm c)
342-
RUN(NAME test_global LABELS cpython llvm)
342+
RUN(NAME test_global LABELS cpython llvm c)
343343
RUN(NAME test_global_decl LABELS cpython llvm c)
344344
RUN(NAME test_integer_bitnot LABELS cpython llvm c)
345345
RUN(NAME test_ifexp LABELS cpython llvm c)
@@ -383,7 +383,7 @@ RUN(NAME union_03 LABELS cpython llvm c)
383383
RUN(NAME test_str_to_int LABELS cpython llvm c)
384384
RUN(NAME test_platform LABELS cpython llvm c)
385385
RUN(NAME test_vars_01 LABELS cpython llvm)
386-
RUN(NAME test_version LABELS cpython llvm c)
386+
RUN(NAME test_version LABELS cpython llvm)
387387
RUN(NAME vec_01 LABELS cpython llvm c)
388388
RUN(NAME test_str_comparison LABELS cpython llvm c)
389389
RUN(NAME test_bit_length LABELS cpython llvm c)

src/libasr/codegen/asr_to_c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
150150
}
151151
ASR::ttype_t* mem_type = ASRUtils::symbol_type(itr.second);
152152
if( ASRUtils::is_character(*mem_type) ) {
153-
sub += indent + name + "->" + itr.first + " = (char*) malloc(40 * sizeof(char));\n";
153+
sub += indent + name + "->" + itr.first + " = NULL;\n";
154154
} else if( ASRUtils::is_array(mem_type) &&
155155
ASR::is_a<ASR::Variable_t>(*itr.second) ) {
156156
ASR::Variable_t* mem_var = ASR::down_cast<ASR::Variable_t>(itr.second);
@@ -357,7 +357,7 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
357357
!(ASR::is_a<ASR::symbol_t>(*v.m_parent_symtab->asr_owner) &&
358358
ASR::is_a<ASR::StructType_t>(
359359
*ASR::down_cast<ASR::symbol_t>(v.m_parent_symtab->asr_owner))) ) {
360-
sub += " = (char*) malloc(40 * sizeof(char))";
360+
sub += " = NULL";
361361
return sub;
362362
}
363363
} else if (ASR::is_a<ASR::Struct_t>(*v_m_type)) {

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,7 @@ R"(#include <stdio.h>
731731
std::string alloc = "";
732732
if (alloc_return_var) {
733733
// char * return variable;
734-
alloc = indent + target + " = " + "(char *) malloc((strlen(" +
735-
value + ") + 1 ) * sizeof(char));\n";
734+
alloc = indent + target + " = NULL;\n";
736735
}
737736
if( ASRUtils::is_array(m_target_type) && ASRUtils::is_array(m_value_type) ) {
738737
ASR::dimension_t* m_target_dims = nullptr;
@@ -844,7 +843,7 @@ R"(#include <stdio.h>
844843
for( size_t i = 0; i < x.n_args; i++ ) {
845844
self().visit_expr(*x.m_args[i]);
846845
if( ASR::is_a<ASR::Character_t>(*t->m_type) ) {
847-
src_tmp += indent + var_name + ".data[" + std::to_string(i) +"] = (char*) malloc(40 * sizeof(char));\n";
846+
src_tmp += indent + var_name + ".data[" + std::to_string(i) +"] = NULL;\n";
848847
}
849848
src_tmp += indent + c_ds_api->get_deepcopy(t->m_type, src,
850849
var_name + ".data[" + std::to_string(i) +"]") + "\n";
@@ -869,7 +868,7 @@ R"(#include <stdio.h>
869868
self().visit_expr(*x.m_elements[i]);
870869
std::string ele = ".element_" + std::to_string(i);
871870
if (ASR::is_a<ASR::Character_t>(*t->m_type[i])) {
872-
src_tmp += indent + var_name + ele + " = (char*) malloc(40 * sizeof(char));\n";
871+
src_tmp += indent + var_name + ele + " = NULL;\n";
873872
}
874873
src_tmp += indent + c_ds_api->get_deepcopy(t->m_type[i], src, var_name + ele) + "\n";
875874
}

src/libasr/codegen/c_utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ class CCPPDSUtils {
397397
}
398398
case ASR::ttypeType::Character : {
399399
if (is_c) {
400-
result = "strcpy(" + target + ", " + value + ");";
400+
result = "_lfortran_strcpy(&" + target + ", " + value + ");";
401401
} else {
402402
result = target + " = " + value + ";";
403403
}
@@ -927,7 +927,7 @@ class CCPPDSUtils {
927927
std::string list_resize_func = get_list_resize_func(list_type_code);
928928
generated_code += indent + tab + list_resize_func + "(x);\n";
929929
if( ASR::is_a<ASR::Character_t>(*m_type) ) {
930-
generated_code += indent + tab + "x->data[x->current_end_point] = (char*) malloc(40 * sizeof(char));\n";
930+
generated_code += indent + tab + "x->data[x->current_end_point] = NULL;\n";
931931
}
932932
generated_code += indent + tab + \
933933
get_deepcopy(m_type, "element", "x->data[x->current_end_point]") + "\n";
@@ -962,7 +962,7 @@ class CCPPDSUtils {
962962
generated_code += indent + tab + "}\n\n";
963963

964964
if( ASR::is_a<ASR::Character_t>(*m_type) ) {
965-
generated_code += indent + tab + "x->data[pos] = (char*) malloc(40 * sizeof(char));\n";
965+
generated_code += indent + tab + "x->data[pos] = NULL;\n";
966966
}
967967
generated_code += indent + tab + get_deepcopy(m_type, "element", "x->data[pos]") + "\n";
968968
generated_code += indent + tab + "x->current_end_point += 1;\n";
@@ -1100,7 +1100,7 @@ class CCPPDSUtils {
11001100
std::string n = std::to_string(i);
11011101
if (ASR::is_a<ASR::Character_t>(*t->m_type[i])) {
11021102
tmp_gen += indent + tab + "dest->element_" + n + " = " + \
1103-
"(char *) malloc(40*sizeof(char));\n";
1103+
"NULL;\n";
11041104
}
11051105
tmp_gen += indent + tab + get_deepcopy(t->m_type[i], "src.element_" + n,
11061106
"dest->element_" + n) + "\n";

src/libasr/runtime/lfortran_intrinsics.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,15 @@ LFORTRAN_API void _lfortran_strcat(char** s1, char** s2, char** dest)
623623
*dest = &(dest_char[0]);
624624
}
625625

626+
// strcpy -----------------------------------------------------------
627+
628+
LFORTRAN_API void _lfortran_strcpy(char** x, char *y)
629+
{
630+
if (*x) free((void *)*x);
631+
*x = (char*) malloc((strlen(y) + 1) * sizeof(char));
632+
strcpy(*x, y);
633+
}
634+
626635
#define MIN(x, y) ((x < y) ? x : y)
627636

628637
int str_compare(char **s1, char **s2)

src/libasr/runtime/lfortran_intrinsics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ LFORTRAN_API int32_t _lpython_bit_length8(int64_t num);
156156
LFORTRAN_API void _lfortran_strrepeat(char** s, int32_t n, char** dest);
157157
LFORTRAN_API char* _lfortran_strrepeat_c(char* s, int32_t n);
158158
LFORTRAN_API void _lfortran_strcat(char** s1, char** s2, char** dest);
159+
LFORTRAN_API void _lfortran_strcpy(char** x, char *y);
159160
LFORTRAN_API int _lfortran_str_len(char** s);
160161
LFORTRAN_API int _lfortran_str_ord(char** s);
161162
LFORTRAN_API int _lfortran_str_ord_c(char* s);

tests/reference/c-print_01-4d44628.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "c-print_01-4d44628.stdout",
9-
"stdout_hash": "cd27fae5e2741d904b77e8872c0eefa43c121a8abdfc54ebfd51d4b3",
9+
"stdout_hash": "876b77dde0878832747e9695554503be1fe752bf6bc76eb6be0ec50e",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/c-print_01-4d44628.stdout

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ struct dimension_descriptor
3737
// Implementations
3838
void f()
3939
{
40-
char * x = (char*) malloc(40 * sizeof(char));
41-
char * y = (char*) malloc(40 * sizeof(char));
40+
char * x = NULL;
41+
char * y = NULL;
4242
printf("%s\n", "Hello World!");
43-
strcpy(x, ",");
44-
strcpy(y, "!!");
43+
_lfortran_strcpy(&x, ",");
44+
_lfortran_strcpy(&y, "!!");
4545
printf("%s%s%s\n", "a", x, "b");
46-
strcpy(x, "-+-+-");
46+
_lfortran_strcpy(&x, "-+-+-");
4747
printf("%s%s%s%s%s\n", "a", x, "b", x, "c");
4848
printf("%s%s%s%s%s%s", "d", "=", "e", "=", "f", "+\n");
4949
printf("%s%s%s%s%s%s", "x", "*\n", "y", "*\n", "z", y);

0 commit comments

Comments
 (0)