Skip to content

Commit 86592f0

Browse files
committed
edits
1 parent bda8fa9 commit 86592f0

2 files changed

Lines changed: 22 additions & 13 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,22 +2184,29 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
21842184

21852185
auto *decl = expr->getDecl();
21862186
if (ShouldReplaceWithMappedBody(expr)) {
2187-
return GetMappedAsString(expr);
2188-
} else if (auto *function = decl->getAsFunction()) {
2187+
if (auto str = GetMappedAsString(expr); !str.empty()) {
2188+
return str;
2189+
}
2190+
}
2191+
2192+
if (auto *function = decl->getAsFunction()) {
21892193
if (auto method = clang::dyn_cast<clang::CXXMethodDecl>(function)) {
21902194
if (method->isStatic()) {
21912195
return std::format("{}::{}", GetRecordName(method->getParent()),
21922196
GetNamedDeclAsString(method));
21932197
}
21942198
}
21952199
return GetNamedDeclAsString(function->getCanonicalDecl());
2196-
} else if (auto enum_constant =
2197-
clang::dyn_cast<clang::EnumConstantDecl>(decl)) {
2200+
}
2201+
2202+
if (auto enum_constant = clang::dyn_cast<clang::EnumConstantDecl>(decl)) {
21982203
return std::format("{}::{}",
21992204
GetRecordName(clang::dyn_cast<clang::EnumDecl>(
22002205
enum_constant->getDeclContext())),
22012206
std::string_view(enum_constant->getName()));
2202-
} else if (IsGlobalVar(expr)) {
2207+
}
2208+
2209+
if (IsGlobalVar(expr)) {
22032210
return ReplaceAll(Mapper::ToString(expr->getDecl()), "::", "_");
22042211
}
22052212

@@ -3657,7 +3664,7 @@ bool Converter::ShouldReplaceWithMappedBody(clang::DeclRefExpr *expr) const {
36573664
if (clang::isa<clang::FunctionDecl>(expr->getDecl()) && isAddrOf()) {
36583665
return false;
36593666
}
3660-
return Mapper::Contains(expr);
3667+
return true;
36613668
}
36623669

36633670
void Converter::SetFresh() {

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -631,15 +631,17 @@ bool ConverterRefCount::VisitConditionalOperator(
631631
bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
632632
if (isAddrOf()) {
633633
clang::Expr *addrof_op = ToAddrOf(ctx_, expr);
634-
if (Mapper::Contains(addrof_op)) {
635-
StrCat(GetMappedAsString(addrof_op));
634+
if (auto str = GetMappedAsString(addrof_op); !str.empty()) {
635+
StrCat(str);
636636
return false;
637637
}
638638
}
639639

640640
if (ShouldReplaceWithMappedBody(expr)) {
641-
StrCat(GetMappedAsString(expr));
642-
return false;
641+
if (auto str = GetMappedAsString(expr); !str.empty()) {
642+
StrCat(str);
643+
return false;
644+
}
643645
}
644646

645647
auto str = ConvertDeclRefExpr(expr);
@@ -723,7 +725,7 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
723725
static std::vector<const char *> printf2fmt(std::string &format) {
724726
std::vector<const char *> types;
725727
size_t pos = 0;
726-
while ((pos = format.find("%", pos)) != std::string::npos) {
728+
while ((pos = format.find('%', pos)) != std::string::npos) {
727729
if (pos + 1 >= format.size())
728730
break;
729731

@@ -1529,8 +1531,8 @@ std::string ConverterRefCount::ConvertStream(clang::Expr *expr) {
15291531
bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
15301532
PushConversionKind push(*this, ConversionKind::Unboxed);
15311533

1532-
if (Mapper::Contains(expr)) {
1533-
auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs());
1534+
if (auto str = GetMappedAsString(expr, expr->getArgs(), expr->getNumArgs());
1535+
!str.empty()) {
15341536
if (isAddrOf()) {
15351537
StrCat(std::format("Rc::new(RefCell::new({})).as_pointer()",
15361538
std::move(str)));

0 commit comments

Comments
 (0)