diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-09 23:05:20 +0100 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2017-01-09 23:06:08 +0100 |
commit | a20cf85dbb8b9f4ffcd656b3a541de24f4c37d95 (patch) | |
tree | 5e8aa9b5a20ebb4d37ab4b0cd185e048a93a1b40 /libbindgen | |
parent | b30fe53aec602c3ac506e1c89b5311aa08b4a548 (diff) |
ir: Evaluate constant strings too.
Signed-off-by: Emilio Cobos Álvarez <emilio@crisal.io>
Diffstat (limited to 'libbindgen')
-rw-r--r-- | libbindgen/src/clang.rs | 14 | ||||
-rw-r--r-- | libbindgen/src/ir/var.rs | 4 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/constant-evaluate.rs | 2 | ||||
-rw-r--r-- | libbindgen/tests/headers/constant-evaluate.h | 3 |
4 files changed, 22 insertions, 1 deletions
diff --git a/libbindgen/src/clang.rs b/libbindgen/src/clang.rs index fc5575b3..491aaa07 100644 --- a/libbindgen/src/clang.rs +++ b/libbindgen/src/clang.rs @@ -1348,6 +1348,20 @@ impl EvalResult { _ => None, } } + + /// Evaluates the expression as a literal string, that may or may not be + /// valid utf-8. + pub fn as_literal_string(&self) -> Option<Vec<u8>> { + match self.kind() { + CXEval_StrLiteral => { + let ret = unsafe { + CStr::from_ptr(clang_EvalResult_getAsStr(self.x)) + }; + Some(ret.to_bytes().to_vec()) + } + _ => None, + } + } } impl Drop for EvalResult { diff --git a/libbindgen/src/ir/var.rs b/libbindgen/src/ir/var.rs index d9160eca..e18af91b 100644 --- a/libbindgen/src/ir/var.rs +++ b/libbindgen/src/ir/var.rs @@ -236,7 +236,9 @@ impl ClangSubItemParser for Var { .and_then(|v| v.as_double()) .map(VarType::Float) } else { - None + cursor.evaluate() + .and_then(|v| v.as_literal_string()) + .map(VarType::String) }; let mangling = cursor_mangling(&cursor); diff --git a/libbindgen/tests/expectations/tests/constant-evaluate.rs b/libbindgen/tests/expectations/tests/constant-evaluate.rs index 6947be98..cdf097a2 100644 --- a/libbindgen/tests/expectations/tests/constant-evaluate.rs +++ b/libbindgen/tests/expectations/tests/constant-evaluate.rs @@ -16,3 +16,5 @@ pub const BAZ: ::std::os::raw::c_longlong = 24; pub const fuzz: f64 = 51.; pub const BAZZ: ::std::os::raw::c_char = 53; pub const WAT: ::std::os::raw::c_char = 0; +pub const bytestring: &'static [u8; 4usize] = b"Foo\x00"; +pub const NOT_UTF8: [u8; 5usize] = [240, 40, 140, 40, 0]; diff --git a/libbindgen/tests/headers/constant-evaluate.h b/libbindgen/tests/headers/constant-evaluate.h index b6a0492f..f9f1fa66 100644 --- a/libbindgen/tests/headers/constant-evaluate.h +++ b/libbindgen/tests/headers/constant-evaluate.h @@ -14,3 +14,6 @@ const long long BAZ = (1 << foo) | bar; const double fuzz = (1 + 50.0f); const char BAZZ = '5'; const char WAT = '\0'; + +const char* bytestring = "Foo"; +const char* NOT_UTF8 = "\xf0\x28\x8c\x28"; |