Skip to content

Commit dcc21c1

Browse files
authored
Don't generate wrong strings. (#2487)
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.
1 parent f03d4f4 commit dcc21c1

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,15 @@
186186
* The documentation of the generated `type` aliases now matches the comments
187187
of their `typedef` counterparts instead of using the comments of the aliased
188188
types.
189+
189190
## Removed
190191
* The following deprecated flags were removed: `--use-msvc-mangling`,
191192
`--rustfmt-bindings` and `--size_t-is-usize`.
192193
* The `--no-rustfmt-bindings` flag was removed in favor of `--formatter=none`.
193194
* The `Bindings::emit_warnings` and `Bindings::warnings` methods were removed
194195
in favor of `--emit-diagnostics`.
196+
* Bindgen no longer generates C string constants that cannot be represented as
197+
byte slices.
195198

196199
## Fixed
197200

bindgen/clang.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,7 @@ pub(crate) fn extract_clang_version() -> String {
21042104
#[derive(Debug)]
21052105
pub(crate) struct EvalResult {
21062106
x: CXEvalResult,
2107+
ty: Type,
21072108
}
21082109

21092110
impl EvalResult {
@@ -2131,6 +2132,7 @@ impl EvalResult {
21312132
}
21322133
Some(EvalResult {
21332134
x: unsafe { clang_Cursor_Evaluate(cursor.x) },
2135+
ty: cursor.cur_type().canonical_type(),
21342136
})
21352137
}
21362138

@@ -2177,13 +2179,22 @@ impl EvalResult {
21772179
/// Evaluates the expression as a literal string, that may or may not be
21782180
/// valid utf-8.
21792181
pub(crate) fn as_literal_string(&self) -> Option<Vec<u8>> {
2180-
match self.kind() {
2181-
CXEval_StrLiteral => {
2182+
if self.kind() != CXEval_StrLiteral {
2183+
return None;
2184+
}
2185+
2186+
let char_ty = self.ty.pointee_type().or_else(|| self.ty.elem_type())?;
2187+
match char_ty.kind() {
2188+
CXType_Char_S | CXType_SChar | CXType_Char_U | CXType_UChar => {
21822189
let ret = unsafe {
21832190
CStr::from_ptr(clang_EvalResult_getAsStr(self.x))
21842191
};
21852192
Some(ret.to_bytes().to_vec())
21862193
}
2194+
// FIXME: Support generating these.
2195+
CXType_Char16 => None,
2196+
CXType_Char32 => None,
2197+
CXType_WChar => None,
21872198
_ => None,
21882199
}
21892200
}

0 commit comments

Comments
 (0)