Skip to content

Commit fd26731

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 63b4a22 commit fd26731

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
@@ -808,7 +808,9 @@ impl ClangToken {
808808
pub fn as_swcc_token(
809809
&self,
810810
) -> Option<saltwater::Locatable<saltwater::Token>> {
811-
use saltwater::{Files, Lexer, Literal, Token};
811+
use saltwater::{
812+
error::LexError, Files, Lexer, Literal, Locatable, Token,
813+
};
812814

813815
match self.kind {
814816
// `saltwater` does not have a comment token
@@ -817,13 +819,40 @@ impl ClangToken {
817819
CXToken_Keyword => {
818820
let spelling = std::str::from_utf8(self.spelling())
819821
.expect("invalid utf8 in token");
820-
let mut files = Files::new();
821-
let id = files.add("", "".into());
822-
let mut lexer = Lexer::new(id, spelling, false);
823-
let mut token = lexer
824-
.next()
825-
.unwrap()
826-
.expect("saltwater failed to parse clang token");
822+
let parse = |spelling| {
823+
let mut files = Files::new();
824+
let id = files.add("", "".into());
825+
let mut lexer = Lexer::new(id, spelling, false);
826+
lexer.next().unwrap()
827+
};
828+
let failed_parse = |err: Locatable<_>| {
829+
panic!(
830+
"saltwater failed to parse clang token '{}': {}",
831+
spelling, err.data
832+
);
833+
};
834+
let mut token = match parse(spelling) {
835+
Ok(token) => token,
836+
Err(Locatable {
837+
data:
838+
LexError::IntegerOverflow {
839+
is_signed: Some(true),
840+
},
841+
..
842+
}) => {
843+
warn!("integer does not fit into `long long`, trying again with `unsigned long long`");
844+
// saltwater ignores trailing `LL`, but requires any `u` suffix to come before `LL`
845+
let mut spelling = String::from(
846+
spelling
847+
.trim_end_matches('l')
848+
.trim_end_matches('L'),
849+
);
850+
spelling.push('u');
851+
parse(&spelling).unwrap_or_else(failed_parse)
852+
}
853+
Err(err) => failed_parse(err),
854+
};
855+
827856
// saltwater generates null-terminated string immediately,
828857
// but bindgen only adds the null-terminator during codegen.
829858
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
@@ -94,9 +94,21 @@ where
9494
}
9595
}
9696
}
97+
pub const JSVAL_ALIGNMENT: u32 = 0;
9798
pub const JSVAL_TAG_SHIFT: u32 = 47;
9899
pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327;
99100
pub const JSVAL_TAG_MASK: i64 = -140737488355328;
101+
pub const JSVAL_TYPE_TO_TAG: u32 = 0;
102+
pub const JSVAL_LOWER_INCL_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
103+
pub const JSVAL_UPPER_EXCL_TAG_OF_PRIMITIVE_SET: u32 = 0;
104+
pub const JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET: u32 = 0;
105+
pub const JSVAL_LOWER_INCL_TAG_OF_GCTHING_SET: u32 = 0;
106+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
107+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_PRIMITIVE_SET: u32 = 0;
108+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_NUMBER_SET: u32 = 0;
109+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_GCTHING_SET: u32 = 0;
110+
pub const JS_VALUE_CONSTEXPR: u32 = 0;
111+
pub const JS_VALUE_CONSTEXPR_VAR: u32 = 0;
100112
pub type size_t = ::std::os::raw::c_ulonglong;
101113
#[repr(u8)]
102114
#[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
@@ -137,9 +137,21 @@ impl<T> ::std::cmp::PartialEq for __BindgenUnionField<T> {
137137
}
138138
}
139139
impl<T> ::std::cmp::Eq for __BindgenUnionField<T> {}
140+
pub const JSVAL_ALIGNMENT: u32 = 0;
140141
pub const JSVAL_TAG_SHIFT: u32 = 47;
141142
pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327;
142143
pub const JSVAL_TAG_MASK: i64 = -140737488355328;
144+
pub const JSVAL_TYPE_TO_TAG: u32 = 0;
145+
pub const JSVAL_LOWER_INCL_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
146+
pub const JSVAL_UPPER_EXCL_TAG_OF_PRIMITIVE_SET: u32 = 0;
147+
pub const JSVAL_UPPER_INCL_TAG_OF_NUMBER_SET: u32 = 0;
148+
pub const JSVAL_LOWER_INCL_TAG_OF_GCTHING_SET: u32 = 0;
149+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_OBJ_OR_NULL_SET: u32 = 0;
150+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_PRIMITIVE_SET: u32 = 0;
151+
pub const JSVAL_UPPER_EXCL_SHIFTED_TAG_OF_NUMBER_SET: u32 = 0;
152+
pub const JSVAL_LOWER_INCL_SHIFTED_TAG_OF_GCTHING_SET: u32 = 0;
153+
pub const JS_VALUE_CONSTEXPR: u32 = 0;
154+
pub const JS_VALUE_CONSTEXPR_VAR: u32 = 0;
143155
pub type size_t = ::std::os::raw::c_ulonglong;
144156
#[repr(u8)]
145157
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)