Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"

[project]
name = "openscad_cpp_evaluator"
version = "0.26.0"
version = "0.26.1"
description = "C++ OpenSCAD evaluator with Python bindings"
readme = "README.md"
requires-python = ">=3.12"
Expand Down
26 changes: 24 additions & 2 deletions src/debug_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,31 @@ namespace {
// call), if any. See Evaluator::lastChildrenPositions()'s own doc comment
// for what this feeds. Mirrors the reference's own
// Evaluator._child_statement_positions.
std::optional<std::vector<std::pair<std::string, int>>> childStatementPositions(const oscad::ASTNode& node) {
// Where a debugger's "step into child" should aim from `node`.
//
// Normally the node's own children -- `framed(20) leaf(3);` aims at
// `leaf(3)`. But a `children()` call has no children of its own: it
// forwards the enclosing invocation's, which live on the context. Without
// that case, stepping to a child from the one statement whose entire job
// is to run the children found nothing to aim at, and the step silently
// degraded into a continue -- running to the next breakpoint or off the
// end of the script.
std::optional<std::vector<std::pair<std::string, int>>> childStatementPositions(const oscad::ASTNode& node,
const EvalContext& ctx) {
if (node.kind() != oscad::NodeKind::ModularCall) return std::nullopt;
const auto& call = static_cast<const oscad::ModularCall&>(node);
if (call.name && call.name->name == "children") {
if (!ctx.childrenNodes || ctx.childrenNodes->empty()) return std::nullopt;
std::vector<std::pair<std::string, int>> forwarded;
for (const oscad::ASTNode* c : *ctx.childrenNodes) {
if (c) forwarded.emplace_back(c->position().origin, c->position().line);
}
if (forwarded.empty()) return std::nullopt;
// Every forwarded child, even when an index argument (children(0),
// children([1:2])) will run only some: a position that is never
// reached can never be stopped at, so the extra targets are inert.
return forwarded;
}
std::vector<std::pair<std::string, int>> positions;
for (const auto& c : call.children) {
if (c->kind() == oscad::NodeKind::Assignment || c->kind() == oscad::NodeKind::ModuleDeclaration ||
Expand Down Expand Up @@ -121,7 +143,7 @@ void Evaluator::checkDebug(const oscad::ASTNode& node, EvalContext& ctx, bool fo
}
const int depth = static_cast<int>(callStack_.size());
const DebugFramesFn getFrame = [this, &ctx]() { return buildDebugFrames(&ctx); };
lastChildrenPositions_ = childStatementPositions(node);
lastChildrenPositions_ = childStatementPositions(node, ctx);

DebugAction action = debugHooks_.debugHook(pos.line, depth, forced, exprLevel, pos.origin, callStack_, getFrame);
for (auto& [k, v] : action.mods) ctx.let_->set(k, v);
Expand Down
40 changes: 40 additions & 0 deletions tests/test_debug_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,46 @@ TEST(DebugHooksParity, CallSiteOnItsOwnLineStillStops) {
EXPECT_EQ(line4, 2);
}

// Step-into-child aims at the children a `children()` call forwards, not
// at its own (it has none). Without this, the one statement whose entire
// job is to run the children offered a debugger nothing to aim at, and the
// step degraded into a continue -- running off the end of the script.
TEST(DebugHooksChildren, ChildrenCallForwardsItsCallersChildrenAsStepTargets) {
// Line 5 is the child; line 2 is the `children();` that forwards it.
const char* src = "module framed(gap) {\n"
" children();\n"
"}\n"
"framed(20)\n"
" leaf();\n"
"module leaf() { cube(1); }\n";
std::vector<std::pair<int, std::vector<std::pair<std::string, int>>>> seen;
DebugHooks hooks;
Evaluator* evp = nullptr;
hooks.debugHook = [&](int line, int, bool, bool exprLevel, const std::string&,
const std::vector<CallStackFrame>&, const DebugFramesFn&) {
if (!exprLevel && evp && evp->lastChildrenPositions())
seen.emplace_back(line, *evp->lastChildrenPositions());
return DebugAction{};
};
Evaluator ev(EchoFn{}, nullptr, nullptr, hooks);
evp = &ev;
auto ast = parseSrc(src);
auto scope = oscad::buildScopes(ast);
EvalContext ctx = EvalContext::makeRoot(scope.get());
ev.evaluate(ast, ctx);

// At `children();` on line 2, the target is the forwarded child on
// line 5 -- not nothing, and not the children() call's own position.
bool found = false;
for (const auto& [line, positions] : seen) {
if (line != 2) continue;
found = true;
ASSERT_EQ(positions.size(), 1u);
EXPECT_EQ(positions.front().second, 5);
}
EXPECT_TRUE(found) << "no statement-level stop at the children() call";
}

TEST(DebugHooksParity, BuiltinFunctionCallGetsNoCallSiteStop) {
// Only the assignment is statement-level; the 3 expr-level stops are
// the argument list literal's own elements, not a call-site stop.
Expand Down