├── .github └── workflows │ ├── ci.yml │ └── internal-testsuite.yml ├── .gitignore ├── .gitmodules ├── .typos.toml ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── analysis ├── runtime │ ├── Cargo.toml │ └── src │ │ ├── events.rs │ │ ├── handlers.rs │ │ ├── lib.rs │ │ ├── metadata.rs │ │ ├── mir_loc.rs │ │ ├── parse.rs │ │ └── runtime │ │ ├── backend.rs │ │ ├── global_runtime.rs │ │ ├── mod.rs │ │ ├── scoped_runtime.rs │ │ └── skip.rs └── tests │ ├── .gitignore │ ├── lighttpd-minimal │ ├── Cargo.toml │ └── src │ │ └── main.rs │ ├── lighttpd │ ├── Cargo.toml │ └── src │ │ └── main.rs │ ├── minimal │ ├── Cargo.toml │ ├── reference_pdg.bc │ └── src │ │ └── main.rs │ └── misc │ ├── Cargo.toml │ └── src │ ├── main.rs │ ├── pointers.c │ ├── pointers.rs │ ├── safe_rust.rs │ └── stdlib.rs ├── azure-pipelines.yml ├── book.toml ├── c2rust-analyze ├── .gitignore ├── Cargo.toml ├── README.md ├── build.rs ├── rename_nll_facts.py ├── rust-toolchain.toml ├── scripts │ ├── auto_fix_errors.py │ ├── extract_working_defs.py │ ├── pointwise_metrics.py │ ├── pointwise_try_build.sh │ └── run_pointwise_metrics.sh ├── src │ ├── analyze.rs │ ├── annotate.rs │ ├── borrowck │ │ ├── atoms.rs │ │ ├── def_use.rs │ │ ├── dump.rs │ │ ├── mod.rs │ │ └── type_check.rs │ ├── context.rs │ ├── dataflow │ │ ├── mod.rs │ │ └── type_check.rs │ ├── equiv.rs │ ├── known_fn.rs │ ├── labeled_ty.rs │ ├── last_use.rs │ ├── log.rs │ ├── main.rs │ ├── panic_detail.rs │ ├── pointee_type │ │ ├── constraint_set.rs │ │ ├── mod.rs │ │ ├── solve.rs │ │ └── type_check.rs │ ├── pointer_id.rs │ ├── recent_writes.rs │ ├── rewrite │ │ ├── apply.rs │ │ ├── expr │ │ │ ├── convert.rs │ │ │ ├── distribute.rs │ │ │ ├── hir_only_casts.rs │ │ │ ├── mir_op.rs │ │ │ ├── mod.rs │ │ │ └── unlower.rs │ │ ├── mod.rs │ │ ├── shim.rs │ │ ├── span_index.rs │ │ ├── statics.rs │ │ └── ty.rs │ ├── trivial.rs │ ├── type_desc.rs │ └── util.rs └── tests │ ├── analyze.rs │ ├── analyze │ ├── macros.rs │ ├── ptr_addr_of.rs │ ├── rust_intrinsic.rs │ ├── string_casts.rs │ └── string_literals.rs │ ├── auto_fix_errors.rs │ ├── auto_fix_errors │ └── derive_copy.rs │ ├── common │ └── mod.rs │ ├── filecheck.rs │ └── filecheck │ ├── addr_of.rs │ ├── adjust_unsize.rs │ ├── aggregate1.rs │ ├── algo_md5.rs │ ├── alias1.rs │ ├── alias2.rs │ ├── alias3.rs │ ├── alloc.rs │ ├── as_ptr.rs │ ├── call1.rs │ ├── call_cast.rs │ ├── cast.rs │ ├── catch_panic.rs │ ├── cell.rs │ ├── clone1.rs │ ├── extern_fn1.rs │ ├── field_temp.rs │ ├── fields.rs │ ├── fixed.rs │ ├── foreign.rs │ ├── insertion_sort.rs │ ├── insertion_sort_driver.rs │ ├── insertion_sort_rewrites.rs │ ├── known_fn.rs │ ├── lighttpd_buffer.rs │ ├── move_mut.rs │ ├── non_null.rs │ ├── non_null_force.rs │ ├── non_null_rewrites.rs │ ├── offset1.rs │ ├── offset2.rs │ ├── offset_rewrites.rs │ ├── pointee.rs │ ├── ptrptr1.rs │ ├── regions_fixed.rs │ ├── rewrite_nullable_box.rs │ ├── rewrite_paths.rs │ ├── rewrite_paths_manual_shim.rs │ ├── statics.rs │ ├── test_attrs.rs │ ├── trivial.rs │ ├── type_alias.rs │ ├── type_annotation_rewrite.rs │ ├── unrewritten_calls.rs │ └── unrewritten_calls_shim_fail.rs ├── c2rust-asm-casts ├── Cargo.toml └── src │ └── lib.rs ├── c2rust-ast-builder ├── Cargo.toml └── src │ ├── builder.rs │ ├── into_symbol.rs │ └── lib.rs ├── c2rust-ast-exporter ├── Cargo.toml ├── build.rs └── src │ ├── .clang-format │ ├── .dir-locals.el │ ├── AstExporter.cpp │ ├── AstExporter.hpp │ ├── CMakeLists.txt │ ├── ExportResult.cpp │ ├── ExportResult.hpp │ ├── FloatingLexer.cpp │ ├── FloatingLexer.h │ ├── Main.cpp │ ├── ast_tags.hpp │ ├── clang_ast.rs │ ├── lib.rs │ ├── tinycbor.config │ └── tinycbor_fix_build.patch ├── c2rust-ast-printer ├── COPYRIGHT ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md └── src │ ├── lib.rs │ ├── pprust.rs │ └── pprust │ └── tests.rs ├── c2rust-bitfields-derive ├── Cargo.toml ├── README.md └── src │ └── lib.rs ├── c2rust-bitfields ├── Cargo.toml ├── README.md ├── c2rust-tests │ ├── bitfields.c │ └── test_structs.rs └── src │ └── lib.rs ├── c2rust-build-paths ├── Cargo.toml └── src │ └── lib.rs ├── c2rust-macros ├── Cargo.toml └── src │ └── lib.rs ├── c2rust-refactor ├── .gitignore ├── Cargo.toml ├── PLUGINS.txt ├── README.md ├── SAVE.txt ├── TODO ├── build.rs ├── config.ld ├── doc │ ├── gen_command_docs.py │ ├── ldoc.css │ ├── literate │ │ ├── README.md │ │ ├── __init__.py │ │ ├── __main__.py │ │ ├── annot.py │ │ ├── diff.py │ │ ├── file.py │ │ ├── format.py │ │ ├── highlight.py │ │ ├── marks.py │ │ ├── parse.py │ │ ├── points.py │ │ ├── refactor.py │ │ └── render.py │ ├── rewrite.md │ ├── run_ldoc.sh │ ├── run_literate.py │ ├── scripting_api.html │ └── select.md ├── editor │ └── vim8.vim ├── gen │ ├── ast.py │ ├── ast.txt │ ├── ast_deref.py │ ├── ast_equiv.py │ ├── ast_names.py │ ├── get_node_id.py │ ├── get_span.py │ ├── list_node_ids.py │ ├── lr_expr.py │ ├── lua_ast_node.py │ ├── mac_match.py │ ├── mac_table.py │ ├── matcher.py │ ├── nt_match.py │ ├── process_ast.py │ ├── rewrite.py │ └── util.py ├── misc │ └── rustfmt-clean-parens.diff ├── plugin_stub.rs ├── runtime │ ├── Cargo.toml │ └── src │ │ ├── array.rs │ │ ├── block_ptr.rs │ │ ├── lib.rs │ │ └── util │ │ ├── mod.rs │ │ ├── nullable.rs │ │ └── prefix_ptr.rs ├── scripts │ ├── change_ast_kinds.lua │ ├── cleanup_params_locals.lua │ ├── lh_table_counter_cell.rs │ ├── noop_traversal.lua │ ├── rust_ast.lua │ ├── simplify_paths.lua │ ├── test_ownership_upgrade_ptrs.lua │ ├── test_upgrade_ptr_to_ref.lua │ ├── upgrade_ptr_to_ref.lua │ ├── utils.lua │ └── wrapping_arith_to_normal.lua ├── src │ ├── analysis │ │ ├── labeled_ty.rs │ │ ├── mod.rs │ │ ├── ownership │ │ │ ├── README.md │ │ │ ├── annot.rs │ │ │ ├── constraint.rs │ │ │ ├── context.rs │ │ │ ├── debug.rs │ │ │ ├── inst.rs │ │ │ ├── inter.rs │ │ │ ├── intra.rs │ │ │ ├── mod.rs │ │ │ ├── mono.rs │ │ │ └── mono_filter.rs │ │ └── type_eq.rs │ ├── ast_manip │ │ ├── ast_deref.rs │ │ ├── ast_equiv.rs │ │ ├── ast_map.rs │ │ ├── ast_names.rs │ │ ├── ast_node.rs │ │ ├── comments.rs │ │ ├── fn_edit.rs │ │ ├── fold.rs │ │ ├── get_node_id.rs │ │ ├── get_span.rs │ │ ├── list_node_ids.rs │ │ ├── lr_expr.rs │ │ ├── mod.rs │ │ ├── number_nodes.rs │ │ ├── output_exprs.rs │ │ ├── remove_paren.rs │ │ ├── seq_edit.rs │ │ ├── util.rs │ │ ├── visit.rs │ │ └── visit_node.rs │ ├── collapse │ │ ├── cfg_attr.rs │ │ ├── deleted.rs │ │ ├── mac_table.rs │ │ ├── macros.rs │ │ ├── mod.rs │ │ ├── node_map.rs │ │ └── nt_match.rs │ ├── command.rs │ ├── contains_mark.rs │ ├── context.rs │ ├── driver.rs │ ├── file_io.rs │ ├── illtyped.rs │ ├── interact │ │ ├── main_thread.rs │ │ ├── mod.rs │ │ ├── plain_backend.rs │ │ ├── vim8_backend.rs │ │ └── worker.rs │ ├── lib.rs │ ├── macros.rs │ ├── mark_adjust.rs │ ├── matcher │ │ ├── bindings.rs │ │ ├── impls.rs │ │ ├── mod.rs │ │ └── subst.rs │ ├── node_map.rs │ ├── path_edit.rs │ ├── pick_node.rs │ ├── plugin.rs │ ├── print_spans.rs │ ├── reflect.rs │ ├── resolve.rs │ ├── rewrite │ │ ├── base.rs │ │ ├── cleanup.rs │ │ ├── files.rs │ │ ├── json.rs │ │ ├── mod.rs │ │ └── strategy │ │ │ ├── equal.rs │ │ │ ├── item_header.rs │ │ │ ├── mod.rs │ │ │ ├── print.rs │ │ │ └── recursive.rs │ ├── scripting.rs │ ├── scripting │ │ ├── ast_visitor.rs │ │ ├── into_lua_ast.rs │ │ ├── lua_ty.rs │ │ ├── merge_lua_ast.rs │ │ └── to_lua_ast_node.rs │ ├── select │ │ ├── filter.rs │ │ ├── mod.rs │ │ ├── parse.rs │ │ └── visitor.rs │ ├── span_fix.rs │ ├── transform │ │ ├── canonicalize_refs.rs │ │ ├── casts.rs │ │ ├── casts │ │ │ └── tests.rs │ │ ├── char_literals.rs │ │ ├── control_flow.rs │ │ ├── externs.rs │ │ ├── format.rs │ │ ├── funcs.rs │ │ ├── generics.rs │ │ ├── ionize.rs │ │ ├── items.rs │ │ ├── lifetime_analysis.rs │ │ ├── linkage.rs │ │ ├── literals.rs │ │ ├── mod.rs │ │ ├── ownership.rs │ │ ├── reorganize_definitions.rs │ │ ├── retype.rs │ │ ├── rewrite.rs │ │ ├── statics.rs │ │ ├── structs.rs │ │ ├── test.rs │ │ └── vars.rs │ ├── type_map.rs │ └── util │ │ ├── cursor.rs │ │ ├── dataflow.rs │ │ └── mod.rs └── tests │ ├── .gitignore │ ├── abstract │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── autoretype_array │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── autoretype_method │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── bitcast_retype │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── canonicalize_structs_derive │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── change_ast_kinds │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── cleanup_params_locals │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── collapse_cfg │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── collapse_cfg_attr │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── convert_format_args │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── create_item_after │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── create_item_inside │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── create_item_preserve_syntax │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── expr_parens_1 │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── expr_parens_2 │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── fold_let_assign │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── func_to_method │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── generalize_item │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── ionize │ └── old.rs │ ├── let_x_uninitialized │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── matcher_def │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── matcher_marked │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── matcher_typed │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── misc │ ├── analysis_ownership.rs │ ├── analysis_ownership_split.rs │ ├── analysis_type_eq.rs │ ├── analysis_type_eq_alias.rs │ ├── debug_callees.rs │ ├── mark_uses.rs │ ├── multi_stmt.rs │ ├── pat_let_x.txt │ ├── pat_while_loop.txt │ ├── pat_wrapping_add.txt │ ├── repl_add.txt │ ├── repl_for_loop.txt │ ├── repl_let_x_uninit.txt │ ├── select_path_prefix.rs │ ├── src_let_x.txt │ ├── src_while_loop.txt │ ├── src_wrapping_add.txt │ ├── src_wrapping_add_inside_macro.txt │ ├── src_wrapping_add_multi.txt │ └── src_wrapping_add_println.txt │ ├── multi_rewrite │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── ownership_constraint_attr │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── ownership_reinfer │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── ownership_split │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── ownership_split_part1 │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── ownership_split_part2 │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── ownership_upgrade_ptrs │ ├── new.rs │ ├── old.rs │ ├── run.sh │ └── runtime │ │ ├── Cargo.toml │ │ └── src │ │ └── lib.rs │ ├── reconstruct_for_range │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── reconstruct_while │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── reflect │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── remove_paren │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── remove_unused_labels │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── rename_unnamed │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── reorganize_definitions │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── retype_argument │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── retype_return │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── retype_static │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── retype_str │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── rewrite_arg_list │ ├── new.rs │ ├── no-rustfmt │ ├── old.rs │ └── run.sh │ ├── run-all-tests.sh │ ├── run-test.sh │ ├── set_visibility │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── sink_lets │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── static_collect_to_struct │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── static_to_local_ref │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── struct_assign_to_update │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── struct_merge_updates │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── uninit_to_default │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── upgrade_ptr_to_ref │ ├── new.rs │ ├── old.rs │ └── run.sh │ ├── wrap_api │ ├── new.rs │ ├── old.rs │ └── run.sh │ └── wrap_extern │ ├── new.rs │ ├── old.rs │ └── run.sh ├── c2rust-transpile ├── .gitignore ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ ├── build_files │ ├── Cargo.toml.hbs │ ├── build.rs.hbs │ ├── generated-rust-toolchain.toml │ ├── lib.rs.hbs │ └── mod.rs │ ├── c_ast │ ├── conversion.rs │ ├── iterators.rs │ ├── mod.rs │ └── print.rs │ ├── cfg │ ├── inc_cleanup.rs │ ├── loops.rs │ ├── mod.rs │ ├── multiples.rs │ ├── relooper.rs │ └── structures.rs │ ├── compile_cmds │ └── mod.rs │ ├── convert_type.rs │ ├── diagnostics.rs │ ├── lib.rs │ ├── renamer.rs │ ├── rust_ast │ ├── comment_store.rs │ ├── item_store.rs │ ├── mod.rs │ ├── set_span.rs │ └── traverse.rs │ ├── translator │ ├── assembly.rs │ ├── atomics.rs │ ├── builtins.rs │ ├── comments.rs │ ├── literals.rs │ ├── main_function.rs │ ├── mod.rs │ ├── named_references.rs │ ├── operators.rs │ ├── simd.rs │ ├── structs.rs │ └── variadic.rs │ └── with_stmts.rs ├── c2rust ├── Cargo.toml ├── README.md ├── rust-toolchain.toml └── src │ ├── bin │ └── c2rust-transpile.rs │ ├── main.rs │ └── refactor.yaml ├── default.nix ├── docker ├── .dockerignore ├── Dockerfile ├── README.md ├── docker_exec.sh └── docker_run.sh ├── docs ├── README-developers.md ├── build_manual.md ├── c2rust-overview.png ├── known-limitations.md └── source_walkthrough.md ├── dynamic_instrumentation ├── Cargo.toml ├── README.md ├── build.rs ├── rust-toolchain.toml └── src │ ├── arg.rs │ ├── callbacks.rs │ ├── hooks.rs │ ├── instrument.rs │ ├── into_operand.rs │ ├── main.rs │ ├── mir_utils │ ├── deref.rs │ └── mod.rs │ ├── point │ ├── apply.rs │ ├── build.rs │ ├── cast.rs │ ├── mod.rs │ └── source.rs │ ├── runtime_conversions.rs │ └── util.rs ├── examples ├── README.md ├── genann │ └── README.md ├── grabc │ └── README.md ├── json-c │ ├── README.md │ ├── configure │ ├── repo │ │ ├── .editorconfig │ │ ├── .gitignore │ │ ├── .travis.yml │ │ ├── AUTHORS │ │ ├── Android.configure.mk │ │ ├── CMakeLists.txt │ │ ├── COPYING │ │ ├── ChangeLog │ │ ├── Doxyfile │ │ ├── INSTALL │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ ├── NEWS │ │ ├── README │ │ ├── README.html │ │ ├── README.md │ │ ├── RELEASE_CHECKLIST.txt │ │ ├── STYLE.txt │ │ ├── aclocal.m4 │ │ ├── appveyor.yml │ │ ├── arraylist.c │ │ ├── arraylist.h │ │ ├── autoconf-archive │ │ │ ├── README.txt │ │ │ └── m4 │ │ │ │ ├── ax_append_compile_flags.m4 │ │ │ │ ├── ax_append_flag.m4 │ │ │ │ ├── ax_check_compile_flag.m4 │ │ │ │ ├── ax_compile_check_sizeof.m4 │ │ │ │ └── ax_require_defined.m4 │ │ ├── autogen.sh │ │ ├── bits.h │ │ ├── compile │ │ ├── config.guess │ │ ├── config.h.in │ │ ├── config.h.win32 │ │ ├── config.sub │ │ ├── configure │ │ ├── configure.ac │ │ ├── debug.c │ │ ├── debug.h │ │ ├── depcomp │ │ ├── doc │ │ │ └── html │ │ │ │ ├── README_8md.html │ │ │ │ ├── annotated.html │ │ │ │ ├── arraylist_8h.html │ │ │ │ ├── bc_s.png │ │ │ │ ├── bdwn.png │ │ │ │ ├── bits_8h.html │ │ │ │ ├── classes.html │ │ │ │ ├── closed.png │ │ │ │ ├── debug_8h.html │ │ │ │ ├── deprecated.html │ │ │ │ ├── doc.png │ │ │ │ ├── doxygen.css │ │ │ │ ├── doxygen.png │ │ │ │ ├── dynsections.js │ │ │ │ ├── files.html │ │ │ │ ├── folderclosed.png │ │ │ │ ├── folderopen.png │ │ │ │ ├── ftv2blank.png │ │ │ │ ├── ftv2doc.png │ │ │ │ ├── ftv2folderclosed.png │ │ │ │ ├── ftv2folderopen.png │ │ │ │ ├── ftv2lastnode.png │ │ │ │ ├── ftv2link.png │ │ │ │ ├── ftv2mlastnode.png │ │ │ │ ├── ftv2mnode.png │ │ │ │ ├── ftv2node.png │ │ │ │ ├── ftv2plastnode.png │ │ │ │ ├── ftv2pnode.png │ │ │ │ ├── ftv2splitbar.png │ │ │ │ ├── ftv2vertline.png │ │ │ │ ├── functions.html │ │ │ │ ├── functions_vars.html │ │ │ │ ├── globals.html │ │ │ │ ├── globals_a.html │ │ │ │ ├── globals_defs.html │ │ │ │ ├── globals_e.html │ │ │ │ ├── globals_enum.html │ │ │ │ ├── globals_eval.html │ │ │ │ ├── globals_f.html │ │ │ │ ├── globals_func.html │ │ │ │ ├── globals_h.html │ │ │ │ ├── globals_i.html │ │ │ │ ├── globals_j.html │ │ │ │ ├── globals_l.html │ │ │ │ ├── globals_m.html │ │ │ │ ├── globals_n.html │ │ │ │ ├── globals_p.html │ │ │ │ ├── globals_s.html │ │ │ │ ├── globals_t.html │ │ │ │ ├── globals_type.html │ │ │ │ ├── globals_vars.html │ │ │ │ ├── index.html │ │ │ │ ├── issues__closed__for__0_813_8md.html │ │ │ │ ├── jquery.js │ │ │ │ ├── json_8h.html │ │ │ │ ├── json__c__version_8h.html │ │ │ │ ├── json__inttypes_8h.html │ │ │ │ ├── json__object_8h.html │ │ │ │ ├── json__object__iterator_8h.html │ │ │ │ ├── json__object__private_8h.html │ │ │ │ ├── json__pointer_8h.html │ │ │ │ ├── json__tokener_8h.html │ │ │ │ ├── json__util_8h.html │ │ │ │ ├── json__visit_8h.html │ │ │ │ ├── linkhash_8h.html │ │ │ │ ├── math__compat_8h.html │ │ │ │ ├── md_issues_closed_for_0_813.html │ │ │ │ ├── menu.js │ │ │ │ ├── menudata.js │ │ │ │ ├── nav_f.png │ │ │ │ ├── nav_g.png │ │ │ │ ├── nav_h.png │ │ │ │ ├── open.png │ │ │ │ ├── pages.html │ │ │ │ ├── printbuf_8h.html │ │ │ │ ├── random__seed_8h.html │ │ │ │ ├── snprintf__compat_8h.html │ │ │ │ ├── splitbar.png │ │ │ │ ├── strdup__compat_8h.html │ │ │ │ ├── strerror__override_8h.html │ │ │ │ ├── strerror__override__private_8h.html │ │ │ │ ├── structarray__list.html │ │ │ │ ├── structjson__object.html │ │ │ │ ├── structjson__object__iter.html │ │ │ │ ├── structjson__object__iterator.html │ │ │ │ ├── structjson__tokener.html │ │ │ │ ├── structjson__tokener__srec.html │ │ │ │ ├── structlh__entry.html │ │ │ │ ├── structlh__table.html │ │ │ │ ├── structprintbuf.html │ │ │ │ ├── sync_off.png │ │ │ │ ├── sync_on.png │ │ │ │ ├── tab_a.png │ │ │ │ ├── tab_b.png │ │ │ │ ├── tab_h.png │ │ │ │ ├── tab_s.png │ │ │ │ ├── tabs.css │ │ │ │ ├── unionjson__object_1_1data.html │ │ │ │ └── vasprintf__compat_8h.html │ │ ├── fuzz │ │ │ ├── README.md │ │ │ ├── build.sh │ │ │ ├── tokener_parse_ex_fuzzer.cc │ │ │ └── tokener_parse_ex_fuzzer.dict │ │ ├── install-sh │ │ ├── issues_closed_for_0.13.md │ │ ├── json-c-uninstalled.pc.in │ │ ├── json-c.pc.in │ │ ├── json.h │ │ ├── json_c_version.c │ │ ├── json_c_version.h │ │ ├── json_config.h.in │ │ ├── json_config.h.win32 │ │ ├── json_inttypes.h │ │ ├── json_object.c │ │ ├── json_object.h │ │ ├── json_object_iterator.c │ │ ├── json_object_iterator.h │ │ ├── json_object_private.h │ │ ├── json_pointer.c │ │ ├── json_pointer.h │ │ ├── json_tokener.c │ │ ├── json_tokener.h │ │ ├── json_util.c │ │ ├── json_util.h │ │ ├── json_visit.c │ │ ├── json_visit.h │ │ ├── libjson.c │ │ ├── linkhash.c │ │ ├── linkhash.h │ │ ├── ltmain.sh │ │ ├── math_compat.h │ │ ├── missing │ │ ├── printbuf.c │ │ ├── printbuf.h │ │ ├── random_seed.c │ │ ├── random_seed.h │ │ ├── rust │ │ │ ├── build.ninja │ │ │ └── src │ │ │ │ ├── c_funcs.rs │ │ │ │ ├── json_pointer_f.c │ │ │ │ ├── last_err.c │ │ │ │ ├── lib.rs │ │ │ │ └── sprintbuf.c │ │ ├── snprintf_compat.h │ │ ├── strdup_compat.h │ │ ├── strerror_override.c │ │ ├── strerror_override.h │ │ ├── strerror_override_private.h │ │ ├── test-driver │ │ ├── tests │ │ │ ├── Makefile.am │ │ │ ├── Makefile.in │ │ │ ├── parse_flags.c │ │ │ ├── parse_flags.h │ │ │ ├── test-defs.sh │ │ │ ├── test1.c │ │ │ ├── test1.expected │ │ │ ├── test1.test │ │ │ ├── test1Formatted_plain.expected │ │ │ ├── test1Formatted_pretty.expected │ │ │ ├── test1Formatted_spaced.expected │ │ │ ├── test2.c │ │ │ ├── test2.expected │ │ │ ├── test2.test │ │ │ ├── test2Formatted_plain.expected │ │ │ ├── test2Formatted_pretty.expected │ │ │ ├── test2Formatted_spaced.expected │ │ │ ├── test4.c │ │ │ ├── test4.expected │ │ │ ├── test4.test │ │ │ ├── testReplaceExisting.c │ │ │ ├── testReplaceExisting.expected │ │ │ ├── testReplaceExisting.test │ │ │ ├── test_basic.test │ │ │ ├── test_cast.c │ │ │ ├── test_cast.expected │ │ │ ├── test_cast.test │ │ │ ├── test_charcase.c │ │ │ ├── test_charcase.expected │ │ │ ├── test_charcase.test │ │ │ ├── test_compare.c │ │ │ ├── test_compare.expected │ │ │ ├── test_compare.test │ │ │ ├── test_deep_copy.c │ │ │ ├── test_deep_copy.expected │ │ │ ├── test_deep_copy.test │ │ │ ├── test_double_serializer.c │ │ │ ├── test_double_serializer.expected │ │ │ ├── test_double_serializer.test │ │ │ ├── test_float.c │ │ │ ├── test_float.expected │ │ │ ├── test_float.test │ │ │ ├── test_int_add.c │ │ │ ├── test_int_add.expected │ │ │ ├── test_int_add.test │ │ │ ├── test_json_pointer.c │ │ │ ├── test_json_pointer.expected │ │ │ ├── test_json_pointer.test │ │ │ ├── test_locale.c │ │ │ ├── test_locale.expected │ │ │ ├── test_locale.test │ │ │ ├── test_null.c │ │ │ ├── test_null.expected │ │ │ ├── test_null.test │ │ │ ├── test_parse.c │ │ │ ├── test_parse.expected │ │ │ ├── test_parse.test │ │ │ ├── test_parse_int64.c │ │ │ ├── test_parse_int64.expected │ │ │ ├── test_parse_int64.test │ │ │ ├── test_printbuf.c │ │ │ ├── test_printbuf.expected │ │ │ ├── test_printbuf.test │ │ │ ├── test_set_serializer.c │ │ │ ├── test_set_serializer.expected │ │ │ ├── test_set_serializer.test │ │ │ ├── test_set_value.c │ │ │ ├── test_set_value.expected │ │ │ ├── test_set_value.test │ │ │ ├── test_util_file.c │ │ │ ├── test_util_file.expected │ │ │ ├── test_util_file.test │ │ │ ├── test_visit.c │ │ │ ├── test_visit.expected │ │ │ ├── test_visit.test │ │ │ └── valid.json │ │ └── vasprintf_compat.h │ └── translate.py ├── libxml2 │ ├── README.md │ ├── common.py │ ├── patch_translated_code.py │ └── translate.py ├── lil │ └── README.md ├── qsort │ ├── CMakeLists.txt │ ├── README.md │ └── qsort.c ├── robotfindskitten │ ├── .gitignore │ ├── build.sh │ ├── refactor.md │ ├── repo │ │ ├── .gitignore │ │ ├── AUTHORS │ │ ├── BUGS │ │ ├── COPYING │ │ ├── ChangeLog │ │ ├── INSTALL │ │ ├── Makefile.am │ │ ├── Makefile.in │ │ ├── NEWS │ │ ├── README │ │ ├── aclocal.m4 │ │ ├── config.guess │ │ ├── config.h.in │ │ ├── config.sub │ │ ├── configure │ │ ├── configure.in │ │ ├── depcomp │ │ ├── doc │ │ │ ├── Makefile.am │ │ │ ├── Makefile.in │ │ │ ├── robotfindskitten.6 │ │ │ ├── robotfindskitten.info │ │ │ ├── robotfindskitten.texi │ │ │ └── texinfo.tex │ │ ├── install-sh │ │ ├── ltmain.sh │ │ ├── missing │ │ ├── rust │ │ │ ├── Cargo.toml │ │ │ ├── build.rs │ │ │ └── rust-toolchain.toml │ │ └── src │ │ │ ├── Makefile.am │ │ │ ├── Makefile.in │ │ │ ├── draw.h │ │ │ ├── messages.h │ │ │ └── robotfindskitten.c │ ├── rfk.conf │ └── translate.py ├── snudown │ ├── README.md │ ├── common.py │ ├── snudownrust.rs │ └── translate.py ├── tinycc │ ├── README.md │ ├── common.py │ └── translate.py ├── tmux │ ├── README.md │ ├── common.py │ └── translate.py ├── urlparser │ └── README.md └── xzoom │ └── README.md ├── flake.lock ├── flake.nix ├── manual ├── README.md ├── SUMMARY.md ├── c2rust-bitfields │ └── README.md ├── c2rust-refactor │ ├── README.md │ ├── commands.md │ ├── doc │ │ ├── ldoc.css │ │ └── scripting_api.html │ ├── rewrite.md │ ├── select.md │ └── src │ │ └── analysis │ │ └── ownership │ │ └── README.md ├── c2rust-transpile │ └── README.md ├── docker │ └── README.md ├── docs │ ├── README-developers.md │ ├── build_manual.md │ ├── c2rust-overview.png │ ├── known-limitations.md │ └── source_walkthrough.md ├── examples │ ├── README.md │ ├── genann │ │ └── README.md │ ├── grabc │ │ └── README.md │ ├── json-c │ │ ├── README.md │ │ └── repo │ │ │ ├── README.md │ │ │ ├── fuzz │ │ │ └── README.md │ │ │ └── issues_closed_for_0.13.md │ ├── libxml2 │ │ └── README.md │ ├── lil │ │ └── README.md │ ├── qsort │ │ └── README.md │ ├── robotfindskitten │ │ └── README.md │ ├── snudown │ │ └── README.md │ ├── tmux │ │ └── README.md │ ├── urlparser │ │ └── README.md │ └── xzoom │ │ └── README.md ├── installation.md ├── intro.md ├── manual │ ├── README.md │ └── SUMMARY.md ├── preprocessors │ └── generator_dispatch.py ├── quickstart.md ├── rust-toolchain.toml ├── scripts │ └── requirements.txt └── tests │ └── README.md ├── pdg ├── Cargo.toml ├── build.rs └── src │ ├── assert.rs │ ├── builder.rs │ ├── graph.rs │ ├── info.rs │ ├── lib.rs │ ├── main.rs │ ├── query.rs │ ├── snapshots │ ├── c2rust_pdg__tests__analysis_tests_misc_pdg_snapshot_debug.snap │ └── c2rust_pdg__tests__analysis_tests_misc_pdg_snapshot_release.snap │ ├── util.rs │ └── util │ └── serde.rs ├── rust-toolchain.toml ├── scripts ├── .flake8 ├── build_translator.py ├── cborpp.py ├── cc-wrappers │ ├── cc │ ├── clang │ ├── common.py │ ├── gcc │ ├── ld │ ├── ld.bfd │ └── ld.gold ├── common.py ├── convert_build_commands.py ├── csmith.py ├── docker_build.sh ├── integration_test_translator.py ├── link_manual.py ├── llvm-10.0.0-key.asc ├── llvm-10.0.1-key.asc ├── llvm-11.0.0-key.asc ├── llvm-11.1.0-key.asc ├── llvm-12.0.0-key.asc ├── llvm-15.0.1-key.asc ├── llvm-16.0.6-key.asc ├── llvm-17.0.6-key.asc ├── llvm-18.1.1-key.asc ├── llvm-6.0.0-key.asc ├── llvm-6.0.1-key.asc ├── llvm-7.0.0-key.asc ├── llvm-7.0.1-key.asc ├── llvm-8.0.0-key.asc ├── llvm-9.0.0-key.asc ├── package.py ├── pdg.sh ├── pdg_setup.sh ├── print_clang_ast.py ├── provision.sh ├── provision_arch.sh ├── provision_cmake.sh ├── provision_deb.sh ├── provision_dnf.sh ├── provision_git.sh ├── provision_mac.sh ├── provision_rust.sh ├── provision_yum.sh ├── query_toml.py ├── requirements.txt ├── run_ci_checks.sh ├── rust_file.py ├── test_examples.py ├── test_rust_refactor.py └── test_translator.py └── tests ├── .gitignore ├── README.md ├── arrays ├── Cargo.toml ├── build.rs └── src │ ├── arrays.c │ ├── incomplete_arrays.c │ ├── packed_arrays.c │ ├── test_arrays.rs │ └── variable_arrays.c ├── asm.aarch64 ├── Cargo.toml ├── build.rs ├── src │ ├── asm.c │ └── test_asm.rs └── target-tuple ├── asm.arm ├── Cargo.toml ├── build.rs ├── src │ ├── asm.c │ └── test_asm.rs └── target-tuple ├── asm.x86_64 ├── Cargo.toml ├── build.rs ├── src │ ├── asm.c │ └── test_asm.rs └── target-tuple ├── builtins ├── Cargo.toml ├── build.rs └── src │ ├── atomics.c │ ├── math.c │ ├── mem_x_fns.c │ └── test_builtins.rs ├── casts ├── Cargo.toml ├── build.rs └── src │ ├── cast_funptr.c │ ├── casts.c │ └── test_casts.rs ├── comments ├── Cargo.toml ├── build.rs └── src │ ├── comments.c │ ├── comments.h │ └── test_comments.rs ├── conditionals ├── Cargo.toml ├── build.rs └── src │ ├── binary_conditional.c │ ├── conditional.c │ ├── conditionals.c │ ├── else_if_chain.c │ ├── test_conditionals.rs │ └── unused_conditionals.c ├── enums ├── Cargo.toml ├── build.rs └── src │ ├── big_enum.c │ ├── enum_as_int.c │ ├── enum_compound.c │ ├── enum_duplicate.c │ ├── enum_fwd_decl.c │ ├── enum_ret.c │ ├── non_canonical_enum_def.c │ ├── test_enums.rs │ └── top_enum.c ├── example ├── Cargo.toml ├── build.rs └── src │ ├── add.c │ ├── skip_me.c │ ├── sub.c │ ├── test_add.rs │ ├── test_file_xfail.rs │ ├── test_fn_xfail.rs │ └── test_sub.rs ├── floats ├── Cargo.toml ├── build.rs └── src │ ├── no_float_wrapping_neg.c │ └── test_no_wrapping_neg.rs ├── gotos ├── Cargo.toml ├── build.rs └── src │ ├── dfa_binary_multiple_three.c │ ├── duffs.c │ ├── early_returns.c │ ├── idiomatic_nested_loops.c │ ├── idiomatic_switch.c │ ├── irreducible.c │ ├── jump_into_loop.c │ ├── label_break_trigger.c │ ├── lifted_decls.c │ ├── stmt_expr.c │ ├── test_dfa_multiple_three.rs │ ├── test_duffs.rs │ ├── test_early_returns.rs │ ├── test_idiomatic_control_flow.rs │ ├── test_irreducible.rs │ ├── test_stmt_expr.rs │ └── test_translation_only.rs ├── ints ├── Cargo.toml ├── build.rs └── src │ ├── arithmetic.c │ ├── chars.c │ ├── compound_assignment.c │ ├── const_test.c │ ├── implicit_int.c │ ├── sieve_of_eratosthenes.c │ ├── size_t.c │ ├── test_arithmetic.rs │ ├── test_compound_assignment.rs │ ├── test_const.rs │ ├── test_implicit_ints.rs │ ├── test_ints.rs │ ├── test_sieve_of_eratosthenes.rs │ ├── test_volatile.rs │ └── volatile.c ├── items ├── Cargo.toml ├── build.rs └── src │ ├── fn_attrs.c │ ├── functions.c │ ├── linking.c │ ├── nofnargs.c │ ├── noop.c │ ├── test_fn_attrs.rs │ ├── test_functions.rs │ ├── test_linking.rs │ ├── test_noop.rs │ ├── test_varargs.rs │ └── varargs.c ├── longdouble ├── Cargo.toml ├── build.rs └── src │ ├── long_double.c │ └── test_long_double.rs ├── loops ├── Cargo.toml ├── build.rs └── src │ ├── break_continue.c │ ├── goto_linear_cf.c │ ├── goto_loop_cf.c │ ├── goto_switch_cf.c │ ├── nested_goto.c │ ├── switch.c │ ├── test_goto.rs │ ├── test_loops.rs │ └── test_switch.rs ├── macros ├── Cargo.toml ├── build.rs └── src │ ├── define.c │ └── test_define.rs ├── misc ├── Cargo.toml ├── build.rs └── src │ ├── exprs.c │ ├── lvalues.c │ ├── malloc.c │ ├── qsort.c │ ├── shadowing.c │ ├── sizeofs.c │ ├── strings_h.c │ ├── test_exprs.rs │ ├── test_lvalues.rs │ ├── test_memory.rs │ ├── test_quicksort.rs │ ├── test_shadowing.rs │ ├── test_sizeofs.rs │ ├── test_typedef.rs │ ├── test_uninitialized.rs │ ├── typedef.c │ ├── uninitialized.c │ ├── unsized.c │ └── vectortypes.c ├── modules ├── Cargo.toml ├── build.rs └── src │ ├── modules.c │ ├── other_mod.h │ ├── other_mod2.h │ └── test_modules.rs ├── pointers ├── Cargo.toml ├── build.rs └── src │ ├── function_pointers.c │ ├── pointer_arith.c │ ├── pointer_init.c │ ├── ref_decay.c │ └── test_pointers.rs ├── simd.x86_64 ├── Cargo.toml ├── build.rs ├── src │ ├── test_x86.rs │ └── x86.c └── target-tuple ├── statics ├── Cargo.toml ├── build.rs └── src │ ├── attributes.c │ ├── sections.c │ ├── storage.c │ ├── test_sections.rs │ ├── test_storage.rs │ ├── test_thread_locals.rs │ └── thread_locals.c ├── structs ├── Cargo.toml ├── build.rs └── src │ ├── anonymous_decls.c │ ├── bitfields.c │ ├── flex_array_members.c │ ├── forward.c │ ├── self_referential.c │ ├── struct_with_exp.c │ ├── structs.c │ ├── test_anonymous_decls.rs │ ├── test_bitfields.rs │ ├── test_flex_array_members.rs │ ├── test_forward.rs │ ├── test_self_referential.rs │ ├── test_struct_with_exp.rs │ ├── test_structs.rs │ ├── test_variable_offsetof.rs │ └── variable_offsetof.c └── unions ├── Cargo.toml ├── build.rs └── src ├── test_unions.rs └── unions.c /.typos.toml: -------------------------------------------------------------------------------- 1 | # See https://github.com/crate-ci/typos/blob/master/docs/reference.md to configure typos 2 | 3 | [default] 4 | extend-ignore-re = [ 5 | "__fpr", 6 | "(?i)TYPDEF_DECL", 7 | "ParmVarDecl", 8 | "CharLits", 9 | "hpe_", 10 | "reachable_froms", 11 | "(\\$ot|ot =|ot\\.| ot\\)|ot_ty)", 12 | ] 13 | 14 | [default.extend-identifiers] 15 | mapp = "mapp" 16 | inouts = "inouts" 17 | 18 | [default.extend-words] 19 | clen = "clen" 20 | 21 | [files] 22 | extend-exclude = [ 23 | "examples/*", 24 | "*.asc", 25 | ] 26 | -------------------------------------------------------------------------------- /analysis/runtime/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-analysis-rt" 3 | version.workspace = true 4 | authors.workspace = true 5 | edition.workspace = true 6 | description = "C2Rust runtime for recording dynamic analysis, targeted by c2rust-instrument and consumed by c2rust-pdg" 7 | readme.workspace = true 8 | homepage.workspace = true 9 | repository.workspace = true 10 | license.workspace = true 11 | keywords.workspace = true 12 | categories.workspace = true 13 | publish = false 14 | 15 | [dependencies] 16 | serde = { version = "1.0", features = ["derive"] } 17 | bincode = "1.0.1" 18 | once_cell = "=1.21.2" 19 | enum_dispatch = "0.3" 20 | fs-err = "2" 21 | crossbeam-queue = "0.3" 22 | crossbeam-utils = "0.8" 23 | -------------------------------------------------------------------------------- /analysis/runtime/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod events; 2 | mod handlers; 3 | pub mod metadata; 4 | pub mod mir_loc; 5 | pub mod parse; 6 | pub mod runtime; 7 | 8 | pub use handlers::*; 9 | use runtime::{global_runtime::RUNTIME, skip::notify_if_events_were_skipped_before_main}; 10 | 11 | pub fn initialize() { 12 | notify_if_events_were_skipped_before_main(); 13 | RUNTIME.init(); 14 | } 15 | 16 | pub fn finalize() { 17 | RUNTIME.finalize(); 18 | } 19 | -------------------------------------------------------------------------------- /analysis/runtime/src/runtime/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod backend; 2 | pub mod global_runtime; 3 | pub mod scoped_runtime; 4 | pub mod skip; 5 | 6 | use std::{ 7 | error::Error, 8 | sync::{Condvar, Mutex}, 9 | }; 10 | 11 | use once_cell::sync::Lazy; 12 | 13 | type AnyError = Box; 14 | 15 | pub trait Detect: Sized { 16 | fn detect() -> Result; 17 | } 18 | 19 | static FINISHED: Lazy<(Mutex, Condvar)> = Lazy::new(|| (Mutex::new(false), Condvar::new())); 20 | -------------------------------------------------------------------------------- /analysis/tests/.gitignore: -------------------------------------------------------------------------------- 1 | *.bc 2 | # Sometimes generated by c2rust 3 | rust-toolchain.toml 4 | 5 | instrument.out.log 6 | instrument.err.jsonl 7 | -------------------------------------------------------------------------------- /analysis/tests/lighttpd-minimal/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lighttpd-minimal" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | c2rust-analysis-rt = { path = "../../runtime", optional = true, version = "0.20.0" } 9 | 10 | [features] 11 | miri = [] 12 | -------------------------------------------------------------------------------- /analysis/tests/lighttpd/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "lighttpd" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | c2rust-analysis-rt = { path = "../../runtime", optional = true, version = "0.20.0" } 9 | 10 | [features] 11 | miri = [] 12 | -------------------------------------------------------------------------------- /analysis/tests/minimal/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-analysis-tests-minimal" 3 | version = "0.1.0" 4 | authors = ["The C2Rust Development Team "] 5 | edition = "2021" 6 | 7 | [dependencies] 8 | libc = "0.2" 9 | c2rust-analysis-rt = { path = "../../runtime", optional = true, version = "0.20.0" } 10 | 11 | [features] 12 | miri = [] 13 | -------------------------------------------------------------------------------- /analysis/tests/minimal/reference_pdg.bc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/analysis/tests/minimal/reference_pdg.bc -------------------------------------------------------------------------------- /analysis/tests/minimal/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(rustc_private)] 2 | #![feature(c_variadic)] 3 | 4 | pub unsafe extern "C" fn testing() { 5 | let mut x = 10i32; 6 | let mut ptr = &mut x as *mut i32; 7 | let ref mut fresh1 = ptr; 8 | *fresh1 = &mut x as *mut i32; 9 | let fresh2 = fresh1; 10 | **fresh2 = 0; 11 | } 12 | 13 | fn main() { 14 | unsafe { testing() } 15 | } 16 | -------------------------------------------------------------------------------- /analysis/tests/misc/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-analysis-tests-misc" 3 | version = "0.1.0" 4 | authors = ["The C2Rust Development Team ", 5 | "Stephen Crane "] 6 | edition = "2021" 7 | 8 | [dependencies] 9 | libc = "0.2" 10 | c2rust-analysis-rt = { path = "../../runtime", optional = true, version = "0.20.0" } 11 | 12 | [features] 13 | miri = [] 14 | -------------------------------------------------------------------------------- /analysis/tests/misc/src/main.rs: -------------------------------------------------------------------------------- 1 | #![feature(rustc_private)] 2 | #![feature(c_variadic)] 3 | 4 | #[path = "pointers.rs"] 5 | pub mod pointers; 6 | #[path = "stdlib.rs"] 7 | pub mod stdlib; 8 | 9 | fn main() { 10 | pointers::main() 11 | } 12 | -------------------------------------------------------------------------------- /analysis/tests/misc/src/safe_rust.rs: -------------------------------------------------------------------------------- 1 | fn safe_fn() { 2 | println!("This is safe rust"); 3 | let mut s = "testing"; 4 | let s_ref = &s; 5 | // Deref a reference to ensure this is not analyzed as a raw ptr deref by 6 | // the dynamic analysis 7 | println!("{}", *s_ref); 8 | } 9 | -------------------------------------------------------------------------------- /analysis/tests/misc/src/stdlib.rs: -------------------------------------------------------------------------------- 1 | extern "C" { 2 | pub fn malloc(_: libc::c_ulong) -> *mut libc::c_void; 3 | } 4 | -------------------------------------------------------------------------------- /book.toml: -------------------------------------------------------------------------------- 1 | [book] 2 | title = "C2Rust Manual" 3 | src = "manual/" 4 | 5 | [build] 6 | create-missing = false 7 | preprocess = ["links", "index"] 8 | 9 | [preprocessor.generator_dispatch] 10 | command = "python3 manual/preprocessors/generator_dispatch.py" 11 | 12 | [output.html] 13 | 14 | # Uncomment the following to enable link-checking. This should not be enabled by 15 | # default because linkcheck has false positives due to our use of symlinks. 16 | # [output.linkcheck] 17 | -------------------------------------------------------------------------------- /c2rust-analyze/.gitignore: -------------------------------------------------------------------------------- 1 | /inspect/ 2 | *.rlib 3 | /tests/auto_fix_errors/*.json 4 | -------------------------------------------------------------------------------- /c2rust-analyze/build.rs: -------------------------------------------------------------------------------- 1 | use c2rust_build_paths::SysRoot; 2 | 3 | fn main() { 4 | let sysroot = SysRoot::resolve(); 5 | sysroot.link_rustc_private(); 6 | 7 | print!("cargo:rustc-env=C2RUST_TARGET_LIB_DIR="); 8 | print_bytes::println_lossy(&sysroot.rustlib()); 9 | } 10 | -------------------------------------------------------------------------------- /c2rust-analyze/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | ../rust-toolchain.toml -------------------------------------------------------------------------------- /c2rust-analyze/src/pointee_type/mod.rs: -------------------------------------------------------------------------------- 1 | use crate::context::AnalysisCtxt; 2 | use rustc_middle::mir::Body; 3 | 4 | mod constraint_set; 5 | mod solve; 6 | mod type_check; 7 | 8 | pub use self::constraint_set::{CTy, Constraint, ConstraintSet, VarTable}; 9 | pub use self::solve::{solve_constraints, PointeeTypes}; 10 | 11 | pub fn generate_constraints<'tcx>( 12 | acx: &AnalysisCtxt<'_, 'tcx>, 13 | mir: &Body<'tcx>, 14 | vars: &mut VarTable<'tcx>, 15 | ) -> ConstraintSet<'tcx> { 16 | type_check::visit(acx, mir, vars) 17 | } 18 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/analyze/macros.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // Just make sure this doesn't crash (see #862). 3 | let x = 1; 4 | let _ = std::ptr::addr_of!(x); 5 | } 6 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/analyze/ptr_addr_of.rs: -------------------------------------------------------------------------------- 1 | pub fn std_ptr_addr_of(x: ()) { 2 | let _ = std::ptr::addr_of!(x); 3 | } 4 | 5 | pub fn abs_std_ptr_addr_of(x: ()) { 6 | let _ = ::std::ptr::addr_of!(x); 7 | } 8 | 9 | pub fn core_ptr_addr_of(x: ()) { 10 | let _ = core::ptr::addr_of!(x); 11 | } 12 | 13 | pub fn abs_core_ptr_addr_of(x: ()) { 14 | let _ = ::core::ptr::addr_of!(x); 15 | } 16 | 17 | pub fn use_std_ptr_addr_of(x: ()) { 18 | use std::ptr::addr_of; 19 | let _ = addr_of!(x); 20 | } 21 | 22 | pub fn use_core_ptr_addr_of(x: ()) { 23 | use core::ptr::addr_of; 24 | let _ = addr_of!(x); 25 | } 26 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/analyze/rust_intrinsic.rs: -------------------------------------------------------------------------------- 1 | /// Check that `extern "rust-intrinsic"` (which can be generic) foreign `fn`s 2 | /// like [`std::mem::transmute`] don't crash `c2rust-analyze`. 3 | /// 4 | /// They currently do (in [`Instance::mono`] where there are generic args), 5 | /// which is why this is `#[cfg]`ed out for now. 6 | #[cfg(any())] 7 | pub unsafe fn f(x: *const u8) -> *const i8 { 8 | std::mem::transmute(x) 9 | } 10 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/filecheck/clone1.rs: -------------------------------------------------------------------------------- 1 | // Just check that the analysis doesn't crash on types deriving `Clone`. This derive is one of the 2 | // few sources of `impl`s in translated code. 3 | 4 | // CHECK-LABEL: struct Foo { 5 | #[derive(Clone, Copy)] 6 | struct Foo { 7 | x: i32, 8 | y: i32, 9 | } 10 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/filecheck/extern_fn1.rs: -------------------------------------------------------------------------------- 1 | use std::mem; 2 | 3 | extern "C" { 4 | fn foo(_: i32) -> i32; 5 | fn malloc(_: u64) -> *mut u8; 6 | fn free(_: *mut u8); 7 | } 8 | 9 | /* 10 | // TODO: calls to `malloc`/`free` library functions are not supported 11 | unsafe fn test_malloc() -> u32 { 12 | let p = malloc(4) as *mut u32; 13 | (*p) = 1; 14 | let x = *p; 15 | free(p as *mut u8); 16 | x 17 | } 18 | */ 19 | 20 | // CHECK-LABEL: final labeling for "fn_ptr" 21 | fn fn_ptr() { 22 | // TODO: function pointer types are not fully supported yet 23 | //let f: unsafe extern "C" fn(i32) -> i32 = foo; 24 | let f = foo; 25 | } 26 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/filecheck/known_fn.rs: -------------------------------------------------------------------------------- 1 | use std::ffi::{c_char, c_int}; 2 | 3 | extern "C" { 4 | fn access( 5 | path: *const c_char, 6 | amode: c_int, 7 | ) -> c_int; 8 | } 9 | 10 | // CHECK-LABEL: final labeling for "known_fn" 11 | pub fn known_fn() { 12 | // CHECK-DAG: ([[@LINE+3]]: path): addr_of = UNIQUE | NON_NULL | STACK, type = READ | UNIQUE | OFFSET_ADD | NON_NULL | STACK# 13 | // CHECK-LABEL: type assignment for "known_fn": 14 | // CHECK-DAG: ([[@LINE+1]]: path): &[i8] 15 | let path = b".\0" as *const u8 as *const c_char; 16 | unsafe { 17 | access(path, 0); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/filecheck/offset_rewrites.rs: -------------------------------------------------------------------------------- 1 | 2 | // Test dereferencing a pointer that has OFFSET permissions. This requires inserting a cast from 3 | // `&mut [T]` to `&mut T`. 4 | // CHECK-LABEL: fn offset_deref 5 | pub unsafe fn offset_deref(x: *mut i32, off: isize) -> *mut i32 { 6 | // CHECK: *&mut (x)[0] = 0; 7 | *x = 0; 8 | // CHECK: *&mut (&mut (x)[((1) as usize) ..])[0] = 1; 9 | *x.offset(1) = 1; 10 | // CHECK: {{.*}}x{{.*}} 11 | x 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-analyze/tests/filecheck/type_alias.rs: -------------------------------------------------------------------------------- 1 | // Regression test: previously, type aliases for `Option<*const T>` caused a panic, as 2 | // `deconstruct_hir_ty` would match the HIR alias with the MIR type `Option` and then get 3 | // confused due to the mismatched number of type arguments. 4 | type AliasOption = Option<*const u8>; 5 | 6 | // CHECK: struct UseAliasOption<'h0> 7 | struct UseAliasOption { 8 | // FIXME: should be `Option<&'h0 u8>` 9 | // CHECK: x: std::option::Option<&u8> 10 | x: AliasOption, 11 | } 12 | -------------------------------------------------------------------------------- /c2rust-asm-casts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-asm-casts" 3 | version.workspace = true 4 | authors.workspace = true 5 | edition.workspace = true 6 | description = "Type cast helpers for use with C2Rust's inline assembly implementation" 7 | readme.workspace = true 8 | homepage.workspace = true 9 | repository.workspace = true 10 | license.workspace = true 11 | keywords.workspace = true 12 | categories.workspace = true 13 | 14 | [dependencies] 15 | -------------------------------------------------------------------------------- /c2rust-ast-builder/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-ast-builder" 3 | version.workspace = true 4 | authors.workspace = true 5 | edition.workspace = true 6 | description = "Rust AST builder support crate for the C2Rust project" 7 | readme.workspace = true 8 | homepage.workspace = true 9 | repository.workspace = true 10 | license.workspace = true 11 | keywords.workspace = true 12 | categories.workspace = true 13 | 14 | [dependencies] 15 | proc-macro2 = { version = "1.0", features = ["span-locations"]} 16 | syn = { version = "1.0", features = ["full", "extra-traits", "printing", "clone-impls"]} 17 | -------------------------------------------------------------------------------- /c2rust-ast-builder/src/lib.rs: -------------------------------------------------------------------------------- 1 | mod builder; 2 | pub use crate::builder::{mk, properties, Builder, Make}; 3 | -------------------------------------------------------------------------------- /c2rust-ast-exporter/src/.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | IndentWidth: 4 3 | AllowShortCaseLabelsOnASingleLine: true 4 | -------------------------------------------------------------------------------- /c2rust-ast-exporter/src/.dir-locals.el: -------------------------------------------------------------------------------- 1 | ;;; Directory Local Variables 2 | ;;; For more information see (info "(emacs) Directory Variables") 3 | 4 | ((c++-mode 5 | (c-basic-offset . 4))) 6 | -------------------------------------------------------------------------------- /c2rust-ast-exporter/src/AstExporter.hpp: -------------------------------------------------------------------------------- 1 | // 2 | // AstExporter.hpp 3 | // 4 | // Created by Alec Theriault on 10/4/18. 5 | // 6 | 7 | #ifndef AstExporter_hpp 8 | #define AstExporter_hpp 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | using Outputs = std::unordered_map>; 15 | 16 | Outputs process(int argc, const char *argv[], int *result); 17 | 18 | #endif /* AstExporter_hpp */ 19 | -------------------------------------------------------------------------------- /c2rust-ast-exporter/src/FloatingLexer.h: -------------------------------------------------------------------------------- 1 | // 2 | // FloatingLexer.hpp 3 | // LLVMDemangle 4 | // 5 | // Created by Eric Mertens on 11/5/18. 6 | // 7 | 8 | #ifndef FloatingLexer_hpp 9 | #define FloatingLexer_hpp 10 | 11 | #include 12 | #include 13 | 14 | std::string matchFloatingLiteral(const char * prefix); 15 | 16 | #endif /* FloatingLexer_hpp */ 17 | -------------------------------------------------------------------------------- /c2rust-ast-exporter/src/Main.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Main.cpp 3 | // 4 | // Created by Alec Theriault on 10/4/18. 5 | // 6 | #include 7 | #include 8 | 9 | #include "AstExporter.hpp" 10 | 11 | int main(int argc, char *argv[]) { 12 | int result; 13 | auto outputs = process(argc, const_cast(argv), &result); 14 | 15 | for (auto const &kv : outputs) { 16 | auto const &filename = kv.first; 17 | auto const &bytes = kv.second; 18 | 19 | std::ofstream out(filename + ".cbor", out.binary | out.trunc); 20 | 21 | out.write(reinterpret_cast(bytes.data()), bytes.size()); 22 | } 23 | 24 | return result; 25 | } 26 | -------------------------------------------------------------------------------- /c2rust-ast-exporter/src/tinycbor.config: -------------------------------------------------------------------------------- 1 | # Disable json2cbor by disabling cjson. 2 | # We don't need json2cbor and it doesn't compile on BSD. 3 | system-cjson-pass := 4 | cjson-pass := 5 | -------------------------------------------------------------------------------- /c2rust-ast-printer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-ast-printer" 3 | version.workspace = true 4 | authors = ["The C2Rust Project Developers ", "The Rust Project Developers"] 5 | edition.workspace = true 6 | description = "Customized version of libsyntax rust pretty-printer" 7 | readme = "README.md" 8 | homepage.workspace = true 9 | repository.workspace = true 10 | license = "MIT OR Apache-2.0" 11 | keywords.workspace = true 12 | categories.workspace = true 13 | 14 | [dependencies] 15 | log = "0.4" 16 | syn = { version = "1.0", features = ["full", "proc-macro", "printing", "clone-impls"] } 17 | proc-macro2 = "1.0" 18 | prettyplease = "0.1.9" 19 | -------------------------------------------------------------------------------- /c2rust-ast-printer/README.md: -------------------------------------------------------------------------------- 1 | Forked from the `print` module from rustc `libsyntax` as of 3c2fd1a72d2e8cc80b354b4d2dd7931a7afe1b02 (2019-07-14). 2 | 3 | This code was originally and is still dual-licensed under the Apache 2.0 and MIT licenses. 4 | -------------------------------------------------------------------------------- /c2rust-ast-printer/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod pprust; 2 | -------------------------------------------------------------------------------- /c2rust-bitfields-derive/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-bitfields-derive" 3 | version.workspace = true 4 | authors.workspace = true 5 | edition.workspace = true 6 | description = "C-compatible struct bitfield derive implementation used in the C2Rust project" 7 | readme = "README.md" 8 | homepage.workspace = true 9 | repository.workspace = true 10 | license.workspace = true 11 | keywords.workspace = true 12 | categories.workspace = true 13 | 14 | [dependencies] 15 | proc-macro2 = "1.0" 16 | syn = { version = "1.0", features = ["full"] } 17 | quote = "1.0" 18 | 19 | [lib] 20 | proc-macro = true 21 | -------------------------------------------------------------------------------- /c2rust-bitfields-derive/README.md: -------------------------------------------------------------------------------- 1 | # C2Rust-Bitfields-Derive 2 | 3 | This crate is used to generate a proc macro in [c2rust-bitfields](https://github.com/immunant/c2rust/tree/master/c2rust-bitfields) and should not be a direct dependency. `c2rust-bitfields` re-exports the proc macro as well as other types and should be used instead. 4 | -------------------------------------------------------------------------------- /c2rust-bitfields/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-bitfields" 3 | version.workspace = true 4 | authors.workspace = true 5 | edition.workspace = true 6 | description = "C-compatible struct bitfield implementation used in the C2Rust project" 7 | readme = "README.md" 8 | homepage.workspace = true 9 | repository.workspace = true 10 | license.workspace = true 11 | keywords.workspace = true 12 | categories.workspace = true 13 | 14 | [dependencies] 15 | c2rust-bitfields-derive = { version = "0.20.0", path = "../c2rust-bitfields-derive" } 16 | 17 | [features] 18 | no_std = [] 19 | -------------------------------------------------------------------------------- /c2rust-build-paths/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-build-paths" 3 | version.workspace = true 4 | authors.workspace = true 5 | edition.workspace = true 6 | description = "C2Rust utilities related to build paths, primarily at build time" 7 | readme.workspace = true 8 | homepage.workspace = true 9 | repository.workspace = true 10 | license.workspace = true 11 | keywords.workspace = true 12 | categories.workspace = true 13 | 14 | [dependencies] 15 | print_bytes = "1.1" 16 | -------------------------------------------------------------------------------- /c2rust-macros/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust-macros" 3 | version = "0.20.0" 4 | authors = ["Stephen Crane ", "The C2Rust Project Developers "] 5 | edition = "2021" 6 | description = "Procedural macro support crate for C2Rust" 7 | license = "BSD-3-Clause" 8 | homepage = "https://c2rust.com/" 9 | repository = "https://github.com/immunant/c2rust" 10 | 11 | [lib] 12 | proc-macro = true 13 | 14 | [dependencies] 15 | proc-macro2 = { version = "1.0", features = ["nightly"] } 16 | quote = "1.0" 17 | syn = { version = "1.0", features = ["full", "extra-traits", "visit"] } 18 | -------------------------------------------------------------------------------- /c2rust-refactor/.gitignore: -------------------------------------------------------------------------------- 1 | /c2rust-refactor 2 | /c2rust-refactor.d 3 | __pycache__/ 4 | *.inc.rs 5 | /target/ 6 | /*.so 7 | *.new 8 | 9 | !/tests/ 10 | -------------------------------------------------------------------------------- /c2rust-refactor/SAVE.txt: -------------------------------------------------------------------------------- 1 | cargo +c2rust run -- select target 'crate; desc(union);' \; ionize -- old.rs -L /Users/emertens/Source/rust-epdtry/rust/build/x86_64-apple-darwin/stage2/lib/rustlib/x86_64-apple-darwin/lib 2 | 3 | file:///Users/emertens/Source/rust-epdtry/rust/build/x86_64-apple-darwin/doc/rustc/hir/struct.Path.html 4 | -------------------------------------------------------------------------------- /c2rust-refactor/config.ld: -------------------------------------------------------------------------------- 1 | project = 'c2rust-refactor' 2 | description = 'C2Rust Refactoring API' 3 | file = {'src/scripting.rs', 'src/scripting/'} 4 | dir = 'doc/' 5 | output = 'scripting_api' 6 | add_language_extension('rs','c') 7 | format = 'markdown' 8 | merge = true 9 | -------------------------------------------------------------------------------- /c2rust-refactor/doc/literate/__main__.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from literate import main 3 | if __name__ == '__main__': 4 | main(sys.argv[1:]) 5 | -------------------------------------------------------------------------------- /c2rust-refactor/doc/run_ldoc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SCRIPT_DIR=$(readlink -f $(dirname $0)) 6 | C2RUST_REFACTOR_DIR=$(dirname $SCRIPT_DIR) 7 | LUA_AST_NODE_RS=src/scripting/lua_ast_node_gen.inc.rs 8 | 9 | # Auto-generate the .rs file before running ldoc 10 | cd $C2RUST_REFACTOR_DIR 11 | python3 -B gen/process_ast.py lua_ast_node $LUA_AST_NODE_RS 12 | ldoc . 13 | rm $LUA_AST_NODE_RS 14 | -------------------------------------------------------------------------------- /c2rust-refactor/doc/run_literate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import os 3 | import sys 4 | 5 | if __name__ == '__main__': 6 | sys.path.append(os.path.dirname(__file__)) 7 | from literate import main 8 | main(sys.argv[1:]) 9 | -------------------------------------------------------------------------------- /c2rust-refactor/gen/ast.py: -------------------------------------------------------------------------------- 1 | from collections import namedtuple 2 | 3 | Enum = namedtuple('Enum', ('name', 'variants', 'attrs')) 4 | Struct = namedtuple('Struct', ('name', 'fields', 'is_tuple', 'attrs')) 5 | Flag = namedtuple('Flag', ('name', 'attrs')) 6 | Field = namedtuple('Field', ('name', 'attrs')) 7 | 8 | 9 | def variants_paths(se): 10 | if isinstance(se, Enum): 11 | return [(v, '%s::%s' % (se.name, v.name)) for v in se.variants] 12 | elif isinstance(se, Struct): 13 | return [(se, se.name)] 14 | else: 15 | raise TypeError('expected Struct or Enum') 16 | 17 | def has_field(s, name): 18 | if not isinstance(s, Struct): 19 | return False 20 | return any(f.name == name for f in s.fields) 21 | -------------------------------------------------------------------------------- /c2rust-refactor/gen/ast_deref.py: -------------------------------------------------------------------------------- 1 | from datetime import datetime 2 | 3 | from util import * 4 | 5 | @linewise 6 | def do_ast_deref_impl(d): 7 | supported = 'rewrite_seq_item' in d.attrs 8 | 9 | yield '#[allow(unused)]' 10 | yield 'impl AstDeref for %s {' % d.name 11 | yield ' type Target = Self;' 12 | yield ' fn ast_deref(&self) -> &Self { self }' 13 | yield '}' 14 | 15 | @linewise 16 | def generate(decls): 17 | yield '// AUTOMATICALLY GENERATED - DO NOT EDIT' 18 | yield '// Produced %s by process_ast.py' % (datetime.now(),) 19 | yield '' 20 | 21 | for d in decls: 22 | yield do_ast_deref_impl(d) 23 | 24 | -------------------------------------------------------------------------------- /c2rust-refactor/plugin_stub.rs: -------------------------------------------------------------------------------- 1 | //! A generic wrapper for building parts of `c2rust-refactor` as a plugin. 2 | 3 | #![feature( 4 | rustc_private, 5 | trace_macros, 6 | )] 7 | 8 | pub use c2rust_refactor::*; 9 | 10 | fn mk(t: T) -> Box { 11 | Box::new(transform::TransformCommand(t)) 12 | } 13 | 14 | // Adjust these lines to control what part of `c2rust-refactor` gets built. 15 | //#[path="src/analysis/mod.rs"] 16 | #[path="src/transform/retype.rs"] 17 | mod plugin; 18 | //use self::plugin as analysis; 19 | 20 | #[no_mangle] 21 | pub fn register_commands(reg: &mut command::Registry) { 22 | plugin::register_commands(reg); 23 | } 24 | -------------------------------------------------------------------------------- /c2rust-refactor/runtime/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "c2rust_runtime" 3 | version = "0.20.0" 4 | authors = ["Stuart Pernsteiner "] 5 | edition = "2021" 6 | 7 | [dependencies] 8 | libc = "0.2" 9 | 10 | [lib] 11 | name = "c2rust_runtime" 12 | path = "src/lib.rs" 13 | -------------------------------------------------------------------------------- /c2rust-refactor/runtime/src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod array; 2 | pub mod block_ptr; 3 | pub mod util; 4 | 5 | pub use self::array::CArray; 6 | pub use self::block_ptr::CBlockPtr; 7 | -------------------------------------------------------------------------------- /c2rust-refactor/scripts/noop_traversal.lua: -------------------------------------------------------------------------------- 1 | refactor:transform( 2 | function(transform_ctx) 3 | return transform_ctx:visit_fn_like({}) 4 | end 5 | ) 6 | -------------------------------------------------------------------------------- /c2rust-refactor/scripts/test_ownership_upgrade_ptrs.lua: -------------------------------------------------------------------------------- 1 | require "upgrade_ptr_to_ref" 2 | 3 | run_ptr_upgrades() 4 | 5 | refactor:save_crate() 6 | 7 | print("Finished test_ownership_upgrade_ptrs.lua") 8 | 9 | -------------------------------------------------------------------------------- /c2rust-refactor/scripts/utils.lua: -------------------------------------------------------------------------------- 1 | DEBUG = false 2 | 3 | function debug(str) 4 | if DEBUG then 5 | print(str) 6 | end 7 | end 8 | 9 | function starts_with(str, start) 10 | return str:sub(1, #start) == start 11 | end 12 | -------------------------------------------------------------------------------- /c2rust-refactor/src/rewrite/strategy/equal.rs: -------------------------------------------------------------------------------- 1 | use crate::rewrite::RewriteCtxtRef; 2 | 3 | /// World's simplest rewrite strategy: just hope the two ASTs are already equal! Returns success 4 | /// (and does nothing) if they are; returns failure (and still does nothing) if they aren't. 5 | pub fn rewrite>(old: &T, new: &T, _rcx: RewriteCtxtRef) -> bool { 6 | old == new 7 | } 8 | -------------------------------------------------------------------------------- /c2rust-refactor/src/rewrite/strategy/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod equal; 2 | pub mod item_header; 3 | pub mod print; 4 | pub mod recursive; 5 | -------------------------------------------------------------------------------- /c2rust-refactor/src/util/mod.rs: -------------------------------------------------------------------------------- 1 | //! Miscellaneous utility functions. 2 | use smallvec::SmallVec; 3 | 4 | pub mod cursor; 5 | pub mod dataflow; 6 | 7 | /// Move the lone item out of a 1-element container. 8 | pub trait Lone { 9 | fn lone(self) -> T; 10 | } 11 | 12 | impl Lone for T { 13 | fn lone(self) -> T { 14 | self 15 | } 16 | } 17 | 18 | impl Lone for Vec { 19 | fn lone(mut self) -> T { 20 | assert!(self.len() == 1); 21 | self.pop().unwrap() 22 | } 23 | } 24 | 25 | impl Lone for SmallVec<[T; 1]> { 26 | fn lone(mut self) -> T { 27 | assert!(self.len() == 1); 28 | self.pop().unwrap() 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/.gitignore: -------------------------------------------------------------------------------- 1 | old.rs.new 2 | old.rs.new.* 3 | log 4 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/abstract/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = add(1, 2); 3 | let x = add(add(1, 2), 3); 4 | 5 | let x = sub::(1_u8, 2_u8); 6 | let x = sub::(1_u32, 2_u32); 7 | let x = sub::(1_f64, 2_f64); 8 | } 9 | unsafe fn add(x: i32, y: i32) -> i32 { 10 | x + y 11 | } 12 | unsafe fn sub>(x: T, y: T) -> T { 13 | x - y 14 | } 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/abstract/old.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = 1 + 2; 3 | let x = 1 + 2 + 3; 4 | 5 | let x = 1_u8 - 2_u8; 6 | let x = 1_u32 - 2_u32; 7 | let x = 1_f64 - 2_f64; 8 | } 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/abstract/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | abstract 'add(x: i32, y: i32) -> i32' 'x + y' \; \ 10 | abstract 'sub>(x: T, y: T) -> T' \ 11 | 'typed!(x, T) - y' 'x - y' \ 12 | -- old.rs $rustflags 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/autoretype_array/new.rs: -------------------------------------------------------------------------------- 1 | static S: [u8; 2] = [1, 2]; 2 | 3 | fn main() { 4 | // Something to rewrite, to force generation of old.new 5 | 2; 6 | } 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/autoretype_array/old.rs: -------------------------------------------------------------------------------- 1 | static S: [u8; 2] = [1, 2]; 2 | 3 | fn main() { 4 | // Something to rewrite, to force generation of old.new 5 | 1 + 1; 6 | } 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/autoretype_array/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | rewrite_expr '1 + 1' '2' \; \ 10 | autoretype \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/autoretype_method/new.rs: -------------------------------------------------------------------------------- 1 | struct S; 2 | 3 | impl S { 4 | fn f(&self) {} 5 | } 6 | 7 | fn main() { 8 | S.f(); 9 | 10 | // Something to rewrite, to force generation of old.new 11 | 2; 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/autoretype_method/old.rs: -------------------------------------------------------------------------------- 1 | struct S; 2 | 3 | impl S { 4 | fn f(&self) {} 5 | } 6 | 7 | fn main() { 8 | S.f(); 9 | 10 | // Something to rewrite, to force generation of old.new 11 | 1 + 1; 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/autoretype_method/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | rewrite_expr '1 + 1' '2' \; \ 10 | autoretype \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/bitcast_retype/old.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | x: i32, 3 | } 4 | 5 | unsafe fn get_x(s: &S) -> i32 { 6 | s.x 7 | } 8 | 9 | unsafe fn set_x(s: &mut S, x: i32) { 10 | s.x = x; 11 | } 12 | 13 | unsafe fn get_x_addr(s: &S) -> &i32 { 14 | &s.x 15 | } 16 | 17 | unsafe fn get_x_addr_mut(s: &mut S) -> &mut i32 { 18 | &mut s.x 19 | } 20 | 21 | unsafe fn set_x_2(s: &mut S, x: i32) { 22 | *get_x_addr_mut(s) = x; 23 | } 24 | 25 | unsafe fn set_x_3(s: &mut S, x: i32) { 26 | set_x(s, x); 27 | } 28 | 29 | fn main() {} 30 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/bitcast_retype/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | bitcast_retype i32 u32 \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/canonicalize_structs_derive/new.rs: -------------------------------------------------------------------------------- 1 | mod a { 2 | #[derive(Clone, Copy)] 3 | pub struct TheStruct { 4 | pub x: i32, 5 | } 6 | } 7 | 8 | mod b { 9 | struct Before; 10 | 11 | struct After; 12 | } 13 | 14 | fn main() { 15 | let a_struct = crate::a::TheStruct { x: 100 }; 16 | let b_struct = crate::a::TheStruct { x: 100 }; 17 | } 18 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/canonicalize_structs_derive/old.rs: -------------------------------------------------------------------------------- 1 | mod a { 2 | #[derive(Clone, Copy)] 3 | pub struct TheStruct { 4 | pub x: i32, 5 | } 6 | } 7 | 8 | mod b { 9 | struct Before; 10 | 11 | #[derive(Clone, Copy)] 12 | pub struct TheStruct { 13 | pub x: i32, 14 | } 15 | 16 | struct After; 17 | } 18 | 19 | fn main() { 20 | let a_struct = crate::a::TheStruct { x: 100 }; 21 | let b_struct = crate::b::TheStruct { x: 100 }; 22 | } 23 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/canonicalize_structs_derive/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'item(a::TheStruct);' \; print_spans \; canonicalize_structs \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/change_ast_kinds/new.rs: -------------------------------------------------------------------------------- 1 | fn foo(a: i32) -> i32 { 2 | let b = 2 + 1; 3 | let c = a + 3; 4 | 5 | return c * 10; 6 | } 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/change_ast_kinds/old.rs: -------------------------------------------------------------------------------- 1 | fn foo(a: i32) -> i32 { 2 | let b = 1 + 2; 3 | let c = 3 + a; 4 | 5 | return c; 6 | } 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/change_ast_kinds/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor script ../../scripts/change_ast_kinds.lua -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/cleanup_params_locals/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor script ../../scripts/cleanup_params_locals.lua -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/collapse_cfg/new.rs: -------------------------------------------------------------------------------- 1 | // Test restoration of #[cfg] during macro collapsing. 2 | 3 | #[cfg(all())] 4 | mod a {} 5 | 6 | #[cfg(any())] 7 | mod b {} 8 | 9 | fn main() { 10 | // Need something to rewrite, otherwise `-r alongside` won't actually produce the new file. 11 | let x = 1 + 1; 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/collapse_cfg/old.rs: -------------------------------------------------------------------------------- 1 | // Test restoration of #[cfg] during macro collapsing. 2 | 3 | #[cfg(all())] 4 | mod a {} 5 | 6 | #[cfg(any())] 7 | mod b {} 8 | 9 | fn main() { 10 | // Need something to rewrite, otherwise `-r alongside` won't actually produce the new file. 11 | let x = 2; 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/collapse_cfg/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | test_one_plus_one \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/collapse_cfg_attr/new.rs: -------------------------------------------------------------------------------- 1 | // Test restoration of #[cfg_attr] during macro collapsing. Both mods' attrs should be passed 2 | // through unmodified. 3 | 4 | #[cfg_attr(all(), allow(warnings))] 5 | mod a {} 6 | 7 | #[cfg_attr(any(), allow(warnings))] 8 | mod b {} 9 | 10 | fn main() { 11 | // Need something to rewrite, otherwise `-r alongside` won't actually produce the new file. 12 | let x = 1 + 1; 13 | } 14 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/collapse_cfg_attr/old.rs: -------------------------------------------------------------------------------- 1 | // Test restoration of #[cfg_attr] during macro collapsing. Both mods' attrs should be passed 2 | // through unmodified. 3 | 4 | #[cfg_attr(all(), allow(warnings))] 5 | mod a {} 6 | 7 | #[cfg_attr(any(), allow(warnings))] 8 | mod b {} 9 | 10 | fn main() { 11 | // Need something to rewrite, otherwise `-r alongside` won't actually produce the new file. 12 | let x = 2; 13 | } 14 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/collapse_cfg_attr/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | test_one_plus_one \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/convert_format_args/old.rs: -------------------------------------------------------------------------------- 1 | extern "C" { 2 | fn printf(s: &str, ...); 3 | } 4 | 5 | fn main() { 6 | unsafe { 7 | printf("int %d", 1); 8 | printf("char %c", 65); 9 | printf("multi %d %c %x", 65, 65, 65); 10 | 11 | // Needs to be properly implemented still 12 | // printf("star %*d %*.*d %.*d", 1, 2, 3, 4, 5, 6, 7); 13 | 14 | // Used to trigger a bug with macro collapsing, fixed in 3a721469 15 | printf("%c%c%c\x00", 27i32, 16 | '(' as i32, 'B' as i32); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/convert_format_args/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(fn && name("printf"));' \; \ 10 | mark_arg_uses 0 target \; \ 11 | convert_format_args \ 12 | clear_marks \; \ 13 | select target 'crate; child(foreign_mod); last;' \; \ 14 | -- old.rs $rustflags 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_after/new.rs: -------------------------------------------------------------------------------- 1 | mod test { 2 | fn f() {} 3 | fn new() {} 4 | } 5 | 6 | fn main() {} 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_after/old.rs: -------------------------------------------------------------------------------- 1 | mod test { 2 | fn f() {} 3 | } 4 | 5 | fn main() {} 6 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_after/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select dest 'crate; desc(fn && name("f"));' \; \ 10 | create_item 'fn new() {}' after dest \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_inside/new.rs: -------------------------------------------------------------------------------- 1 | mod test { 2 | fn new() {} 3 | fn f() {} 4 | } 5 | 6 | fn main() {} 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_inside/old.rs: -------------------------------------------------------------------------------- 1 | mod test { 2 | fn f() {} 3 | } 4 | 5 | fn main() {} 6 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_inside/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select dest 'crate; desc(mod && name("test"));' \; \ 10 | create_item 'fn new() {}' inside dest \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_preserve_syntax/new.rs: -------------------------------------------------------------------------------- 1 | mod test { 2 | fn before() {} 3 | fn f() { /* syntax preserved */ 4 | } 5 | fn after() {} 6 | } 7 | 8 | fn main() {} 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_preserve_syntax/old.rs: -------------------------------------------------------------------------------- 1 | mod test { 2 | fn before() {} 3 | fn after() {} 4 | } 5 | 6 | fn main() {} 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/create_item_preserve_syntax/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select dest 'crate; desc(fn && name("before"));' \; \ 10 | create_item 'fn f() { /* syntax preserved */ }' after dest \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/expr_parens_1/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor test_one_plus_one -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/expr_parens_2/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor test_f_plus_one -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/fold_let_assign/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | { 3 | let x = 5; 4 | } 5 | 6 | { 7 | let x = 5; 8 | let y = 5; 9 | } 10 | 11 | { 12 | let y = 5; 13 | let x = 5; 14 | } 15 | 16 | { 17 | let mut y = 17; 18 | let x = 5; 19 | } 20 | 21 | { 22 | let mut x; 23 | { 24 | x = 5; 25 | } 26 | x = 10; 27 | } 28 | 29 | { 30 | let x = 5; 31 | let y = 5; 32 | let mut x = ::std::mem::uninitialized(); 33 | x = 10; 34 | } 35 | 36 | { 37 | let mut x = ::std::mem::uninitialized(); 38 | ::std::mem::drop(&mut x); 39 | x = 10; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/fold_let_assign/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor fold_let_assign -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/func_to_method/old.rs: -------------------------------------------------------------------------------- 1 | struct S { x: u32 } 2 | 3 | impl S { 4 | } 5 | 6 | fn by_val(s: S) -> u32 { 7 | s.x 8 | } 9 | 10 | fn by_ref(s: &S) -> u32 { 11 | s.x 12 | } 13 | 14 | fn by_mut_ref(new_x: u32, s: &mut S) { 15 | s.x = new_x; 16 | } 17 | 18 | fn by_ref_with_lt<'a>(s: &'a S) -> &'a u32 { 19 | &s.x 20 | } 21 | 22 | fn static_method(x: i32, y: i32) -> i32 { 23 | x + y 24 | } 25 | 26 | fn main() { 27 | let mut s = S { x: 0 }; 28 | println!("by_ref: {}", by_ref(&s)); 29 | by_mut_ref(1, &mut s); 30 | println!("by_ref_with_lt: {}", by_ref_with_lt(&s)); 31 | println!("by_val: {}", by_val(s)); 32 | println!("static_method: {}", static_method(100, -100)); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/func_to_method/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select dest 'crate; desc(impl);' \; \ 10 | select target 'crate; desc(arg && any_child(match_pat(s)));' \; \ 11 | select target 'crate; desc(fn && name("static_method"));' \; \ 12 | func_to_method -- old.rs $rustflags 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/generalize_item/new.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | x: T, 3 | y: i32, 4 | } 5 | 6 | fn f(x: T, y: i32) {} 7 | 8 | fn g(s: S) { 9 | f::(s.x, s.y); 10 | } 11 | 12 | fn main() {} 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/generalize_item/old.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | x: i16, 3 | y: i32, 4 | } 5 | 6 | fn f(x: i16, y: i32) { 7 | } 8 | 9 | fn g(s: S) { 10 | f(s.x, s.y); 11 | } 12 | 13 | fn main() { 14 | } 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/generalize_item/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(match_ty(i16));' \; \ 10 | select target 'crate; desc(struct && name("S"));' \; \ 11 | select target 'crate; desc(fn && name("f"));' \; \ 12 | generalize_items \ 13 | -- old.rs $rustflags 14 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ionize/old.rs: -------------------------------------------------------------------------------- 1 | union example_union { 2 | field1: u32, 3 | field2: u64, 4 | } 5 | 6 | unsafe fn go(u: example_union) -> u32 { 7 | u.field1 8 | } 9 | 10 | fn main () { } 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/let_x_uninitialized/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = ::std::mem::uninitialized(); 3 | let y: u32 = ::std::mem::uninitialized(); 4 | let z: Option<& /* aeiou */ u32> = ::std::mem::uninitialized(); 5 | let (a, b) = ::std::mem::uninitialized(); 6 | } 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/let_x_uninitialized/old.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x; 3 | let y: u32; 4 | let z: Option<& /* aeiou */ u32>; 5 | let (a, b); 6 | } 7 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/let_x_uninitialized/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor let_x_uninitialized -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_def/new.rs: -------------------------------------------------------------------------------- 1 | fn f() {} 2 | fn f2() {} 3 | 4 | fn g() { 5 | fn f() {} 6 | f(); 7 | } 8 | 9 | fn main() { 10 | crate::f2(); 11 | g(); 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_def/old.rs: -------------------------------------------------------------------------------- 1 | fn f() {} 2 | fn f2() {} 3 | 4 | fn g() { 5 | fn f() {} 6 | f(); 7 | } 8 | 9 | fn main() { 10 | f(); 11 | g(); 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_def/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | rewrite_expr 'def!(crate::f)()' 'crate::f2()' \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_marked/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("{}", 2 + 1 + (3 + 4)); 3 | } 4 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_marked/old.rs: -------------------------------------------------------------------------------- 1 | 2 | fn main() { 3 | println!("{}", (1 + 2) + (3 + 4)); 4 | } 5 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_marked/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(match_expr(1));' \; \ 10 | rewrite_expr 'marked!($e:Expr) + $f:Expr' '$f + $e' \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_typed/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let a = 1u8; 3 | let b = 2u16; 4 | let c = 3u32; 5 | let d = 4u64; 6 | let s = "hello"; 7 | println!("{} {} {} {} {}", a, 1000u16, c, d, s); 8 | } 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_typed/old.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let a = 1u8; 3 | let b = 2u16; 4 | let c = 3u32; 5 | let d = 4u64; 6 | let s = "hello"; 7 | println!("{} {} {} {} {}", a, b, c, d, s); 8 | } 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/matcher_typed/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | rewrite_expr 'typed!($i:Ident, u16)' '1000u16' \; \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/analysis_type_eq_alias.rs: -------------------------------------------------------------------------------- 1 | type A = u32; 2 | 3 | fn f(x: u32) { 4 | g(x); 5 | } 6 | 7 | fn g(x: A) { 8 | } 9 | 10 | fn main() {} 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/mark_uses.rs: -------------------------------------------------------------------------------- 1 | struct Test { 2 | val: u32, 3 | } 4 | 5 | fn f(s: Test) -> u32 { 6 | let Test { val: x } = s; 7 | x 8 | } 9 | 10 | struct Test2(u32); 11 | 12 | fn g(s: Test2) -> u32 { 13 | let Test2(x) = s; 14 | x 15 | } 16 | 17 | fn main() { 18 | println!("{}", f(Test { val: 1234 })); 19 | println!("{}", g(Test2(1234))); 20 | } 21 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/pat_let_x.txt: -------------------------------------------------------------------------------- 1 | let __x; 2 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/pat_while_loop.txt: -------------------------------------------------------------------------------- 1 | let mut __i = __low; 2 | while __i < __high { 3 | __body; 4 | __i += 1; 5 | } 6 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/pat_wrapping_add.txt: -------------------------------------------------------------------------------- 1 | __x.wrapping_add(__y) 2 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/repl_add.txt: -------------------------------------------------------------------------------- 1 | __x + __y 2 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/repl_for_loop.txt: -------------------------------------------------------------------------------- 1 | for __i in __low .. __high { 2 | __body; 3 | } 4 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/repl_let_x_uninit.txt: -------------------------------------------------------------------------------- 1 | let __x = ::std::mem::uninitialized(); 2 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/select_path_prefix.rs: -------------------------------------------------------------------------------- 1 | mod a { 2 | fn f() {} 3 | static S: i32 = 0; 4 | 5 | mod a2 { 6 | fn f() {} 7 | static S: i32 = 0; 8 | } 9 | } 10 | 11 | mod b { 12 | fn f() {} 13 | fn g() {} 14 | } 15 | 16 | mod c { 17 | fn g() {} 18 | } 19 | 20 | fn main() { 21 | } 22 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/src_let_x.txt: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x; 3 | let y; 4 | let z = 12345; 5 | } 6 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/src_while_loop.txt: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut i = 0; 3 | while i < 10 { 4 | println!("{}", i); 5 | i += 1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/src_wrapping_add.txt: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let sum = 1_u8.wrapping_add(1); 3 | println!("1 + 1 = {}", sum); 4 | } 5 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/src_wrapping_add_inside_macro.txt: -------------------------------------------------------------------------------- 1 | 2 | fn f() -> i32 { 3 | 1 4 | } 5 | 6 | fn main() { 7 | 5.wrapping_add({ 8 | println!("1 + 1 = {}", f(/*comment*/).wrapping_add(f())); 9 | 5 10 | }); 11 | } 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/src_wrapping_add_multi.txt: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("1 + 1 = {}", 1.wrapping_add(1)); 3 | println!("1 + 2 + 3 = {}", 1.wrapping_add(2).wrapping_add(3)); 4 | println!("1 + (2 + 3) = {}", 1.wrapping_add(2.wrapping_add(3))); 5 | println!("(1 + 1) * 2 = {}", 1.wrapping_add(1).wrapping_mul(2)); 6 | } 7 | 8 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/misc/src_wrapping_add_println.txt: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = (println!("1 + 1 = {}", 1_u8.wrapping_add(1))); 3 | } 4 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/multi_rewrite/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = 3; 3 | } 4 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/multi_rewrite/old.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = 1; 3 | } 4 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/multi_rewrite/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | # $refactor sets `-r alongside`. This test makes sure that changes are visible 9 | # across `commit`s, even when those changes aren't written to the original file 10 | # (as would be done with `-r inplace`). 11 | $refactor \ 12 | rewrite_expr 1 2 \; commit \; \ 13 | rewrite_expr 2 3 \; commit \; \ 14 | -- old.rs $rustflags 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_constraint_attr/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select box 'crate; 10 | desc(foreign_item && fn && name("malloc|free|realloc")); 11 | desc(match_ty(*mut __t));' \; \ 12 | select ann 'crate; desc(fn || field);' \; \ 13 | ownership_annotate ann \ 14 | -- old.rs $rustflags 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_reinfer/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select box 'crate; 10 | desc(foreign_item && fn && name("malloc|free|realloc")); 11 | desc(match_ty(*mut __t));' \; \ 12 | select ann 'crate; desc(fn || field);' \; \ 13 | ownership_annotate ann \ 14 | -- old.rs $rustflags 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_split/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | $refactor \ 3 | select target 'crate; desc(item && fn);' \; \ 4 | ownership_split_variants \ 5 | -- old.rs $rustflags 6 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_split_part1/old.rs: -------------------------------------------------------------------------------- 1 | //! Testing part 1 of two-part splitting. We split the middle function (`f2`) first, and ensure 2 | //! that the `f1 -> f2` and `f2 -> f3` calls continue to line up. 3 | 4 | unsafe fn f1(p: *mut u8) -> *mut u8 { 5 | f2(p) 6 | } 7 | 8 | unsafe fn f2(p: *mut u8) -> *mut u8 { 9 | f3(p) 10 | } 11 | 12 | unsafe fn f3(p: *mut u8) -> *mut u8 { 13 | g(p) 14 | } 15 | 16 | unsafe fn g(p: *mut u8) -> *mut u8 { 17 | p 18 | } 19 | 20 | fn main() {} 21 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_split_part1/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(fn && name("f2"));' \; \ 10 | ownership_split_variants \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_split_part2/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(fn && (name("f1") || name("f3")));' \; \ 10 | ownership_split_variants \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_upgrade_ptrs/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor script ../../scripts/test_ownership_upgrade_ptrs.lua -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/ownership_upgrade_ptrs/runtime/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "runtime" 3 | version = "0.1.0" 4 | authors = ["Daniel Kolsoi "] 5 | edition = "2021" 6 | 7 | [dependencies] 8 | libc = "0.2" 9 | 10 | [workspace] 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reconstruct_for_range/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let (mut i, mut j, mut k); 3 | 4 | 'a: for i in 0..10 { 5 | println!("{}", i); 6 | } 7 | 'b: for j in (0..10).step_by(2 as usize) { 8 | println!("{}", j); 9 | } 10 | 11 | k = 0; 12 | 'c: while (k < 10) { 13 | k += 2; 14 | println!("{}", k); 15 | k = k + 1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reconstruct_for_range/old.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let (mut i, mut j, mut k); 3 | 4 | i = 0; 5 | 'a: while (i < 10) { 6 | println!("{}", i); 7 | i = i + 1; 8 | } 9 | 10 | j = 0; 11 | 'b: while (j < 10) { 12 | println!("{}", j); 13 | j = j + 2; 14 | } 15 | 16 | k = 0; 17 | 'c: while (k < 10) { 18 | k += 2; 19 | println!("{}", k); 20 | k = k + 1; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reconstruct_for_range/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor reconstruct_for_range -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reconstruct_while/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut i = 0; 3 | 'a: while i < 10 { 4 | println!("{}", i); 5 | i = i + 1; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reconstruct_while/old.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut i = 0; 3 | 'a: loop { 4 | if !(i < 10) { 5 | break; 6 | } 7 | println!("{}", i); 8 | i = i + 1; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reconstruct_while/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor reconstruct_while -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reflect/old.rs: -------------------------------------------------------------------------------- 1 | #![feature(type_ascription)] 2 | 3 | struct S(T); 4 | 5 | impl S<(i32, S>)> { 6 | fn f(self) {} 7 | } 8 | 9 | fn g() {} 10 | 11 | fn main() { 12 | let s = S((0, S(None))); 13 | let x = s.f(); 14 | let f = S::f; 15 | let s = S((1, S(Some(2)))); 16 | 17 | let mut v = Vec::new(); 18 | v.push(123); 19 | let f = Vec::pop; 20 | f(&mut v); 21 | Vec::drain(&mut v, ..); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reflect/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | test_reflect -- old.rs $rustflags 10 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/remove_paren/new.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = (1 + 1 + (1 + 1)); 3 | let x = (1 + 1) + 1; 4 | let x = 1 + (1 + 1); 5 | let x = (1 as u8) + 1; 6 | let x = ((1 + 1) as u8) + 1; 7 | } 8 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/remove_paren/old.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = (2 + 2); 3 | let x = (2) + 1; 4 | let x = 1 + (2); 5 | let x = (1 as u8) + 1; 6 | let x = (2 as u8) + 1; 7 | } 8 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/remove_paren/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | test_one_plus_one \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/remove_unused_labels/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor remove_unused_labels -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/rename_unnamed/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | rename_unnamed \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/reorganize_definitions/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | reorganize_definitions \ 10 | -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_argument/old.rs: -------------------------------------------------------------------------------- 1 | fn f(x: i32, y: i32) -> i32 { 2 | x + y 3 | } 4 | 5 | struct S; 6 | 7 | impl S { 8 | fn g(x: i32, y: i32) -> i32 { 9 | x + y 10 | } 11 | 12 | fn h1(self, x: i32, y: i32) -> i32 { 13 | x + y 14 | } 15 | 16 | fn h2(&self, x: i32, y: i32) -> i32 { 17 | x + y 18 | } 19 | 20 | fn h3(&mut self, x: i32, y: i32) -> i32 { 21 | x + y 22 | } 23 | } 24 | 25 | fn main() { 26 | println!("1 + 1 = {}", f(1, 1)); 27 | println!("1 + 1 = {}", S::g(1, 1)); 28 | println!("1 + 1 = {}", S.h1(1, 1)); 29 | println!("1 + 1 = {}", S.h2(1, 1)); 30 | println!("1 + 1 = {}", S.h3(1, 1)); 31 | } 32 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_argument/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(arg && any_child(match_pat(y)));' \; \ 10 | retype_argument u8 '__old as u8' '__new as i32' -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_return/new.rs: -------------------------------------------------------------------------------- 1 | fn get_char() -> char { 2 | b'!' as char 3 | } 4 | 5 | fn main() { 6 | let c: u8 = get_char() as u8; 7 | println!("{}", c); 8 | } 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_return/old.rs: -------------------------------------------------------------------------------- 1 | fn get_char() -> u8 { 2 | b'!' 3 | } 4 | 5 | fn main() { 6 | let c: u8 = get_char(); 7 | println!("{}", c); 8 | } 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_return/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(fn && name("get_char"));' \; \ 10 | retype_return 'char' '__old as char' '__new as u8' \ 11 | -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_static/new.rs: -------------------------------------------------------------------------------- 1 | static mut S: char = 0 as char; 2 | 3 | fn main() { 4 | unsafe { 5 | let x: u8 = S as u8; 6 | S = x as char; 7 | { 8 | let r: &u8 = &*(&S as *const char as *const u8); 9 | } 10 | { 11 | let r: &mut u8 = &mut *(&mut S as *mut char as *mut u8); 12 | } 13 | println!("S = {}" , *(&S as *const char as *const u8)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_static/old.rs: -------------------------------------------------------------------------------- 1 | static mut S: u8 = 0; 2 | 3 | fn main() { 4 | unsafe { 5 | let x: u8 = S; 6 | S = x; 7 | { 8 | let r: &u8 = &S; 9 | } 10 | { 11 | let r: &mut u8 = &mut S; 12 | } 13 | println!("S = {}", S); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_static/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'item(S);' \; \ 10 | retype_static 'char' '__old as char' '__new as u8' \ 11 | '*(&__new as *const char as *const u8)' \ 12 | '*(&mut __new as *mut char as *mut u8)' \ 13 | -- old.rs $rustflags 14 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/retype_str/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select str 'crate; desc(arg || field || fn); child(ty && match_ty(*const libc::c_char));' \; \ 10 | select mut_str 'crate; desc(arg || field || fn); child(ty && match_ty(*mut libc::c_char));' \; \ 11 | canonicalize_refs \; \ 12 | autoretype 'str: *const u8' 'mut_str: *mut u8' \; \ 13 | remove_unnecessary_refs \ 14 | -- old.rs $rustflags 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/rewrite_arg_list/no-rustfmt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/c2rust-refactor/tests/rewrite_arg_list/no-rustfmt -------------------------------------------------------------------------------- /c2rust-refactor/tests/run-all-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | exit_code=0 3 | for f in */; do 4 | [ -f "$f/run.sh" ] || continue 5 | ./run-test.sh $f >$f/log 2>&1 6 | ret=$? 7 | if [ $ret -eq 0 ]; then 8 | echo "[ OK ] ${f%/}" 9 | else 10 | echo "[FAIL] ${f%/}" 11 | exit_code=$ret 12 | fi 13 | done 14 | exit $exit_code 15 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/set_visibility/new.rs: -------------------------------------------------------------------------------- 1 | pub fn pub1() {} 2 | pub(crate) fn crate1() {} 3 | fn priv1() {} 4 | 5 | pub fn pub2() {} 6 | pub(crate) fn crate2() {} 7 | fn priv2() {} 8 | 9 | pub fn pub3() {} 10 | pub(crate) fn crate3() {} 11 | fn priv3() {} 12 | 13 | fn main() {} 14 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/set_visibility/old.rs: -------------------------------------------------------------------------------- 1 | pub fn pub1() {} 2 | pub fn crate1() {} 3 | pub fn priv1() {} 4 | 5 | pub(crate) fn pub2() {} 6 | pub(crate) fn crate2() {} 7 | pub(crate) fn priv2() {} 8 | 9 | fn pub3() {} 10 | fn crate3() {} 11 | fn priv3() {} 12 | 13 | fn main() {} 14 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/sink_lets/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor sink_lets -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/static_collect_to_struct/new.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | A: u32, 3 | C: u32, 4 | } 5 | static mut AC: S = S { A: 1, C: 1 }; 6 | 7 | #[no_mangle] 8 | static B: u32 = 1; 9 | 10 | fn main() { 11 | println!("{} {} {}", AC.A, B, AC.C); 12 | } 13 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/static_collect_to_struct/old.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | static A: u32 = 1; 3 | #[no_mangle] 4 | static B: u32 = 1; 5 | #[no_mangle] 6 | static C: u32 = 1; 7 | 8 | fn main() { 9 | println!("{} {} {}", A, B, C); 10 | } 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/static_collect_to_struct/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(static && name("A"));' \; \ 10 | select target 'crate; desc(static && name("C"));' \; \ 11 | static_collect_to_struct S AC -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/static_to_local_ref/new.rs: -------------------------------------------------------------------------------- 1 | static S: u32 = 123; 2 | static mut M: u32 = 234; 3 | 4 | unsafe fn f(S_: &u32) { 5 | println!("S = {}", *S_); 6 | } 7 | 8 | unsafe fn g(M_: &mut u32) { 9 | println!("M = {}", *M_); 10 | } 11 | 12 | unsafe fn h(S_: &u32, M_: &mut u32) { 13 | f(S_); 14 | g(M_); 15 | } 16 | 17 | fn main() { 18 | unsafe { 19 | f(&S); 20 | g(&mut M); 21 | h(&S, &mut M); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/static_to_local_ref/old.rs: -------------------------------------------------------------------------------- 1 | static S: u32 = 123; 2 | static mut M: u32 = 234; 3 | 4 | unsafe fn f() { 5 | println!("S = {}", S); 6 | } 7 | 8 | unsafe fn g() { 9 | println!("M = {}", M); 10 | } 11 | 12 | unsafe fn h() { 13 | f(); 14 | g(); 15 | } 16 | 17 | fn main() { 18 | unsafe { 19 | f(); 20 | g(); 21 | h(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/static_to_local_ref/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; child(static);' \; \ 10 | select user 'crate; desc(fn && !name("main"));' \; \ 11 | static_to_local_ref -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/struct_assign_to_update/new.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | x: u32, 3 | y: u32, 4 | } 5 | 6 | fn main() { 7 | let mut s = S { x: 0, y: 0 }; 8 | s = crate::S { x: 1, ..s }; 9 | s = crate::S { y: 2, ..s }; 10 | } 11 | 12 | mod m { 13 | fn g() { 14 | let mut r = 0..10; 15 | r = ::std::ops::Range { start: 2, ..r }; 16 | r = ::std::ops::Range { end: 8, ..r }; 17 | } 18 | } 19 | 20 | /* 21 | // This part of the test case doesn't work yet. The generated code refers to "::f::T" which is not 22 | // a valid path. 23 | fn f() { 24 | struct T { x: u32 } 25 | let mut t = T { x: 0 }; 26 | t.x = 1; 27 | } 28 | */ 29 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/struct_assign_to_update/old.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | x: u32, 3 | y: u32, 4 | } 5 | 6 | fn main() { 7 | let mut s = S { x: 0, y: 0 }; 8 | s.x = 1; 9 | s.y = 2; 10 | } 11 | 12 | mod m { 13 | fn g() { 14 | let mut r = 0 .. 10; 15 | r.start = 2; 16 | r.end = 8; 17 | } 18 | } 19 | 20 | /* 21 | // This part of the test case doesn't work yet. The generated code refers to "::f::T" which is not 22 | // a valid path. 23 | fn f() { 24 | struct T { x: u32 } 25 | let mut t = T { x: 0 }; 26 | t.x = 1; 27 | } 28 | */ 29 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/struct_assign_to_update/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor struct_assign_to_update -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/struct_merge_updates/new.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | x: u32, 3 | y: u32, 4 | z: u32, 5 | } 6 | 7 | fn main() { 8 | let mut s = S { x: 0, y: 0, z: 0 }; 9 | s = S { 10 | x: 1, 11 | y: 2, 12 | z: 3, 13 | ..s 14 | }; 15 | 16 | let mut s2 = S { x: 0, y: 0, z: 0 }; 17 | s2 = S { 18 | x: 1, 19 | y: 2, 20 | x: 1, 21 | ..s2 22 | }; 23 | } 24 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/struct_merge_updates/old.rs: -------------------------------------------------------------------------------- 1 | struct S { 2 | x: u32, 3 | y: u32, 4 | z: u32, 5 | } 6 | 7 | fn main() { 8 | let mut s = S { x: 0, y: 0, z: 0 }; 9 | s = S { x: 1, .. s }; 10 | s = S { y: 2, z: 3, .. s }; 11 | 12 | let mut s2 = S { x: 0, y: 0, z: 0 }; 13 | s2 = S { x: 1, .. s2 }; 14 | s2 = S { y: 2, .. s2 }; 15 | s2 = S { x: 1, .. s2 }; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/struct_merge_updates/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor struct_merge_updates -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/uninit_to_default/new.rs: -------------------------------------------------------------------------------- 1 | use std::mem::{self, uninitialized}; 2 | fn main() { 3 | unsafe { 4 | let x: i32 = 0i32; 5 | let y: char = '\u{0}'; 6 | let z: bool = false; 7 | let x: i32 = 7; 8 | let x: i32; 9 | let x: i32 = f(); 10 | } 11 | } 12 | 13 | fn f() -> i32 { 14 | 17 15 | } 16 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/uninit_to_default/old.rs: -------------------------------------------------------------------------------- 1 | use std::mem::{self, uninitialized}; 2 | fn main() { 3 | unsafe { 4 | let x: i32 = ::std::mem::uninitialized(); 5 | let y: char = mem::uninitialized(); 6 | let z: bool = uninitialized(); 7 | let x: i32 = 7; 8 | let x: i32; 9 | let x: i32 = f(); 10 | } 11 | } 12 | 13 | fn f() -> i32 { 14 | 17 15 | } 16 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/uninit_to_default/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor uninit_to_default -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/upgrade_ptr_to_ref/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor script ../../scripts/test_upgrade_ptr_to_ref.lua -- old.rs $rustflags 9 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/wrap_api/old.rs: -------------------------------------------------------------------------------- 1 | #[no_mangle] 2 | pub unsafe extern "C" fn api() { 3 | } 4 | 5 | #[export_name="api2"] 6 | pub unsafe extern "C" fn api_two(x: i32, y: i32) { 7 | } 8 | 9 | #[no_mangle] 10 | pub unsafe extern "C" fn api3(_: i32, y: i32) { 11 | } 12 | 13 | #[no_mangle] 14 | pub unsafe extern "C" fn api4(x: i32, _: i32) { 15 | } 16 | 17 | #[no_mangle] 18 | pub unsafe extern "C" fn api5(_: i32, _: i32) { 19 | } 20 | 21 | #[no_mangle] 22 | pub unsafe extern "C" fn api6(_: i32, arg0: i32) { 23 | } 24 | 25 | 26 | fn main() { 27 | unsafe { 28 | api(); 29 | api_two(2, 2); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/wrap_api/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; child(fn && name("api.*"));' \; \ 10 | wrap_api -- old.rs $rustflags 11 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/wrap_extern/new.rs: -------------------------------------------------------------------------------- 1 | extern "C" { 2 | fn f(); 3 | fn g(x: u32); 4 | fn h(x: u32, y: u32) -> u32; 5 | } 6 | 7 | mod wrap { 8 | pub unsafe fn f() { 9 | crate::f() 10 | } 11 | pub unsafe fn g(x: u32) { 12 | crate::g(x) 13 | } 14 | pub unsafe fn h(x: u32, y: u32) -> u32 { 15 | crate::h(x, y) 16 | } 17 | } 18 | 19 | fn main() { 20 | unsafe { 21 | crate::wrap::f(); 22 | crate::wrap::g(17); 23 | let x = crate::wrap::h(10, 20); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/wrap_extern/old.rs: -------------------------------------------------------------------------------- 1 | extern "C" { 2 | fn f(); 3 | fn g(x: u32); 4 | fn h(x: u32, y: u32) -> u32; 5 | } 6 | 7 | mod wrap { 8 | } 9 | 10 | fn main() { 11 | unsafe { 12 | f(); 13 | g(17); 14 | let x = h(10, 20); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /c2rust-refactor/tests/wrap_extern/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # work around System Integrity Protection on macOS 4 | if [ `uname` = 'Darwin' ]; then 5 | export LD_LIBRARY_PATH=$not_LD_LIBRARY_PATH 6 | fi 7 | 8 | $refactor \ 9 | select target 'crate; desc(foreign_item && fn);' \; \ 10 | select dest 'crate; desc(mod && name("wrap"));' \; \ 11 | wrap_extern -- old.rs $rustflags 12 | -------------------------------------------------------------------------------- /c2rust-transpile/.gitignore: -------------------------------------------------------------------------------- 1 | /transpiler.iml 2 | /transpiler.ipr 3 | /transpiler.iws 4 | /target* 5 | -------------------------------------------------------------------------------- /c2rust-transpile/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | ../rust-toolchain.toml -------------------------------------------------------------------------------- /c2rust-transpile/src/build_files/build.rs.hbs: -------------------------------------------------------------------------------- 1 | #[cfg(all(unix, not(target_os = "macos")))] 2 | fn main() { 3 | {{#each libraries}} println!("cargo:rustc-link-lib={{{this}}}"); 4 | {{/each}} 5 | // add unix dependencies below 6 | // println!("cargo:rustc-flags=-l readline"); 7 | } 8 | 9 | #[cfg(target_os = "macos")] 10 | fn main() { 11 | {{#each libraries}} println!("cargo:rustc-link-lib={{{this}}}"); 12 | {{/each}} 13 | // add macos dependencies below 14 | // println!("cargo:rustc-flags=-l edit"); 15 | } 16 | -------------------------------------------------------------------------------- /c2rust-transpile/src/build_files/generated-rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly-2022-08-08" 3 | components = ["rustfmt"] 4 | -------------------------------------------------------------------------------- /c2rust-transpile/src/build_files/lib.rs.hbs: -------------------------------------------------------------------------------- 1 | {{#each pragmas~}} 2 | #![{{this.0}}({{this.1}})] 3 | {{/each}} 4 | 5 | {{#each crates~}} 6 | {{!-- TODO(kkysen) `#[macro_use]` shouldn't be needed. --}} 7 | {{!-- Waiting on fix for https://github.com/immunant/c2rust/issues/426. --}} 8 | {{#if this.macro_use~}}#[macro_use]{{~/if}} 9 | extern crate {{this.ident}}; 10 | {{~/each}} 11 | 12 | {{#each modules~}} 13 | {{~#if this.path~}} 14 | #[path = "{{this.path}}"] 15 | {{/if~}} 16 | {{#unless this.close~}} 17 | pub mod {{this.name}} 18 | {{~#if this.open}} { {{~else~}};{{~/if}} 19 | {{else~}} 20 | } // mod {{this.name}} 21 | {{/unless}} 22 | {{/each}} 23 | -------------------------------------------------------------------------------- /c2rust/README.md: -------------------------------------------------------------------------------- 1 | ../README.md -------------------------------------------------------------------------------- /c2rust/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | ../rust-toolchain.toml -------------------------------------------------------------------------------- /default.nix: -------------------------------------------------------------------------------- 1 | let 2 | nixpkgs = import { }; 3 | inherit (nixpkgs) pkgs llvmPackages; 4 | stdenv = pkgs.clangStdenv; 5 | in 6 | stdenv.mkDerivation { 7 | name = "c2rust"; 8 | buildInputs = [ 9 | pkgs.clang 10 | pkgs.cmake 11 | pkgs.llvm 12 | pkgs.libllvm 13 | pkgs.openssl 14 | pkgs.pkgconfig 15 | pkgs.python3 16 | pkgs.rustup 17 | pkgs.zlib 18 | ]; 19 | LIBCLANG_PATH = "${pkgs.libclang.lib}/lib"; 20 | CMAKE_LLVM_DIR = "${llvmPackages.libllvm.dev}/lib/cmake/llvm"; 21 | CMAKE_CLANG_DIR = "${llvmPackages.libclang.dev}/lib/cmake/clang"; 22 | } 23 | -------------------------------------------------------------------------------- /docker/.dockerignore: -------------------------------------------------------------------------------- 1 | https://docs.docker.com/engine/reference/builder/#dockerignore-file 2 | 3 | 4 | *.sh -------------------------------------------------------------------------------- /docker/docker_exec.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | USER=${1:-docker} 4 | 5 | docker exec --user $USER -it c2rust /bin/bash -c "export COLUMNS=`tput cols`; export LINES=`tput lines`; exec bash" 6 | -------------------------------------------------------------------------------- /docs/c2rust-overview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/docs/c2rust-overview.png -------------------------------------------------------------------------------- /dynamic_instrumentation/README.md: -------------------------------------------------------------------------------- 1 | From the repository root: 2 | 3 | ```sh 4 | cargo build 5 | ${CARGO_TARGET_DIR:-target}/debug/c2rust-instrument --metadata analysis/tests/misc/instrument.target/debug/metadata.bc -- build --manifest-path analysis/tests/misc/Cargo.toml 6 | (cd analysis/tests/misc/instrument.target/debug; INSTRUMENT_BACKEND=debug INSTRUMENT_RUNTIME=bg METADATA_FILE=metadata.bc ./c2rust-analysis-tests-misc) 7 | ``` 8 | 9 | This instruments the binary built from main.rs with dynamic memory tracing, and 10 | outputs the necessary metadata to match up instrumentation points to source code 11 | into `metadata.bc`. We then run the binary, printing output to the 12 | debug console and using the aforementioned metadata file. -------------------------------------------------------------------------------- /dynamic_instrumentation/build.rs: -------------------------------------------------------------------------------- 1 | use c2rust_build_paths::SysRoot; 2 | 3 | fn main() { 4 | let sysroot = SysRoot::resolve(); 5 | // Not strictly needed here since this is not used as this is a library crate (it's needed in `c2rust`), 6 | // but it doesn't hurt, and in case this ever adds a binary crate, it's useful. 7 | sysroot.link_rustc_private(); 8 | } 9 | -------------------------------------------------------------------------------- /dynamic_instrumentation/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | ../rust-toolchain.toml -------------------------------------------------------------------------------- /dynamic_instrumentation/src/mir_utils/deref.rs: -------------------------------------------------------------------------------- 1 | use rustc_middle::{ 2 | mir::{Place, PlaceRef, ProjectionElem}, 3 | ty::TyCtxt, 4 | }; 5 | 6 | /// Strip the initital [`Deref`](ProjectionElem::Deref) 7 | /// from a [`projection`](PlaceRef::projection) sequence. 8 | pub fn remove_outer_deref<'tcx>(p: Place<'tcx>, tcx: TyCtxt<'tcx>) -> Place<'tcx> { 9 | // Remove outer deref if present 10 | match p.as_ref() { 11 | PlaceRef { 12 | local, 13 | projection: &[ref base @ .., ProjectionElem::Deref], 14 | } => Place { 15 | local, 16 | projection: tcx.intern_place_elems(base), 17 | }, 18 | _ => p, 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /dynamic_instrumentation/src/util.rs: -------------------------------------------------------------------------------- 1 | /// Like [`From`] and [`Into`], but can't `impl` those because of the orphan rule. 2 | pub trait Convert { 3 | fn convert(self) -> T; 4 | } 5 | -------------------------------------------------------------------------------- /examples/genann/README.md: -------------------------------------------------------------------------------- 1 | # genann (Neural Network Library) 2 | 3 | ## Getting Started 4 | 5 | If the repo submodule appears to be empty or out of date, you may need to run `git submodule update --init path/to/repo`. 6 | 7 | ## Transpiling 8 | 9 | # generate compile_commands.json 10 | $ intercept-build make 11 | $ c2rust transpile compile_commands.json --emit-build-files 12 | 13 | ## Testing 14 | 15 | Instead of translating with `--emit-build-files` to generate a library crate, 16 | you can build with `--binary exampleN` where `N` is one of 1, 3, or 4 17 | (`example2.c` seems to never halt in both C and Rust but translates and executes 18 | just fine). This will create a binary crate that will run the specified example. 19 | -------------------------------------------------------------------------------- /examples/grabc/README.md: -------------------------------------------------------------------------------- 1 | # grabc 2 | 3 | ## Getting Started 4 | 5 | If the repo submodule appears to be empty or out of date, you may need to run `git submodule update --init path/to/repo`. 6 | 7 | ## Transpiling 8 | 9 | The steps to get the transpiled code are as follows: 10 | 11 | $ intercept-build make 12 | $ c2rust transpile compile_commands.json 13 | $ rustc grabc.rs -L/usr/X11R6/lib -lX11 14 | 15 | If you want to have the transpiler create a crate: 16 | 17 | $ intercept-build make 18 | $ c2rust transpile compile_commands.json --emit-build-files -m grabc --output-dir rust 19 | $ cd rust 20 | $ RUSTFLAGS="-L/usr/X11R6/lib -lX11" cargo build 21 | -------------------------------------------------------------------------------- /examples/json-c/configure: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Custom configure script for json-c. Use this in place of ./configure to 4 | # build json-c for translation. 5 | 6 | cd "$(dirname "$0")"/repo 7 | 8 | # Prevent library from using compare-and-swap intrinsics, which c2rust doesn't 9 | # support. 10 | CFLAGS="$CFLAGS -U__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2" 11 | CFLAGS="$CFLAGS -U__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4" 12 | CFLAGS="$CFLAGS -U__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8" 13 | 14 | config_args=( 15 | "CFLAGS=$CFLAGS" 16 | # Always behave as if __thread (thread-local variables) is unavailable. 17 | ac_cv___thread=no 18 | ) 19 | 20 | ./configure "${config_args[@]}" "$@" 21 | -------------------------------------------------------------------------------- /examples/json-c/repo/.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig 2 | # http://EditorConfig.org 3 | 4 | # top-most EditorConfig file 5 | root = true 6 | 7 | # LF end-of-line, insert an empty new line and UTF-8 8 | [*] 9 | end_of_line = lf 10 | insert_final_newline = true 11 | charset = utf-8 12 | 13 | # Tab indentation 14 | [makefile,Makefile] 15 | indent_style = tab 16 | -------------------------------------------------------------------------------- /examples/json-c/repo/.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | compiler: 4 | - gcc 5 | - clang 6 | 7 | addons: 8 | apt: 9 | packages: 10 | - cppcheck 11 | 12 | os: 13 | - linux 14 | - osx 15 | 16 | before_install: 17 | - echo $LANG 18 | - echo $LC_ALL 19 | - set -e 20 | 21 | install: 22 | - sh autogen.sh 23 | 24 | before_script: 25 | - ./configure 26 | 27 | script: 28 | - make 29 | 30 | after_success: 31 | - make check 32 | - if type cppcheck &> /dev/null ; then cppcheck --error-exitcode=1 --quiet *.h *.c tests/ ; fi 33 | -------------------------------------------------------------------------------- /examples/json-c/repo/AUTHORS: -------------------------------------------------------------------------------- 1 | Michael Clark 2 | Jehiah Czebotar 3 | Eric Haszlakiewicz 4 | C. Watford (christopher.watford@gmail.com) 5 | 6 | -------------------------------------------------------------------------------- /examples/json-c/repo/ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/ChangeLog -------------------------------------------------------------------------------- /examples/json-c/repo/INSTALL: -------------------------------------------------------------------------------- 1 | 2 | See README.md for installation instructions. 3 | 4 | -------------------------------------------------------------------------------- /examples/json-c/repo/NEWS: -------------------------------------------------------------------------------- 1 | See the git repo. 2 | -------------------------------------------------------------------------------- /examples/json-c/repo/README: -------------------------------------------------------------------------------- 1 | See README.md or README.html 2 | -------------------------------------------------------------------------------- /examples/json-c/repo/autoconf-archive/README.txt: -------------------------------------------------------------------------------- 1 | 2 | Autoconf Archive fetched from: 3 | 4 | http://gnu.mirror.iweb.com/autoconf-archive/autoconf-archive-2015.09.25.tar.xz 5 | 6 | Grabbed the minimum files needed for the AX_APPEND_COMPILE_FLAGS and 7 | AX_COMPILE_CHECK_SIZEOF macros. 8 | 9 | -------------------------------------------------------------------------------- /examples/json-c/repo/autogen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | autoreconf -v --install || exit 1 3 | 4 | # If there are any options, assume the user wants to run configure. 5 | # To run configure w/o any options, use ./autogen.sh --configure 6 | if [ $# -gt 0 ] ; then 7 | case "$1" in 8 | --conf*) 9 | shift 1 10 | ;; 11 | esac 12 | exec ./configure "$@" 13 | fi 14 | -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/bc_s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/bc_s.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/bdwn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/bdwn.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/closed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/closed.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/doc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/doc.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/doxygen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/doxygen.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/folderclosed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/folderclosed.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/folderopen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/folderopen.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2blank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2blank.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2doc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2doc.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2folderclosed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2folderclosed.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2folderopen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2folderopen.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2lastnode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2lastnode.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2link.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2link.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2mlastnode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2mlastnode.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2mnode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2mnode.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2node.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2node.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2plastnode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2plastnode.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2pnode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2pnode.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2splitbar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2splitbar.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/ftv2vertline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/ftv2vertline.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/nav_f.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/nav_f.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/nav_g.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/nav_g.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/nav_h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/nav_h.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/open.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/splitbar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/splitbar.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/sync_off.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/sync_off.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/sync_on.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/sync_on.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/tab_a.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/tab_a.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/tab_b.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/tab_b.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/tab_h.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/tab_h.png -------------------------------------------------------------------------------- /examples/json-c/repo/doc/html/tab_s.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/examples/json-c/repo/doc/html/tab_s.png -------------------------------------------------------------------------------- /examples/json-c/repo/fuzz/README.md: -------------------------------------------------------------------------------- 1 | # Fuzzers 2 | 3 | This directory contains fuzzers that 4 | target [llvm's LibFuzzer](https://llvm.org/docs/LibFuzzer.html). They are built 5 | and run automatically by 6 | Google's [OSS-Fuzz](https://github.com/google/oss-fuzz/) infrastructure. 7 | -------------------------------------------------------------------------------- /examples/json-c/repo/fuzz/tokener_parse_ex_fuzzer.cc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { 6 | const char *data1 = reinterpret_cast(data); 7 | json_tokener *tok = json_tokener_new(); 8 | json_object *obj = json_tokener_parse_ex(tok, data1, size); 9 | 10 | json_object_put(obj); 11 | json_tokener_free(tok); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /examples/json-c/repo/fuzz/tokener_parse_ex_fuzzer.dict: -------------------------------------------------------------------------------- 1 | "{" 2 | "}" 3 | "," 4 | "[" 5 | "]" 6 | "," 7 | ":" 8 | "e" 9 | "e+" 10 | "e-" 11 | "E" 12 | "E+" 13 | "E-" 14 | "\"" 15 | "null" 16 | "1" 17 | "1.234" 18 | "3e4" 19 | -------------------------------------------------------------------------------- /examples/json-c/repo/json-c-uninstalled.pc.in: -------------------------------------------------------------------------------- 1 | prefix= 2 | exec_prefix= 3 | libdir=@abs_top_builddir@ 4 | includedir=@abs_top_srcdir@ 5 | 6 | Name: json 7 | Description: JSON implementation in C 8 | Version: @VERSION@ 9 | Requires: 10 | Libs: -L@abs_top_builddir@ -ljson-c 11 | Cflags: -I@abs_top_srcdir@ 12 | -------------------------------------------------------------------------------- /examples/json-c/repo/json-c.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@prefix@ 2 | exec_prefix=@exec_prefix@ 3 | libdir=@libdir@ 4 | includedir=@includedir@ 5 | 6 | Name: json-c 7 | Description: A JSON implementation in C 8 | Version: @VERSION@ 9 | Requires: 10 | Libs.private: @LIBS@ 11 | Libs: -L${libdir} -ljson-c 12 | Cflags: -I${includedir}/json-c 13 | -------------------------------------------------------------------------------- /examples/json-c/repo/json_c_version.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 Eric Haszlakiewicz 3 | * 4 | * This library is free software; you can redistribute it and/or modify 5 | * it under the terms of the MIT license. See COPYING for details. 6 | */ 7 | #include "config.h" 8 | 9 | #include "json_c_version.h" 10 | 11 | const char *json_c_version(void) 12 | { 13 | return JSON_C_VERSION; 14 | } 15 | 16 | int json_c_version_num(void) 17 | { 18 | return JSON_C_VERSION_NUM; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /examples/json-c/repo/json_config.h.in: -------------------------------------------------------------------------------- 1 | 2 | /* Define to 1 if you have the header file. */ 3 | #undef JSON_C_HAVE_INTTYPES_H 4 | -------------------------------------------------------------------------------- /examples/json-c/repo/json_config.h.win32: -------------------------------------------------------------------------------- 1 | 2 | /* Define to 1 if you have the header file. */ 3 | #if defined(_MSC_VER) && _MSC_VER >= 1800 4 | #define JSON_C_HAVE_INTTYPES_H 1 5 | #endif 6 | -------------------------------------------------------------------------------- /examples/json-c/repo/json_inttypes.h: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * @file 4 | * @brief Do not use, json-c internal, may be changed or removed at any time. 5 | */ 6 | #ifndef _json_inttypes_h_ 7 | #define _json_inttypes_h_ 8 | 9 | #include "json_config.h" 10 | 11 | #ifdef JSON_C_HAVE_INTTYPES_H 12 | /* inttypes.h includes stdint.h */ 13 | #include 14 | 15 | #else 16 | #include 17 | 18 | #define PRId64 "I64d" 19 | #define SCNd64 "I64d" 20 | 21 | #endif 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /examples/json-c/repo/random_seed.h: -------------------------------------------------------------------------------- 1 | /* 2 | * random_seed.h 3 | * 4 | * Copyright (c) 2013 Metaparadigm Pte. Ltd. 5 | * Michael Clark 6 | * 7 | * This library is free software; you can redistribute it and/or modify 8 | * it under the terms of the MIT license. See COPYING for details. 9 | * 10 | */ 11 | 12 | /** 13 | * @file 14 | * @brief Do not use, json-c internal, may be changed or removed at any time. 15 | */ 16 | #ifndef seed_h 17 | #define seed_h 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | extern int json_c_get_random_seed(); 24 | 25 | #ifdef __cplusplus 26 | } 27 | #endif 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /examples/json-c/repo/rust/src/last_err.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | static char _last_err[256] = ""; 5 | 6 | const char *json_util_get_last_err() 7 | { 8 | if (_last_err[0] == '\0') 9 | return NULL; 10 | return _last_err; 11 | } 12 | 13 | void _json_c_set_last_err(const char *err_fmt, ...) 14 | { 15 | va_list ap; 16 | va_start(ap, err_fmt); 17 | // Ignore (attempted) overruns from snprintf 18 | (void)vsnprintf(_last_err, sizeof(_last_err), err_fmt, ap); 19 | va_end(ap); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /examples/json-c/repo/rust/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature( 2 | extern_types, 3 | libc, 4 | ptr_wrapping_offset_from, 5 | used, 6 | )] 7 | extern crate libc; 8 | 9 | pub mod arraylist; 10 | pub mod debug; 11 | pub mod json_c_version; 12 | pub mod json_object; 13 | pub mod json_object_iterator; 14 | pub mod json_pointer; 15 | pub mod json_tokener; 16 | pub mod json_util; 17 | pub mod json_visit; 18 | pub mod linkhash; 19 | pub mod printbuf; 20 | pub mod random_seed; 21 | pub mod strerror_override; 22 | 23 | mod c_funcs; 24 | -------------------------------------------------------------------------------- /examples/json-c/repo/strdup_compat.h: -------------------------------------------------------------------------------- 1 | #ifndef __strdup_compat_h 2 | #define __strdup_compat_h 3 | 4 | /** 5 | * @file 6 | * @brief Do not use, json-c internal, may be changed or removed at any time. 7 | */ 8 | 9 | #if !defined(HAVE_STRDUP) && defined(_MSC_VER) 10 | /* MSC has the version as _strdup */ 11 | # define strdup _strdup 12 | #elif !defined(HAVE_STRDUP) 13 | # error You do not have strdup on your system. 14 | #endif /* HAVE_STRDUP */ 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /examples/json-c/repo/strerror_override.h: -------------------------------------------------------------------------------- 1 | #ifndef _json_strerror_override_h_ 2 | #define _json_strerror_override_h_ 3 | 4 | /** 5 | * @file 6 | * @brief Do not use, json-c internal, may be changed or removed at any time. 7 | */ 8 | 9 | #include "config.h" 10 | #include 11 | 12 | #include "json_object.h" /* for JSON_EXPORT */ 13 | 14 | #ifdef __cplusplus 15 | extern "C" { 16 | #endif 17 | 18 | #include 19 | 20 | JSON_EXPORT char *_json_c_strerror(int errno_in); 21 | 22 | #ifndef STRERROR_OVERRIDE_IMPL 23 | #define strerror _json_c_strerror 24 | #endif 25 | 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | 30 | #endif /* _json_strerror_override_h_ */ 31 | -------------------------------------------------------------------------------- /examples/json-c/repo/strerror_override_private.h: -------------------------------------------------------------------------------- 1 | #ifndef __json_strerror_override_private_h__ 2 | #define __json_strerror_override_private_h__ 3 | 4 | /** 5 | * @file 6 | * @brief Do not use, json-c internal, may be changed or removed at any time. 7 | */ 8 | 9 | /* Used by tests to get consistent output */ 10 | extern int _json_c_strerror_enable; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/parse_flags.h: -------------------------------------------------------------------------------- 1 | #ifndef __parse_flags_h 2 | #define __parse_flags_h 3 | int parse_flags(int argc, char **argv); 4 | #endif 5 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test1.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Common definitions 4 | if test -z "$srcdir"; then 5 | srcdir="${0%/*}" 6 | test "$srcdir" = "$0" && srcdir=. 7 | test -z "$srcdir" && srcdir=. 8 | fi 9 | . "$srcdir/test-defs.sh" 10 | 11 | run_output_test test1 12 | _err=$? 13 | 14 | for flag in plain spaced pretty ; do 15 | run_output_test -o test1Formatted_${flag} test1Formatted ${flag} 16 | _err2=$? 17 | if [ $_err -eq 0 ] ; then 18 | _err=$_err2 19 | fi 20 | done 21 | 22 | exit $_err 23 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test2.expected: -------------------------------------------------------------------------------- 1 | new_obj.to_string()={ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": [ { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": [ "GML", "XML", "markup" ] } ] } } } 2 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test2.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Common definitions 4 | if test -z "$srcdir"; then 5 | srcdir="${0%/*}" 6 | test "$srcdir" = "$0" && srcdir=. 7 | test -z "$srcdir" && srcdir=. 8 | fi 9 | . "$srcdir/test-defs.sh" 10 | 11 | run_output_test test2 12 | _err=$? 13 | 14 | for flag in plain spaced pretty ; do 15 | run_output_test -o test2Formatted_${flag} test2Formatted ${flag} 16 | _err2=$? 17 | if [ $_err -eq 0 ] ; then 18 | _err=$_err2 19 | fi 20 | done 21 | 22 | exit $_err 23 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test2Formatted_plain.expected: -------------------------------------------------------------------------------- 1 | new_obj.to_string()={"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":[{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML","markup"]}]}}} 2 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test2Formatted_pretty.expected: -------------------------------------------------------------------------------- 1 | new_obj.to_string()={ 2 | "glossary":{ 3 | "title":"example glossary", 4 | "GlossDiv":{ 5 | "title":"S", 6 | "GlossList":[ 7 | { 8 | "ID":"SGML", 9 | "SortAs":"SGML", 10 | "GlossTerm":"Standard Generalized Markup Language", 11 | "Acronym":"SGML", 12 | "Abbrev":"ISO 8879:1986", 13 | "GlossDef":"A meta-markup language, used to create markup languages such as DocBook.", 14 | "GlossSeeAlso":[ 15 | "GML", 16 | "XML", 17 | "markup" 18 | ] 19 | } 20 | ] 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test2Formatted_spaced.expected: -------------------------------------------------------------------------------- 1 | new_obj.to_string()={ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": [ { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": [ "GML", "XML", "markup" ] } ] } } } 2 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test4.expected: -------------------------------------------------------------------------------- 1 | input: "\ud840\udd26,\ud840\udd27,\ud800\udd26,\ud800\udd27" 2 | JSON parse result is correct: 𠄦,𠄧,𐄦,𐄧 3 | PASS 4 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test4.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/testReplaceExisting.expected: -------------------------------------------------------------------------------- 1 | ==== delete-in-loop test starting ==== 2 | Key at index 0 is [foo1] 0 (kept) 3 | Key at index 1 is [foo2] 0 (kept) 4 | Key at index 2 is [deleteme] 0 (deleted) 5 | Key at index 3 is [foo3] 0 (kept) 6 | ==== replace-value first loop starting ==== 7 | Key at index 0 is [foo1] 0 8 | Key at index 1 is [foo2] 0 9 | replacing value for key [foo2] 10 | Key at index 2 is [foo3] 0 11 | ==== second loop starting ==== 12 | Key at index 0 is [foo1] 0 13 | Key at index 1 is [foo2] 0 14 | pointer for key [foo2] does match 15 | Key at index 2 is [foo3] 0 16 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/testReplaceExisting.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_basic.test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Common definitions 4 | if test -z "$srcdir"; then 5 | srcdir="${0%/*}" 6 | test "$srcdir" = "$0" && srcdir=. 7 | test -z "$srcdir" && srcdir=. 8 | fi 9 | . "$srcdir/test-defs.sh" 10 | 11 | filename=$(basename "$0") 12 | filename="${filename%.*}" 13 | 14 | # This is only for the test_util_file.test ; 15 | # more stuff could be extended 16 | cp -f "$srcdir/valid.json" . 17 | 18 | run_output_test $filename "$srcdir" 19 | exit $? 20 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_cast.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_charcase.expected: -------------------------------------------------------------------------------- 1 | OK 2 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_charcase.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_compare.expected: -------------------------------------------------------------------------------- 1 | JSON integer comparision is correct 2 | JSON same object comparision is correct 3 | JSON same integer comparision is correct 4 | Comparing equal strings is correct 5 | Comparing different strings is correct 6 | Comparing equal doubles is correct 7 | Comparing different doubles is correct 8 | Comparing equal arrays is correct 9 | Comparing arrays of different len is correct 10 | Comparing different arrays is correct 11 | Comparing different arrays (one empty) is correct 12 | Comparing JSON object with different key order is correct 13 | Comparing different objects is correct 14 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_compare.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_deep_copy.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_double_serializer.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_float.expected: -------------------------------------------------------------------------------- 1 | json = 1.0 2 | json = 1.23 3 | json = 123456789.0 4 | json = 123456789.123 5 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_float.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_int_add.expected: -------------------------------------------------------------------------------- 1 | INT ADD PASSED 2 | INT ADD OVERFLOW PASSED 3 | INT ADD UNDERFLOW PASSED 4 | INT64 ADD PASSED 5 | INT64 ADD OVERFLOW PASSED 6 | INT64 ADD UNDERFLOW PASSED 7 | PASSED 8 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_int_add.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_json_pointer.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_locale.expected: -------------------------------------------------------------------------------- 1 | new_obj.to_string()=[1.20,3.40,123456.78,5.00,23000000000.00] 2 | new_obj.to_string()=[1.2,3.4,123456.78,5.0,2.3e10] 3 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_locale.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_null.expected: -------------------------------------------------------------------------------- 1 | JSON write result is correct: " \u0000 " 2 | PASS 3 | Re-parsed object string len=3, chars=[32, 0, 32] 4 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_null.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_parse.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_parse_int64.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_printbuf.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_set_serializer.expected: -------------------------------------------------------------------------------- 1 | Test setting, then resetting a custom serializer: 2 | my_object.to_string(standard)={ "abc": 12, "foo": "bar" } 3 | my_object.to_string(custom serializer)=Custom Output 4 | Next line of output should be from the custom freeit function: 5 | freeit, value=123 6 | my_object.to_string(standard)={ "abc": 12, "foo": "bar" } 7 | Check that the custom serializer isn't free'd until the last json_object_put: 8 | my_object.to_string(custom serializer)=Custom Output 9 | Next line of output should be from the custom freeit function: 10 | freeit, value=123 11 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_set_serializer.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_set_value.expected: -------------------------------------------------------------------------------- 1 | INT PASSED 2 | INT64 PASSED 3 | BOOL PASSED 4 | DOUBLE PASSED 5 | STRING PASSED 6 | PASSED 7 | -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_set_value.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_util_file.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/test_visit.test: -------------------------------------------------------------------------------- 1 | test_basic.test -------------------------------------------------------------------------------- /examples/json-c/repo/tests/valid.json: -------------------------------------------------------------------------------- 1 | {"foo":123} 2 | -------------------------------------------------------------------------------- /examples/libxml2/common.py: -------------------------------------------------------------------------------- 1 | ../../scripts/common.py -------------------------------------------------------------------------------- /examples/lil/README.md: -------------------------------------------------------------------------------- 1 | # lil (Little Interpreted Language) 2 | 3 | ## Getting Started 4 | 5 | If the repo submodule appears to be empty or out of date, you may need to run `git submodule update --init path/to/repo`. 6 | 7 | ## Transpiling 8 | 9 | $ intercept-build make 10 | $ c2rust transpile compile_commands.json --emit-build-files -b main --output-dir rust 11 | $ cd rust 12 | $ cargo build 13 | -------------------------------------------------------------------------------- /examples/qsort/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required (VERSION 3.9.0) 2 | project (QSORT) 3 | add_library (Qsort qsort.c) 4 | -------------------------------------------------------------------------------- /examples/qsort/README.md: -------------------------------------------------------------------------------- 1 | # qsort 2 | 3 | This tiny project provides an example of how to use 4 | CMake to build a C project and to generate the clang 5 | "compile_commands.json" file which is used by tools 6 | like the c2rust-ast-exporter. 7 | 8 | Build with the following commands: 9 | 10 | $ mkdir ../build 11 | $ cd ../build 12 | $ cmake ../qsort -DCMAKE_EXPORT_COMPILE_COMMANDS=1 13 | $ cmake --build . 14 | $ c2rust transpile compile_commands.json 15 | -------------------------------------------------------------------------------- /examples/qsort/qsort.c: -------------------------------------------------------------------------------- 1 | void swap(int* a, int* b) 2 | { 3 | int t = *a; 4 | *a = *b; 5 | *b = t; 6 | } 7 | 8 | int partition (int arr[], int low, int high) 9 | { 10 | int pivot = arr[high]; 11 | int i = low - 1; 12 | 13 | for (int j = low; j <= high - 1; j++) { 14 | if (arr[j] <= pivot) { 15 | i++; 16 | swap(&arr[i], &arr[j]); 17 | } 18 | } 19 | swap(&arr[i + 1], &arr[high]); 20 | return i + 1; 21 | } 22 | 23 | void quickSort(int arr[], int low, int high) 24 | { 25 | if (low < high) { 26 | int i = partition(arr, low, high); 27 | quickSort(arr, low, i - 1); 28 | quickSort(arr, i + 1, high); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /examples/robotfindskitten/.gitignore: -------------------------------------------------------------------------------- 1 | /rfk-gcc 2 | /rfk-rust 3 | -------------------------------------------------------------------------------- /examples/robotfindskitten/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | cflags='-Isrc -DHAVE_CONFIG_H' 5 | rustc=~/install/rust/build/x86_64-unknown-linux-gnu/stage2/bin/rustc 6 | libs='-ldl -lrt -lpthread -lgcc_s -lc -lm -lutil -lncurses' 7 | ldflags="-Wl,--no-as-needed -z now $libs" 8 | 9 | gcc -o rfk-gcc src/robotfindskitten.c $cflags $ldflags 10 | $rustc -Z print-link-args -o rfk-rust src/robotfindskitten.rs -C link-args="$ldflags" 11 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/.gitignore: -------------------------------------------------------------------------------- 1 | /config.h 2 | /config.status 3 | /configure.lineno 4 | /libtool 5 | /stamp-h1 6 | 7 | Makefile 8 | .deps 9 | *.o 10 | /src/robotfindskitten 11 | /rust/src/ 12 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/AUTHORS: -------------------------------------------------------------------------------- 1 | I (leonardr@segfault.org) wrote this program. 2 | 3 | Pete Peterson (pedro@zork.net) supplied about [many] non-kitten messages. 4 | 5 | Frederick Lee (phaethon@csua.ucla.edu) showed me how to get my beloved 6 | IBM character set under Linux. 7 | 8 | Some ncurses initialization code stolen from "Writing Programs with 9 | NCURSES", by Eric S. Raymond and Zeyd M. Ben-Halim. I don't know if 10 | there's an official URL, but it's only a Google search away. 11 | 12 | Nick Moffitt (nick@zork.net) and George Moffitt (zen@zork.net) 13 | refactored the code and added new key commands as well as non-kitten 14 | messages. 15 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/Makefile.am: -------------------------------------------------------------------------------- 1 | SUBDIRS=src doc 2 | EXTRA_DIST=BUGS 3 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/NEWS: -------------------------------------------------------------------------------- 1 | ** EXTRA EXTRA READ ALL ABOUT IT *** 2 | 3 | Robot Finds Kitten! 4 | 5 | San Francisco, 2000 -- In a move that shocked the kitten-location industry, a 6 | group of hackers in the monastery beneath the sea have constructed a zen 7 | simulation that enables users with the premier kitten-finding robot technology. 8 | 9 | "We feel that this is a great boon to those who are in danger of losing their 10 | kittens, and wish to practice in the use of automated kitten-finding 11 | equipment," said Leonard Richardson, chief kitten-finder to Zen master Ozdoba. 12 | "We expect to be profitable by the third quarter of 2001." 13 | 14 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/README: -------------------------------------------------------------------------------- 1 | IMPORTANT! 2 | robotfindskitten will NOT compile properly without ncurses headers 3 | (libncursesN-dev in Debian GNU/Linux). Eventually ./configure will warn 4 | you if you do not have it installed. 5 | 6 | To compile, make sure you have the ncurses libraries installed, and 7 | simply type "./configure" and then "make". 8 | 9 | To install, do the preceeding and then type "make install". 10 | 11 | The program is called "robotfindskitten", and the documentation can be 12 | viewed by typing "info robotfindskitten" or "tkinfo robotfindskitten". 13 | The robotfindskitten binary is installed in $(prefix)/games 14 | (/usr/local/games by default on most systems). 15 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/doc/Makefile.am: -------------------------------------------------------------------------------- 1 | info_TEXINFOS=robotfindskitten.texi 2 | man_MANS=robotfindskitten.6 3 | EXTRA_DIST=$(man_MANS) 4 | 5 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/rust/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "robotfindskitten" 3 | version = "0.1.0" 4 | authors = ["Stuart Pernsteiner "] 5 | links = "ncurses" 6 | 7 | [[bin]] 8 | name = "robotfindskitten" 9 | path = "src/robotfindskitten.rs" 10 | 11 | [dependencies] 12 | c2rust_runtime = { path = "../../../../c2rust-refactor/runtime", version = "0.20.0" } 13 | rand = "0.5.5" 14 | pancurses = "0.16.0" 15 | libc = "0.2" 16 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/rust/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("cargo:rustc-link-lib=ncurses"); 3 | } 4 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/rust/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly-2022-02-14" 3 | -------------------------------------------------------------------------------- /examples/robotfindskitten/repo/src/Makefile.am: -------------------------------------------------------------------------------- 1 | execgamesdir= $(prefix)/games 2 | execgames_PROGRAMS=robotfindskitten 3 | 4 | robotfindskitten_SOURCES= draw.h messages.h robotfindskitten.c 5 | #robotfindskitten_LDADD= -lncurses 6 | 7 | -------------------------------------------------------------------------------- /examples/snudown/common.py: -------------------------------------------------------------------------------- 1 | ../../scripts/common.py -------------------------------------------------------------------------------- /examples/snudown/snudownrust.rs: -------------------------------------------------------------------------------- 1 | // TODO(kkysen) `extern crate`s shouldn't be needed, but not sure how to test this 2 | #[cfg(feature="cross-check")] 3 | extern crate c2rust_xcheck_derive; 4 | #[cfg(feature="cross-check")] 5 | extern crate c2rust_xcheck_runtime; 6 | extern crate autolink; 7 | extern crate buffer; 8 | extern crate markdown; 9 | extern crate stack; 10 | -------------------------------------------------------------------------------- /examples/tinycc/common.py: -------------------------------------------------------------------------------- 1 | ../../scripts/common.py -------------------------------------------------------------------------------- /examples/tmux/common.py: -------------------------------------------------------------------------------- 1 | ../../scripts/common.py -------------------------------------------------------------------------------- /examples/urlparser/README.md: -------------------------------------------------------------------------------- 1 | # url parser 2 | 3 | ## Getting Started 4 | 5 | If the repo submodule appears to be empty or out of date, you may need to run `git submodule update --init path/to/repo`. 6 | 7 | ## Transpiling 8 | 9 | $ intercept-build make 10 | $ c2rust transpile compile_commands.json 11 | $ rustc test.rs 12 | -------------------------------------------------------------------------------- /manual/README.md: -------------------------------------------------------------------------------- 1 | ../README.md -------------------------------------------------------------------------------- /manual/c2rust-bitfields/README.md: -------------------------------------------------------------------------------- 1 | ../../c2rust-bitfields/README.md -------------------------------------------------------------------------------- /manual/c2rust-refactor/README.md: -------------------------------------------------------------------------------- 1 | ../../c2rust-refactor/README.md -------------------------------------------------------------------------------- /manual/c2rust-refactor/commands.md: -------------------------------------------------------------------------------- 1 | {{#generate refactor_commands}} 2 | -------------------------------------------------------------------------------- /manual/c2rust-refactor/doc/ldoc.css: -------------------------------------------------------------------------------- 1 | ../../../c2rust-refactor/doc/ldoc.css -------------------------------------------------------------------------------- /manual/c2rust-refactor/doc/scripting_api.html: -------------------------------------------------------------------------------- 1 | ../../../c2rust-refactor/doc/scripting_api.html -------------------------------------------------------------------------------- /manual/c2rust-refactor/rewrite.md: -------------------------------------------------------------------------------- 1 | {{#generate literate 2 | c2rust-refactor/doc/rewrite.md}} 3 | -------------------------------------------------------------------------------- /manual/c2rust-refactor/select.md: -------------------------------------------------------------------------------- 1 | {{#generate literate 2 | c2rust-refactor/doc/select.md}} 3 | -------------------------------------------------------------------------------- /manual/c2rust-refactor/src/analysis/ownership/README.md: -------------------------------------------------------------------------------- 1 | ../../../../../c2rust-refactor/src/analysis/ownership/README.md -------------------------------------------------------------------------------- /manual/c2rust-transpile/README.md: -------------------------------------------------------------------------------- 1 | ../../c2rust-transpile/README.md -------------------------------------------------------------------------------- /manual/docker/README.md: -------------------------------------------------------------------------------- 1 | ../../docker/README.md -------------------------------------------------------------------------------- /manual/docs/README-developers.md: -------------------------------------------------------------------------------- 1 | ../../docs/README-developers.md -------------------------------------------------------------------------------- /manual/docs/build_manual.md: -------------------------------------------------------------------------------- 1 | ../../docs/build_manual.md -------------------------------------------------------------------------------- /manual/docs/c2rust-overview.png: -------------------------------------------------------------------------------- 1 | ../../docs/c2rust-overview.png -------------------------------------------------------------------------------- /manual/docs/known-limitations.md: -------------------------------------------------------------------------------- 1 | ../../docs/known-limitations.md -------------------------------------------------------------------------------- /manual/docs/source_walkthrough.md: -------------------------------------------------------------------------------- 1 | ../../docs/source_walkthrough.md -------------------------------------------------------------------------------- /manual/examples/README.md: -------------------------------------------------------------------------------- 1 | ../../examples/README.md -------------------------------------------------------------------------------- /manual/examples/genann/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/genann/README.md -------------------------------------------------------------------------------- /manual/examples/grabc/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/grabc/README.md -------------------------------------------------------------------------------- /manual/examples/json-c/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/json-c/README.md -------------------------------------------------------------------------------- /manual/examples/json-c/repo/README.md: -------------------------------------------------------------------------------- 1 | ../../../../examples/json-c/repo/README.md -------------------------------------------------------------------------------- /manual/examples/json-c/repo/fuzz/README.md: -------------------------------------------------------------------------------- 1 | ../../../../../examples/json-c/repo/fuzz/README.md -------------------------------------------------------------------------------- /manual/examples/json-c/repo/issues_closed_for_0.13.md: -------------------------------------------------------------------------------- 1 | ../../../../examples/json-c/repo/issues_closed_for_0.13.md -------------------------------------------------------------------------------- /manual/examples/libxml2/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/libxml2/README.md -------------------------------------------------------------------------------- /manual/examples/lil/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/lil/README.md -------------------------------------------------------------------------------- /manual/examples/qsort/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/qsort/README.md -------------------------------------------------------------------------------- /manual/examples/robotfindskitten/README.md: -------------------------------------------------------------------------------- 1 | {{#generate translate_example robotfindskitten}} 2 | {{#generate literate 3 | --project-dir examples/robotfindskitten/repo/rust 4 | examples/robotfindskitten/refactor.md}} 5 | -------------------------------------------------------------------------------- /manual/examples/snudown/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/snudown/README.md -------------------------------------------------------------------------------- /manual/examples/tmux/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/tmux/README.md -------------------------------------------------------------------------------- /manual/examples/urlparser/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/urlparser/README.md -------------------------------------------------------------------------------- /manual/examples/xzoom/README.md: -------------------------------------------------------------------------------- 1 | ../../../examples/xzoom/README.md -------------------------------------------------------------------------------- /manual/installation.md: -------------------------------------------------------------------------------- 1 | {{#include README.md:installation}} 2 | -------------------------------------------------------------------------------- /manual/manual/README.md: -------------------------------------------------------------------------------- 1 | ../README.md -------------------------------------------------------------------------------- /manual/manual/SUMMARY.md: -------------------------------------------------------------------------------- 1 | ../SUMMARY.md -------------------------------------------------------------------------------- /manual/quickstart.md: -------------------------------------------------------------------------------- 1 | {{#include README.md:translating-c-to-rust}} 2 | -------------------------------------------------------------------------------- /manual/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | ../rust-toolchain.toml -------------------------------------------------------------------------------- /manual/scripts/requirements.txt: -------------------------------------------------------------------------------- 1 | ../../scripts/requirements.txt -------------------------------------------------------------------------------- /manual/tests/README.md: -------------------------------------------------------------------------------- 1 | ../../tests/README.md -------------------------------------------------------------------------------- /pdg/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | use c2rust_build_paths::SysRoot; 4 | 5 | fn main() { 6 | let sysroot = SysRoot::resolve(); 7 | sysroot.link_rustc_private(); 8 | 9 | let profile = env::var("PROFILE").expect("`cargo` should set `$PROFILE`"); 10 | println!("cargo:rustc-env=PROFILE={profile}"); 11 | } 12 | -------------------------------------------------------------------------------- /pdg/src/lib.rs: -------------------------------------------------------------------------------- 1 | #![feature(min_specialization)] 2 | #![feature(rustc_private)] 3 | #![feature(map_try_insert)] 4 | 5 | extern crate rustc_ast; 6 | extern crate rustc_const_eval; 7 | extern crate rustc_data_structures; 8 | extern crate rustc_driver; 9 | extern crate rustc_hir; 10 | extern crate rustc_index; 11 | extern crate rustc_interface; 12 | extern crate rustc_middle; 13 | extern crate rustc_mir_build; 14 | extern crate rustc_mir_transform; 15 | extern crate rustc_serialize; 16 | extern crate rustc_session; 17 | extern crate rustc_span; 18 | extern crate rustc_target; 19 | 20 | pub mod assert; 21 | pub mod builder; 22 | pub mod graph; 23 | pub mod info; 24 | pub mod query; 25 | pub mod util; 26 | -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly-2022-08-08" 3 | components = ["rustfmt", "rustc-dev", "rust-src", "miri", "rust-analyzer"] 4 | -------------------------------------------------------------------------------- /scripts/.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = W504, E501 3 | -------------------------------------------------------------------------------- /scripts/cc-wrappers/cc: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import common 4 | import sys 5 | 6 | if __name__ == '__main__': 7 | sys.exit(common.run(build_type="cc")) 8 | -------------------------------------------------------------------------------- /scripts/cc-wrappers/clang: -------------------------------------------------------------------------------- 1 | cc -------------------------------------------------------------------------------- /scripts/cc-wrappers/gcc: -------------------------------------------------------------------------------- 1 | cc -------------------------------------------------------------------------------- /scripts/cc-wrappers/ld: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import common 4 | import sys 5 | 6 | if __name__ == '__main__': 7 | sys.exit(common.run(build_type="ld")) 8 | -------------------------------------------------------------------------------- /scripts/cc-wrappers/ld.bfd: -------------------------------------------------------------------------------- 1 | ld -------------------------------------------------------------------------------- /scripts/cc-wrappers/ld.gold: -------------------------------------------------------------------------------- 1 | ld -------------------------------------------------------------------------------- /scripts/llvm-10.0.1-key.asc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/scripts/llvm-10.0.1-key.asc -------------------------------------------------------------------------------- /scripts/llvm-11.1.0-key.asc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/scripts/llvm-11.1.0-key.asc -------------------------------------------------------------------------------- /scripts/llvm-12.0.0-key.asc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/scripts/llvm-12.0.0-key.asc -------------------------------------------------------------------------------- /scripts/llvm-6.0.1-key.asc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/scripts/llvm-6.0.1-key.asc -------------------------------------------------------------------------------- /scripts/llvm-7.0.1-key.asc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/immunant/c2rust/6df557bd06ffcca3cd3d9339df2289970f5f6f66/scripts/llvm-7.0.1-key.asc -------------------------------------------------------------------------------- /scripts/provision_arch.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SCRIPT_DIR="$(dirname "$0")" 6 | 7 | pacman -Sy 8 | pacman -S --quiet --noconfirm pkgconf make cmake ninja llvm clang python python-pip ncurses git diffutils luarocks libffi strace 9 | 10 | pip3 install --no-cache-dir --disable-pip-version-check -r $SCRIPT_DIR/requirements.txt 11 | 12 | # Set the system-wide Lua path to include luarocks directories 13 | luarocks path > /etc/profile.d/luarocks-path.sh 14 | 15 | # Install penlight lua package with luarocks 16 | luarocks install penlight 17 | -------------------------------------------------------------------------------- /scripts/provision_git.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | git config --global user.name $GIT_USER_NAME 4 | git config --global user.email $GIT_USER_EMAIL 5 | git config --global push.default simple 6 | -------------------------------------------------------------------------------- /scripts/requirements.txt: -------------------------------------------------------------------------------- 1 | bencode-python3 2 | cbor 3 | colorlog 4 | mako 5 | pip 6 | plumbum >= 1.6.3 7 | psutil 8 | pygments 9 | typing;python_version<"3.5" 10 | scan-build 11 | pyyaml 12 | toml 13 | -------------------------------------------------------------------------------- /tests/.gitignore: -------------------------------------------------------------------------------- 1 | # C compilation artifacts 2 | *.o 3 | *.a 4 | compile_commands.json 5 | 6 | # Rust compilation artifacts 7 | Cargo.lock 8 | target/ 9 | 10 | # specific `c2rust` generated Rust files 11 | main.rs 12 | c2rust-lib.rs 13 | 14 | # other `c2rust` generated Rust files 15 | *.rs 16 | # handwritten test Rust files 17 | !test_*.rs 18 | # also checked in already 19 | !build.rs 20 | 21 | # sometimes generated by `c2rust` 22 | rust-toolchain.toml 23 | 24 | # sometimes generated by `c2rust`, but only in `src/` 25 | **/src/Cargo.toml 26 | **/src/build.rs 27 | 28 | # generated for cross tests 29 | **/.cargo/config.toml 30 | -------------------------------------------------------------------------------- /tests/arrays/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "array-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/arrays/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/arrays/src/packed_arrays.c: -------------------------------------------------------------------------------- 1 | /* testcase from https://github.com/immunant/c2rust/issues/381 */ 2 | 3 | typedef struct __attribute__((aligned(4))) { 4 | int waiter; 5 | } event_queue_t; 6 | 7 | void event_queues_init(event_queue_t *queues) 8 | { 9 | queues[0].waiter = 42; 10 | } 11 | -------------------------------------------------------------------------------- /tests/asm.aarch64/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "asm-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | c2rust-asm-casts = { path = "../../c2rust-asm-casts", version = "0.20.0" } 8 | -------------------------------------------------------------------------------- /tests/asm.aarch64/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/asm.aarch64/src/test_asm.rs: -------------------------------------------------------------------------------- 1 | //! extern_crate_c2rust_asm_casts 2 | 3 | use crate::asm::rust_entry; 4 | use std::ffi::{c_int, c_uint}; 5 | 6 | #[link(name = "test")] 7 | extern "C" { 8 | fn entry(_: c_uint, _: *mut c_int); 9 | } 10 | 11 | const BUFFER_SIZE: usize = 6; 12 | 13 | pub fn test_buffer() { 14 | let mut buffer = [0; BUFFER_SIZE]; 15 | let mut rust_buffer = [0; BUFFER_SIZE]; 16 | let expected_buffer = [37, 12, 25, 6, 37, -90]; 17 | 18 | unsafe { 19 | entry(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 20 | rust_entry(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 21 | } 22 | 23 | assert_eq!(buffer, rust_buffer); 24 | assert_eq!(buffer, expected_buffer); 25 | } 26 | -------------------------------------------------------------------------------- /tests/asm.aarch64/target-tuple: -------------------------------------------------------------------------------- 1 | aarch64-unknown-linux-gnu 2 | -------------------------------------------------------------------------------- /tests/asm.arm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "asm-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/asm.arm/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/asm.arm/src/asm.c: -------------------------------------------------------------------------------- 1 | /* test.c */ 2 | typedef unsigned int uint32_t; 3 | typedef unsigned char uint8_t; 4 | 5 | uint32_t __STREXB(uint8_t value, volatile uint8_t *addr) 6 | { 7 | uint32_t result; 8 | 9 | asm volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) ); 10 | return(result); 11 | } 12 | 13 | void entry(const unsigned int buffer_size, int buffer[const]) 14 | { 15 | int i = 0; 16 | 17 | //uint32_t 0xfedcba98 18 | uint8_t dest = 0; 19 | __STREXB(5, &dest); 20 | buffer[i++] = dest; 21 | } 22 | -------------------------------------------------------------------------------- /tests/asm.arm/src/test_asm.rs: -------------------------------------------------------------------------------- 1 | use crate::asm::rust_entry; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn entry(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 1; 10 | 11 | pub fn test_buffer() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [5]; 15 | 16 | unsafe { 17 | entry(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 18 | rust_entry(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | -------------------------------------------------------------------------------- /tests/asm.arm/target-tuple: -------------------------------------------------------------------------------- 1 | arm-unknown-linux-gnueabihf 2 | -------------------------------------------------------------------------------- /tests/asm.x86_64/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "asm-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | c2rust-asm-casts = { path = "../../c2rust-asm-casts", version = "0.20.0" } 8 | -------------------------------------------------------------------------------- /tests/asm.x86_64/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/asm.x86_64/src/test_asm.rs: -------------------------------------------------------------------------------- 1 | //! extern_crate_c2rust_asm_casts 2 | 3 | use crate::asm::rust_entry; 4 | use std::ffi::{c_int, c_uint}; 5 | 6 | #[link(name = "test")] 7 | extern "C" { 8 | fn entry(_: c_uint, _: *mut c_int); 9 | } 10 | 11 | const BUFFER_SIZE: usize = 6; 12 | 13 | pub fn test_buffer() { 14 | let mut buffer = [0; BUFFER_SIZE]; 15 | let mut rust_buffer = [0; BUFFER_SIZE]; 16 | let expected_buffer = [243025, 65070, 51450, 12, 12, 6]; 17 | 18 | unsafe { 19 | entry(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 20 | rust_entry(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 21 | } 22 | 23 | assert_eq!(buffer, rust_buffer); 24 | assert_eq!(buffer, expected_buffer); 25 | } 26 | -------------------------------------------------------------------------------- /tests/asm.x86_64/target-tuple: -------------------------------------------------------------------------------- 1 | x86_64-unknown-linux-gnu 2 | -------------------------------------------------------------------------------- /tests/builtins/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "builtins-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | -------------------------------------------------------------------------------- /tests/builtins/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/builtins/src/math.c: -------------------------------------------------------------------------------- 1 | int ffs(int a) { 2 | return __builtin_ffs(a); 3 | } 4 | 5 | int ffsl(long a) { 6 | return __builtin_ffsl(a); 7 | } 8 | 9 | int ffsll(long long a) { 10 | return __builtin_ffsll(a); 11 | } 12 | 13 | int isfinite(double a) { 14 | return __builtin_isfinite(a); 15 | } 16 | 17 | int isnan(double a) { 18 | return __builtin_isnan(a); 19 | } 20 | 21 | int isinf_sign(double a) { 22 | return __builtin_isinf_sign(a); 23 | } -------------------------------------------------------------------------------- /tests/builtins/src/mem_x_fns.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void mem_x(const char src[4], char dest[4]) { 5 | __builtin_memcpy(dest, src, strlen(src)+1); 6 | __builtin_memchr(dest, 'a', strlen(src)+1); 7 | __builtin_memcmp(dest, src, strlen(src)+1); 8 | __builtin_memmove(dest, src, strlen(src)+1); 9 | __builtin_memset(dest, 'a', 2); 10 | __builtin_strcspn(dest, "a"); 11 | __builtin_strchr(dest, 'a'); 12 | __builtin_strndup(dest, 4); 13 | __builtin_strdup(dest); 14 | } 15 | 16 | void* assume_aligned(void* ptr) { 17 | __builtin_unwind_init(); 18 | return __builtin_assume_aligned(ptr, 8); 19 | } 20 | -------------------------------------------------------------------------------- /tests/casts/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cast-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | -------------------------------------------------------------------------------- /tests/casts/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/casts/src/cast_funptr.c: -------------------------------------------------------------------------------- 1 | #include 2 | int identity(int i) { return i; } 3 | 4 | // dlsym also returns void*, it's not generally safe 5 | // to cast a void pointer to a function pointer but 6 | // I believe that POSIX mandates that you can do it. 7 | void *get_identity(void) { return identity; } 8 | 9 | void entry(const unsigned sz, int buffer[const]) { 10 | typeof(identity) * f = get_identity(); 11 | buffer[0] = f(10); 12 | buffer[1] = (size_t)f; 13 | } 14 | -------------------------------------------------------------------------------- /tests/comments/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "comments-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | -------------------------------------------------------------------------------- /tests/comments/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/comments/src/comments.h: -------------------------------------------------------------------------------- 1 | /* header comment */ 2 | 3 | /* another header comment */ 4 | 5 | /* comment on the decl of test_fn */ 6 | int test_fn(); 7 | 8 | /* a comment on a declaration */ 9 | int a_function(); 10 | 11 | int header_fn2(int x) { 12 | return x; 13 | } 14 | 15 | /* comment on CONSTANT1 */ 16 | #define CONSTANT1 1 17 | 18 | /* comment after CONSTANT1 */ 19 | 20 | /* comment on header_fn */ 21 | int header_fn(int x) { 22 | return x; 23 | } 24 | /* comment after header_fn */ 25 | -------------------------------------------------------------------------------- /tests/comments/src/test_comments.rs: -------------------------------------------------------------------------------- 1 | use crate::comments::{rust_test_fn, CONSTANT, CONSTANT1}; 2 | 3 | pub fn test_comments() { 4 | let val = unsafe { rust_test_fn() }; 5 | assert_eq!(6, val); 6 | } 7 | -------------------------------------------------------------------------------- /tests/conditionals/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "conditional-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/conditionals/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/conditionals/src/binary_conditional.c: -------------------------------------------------------------------------------- 1 | static int id(int i) { return i;} 2 | static int add(int *p, int i, int r) { *p += i; return r;} 3 | 4 | void entry3(const unsigned sz, int buf[const]) 5 | { 6 | buf[0] = id(0) ?: id(1); 7 | buf[1] = id(2) ?: id(3); 8 | 9 | (void) (add(buf+2, 2, 0) ?: add(buf+3, 3, 0)); 10 | (void) (add(buf+4, 4, 1) ?: add(buf+5, 5, 0)); 11 | } 12 | -------------------------------------------------------------------------------- /tests/conditionals/src/conditional.c: -------------------------------------------------------------------------------- 1 | void entry(const unsigned sz, int buf[const]) { 2 | int *x = &buf[0], *y = &buf[1]; 3 | *(0 ? &y : &x) = 10; 4 | 5 | buf[2] = 1 ? 2 : 3; 6 | buf[3] = 0 ? 2 : 3; 7 | } 8 | -------------------------------------------------------------------------------- /tests/conditionals/src/else_if_chain.c: -------------------------------------------------------------------------------- 1 | // should produce else if chain 2 | int entry4(int i) 3 | { 4 | if (i == 0){ 5 | return 0; 6 | }else if (i == 10){ 7 | return 10; 8 | }else if (i == 20){ 9 | return 20; 10 | } 11 | 12 | return -1; 13 | } 14 | -------------------------------------------------------------------------------- /tests/conditionals/src/unused_conditionals.c: -------------------------------------------------------------------------------- 1 | // Regression case for issue #147 where side-effects of unused binary expressions are swallowed up. 2 | 3 | int inc(int *n) { 4 | *n++; 5 | return 1; 6 | } 7 | 8 | int unused_conditional1(void) { 9 | int i = 2; 10 | 0 < inc(&i); 11 | return i; 12 | } 13 | 14 | int unused_conditional2(void) { 15 | int i = 2; 16 | 0 || inc(&i); 17 | return i; 18 | } 19 | 20 | int unused_conditional3(void) { 21 | int i = 2; 22 | 0 && inc(&i); 23 | return i; 24 | } 25 | -------------------------------------------------------------------------------- /tests/enums/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "enum-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/enums/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/enums/src/big_enum.c: -------------------------------------------------------------------------------- 1 | enum E1 { A = -1, B = 3000000000 }; // uses 64-bit 2 | enum E2 { C = 3000000000 }; // uses 32-bit 3 | enum E3 { D = 30000000000 }; // uses 64-bit 4 | void entry5(const unsigned int sz, int buffer[const]) { 5 | // this exercises both the explicitly initialized enum case and implicit one. 6 | enum E1 e1[2] = {1}; 7 | enum E2 e2[2] = {1}; 8 | enum E3 e3[2] = {1}; 9 | 10 | int i = 0; 11 | buffer[i++] = e1[0]; 12 | buffer[i++] = e1[1]; 13 | 14 | buffer[i++] = e2[0]; 15 | buffer[i++] = e2[1]; 16 | 17 | buffer[i++] = e3[0]; 18 | buffer[i++] = e3[1]; 19 | } 20 | -------------------------------------------------------------------------------- /tests/enums/src/enum_compound.c: -------------------------------------------------------------------------------- 1 | 2 | typedef enum { 3 | B = 2 4 | } a; 5 | 6 | void entry6(const unsigned buffer_size, int buffer[]) { 7 | if (buffer_size < 1) { 8 | return; 9 | } 10 | 11 | // Using a compound literal with enum to assign the value 12 | buffer[0] = (a) { B }; 13 | } 14 | -------------------------------------------------------------------------------- /tests/enums/src/enum_duplicate.c: -------------------------------------------------------------------------------- 1 | enum e { 2 | A = 0, 3 | B = 0, 4 | C = -10, 5 | D, 6 | }; 7 | 8 | void entry3(const unsigned int sz, int buffer[const]) 9 | { 10 | buffer[0] = A; 11 | buffer[1] = B; 12 | buffer[2] = C; 13 | buffer[3] = D; 14 | } 15 | -------------------------------------------------------------------------------- /tests/enums/src/enum_fwd_decl.c: -------------------------------------------------------------------------------- 1 | // Unlike struct or union, there are no forward-declared enums in C. However, 2 | // the Enlightenment Foundation Libraries, and possibly other code, contains 3 | // code containing this pattern: 4 | 5 | typedef enum _Evas_Filter_Support Evas_Filter_Support; 6 | 7 | struct _Evas_Func 8 | { 9 | Evas_Filter_Support (*gfx_filter_supports) (void *engine, void *cmd); 10 | }; 11 | 12 | // dummy item imported by `test_enums.rs` 13 | int foo(int i) { return i;} -------------------------------------------------------------------------------- /tests/enums/src/non_canonical_enum_def.c: -------------------------------------------------------------------------------- 1 | // In this case the extern fn creates the canonical enum decl 2 | // and the enum definition is non canonical 3 | 4 | extern enum hrtimer_restart it_real_fn(void); 5 | 6 | enum hrtimer_restart { 7 | HRTIMER_NORESTART, 8 | HRTIMER_RESTART, 9 | }; 10 | 11 | void abc(void) { 12 | it_real_fn(); 13 | } 14 | -------------------------------------------------------------------------------- /tests/enums/src/top_enum.c: -------------------------------------------------------------------------------- 1 | enum E { 2 | A = 0, 3 | B = 1, 4 | }; 5 | 6 | static enum E e = B; 7 | 8 | void entry4(const unsigned buffer_size, int buffer[]) { 9 | buffer[0] = e; 10 | } -------------------------------------------------------------------------------- /tests/example/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "example-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/example/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/example/src/add.c: -------------------------------------------------------------------------------- 1 | unsigned int add(unsigned int left, unsigned int right) { 2 | return left + right; 3 | } 4 | -------------------------------------------------------------------------------- /tests/example/src/skip_me.c: -------------------------------------------------------------------------------- 1 | //! skip_translation 2 | // This file uses a rust style module doc comment (above) to specify config 3 | // for this file, namely that this file will not be translated but ignored: 4 | 5 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam non mauris 6 | laoreet, vestibulum lectus sit amet, tincidunt ipsum. Aliquam feugiat 7 | in turpis sit amet iaculis. 8 | -------------------------------------------------------------------------------- /tests/example/src/sub.c: -------------------------------------------------------------------------------- 1 | unsigned int sub(unsigned int left, unsigned int right) { 2 | return left - right; 3 | } 4 | -------------------------------------------------------------------------------- /tests/example/src/test_add.rs: -------------------------------------------------------------------------------- 1 | use crate::add::rust_add; 2 | use std::ffi::c_uint; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn add(left: c_uint, right: c_uint) -> c_uint; 7 | } 8 | 9 | pub fn test_addition() { 10 | let sum = unsafe { add(1, 2) }; 11 | let rust_sum = unsafe { rust_add(1, 2) }; 12 | 13 | assert_eq!(sum, 3); 14 | assert_eq!(rust_sum, 3); 15 | } 16 | 17 | pub fn test_overflow() { 18 | let max_uint = c_uint::max_value(); 19 | let sum = unsafe { add(max_uint, 3) }; 20 | let rust_sum = unsafe { rust_add(max_uint, 3) }; 21 | 22 | assert_eq!(sum, 2); 23 | assert_eq!(rust_sum, 2); 24 | } 25 | -------------------------------------------------------------------------------- /tests/example/src/test_file_xfail.rs: -------------------------------------------------------------------------------- 1 | //! xfail 2 | // This test file is expected not to compile for whatever reason 3 | 4 | pub fn test_foo() { 5 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam non mauris 6 | laoreet, vestibulum lectus sit amet, tincidunt ipsum. Aliquam feugiat 7 | in turpis sit amet iaculis. 8 | } 9 | -------------------------------------------------------------------------------- /tests/example/src/test_fn_xfail.rs: -------------------------------------------------------------------------------- 1 | // This failure is specific to this test, other passing tests may be present 2 | 3 | // xfail 4 | pub fn test_xfails() { 5 | panic!("Not meant to pass"); 6 | } 7 | -------------------------------------------------------------------------------- /tests/example/src/test_sub.rs: -------------------------------------------------------------------------------- 1 | use crate::sub::rust_sub; 2 | use std::ffi::c_uint; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn sub(left: c_uint, right: c_uint) -> c_uint; 7 | } 8 | 9 | pub fn test_subtraction() { 10 | let diff = unsafe { sub(5, 2) }; 11 | let rust_diff = unsafe { rust_sub(5, 2) }; 12 | 13 | assert_eq!(diff, 3); 14 | assert_eq!(rust_diff, 3); 15 | } 16 | 17 | pub fn test_underflow() { 18 | let max_uint = c_uint::max_value(); 19 | let diff = unsafe { sub(2, 3) }; 20 | let rust_diff = unsafe { rust_sub(2, 3) }; 21 | 22 | assert_eq!(diff, max_uint); 23 | assert_eq!(rust_diff, max_uint); 24 | } 25 | -------------------------------------------------------------------------------- /tests/floats/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "float-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/floats/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/gotos/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "goto-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/gotos/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/gotos/src/duffs.c: -------------------------------------------------------------------------------- 1 | //! allow_current_block 2 | 3 | void copy(int *to, int *from, int count) { 4 | int n = (count + 7) / 8; 5 | switch (count % 8) { 6 | case 0: do { *to++ = *from++; 7 | case 7: *to++ = *from++; 8 | case 6: *to++ = *from++; 9 | case 5: *to++ = *from++; 10 | case 4: *to++ = *from++; 11 | case 3: *to++ = *from++; 12 | case 2: *to++ = *from++; 13 | case 1: *to++ = *from++; 14 | } while (--n > 0); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/gotos/src/early_returns.c: -------------------------------------------------------------------------------- 1 | // This should translate to straightforward one-armed if's 2 | int early_returns(int a) { 3 | if (a == 2) { 4 | return 2; 5 | } 6 | if (a == 3) { 7 | a += 1; 8 | } 9 | if (a == 4) { 10 | return 1; 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /tests/gotos/src/idiomatic_switch.c: -------------------------------------------------------------------------------- 1 | // This should translate to a straightforward `match`. 2 | int idiomatic_switch(int x) { 3 | 4 | switch (1 + x) { 5 | case 0: 6 | case 2: 7 | x += 2; 8 | break; 9 | case 1: 10 | x += 1; 11 | break; 12 | default: 13 | x += 3; 14 | } 15 | 16 | return x; 17 | } 18 | -------------------------------------------------------------------------------- /tests/gotos/src/irreducible.c: -------------------------------------------------------------------------------- 1 | //! allow_current_block 2 | int irreducible(int x) { 3 | 4 | l1: 5 | if (x < 6) { 6 | x += 1; 7 | goto l3; 8 | } 9 | 10 | l2: 11 | if (x < 9) { 12 | x += 2; 13 | goto l1; 14 | } 15 | 16 | l3: 17 | if (x < 20) { 18 | x += 90; 19 | goto l2; 20 | } 21 | 22 | return x; 23 | } 24 | -------------------------------------------------------------------------------- /tests/gotos/src/jump_into_loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // This used to cause the translator to crash at translation time. 4 | // It doesn't do anything, but it does have interesting control-flow. 5 | int jump_into_loop() { 6 | int x = 0; 7 | 8 | while (x < 10) { 9 | if (x > 11) { 10 | foo: 11 | exit(x); 12 | } 13 | } 14 | 15 | x = 1; 16 | 17 | goto foo; 18 | 19 | return 1; 20 | } 21 | -------------------------------------------------------------------------------- /tests/gotos/src/label_break_trigger.c: -------------------------------------------------------------------------------- 1 | // This function doesn't do anything interesting, so we aren't checking its 2 | // inputs or outputs. However, it does have interesting control flow, so 3 | // we do check that we can generate compiling code for it. 4 | void triggers_label_break(void) { 5 | lbl: 6 | if (0 < 1) 7 | return; 8 | else { 9 | if (0 < 1) 10 | goto lbl; 11 | } 12 | return; 13 | } 14 | -------------------------------------------------------------------------------- /tests/gotos/src/lifted_decls.c: -------------------------------------------------------------------------------- 1 | void foo(int a) { 2 | switch (a) { 3 | case 1: { 4 | char buf[3]; 5 | 6 | buf[0] = 'a'; 7 | } 8 | break; 9 | case 2: { 10 | char buf[2]; 11 | 12 | buf[0] = 'a'; 13 | } 14 | break; 15 | default: { 16 | char buf[1]; 17 | 18 | buf[0] = 'a'; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/gotos/src/stmt_expr.c: -------------------------------------------------------------------------------- 1 | int stmt_expr_func(int y) { 2 | 3 | int x = ({ 4 | int z = 9; 5 | 6 | switch (y) { 7 | case 0: z += 3; 8 | case 1: goto lbl; 9 | case 2: return -42; 10 | default: z += 6; 11 | }; 12 | 13 | z += 6; 14 | lbl: ; 15 | 16 | z + (++y); 17 | }); 18 | 19 | return x + y; 20 | } 21 | -------------------------------------------------------------------------------- /tests/gotos/src/test_dfa_multiple_three.rs: -------------------------------------------------------------------------------- 1 | use crate::dfa_binary_multiple_three::rust_multiple_three; 2 | 3 | use std::ffi::CString; 4 | 5 | pub fn test_multiple_three() { 6 | let n1 = CString::new(format!("{:b}", 4529465 * 3 + 0)).unwrap(); 7 | let n2 = CString::new(format!("{:b}", 65424738 * 3 + 1)).unwrap(); 8 | let n3 = CString::new(format!("{:b}", 98078783 * 3 + 2)).unwrap(); 9 | let n4 = CString::new("010100150101010001").unwrap(); 10 | 11 | unsafe { 12 | assert_eq!(rust_multiple_three(n1.as_ptr()), 1); 13 | assert_eq!(rust_multiple_three(n2.as_ptr()), 0); 14 | assert_eq!(rust_multiple_three(n3.as_ptr()), 0); 15 | assert_eq!(rust_multiple_three(n4.as_ptr()), 2); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/gotos/src/test_duffs.rs: -------------------------------------------------------------------------------- 1 | use crate::duffs::rust_copy; 2 | 3 | pub fn test_multiple_three() { 4 | let mut from = [1, 2, 3, 8, 2, 9, 8, 1, 8, 4, 5, 6, 2, 89, 0, 2, 3, 4, 56, 8]; 5 | let mut to = [0; 20]; 6 | 7 | unsafe { 8 | rust_copy(to.as_mut_ptr(), from.as_mut_ptr(), 20); 9 | } 10 | 11 | assert_eq!(from, to); 12 | } 13 | -------------------------------------------------------------------------------- /tests/gotos/src/test_early_returns.rs: -------------------------------------------------------------------------------- 1 | use crate::early_returns::rust_early_returns; 2 | 3 | pub fn test_early_returns() { 4 | unsafe { 5 | assert_eq!(rust_early_returns(2), 2); 6 | assert_eq!(rust_early_returns(3), 1); 7 | assert_eq!(rust_early_returns(4), 1); 8 | assert_eq!(rust_early_returns(5), 0); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /tests/gotos/src/test_irreducible.rs: -------------------------------------------------------------------------------- 1 | use crate::irreducible::rust_irreducible; 2 | use std::ffi::c_int; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn irreducible(_: c_int) -> c_int; 7 | } 8 | 9 | pub fn test_irreducible() { 10 | unsafe { 11 | for i in 0..20 { 12 | assert_eq!(rust_irreducible(i), irreducible(i)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/gotos/src/test_stmt_expr.rs: -------------------------------------------------------------------------------- 1 | use crate::stmt_expr::rust_stmt_expr_func; 2 | 3 | use std::ffi::c_int; 4 | 5 | pub fn test_stmt_expr_relooper() { 6 | unsafe { 7 | assert_eq!(rust_stmt_expr_func(0), 14); 8 | assert_eq!(rust_stmt_expr_func(1), 13); 9 | assert_eq!(rust_stmt_expr_func(2), -42); 10 | assert_eq!(rust_stmt_expr_func(3), 29); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/gotos/src/test_translation_only.rs: -------------------------------------------------------------------------------- 1 | //! feature_label_break_value 2 | 3 | use crate::jump_into_loop::rust_jump_into_loop; 4 | use crate::label_break_trigger::rust_triggers_label_break; 5 | 6 | // This test case just makes sure that the functions imported actually were 7 | // translated. Compilation suffices. 8 | pub fn test_nothing() {} 9 | -------------------------------------------------------------------------------- /tests/ints/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "int-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/ints/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/ints/src/chars.c: -------------------------------------------------------------------------------- 1 | int multibyte_chars(const unsigned sz, int buffer[]) { 2 | int i = 0; 3 | 4 | // Test unicode character literals 5 | buffer[i++] = u'✓'; 6 | buffer[i++] = U'😱'; 7 | buffer[i++] = L'😱'; 8 | 9 | buffer[i++] = '\x00'; 10 | buffer[i++] = '\x01'; 11 | buffer[i++] = '\xff'; 12 | 13 | return i; 14 | } 15 | -------------------------------------------------------------------------------- /tests/ints/src/sieve_of_eratosthenes.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void sieve_of_eratosthenes(int *buffer) { 5 | bool prime[102]; 6 | 7 | memset(prime, true, sizeof(prime)); 8 | for(int p = 2; p*p <= 101; p++) { 9 | if (prime[p]) 10 | for (int i = p * 2; i <= 100; i += p) 11 | prime[i] = false; 12 | } 13 | 14 | for (int p = 2; p <= 101; p++) { 15 | if (prime[p]) 16 | buffer[p] = 1; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/ints/src/size_t.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void entry(unsigned n, int buf[]) { 4 | if (n < 10) return; 5 | 6 | size_t z = 5; 7 | buf[z] = 8; 8 | } 9 | -------------------------------------------------------------------------------- /tests/ints/src/test_const.rs: -------------------------------------------------------------------------------- 1 | use crate::const_test::rust_entry4; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn entry4(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 2; 10 | 11 | pub fn test_const() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [3, 2]; 15 | 16 | unsafe { 17 | entry4(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 18 | rust_entry4(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | -------------------------------------------------------------------------------- /tests/ints/src/test_implicit_ints.rs: -------------------------------------------------------------------------------- 1 | //! xfail 2 | 3 | 4 | use crate::implicit_int::{identity as rust_identity, implicit_int as rust_implicit_int}; 5 | use std::ffi::{c_int, c_uint}; 6 | 7 | extern "C" { 8 | fn identity(_: c_int) -> c_int; 9 | 10 | fn implicit_int(); 11 | } 12 | 13 | pub fn test_identity() { 14 | unsafe { 15 | assert_eq!(identity(1), 1); 16 | assert_eq!(rust_identity(1), 1); 17 | } 18 | } 19 | 20 | 21 | pub fn test_implicit_int() { 22 | unsafe { 23 | implicit_int(); 24 | rust_implicit_int(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/ints/src/test_volatile.rs: -------------------------------------------------------------------------------- 1 | use crate::volatile::rust_entry3; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn entry3(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 9; 10 | 11 | pub fn test_buffer() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [5, 11, 5, 9, 99, 116, 101, 115, 116]; 15 | 16 | unsafe { 17 | entry3(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 18 | rust_entry3(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | -------------------------------------------------------------------------------- /tests/items/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "items-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/items/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/items/src/functions.c: -------------------------------------------------------------------------------- 1 | /* This should translate cleanly with a single extern function definition. */ 2 | void coreutils_static_assert() { 3 | enum { count = 2 }; 4 | 5 | /* Expanded form of verify (count > 1); from coreutils/lib/verify.h 6 | This is a declaration of _gl_verify_function2 as an extern function (void) 7 | returning a pointer to array of size 1 of int */ 8 | extern int (*_gl_verify_function2 (void)) [(!!sizeof (struct { unsigned int _gl_verify_error_if_negative: (count > 1) ? 1 : -1; }))]; 9 | } 10 | 11 | /* Clang doesn't support nested functions */ 12 | /* 13 | void nested() { 14 | void inner_function() { 15 | printf("in inner"); 16 | } 17 | 18 | inner_function(); 19 | } 20 | */ 21 | -------------------------------------------------------------------------------- /tests/items/src/linking.c: -------------------------------------------------------------------------------- 1 | extern int mut = 1; // #[export_name = "mut"] 2 | extern int y = 2; // #[no_mangle] 3 | 4 | extern int l(void) { // #[export_name = "let"] 5 | return 3; 6 | } 7 | extern int w(void) { // #[no_mangle] 8 | return 4; 9 | } 10 | 11 | // Internal linkage 12 | static int crate = 3; // renamed 13 | static int x = 4; // unrenamed 14 | 15 | static void ref(void) { } // renamed 16 | static void z(void) { } // unrenamed 17 | -------------------------------------------------------------------------------- /tests/items/src/nofnargs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Real function to test. Clang handles functions w/o 3 | * arguments specially, which is why we need this test. 4 | * 5 | * The correct definition is "int nofnargs(void)", but 6 | * it's common for people to miss this and fallback to 7 | * previous C behavior. 8 | */ 9 | int nofnargs() { 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /tests/items/src/noop.c: -------------------------------------------------------------------------------- 1 | void noop(void) { 2 | } 3 | -------------------------------------------------------------------------------- /tests/items/src/test_functions.rs: -------------------------------------------------------------------------------- 1 | use crate::functions::rust_coreutils_static_assert; 2 | 3 | #[link(name = "test")] 4 | extern "C" { 5 | fn coreutils_static_assert(); 6 | } 7 | 8 | pub fn test_coreutils_static_assert() { 9 | unsafe { 10 | coreutils_static_assert(); 11 | rust_coreutils_static_assert(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/items/src/test_linking.rs: -------------------------------------------------------------------------------- 1 | use crate::linking::{rust_l, rust_w}; 2 | use std::ffi::c_int; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn l() -> c_int; 7 | 8 | fn w() -> c_int; 9 | } 10 | 11 | pub fn test_linking() { 12 | let mut ret = unsafe { l() }; 13 | 14 | let mut rust_ret = unsafe { rust_l() }; 15 | 16 | assert_eq!(ret, rust_ret); 17 | assert_eq!(ret, 3); 18 | assert_eq!(rust_ret, 3); 19 | 20 | ret = unsafe { w() }; 21 | 22 | rust_ret = unsafe { rust_w() }; 23 | 24 | assert_eq!(ret, rust_ret); 25 | assert_eq!(ret, 4); 26 | assert_eq!(rust_ret, 4); 27 | } 28 | -------------------------------------------------------------------------------- /tests/items/src/test_noop.rs: -------------------------------------------------------------------------------- 1 | use crate::nofnargs::rust_nofnargs; 2 | use crate::noop::rust_noop; 3 | 4 | use std::ffi::c_int; 5 | 6 | #[link(name = "test")] 7 | extern "C" { 8 | fn noop(); 9 | 10 | fn nofnargs() -> c_int; 11 | } 12 | 13 | pub fn test_noop() { 14 | unsafe { 15 | noop(); 16 | rust_noop(); 17 | } 18 | } 19 | 20 | pub fn test_nofnargs() { 21 | let ret = unsafe { nofnargs() }; 22 | let rust_ret = unsafe { rust_nofnargs() }; 23 | 24 | assert_eq!(ret, 0); 25 | assert_eq!(rust_ret, 0); 26 | } 27 | -------------------------------------------------------------------------------- /tests/longdouble/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "long-double-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | f128 = "0.2" 8 | num-traits = "0.2.6" 9 | -------------------------------------------------------------------------------- /tests/longdouble/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/loops/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "loops-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/loops/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/loops/src/goto_linear_cf.c: -------------------------------------------------------------------------------- 1 | void goto_linear(const unsigned int sz, int buffer[const]) { 2 | int i = 0; 3 | buffer[++i] = 1; 4 | goto l1; 5 | 6 | l2: 7 | buffer[++i] = 2; 8 | return; 9 | 10 | l1: 11 | buffer[++i] = 3; 12 | goto l2; 13 | } 14 | -------------------------------------------------------------------------------- /tests/loops/src/goto_loop_cf.c: -------------------------------------------------------------------------------- 1 | void goto_loop(const unsigned int sz, int buffer[const]) { 2 | 3 | int i = 0; 4 | goto l1; 5 | 6 | l2: 7 | buffer[++i] = 2; 8 | goto l1; 9 | 10 | l1: 11 | buffer[++i] = 1; 12 | if (i < 10) 13 | goto l2; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /tests/loops/src/goto_switch_cf.c: -------------------------------------------------------------------------------- 1 | //! allow_current_block 2 | void goto_switch(unsigned buffer_size, int buffer[]) { 3 | 4 | int i = -3; 5 | 6 | l1: 7 | switch (i) { 8 | default: 9 | ++i; 10 | buffer[i+3] = 1; 11 | goto l1; 12 | case 0: 13 | ++i; 14 | buffer[i+3] = 2; 15 | case 4: 16 | case 1: 17 | ++i; 18 | buffer[i+3] = 3; 19 | break; 20 | case 3: 21 | buffer[i+3] = 4; 22 | break; 23 | } 24 | 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /tests/loops/src/nested_goto.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Adapted from gnulib add_exclude_fp 4 | // Regression test for incremental relooper 5 | void test_nested_with_goto (int n, int x, char* buf) { 6 | int i; 7 | for (i = 0; i < n; i++) 8 | if (n == 10) { 9 | if (x < 100) { 10 | for (;; x--) 11 | if (x == i) 12 | goto next; 13 | else if (x == 0) 14 | break; 15 | } 16 | 17 | printf("didn't hit goto"); 18 | next: 19 | printf("after label"); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/loops/src/switch.c: -------------------------------------------------------------------------------- 1 | // This code does not translate properly without the relooper 2 | int switch_val(int val) { 3 | switch (val) { 4 | case 1: 5 | return 2; 6 | case 2: 7 | return 4; 8 | default: 9 | return val + 1; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/macros/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "macros-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | -------------------------------------------------------------------------------- /tests/macros/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/misc/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "misc-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/misc/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/misc/src/exprs.c: -------------------------------------------------------------------------------- 1 | int side_effect(int* x) { 2 | *x = 1; 3 | return 0; 4 | } 5 | 6 | void exprs(const unsigned n, int * const buffer) { 7 | 8 | unsigned long i = 0; 9 | char arr[1] = {0}; 10 | 11 | side_effect(&buffer[0]); 12 | -side_effect(&buffer[1]); 13 | &""[side_effect(&buffer[2])]; 14 | ++arr[side_effect(&buffer[3])]; 15 | } 16 | -------------------------------------------------------------------------------- /tests/misc/src/lvalues.c: -------------------------------------------------------------------------------- 1 | 2 | int* foo(int *j) { return ++j; } 3 | int bar(int k) { return ++k; } 4 | 5 | // This tests more complex lvalues when the result is not used 6 | void lvalue(int buffer[]) 7 | { 8 | // Direct (Path) 9 | int n = 5; 10 | n = 6; 11 | 12 | // Pointer 13 | int *p = buffer; 14 | *p = 8; // buffer[0] 15 | *(p + 1) = 9; // buffer[1] 16 | *(foo(p + 1)) = 3; // buffer[2] 17 | 18 | // Array index 19 | int arr[1][1] = { 3 }; 20 | buffer[3] = n; // buffer[3] 21 | arr[0][0] = ++n; 22 | buffer[4] = arr[0][0]; // buffer[4] 23 | buffer[bar(4)] = -8; // buffer[5] 24 | } 25 | -------------------------------------------------------------------------------- /tests/misc/src/malloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void malloc_test(const unsigned int buffer_size, int buffer[const]) 4 | { 5 | int i = 0; 6 | 7 | void **p = malloc(sizeof(*p)); 8 | free(p); 9 | 10 | int *ip = malloc(sizeof(*ip)); 11 | *ip = 34; 12 | buffer[i++] = *ip; 13 | 14 | ip = realloc(ip, 2 * sizeof(*ip)); 15 | ip[0] = 35; 16 | ip[1] = 36; 17 | buffer[i++] = ip[0]; 18 | buffer[i++] = ip[1]; 19 | 20 | free(ip); 21 | } 22 | -------------------------------------------------------------------------------- /tests/misc/src/qsort.c: -------------------------------------------------------------------------------- 1 | 2 | void swap(int* a, int* b) 3 | { 4 | int t = *a; 5 | *a = *b; 6 | *b = t; 7 | } 8 | 9 | int partition (int arr[], int low, int high) 10 | { 11 | int pivot = arr[high]; 12 | int i = low - 1; 13 | 14 | for (int j = low; j <= high - 1; j++) { 15 | if (arr[j] <= pivot) { 16 | i++; 17 | swap(&arr[i], &arr[j]); 18 | } 19 | } 20 | swap(&arr[i + 1], &arr[high]); 21 | return i + 1; 22 | } 23 | 24 | void quickSort(int arr[], int low, int high) 25 | { 26 | if (low < high) { 27 | int i = partition(arr, low, high); 28 | quickSort(arr, low, i - 1); 29 | quickSort(arr, i + 1, high); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/misc/src/shadowing.c: -------------------------------------------------------------------------------- 1 | int twice(int i) { 2 | return i*2; 3 | } 4 | 5 | void shadow(unsigned buffer_size, int buffer[]) { 6 | int i = 10; 7 | for (int i = 0, j = 0; i < buffer_size; i++, j+=3) { 8 | buffer[i] = twice(j); 9 | int i = 0; 10 | } 11 | if (buffer_size > 0) { 12 | buffer[0] = i; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/misc/src/strings_h.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setmem(unsigned size, int* buffer) { 4 | memset(buffer, 1, size * sizeof(*buffer)); 5 | } 6 | -------------------------------------------------------------------------------- /tests/misc/src/test_exprs.rs: -------------------------------------------------------------------------------- 1 | use crate::exprs::rust_exprs; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn exprs(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 60; 10 | 11 | pub fn test_exprs() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | 15 | unsafe { 16 | exprs(BUFFER_SIZE as c_uint, buffer.as_mut_ptr()); 17 | rust_exprs(BUFFER_SIZE as c_uint, rust_buffer.as_mut_ptr()); 18 | } 19 | 20 | for x in 0..BUFFER_SIZE { 21 | assert_eq!(buffer[x], rust_buffer[x], "index {}", x); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/misc/src/test_lvalues.rs: -------------------------------------------------------------------------------- 1 | use crate::lvalues::rust_lvalue; 2 | use std::ffi::c_int; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn lvalue(_: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 6; 10 | 11 | pub fn test_lvalue() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [8, 9, 3, 6, 7, -8]; 15 | 16 | unsafe { 17 | lvalue(buffer.as_mut_ptr()); 18 | rust_lvalue(rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | -------------------------------------------------------------------------------- /tests/misc/src/test_sizeofs.rs: -------------------------------------------------------------------------------- 1 | //! feature_core_intrinsics, feature_label_break_value 2 | 3 | use crate::sizeofs::rust_sizeofs; 4 | use std::ffi::{c_int, c_uint}; 5 | 6 | #[link(name = "test")] 7 | extern "C" { 8 | fn sizeofs(_: c_uint, _: *mut c_int); 9 | } 10 | 11 | const BUFFER_SIZE: usize = 60; 12 | 13 | pub fn test_sizeofs() { 14 | let mut buffer = [0; BUFFER_SIZE]; 15 | let mut rust_buffer = [0; BUFFER_SIZE]; 16 | 17 | unsafe { 18 | sizeofs(BUFFER_SIZE as c_uint, buffer.as_mut_ptr()); 19 | rust_sizeofs(BUFFER_SIZE as c_uint, rust_buffer.as_mut_ptr()); 20 | } 21 | 22 | for x in 0..BUFFER_SIZE { 23 | assert_eq!(buffer[x], rust_buffer[x], "index {}", x); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/misc/src/test_typedef.rs: -------------------------------------------------------------------------------- 1 | use crate::typedef::{int_ptr, my_int, rust_entry}; 2 | 3 | use std::ffi::c_int; 4 | 5 | #[link(name = "test")] 6 | extern "C" { 7 | fn entry() -> c_int; 8 | } 9 | 10 | pub fn test_typedef() { 11 | let ret = unsafe { entry() }; 12 | let rust_ret = unsafe { rust_entry() }; 13 | assert_eq!(ret, 0); 14 | assert_eq!(rust_ret, 0); 15 | 16 | let mut rust_var: my_int = 5; 17 | let mut c_var: c_int = 5; 18 | assert_eq!(rust_var, c_var); 19 | 20 | let rptr_var: int_ptr = &mut rust_var as *mut my_int; 21 | let cptr_var: int_ptr = &mut c_var as *mut c_int; 22 | 23 | unsafe { 24 | assert_eq!(*rptr_var, *cptr_var); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /tests/misc/src/uninitialized.c: -------------------------------------------------------------------------------- 1 | typedef int myint; 2 | typedef myint *myintp; 3 | union u { int x; char y; }; 4 | enum e { foo = 1, bar, baz }; 5 | struct s { union u a_u; char a_c; enum e a_e; }; 6 | 7 | void entry2(const unsigned sz, int buf[const]) { 8 | myint x; 9 | myintp p; 10 | p = &x; 11 | *p = 1; 12 | if (sz > 0) { *buf = x; } 13 | 14 | const int * const q1; 15 | const int * q2; 16 | int * const q3; 17 | int * q4; 18 | void (*f)(void); 19 | void (*fs[2])(void); 20 | int xs[10]; 21 | int ys[4][5]; 22 | int *zs[4]; 23 | struct s my_s; 24 | union u my_u; 25 | } 26 | -------------------------------------------------------------------------------- /tests/misc/src/unsized.c: -------------------------------------------------------------------------------- 1 | //! skip_translation 2 | 3 | struct Foo { 4 | int i; 5 | char unspec[]; 6 | }; 7 | 8 | int main(void) { 9 | struct Foo f1 = { 1, "hello" }; 10 | 11 | printf("%s!", f1.unspec); 12 | printf("%d", sizeof(f1.unspec)); 13 | } 14 | -------------------------------------------------------------------------------- /tests/misc/src/vectortypes.c: -------------------------------------------------------------------------------- 1 | // typedef signed char __v16qs __attribute__((__vector_size__(16))); 2 | -------------------------------------------------------------------------------- /tests/modules/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "modules-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | -------------------------------------------------------------------------------- /tests/modules/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/modules/src/modules.c: -------------------------------------------------------------------------------- 1 | //! translate_const_macros, reorganize_definitions 2 | 3 | #include 4 | #include "other_mod2.h" 5 | 6 | typedef int char_to_int(char); 7 | 8 | int c_to_i(char c) { 9 | return (int) c; 10 | } 11 | 12 | #define FN_PTR_MACRO c_to_i 13 | #define FN_PTR_MACRO2 other_c_to_i 14 | 15 | void modules() { 16 | char x = 10; 17 | char_to_int* ptr = NULL; 18 | 19 | char_to_int* ptr2 = FN_PTR_MACRO; 20 | char_to_int* ptr3 = FN_PTR_MACRO2; 21 | ptr2(x); 22 | } 23 | -------------------------------------------------------------------------------- /tests/modules/src/other_mod.h: -------------------------------------------------------------------------------- 1 | 2 | static int other_c_to_i(char c) { 3 | int *null_var = NULL; 4 | return (int) c; 5 | } 6 | 7 | 8 | typedef enum { 9 | E_variant1 = 1, 10 | E_variant2 = 2, 11 | } E_enum; 12 | -------------------------------------------------------------------------------- /tests/modules/src/other_mod2.h: -------------------------------------------------------------------------------- 1 | #include "other_mod.h" 2 | 3 | int (*global_fn)(char) = other_c_to_i; 4 | 5 | E_enum use_enum() { 6 | int x = E_variant1; 7 | return (E_enum) 2; 8 | } 9 | -------------------------------------------------------------------------------- /tests/modules/src/test_modules.rs: -------------------------------------------------------------------------------- 1 | use crate::modules::rust_modules; 2 | use std::ffi::c_uint; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn modules(); 7 | } 8 | 9 | pub fn test_modules() {} 10 | -------------------------------------------------------------------------------- /tests/pointers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "pointer-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | -------------------------------------------------------------------------------- /tests/pointers/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/pointers/src/pointer_arith.c: -------------------------------------------------------------------------------- 1 | void entry2(const unsigned sz, int buf[const]) { 2 | for (int *cursor = buf; cursor < buf + sz; cursor += 1) { 3 | *cursor = 1; 4 | } 5 | 6 | for (int *cursor = buf + 10; cursor > buf; cursor -= 1) { 7 | *cursor = 2; 8 | } 9 | 10 | int * p = buf + 30; 11 | *(p - 10U) = 33; 12 | *(p--) = 34; 13 | *(p-=2) = 35; 14 | } 15 | -------------------------------------------------------------------------------- /tests/simd.x86_64/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "simd-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | 8 | -------------------------------------------------------------------------------- /tests/simd.x86_64/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/simd.x86_64/target-tuple: -------------------------------------------------------------------------------- 1 | x86_64-unknown-linux-gnu 2 | -------------------------------------------------------------------------------- /tests/statics/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "statics-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | libc = "0.2" 8 | -------------------------------------------------------------------------------- /tests/statics/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/statics/src/test_storage.rs: -------------------------------------------------------------------------------- 1 | use crate::storage::rust_entry; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn entry(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 11; 10 | 11 | pub fn test_buffer() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [1, 4, 2, 0, 0, 0, 0, 4, 4, 104, 111]; 15 | 16 | unsafe { 17 | entry(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 18 | rust_entry(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | -------------------------------------------------------------------------------- /tests/statics/src/thread_locals.c: -------------------------------------------------------------------------------- 1 | 2 | static int gssi = 17; 3 | extern int gesi; 4 | static __thread int gsti = 37; 5 | extern __thread int geti; 6 | 7 | void thread_entry(const unsigned buffer_size, int buffer[]) { 8 | if (buffer_size < 16) return; 9 | 10 | static int fssi = 53; 11 | extern int fesi; 12 | static __thread int fsti = 59; 13 | extern __thread int feti; 14 | 15 | int i = 0; 16 | #define ADD_VAR(x) do { buffer[i++] = (x); (x) += 1337; buffer[i++] = (x); } while (0) 17 | ADD_VAR(gssi); 18 | ADD_VAR(gesi); 19 | ADD_VAR(gsti); 20 | ADD_VAR(geti); 21 | ADD_VAR(fssi); 22 | ADD_VAR(fesi); 23 | ADD_VAR(fsti); 24 | ADD_VAR(feti); 25 | } 26 | -------------------------------------------------------------------------------- /tests/structs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "structs-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | c2rust-bitfields = { path = "../../c2rust-bitfields", version = "0.20.0" } 8 | memoffset = "0.2" 9 | libc = "0.2" 10 | -------------------------------------------------------------------------------- /tests/structs/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/structs/src/anonymous_decls.c: -------------------------------------------------------------------------------- 1 | struct { 2 | struct { 3 | int l; 4 | } j; 5 | } k; 6 | -------------------------------------------------------------------------------- /tests/structs/src/forward.c: -------------------------------------------------------------------------------- 1 | 2 | // forward declaration of structs should not cause problems. 3 | struct s; 4 | struct forward; 5 | struct s { int x; }; 6 | 7 | void forward(const unsigned int buffer_size, int buffer[const]){ 8 | if (buffer_size < 1) return; 9 | 10 | struct s foo = { 1 }; 11 | buffer[0] = foo.x; 12 | 13 | // GH #84: Previously could not initialize null ptrs 14 | // for forward declared structs which do not have a definition 15 | struct forward *initialized_null_ptr; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /tests/structs/src/self_referential.c: -------------------------------------------------------------------------------- 1 | typedef struct Node { 2 | struct Node* next; 3 | } Node; 4 | 5 | void whatever(Node *np) { 6 | 7 | } -------------------------------------------------------------------------------- /tests/structs/src/struct_with_exp.c: -------------------------------------------------------------------------------- 1 | 2 | struct s { 3 | int i; 4 | }; 5 | 6 | void struct_with_exp(const unsigned int buffer_size, int buffer[const]){ 7 | if (buffer_size < 1) return; 8 | 9 | struct s *p; 10 | int j = 42; 11 | p = &((struct s){j++}); // Compound literal with address-of operator and post-increment operator 12 | 13 | buffer[0] = p->i; 14 | } 15 | -------------------------------------------------------------------------------- /tests/structs/src/test_anonymous_decls.rs: -------------------------------------------------------------------------------- 1 | use crate::anonymous_decls::rust_k; 2 | 3 | pub fn test_anonymous_decl() { 4 | unsafe { 5 | assert_eq!(rust_k.j.l, 0); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /tests/structs/src/test_forward.rs: -------------------------------------------------------------------------------- 1 | use crate::forward::rust_forward; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn forward(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 1; 10 | 11 | pub fn test_buffer() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [1]; 15 | 16 | unsafe { 17 | forward(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 18 | rust_forward(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | -------------------------------------------------------------------------------- /tests/structs/src/test_self_referential.rs: -------------------------------------------------------------------------------- 1 | use crate::self_referential::Node; 2 | 3 | #[link(name = "test")] 4 | extern "C" { 5 | fn whatever(np: *mut Node); 6 | } 7 | 8 | pub fn test_buffer2() {} 9 | -------------------------------------------------------------------------------- /tests/structs/src/test_struct_with_exp.rs: -------------------------------------------------------------------------------- 1 | use crate::struct_with_exp::rust_struct_with_exp; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn struct_with_exp(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 1; 10 | 11 | pub fn test_struct_with_exp() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [42]; 15 | 16 | unsafe { 17 | struct_with_exp(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 18 | rust_struct_with_exp(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | -------------------------------------------------------------------------------- /tests/structs/src/test_variable_offsetof.rs: -------------------------------------------------------------------------------- 1 | //! extern_crate_memoffset 2 | 3 | use crate::variable_offsetof::{rust_get_offset, rust_get_offset2, size_t}; 4 | 5 | #[link(name = "test")] 6 | extern "C" { 7 | fn get_offset(_: size_t) -> size_t; 8 | fn get_offset2(_: size_t) -> size_t; 9 | } 10 | 11 | pub fn test_get_offset() { 12 | for idx in 0..3 { 13 | let rust_ret = unsafe { rust_get_offset(idx) }; 14 | let c_ret = unsafe { get_offset(idx) }; 15 | 16 | assert_eq!(rust_ret, c_ret); 17 | 18 | let rust_ret2 = unsafe { rust_get_offset2(idx) }; 19 | let c_ret2 = unsafe { get_offset2(idx) }; 20 | 21 | assert_eq!(rust_ret2, c_ret2); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/structs/src/variable_offsetof.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { 4 | int a; 5 | float mod[3]; 6 | } use; 7 | 8 | size_t get_offset(size_t idx) { 9 | return offsetof(use, mod[idx]); 10 | } 11 | 12 | struct yield { 13 | int a; 14 | float mod[3]; 15 | }; 16 | 17 | size_t get_offset2(size_t idx) { 18 | return offsetof(struct yield, mod[idx]); 19 | } 20 | -------------------------------------------------------------------------------- /tests/unions/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "union-tests" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /tests/unions/build.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); 5 | 6 | println!("cargo:rustc-link-search=native={}", manifest_dir); 7 | } 8 | -------------------------------------------------------------------------------- /tests/unions/src/test_unions.rs: -------------------------------------------------------------------------------- 1 | use crate::unions::rust_entry; 2 | use std::ffi::{c_int, c_uint}; 3 | 4 | #[link(name = "test")] 5 | extern "C" { 6 | fn entry(_: c_uint, _: *mut c_int); 7 | } 8 | 9 | const BUFFER_SIZE: usize = 19; 10 | 11 | pub fn test_buffer() { 12 | let mut buffer = [0; BUFFER_SIZE]; 13 | let mut rust_buffer = [0; BUFFER_SIZE]; 14 | let expected_buffer = [12, 12, 0, 5, 1, 2, 3, 4, 0, 5, 6, 7, 8, 0, 8, 9, 10, 12, 18]; 15 | 16 | unsafe { 17 | entry(BUFFER_SIZE as u32, buffer.as_mut_ptr()); 18 | rust_entry(BUFFER_SIZE as u32, rust_buffer.as_mut_ptr()); 19 | } 20 | 21 | assert_eq!(buffer, rust_buffer); 22 | assert_eq!(buffer, expected_buffer); 23 | } 24 | --------------------------------------------------------------------------------