Skip to content

Commit dd08d16

Browse files
committed
Fix variables named _
1 parent 2bb3063 commit dd08d16

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
968968
// keyword -> keyword_
969969
// keyword_ -> keyword__
970970
// etc
971+
if (!name.empty() &&
972+
std::ranges::all_of(name, [](char c) { return c == '_'; })) {
973+
name += '_';
974+
}
971975
for (auto &keyword : rust_keywords) {
972976
if (!name.starts_with(keyword))
973977
continue;

‎tests/unit/union_basic.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ union basic {
88

99
union empty {};
1010

11+
union underscores {
12+
int _;
13+
double __;
14+
};
15+
1116
int main(void) {
1217
union basic u;
1318
union empty e;
@@ -33,5 +38,15 @@ int main(void) {
3338
assert(*pf == 1.0f);
3439
assert(buf[3] == 0x3F);
3540

41+
union underscores us;
42+
us._ = 5;
43+
assert(us._ == 5);
44+
us.__ = 2.5;
45+
assert(us.__ == 2.5);
46+
47+
int _ = 1;
48+
int __ = 2;
49+
assert(_ + __ == 3);
50+
3651
return 0;
3752
}

0 commit comments

Comments
 (0)