Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support large integer constants #490

Merged
merged 3 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ impl Literal {
other_code => format!(r"L'\U{:08X}'", other_code),
})),
syn::Lit::Int(ref value) => {
Ok(Literal::Expr(value.base10_digits().to_string()))
if value.base10_parse::<i64>().is_err() {
Ok(Literal::Expr(format!("{}ULL", value.base10_digits())))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, what if it is negative? Do we need just LL instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question - per the C/C++ spec (https://en.cppreference.com/w/cpp/language/integer_literal, the Notes section):

There are no negative integer literals. Expressions such as -1 apply the unary minus operator to the value represented by the literal, which may involve implicit type conversions.

So to export a constant that has the value of i64::min_value(), the ULL suffix is needed because the magnitude of the value is too large to fit into a signed 64-bit representation. I added this to the constant_big.rs test case.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, that makes sense. Thanks for the extra test!

} else {
Ok(Literal::Expr(value.base10_digits().to_string()))
}
}
syn::Lit::Float(ref value) => {
Ok(Literal::Expr(value.base10_digits().to_string()))
Expand Down
8 changes: 8 additions & 0 deletions tests/expectations/both/constant_big.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define DOESNT_NEED_ULL_SUFFIX 8070450532247928832

#define NEEDS_ULL_SUFFIX 9223372036854775808ULL
8 changes: 8 additions & 0 deletions tests/expectations/both/constant_big.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define DOESNT_NEED_ULL_SUFFIX 8070450532247928832

#define NEEDS_ULL_SUFFIX 9223372036854775808ULL
8 changes: 8 additions & 0 deletions tests/expectations/constant_big.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define DOESNT_NEED_ULL_SUFFIX 8070450532247928832

#define NEEDS_ULL_SUFFIX 9223372036854775808ULL
8 changes: 8 additions & 0 deletions tests/expectations/constant_big.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define DOESNT_NEED_ULL_SUFFIX 8070450532247928832

#define NEEDS_ULL_SUFFIX 9223372036854775808ULL
8 changes: 8 additions & 0 deletions tests/expectations/constant_big.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <new>

static const uint64_t DOESNT_NEED_ULL_SUFFIX = 8070450532247928832;

static const uint64_t NEEDS_ULL_SUFFIX = 9223372036854775808ULL;
8 changes: 8 additions & 0 deletions tests/expectations/tag/constant_big.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define DOESNT_NEED_ULL_SUFFIX 8070450532247928832

#define NEEDS_ULL_SUFFIX 9223372036854775808ULL
8 changes: 8 additions & 0 deletions tests/expectations/tag/constant_big.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define DOESNT_NEED_ULL_SUFFIX 8070450532247928832

#define NEEDS_ULL_SUFFIX 9223372036854775808ULL
2 changes: 2 additions & 0 deletions tests/rust/constant_big.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const NEEDS_ULL_SUFFIX: u64 = 0x8000_0000_0000_0000;
pub const DOESNT_NEED_ULL_SUFFIX: u64 = 0x7000_0000_0000_0000;