Skip to content

Commit

Permalink
Avoid the use of #[cfg(doc)] in std_detect (#1283)
Browse files Browse the repository at this point in the history
Co-authored-by: bjorn3 <bjorn3@users.noreply.github.com>
  • Loading branch information
Amanieu and bjorn3 committed Feb 17, 2022
1 parent 863d31b commit b4a0e07
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 271 deletions.
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

features! {
@TARGET: aarch64;
@CFG: target_arch = "aarch64";
@MACRO_NAME: is_aarch64_feature_detected;
@MACRO_ATTRS:
/// This macro tests, at runtime, whether an `aarch64` feature is enabled on aarch64 platforms.
Expand Down
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

features! {
@TARGET: arm;
@CFG: target_arch = "arm";
@MACRO_NAME: is_arm_feature_detected;
@MACRO_ATTRS:
/// Checks if `arm` feature is enabled.
Expand Down
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/mips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

features! {
@TARGET: mips;
@CFG: target_arch = "mips";
@MACRO_NAME: is_mips_feature_detected;
@MACRO_ATTRS:
/// Checks if `mips` feature is enabled.
Expand Down
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/mips64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

features! {
@TARGET: mips64;
@CFG: target_arch = "mips64";
@MACRO_NAME: is_mips64_feature_detected;
@MACRO_ATTRS:
/// Checks if `mips64` feature is enabled.
Expand Down
56 changes: 56 additions & 0 deletions crates/std_detect/src/detect/arch/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#![allow(dead_code)]

use cfg_if::cfg_if;

// Export the macros for all supported architectures.
#[macro_use]
mod x86;
#[macro_use]
mod arm;
#[macro_use]
mod aarch64;
#[macro_use]
mod riscv;
#[macro_use]
mod powerpc;
#[macro_use]
mod powerpc64;
#[macro_use]
mod mips;
#[macro_use]
mod mips64;

cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
pub use x86::*;
} else if #[cfg(target_arch = "arm")] {
pub use arm::*;
} else if #[cfg(target_arch = "aarch64")] {
pub use aarch64::*;
} else if #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))] {
pub use riscv::*;
} else if #[cfg(target_arch = "powerpc")] {
pub use powerpc::*;
} else if #[cfg(target_arch = "powerpc64")] {
pub use powerpc64::*;
} else if #[cfg(target_arch = "mips")] {
pub use mips::*;
} else if #[cfg(target_arch = "mips64")] {
pub use mips64::*;
} else {
// Unimplemented architecture:
#[doc(hidden)]
pub(crate) enum Feature {
Null
}
#[doc(hidden)]
pub mod __is_feature_detected {}

impl Feature {
#[doc(hidden)]
pub(crate) fn from_str(_s: &str) -> Result<Feature, ()> { Err(()) }
#[doc(hidden)]
pub(crate) fn to_str(self) -> &'static str { "" }
}
}
}
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/powerpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

features! {
@TARGET: powerpc;
@CFG: target_arch = "powerpc";
@MACRO_NAME: is_powerpc_feature_detected;
@MACRO_ATTRS:
/// Checks if `powerpc` feature is enabled.
Expand Down
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/powerpc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

features! {
@TARGET: powerpc64;
@CFG: target_arch = "powerpc64";
@MACRO_NAME: is_powerpc64_feature_detected;
@MACRO_ATTRS:
/// Checks if `powerpc` feature is enabled.
Expand Down
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/riscv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

features! {
@TARGET: riscv;
@CFG: any(target_arch = "riscv32", target_arch = "riscv64");
@MACRO_NAME: is_riscv_feature_detected;
@MACRO_ATTRS:
/// A macro to test at *runtime* whether instruction sets are available on
Expand Down
1 change: 1 addition & 0 deletions crates/std_detect/src/detect/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

features! {
@TARGET: x86;
@CFG: any(target_arch = "x86", target_arch = "x86_64");
@MACRO_NAME: is_x86_feature_detected;
@MACRO_ATTRS:
/// A macro to test at *runtime* whether a CPU feature is available on
Expand Down
171 changes: 0 additions & 171 deletions crates/std_detect/src/detect/error_macros.rs

This file was deleted.

50 changes: 50 additions & 0 deletions crates/std_detect/src/detect/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
macro_rules! features {
(
@TARGET: $target:ident;
@CFG: $cfg:meta;
@MACRO_NAME: $macro_name:ident;
@MACRO_ATTRS: $(#[$macro_attrs:meta])*
$(@BIND_FEATURE_NAME: $bind_feature:tt; $feature_impl:tt; )*
Expand All @@ -11,6 +12,8 @@ macro_rules! features {
#[macro_export]
$(#[$macro_attrs])*
#[allow_internal_unstable(stdsimd_internal)]
#[cfg($cfg)]
#[doc(cfg($cfg))]
macro_rules! $macro_name {
$(
($feature_lit) => {
Expand Down Expand Up @@ -44,6 +47,50 @@ macro_rules! features {
};
}

$(#[$macro_attrs])*
#[macro_export]
#[cfg(not($cfg))]
#[doc(cfg($cfg))]
macro_rules! $macro_name {
$(
($feature_lit) => {
compile_error!(
concat!(
r#"This macro cannot be used on the current target.
You can prevent it from being used in other architectures by
guarding it behind a cfg("#,
stringify!($cfg),
")."
)
)
};
)*
$(
($bind_feature) => { $macro_name!($feature_impl) };
)*
$(
($nort_feature) => {
compile_error!(
concat!(
stringify!($nort_feature),
" feature cannot be detected at run-time"
)
)
};
)*
($t:tt,) => {
$macro_name!($t);
};
($t:tt) => {
compile_error!(
concat!(
concat!("unknown ", stringify!($target)),
concat!(" target feature: ", $t)
)
)
};
}

/// Each variant denotes a position in a bitset for a particular feature.
///
/// PLEASE: do not use this, it is an implementation detail subject
Expand All @@ -53,6 +100,7 @@ macro_rules! features {
#[derive(Copy, Clone)]
#[repr(u8)]
#[unstable(feature = "stdsimd_internal", issue = "none")]
#[cfg($cfg)]
pub(crate) enum Feature {
$(
$(#[$feature_comment])*
Expand All @@ -63,6 +111,7 @@ macro_rules! features {
_last
}

#[cfg($cfg)]
impl Feature {
pub(crate) fn to_str(self) -> &'static str {
match self {
Expand All @@ -86,6 +135,7 @@ macro_rules! features {
/// PLEASE: do not use this, it is an implementation detail subject
/// to change.
#[doc(hidden)]
#[cfg($cfg)]
pub mod __is_feature_detected {
$(

Expand Down
Loading

0 comments on commit b4a0e07

Please sign in to comment.