1
Fork 0

Don't require --verbose to show linker stdout

This commit is contained in:
jyn 2025-01-17 08:00:28 -05:00
parent b757663a00
commit 26708aa941
7 changed files with 28 additions and 29 deletions

View file

@ -1040,6 +1040,11 @@ fn link_natively(
sess.dcx().abort_if_errors(); sess.dcx().abort_if_errors();
} }
let stderr = escape_string(&prog.stderr);
let stdout = escape_string(&prog.stdout);
info!("linker stderr:\n{}", &stderr);
info!("linker stdout:\n{}", &stdout);
let (level, src) = codegen_results.crate_info.lint_levels.linker_messages; let (level, src) = codegen_results.crate_info.lint_levels.linker_messages;
let lint = |msg| { let lint = |msg| {
lint_level(sess, LINKER_MESSAGES, level, src, None, |diag| { lint_level(sess, LINKER_MESSAGES, level, src, None, |diag| {
@ -1049,16 +1054,14 @@ fn link_natively(
if !prog.stderr.is_empty() { if !prog.stderr.is_empty() {
// We already print `warning:` at the start of the diagnostic. Remove it from the linker output if present. // We already print `warning:` at the start of the diagnostic. Remove it from the linker output if present.
let stderr = escape_string(&prog.stderr);
debug!("original stderr: {stderr}");
let stderr = stderr let stderr = stderr
.strip_prefix("warning: ") .strip_prefix("warning: ")
.unwrap_or(&stderr) .unwrap_or(&stderr)
.replace(": warning: ", ": "); .replace(": warning: ", ": ");
lint(format!("linker stderr: {stderr}")); lint(format!("linker stderr: {stderr}"));
} }
if !prog.stdout.is_empty() && sess.opts.verbose { if !prog.stdout.is_empty() {
lint(format!("linker stdout: {}", escape_string(&prog.stdout))) lint(format!("linker stdout: {}", stdout))
} }
} }
Err(e) => { Err(e) => {

View file

@ -2338,11 +2338,20 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}) })
{ {
if hir_id != CRATE_HIR_ID { if hir_id != CRATE_HIR_ID {
let err = match attr.style { match attr.style {
ast::AttrStyle::Outer => errors::OuterCrateLevelAttr, ast::AttrStyle::Outer => self.tcx.emit_node_span_lint(
ast::AttrStyle::Inner => errors::OuterCrateLevelAttr, UNUSED_ATTRIBUTES,
hir_id,
attr.span,
errors::OuterCrateLevelAttr,
),
ast::AttrStyle::Inner => self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr.span,
errors::InnerCrateLevelAttr,
),
}; };
self.tcx.emit_node_span_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, err);
return; return;
} else { } else {
let never_needs_link = self let never_needs_link = self

View file

@ -14,16 +14,11 @@ fn main() {
let warnings = run_rustc().link_arg("run_make_warn").run(); let warnings = run_rustc().link_arg("run_make_warn").run();
warnings.assert_stderr_contains("warning: linker stderr: bar"); warnings.assert_stderr_contains("warning: linker stderr: bar");
// Make sure it shows stdout, but only when --verbose is passed // Make sure it shows stdout
run_rustc() run_rustc()
.link_arg("run_make_info") .link_arg("run_make_info")
.verbose()
.run() .run()
.assert_stderr_contains("warning: linker stdout: foo"); .assert_stderr_contains("warning: linker stdout: foo");
run_rustc()
.link_arg("run_make_info")
.run()
.assert_stderr_not_contains("warning: linker stdout: foo");
// Make sure we short-circuit this new path if the linker exits with an error // Make sure we short-circuit this new path if the linker exits with an error
// (so the diagnostic is less verbose) // (so the diagnostic is less verbose)

View file

@ -12,7 +12,7 @@ use run_make_support::rustc;
fn main() { fn main() {
// A regular compilation should not use rust-lld by default. We'll check that by asking the // A regular compilation should not use rust-lld by default. We'll check that by asking the
// linker to display its version number with a link-arg. // linker to display its version number with a link-arg.
let output = rustc().verbose().link_arg("-Wl,-v").input("main.rs").run(); let output = rustc().link_arg("-Wl,-v").input("main.rs").run();
assert!( assert!(
!find_lld_version_in_logs(output.stderr_utf8()), !find_lld_version_in_logs(output.stderr_utf8()),
"the LLD version string should not be present in the output logs:\n{}", "the LLD version string should not be present in the output logs:\n{}",
@ -21,6 +21,7 @@ fn main() {
} }
fn find_lld_version_in_logs(stderr: String) -> bool { fn find_lld_version_in_logs(stderr: String) -> bool {
let lld_version_re = Regex::new(r"^LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap(); let lld_version_re =
Regex::new(r"^warning: linker stdout: LLD [0-9]+\.[0-9]+\.[0-9]+").unwrap();
stderr.lines().any(|line| lld_version_re.is_match(line.trim())) stderr.lines().any(|line| lld_version_re.is_match(line.trim()))
} }

View file

@ -12,7 +12,7 @@ use run_make_support::rustc;
fn main() { fn main() {
// A regular compilation should use rust-lld by default. We'll check that by asking the linker // A regular compilation should use rust-lld by default. We'll check that by asking the linker
// to display its version number with a link-arg. // to display its version number with a link-arg.
let output = rustc().verbose().link_arg("-Wl,-v").input("main.rs").run(); let output = rustc().link_arg("-Wl,-v").input("main.rs").run();
assert!( assert!(
find_lld_version_in_logs(output.stderr_utf8()), find_lld_version_in_logs(output.stderr_utf8()),
"the LLD version string should be present in the output logs:\n{}", "the LLD version string should be present in the output logs:\n{}",
@ -20,8 +20,7 @@ fn main() {
); );
// But it can still be disabled by turning the linker feature off. // But it can still be disabled by turning the linker feature off.
let output = let output = rustc().link_arg("-Wl,-v").arg("-Zlinker-features=-lld").input("main.rs").run();
rustc().verbose().link_arg("-Wl,-v").arg("-Zlinker-features=-lld").input("main.rs").run();
assert!( assert!(
!find_lld_version_in_logs(output.stderr_utf8()), !find_lld_version_in_logs(output.stderr_utf8()),
"the LLD version string should not be present in the output logs:\n{}", "the LLD version string should not be present in the output logs:\n{}",

View file

@ -15,7 +15,6 @@ fn main() {
// Compile to a custom target spec with rust-lld enabled by default. We'll check that by asking // Compile to a custom target spec with rust-lld enabled by default. We'll check that by asking
// the linker to display its version number with a link-arg. // the linker to display its version number with a link-arg.
let output = rustc() let output = rustc()
.verbose()
.crate_type("cdylib") .crate_type("cdylib")
.target("custom-target.json") .target("custom-target.json")
.link_arg("-Wl,-v") .link_arg("-Wl,-v")
@ -29,7 +28,6 @@ fn main() {
// But it can also be disabled via linker features. // But it can also be disabled via linker features.
let output = rustc() let output = rustc()
.verbose()
.crate_type("cdylib") .crate_type("cdylib")
.target("custom-target.json") .target("custom-target.json")
.arg("-Zlinker-features=-lld") .arg("-Zlinker-features=-lld")

View file

@ -17,7 +17,6 @@ fn main() {
.arg("-Zlinker-features=+lld") .arg("-Zlinker-features=+lld")
.arg("-Clink-self-contained=+linker") .arg("-Clink-self-contained=+linker")
.arg("-Zunstable-options") .arg("-Zunstable-options")
.verbose()
.link_arg(linker_version_flag) .link_arg(linker_version_flag)
.input("main.rs") .input("main.rs")
.run(); .run();
@ -28,12 +27,8 @@ fn main() {
); );
// It should not be used when we explicitly opt-out of lld. // It should not be used when we explicitly opt-out of lld.
let output = rustc() let output =
.link_arg(linker_version_flag) rustc().link_arg(linker_version_flag).arg("-Zlinker-features=-lld").input("main.rs").run();
.verbose()
.arg("-Zlinker-features=-lld")
.input("main.rs")
.run();
assert!( assert!(
!find_lld_version_in_logs(output.stderr_utf8()), !find_lld_version_in_logs(output.stderr_utf8()),
"the LLD version string should not be present in the output logs:\n{}", "the LLD version string should not be present in the output logs:\n{}",
@ -44,7 +39,6 @@ fn main() {
// times to rustc. // times to rustc.
let output = rustc() let output = rustc()
.link_arg(linker_version_flag) .link_arg(linker_version_flag)
.verbose()
.arg("-Clink-self-contained=+linker") .arg("-Clink-self-contained=+linker")
.arg("-Zunstable-options") .arg("-Zunstable-options")
.arg("-Zlinker-features=-lld") .arg("-Zlinker-features=-lld")