Skip to content

Commit

Permalink
Don't generate wrong strings. (#2487)
Browse files Browse the repository at this point in the history
A header with

```cpp
#include <stddef.h>

const char S1[] = "Hello";
const unsigned char S2[] = u8"Hello";
const char16_t S3[] = u"Hello";
const char32_t S4[] = U"Hello";
const wchar_t S5[] = L"Hello";
```

will wrongly generate (`-- -std=c++11`)


```rust
pub const S1: &[u8; 6usize] = b"Hello\0";
pub const S2: &[u8; 6usize] = b"Hello\0";
pub const S3: &[u8; 2usize] = b"H\0";
pub const S4: &[u8; 2usize] = b"H\0";
pub const S5: &[u8; 2usize] = b"H\0";
```

since `clang_EvalResult_getAsStr` only works for ordinary and UTF-8 strings. This seems like a `libclang` limitation since there isn't a similar function for other string literal types.

This disables generating code for unsupported string types.
  • Loading branch information
reitermarkus authored Apr 10, 2023
1 parent f03d4f4 commit dcc21c1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,15 @@
* The documentation of the generated `type` aliases now matches the comments
of their `typedef` counterparts instead of using the comments of the aliased
types.

## Removed
* The following deprecated flags were removed: `--use-msvc-mangling`,
`--rustfmt-bindings` and `--size_t-is-usize`.
* The `--no-rustfmt-bindings` flag was removed in favor of `--formatter=none`.
* The `Bindings::emit_warnings` and `Bindings::warnings` methods were removed
in favor of `--emit-diagnostics`.
* Bindgen no longer generates C string constants that cannot be represented as
byte slices.

## Fixed

Expand Down
15 changes: 13 additions & 2 deletions bindgen/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,6 +2104,7 @@ pub(crate) fn extract_clang_version() -> String {
#[derive(Debug)]
pub(crate) struct EvalResult {
x: CXEvalResult,
ty: Type,
}

impl EvalResult {
Expand Down Expand Up @@ -2131,6 +2132,7 @@ impl EvalResult {
}
Some(EvalResult {
x: unsafe { clang_Cursor_Evaluate(cursor.x) },
ty: cursor.cur_type().canonical_type(),
})
}

Expand Down Expand Up @@ -2177,13 +2179,22 @@ impl EvalResult {
/// Evaluates the expression as a literal string, that may or may not be
/// valid utf-8.
pub(crate) fn as_literal_string(&self) -> Option<Vec<u8>> {
match self.kind() {
CXEval_StrLiteral => {
if self.kind() != CXEval_StrLiteral {
return None;
}

let char_ty = self.ty.pointee_type().or_else(|| self.ty.elem_type())?;
match char_ty.kind() {
CXType_Char_S | CXType_SChar | CXType_Char_U | CXType_UChar => {
let ret = unsafe {
CStr::from_ptr(clang_EvalResult_getAsStr(self.x))
};
Some(ret.to_bytes().to_vec())
}
// FIXME: Support generating these.
CXType_Char16 => None,
CXType_Char32 => None,
CXType_WChar => None,
_ => None,
}
}
Expand Down

0 comments on commit dcc21c1

Please sign in to comment.