Translate string literals in C and C++#36
Merged
nunoplopes merged 23 commits intoCpp2Rust:masterfrom Apr 29, 2026
Merged
Conversation
ae559d4 to
fd4780f
Compare
In C, as opposed to C++, string literals are char [] instead of const char [] so cast_mut should be added on ArrayToPointerDecay from char [] to char * instead of NoOp const char * to char *.
In unsafe, they are translated as [u8; N] and in refcount they are translated as Box<[u8]>.
3aecceb to
e64540d
Compare
nunoplopes
reviewed
Apr 29, 2026
| void foo_const(const char *str) {} | ||
|
|
||
| int main() { | ||
| char *mutable_strings[] = {"a", "b", "c"}; |
Contributor
There was a problem hiding this comment.
this code is invalid in C++. strings are const.
Contributor
Author
There was a problem hiding this comment.
Both GCC and Clang generate a warning in this case. Should we reject this code or accept it?
Contributor
There was a problem hiding this comment.
I would just remove these tests.
nunoplopes
reviewed
Apr 29, 2026
| std::process::exit(main_0()); | ||
| } | ||
| fn main_0() -> i32 { | ||
| let empty_buf : Value<Box<[ u8 ]> > = Rc::new(RefCell::new(Box::<[u8]>::from(b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0".as_slice()) )) ; |
Contributor
There was a problem hiding this comment.
this is a bit ugly..
Can we detect this case and emit vec![0; 256].into_boxed_slice()?
lucic71
added a commit
to lucic71/cpp2rust
that referenced
this pull request
May 3, 2026
This PR adds support for declaring and using const/non-const pointers and arrays containing string literals. VisitStringLiteral now always generates a string literal (`[u8; N]` in unsafe, `Box<[u8]>` in refcount). It's the job of `VisitImplicitCast::CK_ArrayToPointerDecay` to convert the string literal to poiner if it's necessary. In C string literals are `char[]` and in C++ they are `const char[]`. `VisitImplicitCast::CK_NoOp` handles the conversion between const and non-const char pointers. Besides always generating a string literal instead of pointer, VisitStringLiteral also pad with null bytes the following code: `char array_bigger_than_string_literal[10] = "1"`, effectively becoming `"1\0\0\0..."`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds support for declaring and using const/non-const pointers and arrays containing string literals. VisitStringLiteral now always generates a string literal (
[u8; N]in unsafe,Box<[u8]>in refcount). It's the job ofVisitImplicitCast::CK_ArrayToPointerDecayto convert the string literal to poiner if it's necessary.In C string literals are
char[]and in C++ they areconst char[].VisitImplicitCast::CK_NoOphandles the conversion between const and non-const char pointers.Besides always generating a string literal instead of pointer, VisitStringLiteral also pad with null bytes the following code:
char array_bigger_than_string_literal[10] = "1", effectively becoming"1\0\0\0...".