Skip to content

Commit 2dbc338

Browse files
committed
Add RAII semantics for curr_init_type_
1 parent 2d73dfc commit 2dbc338

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2946,9 +2946,8 @@ void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
29462946
if (auto *lambda = clang::dyn_cast<clang::LambdaExpr>(
29472947
expr->IgnoreUnlessSpelledInSource())) {
29482948
PushExprKind push(*this, ExprKind::AddrOf);
2949-
curr_init_type_.push(qual_type);
2949+
PushInitType init_type(*this, qual_type);
29502950
VisitLambdaExpr(lambda);
2951-
curr_init_type_.pop();
29522951
return;
29532952
}
29542953
}
@@ -2965,21 +2964,18 @@ void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
29652964
{
29662965
PushParen paren(*this);
29672966
StrCat(token::kStar);
2968-
curr_init_type_.push(qual_type);
2967+
PushInitType init_type(*this, qual_type);
29692968
Convert(expr);
2970-
curr_init_type_.pop();
29712969
}
29722970
StrCat(".clone()");
29732971
} else if (IsReferenceType(expr) || qual_type->isFunctionPointerType()) {
29742972
PushExprKind push(*this, ExprKind::AddrOf);
2975-
curr_init_type_.push(qual_type);
2973+
PushInitType init_type(*this, qual_type);
29762974
Convert(expr);
2977-
curr_init_type_.pop();
29782975
} else {
29792976
PushExprKind push(*this, ExprKind::RValue);
2980-
curr_init_type_.push(qual_type);
2977+
PushInitType init_type(*this, qual_type);
29812978
Convert(expr);
2982-
curr_init_type_.pop();
29832979
}
29842980
if (qual_type->isReferenceType() && !IsReferenceType(expr)) {
29852981
StrCat(keyword::kAs);
@@ -3053,9 +3049,11 @@ void Converter::ConvertArraySubscript(clang::Expr *base, clang::Expr *idx,
30533049

30543050
void Converter::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs,
30553051
std::string_view assign_operator) {
3056-
curr_init_type_.push(lhs->getType());
3057-
auto lhs_as_string = ConvertLValue(lhs);
3058-
curr_init_type_.pop();
3052+
std::string lhs_as_string;
3053+
{
3054+
PushInitType init_type(*this, lhs->getType());
3055+
lhs_as_string = ConvertLValue(lhs);
3056+
}
30593057
auto rhs_as_string = ConvertFreshRValue(rhs);
30603058

30613059
PushBrace brace(*this, !isVoid());

cpp2rust/converter/converter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,19 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
518518
std::stack<BreakTarget> &stack_;
519519
};
520520

521+
class PushInitType {
522+
public:
523+
PushInitType(Converter &c, clang::QualType type) : c_(c) {
524+
c_.curr_init_type_.push(type);
525+
}
526+
~PushInitType() { c_.curr_init_type_.pop(); }
527+
PushInitType(const PushInitType &) = delete;
528+
PushInitType &operator=(const PushInitType &) = delete;
529+
530+
private:
531+
Converter &c_;
532+
};
533+
521534
std::unordered_set<const clang::VarDecl *> map_iter_decls_;
522535

523536
struct ScopedMapIterDecl {

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,7 @@ void ConverterRefCount::ConvertVarInit(clang::QualType qual_type,
16641664
bool is_ref = qual_type->isReferenceType();
16651665
in_function_formals_ = true;
16661666
PushConversionKind push(*this, ConversionKind::Unboxed, is_ref);
1667+
PushInitType init_type(*this, qual_type);
16671668
StrCat(BoxValue((is_ref || qual_type->isFunctionPointerType())
16681669
? ConvertFreshPointer(expr)
16691670
: ConvertFreshRValue(expr)));

0 commit comments

Comments
 (0)