Add more documentation style options (#305)

* Ignoring IDE files.

* Addresses issue #302, also amends #59 insofar that vanilla C-style now does not prefix individual lines with `*` anymore.

* Removed Javadoc reference.

* Renamed `Doxylight` to `Doxy` and changed C default to that.

* Added documentation.

* Changed enum name and applied `fmt`.

* Fixed comment.

* Fixed match.
This commit is contained in:
Ralf Biedert 2019-03-24 20:56:17 +01:00 committed by Emilio Cobos Álvarez
parent e14f782743
commit 51aff0a119
20 changed files with 206 additions and 14 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
target target
.idea
.vscode

View File

@ -20,7 +20,7 @@ This project can be used to generate C bindings for Rust code. It is currently b
* Support for generating `#ifdef`'s for `#[cfg]` attributes * Support for generating `#ifdef`'s for `#[cfg]` attributes
* Support for `#[repr(sized)]` tagged enum's * Support for `#[repr(sized)]` tagged enum's
## Installation ## Installation
``` ```
cargo install cbindgen cargo install cbindgen
@ -104,6 +104,10 @@ tab_width = 2
language = "[C|C++]" language = "[C|C++]"
# A rule to use to select style of declaration in C, tagname vs typedef # A rule to use to select style of declaration in C, tagname vs typedef
style = "[Both|Type|Tag]" style = "[Both|Type|Tag]"
# How the generated documentation should be commented.
# C uses /* */; C++ uses //; Doxy is like C but with leading * per line.
documentation_style = "[C, C++, Doxy]"
[defines] [defines]
# A rule for generating `#ifdef`s for matching `#[cfg]`ed items, # A rule for generating `#ifdef`s for matching `#[cfg]`ed items,

View File

@ -100,6 +100,32 @@ impl FromStr for Layout {
deserialize_enum_str!(Layout); deserialize_enum_str!(Layout);
/// How the comments containing documentation should be styled.
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum DocumentationStyle {
C,
Doxy,
Cxx,
Auto,
}
impl FromStr for DocumentationStyle {
type Err = String;
fn from_str(s: &str) -> Result<DocumentationStyle, Self::Err> {
match s.to_lowercase().as_ref() {
"c" => Ok(DocumentationStyle::C),
"cxx" => Ok(DocumentationStyle::Cxx),
"c++" => Ok(DocumentationStyle::Cxx),
"doxy" => Ok(DocumentationStyle::Doxy),
"auto" => Ok(DocumentationStyle::Auto),
_ => Err(format!("Unrecognized documentation style: '{}'.", s)),
}
}
}
deserialize_enum_str!(DocumentationStyle);
/// A style of Style to use when generating structs and enums. /// A style of Style to use when generating structs and enums.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum Style { pub enum Style {
@ -597,6 +623,8 @@ pub struct Config {
pub defines: HashMap<String, String>, pub defines: HashMap<String, String>,
/// Include doc comments from rust as documentation /// Include doc comments from rust as documentation
pub documentation: bool, pub documentation: bool,
/// How documentation comments should be styled.
pub documentation_style: DocumentationStyle,
} }
impl Default for Config { impl Default for Config {
@ -626,6 +654,7 @@ impl Default for Config {
constant: ConstantConfig::default(), constant: ConstantConfig::default(),
defines: HashMap::new(), defines: HashMap::new(),
documentation: true, documentation: true,
documentation_style: DocumentationStyle::Auto,
} }
} }
} }

View File

@ -6,7 +6,7 @@ use std::io::Write;
use syn; use syn;
use bindgen::config::{Config, Language}; use bindgen::config::{Config, DocumentationStyle, Language};
use bindgen::utilities::SynAttributeHelpers; use bindgen::utilities::SynAttributeHelpers;
use bindgen::writer::{Source, SourceWriter}; use bindgen::writer::{Source, SourceWriter};
@ -39,25 +39,57 @@ impl Source for Documentation {
return; return;
} }
if config.language == Language::C { let style = match config.documentation_style {
out.write("/**"); DocumentationStyle::Auto if config.language == Language::C => DocumentationStyle::Doxy,
out.new_line(); DocumentationStyle::Auto if config.language == Language::Cxx => DocumentationStyle::Cxx,
} DocumentationStyle::Auto => DocumentationStyle::C, // Fallback if `Language` gets extended.
for line in &self.doc_comment { other => other,
if config.language != Language::C { };
out.write("///");
} else { // Following these documents for style conventions:
out.write(" *"); // https://en.wikibooks.org/wiki/C++_Programming/Code/Style_Conventions/Comments
// https://www.cs.cmu.edu/~410/doc/doxygen.html
match style {
DocumentationStyle::C => {
out.write("/*");
out.new_line();
} }
DocumentationStyle::Doxy => {
out.write("/**");
out.new_line();
}
_ => (),
}
for line in &self.doc_comment {
match style {
DocumentationStyle::C => out.write(""),
DocumentationStyle::Doxy => out.write(" *"),
DocumentationStyle::Cxx => out.write("///"),
DocumentationStyle::Auto => unreachable!(), // Auto case should always be covered
}
if line.len() != 0 { if line.len() != 0 {
out.write(" "); out.write(" ");
} }
write!(out, "{}", line); write!(out, "{}", line);
out.new_line(); out.new_line();
} }
if config.language == Language::C {
out.write(" */"); match style {
out.new_line(); DocumentationStyle::C => {
out.write(" */");
out.new_line();
}
DocumentationStyle::Doxy => {
out.write(" */");
out.new_line();
}
_ => (),
} }
} }
} }

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,10 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
extern "C" {
/// The root of all evil.
void root();
} // extern "C"

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,12 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
extern "C" {
/**
* The root of all evil.
*/
void root();
} // extern "C"

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,12 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
extern "C" {
/**
* The root of all evil.
*/
void root();
} // extern "C"

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,9 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
/**
* The root of all evil.
*/
void root(void);

View File

@ -0,0 +1,4 @@
/// The root of all evil.
#[no_mangle]
pub extern "C" fn root() {
}

View File

@ -0,0 +1 @@
documentation_style = "auto"

View File

@ -0,0 +1,4 @@
/// The root of all evil.
#[no_mangle]
pub extern "C" fn root() {
}

View File

@ -0,0 +1 @@
documentation_style = "doxy"