-
Notifications
You must be signed in to change notification settings - Fork 778
Precise macro value type #3323
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
base: main
Are you sure you want to change the base?
Precise macro value type #3323
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // bindgen-flags: --macro-const-use-ctypes | ||
|
|
||
| #ifndef ISSUE_923_H | ||
| #define ISSUE_923_H | ||
|
|
||
| #define ULONG_ZERO 0UL | ||
| #define ULONGLONG_ZERO 0ULL | ||
| #define CHAR_ZERO ((char)'\0') | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2329,7 +2329,8 @@ impl EvalResult { | |
| }) | ||
| } | ||
|
|
||
| fn kind(&self) -> CXEvalResultKind { | ||
| /// Return the kind of the evaluation result. | ||
| pub(crate) fn kind(&self) -> CXEvalResultKind { | ||
| unsafe { clang_EvalResult_getKind(self.x) } | ||
| } | ||
|
|
||
|
|
@@ -2370,6 +2371,29 @@ impl EvalResult { | |
| Some(value as i64) | ||
| } | ||
|
|
||
| /// Try to resolve the result into a string literal. | ||
| /// This returns `None` if the result is not immediately a string literal. | ||
| pub(crate) fn as_str_literal(&self) -> Option<Vec<u8>> { | ||
| if !matches!( | ||
| self.kind(), | ||
| CXEval_StrLiteral | CXEval_CFStr | CXEval_ObjCStrLiteral, | ||
| ) { | ||
| return None; | ||
| } | ||
| // Safety: we are only copying the content, not assuming a borrow. | ||
| // TODO(@dingxiangfei2009): LLVM Libclang does not return the true size | ||
| // of a string literal, which could be truncated due to a null character | ||
| // '\0' in the middle. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, hopefully this is rare, but maybe file an upstream issue and reference it here? |
||
| let value = | ||
| unsafe { CStr::from_ptr(clang_EvalResult_getAsStr(self.x)) }; | ||
| Some(value.to_bytes().into()) | ||
| } | ||
|
|
||
| /// Return the type of the value. | ||
| pub(crate) fn value_type(&self) -> Type { | ||
| self.ty | ||
| } | ||
|
|
||
| /// 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>> { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -353,7 +353,7 @@ pub(crate) struct BindgenContext { | |
| /// hard errors while parsing duplicated macros, as well to allow macro | ||
| /// expression parsing. | ||
| /// | ||
| /// This needs to be an `std::HashMap` because the `cexpr` API requires it. | ||
| /// This needs to be an `std::HashMap` because the [`cexpr`] API requires it. | ||
| parsed_macros: StdHashMap<Vec<u8>, cexpr::expr::EvalResult>, | ||
|
|
||
| /// A map with all include locations. | ||
|
|
@@ -2055,10 +2055,12 @@ If you encounter an error missing from this list, please file an issue or a PR!" | |
| let mut header_names_to_compile = Vec::new(); | ||
| let mut header_paths = Vec::new(); | ||
| let mut header_includes = Vec::new(); | ||
| let single_header = self.options().input_headers.last().cloned()?; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change seems drive-by, right? Seems fine tho. |
||
| for input_header in &self.options.input_headers | ||
| [..self.options.input_headers.len() - 1] | ||
| { | ||
| let [input_headers @ .., single_header] = | ||
| &self.options().input_headers[..] | ||
| else { | ||
| return None; | ||
| }; | ||
| for input_header in input_headers { | ||
| let path = Path::new(input_header.as_ref()); | ||
| if let Some(header_path) = path.parent() { | ||
| if header_path == Path::new("") { | ||
|
|
@@ -2095,7 +2097,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" | |
| } | ||
| let mut tu = clang::TranslationUnit::parse( | ||
| &index, | ||
| &single_header, | ||
| single_header, | ||
| &c_args, | ||
| &[], | ||
| clang_sys::CXTranslationUnit_ForSerialization, | ||
|
|
@@ -2129,6 +2131,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" | |
| } | ||
|
|
||
| /// Get the currently parsed macros. | ||
| /// This map only contains macros accepted by [`cexpr`] | ||
| pub(crate) fn parsed_macros( | ||
| &self, | ||
| ) -> &StdHashMap<Vec<u8>, cexpr::expr::EvalResult> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no unsafe here tho, right?
This seems fine but might be worth a comment like "this is to prevent tests running in parallel from clobbering each other's results".