From 2b4a3b22bf3fab23c25952e52d7d09d9e878b931 Mon Sep 17 00:00:00 2001 From: Matt Pharr Date: Thu, 21 Jun 2012 16:53:27 -0700 Subject: [PATCH] Issue an error if the user has nested foreach statements. Partially addresses issue #280. (We should support them properly, but at least now we don't silently generate incorrect code.) --- ctx.cpp | 12 ++++++++++++ tests_errors/nested-foreach.ispc | 11 +++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests_errors/nested-foreach.ispc diff --git a/ctx.cpp b/ctx.cpp index c7aaf0a2..63a4391e 100644 --- a/ctx.cpp +++ b/ctx.cpp @@ -584,6 +584,18 @@ FunctionEmitContext::EndLoop() { void FunctionEmitContext::StartForeach() { + // Issue an error if we're in a nested foreach... + for (int i = 0; i < (int)controlFlowInfo.size(); ++i) { + if (controlFlowInfo[i]->type == CFInfo::Foreach) { + Error(currentPos, "Nested \"foreach\" statements are currently illegal."); + break; + // Don't return here, however, and in turn allow the caller to + // do the rest of its codegen and then call EndForeach() + // normally--the idea being that this gives a chance to find + // any other errors inside the body of the foreach loop... + } + } + // Store the current values of various loop-related state so that we // can restore it when we exit this loop. llvm::Value *oldMask = GetInternalMask(); diff --git a/tests_errors/nested-foreach.ispc b/tests_errors/nested-foreach.ispc new file mode 100644 index 00000000..e3d71aa2 --- /dev/null +++ b/tests_errors/nested-foreach.ispc @@ -0,0 +1,11 @@ +// Nested "foreach" statements are currently illegal + +void func(); + +void foo(uniform int a, uniform int b) { + foreach (i = 0 ... a) { + foreach (j = 0 ... b) { + func(); + } + } +}