Skip to content

Commit

Permalink
DEV: Generate Exit(...) only for break statement inside loops
Browse files Browse the repository at this point in the history
  • Loading branch information
czgdp1807 committed Mar 11, 2024
1 parent e396f18 commit 467fed5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/lc/clang_ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
std::map<ASR::symbol_t*, std::map<std::string, ASR::expr_t*>> struct2member_inits;
std::map<SymbolTable*, std::vector<ASR::symbol_t*>> scope2enums;
clang::ForStmt* for_loop;
bool inside_loop;

explicit ClangASTtoASRVisitor(clang::ASTContext *Context_,
Allocator& al_, ASR::asr_t*& tu_):
Expand All @@ -175,7 +176,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
is_all_called{false}, is_range_called{false},
current_switch_case{nullptr}, default_stmt{nullptr},
interpret_init_list_expr_as_list{false}, enable_fall_through{false},
for_loop{nullptr} {}
for_loop{nullptr}, inside_loop{false} {}

template <typename T>
Location Lloc(T *x) {
Expand Down Expand Up @@ -2292,8 +2293,10 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit

bool TraverseBreakStmt(clang::BreakStmt* x) {
clang::RecursiveASTVisitor<ClangASTtoASRVisitor>::TraverseBreakStmt(x);
tmp = ASR::make_Exit_t(al, Lloc(x), nullptr);
is_stmt_created = true;
if( inside_loop ) {
tmp = ASR::make_Exit_t(al, Lloc(x), nullptr);
is_stmt_created = true;
}
is_break_stmt_present.set(true);
return true;
}
Expand Down Expand Up @@ -2479,6 +2482,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
}

bool TraverseWhileStmt(clang::WhileStmt* x) {
bool inside_loop_copy = inside_loop;
inside_loop = true;
std::map<std::string, std::string> alias;
scopes.push_back(alias);

Expand All @@ -2496,10 +2501,13 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
tmp = ASR::make_WhileLoop_t(al, Lloc(x), nullptr, test, body.p, body.size());
is_stmt_created = true;
scopes.pop_back();
inside_loop = inside_loop_copy;
return true;
}

bool TraverseForStmt(clang::ForStmt* x) {
bool inside_loop_copy = inside_loop;
inside_loop = true;
clang::ForStmt* for_loop_copy = for_loop;
for_loop = x;
std::map<std::string, std::string> alias;
Expand Down Expand Up @@ -2528,6 +2536,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
is_stmt_created = true;
scopes.pop_back();
for_loop = for_loop_copy;
inside_loop = inside_loop_copy;
return true;
}

Expand Down

0 comments on commit 467fed5

Please sign in to comment.