Skip to content

Commit 9db550f

Browse files
committed
Add RAII semantics for curr_init_type_
1 parent dc53477 commit 9db550f

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
@@ -2973,9 +2973,8 @@ void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
29732973
if (auto *lambda = clang::dyn_cast<clang::LambdaExpr>(
29742974
expr->IgnoreUnlessSpelledInSource())) {
29752975
PushExprKind push(*this, ExprKind::AddrOf);
2976-
curr_init_type_.push(qual_type);
2976+
PushInitType init_type(*this, qual_type);
29772977
VisitLambdaExpr(lambda);
2978-
curr_init_type_.pop();
29792978
return;
29802979
}
29812980
}
@@ -2992,21 +2991,18 @@ void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
29922991
{
29932992
PushParen paren(*this);
29942993
StrCat(token::kStar);
2995-
curr_init_type_.push(qual_type);
2994+
PushInitType init_type(*this, qual_type);
29962995
Convert(expr);
2997-
curr_init_type_.pop();
29982996
}
29992997
StrCat(".clone()");
30002998
} else if (IsReferenceType(expr) || qual_type->isFunctionPointerType()) {
30012999
PushExprKind push(*this, ExprKind::AddrOf);
3002-
curr_init_type_.push(qual_type);
3000+
PushInitType init_type(*this, qual_type);
30033001
Convert(expr);
3004-
curr_init_type_.pop();
30053002
} else {
30063003
PushExprKind push(*this, ExprKind::RValue);
3007-
curr_init_type_.push(qual_type);
3004+
PushInitType init_type(*this, qual_type);
30083005
Convert(expr);
3009-
curr_init_type_.pop();
30103006
}
30113007
if (qual_type->isReferenceType() && !IsReferenceType(expr)) {
30123008
StrCat(keyword::kAs);
@@ -3080,9 +3076,11 @@ void Converter::ConvertArraySubscript(clang::Expr *base, clang::Expr *idx,
30803076

30813077
void Converter::ConvertAssignment(clang::Expr *lhs, clang::Expr *rhs,
30823078
std::string_view assign_operator) {
3083-
curr_init_type_.push(lhs->getType());
3084-
auto lhs_as_string = ConvertLValue(lhs);
3085-
curr_init_type_.pop();
3079+
std::string lhs_as_string;
3080+
{
3081+
PushInitType init_type(*this, lhs->getType());
3082+
lhs_as_string = ConvertLValue(lhs);
3083+
}
30863084
auto rhs_as_string = ConvertFreshRValue(rhs);
30873085

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

‎cpp2rust/converter/converter.h‎

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

525+
class PushInitType {
526+
public:
527+
PushInitType(Converter &c, clang::QualType type) : c_(c) {
528+
c_.curr_init_type_.push(type);
529+
}
530+
~PushInitType() { c_.curr_init_type_.pop(); }
531+
PushInitType(const PushInitType &) = delete;
532+
PushInitType &operator=(const PushInitType &) = delete;
533+
534+
private:
535+
Converter &c_;
536+
};
537+
525538
std::unordered_set<const clang::VarDecl *> map_iter_decls_;
526539

527540
struct ScopedMapIterDecl {

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,7 @@ void ConverterRefCount::ConvertVarInit(clang::QualType qual_type,
16681668
bool is_ref = qual_type->isReferenceType();
16691669
in_function_formals_ = true;
16701670
PushConversionKind push(*this, ConversionKind::Unboxed, is_ref);
1671+
PushInitType init_type(*this, qual_type);
16711672
StrCat(BoxValue((is_ref || qual_type->isFunctionPointerType())
16721673
? ConvertFreshPointer(expr)
16731674
: ConvertFreshRValue(expr)));

0 commit comments

Comments
 (0)