Skip to content

Commit d0ecf0c

Browse files
authored
Merge pull request #458 from rust-lang/format-code
Format the code
2 parents 6ec5010 + c2c68e3 commit d0ecf0c

27 files changed

+3748
-2633
lines changed

.github/workflows/ci.yml

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ jobs:
4949
# `llvm-14-tools` is needed to install the `FileCheck` binary which is used for asm tests.
5050
run: sudo apt-get install ninja-build ripgrep llvm-14-tools
5151

52+
- name: Install rustfmt
53+
run: rustup component add rustfmt
54+
5255
- name: Download artifact
5356
run: curl -LO https://github.com/antoyo/gcc/releases/latest/download/${{ matrix.libgccjit_version.gcc }}
5457

@@ -92,6 +95,9 @@ jobs:
9295
run: |
9396
./y.sh test --release --clean --build-sysroot ${{ matrix.commands }}
9497
98+
- name: Check formatting
99+
run: cargo fmt -- --check
100+
95101
duplicates:
96102
runs-on: ubuntu-latest
97103
steps:

.rustfmt.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ignore = ["/src", "/tests"]
1+
use_small_heuristics = "Max"

src/abi.rs

+55-52
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@ impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
1818
fn get_param(&mut self, index: usize) -> Self::Value {
1919
let func = self.current_func();
2020
let param = func.get_param(index as i32);
21-
let on_stack =
22-
if let Some(on_stack_param_indices) = self.on_stack_function_params.borrow().get(&func) {
23-
on_stack_param_indices.contains(&index)
24-
}
25-
else {
26-
false
27-
};
21+
let on_stack = if let Some(on_stack_param_indices) =
22+
self.on_stack_function_params.borrow().get(&func)
23+
{
24+
on_stack_param_indices.contains(&index)
25+
} else {
26+
false
27+
};
2828
if on_stack {
2929
param.to_lvalue().get_address(None)
30-
}
31-
else {
30+
} else {
3231
param.to_rvalue()
3332
}
3433
}
@@ -37,13 +36,14 @@ impl<'a, 'gcc, 'tcx> AbiBuilderMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
3736
impl GccType for CastTarget {
3837
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
3938
let rest_gcc_unit = self.rest.unit.gcc_type(cx);
40-
let (rest_count, rem_bytes) =
41-
if self.rest.unit.size.bytes() == 0 {
42-
(0, 0)
43-
}
44-
else {
45-
(self.rest.total.bytes() / self.rest.unit.size.bytes(), self.rest.total.bytes() % self.rest.unit.size.bytes())
46-
};
39+
let (rest_count, rem_bytes) = if self.rest.unit.size.bytes() == 0 {
40+
(0, 0)
41+
} else {
42+
(
43+
self.rest.total.bytes() / self.rest.unit.size.bytes(),
44+
self.rest.total.bytes() % self.rest.unit.size.bytes(),
45+
)
46+
};
4747

4848
if self.prefix.iter().all(|x| x.is_none()) {
4949
// Simplify to a single unit when there is no prefix and size <= unit size
@@ -61,9 +61,7 @@ impl GccType for CastTarget {
6161
let mut args: Vec<_> = self
6262
.prefix
6363
.iter()
64-
.flat_map(|option_reg| {
65-
option_reg.map(|reg| reg.gcc_type(cx))
66-
})
64+
.flat_map(|option_reg| option_reg.map(|reg| reg.gcc_type(cx)))
6765
.chain((0..rest_count).map(|_| rest_gcc_unit))
6866
.collect();
6967

@@ -86,12 +84,10 @@ impl GccType for Reg {
8684
fn gcc_type<'gcc>(&self, cx: &CodegenCx<'gcc, '_>) -> Type<'gcc> {
8785
match self.kind {
8886
RegKind::Integer => cx.type_ix(self.size.bits()),
89-
RegKind::Float => {
90-
match self.size.bits() {
91-
32 => cx.type_f32(),
92-
64 => cx.type_f64(),
93-
_ => bug!("unsupported float: {:?}", self),
94-
}
87+
RegKind::Float => match self.size.bits() {
88+
32 => cx.type_f32(),
89+
64 => cx.type_f64(),
90+
_ => bug!("unsupported float: {:?}", self),
9591
},
9692
RegKind::Vector => unimplemented!(), //cx.type_vector(cx.type_i8(), self.size.bytes()),
9793
}
@@ -119,19 +115,18 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
119115

120116
// This capacity calculation is approximate.
121117
let mut argument_tys = Vec::with_capacity(
122-
self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 }
118+
self.args.len() + if let PassMode::Indirect { .. } = self.ret.mode { 1 } else { 0 },
123119
);
124120

125-
let return_type =
126-
match self.ret.mode {
127-
PassMode::Ignore => cx.type_void(),
128-
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
129-
PassMode::Cast { ref cast, .. } => cast.gcc_type(cx),
130-
PassMode::Indirect { .. } => {
131-
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
132-
cx.type_void()
133-
}
134-
};
121+
let return_type = match self.ret.mode {
122+
PassMode::Ignore => cx.type_void(),
123+
PassMode::Direct(_) | PassMode::Pair(..) => self.ret.layout.immediate_gcc_type(cx),
124+
PassMode::Cast { ref cast, .. } => cast.gcc_type(cx),
125+
PassMode::Indirect { .. } => {
126+
argument_tys.push(cx.type_ptr_to(self.ret.memory_ty(cx)));
127+
cx.type_void()
128+
}
129+
};
135130
#[cfg(feature = "master")]
136131
let mut non_null_args = Vec::new();
137132

@@ -149,17 +144,23 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
149144
ty
150145
};
151146
#[cfg(not(feature = "master"))]
152-
let apply_attrs = |ty: Type<'gcc>, _attrs: &ArgAttributes, _arg_index: usize| {
153-
ty
154-
};
147+
let apply_attrs = |ty: Type<'gcc>, _attrs: &ArgAttributes, _arg_index: usize| ty;
155148

156149
for arg in self.args.iter() {
157150
let arg_ty = match arg.mode {
158151
PassMode::Ignore => continue,
159152
PassMode::Pair(a, b) => {
160153
let arg_pos = argument_tys.len();
161-
argument_tys.push(apply_attrs(arg.layout.scalar_pair_element_gcc_type(cx, 0), &a, arg_pos));
162-
argument_tys.push(apply_attrs(arg.layout.scalar_pair_element_gcc_type(cx, 1), &b, arg_pos + 1));
154+
argument_tys.push(apply_attrs(
155+
arg.layout.scalar_pair_element_gcc_type(cx, 0),
156+
&a,
157+
arg_pos,
158+
));
159+
argument_tys.push(apply_attrs(
160+
arg.layout.scalar_pair_element_gcc_type(cx, 1),
161+
&b,
162+
arg_pos + 1,
163+
));
163164
continue;
164165
}
165166
PassMode::Cast { ref cast, pad_i32 } => {
@@ -174,14 +175,17 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
174175
// This is a "byval" argument, so we don't apply the `restrict` attribute on it.
175176
on_stack_param_indices.insert(argument_tys.len());
176177
arg.memory_ty(cx)
177-
},
178-
PassMode::Direct(attrs) => apply_attrs(arg.layout.immediate_gcc_type(cx), &attrs, argument_tys.len()),
178+
}
179+
PassMode::Direct(attrs) => {
180+
apply_attrs(arg.layout.immediate_gcc_type(cx), &attrs, argument_tys.len())
181+
}
179182
PassMode::Indirect { attrs, meta_attrs: None, on_stack: false } => {
180183
apply_attrs(cx.type_ptr_to(arg.memory_ty(cx)), &attrs, argument_tys.len())
181184
}
182185
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
183186
assert!(!on_stack);
184-
let ty = apply_attrs(cx.type_ptr_to(arg.memory_ty(cx)), &attrs, argument_tys.len());
187+
let ty =
188+
apply_attrs(cx.type_ptr_to(arg.memory_ty(cx)), &attrs, argument_tys.len());
185189
apply_attrs(ty, &meta_attrs, argument_tys.len())
186190
}
187191
};
@@ -207,15 +211,14 @@ impl<'gcc, 'tcx> FnAbiGccExt<'gcc, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
207211

208212
fn ptr_to_gcc_type(&self, cx: &CodegenCx<'gcc, 'tcx>) -> Type<'gcc> {
209213
// FIXME(antoyo): Should we do something with `FnAbiGcc::fn_attributes`?
210-
let FnAbiGcc {
211-
return_type,
212-
arguments_type,
213-
is_c_variadic,
214+
let FnAbiGcc { return_type, arguments_type, is_c_variadic, on_stack_param_indices, .. } =
215+
self.gcc_type(cx);
216+
let pointer_type =
217+
cx.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic);
218+
cx.on_stack_params.borrow_mut().insert(
219+
pointer_type.dyncast_function_ptr_type().expect("function ptr type"),
214220
on_stack_param_indices,
215-
..
216-
} = self.gcc_type(cx);
217-
let pointer_type = cx.context.new_function_pointer_type(None, return_type, &arguments_type, is_c_variadic);
218-
cx.on_stack_params.borrow_mut().insert(pointer_type.dyncast_function_ptr_type().expect("function ptr type"), on_stack_param_indices);
221+
);
219222
pointer_type
220223
}
221224
}

src/allocator.rs

+39-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(feature="master")]
1+
#[cfg(feature = "master")]
22
use gccjit::FnAttribute;
33
use gccjit::{Context, FunctionType, GlobalKind, ToRValue, Type};
44
use rustc_ast::expand::allocator::{
@@ -11,15 +11,20 @@ use rustc_session::config::OomStrategy;
1111

1212
use crate::GccContext;
1313

14-
pub(crate) unsafe fn codegen(tcx: TyCtxt<'_>, mods: &mut GccContext, _module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind) {
14+
pub(crate) unsafe fn codegen(
15+
tcx: TyCtxt<'_>,
16+
mods: &mut GccContext,
17+
_module_name: &str,
18+
kind: AllocatorKind,
19+
alloc_error_handler_kind: AllocatorKind,
20+
) {
1521
let context = &mods.context;
16-
let usize =
17-
match tcx.sess.target.pointer_width {
18-
16 => context.new_type::<u16>(),
19-
32 => context.new_type::<u32>(),
20-
64 => context.new_type::<u64>(),
21-
tws => bug!("Unsupported target word size for int: {}", tws),
22-
};
22+
let usize = match tcx.sess.target.pointer_width {
23+
16 => context.new_type::<u16>(),
24+
32 => context.new_type::<u32>(),
25+
64 => context.new_type::<u64>(),
26+
tws => bug!("Unsupported target word size for int: {}", tws),
27+
};
2328
let i8 = context.new_type::<i8>();
2429
let i8p = i8.make_pointer();
2530

@@ -85,24 +90,42 @@ fn create_wrapper_function(
8590
) {
8691
let void = context.new_type::<()>();
8792

88-
let args: Vec<_> = types.iter().enumerate()
93+
let args: Vec<_> = types
94+
.iter()
95+
.enumerate()
8996
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
9097
.collect();
91-
let func = context.new_function(None, FunctionType::Exported, output.unwrap_or(void), &args, from_name, false);
98+
let func = context.new_function(
99+
None,
100+
FunctionType::Exported,
101+
output.unwrap_or(void),
102+
&args,
103+
from_name,
104+
false,
105+
);
92106

93107
if tcx.sess.default_hidden_visibility() {
94-
#[cfg(feature="master")]
108+
#[cfg(feature = "master")]
95109
func.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
96110
}
97111
if tcx.sess.must_emit_unwind_tables() {
98112
// TODO(antoyo): emit unwind tables.
99113
}
100114

101-
let args: Vec<_> = types.iter().enumerate()
115+
let args: Vec<_> = types
116+
.iter()
117+
.enumerate()
102118
.map(|(index, typ)| context.new_parameter(None, *typ, &format!("param{}", index)))
103119
.collect();
104-
let callee = context.new_function(None, FunctionType::Extern, output.unwrap_or(void), &args, to_name, false);
105-
#[cfg(feature="master")]
120+
let callee = context.new_function(
121+
None,
122+
FunctionType::Extern,
123+
output.unwrap_or(void),
124+
&args,
125+
to_name,
126+
false,
127+
);
128+
#[cfg(feature = "master")]
106129
callee.add_attribute(FnAttribute::Visibility(gccjit::Visibility::Hidden));
107130

108131
let block = func.new_block("entry");
@@ -116,8 +139,7 @@ fn create_wrapper_function(
116139
//llvm::LLVMSetTailCall(ret, True);
117140
if output.is_some() {
118141
block.end_with_return(None, ret);
119-
}
120-
else {
142+
} else {
121143
block.end_with_void_return(None);
122144
}
123145

0 commit comments

Comments
 (0)