Reputation: 9436
I am trying to use #pragma
inside #define
. I am doing something like this:
// I assume this is a correct syntax. Please point out if it is wrong
#define COVERAGE(x) _Pragma (Coverage_tool #x)
And then inside the code I am using it as
COVERAGE(off) or COVERAGE(on).
Here on and off are strings.
But when I do this I get a compilation error saying "expected ) near off".
I also tried "off"
/"on"
with quotes (regular strings.) But still same error.
Any idea what is the problem?
Upvotes: 3
Views: 3135
Reputation: 665
For Visual Studio 2005, the __pragma
keyword is undocumented. It is documented and available in Visual Studio 2008. Note that you need two underscore characters and it's all lowercase.
Pragma Directives and the __Pragma Keyword
Upvotes: 0
Reputation: 340356
I think that strictly speaking, Lindydancer's solution needs to be of the form:
#define COVERAGE(x) PRAGMA(Coverage_tool x)
#define PRAGMA(x) _Pragma(#x)
This is the form the standard uses in an example, and it needs to be this way because _Pragma
operator processing occurs in a translation phase before string literal concatenation. _Pragma
is defined in such a way that it'll only deal with stripping off the leading and trailing quotes when manufacturing the corresponding #pragma
- not any other ones that are from 'concatenated' literals.
However, your compiler might be tolerant of the other approach (GCC isn't).
Note: if the #pragma Coverage_tool
pragma needs quotes around the on
/off
operand, then the COVERAGE
macro needs to be:
#define COVERAGE(x) PRAGMA(Coverage_tool #x)
Note #2: If you're using Microsoft C, I think you want:
#define COVERAGE(x) PRAGMA(Coverage_tool x)
#define PRAGMA(x) __pragma(x)
because Microsoft's directive is spelled slightly differently and, more annoyingly, doesn't want quotes surrounding the argument to the operator.
But Coverage_tool
isn't a documented pragma supported by MSVC, so I think there's still some important information missing.
Since you have some code coverage tool outside of the compiler that's processing this pragma, I think you'll need to hide it from the compiler. The tool will probably define some macro name that it recognizes when it's processing that won't be defined when the compiler is doing its work. For example, lint will define the macro lint
when it's processing and Microsoft's resource compiler will define RC_INVOKED
.
Let's assume that your code coverage tool defines COVERAGE_TOOL
when it's running. You might make both tools happy with something like:
#if COVERAGE_TOOL
#define COVERAGE(x) PRAGMA(Coverage_tool x)
#define PRAGMA(x) _Pragma(#x)
#else
#define COVERAGE(x)
#endif
But I'm just guessing. I would expect that the docs for the coverage tool would be quite explicit about how these directives need to be integrated into your code if it supports MSVC - you should look there for details (or ask the vendor).
Upvotes: 7