Skip to content

Commit 4899cd9

Browse files
committed
String literals are mutable in C
1 parent 72d005f commit 4899cd9

3 files changed

Lines changed: 52 additions & 15 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,10 @@ std::string Converter::GetEscapedStringLiteral(clang::Expr *expr,
17071707

17081708
bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
17091709
StrCat(std::format("b{}.as_ptr()", GetEscapedStringLiteral(expr, true)));
1710+
// In C, string literals are char[], in C++ they are const char[]
1711+
if (!expr->getType().isConstQualified()) {
1712+
StrCat(".cast_mut()");
1713+
}
17101714
return false;
17111715
}
17121716

@@ -1726,27 +1730,27 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17261730
SetValueFreshness(type);
17271731
break;
17281732
}
1729-
case clang::CastKind::CK_ArrayToPointerDecay:
1730-
if (clang::isa<clang::StringLiteral>(sub_expr) ||
1731-
clang::isa<clang::PredefinedExpr>(sub_expr)) {
1732-
Convert(sub_expr);
1733-
if (IsConversionFromStringLiteralToCharPtr(expr)) {
1734-
StrCat(".cast_mut()");
1735-
}
1736-
return false;
1737-
}
1733+
case clang::CastKind::CK_ArrayToPointerDecay: {
17381734
// __va_list_tag [1] decays to __va_list_tag *. Just pass through by value
17391735
if (IsVaListType(sub_expr->getType())) {
17401736
Convert(sub_expr);
17411737
break;
17421738
}
17431739
Convert(sub_expr);
1744-
if (sub_expr->getType().isConstQualified()) {
1745-
StrCat(keyword_ptr_decay_const_);
1746-
} else {
1747-
StrCat(keyword_ptr_decay_);
1740+
switch (GetConstCastType(
1741+
expr->getType()->getPointeeType(),
1742+
sub_expr->getType()->getAsArrayTypeUnsafe()->getElementType())) {
1743+
case ConstCastType::MutableToConst:
1744+
StrCat(".cast_const()");
1745+
break;
1746+
case ConstCastType::ConstToMutable:
1747+
StrCat(".cast_mut()");
1748+
break;
1749+
default:
1750+
break;
17481751
}
17491752
break;
1753+
}
17501754
case clang::CastKind::CK_BitCast: {
17511755
PushParen paren(*this);
17521756
Convert(sub_expr);
@@ -1760,8 +1764,19 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
17601764
}
17611765
case clang::CastKind::CK_NoOp: {
17621766
Convert(sub_expr);
1763-
if (IsConversionFromStringLiteralToCharPtr(expr)) {
1764-
StrCat(".cast_mut()");
1767+
if (expr->getType()->isPointerType() &&
1768+
sub_expr->getType()->isPointerType()) {
1769+
switch (GetConstCastType(expr->getType()->getPointeeType(),
1770+
sub_expr->getType()->getPointeeType())) {
1771+
case ConstCastType::MutableToConst:
1772+
StrCat(".cast_const()");
1773+
break;
1774+
case ConstCastType::ConstToMutable:
1775+
StrCat(".cast_mut()");
1776+
break;
1777+
default:
1778+
break;
1779+
}
17651780
}
17661781
break;
17671782
}

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,4 +802,16 @@ std::string ReplaceAll(std::string str, std::string_view from,
802802
return str;
803803
}
804804

805+
ConstCastType GetConstCastType(clang::QualType to, clang::QualType from) {
806+
if (to.isConstQualified() && from.isConstQualified()) {
807+
return ConstCastType::ConstToConst;
808+
} else if (!to.isConstQualified() && from.isConstQualified()) {
809+
return ConstCastType::ConstToMutable;
810+
} else if (to.isConstQualified() && !from.isConstQualified()) {
811+
return ConstCastType::MutableToConst;
812+
} else {
813+
return ConstCastType::MutableToMutable;
814+
}
815+
}
816+
805817
} // namespace cpp2rust

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,14 @@ void Unwrap(std::string &s, std::string_view prefix, std::string_view suffix);
170170

171171
std::string ReplaceAll(std::string str, std::string_view from,
172172
std::string_view to);
173+
174+
enum class ConstCastType {
175+
ConstToConst,
176+
ConstToMutable,
177+
MutableToConst,
178+
MutableToMutable,
179+
};
180+
181+
ConstCastType GetConstCastType(clang::QualType to, clang::QualType from);
182+
173183
} // namespace cpp2rust

0 commit comments

Comments
 (0)