Skip to content

Commit 294a4f5

Browse files
committed
Implement printing to file in codegen_backend.print
1 parent 62ecfd3 commit 294a4f5

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ impl CodegenBackend for LlvmCodegenBackend {
262262
|tcx, ()| llvm_util::global_llvm_features(tcx.sess, true)
263263
}
264264

265-
fn print(&self, req: PrintRequest, sess: &Session) {
265+
fn print(&self, req: PrintRequest, out: &mut dyn PrintBackendInfo, sess: &Session) {
266266
match req {
267267
PrintRequest::RelocationModels => {
268-
println!("Available relocation models:");
268+
writeln!(out, "Available relocation models:");
269269
for name in &[
270270
"static",
271271
"pic",
@@ -276,26 +276,27 @@ impl CodegenBackend for LlvmCodegenBackend {
276276
"ropi-rwpi",
277277
"default",
278278
] {
279-
println!(" {}", name);
279+
writeln!(out, " {}", name);
280280
}
281-
println!();
281+
writeln!(out);
282282
}
283283
PrintRequest::CodeModels => {
284-
println!("Available code models:");
284+
writeln!(out, "Available code models:");
285285
for name in &["tiny", "small", "kernel", "medium", "large"] {
286-
println!(" {}", name);
286+
writeln!(out, " {}", name);
287287
}
288-
println!();
288+
writeln!(out);
289289
}
290290
PrintRequest::TlsModels => {
291-
println!("Available TLS models:");
291+
writeln!(out, "Available TLS models:");
292292
for name in &["global-dynamic", "local-dynamic", "initial-exec", "local-exec"] {
293-
println!(" {}", name);
293+
writeln!(out, " {}", name);
294294
}
295-
println!();
295+
writeln!(out);
296296
}
297297
PrintRequest::StackProtectorStrategies => {
298-
println!(
298+
writeln!(
299+
out,
299300
r#"Available stack protector strategies:
300301
all
301302
Generate stack canaries in all functions.

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use rustc_span::symbol::Symbol;
2323
use rustc_target::abi::call::FnAbi;
2424
use rustc_target::spec::Target;
2525

26+
use std::fmt;
27+
2628
pub trait BackendTypes {
2729
type Value: CodegenObject;
2830
type Function: CodegenObject;
@@ -61,7 +63,7 @@ pub trait CodegenBackend {
6163
fn locale_resource(&self) -> &'static str;
6264

6365
fn init(&self, _sess: &Session) {}
64-
fn print(&self, _req: PrintRequest, _sess: &Session) {}
66+
fn print(&self, _req: PrintRequest, _out: &mut dyn PrintBackendInfo, _sess: &Session) {}
6567
fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<Symbol> {
6668
vec![]
6769
}
@@ -162,3 +164,19 @@ pub trait ExtraBackendMethods:
162164
std::thread::Builder::new().name(name).spawn(f)
163165
}
164166
}
167+
168+
pub trait PrintBackendInfo {
169+
fn infallible_write_fmt(&mut self, args: fmt::Arguments<'_>);
170+
}
171+
172+
impl PrintBackendInfo for String {
173+
fn infallible_write_fmt(&mut self, args: fmt::Arguments<'_>) {
174+
fmt::Write::write_fmt(self, args).unwrap();
175+
}
176+
}
177+
178+
impl dyn PrintBackendInfo + '_ {
179+
pub fn write_fmt(&mut self, args: fmt::Arguments<'_>) {
180+
self.infallible_write_fmt(args);
181+
}
182+
}

compiler/rustc_codegen_ssa/src/traits/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ mod write;
3030

3131
pub use self::abi::AbiBuilderMethods;
3232
pub use self::asm::{AsmBuilderMethods, AsmMethods, GlobalAsmOperandRef, InlineAsmOperandRef};
33-
pub use self::backend::{Backend, BackendTypes, CodegenBackend, ExtraBackendMethods};
33+
pub use self::backend::{
34+
Backend, BackendTypes, CodegenBackend, ExtraBackendMethods, PrintBackendInfo,
35+
};
3436
pub use self::builder::{BuilderMethods, OverflowOp};
3537
pub use self::consts::ConstMethods;
3638
pub use self::coverageinfo::CoverageInfoBuilderMethods;

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ fn print_crate_info(
841841
| TargetCPUs
842842
| StackProtectorStrategies
843843
| TargetFeatures => {
844-
codegen_backend.print(*req, sess);
844+
codegen_backend.print(*req, &mut crate_info, sess);
845845
}
846846
// Any output here interferes with Cargo's parsing of other printed output
847847
NativeStaticLibs => {}

0 commit comments

Comments
 (0)