Skip to content

Generate C++ code for tests - #707

Open
adazem009 wants to merge 3 commits into
masterfrom
cppcodebuilder_tests
Open

adazem009 wants to merge 3 commits into
masterfrom
cppcodebuilder_tests

Conversation

@adazem009

@adazem009 adazem009 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Resolves: #687


Devin Review

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 4 potential issues.

2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +332 to +335
#ifdef CODEGEN_BACKEND_CPP
std::error_code error;
std::filesystem::remove_all(std::filesystem::path(destination) / "src", error);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Project export deletes existing source files

When the export destination already contains src, Engine::save deletes that entire directory before generating any code. Unrelated files in it are lost.

Learn more

The public Project::save takes a destination directory. CppCodeSaver::save organizes generated files under a src subtree, but the export API does not require that the destination or its src directory be owned by the exporter. The cleanup removes all existing contents of that subtree, not just files produced by previous exports.

Example: An application exports to its workspace to keep generated scripts alongside other code. An existing src/main.cpp is deleted before any generated script is written.

Recommended fix: Put generated files in a dedicated exporter-owned directory. Reject a pre-existing directory without an ownership marker, or track and replace only previously generated files. Add a regression test that saves into a destination containing an unrelated source file and verifies that the file survives.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +342 to +344
m_saveDestination = destination;
compile();
m_saveDestination.clear();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Saving leaves dangling event scripts

After project load, Engine::save recompiles scripts and frees the originals. addHatToMap leaves the old script pointers registered. The next green flag dereferences a freed script in startHats.

Learn more

ProjectPrivate::tryLoad already compiles the project. Engine::compile owns each compiled Script through m_scripts, while addGreenFlagScript adds its raw pointer to the green-flag event index. Saving compiles again and replaces the owner, but addHatToMap only appends the replacement pointer; it does not remove the original. Engine::start later starts every registered green-flag script, dereferencing those pointers in startHats. The hazard also exists on LLVM because the save method still recompiles before the backend rejects export.

Example: Load a stopped project with one green-flag script, save to a fresh location, and then press the green flag. The event index now contains the freed original as well as its replacement.

Recommended fix: Export without recompiling into the live engine's runtime registries. Retain export artifacts during compilation or compile for export in isolated state. Reject unsupported backends before changing engine state. Add a load–save–start regression test with a green-flag script.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +129 to +130
m_codeGen.setCodeType(m_codeType, returnValue);
CppCodeSaver::save(m_codeGen, m_ctx, destination);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Exported scripts ignore their conditions

When a Scratch if contains a say block, CppCodeBuilder::save exports the say call without its condition. buildBeginIf emits no branch, so the exported script speaks even when the condition is false.

Learn more

compileIf sends its condition and body to Compiler::moveToIf, which places a branch instruction before the body. The C++ branch and loop handlers advance without emitting control flow. In contrast, compileSay emits a normal function call, which buildFunctionCall translates. The public save path therefore writes source that runs the body without its condition rather than rejecting an incomplete export.

Example: Export a green-flag script containing if <mouse down?> then say [Pressed]. The exported code calls looks_say even when the mouse button is up.

Recommended fix: Implement control-flow emission or reject export when a script contains an instruction the backend cannot emit. Propagate the error through project save and add a regression test that either rejects the example or confirms the exported script stays silent when the condition is false.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +383 to +384
if (retType == CppIdentifier::Type::ValueData)
includeHeader(Header::ValueData);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Generated sensing calls leak strings

When sensing_variable_of_target reads a text variable, it returns an owned string. createCall stores it in a ValueData local and never releases it. Each evaluation leaks a string even when the caller frees the reporter result.

Learn more

When the source of an of block is resolved at runtime, compileOf uses sensing_variable_of_target to read its variables. The helper uses value_assign_copy, which checks out a new string for a string-valued result. Generated native calls assign that result to a local ValueData, which has no destructor. When finalize copies the value again for the reporter return, freeing the returned copy does not release the original string.

Example: A generated reporter reads the status variable of a sprite selected by a runtime expression. If status contains ready, every call leaks the original ready string even when the caller frees the returned value.

Recommended fix: Give owned ValueData temporaries scoped cleanup, or transfer ownership when returning them and release them after their final use otherwise. Keep borrowed ValueData separate so cleanup does not free strings owned by other objects. Exercise string-valued results in the runtime-variable sensing test.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add automated tests for CppCodeBuilder

1 participant