Add predefined version D_Optimized when compiling with -O (#14245)

* Add predefined version `D_Optimized` when compiling with `-O`

So that code can be aware of whether it's compiled with `-O`, e.g., to
distinguish between debug and optimized release builds (not necessarily
using `-release`).

* [update changelog/d_optimized.dd]

Co-authored-by: Petar Kirov <petar.p.kirov@gmail.com>

Co-authored-by: Petar Kirov <petar.p.kirov@gmail.com>
This commit is contained in:
Martin Kinkelin 2022-10-05 04:28:03 +02:00 committed by GitHub
parent c637c6ad16
commit c5337c88c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 0 deletions

5
changelog/d_optimized.dd Normal file
View file

@ -0,0 +1,5 @@
Add predefined version `D_Optimized` when compiling with `-O`
It allows code to distinguish whether it's being compiled with optimizations enabled (the `-O` flag was provided).
This is orthogonal to whether `-release` mode is active - for that see the
predefined versions `assert`, `D_NoBoundsChecks`, `D_Invariants`, etc.

View file

@ -1204,6 +1204,9 @@ void addDefaultVersionIdentifiers(const ref Param params, const ref Target tgt)
if (params.tracegc)
VersionCondition.addPredefinedGlobalIdent("D_ProfileGC");
if (driverParams.optimize)
VersionCondition.addPredefinedGlobalIdent("D_Optimized");
}
/**

View file

@ -0,0 +1,15 @@
/+
ARG_SETS: -version=Unoptimized
ARG_SETS: -O
+/
version (Unoptimized)
{
version (D_Optimized)
static assert(0);
}
else
{
version (D_Optimized) { /* expected */ } else
static assert(0);
}

View file

@ -117,6 +117,7 @@ fail_compilation/reserved_version.d(218): Error: version identifier `D_PreCondit
fail_compilation/reserved_version.d(219): Error: version identifier `D_PostConditions` is reserved and cannot be set
fail_compilation/reserved_version.d(220): Error: version identifier `D_ProfileGC` is reserved and cannot be set
fail_compilation/reserved_version.d(221): Error: version identifier `D_Invariants` is reserved and cannot be set
fail_compilation/reserved_version.d(222): Error: version identifier `D_Optimized` is reserved and cannot be set
---
*/
@ -240,6 +241,7 @@ version = D_PreConditions;
version = D_PostConditions;
version = D_ProfileGC;
version = D_Invariants;
version = D_Optimized;
// This should work though
debug = DigitalMars;
@ -351,3 +353,4 @@ debug = AVR;
debug = D_PreConditions;
debug = D_PostConditions;
debug = D_ProfileGC;
debug = D_Optimized;

View file

@ -107,6 +107,7 @@
// REQUIRED_ARGS: -version=D_PostConditions
// REQUIRED_ARGS: -version=D_ProfileGC
// REQUIRED_ARGS: -version=D_Invariants
// REQUIRED_ARGS: -version=D_Optimized
// REQUIRED_ARGS: -debug=DigitalMars
// REQUIRED_ARGS: -debug=GNU
// REQUIRED_ARGS: -debug=LDC
@ -211,6 +212,7 @@
// REQUIRED_ARGS: -debug=D_PostConditions
// REQUIRED_ARGS: -debug=D_ProfileGC
// REQUIRED_ARGS: -debug=D_Invariants
// REQUIRED_ARGS: -debug=D_Optimized
/*
TEST_OUTPUT:
---
@ -321,5 +323,6 @@ Error: version identifier `D_PreConditions` is reserved and cannot be set
Error: version identifier `D_PostConditions` is reserved and cannot be set
Error: version identifier `D_ProfileGC` is reserved and cannot be set
Error: version identifier `D_Invariants` is reserved and cannot be set
Error: version identifier `D_Optimized` is reserved and cannot be set
---
*/