Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static const char rust_keywords[][12] = {
"safe",
// Standard library keywords
"vec",
"Err",
};

namespace cpp2rust {
Expand Down Expand Up @@ -912,6 +913,23 @@ static size_t GetDeclId(const clang::NamedDecl *decl, bool internal) {
return type_mapping.try_emplace(key, type_mapping.size()).first->second;
}

void EscapeRustKeyword(std::string &name) {
if (!name.empty() &&
std::ranges::all_of(name, [](char c) { return c == '_'; })) {
name += '_';
}
for (auto &keyword : rust_keywords) {
if (!name.starts_with(keyword))
continue;

auto suffix = std::string_view(name).substr(strlen(keyword));
if (std::ranges::all_of(suffix, [](char c) { return c == '_'; })) {
name += '_';
break;
}
}
}

std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
auto name = decl->getDeclName().isIdentifier() ? decl->getName().str()
: decl->getNameAsString();
Expand Down Expand Up @@ -962,24 +980,7 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
name += std::to_string(*id);
}

// transform decl names that are rust keywords:
// keyword -> keyword_
// keyword_ -> keyword__
// etc
if (!name.empty() &&
std::ranges::all_of(name, [](char c) { return c == '_'; })) {
name += '_';
}
for (auto &keyword : rust_keywords) {
if (!name.starts_with(keyword))
continue;

auto suffix = std::string_view(name).substr(strlen(keyword));
if (std::ranges::all_of(suffix, [](char c) { return c == '_'; })) {
name += '_';
break;
}
}
EscapeRustKeyword(name);

if (name.empty()) {
auto *pdecl = llvm::dyn_cast<clang::ParmVarDecl>(decl);
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ unsigned GetArraySize(clang::QualType array_type);
std::string GetID(const clang::Decl *decl);
std::string GetMethodID(const clang::CXXMethodDecl *decl);

void EscapeRustKeyword(std::string &name);

std::string GetNamedDeclAsString(const clang::NamedDecl *decl);

std::string DisambiguateAnonymousTag(const clang::TagDecl *tag);
Expand Down
1 change: 1 addition & 0 deletions cpp2rust/converter/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ std::string ToRustName(std::string name) {
if (stem == "Ptr" || stem == "Value" || stem == "Box") {
name += '_';
}
EscapeRustKeyword(name);
return name;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/unit/keywords.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#include <cassert>

int report(int Err) { return Err + 1; }

int main() {
int in = 123;
assert(in == 123);
int Err = 1;
assert(report(Err) == 2);
return 0;
}
5 changes: 5 additions & 0 deletions tests/unit/out/refcount/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
pub fn report_0(mut Err_: i32) -> i32 {
return (Err_ + 1);
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
let mut in_: i32 = 123;
assert!((in_ == 123));
let mut Err_: i32 = 1;
assert!((({ report_0(Err_,) }) == 2));
return 0;
}
pub fn __cpp2rust_init_globals() {}
56 changes: 54 additions & 2 deletions tests/unit/out/refcount/reserved_keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ pub struct S {
#[offset(148)]
pub vec_: i32,
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(4)]
pub struct type_ {
#[offset(0)]
pub v: i32,
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(4)]
pub struct loop_ {
#[offset(0)]
#[byte_size(4)]
pub t: type_,
}
pub fn foo_0(
mut as_: i32,
mut async_: i32,
Expand Down Expand Up @@ -212,10 +225,49 @@ fn main_0() -> i32 {
let mut raw_: i32 = 0;
let mut safe_: i32 = 0;
let mut vec_: i32 = 0;
let mut l: loop_ = loop_ { t: type_ { v: 3 } };
let mut t: type_ = l.t.clone();
return ({
foo_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,
(t.v - 3),
)
});
}
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/out/unsafe/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
pub unsafe fn report_0(mut Err_: i32) -> i32 {
return ((Err_) + (1));
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -15,6 +18,8 @@ pub fn main() {
unsafe fn main_0() -> i32 {
let mut in_: i32 = 123;
assert!(((in_) == (123)));
let mut Err_: i32 = 1;
assert!(((unsafe { report_0(Err_,) }) == (2)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
53 changes: 51 additions & 2 deletions tests/unit/out/unsafe/reserved_keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ pub struct S {
pub safe_: i32,
pub vec_: i32,
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct type_ {
pub v: i32,
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct loop_ {
pub t: type_,
}
pub unsafe fn foo_0(
mut as_: i32,
mut async_: i32,
Expand Down Expand Up @@ -176,10 +186,49 @@ unsafe fn main_0() -> i32 {
let mut raw_: i32 = 0;
let mut safe_: i32 = 0;
let mut vec_: i32 = 0;
let mut l: loop_ = loop_ { t: type_ { v: 3 } };
let mut t: type_ = l.t;
return (unsafe {
foo_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,
((t.v) - (3)),
)
});
}
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/reserved_keywords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ struct S {
};
#undef F

// Record names
struct type {
int v;
};
struct loop {
type t;
};

// Function arguments
#define F(n) int n,
int foo(KW(F) int dummy) { return 0; }
Expand All @@ -62,8 +70,11 @@ int main() {
KW(F)
#undef F

struct loop l = {{3}};
struct type t = l.t;

// Function call
#define F(n) 0,
return foo(KW(F) 0);
return foo(KW(F) t.v - 3);
#undef F
}
Loading