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

Error when using unicode character literals #2475

Open
paxbun opened this issue Apr 3, 2023 · 5 comments
Open

Error when using unicode character literals #2475

paxbun opened this issue Apr 3, 2023 · 5 comments

Comments

@paxbun
Copy link

paxbun commented Apr 3, 2023

bindgen panics when it encounters a character literal with a Unicode escape sequence representing a codepoint greater than 128.

Input C/C++ Header

#define OK 'a'
#define PANIC_CHAR_16 u'\uAC00'
#define PANIC_CHAR_32 U'\uAC00'

Bindgen Invocation

$ bindgen input.h -- -x c++
$ cargo run -p bindgen-cli -- input.h -- -x c++

Actual Results

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `3`,
 right: `1`', /Users/paxbun/.cargo/registry/src/github.com-1ecc6299db9ec823/bindgen-0.60.1/src/ir/var.rs:235:33
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::assert_failed_inner
   3: core::panicking::assert_failed
   4: <bindgen::ir::var::Var as bindgen::parse::ClangSubItemParser>::parse
   5: <bindgen::ir::item::Item as bindgen::parse::ClangItemParser>::parse
   6: bindgen::parse_one
   7: bindgen::clang::visit_children
   8: __ZN5clang8cxcursor13CursorVisitor5VisitE8CXCursorb
   9: __ZN5clang8cxcursor13CursorVisitor25visitPreprocessedEntitiesINS_19PreprocessingRecord8iteratorEEEbT_S5_RS3_NS_6FileIDE
  10: __ZN5clang8cxcursor13CursorVisitor13VisitChildrenE8CXCursor
  11: _clang_visitChildren
  12: clang_sys::clang_visitChildren
  13: bindgen::Builder::generate
  14: std::panicking::try
  15: bindgen::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Expected Results

/* automatically generated by rust-bindgen 0.64.0 */

pub const OK: u8 = 97u8;
pub const PANIC_CHAR_16: u16 = 44032; // u16 because its counterpart is char16_t
pub const PANIC_CHAR_32: u32 = 44032; // u32 similarly
@paxbun
Copy link
Author

paxbun commented Apr 3, 2023

We can prevent the error by modifying <binden::ir::var::Var as bindgen::parse::ClangSubItemParser>::parse in var.rs as follows:

let (type_kind, val) = match value {
    ...
    EvalResult::Char(c) => {
        let c = match c {
            CChar::Char(c) => c as u64,
            CChar::Raw(c) => c,
        };

        if c <= ::std::u8::MAX as u64 {
            (TypeKind::Int(IntKind::U8), VarType::Char(c as u8))
        } else {
            assert!(c <= ::std::u32::MAX as u64);
            (TypeKind::Int(IntKind::U32), VarType::Int(c as i64))
        }
    }
    ...
}

But this makes PANIC_CHAR_16 and PANIC_CHAR_32 all u32.

@paxbun
Copy link
Author

paxbun commented Apr 3, 2023

It seems like I have to modify cexpr to achieve my objectives, as CChar lacks enum variants to distinguish different character literals. If it seems okay, I would like to make a PR that adds variants for char8_t, char16_t, char32_t to cexpr::literals::CChar (which will possibly benefit jethrogb/rust-cexpr#25 as well), and then make a PR for this issue.

@pvdrz
Copy link
Contributor

pvdrz commented Apr 3, 2023

I suspect this could be fixed by #2369

@paxbun
Copy link
Author

paxbun commented Apr 4, 2023

I ran the modified version, but #2369 ignores PANIC_CHAR_16 and PANIC_CHAR_32. (Ignoring those macros is enough for me, though.) If #2369 is to be merged, then maybe I should make a PR for this to reitermarkus/cmacro-rs.

@paxbun
Copy link
Author

paxbun commented Apr 4, 2023

I posted an issue for this: reitermarkus/cmacro-rs#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants