Skip to content

Commit e06aab1

Browse files
author
bors-servo
authored
Auto merge of #118 - fitzgen:deny-missing-docs, r=emilio
Add `#![deny(missing_docs)]` This commit adds the `#![deny(missing_docs)]` pragma, which causes compilation to fail if a public type or function is missing a documentation comment. It also adds missing documentation comments for public types and functions that were missing them. Fixes #94 r? @emilio
2 parents 25e45db + fcfb8e5 commit e06aab1

File tree

3 files changed

+118
-4
lines changed

3 files changed

+118
-4
lines changed

src/bin/bindgen.rs

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ Options:
9090

9191
// FIXME(emilio): Replace this with docopt if/when they fix their exponential
9292
// algorithm for argument parsing.
93+
//
94+
// FIXME(fitzgen): Switch from `BindgenOptions` to the non-deprecated `Builder`.
95+
#[allow(deprecated)]
9396
fn parse_args_or_exit(args: Vec<String>) -> (BindgenOptions, Box<io::Write>) {
9497
let mut options = BindgenOptions::default();
9598
let mut dest_file = None;

src/codegen/mod.rs

100644100755
File mode changed.

src/lib.rs

+115-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1+
//! Generate Rust bindings for C and C++ libraries.
2+
//!
3+
//! Provide a C/C++ header file, receive Rust FFI code to call into C/C++
4+
//! functions and use types defined in the header.
5+
//!
6+
//! See the [Builder](./struct.Builder.html) struct for usage.
7+
18
#![crate_name = "bindgen"]
29
#![crate_type = "dylib"]
310

411
#![cfg_attr(feature = "clippy", feature(plugin))]
512
#![cfg_attr(feature = "clippy", plugin(clippy))]
613

14+
#![deny(missing_docs)]
15+
16+
// We internally use the deprecated BindgenOptions all over the place. Once we
17+
// remove its `pub` declaration, we can un-deprecate it and remove this pragma.
18+
#![allow(deprecated)]
19+
720
// To avoid rather annoying warnings when matching with CXCursor_xxx as a
821
// constant.
922
#![allow(non_upper_case_globals)]
@@ -45,107 +58,189 @@ use ir::item::{Item, ItemId};
4558
use parse::{ClangItemParser, ParseError};
4659
use regex_set::RegexSet;
4760

61+
/// Configure and generate Rust bindings for a C/C++ header.
62+
///
63+
/// This is the main entry point to the library.
64+
///
65+
/// ```ignore
66+
/// use bindgen::builder;
67+
///
68+
/// // Configure and generate bindings.
69+
/// let bindings = try!(builder().header("path/to/input/header")
70+
/// .whitelisted_type("SomeCoolClass")
71+
/// .whitelisted_function("do_some_cool_thing")
72+
/// .generate());
73+
///
74+
/// // Write the generated bindings to an output file.
75+
/// try!(bindings.write_to_file("path/to/output.rs"));
76+
/// ```
4877
#[derive(Debug,Default)]
4978
pub struct Builder {
5079
options: BindgenOptions,
5180
}
5281

82+
/// Construct a new [`Builder`](./struct.Builder.html).
5383
pub fn builder() -> Builder {
5484
Default::default()
5585
}
5686

5787
impl Builder {
88+
/// Set the input C/C++ header.
5889
pub fn header<T: Into<String>>(self, header: T) -> Builder {
5990
self.clang_arg(header)
6091
}
6192

93+
/// Hide the given type from the generated bindings.
6294
pub fn hide_type<T: Into<String>>(mut self, arg: T) -> Builder {
6395
self.options.hidden_types.insert(arg.into());
6496
self
6597
}
6698

99+
/// Treat the given type as opaque in the generated bindings.
67100
pub fn opaque_type<T: Into<String>>(mut self, arg: T) -> Builder {
68101
self.options.opaque_types.insert(arg.into());
69102
self
70103
}
71104

105+
/// Whitelist the given type so that it (and all types that it transitively
106+
/// refers to) appears in the generated bindings.
72107
pub fn whitelisted_type<T: Borrow<str>>(mut self, arg: T) -> Builder {
73108
self.options.whitelisted_types.insert(&arg);
74109
self
75110
}
76111

112+
/// Whitelist the given function so that it (and all types that it
113+
/// transitively refers to) appears in the generated bindings.
77114
pub fn whitelisted_function<T: Borrow<str>>(mut self, arg: T) -> Builder {
78115
self.options.whitelisted_functions.insert(&arg);
79116
self
80117
}
81118

119+
/// Whitelist the given variable so that it (and all types that it
120+
/// transitively refers to) appears in the generated bindings.
82121
pub fn whitelisted_var<T: Borrow<str>>(mut self, arg: T) -> Builder {
83122
self.options.whitelisted_vars.insert(&arg);
84123
self
85124
}
86125

126+
/// Add a string to prepend to the generated bindings. The string is passed
127+
/// through without any modification.
87128
pub fn raw_line<T: Into<String>>(mut self, arg: T) -> Builder {
88129
self.options.raw_lines.push(arg.into());
89130
self
90131
}
91132

133+
/// Add an argument to be passed straight through to clang.
92134
pub fn clang_arg<T: Into<String>>(mut self, arg: T) -> Builder {
93135
self.options.clang_args.push(arg.into());
94136
self
95137
}
96138

139+
/// Make the generated bindings link the given shared library.
97140
pub fn link<T: Into<String>>(mut self, library: T) -> Builder {
98141
self.options.links.push((library.into(), LinkType::Default));
99142
self
100143
}
101144

145+
/// Make the generated bindings link the given static library.
102146
pub fn link_static<T: Into<String>>(mut self, library: T) -> Builder {
103147
self.options.links.push((library.into(), LinkType::Static));
104148
self
105149
}
106150

151+
/// Make the generated bindings link the given framework.
107152
pub fn link_framework<T: Into<String>>(mut self, library: T) -> Builder {
108153
self.options.links.push((library.into(), LinkType::Framework));
109154
self
110155
}
111156

157+
/// Emit bindings for builtin definitions (for example `__builtin_va_list`)
158+
/// in the generated Rust.
112159
pub fn emit_builtins(mut self) -> Builder {
113160
self.options.builtins = true;
114161
self
115162
}
116163

164+
/// Avoid generating any unstable Rust in the generated bindings.
117165
pub fn no_unstable_rust(mut self) -> Builder {
118166
self.options.unstable_rust = false;
119167
self
120168
}
121169

170+
/// Generate the Rust bindings using the options built up thus far.
122171
pub fn generate(self) -> Result<Bindings, ()> {
123172
Bindings::generate(self.options, None)
124173
}
125174
}
126175

127-
/// Deprecated - use a `Builder` instead
176+
/// Configuration options for generated bindings.
177+
///
178+
/// Deprecated: use a `Builder` instead.
128179
#[derive(Debug)]
180+
#[deprecated]
129181
pub struct BindgenOptions {
182+
/// The set of types that have been blacklisted and should not appear
183+
/// anywhere in the generated code.
130184
pub hidden_types: HashSet<String>,
185+
186+
/// The set of types that should be treated as opaque structures in the
187+
/// generated code.
131188
pub opaque_types: HashSet<String>,
189+
190+
/// The set of types that we should have bindings for in the generated
191+
/// code.
192+
///
193+
/// This includes all types transitively reachable from any type in this
194+
/// set. One might think of whitelisted types/vars/functions as GC roots,
195+
/// and the generated Rust code as including everything that gets marked.
132196
pub whitelisted_types: RegexSet,
197+
198+
/// Whitelisted functions. See docs for `whitelisted_types` for more.
133199
pub whitelisted_functions: RegexSet,
200+
201+
/// Whitelisted variables. See docs for `whitelisted_types` for more.
134202
pub whitelisted_vars: RegexSet,
203+
204+
/// Whether we should generate builtins or not.
135205
pub builtins: bool,
206+
207+
/// The set of libraries we should link in the generated Rust code.
136208
pub links: Vec<(String, LinkType)>,
209+
210+
/// True if we should dump the Clang AST for debugging purposes.
137211
pub emit_ast: bool,
212+
213+
/// True if we should ignore functions and only generate bindings for
214+
/// structures, types, and methods.
138215
pub ignore_functions: bool,
216+
217+
/// True if we should avoid generating bindings for methods, and instead
218+
/// just generate code for structures and types.
139219
pub ignore_methods: bool,
220+
221+
/// True if we should emulate C++ namespaces with Rust modules in the
222+
/// generated bindings.
140223
pub enable_cxx_namespaces: bool,
224+
225+
/// True if we shold derive Debug trait implementations for C/C++ structures
226+
/// and types.
141227
pub derive_debug: bool,
142-
/// Generate or not only stable rust.
228+
229+
/// True if we can use unstable Rust code in the bindings, false if we
230+
/// cannot.
143231
pub unstable_rust: bool,
144-
/// Wether to generate names that are **directly** under namespaces.
232+
233+
/// True if we should generate constant names that are **directly** under
234+
/// namespaces.
145235
pub namespaced_constants: bool,
146-
/// Whether to use msvc mangling rules
236+
237+
/// True if we should use MSVC name mangling rules.
147238
pub msvc_mangling: bool,
239+
240+
/// The set of raw lines to prepend to the generated Rust code.
148241
pub raw_lines: Vec<String>,
242+
243+
/// The set of arguments to pass straight through to Clang.
149244
pub clang_args: Vec<String>,
150245
}
151246

@@ -173,21 +268,31 @@ impl Default for BindgenOptions {
173268
}
174269
}
175270

271+
/// The linking type to use with a given library.
272+
///
273+
/// TODO: #104: This is ignored at the moment, but shouldn't be.
176274
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
177275
pub enum LinkType {
276+
/// Use shared library linking. This is the default.
178277
Default,
278+
/// Use static linking.
179279
Static,
280+
/// The library is an OSX framework.
180281
Framework
181282
}
182283

284+
/// Generated Rust bindings.
183285
#[derive(Debug, Clone)]
184286
pub struct Bindings {
185287
module: ast::Mod,
186288
raw_lines: Vec<String>,
187289
}
188290

189291
impl Bindings {
292+
/// Generate bindings for the given options.
293+
///
190294
/// Deprecated - use a `Builder` instead
295+
#[deprecated]
191296
pub fn generate(options: BindgenOptions, span: Option<Span>) -> Result<Bindings, ()> {
192297
let span = span.unwrap_or(DUMMY_SP);
193298

@@ -205,10 +310,12 @@ impl Bindings {
205310
})
206311
}
207312

313+
/// Convert these bindings into a Rust AST.
208314
pub fn into_ast(self) -> Vec<P<ast::Item>> {
209315
self.module.items
210316
}
211317

318+
/// Convert these bindings into source text (with raw lines prepended).
212319
pub fn to_string(&self) -> String {
213320
let mut mod_str = vec![];
214321
{
@@ -218,11 +325,13 @@ impl Bindings {
218325
String::from_utf8(mod_str).unwrap()
219326
}
220327

328+
/// Write these bindings as source text to a file.
221329
pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
222330
let file = try!(OpenOptions::new().write(true).truncate(true).create(true).open(path));
223331
self.write(Box::new(file))
224332
}
225333

334+
/// Write these bindings as source text to the given `Write`able.
226335
// https://github.com/Manishearth/rust-clippy/issues/740
227336
#[cfg_attr(feature = "clippy", allow(needless_lifetimes))]
228337
pub fn write<'a>(&self, mut writer: Box<Write + 'a>) -> io::Result<()> {
@@ -255,6 +364,7 @@ fn filter_builtins(ctx: &BindgenContext, cursor: &clang::Cursor) -> bool {
255364
}
256365
}
257366

367+
/// Parse one `Item` from the Clang cursor.
258368
pub fn parse_one(ctx: &mut BindgenContext,
259369
cursor: clang::Cursor,
260370
parent: Option<ItemId>,
@@ -274,6 +384,7 @@ pub fn parse_one(ctx: &mut BindgenContext,
274384
CXChildVisit_Continue
275385
}
276386

387+
/// Parse the Clang AST into our `Item` internal representation.
277388
fn parse(context: &mut BindgenContext) {
278389
use clang::Diagnostic;
279390
use clangll::*;

0 commit comments

Comments
 (0)