Skip to content

Commit 2068bf6

Browse files
authored
Merge pull request lcompilers#1962 from Shaikh-Ubaid/fix_c_redefined_warnings_for_consts
C: Use `const` in place of `#define` for declaring constants
2 parents 1d41971 + 0058241 commit 2068bf6

File tree

4 files changed

+14
-40
lines changed

4 files changed

+14
-40
lines changed

src/libasr/codegen/asr_to_c.cpp

+8-22
Original file line numberDiff line numberDiff line change
@@ -614,17 +614,10 @@ class ASRToCVisitor : public BaseCCPPVisitor<ASRToCVisitor>
614614
ASR::EnumType_t* enum_type = ASR::down_cast<ASR::EnumType_t>(enum_->m_enum_type);
615615
sub = format_type_c("", "enum " + std::string(enum_type->m_name), v.m_name, false, false);
616616
} else if (ASR::is_a<ASR::Const_t>(*v_m_type)) {
617-
if( v.m_intent == ASRUtils::intent_local ) {
618-
LCOMPILERS_ASSERT(v.m_symbolic_value);
619-
visit_expr(*v.m_symbolic_value);
620-
sub = "#define " + std::string(v.m_name) + " " + src + "\n";
621-
return sub;
622-
} else {
623-
std::string const_underlying_type = CUtils::get_c_type_from_ttype_t(
624-
ASR::down_cast<ASR::Const_t>(v_m_type)->m_type);
625-
sub = format_type_c("", "const " + const_underlying_type + " ",
626-
v.m_name, false, false);
627-
}
617+
std::string const_underlying_type = CUtils::get_c_type_from_ttype_t(
618+
ASRUtils::type_get_past_const(v_m_type));
619+
sub = format_type_c("", "const " + const_underlying_type,
620+
v.m_name, false, false);
628621
} else if (ASR::is_a<ASR::TypeParameter_t>(*v_m_type)) {
629622
// Ignore type variables
630623
return "";
@@ -702,8 +695,7 @@ R"(
702695
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(item.second);
703696
unit_src_tmp = convert_variable_decl(*v);
704697
unit_src += unit_src_tmp;
705-
if(unit_src_tmp.size() > 0 && (!ASR::is_a<ASR::Const_t>(*v->m_type) ||
706-
v->m_intent == ASRUtils::intent_return_var )) {
698+
if(unit_src_tmp.size() > 0) {
707699
unit_src += ";\n";
708700
}
709701
}
@@ -852,9 +844,7 @@ R"(
852844
item.second);
853845
unit_src_tmp = convert_variable_decl(*v);
854846
unit_src += unit_src_tmp;
855-
if(unit_src_tmp.size() > 0 &&
856-
(!ASR::is_a<ASR::Const_t>(*v->m_type) ||
857-
v->m_intent == ASRUtils::intent_return_var )) {
847+
if(unit_src_tmp.size() > 0) {
858848
unit_src += ";\n";
859849
}
860850
}
@@ -920,8 +910,7 @@ R"(
920910
decl += indent1;
921911
decl_tmp = convert_variable_decl(*v);
922912
decl += decl_tmp;
923-
if(decl_tmp.size() > 0 && (!ASR::is_a<ASR::Const_t>(*v->m_type) ||
924-
v->m_intent == ASRUtils::intent_return_var )) {
913+
if(decl_tmp.size() > 0) {
925914
decl += ";\n";
926915
}
927916
}
@@ -1007,10 +996,7 @@ R"( // Initialise Numpy
1007996
body += indent + convert_variable_decl(
1008997
*ASR::down_cast<ASR::Variable_t>(member),
1009998
&c_decl_options_);
1010-
if( !ASR::is_a<ASR::Const_t>(*ASRUtils::symbol_type(member)) ||
1011-
ASR::down_cast<ASR::Variable_t>(member)->m_intent == ASRUtils::intent_return_var ) {
1012-
body += ";\n";
1013-
}
999+
body += ";\n";
10141000
}
10151001
indentation_level -= 1;
10161002
std::string end_struct = "};\n\n";

src/libasr/codegen/asr_to_c_cpp.h

+3-15
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,7 @@ R"(#include <stdio.h>
306306
ASR::symbol_t* var_sym = x.m_symtab->get_symbol(item);
307307
if (ASR::is_a<ASR::Variable_t>(*var_sym)) {
308308
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
309-
std::string d = self().convert_variable_decl(*v);
310-
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
311-
v->m_intent == ASRUtils::intent_return_var ) {
312-
d += ";\n";
313-
}
309+
std::string d = self().convert_variable_decl(*v) + ";\n";
314310
decl += check_tmp_buffer() + d;
315311
}
316312
}
@@ -354,11 +350,7 @@ R"(#include <stdio.h>
354350
ASR::symbol_t* var_sym = block->m_symtab->get_symbol(item);
355351
if (ASR::is_a<ASR::Variable_t>(*var_sym)) {
356352
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
357-
std::string d = indent + self().convert_variable_decl(*v);
358-
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
359-
v->m_intent == ASRUtils::intent_return_var ) {
360-
d += ";\n";
361-
}
353+
std::string d = indent + self().convert_variable_decl(*v) + ";\n";
362354
decl += check_tmp_buffer() + d;
363355
}
364356
}
@@ -673,11 +665,7 @@ R"(#include <stdio.h>
673665
ASR::Variable_t *v = ASR::down_cast<ASR::Variable_t>(var_sym);
674666
if (v->m_intent == ASRUtils::intent_local ||
675667
v->m_intent == ASRUtils::intent_return_var) {
676-
std::string d = indent + self().convert_variable_decl(*v);
677-
if( !ASR::is_a<ASR::Const_t>(*v->m_type) ||
678-
v->m_intent == ASRUtils::intent_return_var ) {
679-
d += ";\n";
680-
}
668+
std::string d = indent + self().convert_variable_decl(*v) + ";\n";
681669
if (ASR::is_a<ASR::SymbolicExpression_t>(*v->m_type)) {
682670
std::string v_m_name = v->m_name;
683671
d += indent + "basic_new_stack(" + v_m_name + ");\n";

tests/reference/c-test_import_02-d2c54c4.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"outfile": null,
77
"outfile_hash": null,
88
"stdout": "c-test_import_02-d2c54c4.stdout",
9-
"stdout_hash": "2697ed4e9d1c123a8c2fe3802d8f8458108393593df377117da85593",
9+
"stdout_hash": "fe5d15e377a72c397cba67c89607695c9b77a2d5681aaab85a5c9654",
1010
"stderr": null,
1111
"stderr_hash": null,
1212
"returncode": 0

tests/reference/c-test_import_02-d2c54c4.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ struct dimension_descriptor
1313
};
1414

1515
// Implementations
16-
#define μ 1.45136923488338110e+00
16+
const double μ = 1.45136923488338110e+00;
1717
int32_t add(int32_t x, int32_t y)
1818
{
1919
int32_t _lpython_return_variable;
2020
_lpython_return_variable = x + y;
2121
return _lpython_return_variable;
2222
}
2323

24-
#define e 2.71828182845904509e+00
24+
const double e = 2.71828182845904509e+00;
2525
int32_t multiply(int32_t x, int32_t y)
2626
{
2727
int32_t _lpython_return_variable;

0 commit comments

Comments
 (0)