forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#66245 - tmiasko:cfg-sanitize, r=oli-obk
Conditional compilation for sanitizers Configure sanitize option when compiling with a sanitizer to make it possible to execute different code depending on whether given sanitizer is enabled or not.
- Loading branch information
Showing
8 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/doc/unstable-book/src/language-features/cfg-sanitize.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `cfg_sanitize` | ||
|
||
The tracking issue for this feature is: [#39699] | ||
|
||
[#39699]: https://github.com/rust-lang/rust/issues/39699 | ||
|
||
------------------------ | ||
|
||
The `cfg_sanitize` feature makes it possible to execute different code | ||
depending on whether a particular sanitizer is enabled or not. | ||
|
||
## Examples | ||
|
||
``` rust | ||
#![feature(cfg_sanitize)] | ||
|
||
#[cfg(sanitize = "thread")] | ||
fn a() { | ||
// ... | ||
} | ||
|
||
#[cfg(not(sanitize = "thread"))] | ||
fn a() { | ||
// ... | ||
} | ||
|
||
fn b() { | ||
if cfg!(sanitize = "leak") { | ||
// ... | ||
} else { | ||
// ... | ||
} | ||
} | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#[cfg(not(sanitize = "thread"))] | ||
//~^ `cfg(sanitize)` is experimental | ||
fn main() {} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/feature-gates/feature-gate-cfg_sanitize.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0658]: `cfg(sanitize)` is experimental and subject to change | ||
--> $DIR/feature-gate-cfg_sanitize.rs:1:11 | ||
| | ||
LL | #[cfg(not(sanitize = "thread"))] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: for more information, see https://github.com/rust-lang/rust/issues/39699 | ||
= help: add `#![feature(cfg_sanitize)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Verifies that when compiling with -Zsanitizer=option, | ||
// the `#[cfg(sanitize = "option")]` attribute is configured. | ||
|
||
// needs-sanitizer-support | ||
// only-linux | ||
// only-x86_64 | ||
// check-pass | ||
// revisions: address leak memory thread | ||
//[address]compile-flags: -Zsanitizer=address --cfg address | ||
//[leak]compile-flags: -Zsanitizer=leak --cfg leak | ||
//[memory]compile-flags: -Zsanitizer=memory --cfg memory | ||
//[thread]compile-flags: -Zsanitizer=thread --cfg thread | ||
|
||
#![feature(cfg_sanitize)] | ||
|
||
#[cfg(all(sanitize = "address", address))] | ||
fn main() {} | ||
|
||
#[cfg(all(sanitize = "leak", leak))] | ||
fn main() {} | ||
|
||
#[cfg(all(sanitize = "memory", memory))] | ||
fn main() {} | ||
|
||
#[cfg(all(sanitize = "thread", thread))] | ||
fn main() {} |