Skip to content

Parse src.cpp rules at build time#61

Merged
nunoplopes merged 16 commits into
Cpp2Rust:masterfrom
lucic71:rule-loading
May 10, 2026
Merged

Parse src.cpp rules at build time#61
nunoplopes merged 16 commits into
Cpp2Rust:masterfrom
lucic71:rule-loading

Conversation

@lucic71
Copy link
Copy Markdown
Contributor

@lucic71 lucic71 commented May 4, 2026

Fixes #55

Each src.cpp from the rules/ dir is now parsed at build time, instead of on every invocation of cpp2rust. This gives a 5-8x speedup in running the unit tests in the Github runner. Translating a simple unit test like assert.cpp went down form 6 seconds to 0.1 seconds.

The new cpp-rule-preprocessor binary from cpp2rust/ is responsible for parsing one src.cpp at a time and to generate a ir_src.json in the same dir as the src.cpp file. The format of the json file is: f<n> / t<n>: { "to_string": <Mapper::ToString> }.

All src.cpp files are parsed in parallel, CMakeLists takes care of that. The check-rules target is now responsible for parsing the Rust rules and the C++ rules. The IR for src.cpp is not indexed in git. The IR for the Rust rules remains unchanged.

I ported the clang AST matcher form translation_rule.cpp into the new file cpp_rule_preprocessor.cpp. As a result, translation_rule.cpp is only responsible for loading IR JSONs.

run: ninja check-rules
working-directory: build

- name: Run unit tests
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this change is weird. ninja should regen the files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, I will make the change to regen the files on ninja

However, I think it's better to run check-rules before unit tests because it's cheaper and catches the rules errors early

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now that we run preprocess-{rust,cpp}-rules in ALL, check-rules does not need to delete then re-run preprocess-{rust,cpp}-rules, it can only do git diff

Comment thread CMakeLists.txt Outdated
list(APPEND cpp_rules_ir_outputs ${_out})
endforeach()

add_custom_target("preprocess-cpp-rules" DEPENDS ${cpp_rules_ir_outputs})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add ALL so it runs with the build

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. preprocess-cpp-rules has ALL

Comment thread CMakeLists.txt Outdated

add_custom_target("preprocess-cpp-rules" DEPENDS ${cpp_rules_ir_outputs})

add_custom_target("preprocess-rust-rules"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this needs to be changed to use the same pattern as above, so it runs on build and iff some rust file changed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. preprocess-rust-rules now has a list on dependent files and runs on all + when a file changes

Comment thread CMakeLists.txt
USES_TERMINAL
)

add_custom_target("check-rules"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

move the delete here and call preprocess & then diff.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done. check-rules now contains deltee + call preprocess rust/cpp + diff

Comment thread CMakeLists.txt
)

add_custom_target("preprocess-rust-rules" ALL
DEPENDS ${rust_rules_ir_outputs} install-rust-toolchain)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

doesn't seem like it needs the rust toolchain here.

@nunoplopes nunoplopes merged commit 29c6b16 into Cpp2Rust:master May 10, 2026
9 checks passed
@lucic71 lucic71 deleted the rule-loading branch May 10, 2026 20:30
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.

Parsing rules's source (C++) is the bottleneck

2 participants