Add option to force explicit extern ABI's

This commit is contained in:
Marcus Klaas 2016-04-18 18:39:40 +02:00
parent 68f04cec37
commit f364a7ec43
6 changed files with 40 additions and 6 deletions

View File

@ -328,6 +328,7 @@ create_config! {
"Maximum width of the args of a function call before falling back to vertical formatting";
struct_lit_width: usize, 16,
"Maximum width in the body of a struct lit before falling back to vertical formatting";
force_explicit_abi: bool, true, "Always print the abi for extern items";
newline_style: NewlineStyle, NewlineStyle::Unix, "Unix or Windows line endings";
fn_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for functions";
item_brace_style: BraceStyle, BraceStyle::SameLineWhere, "Brace style for structs and enums";

View File

@ -80,7 +80,8 @@ impl Rewrite for ast::Local {
impl<'a> FmtVisitor<'a> {
pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
self.buffer.push_str(&::utils::format_abi(fm.abi));
let abi_str = ::utils::format_abi(fm.abi, self.config.force_explicit_abi);
self.buffer.push_str(&abi_str);
let snippet = self.snippet(span);
let brace_pos = snippet.find_uncommented("{").unwrap();
@ -1265,7 +1266,7 @@ fn rewrite_fn_base(context: &RewriteContext,
result.push_str(::utils::format_unsafety(unsafety));
if abi != abi::Abi::Rust {
result.push_str(&::utils::format_abi(abi));
result.push_str(&::utils::format_abi(abi, context.config.force_explicit_abi));
}
// fn foo

View File

@ -608,7 +608,7 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
result.push_str(&::utils::format_unsafety(bare_fn.unsafety));
if bare_fn.abi != abi::Abi::Rust {
result.push_str(&::utils::format_abi(bare_fn.abi));
result.push_str(&::utils::format_abi(bare_fn.abi, context.config.force_explicit_abi));
}
result.push_str("fn");

View File

@ -91,9 +91,12 @@ pub fn format_mutability(mutability: ast::Mutability) -> &'static str {
}
#[inline]
// FIXME(#451): include "C"?
pub fn format_abi(abi: abi::Abi) -> String {
format!("extern {} ", abi)
pub fn format_abi(abi: abi::Abi, explicit_abi: bool) -> String {
if abi == abi::Abi::C && !explicit_abi {
"extern ".into()
} else {
format!("extern {} ", abi)
}
}
// The width of the first line in s.

View File

@ -0,0 +1,14 @@
// rustfmt-force_explicit_abi: false
extern "C" {
fn some_fn() -> ();
}
extern "C" fn sup() {
}
type funky_func = extern "C" fn (unsafe extern "rust-call" fn(*const JSJitInfo, *mut JSContext,
HandleObject, *mut libc::c_void, u32,
*mut JSVal)
-> u8);

View File

@ -0,0 +1,15 @@
// rustfmt-force_explicit_abi: false
extern {
fn some_fn() -> ();
}
extern fn sup() {}
type funky_func = extern fn(unsafe extern "rust-call" fn(*const JSJitInfo,
*mut JSContext,
HandleObject,
*mut libc::c_void,
u32,
*mut JSVal)
-> u8);