diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index 918d72c..0988769 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -234,3 +234,5 @@ RUN(NAME union_01.cpp LABELS gcc llvm NOFAST) RUN(NAME union_02.cpp LABELS gcc llvm NOFAST) RUN(NAME vector_01.cpp LABELS gcc llvm NOFAST) + +RUN(NAME loop_01.cpp LABELS gcc llvm NOFAST) diff --git a/integration_tests/loop_01.cpp b/integration_tests/loop_01.cpp new file mode 100644 index 0000000..d3acbfa --- /dev/null +++ b/integration_tests/loop_01.cpp @@ -0,0 +1,142 @@ +#include + +#define assert(cond) if( !(cond) ) { \ + exit(2); \ +} \ + +void test_loop_01() { + int32_t i = 0; + int32_t j = 0; + + while( false ) { + assert( false ); + } + + while( i < 0 ) { + assert( false ); + } + + while( i < 10 ) { + i += 1; + } + std::cout< 0; i-- ) { + j = j + i; + } + std::cout<> struct2member_inits; std::map> scope2enums; + clang::ForStmt* for_loop; + bool inside_loop; explicit ClangASTtoASRVisitor(clang::ASTContext *Context_, Allocator& al_, ASR::asr_t*& tu_): @@ -173,7 +175,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor Location Lloc(T *x) { @@ -2290,10 +2293,26 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor::TraverseBreakStmt(x); + if( inside_loop ) { + tmp = ASR::make_Exit_t(al, Lloc(x), nullptr); + is_stmt_created = true; + } is_break_stmt_present.set(true); return true; } + bool TraverseContinueStmt(clang::ContinueStmt* x) { + if( for_loop != nullptr ) { + clang::Stmt* inc_stmt = for_loop->getInc(); + TraverseStmt(inc_stmt); + LCOMPILERS_ASSERT(tmp != nullptr && is_stmt_created); + current_body->push_back(al, ASRUtils::STMT(tmp.get())); + } + tmp = ASR::make_Cycle_t(al, Lloc(x), nullptr); + is_stmt_created = true; + return true; + } + bool TraverseCaseStmt(clang::CaseStmt* x) { if ( x->caseStmtIsGNURange() ) { throw std::runtime_error("Ranges not supported in case."); @@ -2437,6 +2456,16 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor alias; + scopes.push_back(alias); + + clang::Expr* loop_cond = x->getCond(); + TraverseStmt(loop_cond); + ASR::expr_t* test = ASRUtils::EXPR(tmp.get()); + + Vec body; body.reserve(al, 1); + Vec*current_body_copy = current_body; + current_body = &body; + clang::Stmt* loop_body = x->getBody(); + TraverseStmt(loop_body); + current_body = current_body_copy; + + 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 alias; scopes.push_back(alias); clang::Stmt* init_stmt = x->getInit(); @@ -2478,6 +2535,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor