www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - scoped compiler options (cf C++ pragma push/pop)

--089e01294e7ca18ad704de663f0f
Content-Type: text/plain; charset=ISO-8859-1

A)
a number of C++ compilers support temporarily modifying compiler flags in a
given scope, via push/pop operations:

eg:

----
//normal code
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wc++98-compat"
//special code
#pragma GCC diagnostic pop
//normal code
----

(many other use cases).

I'd like to have that in D to have finer grained setting of compiler &&
conditional compilation flags.

Proposed syntax for D: use

----
//normal code
scope{
version=myversion;
import std.file;
extern(C):
//special code
}
//normal code
//std.file is no longer in scope, version(myversion) no longer holds, code
no longer extern(C)
----

Note, it turns out the above syntax compiles in D2.063 (is that a BUG?),
even though it seems undocumented and appears to currently have no behavior
(ie in code above currently dmd will allow it but version=myversion will
take effect till after the closing bracket).

B)
I'd like to be able to set compiler flags inside a given scope as to
selectively enable/disable certain compiler options, for example allowing
deprecated features only in a given scope, or have a debug build with
bounds checking and asserts enabled except for a certain part of the code
that would otherwise make the debug code too slow, or even having a
check_integer_overflow compiler flag (see recent threads) that would be
disabled for code that relies on overflow behavior.

Proposed syntax:
----
//normal code
pragma(scope,dmd,"-noboundscheck -d -vtls"){ //dmd because flags could be
compiler specific
  import std.algorithm;
  //code using *custom* options, including for functions inside
std.algorithm and its dependencies
}
//normal code
void main(){
  import std.algorithm;
  //code using *normal* options, including for functions inside
std.algorithm and its dependencies
}
----

The behavior of that would be to apply given compiler options recursively
for any code inside the scope. The compiler could then compile several
versions of a given modules (one per compiler option it appears with).
Using a caching mechanism would avoid redoing un-necessary work. The name
mangling for custom code would contain a hash of the compiler options.

--089e01294e7ca18ad704de663f0f
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>A)</div><div>a number of C++ compilers support temporarily modifying c=
ompiler flags in a given scope, via push/pop operations:</div><div><br></di=
v><div>eg:</div><div><br></div><div>----</div><div>//normal code</div><div>
<div>#pragma GCC diagnostic push</div><div>#pragma GCC diagnostic ignored &=
quot;-Wc++98-compat&quot;</div></div><div>//special code</div><div>#pragma =
GCC diagnostic pop</div><div><div>//normal code</div><div></div></div><div>
<div>----</div></div><div><br></div><div>(many other use cases).</div><div>=
<br></div><div>I&#39;d like to have that in D to have finer grained setting=
 of compiler &amp;&amp; conditional compilation flags.</div><div><br></div>
<div>Proposed syntax for D: use=A0</div><div><br></div><div>----</div><div>=
//normal code</div><div></div><div>scope{</div><div>version=3Dmyversion;</d=
iv><div>import std.file;</div><div>extern(C):</div><div><div>//special code=
</div>
</div><div>}</div><div><div><div><div>//normal code</div><div>//std.file is=
 no longer in scope, version(myversion) no longer holds, code no longer ext=
ern(C)</div><div></div></div><div></div></div></div><div>----</div><div>
<br></div><div>Note, it turns out the above syntax compiles in D2.063 (is t=
hat a BUG?), even though it seems undocumented and appears to currently hav=
e no behavior (ie in code above currently dmd will allow it but version=3Dm=
yversion will take effect till after the closing bracket).=A0</div>
<div><br></div><div>B)</div><div><div>I&#39;d like to be able to set compil=
er flags inside a given scope as to selectively enable/disable certain comp=
iler options, for example allowing deprecated features only in a given scop=
e, or have a debug build with bounds checking and asserts enabled except fo=
r a certain part of the code that would otherwise make the debug code too s=
low, or even having a check_integer_overflow compiler flag (see recent thre=
ads) that would be disabled for code that relies on overflow behavior.=A0</=
div>
</div><div><br></div><div>Proposed syntax:</div><div>----</div><div>//norma=
l code=A0</div><div>pragma(scope,dmd,&quot;-noboundscheck -d -vtls&quot;){ =
//dmd because flags could be compiler specific</div><div>=A0 import std.alg=
orithm;</div>
<div>=A0 //code using <b>custom</b> options, including for functions inside=
 std.algorithm and its dependencies</div><div>}</div><div><div>//normal cod=
e=A0</div><div>void main(){</div><div>=A0 import std.algorithm;</div><div>=
=A0 //code using <b>normal</b> options, including for functions inside std.=
algorithm and its dependencies</div>
<div>}</div><div>----</div></div><div><br></div><div>The behavior of that w=
ould be to apply given compiler options recursively for any code inside the=
 scope. The compiler could then compile several versions of a given modules=
 (one per compiler option it appears with). Using a caching mechanism would=
 avoid redoing un-necessary work. The name mangling for custom code would c=
ontain a hash of the compiler options.</div>
<div><br></div><div><br></div>

--089e01294e7ca18ad704de663f0f--
Jun 05 2013