| 1 |
//! Generate a macro to add the various `used` and other attributes to a static. |
| 2 |
#![allow(unknown_lints, edition_2024_expr_fragment_specifier)] |
| 3 |
|
| 4 |
use crate::{__chain, __perform}; |
| 5 |
|
| 6 |
__perform!((all=() export=()), __chain[ |
| 7 |
__add_used, |
| 8 |
__add_asan, |
| 9 |
__add_export_name[$], |
| 10 |
__generate_macro[$], |
| 11 |
]); |
| 12 |
|
| 13 |
#[cfg(linktime_used_linker)] |
| 14 |
macro_rules! __add_used { |
| 15 |
(@entry next=$next:path[$next_args:tt], input=(all=($($input:tt)*) export=$export:tt)) => { |
| 16 |
$next!($next_args, (all=($($input)* #[used(linker)]) export=$export)); |
| 17 |
}; |
| 18 |
} |
| 19 |
|
| 20 |
#[cfg(not(linktime_used_linker))] |
| 21 |
macro_rules! __add_used { |
| 22 |
(@entry next=$next:path[$next_args:tt], input=(all=($($input:tt)*) export=$export:tt)) => { |
| 23 |
$next!($next_args, (all=($($input)* #[used]) export=$export)); |
| 24 |
}; |
| 25 |
} |
| 26 |
|
| 27 |
#[cfg(linktime_asan)] |
| 28 |
macro_rules! __add_asan { |
| 29 |
(@entry next=$next:path[$next_args:tt], input=(all=($($input:tt)*) export=$export:tt)) => { |
| 30 |
$next!($next_args, (all=($($input)* #[sanitize(address = "off")]) export=$export)); |
| 31 |
}; |
| 32 |
} |
| 33 |
|
| 34 |
#[cfg(not(linktime_asan))] |
| 35 |
macro_rules! __add_asan { |
| 36 |
(@entry next=$next:path[$next_args:tt], input=$input:tt) => { |
| 37 |
$next!($next_args, $input); |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
#[cfg(target_os = "aix")] |
| 42 |
macro_rules! __add_export_name { |
| 43 |
(@entry next=$next:path[$next_args:tt], input=(all=$all:tt export=($($input:tt)*)), args=[$dollar:tt]) => { |
| 44 |
$next!($next_args, (all=$all export=(( |
| 45 |
"_P", env!("CARGO_PKG_NAME"), |
| 46 |
"_M", ::core::module_path!(), |
| 47 |
"_L", line!(), |
| 48 |
"_C", column!() |
| 49 |
)))); |
| 50 |
}; |
| 51 |
} |
| 52 |
|
| 53 |
#[cfg(not(target_os = "aix"))] |
| 54 |
macro_rules! __add_export_name { |
| 55 |
(@entry next=$next:path[$next_args:tt], input=$input:tt, args=[$dollar:tt]) => { |
| 56 |
$next!($next_args, $input); |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
macro_rules! __generate_macro { |
| 61 |
(@entry next=$next:path[$next_args:tt], input=(all=($($all:tt)*) export=($(($($export:tt)*))?)), args=[$dollar:tt]) => { |
| 62 |
#[doc(hidden)] |
| 63 |
#[macro_export] |
| 64 |
macro_rules! __add_linktime_attributes_to_static { |
| 65 |
( |
| 66 |
#[link_section = $link_section:expr] |
| 67 |
$dollar (#[$meta:meta])* $vis:vis static $ident:ident : $dollar ($static:tt)* |
| 68 |
) => { |
| 69 |
$($all)* |
| 70 |
$(#[export_name = concat! (stringify!($ident), $($export)* )])? |
| 71 |
$dollar (#[$meta])* |
| 72 |
#[link_section = $link_section] |
| 73 |
$vis static $ident : $dollar ($static)* |
| 74 |
}; |
| 75 |
|
| 76 |
( |
| 77 |
#[export_name = $export_name:expr] |
| 78 |
$dollar (#[$meta:meta])* $vis:vis static $ident:ident : $dollar ($static:tt)* |
| 79 |
) => { |
| 80 |
#[export_name = $export_name] |
| 81 |
$($all)* |
| 82 |
$dollar (#[$meta])* |
| 83 |
$vis static $ident : $dollar ($static)* |
| 84 |
}; |
| 85 |
|
| 86 |
( |
| 87 |
extern "C" { |
| 88 |
#[link_name = $link_name:expr] |
| 89 |
$dollar (#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty; |
| 90 |
} |
| 91 |
) => { |
| 92 |
#[allow(missing_unsafe_on_extern)] // MSRV |
| 93 |
extern "C" { |
| 94 |
#[link_name = $link_name] |
| 95 |
$dollar (#[$meta])* $vis static $ident : $ty; |
| 96 |
} |
| 97 |
}; |
| 98 |
|
| 99 |
( |
| 100 |
$dollar ($item:tt)* |
| 101 |
) => { |
| 102 |
$dollar ($item)* |
| 103 |
}; |
| 104 |
} |
| 105 |
}; |
| 106 |
} |
| 107 |
|
| 108 |
pub(crate) use __add_asan; |
| 109 |
pub(crate) use __add_export_name; |
| 110 |
pub(crate) use __add_used; |
| 111 |
pub(crate) use __generate_macro; |