Skip to content

Commit 75aeca4

Browse files
committed
Merge pull request #950 from marcusklaas/extern-abi
Add option to force explicit extern ABI's
2 parents 68f04ce + f364a7e commit 75aeca4

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

src/config.rs

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

src/items.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ impl Rewrite for ast::Local {
8080

8181
impl<'a> FmtVisitor<'a> {
8282
pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
83-
self.buffer.push_str(&::utils::format_abi(fm.abi));
83+
let abi_str = ::utils::format_abi(fm.abi, self.config.force_explicit_abi);
84+
self.buffer.push_str(&abi_str);
8485

8586
let snippet = self.snippet(span);
8687
let brace_pos = snippet.find_uncommented("{").unwrap();
@@ -1265,7 +1266,7 @@ fn rewrite_fn_base(context: &RewriteContext,
12651266
result.push_str(::utils::format_unsafety(unsafety));
12661267

12671268
if abi != abi::Abi::Rust {
1268-
result.push_str(&::utils::format_abi(abi));
1269+
result.push_str(&::utils::format_abi(abi, context.config.force_explicit_abi));
12691270
}
12701271

12711272
// fn foo

src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
608608
result.push_str(&::utils::format_unsafety(bare_fn.unsafety));
609609

610610
if bare_fn.abi != abi::Abi::Rust {
611-
result.push_str(&::utils::format_abi(bare_fn.abi));
611+
result.push_str(&::utils::format_abi(bare_fn.abi, context.config.force_explicit_abi));
612612
}
613613

614614
result.push_str("fn");

src/utils.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ pub fn format_mutability(mutability: ast::Mutability) -> &'static str {
9191
}
9292

9393
#[inline]
94-
// FIXME(#451): include "C"?
95-
pub fn format_abi(abi: abi::Abi) -> String {
96-
format!("extern {} ", abi)
94+
pub fn format_abi(abi: abi::Abi, explicit_abi: bool) -> String {
95+
if abi == abi::Abi::C && !explicit_abi {
96+
"extern ".into()
97+
} else {
98+
format!("extern {} ", abi)
99+
}
97100
}
98101

99102
// The width of the first line in s.

tests/source/extern_not_explicit.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-force_explicit_abi: false
2+
3+
extern "C" {
4+
fn some_fn() -> ();
5+
}
6+
7+
extern "C" fn sup() {
8+
9+
}
10+
11+
type funky_func = extern "C" fn (unsafe extern "rust-call" fn(*const JSJitInfo, *mut JSContext,
12+
HandleObject, *mut libc::c_void, u32,
13+
*mut JSVal)
14+
-> u8);

tests/target/extern_not_explicit.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-force_explicit_abi: false
2+
3+
extern {
4+
fn some_fn() -> ();
5+
}
6+
7+
extern fn sup() {}
8+
9+
type funky_func = extern fn(unsafe extern "rust-call" fn(*const JSJitInfo,
10+
*mut JSContext,
11+
HandleObject,
12+
*mut libc::c_void,
13+
u32,
14+
*mut JSVal)
15+
-> u8);

0 commit comments

Comments
 (0)