-
Notifications
You must be signed in to change notification settings - Fork 743
Parse macro expressions. #219
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//! A public API for more fine-grained customization of bindgen behavior. | ||
|
||
pub use ir::int::IntKind; | ||
use std::fmt; | ||
|
||
/// A trait to allow configuring different kinds of types in different | ||
/// situations. | ||
pub trait TypeChooser: fmt::Debug { | ||
/// The integer kind an integer macro should have, given a name and the | ||
/// value of that macro, or `None` if you want the default to be chosen. | ||
fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> { | ||
None | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
//! Common context that is passed around during parsing and codegen. | ||
|
||
use BindgenOptions; | ||
use cexpr; | ||
use clang::{self, Cursor}; | ||
use parse::ClangItemParser; | ||
use std::borrow::{Borrow, Cow}; | ||
use std::collections::{HashMap, HashSet, hash_map}; | ||
use std::borrow::Cow; | ||
use std::collections::{HashMap, hash_map}; | ||
use std::collections::btree_map::{self, BTreeMap}; | ||
use std::fmt; | ||
use super::int::IntKind; | ||
|
@@ -77,8 +78,9 @@ pub struct BindgenContext<'ctx> { | |
pub currently_parsed_types: Vec<(Cursor, ItemId)>, | ||
|
||
/// A HashSet with all the already parsed macro names. This is done to avoid | ||
/// hard errors while parsing duplicated macros. | ||
parsed_macros: HashSet<String>, | ||
/// hard errors while parsing duplicated macros, as well to allow macro | ||
/// expression parsing. | ||
parsed_macros: HashMap<Vec<u8>, cexpr::expr::EvalResult>, | ||
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. Why did this become 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. It's what |
||
|
||
/// The active replacements collected from replaces="xxx" annotations. | ||
replacements: HashMap<String, ItemId>, | ||
|
@@ -243,7 +245,7 @@ impl<'ctx> BindgenContext<'ctx> { | |
|
||
/// Returns a mangled name as a rust identifier. | ||
pub fn rust_ident_raw(&self, name: &str) -> Ident { | ||
self.ext_cx().ident_of(name.borrow()) | ||
self.ext_cx().ident_of(name) | ||
} | ||
|
||
/// Iterate over all items that have been defined. | ||
|
@@ -715,14 +717,21 @@ impl<'ctx> BindgenContext<'ctx> { | |
} | ||
|
||
/// Have we parsed the macro named `macro_name` already? | ||
pub fn parsed_macro(&self, macro_name: &str) -> bool { | ||
self.parsed_macros.contains(macro_name) | ||
pub fn parsed_macro(&self, macro_name: &[u8]) -> bool { | ||
self.parsed_macros.contains_key(macro_name) | ||
} | ||
|
||
/// Get the currently parsed macros. | ||
pub fn parsed_macros(&self) -> &HashMap<Vec<u8>, cexpr::expr::EvalResult> { | ||
debug_assert!(!self.in_codegen_phase()); | ||
&self.parsed_macros | ||
} | ||
|
||
/// Mark the macro named `macro_name` as parsed. | ||
pub fn note_parsed_macro(&mut self, macro_name: String) { | ||
debug_assert!(!self.parsed_macros.contains(¯o_name)); | ||
self.parsed_macros.insert(macro_name); | ||
pub fn note_parsed_macro(&mut self, | ||
id: Vec<u8>, | ||
value: cexpr::expr::EvalResult) { | ||
self.parsed_macros.insert(id, value); | ||
} | ||
|
||
/// Are we in the codegen phase? | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not just 3.9, I've observed this in 3.5 through 3.8. The problem is that the cursor extent is always off by one (even on EOF). You don't get an extra token on EOF, because, well, you're at EOF.
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.
That's really unfortunate :(
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.
After inspecting a bit clang's source, the source range for macro expansions comes from deep in the lexer. That's more that what I want to chew in right now inside LLVM internals, so I'll open a bug for it.
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.
Wasn't that hard actually.
LLVM bug (open since 2011): https://llvm.org/bugs/show_bug.cgi?id=9069
Patch: https://reviews.llvm.org/D26446
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.
Fixed in trunk :)
llvm-mirror/clang@3b61b92