Skip to content

Commit 9da3fce

Browse files
authored
Merge pull request #1576 from czgdp1807/cpptr
Handle initialisation at declaration inside for loop
2 parents 2abb949 + 270dfb2 commit 9da3fce

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

integration_tests/structs_21.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,19 @@ def f(c: CPtr):
1818
print(A)
1919
assert A == 3
2020

21+
def g(c: CPtr):
22+
i: i32
23+
for i in range(3):
24+
p: Pointer[S] = c_p_pointer(c, S)
25+
A: i32 = p.a
26+
print(A)
27+
assert A == 3
28+
2129
def main():
2230
s: S = S(3)
2331
p: CPtr = empty_c_void_p()
2432
p_c_pointer(pointer(s, S), p)
2533
f(p)
34+
g(p)
2635

2736
main()

src/libasr/codegen/asr_to_c_cpp.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ R"(#include <stdio.h>
324324
std::string close_paranthesis = indent + "}\n";
325325
indent += std::string(indentation_spaces, ' ');
326326
indentation_level += 1;
327+
SymbolTable* current_scope_copy = current_scope;
328+
current_scope = block->m_symtab;
327329
std::vector<std::string> var_order = ASRUtils::determine_variable_declaration_order(block->m_symtab);
328330
for (auto &item : var_order) {
329331
ASR::symbol_t* var_sym = block->m_symtab->get_symbol(item);
@@ -343,6 +345,7 @@ R"(#include <stdio.h>
343345
decl += check_tmp_buffer();
344346
src = open_paranthesis + decl + body + close_paranthesis;
345347
indentation_level -= 1;
348+
current_scope = current_scope_copy;
346349
}
347350

348351
// Returns the declaration, no semi colon at the end

0 commit comments

Comments
 (0)