Skip to content

Commit a5cd911

Browse files
committed
Fix signed printing of integers
1 parent 2bb3063 commit a5cd911

4 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,8 +2533,11 @@ bool Converter::VisitStringLiteral(clang::StringLiteral *expr) {
25332533
}
25342534
if (expr->getString().contains('\0')) {
25352535
std::string out = "(&[";
2536-
for (unsigned char c : expr->getString()) {
2537-
out += getTypedLiteral(std::to_string(c).c_str(), CharRustType()) + ", ";
2536+
for (char c : expr->getString()) {
2537+
auto value = CharRustTypeIsSigned()
2538+
? std::to_string(static_cast<signed char>(c))
2539+
: std::to_string(static_cast<unsigned char>(c));
2540+
out += getTypedLiteral(value.c_str(), CharRustType()) + ", ";
25382541
}
25392542
out += getTypedLiteral("0", CharRustType()) + "])";
25402543
StrCat(out);

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,15 @@ llvm::APSInt GetIntegerLiteralValue(const clang::ASTContext &ctx,
319319
const clang::QualType *type,
320320
bool char_is_signed) {
321321
auto value = expr->getValue();
322+
auto target = type ? *type : expr->getType();
322323
bool is_signed = false;
323-
if (type && (*type)->isBuiltinType() && (*type)->isIntegerType() &&
324-
!(*type)->isBooleanType()) {
325-
value = value.zextOrTrunc(ctx.getIntWidth(*type));
326-
is_signed =
327-
(*type)->isSpecificBuiltinType(clang::BuiltinType::Char_S) ||
328-
(*type)->isSpecificBuiltinType(clang::BuiltinType::Char_U)
329-
? char_is_signed
330-
: (*type)->isSignedIntegerType();
324+
if (target->isBuiltinType() && target->isIntegerType() &&
325+
!target->isBooleanType()) {
326+
value = value.zextOrTrunc(ctx.getIntWidth(target));
327+
is_signed = target->isSpecificBuiltinType(clang::BuiltinType::Char_S) ||
328+
target->isSpecificBuiltinType(clang::BuiltinType::Char_U)
329+
? char_is_signed
330+
: target->isSignedIntegerType();
331331
}
332332
return llvm::APSInt(value, !is_signed);
333333
}

‎tests/unit/size_t_template_param.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ template <class T> struct RefCtor {
1616
RefCtor(const T &x) : v(x + 1) {}
1717
};
1818

19+
template <long long N> static long long neg_ll() { return N; }
20+
21+
template <char C> static char neg_char() { return C; }
22+
23+
template <int N> static int neg_int() { return N; }
24+
1925
int main() {
2026
size_t a1[] = {1, 2, 3};
2127
assert(array_ref(a1) == 3);
@@ -28,5 +34,9 @@ int main() {
2834
size_t v1 = 6;
2935
RefCtor<size_t> rc(v1);
3036
assert(rc.v == 7);
37+
38+
assert(neg_ll<-9000000>() == -9000000);
39+
assert(neg_char<'\x80'>() == '\x80');
40+
assert(neg_int<-1>() == -1);
3141
return 0;
3242
}

‎tests/unit/string_literal_interior_null.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ int main() {
1818
assert(a == b);
1919
assert(a == 1);
2020

21+
int high = sum_bytes("\xc2\x00\x41", 3);
22+
assert(high == 0xc2 + 0x41);
23+
2124
int c = "\r\n.\r\n"[0] + "\r\n.\r\n"[3];
2225
assert(c == '\r' + '\r');
2326

0 commit comments

Comments
 (0)