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
50 changes: 46 additions & 4 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,16 @@ bool Converter::VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr) {
return false;
}

void Converter::ConvertIntegerToEnumeralCast(clang::Expr *to,
clang::Expr *from) {
StrCat(GetUnsafeTypeAsString(to->getType()), "::from");
PushParen paren(*this);
Convert(from);
if (!from->getType()->isSpecificBuiltinType(clang::BuiltinType::Int)) {
StrCat(keyword::kAs, "i32");
}
}

bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
auto *sub_expr = expr->getSubExpr();
auto type = expr->getType();
Expand Down Expand Up @@ -1846,6 +1856,10 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
Convert(sub_expr);
break;
}
if (type->isEnumeralType() && !sub_expr->getType()->isEnumeralType()) {
ConvertIntegerToEnumeralCast(expr, sub_expr);
break;
}
{
PushParen outer(*this);
if (clang::isa<clang::BinaryOperator>(sub_expr)) {
Expand Down Expand Up @@ -1890,6 +1904,10 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
StrCat(')');
return false;
}
if (type->isEnumeralType() && !sub_expr->getType()->isEnumeralType()) {
ConvertIntegerToEnumeralCast(expr, sub_expr);
return false;
}
{
PushParen paren(*this);
Convert(sub_expr);
Expand Down Expand Up @@ -2200,10 +2218,14 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
}

if (auto enum_constant = clang::dyn_cast<clang::EnumConstantDecl>(decl)) {
return std::format("{}::{}",
GetRecordName(clang::dyn_cast<clang::EnumDecl>(
enum_constant->getDeclContext())),
std::string_view(enum_constant->getName()));
auto qualified = std::format("{}::{}",
GetRecordName(clang::dyn_cast<clang::EnumDecl>(
enum_constant->getDeclContext())),
std::string_view(enum_constant->getName()));
if (!expr->getType()->isEnumeralType()) {
return std::format("({} as i32)", qualified);
}
return qualified;
}

if (IsGlobalVar(expr)) {
Expand Down Expand Up @@ -2704,9 +2726,29 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
std::string_view(init.data(), init.size())));
}
StrCat("}");

AddFromImpl(decl);
return false;
}

void Converter::AddFromImpl(clang::EnumDecl *decl) {
auto name = GetRecordName(decl);
StrCat(std::format("impl From<i32> for {}", name));
PushBrace impl(*this);
StrCat(std::format("fn from(n: i32) -> {}", name));
PushBrace fn(*this);
StrCat("match n");
PushBrace match(*this);
for (auto e : decl->enumerators()) {
llvm::SmallVector<char, 32> init;
e->getInitVal().toString(init, 10);
StrCat(std::format("{} => {}::{},",
std::string_view(init.data(), init.size()), name,
std::string_view(e->getName())));
}
StrCat(std::format("_ => panic!(\"invalid {} value: {{}}\", n),", name));
}

bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) {
if (expr->getType()->isPointerType()) {
StrCat(keyword_default_);
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual bool VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr);

void ConvertIntegerToEnumeralCast(clang::Expr *to, clang::Expr *from);

virtual bool VisitImplicitCastExpr(clang::ImplicitCastExpr *expr);

virtual bool VisitExplicitCastExpr(clang::ExplicitCastExpr *expr);
Expand Down Expand Up @@ -293,6 +295,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual bool VisitEnumDecl(clang::EnumDecl *decl);

virtual void AddFromImpl(clang::EnumDecl *decl);

virtual bool VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr);

virtual bool VisitLambdaExpr(clang::LambdaExpr *expr);
Expand Down
9 changes: 9 additions & 0 deletions tests/ub/enum_out_of_range_cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// panic

enum Color { RED, GREEN, BLUE };

int main() {
int n = 3;
Color c = (Color)n;
return c == BLUE ? 0 : 1;
}
37 changes: 37 additions & 0 deletions tests/ub/out/refcount/enum_out_of_range_cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
extern crate libcc2rs;
use libcc2rs::*;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
#[derive(Clone, Copy, PartialEq, Debug, Default)]
enum Color {
#[default]
RED = 0,
GREEN = 1,
BLUE = 2,
}
impl From<i32> for Color {
fn from(n: i32) -> Color {
match n {
0 => Color::RED,
1 => Color::GREEN,
2 => Color::BLUE,
_ => panic!("invalid Color value: {}", n),
}
}
}
pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
let n: Value<i32> = Rc::new(RefCell::new(3));
let c: Value<Color> = Rc::new(RefCell::new(Color::from((*n.borrow()))));
return if (((*c.borrow()) as i32) == (Color::BLUE as i32)) {
0
} else {
1
};
}
39 changes: 39 additions & 0 deletions tests/ub/out/unsafe/enum_out_of_range_cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
extern crate libc;
use libc::*;
extern crate libcc2rs;
use libcc2rs::*;
use std::collections::BTreeMap;
use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
#[derive(Clone, Copy, PartialEq, Debug, Default)]
enum Color {
#[default]
RED = 0,
GREEN = 1,
BLUE = 2,
}
impl From<i32> for Color {
fn from(n: i32) -> Color {
match n {
0 => Color::RED,
1 => Color::GREEN,
2 => Color::BLUE,
_ => panic!("invalid Color value: {}", n),
}
}
}
pub fn main() {
unsafe {
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut n: i32 = 3;
let mut c: Color = Color::from(n);
return if ((c as i32) == (Color::BLUE as i32)) {
0
} else {
1
};
}
117 changes: 117 additions & 0 deletions tests/unit/enum_int_interop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <assert.h>

enum Color { RED, GREEN, BLUE };

enum Option {
OPT_NONE = 0,
OPT_A = 10,
OPT_B = 20,
OPT_C = 30,
};

typedef enum {
TAG_ZERO = 0,
TAG_ONE = 1,
TAG_TWO = 2,
} Tag;

int as_int(Color c) { return c; }

int classify_option(int option) {
switch (option) {
case OPT_NONE:
return -1;
case OPT_A:
return 1;
case OPT_B:
return 2;
case OPT_C:
return 3;
default:
return 0;
}
}

Color make_color(int n) { return (Color)n; }

int main() {
Color c = RED;

assert(c == RED);
assert(c == 0);
assert(c != 1);

if (c == GREEN) {
return 1;
}

switch (c) {
case 0:
break;
case 1:
return 1;
case 2:
return 2;
default:
return 99;
}

int x = c;
assert(x == 0);

int y = c + 1;
assert(y == 1);

c = (Color)2;
assert(c == BLUE);
assert(c == 2);

c = make_color(1);
assert(c == GREEN);

Color cmp = (Color)(c + 1);
assert(cmp == BLUE);

Option o = OPT_A;
assert(o == OPT_A);
assert(o == 10);

int oi = o;
assert(oi == 10);

o = (Option)20;
assert(o == OPT_B);

int rc = classify_option(o);
assert(rc == 2);

rc = classify_option(20);
assert(rc == 2);

rc = classify_option(OPT_C);
assert(rc == 3);

Tag t = TAG_ONE;
assert(t == 1);
assert(t == TAG_ONE);

int ti = t;
assert(ti == 1);

t = (Tag)2;
assert(t == TAG_TWO);

switch (t) {
case TAG_ZERO:
return 90;
case 1:
return 91;
case 2:
break;
}

int extra = (int)RED + (int)GREEN + (int)BLUE;
assert(extra == 0 + 1 + 2);

return 0;
}
Loading
Loading