Skip to content
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

Add option to force explicit extern ABI's #950

Merged
merged 1 commit into from Apr 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config.rs
Expand Up @@ -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";
Expand Down
5 changes: 3 additions & 2 deletions src/items.rs
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Expand Up @@ -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");
Expand Down
9 changes: 6 additions & 3 deletions src/utils.rs
Expand Up @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions tests/source/extern_not_explicit.rs
@@ -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);
15 changes: 15 additions & 0 deletions tests/target/extern_not_explicit.rs
@@ -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);