Skip to content

Commit c72cdf5

Browse files
committed
work
1 parent 3f30d90 commit c72cdf5

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,11 +3806,16 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
38063806
auto name = GetRecordName(decl);
38073807
StrCat(std::format("pub type {} = {};", name,
38083808
GetUnsafeTypeAsString(decl->getIntegerType())));
3809+
bool is_bool = decl->getIntegerType()->isBooleanType();
38093810
for (auto e : decl->enumerators()) {
38103811
llvm::SmallVector<char, 32> init;
38113812
e->getInitVal().toString(init, 10);
3812-
StrCat(std::format("pub const {}: {} = {};", EnumeratorName(e), name,
3813-
std::string_view(init.data(), init.size())));
3813+
std::string value(init.data(), init.size());
3814+
if (is_bool) {
3815+
value = e->getInitVal().getBoolValue() ? "true" : "false";
3816+
}
3817+
StrCat(
3818+
std::format("pub const {}: {} = {};", EnumeratorName(e), name, value));
38143819
}
38153820
return false;
38163821
}

‎tests/unit/enum_int_interop.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ int classify_option(int option) {
5050

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

53+
enum Flag : bool { FLAG_OFF = false, FLAG_ON = true };
54+
5355
int main() {
5456
Color c = RED;
5557

@@ -140,5 +142,10 @@ int main() {
140142
assert(entries[2].color == BLUE);
141143
assert(entries[2].opt == OPT_C);
142144

145+
Flag flag = FLAG_ON;
146+
assert(flag);
147+
assert(!FLAG_OFF);
148+
flag = FLAG_OFF;
149+
assert(flag == FLAG_OFF);
143150
return 0;
144151
}

0 commit comments

Comments
 (0)