Skip to content

Commit d0ecc58

Browse files
committed
Translate anonymous enums
1 parent 2165500 commit d0ecc58

5 files changed

Lines changed: 112 additions & 1 deletion

File tree

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2606,7 +2606,7 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
26062606
}
26072607
Mapper::AddRuleForUserDefinedType(decl);
26082608
StrCat("#[derive(Clone, Copy, PartialEq, Debug, Default)]");
2609-
StrCat(std::format("enum {}", Mapper::Map(ctx_.getCanonicalTagType(decl))));
2609+
StrCat(std::format("enum {}", GetRecordName(decl)));
26102610
StrCat("{");
26112611
bool first_enumerator = true;
26122612
for (auto e : decl->enumerators()) {

cpp2rust/converter/mapper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,12 @@ std::string ToString(const clang::NamedDecl *decl) {
736736
return synthesizeAnonRecordName(record);
737737
}
738738

739+
if (auto *enum_decl = clang::dyn_cast<clang::EnumDecl>(decl);
740+
enum_decl && !enum_decl->getIdentifier() &&
741+
enum_decl->getTypedefNameForAnonDecl() == nullptr) {
742+
return std::format("anon_enum_{}", GetLineNumber(enum_decl));
743+
}
744+
739745
std::string out;
740746
llvm::raw_string_ostream os(out);
741747

tests/unit/anonymous-enum.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
enum {
2+
FIRST_A,
3+
FIRST_B,
4+
};
5+
6+
struct S {
7+
int a;
8+
9+
enum {
10+
SECOND_A,
11+
SECOND_B,
12+
};
13+
};
14+
15+
int main() {
16+
enum {
17+
THIRD_A,
18+
THIRD_B,
19+
};
20+
return 0;
21+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
10+
enum anon_enum_1 {
11+
#[default]
12+
FIRST_A = 0,
13+
FIRST_B = 1,
14+
}
15+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
16+
enum anon_enum_9 {
17+
#[default]
18+
SECOND_A = 0,
19+
SECOND_B = 1,
20+
}
21+
#[derive(Default)]
22+
pub struct S {
23+
pub a: Value<i32>,
24+
}
25+
impl Clone for S {
26+
fn clone(&self) -> Self {
27+
let mut this = Self {
28+
a: Rc::new(RefCell::new((*self.a.borrow()))),
29+
};
30+
this
31+
}
32+
}
33+
impl ByteRepr for S {}
34+
pub fn main() {
35+
std::process::exit(main_0());
36+
}
37+
fn main_0() -> i32 {
38+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
39+
enum anon_enum_16 {
40+
#[default]
41+
THIRD_A = 0,
42+
THIRD_B = 1,
43+
};
44+
return 0;
45+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
10+
enum anon_enum_1 {
11+
#[default]
12+
FIRST_A = 0,
13+
FIRST_B = 1,
14+
}
15+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
16+
enum anon_enum_9 {
17+
#[default]
18+
SECOND_A = 0,
19+
SECOND_B = 1,
20+
}
21+
#[repr(C)]
22+
#[derive(Copy, Clone, Default)]
23+
pub struct S {
24+
pub a: i32,
25+
}
26+
pub fn main() {
27+
unsafe {
28+
std::process::exit(main_0() as i32);
29+
}
30+
}
31+
unsafe fn main_0() -> i32 {
32+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
33+
enum anon_enum_16 {
34+
#[default]
35+
THIRD_A = 0,
36+
THIRD_B = 1,
37+
};
38+
return 0;
39+
}

0 commit comments

Comments
 (0)