Skip to content

Commit d5cd806

Browse files
committed
Retry parsing with unsigned integers if signed parsing fails
- Don't try to add 'u' if it's already there
1 parent 1eaed5c commit d5cd806

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

src/clang.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,9 @@ impl ClangToken {
785785
pub fn as_swcc_token(
786786
&self,
787787
) -> Option<saltwater::Locatable<saltwater::Token>> {
788-
use saltwater::{Files, Lexer, Literal, Token};
788+
use saltwater::{
789+
error::LexError, Files, Lexer, Literal, Locatable, Token,
790+
};
789791

790792
match self.kind {
791793
// `saltwater` does not have a comment token
@@ -794,13 +796,40 @@ impl ClangToken {
794796
CXToken_Keyword => {
795797
let spelling = std::str::from_utf8(self.spelling())
796798
.expect("invalid utf8 in token");
797-
let mut files = Files::new();
798-
let id = files.add("", "".into());
799-
let mut lexer = Lexer::new(id, spelling, false);
800-
let mut token = lexer
801-
.next()
802-
.unwrap()
803-
.expect("saltwater failed to parse clang token");
799+
let parse = |spelling| {
800+
let mut files = Files::new();
801+
let id = files.add("", "".into());
802+
let mut lexer = Lexer::new(id, spelling, false);
803+
lexer.next().unwrap()
804+
};
805+
let failed_parse = |err: Locatable<_>| {
806+
panic!(
807+
"saltwater failed to parse clang token '{}': {}",
808+
spelling, err.data
809+
);
810+
};
811+
let mut token = match parse(spelling) {
812+
Ok(token) => token,
813+
Err(Locatable {
814+
data:
815+
LexError::IntegerOverflow {
816+
is_signed: Some(true),
817+
},
818+
..
819+
}) => {
820+
warn!("integer does not fit into `long long`, trying again with `unsigned long long`");
821+
// saltwater ignores trailing `LL`, but requires any `u` suffix to come before `LL`
822+
let mut spelling = String::from(
823+
spelling
824+
.trim_end_matches('l')
825+
.trim_end_matches('L'),
826+
);
827+
spelling.push('u');
828+
parse(&spelling).unwrap_or_else(failed_parse)
829+
}
830+
Err(err) => failed_parse(err),
831+
};
832+
804833
// saltwater generates null-terminated string immediately,
805834
// but bindgen only adds the null-terminator during codegen.
806835
if let Token::Literal(Literal::Str(ref mut string)) =

tests/expectations/tests/jsval_layout_opaque.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,21 @@ where
9292
}
9393
}
9494
}
95+
pub const JSVAL_ALIGNMENT: u32 = 0;
9596
pub const JSVAL_TAG_SHIFT: u32 = 47;
9697
pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327;
9798
pub const JSVAL_TAG_MASK: i64 = -140737488355328;
99+
pub const JSVAL_TYPE_TO_TAG: u32 = 0;
100+
pub const JSVAL_LOWER_INCL_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
101+
pub const JSVAL_UPPER_EXCL_TAG_OF_PRIMITIVE_SET: u32 = 0;
102+
pub const JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET: u32 = 0;
103+
pub const JSVAL_LOWER_INCL_TAG_OF_GCTHING_SET: u32 = 0;
104+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
105+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_PRIMITIVE_SET: u32 = 0;
106+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_NUMBER_SET: u32 = 0;
107+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_GCTHING_SET: u32 = 0;
108+
pub const JS_VALUE_CONSTEXPR: u32 = 0;
109+
pub const JS_VALUE_CONSTEXPR_VAR: u32 = 0;
98110
pub type size_t = ::std::os::raw::c_ulonglong;
99111
#[repr(u8)]
100112
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

tests/expectations/tests/jsval_layout_opaque_1_0.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,21 @@ impl<T> ::std::cmp::PartialEq for __BindgenUnionField<T> {
135135
}
136136
}
137137
impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
138+
pub const JSVAL_ALIGNMENT: u32 = 0;
138139
pub const JSVAL_TAG_SHIFT: u32 = 47;
139140
pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327;
140141
pub const JSVAL_TAG_MASK: i64 = -140737488355328;
142+
pub const JSVAL_TYPE_TO_TAG: u32 = 0;
143+
pub const JSVAL_LOWER_INCL_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
144+
pub const JSVAL_UPPER_EXCL_TAG_OF_PRIMITIVE_SET: u32 = 0;
145+
pub const JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET: u32 = 0;
146+
pub const JSVAL_LOWER_INCL_TAG_OF_GCTHING_SET: u32 = 0;
147+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
148+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_PRIMITIVE_SET: u32 = 0;
149+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_NUMBER_SET: u32 = 0;
150+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_GCTHING_SET: u32 = 0;
151+
pub const JS_VALUE_CONSTEXPR: u32 = 0;
152+
pub const JS_VALUE_CONSTEXPR_VAR: u32 = 0;
141153
pub type size_t = ::std::os::raw::c_ulonglong;
142154
#[repr(u8)]
143155
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)