Solved: How to disable warning for a macro?

After some tests I made something like Peter suggested. For best results it was necessary to make separate code for debug and release version. Here is full macro:

#define TTRACE_IF(Area, Level, Action) \
do { \
if (TdbgIsLogAllowed(&TdbgArea_##Area, Level)) { \
try { \
Action; \
_TDBG_PRETEND_POSSIBLE_EXCEPTION; \
} \
catch (…) { \
TTRACE(Area, Level, (“Unknown exception occured inside TTRACE_IF block.”));\
} \
} \
} while (0)

and helper macros:

#ifdef NDEBUG
// release version
__forceinline bool _TdbgPretendPossibleException() {
return(false);
}

#define _TDBG_PRETEND_POSSIBLE_EXCEPTION \
do { \
if (_TdbgPretendPossibleException()) throw(0); \
} while (0)

#else
// debug version
inline void _TdbgPretendPossibleException() {
}

#define _TDBG_PRETEND_POSSIBLE_EXCEPTION \
_TdbgPretendPossibleException()

#endif

For release compilation no additional code is generated. Even catch block is omitted if Action can’t throw an exception. For debug compilation there is no additional code in try block but catch block is generated always. It is no problem. If current release code was used for debug compilation, it generated code necessary to throw exception. That’s why I made it different.

It is amazing what has to be done for something as obvious: disable a warning for given macro expansion. Maybe it isn’t quite easy but PC-lint shows it isn’t impossible. Also, compiler shouldn’t crash with internal error 1001 if previous compilation generated something odd. Today I saw it many times.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http:://www.upek.com]


From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Peter Wieland[SMTP:xxxxx@windows.microsoft.com]
Reply To: Windows System Software Devs Interest List
Sent: Wednesday, June 23, 2004 5:30 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] How to disable warning for a macro?

I like to declare an inline function that returns true. The compiler
sees this as a real choice when processing this warning and the code
optimizer could potentially optimize out the entire branch when it
merges the inline function into the call site.

The optimizer can’t optimize out the branch if you use a global variable
because it could be changed, and I think the analyzer will determine
that you’re trying to trick it if you declare the variable as const (not
to mention that const is meaningless since it can be casted away so I
bet the optimizer won’t care about it)

I ***believe*** I’ve successfully made the compiler do this before. I
don’t remember entirely and I don’t have time right now to do an
experiment, but you might give it a try.

-p

This posting is provided “AS IS” with no warranties, and confers no
rights.

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Marc-Antoine Ruel[SMTP:xxxxx@pyxis.com]
Reply To: Windows System Software Devs Interest List
Sent: Wednesday, June 23, 2004 10:09 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] How to disable warning for a macro?

Talking about speed, forcing the compiler to include useless code (I mean
the catch block + compiler needing to set the try block) by using tricks is
no better than getting lots of warnings. Furthermore if you force access to
dummy global variables, compared to this, async is not that bad (I have to
look at the difference in IDA Pro to further discuss on this subject).

There is a difference. Enabling async exceptions influences all the code but forcing catch blocks in one macro influences this macro only. I tried to compile my simple trace test app with -EHa and it was 10% longer (90112 versus 81920 bytes).

I don’t mean I have a solution about this problem, I just mean you should
think about it twice before implementing a trick to make the compiler
include the catch block if you are caring about code size/speed. :slight_smile:

As you can see from my previous post, for release version unnecessary catch block isn’t generated. Only for debug version where it isn’t a problem. Almost optimal solution for me.

By the way, I originally thought you’d use the macro only on debug build so
I thought you could use the /wd flag in debug build (which I personally
don’t really care that much about warnings) and keep them in release builds
without this macro.

We use -W4 -WX for both debug and release versions. The macro is usually used in debug builds but sometimes we make release builds with traces enabled. And the warning would break nighty build. I prefer to not disable warnings globally; only when really necessary. It wasn’t the case. Unreachable code warning may be important even in debug version. Imagine: you work on debug version, warning disabled, you submit files to depot and next release build stops because of warning. It is better if all warnings all set the same way.

Exception-safety correctness is always an interesting and complex subject…

Yes. Definitelly uneasy. A bit OT here.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http:://www.upek.com]

It would be useful in this case and others if pragmas were implemented
as compiler intrinsic “functions” that could be embedded anywhere,
mid-macro or even mid-expression. I put “functions” in quotes because
although they would look like function calls, they would generate no
code and could be “callable” from the global scope.

Chuck

----- Original Message -----
From: “Michal Vodicka”
To: “Windows System Software Devs Interest List”
Sent: Thursday, June 24, 2004 6:47 AM
Subject: [ntdev] Solved: How to disable warning for a macro?

> After some tests I made something like Peter suggested. For best
results it was necessary to make separate code for debug and release
version. Here is full macro:
>
> #define TTRACE_IF(Area, Level, Action) <br>> do { <br>> if (TdbgIsLogAllowed(&TdbgArea_##Area, Level)) { <br>> try { <br>> Action; <br>> _TDBG_PRETEND_POSSIBLE_EXCEPTION; <br>> } <br>> catch (…) { <br>> TTRACE(Area, Level, (“Unknown exception occured inside
TTRACE_IF block.”));<br>> } <br>> } <br>> } while (0)
>
> and helper macros:
>
> #ifdef NDEBUG
> // release version
> __forceinline bool _TdbgPretendPossibleException() {
> return(false);
> }
>
> #define _TDBG_PRETEND_POSSIBLE_EXCEPTION <br>> do { <br>> if (_TdbgPretendPossibleException()) throw(0); <br>> } while (0)
>
> #else
> // debug version
> inline void _TdbgPretendPossibleException() {
> }
>
> #define _TDBG_PRETEND_POSSIBLE_EXCEPTION <br>> _TdbgPretendPossibleException()
>
> #endif
>
> For release compilation no additional code is generated. Even catch
block is omitted if Action can’t throw an exception. For debug
compilation there is no additional code in try block but catch block is
generated always. It is no problem. If current release code was used for
debug compilation, it generated code necessary to throw exception.
That’s why I made it different.
>
> It is amazing what has to be done for something as obvious: disable a
warning for given macro expansion. Maybe it isn’t quite easy but PC-lint
shows it isn’t impossible. Also, compiler shouldn’t crash with internal
error 1001 if previous compilation generated something odd. Today I saw
it many times.
>
> Best regards,
>
> Michal Vodicka
> UPEK, Inc.
> [xxxxx@upek.com, http:://www.upek.com]

On Thu, 24 Jun 2004 12:21:21 +0700, Chuck Batson
wrote:

I read somewhere that the updated C99 standard includes this, but not all
compilers have implemented it yet.

> It would be useful in this case and others if pragmas were implemented
> as compiler intrinsic “functions” that could be embedded anywhere,
> mid-macro or even mid-expression. I put “functions” in quotes because
> although they would look like function calls, they would generate no
> code and could be “callable” from the global scope.
>
> Chuck
>
> ----- Original Message -----
> From: “Michal Vodicka”
> To: “Windows System Software Devs Interest List”
> Sent: Thursday, June 24, 2004 6:47 AM
> Subject: [ntdev] Solved: How to disable warning for a macro?
>
>
>> After some tests I made something like Peter suggested. For best
> results it was necessary to make separate code for debug and release
> version. Here is full macro:
>>
>> #define TTRACE_IF(Area, Level, Action) <br>>> do { <br>>> if (TdbgIsLogAllowed(&TdbgArea_##Area, Level)) { <br>>> try { <br>>> Action; <br>>> _TDBG_PRETEND_POSSIBLE_EXCEPTION; <br>>> } <br>>> catch (…) { <br>>> TTRACE(Area, Level, (“Unknown exception occured inside
> TTRACE_IF block.”));<br>>> } <br>>> } <br>>> } while (0)
>>
>> and helper macros:
>>
>> #ifdef NDEBUG
>> // release version
>> __forceinline bool _TdbgPretendPossibleException() {
>> return(false);
>> }
>>
>> #define _TDBG_PRETEND_POSSIBLE_EXCEPTION <br>>> do { <br>>> if (_TdbgPretendPossibleException()) throw(0); <br>>> } while (0)
>>
>> #else
>> // debug version
>> inline void _TdbgPretendPossibleException() {
>> }
>>
>> #define _TDBG_PRETEND_POSSIBLE_EXCEPTION <br>>> _TdbgPretendPossibleException()
>>
>> #endif
>>
>> For release compilation no additional code is generated. Even catch
> block is omitted if Action can’t throw an exception. For debug
> compilation there is no additional code in try block but catch block is
> generated always. It is no problem. If current release code was used for
> debug compilation, it generated code necessary to throw exception.
> That’s why I made it different.
>>
>> It is amazing what has to be done for something as obvious: disable a
> warning for given macro expansion. Maybe it isn’t quite easy but PC-lint
> shows it isn’t impossible. Also, compiler shouldn’t crash with internal
> error 1001 if previous compilation generated something odd. Today I saw
> it many times.
>>
>> Best regards,
>>
>> Michal Vodicka
>> UPEK, Inc.
>> [xxxxx@upek.com, http:://www.upek.com]
>
>


#include <disclaimer.h></disclaimer.h>

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Chuck Batson[SMTP:xxxxx@cbatson.com]
Reply To: Windows System Software Devs Interest List
Sent: Thursday, June 24, 2004 7:21 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] Solved: How to disable warning for a macro?

It would be useful in this case and others if pragmas were implemented
as compiler intrinsic “functions” that could be embedded anywhere,
mid-macro or even mid-expression. I put “functions” in quotes because
although they would look like function calls, they would generate no
code and could be “callable” from the global scope.

Embedded compiler for ARC we use allows it. There are both standard #pragma and pragma which can be used inside macros. Very useful. Another approach is used in PC-lint. Behaviour can be controlled using comments (//lint or /*lint */) which can be also used in macros (the second one only, of course). Messages can controlled for symbols, branch, expression etc.

However, in this case more compiler changes would be necessary. Even if standard #pragmas are used around macro “call”, it doesn’t work. It is necessary to use them around whole function which “calls” a macro.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http:://www.upek.com]

> ----------

From: xxxxx@lists.osr.com[SMTP:xxxxx@lists.osr.com] on behalf of Jakob Bohm[SMTP:xxxxx@danware.dk]
Reply To: Windows System Software Devs Interest List
Sent: Thursday, June 24, 2004 11:30 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Solved: How to disable warning for a macro?

I read somewhere that the updated C99 standard includes this, but not all
compilers have implemented it yet.

It would be nice if VC implement C99. I’d appreciate especially macros with variable number of parameters. Hopefully we won’t have to wait so long as for fixed STL.

Best regards,

Michal Vodicka
UPEK, Inc.
[xxxxx@upek.com, http:://www.upek.com]