Skip to content

Selectively load rules in the same TU as the translated code - #451

Open
lucic71 wants to merge 77 commits into
Cpp2Rust:masterfrom
lucic71:rules-in-tu
Open

lucic71 wants to merge 77 commits into
Cpp2Rust:masterfrom
lucic71:rules-in-tu

Conversation

@lucic71

@lucic71 lucic71 commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Part of #447. This only loads the rules into cpp2rust, AST matching comes in a future PR:

To implement this, rules are included in the translated TU. Only the rules that parse are kept. If the TU never included then there are no rules for map. This is faster than the string matching approach.

cpp_rule_indexer runs at build time to build an index of rules. This index is used by rule_loader to lazily load the rules into the converter for doing AST matching.

There are 2 index files: one for C code and one for C++ code. Both live in build/rules_index. C translated code only loads the C index, C++ translated code loads both indexes.

Example of index:

key:      std/vector/push_back
requires: std/vector
text:
    namespace cpp2rust_rules {
      namespace cpp2rust_rules_vector {
        template <typename T1> void f14(std::vector<T1> &o, T1 &&value) {
          return o.push_back(std::move(value));
        }
      }
    }

key:      std/basic_ifstream/rdbuf
requires: std/basic_ifstream, std/basic_filebuf
text:
    namespace cpp2rust_rules {
      namespace cpp2rust_rules_fstream {
        std::filebuf *f4(const std::ifstream &o) { return o.rdbuf(); }
      }
    }

key is generated using the dependent name of the rules for templates and using the concrete name for non-templates. text is extracted from the file and appended into the translated TU if it needs the rule. requires only loads the rule in the TU if the translated code uses all the dependencies, basic_ifstream and basic_fielbuf in the example.

This makes the translation fast. With 100K dummy rules, it takes on avergage 20ms per invocation to selectively load the rules. With full loading of all the 100K dummy rules, it takes 5 seconds on average.

@lucic71 lucic71 changed the title Compile rules in the same TU as user code Selectively load rules in the same PR as the translated code Oct 6, 2026
@lucic71 lucic71 changed the title Selectively load rules in the same PR as the translated code Selectively load rules in the same TU as the translated code Oct 6, 2026
@lucic71
lucic71 marked this pull request as ready for review October 6, 2026 15:09
@lucic71

lucic71 commented Oct 6, 2026

Copy link
Copy Markdown
Contributor Author

@nunoplopes please take a look

This branch has not been deployed

No deployments
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.

1 participant