Skip to content

Commit 90da3da

Browse files
committed
Fix 0 initialization for c_char
1 parent 5b2a10c commit 90da3da

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,14 @@ Converter::ConvertCallExpr(clang::CallExpr *expr) {
17981798
return std::nullopt;
17991799
}
18001800

1801+
static std::string getTypedLiteral(const char *num, std::string_view type) {
1802+
if (type.contains("::")) {
1803+
// Not a builtin type
1804+
return std::format("({} as {})", num, type);
1805+
}
1806+
return std::format("{}_{}", num, type);
1807+
}
1808+
18011809
std::string Converter::getIntegerLiteral(clang::IntegerLiteral *expr,
18021810
bool incl_type,
18031811
const clang::QualType *type) {
@@ -1819,7 +1827,7 @@ std::string Converter::getIntegerLiteral(clang::IntegerLiteral *expr,
18191827
return init;
18201828
}
18211829
}
1822-
return std::format("{}_{}", num_as_string.c_str(), type_as_string);
1830+
return getTypedLiteral(num_as_string.c_str(), type_as_string);
18231831
}
18241832

18251833
return static_cast<std::string>(num_as_string);
@@ -3385,11 +3393,11 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) {
33853393
}
33863394

33873395
if (qual_type->isIntegerType() && !qual_type->isEnumeralType()) {
3388-
return std::format("0_{}", ToString(qual_type));
3396+
return getTypedLiteral("0", ToString(qual_type));
33893397
}
33903398

33913399
if (qual_type->isFloatingType()) {
3392-
return std::format("0.0_{}", ToString(qual_type));
3400+
return getTypedLiteral("0.0", ToString(qual_type));
33933401
}
33943402

33953403
if (auto record = qual_type->getAsRecordDecl();

0 commit comments

Comments
 (0)