TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

C2Rust Transpiler

119 点作者 Aissen超过 2 年前

9 条评论

WalterBright超过 2 年前
C translates quite readily to D. I&#x27;ve been able to translate thousands of lines at a time in less than an hour, usually with some global search &amp; replace and then making adjustments after running it through the D compiler. We relied on being able to do this in the D community for quite a while. There also have been three three translators built, with more or less effectiveness. It is nice to get the code into D, and then take advantage of D&#x27;s safety features.<p>The fundamental problem with translation, followed by some hand tweaking, is that it only works if the C version is to be abandoned. If the C code is maintained by anyone else, as soon as they make changes, the translation gets out of date. Updating the translation turns out to be impractical because of the hand tweaking necessary.<p>Then there are some frustrating structural limits. The largest is that C doesn&#x27;t have modules. The preprocessor puts everything into one file, and every C compilation is for one file. Declarations get duplicated across every translation unit. Somehow, these need to get teased apart into modules. This structural redo gets done by hand, and requires pretty good familiarity with the C code&#x27;s design.<p>The preprocessor poses another major problem. The preprocessor language and the core C compiler have no knowledge of each other. They are completely separate languages, with their own syntax, keywords, semantics, etc. The preprocessor, aside from trivial use of it, simply does not translate into other languages. I also have yet to find a C programmer who could resist using the preprocessor as a metaprogramming language, which does a great job at obstructing all efforts at converting to another language.<p>All this stuff raises a lot of friction for D interacting with C code. Programmers don&#x27;t like friction, they don&#x27;t want to deal with C code they are unfamiliar with, they don&#x27;t want to fold in maintenance changes in C code to the translation, etc. They want it to &quot;just work&quot;.<p>The eventual solution I came up with is obvious, but I&#x27;d always dismissed it as impractical. Just fix the D compiler to be able to compile C code directly, and internally make the C declarations and constructs available to D code. This turned out to be fairly easy to do, and is ridiculously effective. It sometimes works even better than C++&#x27;s ability to #include C code (C++ doesn&#x27;t support things like _Generic, old style C declarations, etc.). All you have to do is import .c code just like importing any D module, and the D compiler takes care of all the dirty work for you.<p>It isn&#x27;t perfect, for example, C compilers have lots of extensions, and dealing with all of them is hopeless. But we just do the common ones, as it turns out most of them are rarely used.
评论 #33310108 未加载
评论 #33309509 未加载
评论 #33310011 未加载
mastax超过 2 年前
Their blogpost about translating Quake 3 was interesting: <a href="https:&#x2F;&#x2F;immunant.com&#x2F;blog&#x2F;2020&#x2F;01&#x2F;quake3&#x2F;" rel="nofollow">https:&#x2F;&#x2F;immunant.com&#x2F;blog&#x2F;2020&#x2F;01&#x2F;quake3&#x2F;</a>
评论 #33311417 未加载
fbdab103超过 2 年前
A potential use case I see is for security auditing. Even if you cannot port an existing C codebase to Rust, you could run this tool to examine the unsafe hotspots. Any place where the translation has to rely upon unsafe is a region of the code more likely to contain any of the mistakes Rust is designed to prevent. Of course, this pre-supposes that 90% of the translation does not have to lean on unsafe annotation.
评论 #33308323 未加载
评论 #33309370 未加载
评论 #33308685 未加载
SaddledBounding超过 2 年前
I think a demo of the transpiler output for a short function would make a great addition to the readme.
评论 #33307538 未加载
als0超过 2 年前
You can try it out on the main website <a href="https:&#x2F;&#x2F;c2rust.com" rel="nofollow">https:&#x2F;&#x2F;c2rust.com</a> where they have a web version. Unfortunately it isn&#x27;t working (HTTP 503 error)
评论 #33324101 未加载
评论 #33308547 未加载
评论 #33307530 未加载
Animats超过 2 年前
How does this compare to Corrode? The trouble with these things is that the Rust that comes out is usually too awful to maintain. Corrode, too, said that someday they&#x27;d generate more reasonable Rust. But that never happened. Converting C into Rust with unsafe raw pointers is not all that useful.<p>What&#x27;s needed is some way to provide key information C doesn&#x27;t have. Mostly about array sizes. Some way to annotate<p><pre><code> int read(int fd, void* buf, size_t len) </code></pre> to tell the system that buf has size len.<p>A file of translation hints with such info could guide the translator into producing decent Rust. Most of the things done with pointer arithmetic can be expressed with slices. (Things being done with pointer arithmetic which can&#x27;t be expressed as slices should be viewed with deep suspicion.) But you need size info to do that.
评论 #33310480 未加载
评论 #33310010 未加载
评论 #33310882 未加载
评论 #33309358 未加载
评论 #33309572 未加载
dahfizz超过 2 年前
I would be interested to see performance numbers of the C version and the transpired rust version of some program.
评论 #33311182 未加载
gwbas1c超过 2 年前
Really cool concept! (Rust is my favorite language for tinkering; I haven&#x27;t touched C since I was in school.)<p>What would really help is success stories: Who&#x27;s used it? What have they used it for? What challenges did they encounter? Then again, maybe this is so new that there aren&#x27;t a lot of success stories yet. :)
评论 #33307806 未加载
评论 #33307550 未加载
kazinator超过 2 年前
Sampling some directories and files in the test suite of this project, I see a problem: testing is done by translating C to rust and compiling it, and then testing the run-time behavior of the result. I don&#x27;t see test cases which cover the behavior of the translator directly: like that a certain C language input maps to a certain Rust output.
评论 #33308679 未加载
评论 #33308534 未加载