Skip to content

Keep radix for integer literals in generated bindings #3237

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions bindgen-tests/tests/expectations/tests/class_static_const.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions bindgen-tests/tests/expectations/tests/different_radix_literals.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bindgen-tests/tests/expectations/tests/layout_eth_conf.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bindgen-tests/tests/expectations/tests/overflowed_enum.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions bindgen-tests/tests/expectations/tests/short-enums.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bindgen-tests/tests/expectations/tests/wrap-static-fns.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions bindgen-tests/tests/headers/different_radix_literals.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// bindgen-flags: -- -std=c++14
// (C23 is not available in clang 9.0, but C++14 supports the same literals)

// Binary integer literals (C23) - 0b10 is 2 in decimal

#define DEFINE_BIN_LITERAL 0b10
#define DEFINE_NEG_BIN_LITERAL -0b10
const int CONST_INT_BIN_LITERAL = 0b10;
const int CONST_INT_NEG_BIN_LITERAL = -0b10;

// Octal integer literals - 010 is 8 in decimal

#define DEFINE_OCT_LITERAL 010
#define DEFINE_NEG_OCT_LITERAL -010
const int CONST_INT_OCT_LITERAL = 010;
const int CONST_INT_NEG_OCT_LITERAL = -010;

// Hexadecimal integer literals - 0x10 is 16 in decimal

#define DEFINE_HEX_LITERAL 0x10
#define DEFINE_NEG_HEX_LITERAL -0x10
const int CONST_INT_HEX_LITERAL = 0x10;
const int CONST_INT_NEG_HEX_LITERAL = -0x10;

// Default decimal integer literals - 10 is 10 in decimal

#define DEFINE_DEC_LITERAL 10
#define DEFINE_NEG_DEC_LITERAL -10
const int CONST_INT_DEC_LITERAL = 10;
const int CONST_INT_NEG_DEC_LITERAL = -10;

// Enums with binary, octal, and hexadecimal integer literals

enum MultiRadixLiteral {
ENUM_BIN_LITERAL = 0b10,
ENUM_NEG_BIN_LITERAL = -0b10,
ENUM_OCT_LITERAL = 010,
ENUM_NEG_OCT_LITERAL = -010,
ENUM_HEX_LITERAL = 0x10,
ENUM_NEG_HEX_LITERAL = -0x10,
ENUM_DEC_LITERAL = 10,
ENUM_NEG_DEC_LITERAL = -10,
};

// Edge cases: minimum i64s

const long long MIN_I64_BIN = -0b1000000000000000000000000000000000000000000000000000000000000000;
const long long MIN_I64_OCT = -01000000000000000000000;
const long long MIN_I64_DEC = -9223372036854775808;
const long long MIN_I64_HEX = -0x8000000000000000;

// Big B or big X

const int BIG_B_BIN = 0B1;
const int BIG_X_HEX = 0XF;
15 changes: 15 additions & 0 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#![deny(clippy::missing_docs_in_private_items)]

use crate::ir::context::BindgenContext;
use crate::ir::var::LiteralRadix;
use clang_sys::*;
use std::cmp;

Expand Down Expand Up @@ -973,6 +974,20 @@ impl Cursor {
pub(crate) fn is_inline_namespace(&self) -> bool {
unsafe { clang_Cursor_isInlineNamespace(self.x) != 0 }
}

/// Obtain the number base (radix) of a literal definition corresponding to the cursor.
///
/// Returns `None` if unable to infer a base.
pub(crate) fn get_literal_radix(&self) -> Option<LiteralRadix> {
self.cexpr_tokens()
.iter()
.filter(|cexpr_token| {
cexpr_token.kind == cexpr::token::Kind::Literal
})
.find_map(|lit_tok| {
LiteralRadix::from_literal_token_raw(&lit_tok.raw)
})
}
}

/// A struct that owns the tokenizer result from a given cursor.
Expand Down
Loading