Skip to content

Commit 5ffc475

Browse files
Tai Sassen-LiangTai Sassen-Liang
Tai Sassen-Liang
authored and
Tai Sassen-Liang
committed
Do not print builtin macro definitions in ast_dump
Resolves issue rust-lang#476. Moves out logic for checking whether a cursor has a filename (previously used exclusively in private function lib::filter_builtins) into the actual cursor. This code is then used to check whether a cursor is a builtin when dumping the AST.
1 parent c85fa24 commit 5ffc475

File tree

5 files changed

+54
-17
lines changed

5 files changed

+54
-17
lines changed

src/clang.rs

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ impl Cursor {
7676
}
7777
}
7878

79+
/// Returns whether the cursor refers to a built-in definition.
80+
pub fn is_builtin(&self) -> bool {
81+
let (file, _, _, _) = self.location().location();
82+
!file.name().is_some()
83+
}
84+
7985
/// Get the `Cursor` for this cursor's referent's lexical parent.
8086
///
8187
/// The lexical parent is the parent of the definition. The semantic parent
@@ -1415,6 +1421,9 @@ pub fn ast_dump(c: &Cursor, depth: isize) -> CXChildVisitResult {
14151421
}
14161422

14171423
fn print_cursor<S: AsRef<str>>(depth: isize, prefix: S, c: &Cursor) {
1424+
if c.is_builtin() {
1425+
return;
1426+
}
14181427
let prefix = prefix.as_ref();
14191428
print_indent(depth,
14201429
format!(" {}kind = {}", prefix, kind_to_str(c.kind())));

src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -826,12 +826,7 @@ impl<'ctx> Bindings<'ctx> {
826826
/// Determines whether the given cursor is in any of the files matched by the
827827
/// options.
828828
fn filter_builtins(ctx: &BindgenContext, cursor: &clang::Cursor) -> bool {
829-
let (file, _, _, _) = cursor.location().location();
830-
831-
match file.name() {
832-
None => ctx.options().builtins,
833-
Some(..) => true,
834-
}
829+
!cursor.is_builtin() || ctx.options().builtins
835830
}
836831

837832
/// Parse one `Item` from the Clang cursor.

tests/expectations/tests/auto.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
pub struct Foo {
1010
pub _address: u8,
1111
}
12-
pub const Foo_kFoo: bool = true;
12+
extern "C" {
13+
#[link_name = "_ZN3Foo4kFooE"]
14+
pub static Foo_kFoo: bool;
15+
}
1316
#[test]
1417
fn bindgen_test_layout_Foo() {
1518
assert_eq!(::std::mem::size_of::<Foo>() , 1usize , concat ! (

tests/expectations/tests/const_bool.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
#![allow(non_snake_case)]
55

66

7-
pub const k: bool = true;
7+
extern "C" {
8+
#[link_name = "_ZL1k"]
9+
pub static k: bool;
10+
}
811
#[repr(C)]
912
#[derive(Debug, Default, Copy)]
1013
pub struct A {
1114
pub _address: u8,
1215
}
13-
pub const A_k: bool = false;
16+
extern "C" {
17+
#[link_name = "_ZN1A1kE"]
18+
pub static A_k: bool;
19+
}
1420
#[test]
1521
fn bindgen_test_layout_A() {
1622
assert_eq!(::std::mem::size_of::<A>() , 1usize , concat ! (
@@ -22,4 +28,7 @@ impl Clone for A {
2228
fn clone(&self) -> Self { *self }
2329
}
2430
pub type foo = bool;
25-
pub const k2: foo = true;
31+
extern "C" {
32+
#[link_name = "_ZL2k2"]
33+
pub static k2: foo;
34+
}

tests/expectations/tests/constant-evaluate.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,31 @@ pub const bar: _bindgen_ty_1 = _bindgen_ty_1::bar;
1111
pub enum _bindgen_ty_1 { foo = 4, bar = 8, }
1212
pub type EasyToOverflow = ::std::os::raw::c_ulonglong;
1313
pub const k: EasyToOverflow = 2147483648;
14-
pub const k_expr: EasyToOverflow = 0;
15-
pub const BAZ: ::std::os::raw::c_longlong = 24;
16-
pub const fuzz: f64 = 51.;
17-
pub const BAZZ: ::std::os::raw::c_schar = 53;
18-
pub const WAT: ::std::os::raw::c_schar = 0;
19-
pub const bytestring: &'static [u8; 4usize] = b"Foo\x00";
20-
pub const NOT_UTF8: [u8; 5usize] = [240, 40, 140, 40, 0];
14+
extern "C" {
15+
#[link_name = "k_expr"]
16+
pub static k_expr: EasyToOverflow;
17+
}
18+
extern "C" {
19+
#[link_name = "BAZ"]
20+
pub static BAZ: ::std::os::raw::c_longlong;
21+
}
22+
extern "C" {
23+
#[link_name = "fuzz"]
24+
pub static fuzz: f64;
25+
}
26+
extern "C" {
27+
#[link_name = "BAZZ"]
28+
pub static BAZZ: ::std::os::raw::c_char;
29+
}
30+
extern "C" {
31+
#[link_name = "WAT"]
32+
pub static WAT: ::std::os::raw::c_char;
33+
}
34+
extern "C" {
35+
#[link_name = "bytestring"]
36+
pub static mut bytestring: *const ::std::os::raw::c_char;
37+
}
38+
extern "C" {
39+
#[link_name = "NOT_UTF8"]
40+
pub static mut NOT_UTF8: *const ::std::os::raw::c_char;
41+
}

0 commit comments

Comments
 (0)