Skip to content

Commit e0b509c

Browse files
committed
rustdoc: Output target feature information
`#[target_feature]` attributes refer to a target-specific list of features. Enabling certain features can imply enabling other features. Certain features are always enabled on certain targets, since they are required by the target's ABI. Features can also be enabled indirectly based on other compiler flags. Feature information is ultimately known to `rustc`. Rather than force external tools to track it -- which may be wildly impractical due to `-C target-cpu` -- have `rustdoc` output `rustc`'s feature data.
1 parent 5337252 commit e0b509c

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/librustdoc/json/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ impl<'tcx> JsonRenderer<'tcx> {
121121
Ok(())
122122
})
123123
}
124+
125+
pub(crate) fn target(&self) -> types::Target {
126+
let sess = self.tcx.sess;
127+
128+
let globally_enabled_features: FxHashMap<&str, ()> =
129+
sess.target_features.iter().map(|sym| (sym.as_str(), ())).collect();
130+
131+
types::Target {
132+
target_features: sess
133+
.target
134+
.rust_target_features()
135+
.into_iter()
136+
.copied()
137+
.map(|(name, stability, implied)| types::TargetFeature {
138+
name: name.into(),
139+
stable: matches!(stability, rustc_target::target_features::Stability::Stable),
140+
implies_features: implied.into_iter().copied().map(String::from).collect(),
141+
globally_enabled: globally_enabled_features.contains_key(name),
142+
})
143+
.collect(),
144+
}
145+
}
124146
}
125147

126148
impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
@@ -288,6 +310,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
288310
)
289311
})
290312
.collect(),
313+
target: self.target(),
291314
format_version: types::FORMAT_VERSION,
292315
};
293316
if let Some(ref out_dir) = self.out_dir {

src/rustdoc-json-types/lib.rs

+24
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,35 @@ pub struct Crate {
5252
pub paths: HashMap<Id, ItemSummary>,
5353
/// Maps `crate_id` of items to a crate name and html_root_url if it exists.
5454
pub external_crates: HashMap<u32, ExternalCrate>,
55+
/// Information about the target for which this documentation was generated
56+
pub target: Target,
5557
/// A single version number to be used in the future when making backwards incompatible changes
5658
/// to the JSON output.
5759
pub format_version: u32,
5860
}
5961

62+
/// Information about a target
63+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
64+
pub struct Target {
65+
/// A list of `#[target_feature]` which exist for this target, along with their status in this
66+
/// compiler session
67+
pub target_features: Vec<TargetFeature>,
68+
}
69+
70+
/// Information about a `#[target_feature]`
71+
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
72+
pub struct TargetFeature {
73+
/// The name of this feature
74+
pub name: String,
75+
/// Other feature(s) which are implied by this feature
76+
pub implies_features: Vec<String>,
77+
/// Whether this feature is stable
78+
pub stable: bool,
79+
/// Whether this feature is globally enabled for this target, for example, due to the
80+
/// target's ABI requiring this feature, or due to compiler flags which enable it everywhere
81+
pub globally_enabled: bool,
82+
}
83+
6084
/// Metadata of a crate, either the same crate on which `rustdoc` was invoked, or its dependency.
6185
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6286
pub struct ExternalCrate {

0 commit comments

Comments
 (0)