Skip to content

Commit 98cd818

Browse files
committed
Refactor exported_symbols and linked_symbols for code reuse
1 parent 460054c commit 98cd818

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+26-44
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{env, mem, str};
1212

1313
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
1414
use rustc_middle::middle::dependency_format::Linkage;
15-
use rustc_middle::middle::exported_symbols::SymbolExportKind;
15+
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportKind};
1616
use rustc_middle::ty::TyCtxt;
1717
use rustc_serialize::{json, Encoder};
1818
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
@@ -1519,22 +1519,13 @@ impl<'a> L4Bender<'a> {
15191519
}
15201520
}
15211521

1522-
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
1523-
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
1524-
return exports.clone();
1525-
}
1526-
1527-
let mut symbols = Vec::new();
1528-
1529-
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1522+
fn for_each_exported_symbols_include_dep<'tcx>(
1523+
tcx: TyCtxt<'tcx>,
1524+
crate_type: CrateType,
1525+
mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum),
1526+
) {
15301527
for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() {
1531-
if info.level.is_below_threshold(export_threshold) {
1532-
symbols.push(symbol_export::symbol_name_for_instance_in_crate(
1533-
tcx,
1534-
symbol,
1535-
LOCAL_CRATE,
1536-
));
1537-
}
1528+
callback(symbol, info, LOCAL_CRATE);
15381529
}
15391530

15401531
let formats = tcx.dependency_formats(());
@@ -1544,16 +1535,26 @@ pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<St
15441535
let cnum = CrateNum::new(index + 1);
15451536
// For each dependency that we are linking to statically ...
15461537
if *dep_format == Linkage::Static {
1547-
// ... we add its symbol list to our export list.
15481538
for &(symbol, info) in tcx.exported_symbols(cnum).iter() {
1549-
if !info.level.is_below_threshold(export_threshold) {
1550-
continue;
1551-
}
1552-
1553-
symbols.push(symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum));
1539+
callback(symbol, info, cnum);
15541540
}
15551541
}
15561542
}
1543+
}
1544+
1545+
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
1546+
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
1547+
return exports.clone();
1548+
}
1549+
1550+
let mut symbols = Vec::new();
1551+
1552+
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1553+
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
1554+
if info.level.is_below_threshold(export_threshold) {
1555+
symbols.push(symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum));
1556+
}
1557+
});
15571558

15581559
symbols
15591560
}
@@ -1572,33 +1573,14 @@ pub(crate) fn linked_symbols(
15721573
let mut symbols = Vec::new();
15731574

15741575
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1575-
for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() {
1576+
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
15761577
if info.level.is_below_threshold(export_threshold) || info.used {
15771578
symbols.push((
1578-
symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, LOCAL_CRATE),
1579+
symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum),
15791580
info.kind,
15801581
));
15811582
}
1582-
}
1583-
1584-
let formats = tcx.dependency_formats(());
1585-
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();
1586-
1587-
for (index, dep_format) in deps.iter().enumerate() {
1588-
let cnum = CrateNum::new(index + 1);
1589-
// For each dependency that we are linking to statically ...
1590-
if *dep_format == Linkage::Static {
1591-
// ... we add its symbol list to our export list.
1592-
for &(symbol, info) in tcx.exported_symbols(cnum).iter() {
1593-
if info.level.is_below_threshold(export_threshold) || info.used {
1594-
symbols.push((
1595-
symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum),
1596-
info.kind,
1597-
));
1598-
}
1599-
}
1600-
}
1601-
}
1583+
});
16021584

16031585
symbols
16041586
}

0 commit comments

Comments
 (0)