Comments (6)
I'm going to start taking a look into (simple) macro translation. The current plan is:
#define CONSTANT 1
-> Rustconst
#ifdef CONSTANT
-> Rustcfg!
macro and attribute
Constants that we recognize as compiler-provided platform constants should be translated to native rust versions when available, otherwise we emit these constants as feature flags.
from c2rust.
Dealing with macros in a sensible fashion is a long-term goal - but there is still a lot of design work to do.
For instance, it would be necessary to work (at least partially) with the un-preprocessed C source. That poses a pretty big challenge, since most of our current translation works on top of a Clang-produced C AST - which has to run the preprocessor in order to successfully parse and type check.
On possible approach to detecting C macros (and mapping them onto calls to Rust macros) might be to look for subtrees in the expanded C AST that were constructed entirely from tokens that map back to one macro call...
As for variadic functions, we're holding out for Rust language features (like rust-lang/rust#49878).
from c2rust.
One possible approach to detecting C macros (and mapping them onto calls to Rust macros) might be to look for subtrees in the expanded C AST that were constructed entirely from tokens that map back to one macro call...
I can confirm that this approach works and can even support nested macros (taking other macros as arguments, calling other macros in the body or both combined). I have a proof of concept (only supports expressions) for a C-like lang here.
One downside is that there's no way to translate macros that are not used, is that acceptable for c2rust? (I am thinking about implementing this properly in c2rust, then forking it and using it as a basis for qc2rust. Not sure if/when i'll have the time so no promises.)
from c2rust.
@martin-t unexpanded macros might be a problem when translating libraries. On the other hand, translating only used macros is better than what we do today, so yes, this is indeed acceptable. If you do get the time to implement this, I'd recommend you feature gate it like we did with const macros.
from c2rust.
This issue is quite old and relatively common in the code. Was there any progress with this? Or maybe some unmerged branch/PR somewhere? If not, where in the code it is probably should be implemented? Thank you for the answer.
from c2rust.
One other way to translate macros would be to use some sort of custom preprocessor:
Within the C code, we replace, (after checking if a macro forms a valid expression)
#define MACRONAME macroexpression
by
const _c2rust_macrotype _c2rust_mconst_MACRONAME = macroexpression;
#define MACRONAME _c2rust_mconst_MACRONAME
When translating the AST to rust, we truncate the _c2rust_mconst_
prefex from variables. If we found the const _c2rust_macrotype _c2rust_mconst_MACRONAME
declaration, we walk the AST to guess the correct type and emit a Rust const declaration.
from c2rust.
Related Issues (20)
- Missing semicolon after volatile pointer cast.
- Aggregate libc functions into one file. HOT 1
- FYI: fatal runtime error: Rust cannot catch foreign exceptions
- Readme example of transpile for main.c doesn't work HOT 1
- Transpile `#include` as `include!()` HOT 4
- C2Rust transpiler emits features removed in recent nightlies HOT 1
- Switch case with ranges HOT 2
- rust-toolchain.toml is too old for 1.75 rust HOT 4
- cargo install c2rust failed: Could not find a configuration file for package "LLVM" that exactly matches requested version "16.0.6". HOT 2
- Build not matching clang version (Fedora 39, Clang 17.0.6) HOT 3
- cargo install c2rust failed: Error 2 HOT 2
- In the while loop body using a volatile variable, the generated code will lack a semicolon.
- why do you not support windows HOT 1
- Build fails with LLVM 18 and tip-of-tree HOT 1
- fail build on archlinux HOT 2
- Casting bool to pointer type results in invalid Rust
- Fatal error: 'stddef.h' file not found HOT 4
- ast_exporter can't convert a c code to ast HOT 2
- (Question) Do you plan to support C++ ? HOT 1
- Missing parentheses on reference applied to a cast
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from c2rust.