Based on this question How to catch empty defined macro with gcc? I have another problem. How to catch undefined macro in preprocessor #if condition? Example code:
#include <stdio.h>
int main()
{
#if ENABLE_SOMETHING == 1
... do something ..
#endif
return 0;
}
Is there a way to catch error/warning when ENABLE_SOMETHING is not set using gcc compiler(maybe some flag)? Or maybe there are external tools which I can use?
I know than i can write something like this :
#ifndef ENABLE_SOMETHING
#error "ENABLE_SOMETHING undefined!"
#endif
But I have a huge amount of different defines(ENABLE_STH1, ENABLE_STH2, ENALBE_STH3 ... etc.) in code and i don't want to fix this manually. I'm looking for some automatic solution for our project.
Is there a way to catch error/warning when ENABLE_SOMETHING is not set
using gcc compiler(maybe some flag)?
With GCC you can use the -Wundef flag.
From the official documentation
-Wundef
Warn if an undefined identifier is evaluated in an #if directive. Such identifiers are replaced with zero.
EDIT:
For example, this C-code:
#include <stdio.h>
int main(void)
{
#if UNDEFINED_MACRO
printf("hi mum!\n");
#endif
return 0;
}
... compiled with GCC and the -Wundef flag yields this:
$ gcc undef.c -Wundef
undef.c: In function 'main':
undef.c:5:5: warning: "UNDEFINED_MACRO" is not defined [-Wundef]
#if UNDEFINED_MACRO
^
Let's assume you have this code and it compiles, but you don't know if MAX_N_LENGTH is a macro, or if it's something else:
int main()
{
int a = MAX_N_LENGTH; // MAX_N_LENGTH could also be an int declared somewhere else
return 0;
}
You can check whether it actually is a macro like this:
#ifdef MAX_N_LENGTH
printf("MAX_N_LENGTH is a macro.\n");
#else
printf("MAX_N_LENGTH is NOT macro.\n");
#endif // MAX_N_LENGTH
Of course, if that ever is an issue, I'd rethink my naming conventions.
You could try something like the following:
#ifndef MAX_N_LENGTH
#warning "MAX_N_LENGTH is undefined"
int array[16];
#else
int array[MAX_N_LENGTH + 1];
#endif
You can test if a macro is defined in a #if preprocessor expression with defined(ENABLE_SOMETHING):
#if !defined(ENABLE_SOMETHING)
#error ENABLE_SOMETHING is not defined
#endif
You can handle macros with an empty definition this way:
#if ENABLE_SOMETHING + 0 == 1
/* ENABLE_SOMETHING is defined and not 0 or empty */
#endif
Related
Based on this question How to catch empty defined macro with gcc? I have another problem. How to catch undefined macro in preprocessor #if condition? Example code:
#include <stdio.h>
int main()
{
#if ENABLE_SOMETHING == 1
... do something ..
#endif
return 0;
}
Is there a way to catch error/warning when ENABLE_SOMETHING is not set using gcc compiler(maybe some flag)? Or maybe there are external tools which I can use?
I know than i can write something like this :
#ifndef ENABLE_SOMETHING
#error "ENABLE_SOMETHING undefined!"
#endif
But I have a huge amount of different defines(ENABLE_STH1, ENABLE_STH2, ENALBE_STH3 ... etc.) in code and i don't want to fix this manually. I'm looking for some automatic solution for our project.
Is there a way to catch error/warning when ENABLE_SOMETHING is not set
using gcc compiler(maybe some flag)?
With GCC you can use the -Wundef flag.
From the official documentation
-Wundef
Warn if an undefined identifier is evaluated in an #if directive. Such identifiers are replaced with zero.
EDIT:
For example, this C-code:
#include <stdio.h>
int main(void)
{
#if UNDEFINED_MACRO
printf("hi mum!\n");
#endif
return 0;
}
... compiled with GCC and the -Wundef flag yields this:
$ gcc undef.c -Wundef
undef.c: In function 'main':
undef.c:5:5: warning: "UNDEFINED_MACRO" is not defined [-Wundef]
#if UNDEFINED_MACRO
^
Let's assume you have this code and it compiles, but you don't know if MAX_N_LENGTH is a macro, or if it's something else:
int main()
{
int a = MAX_N_LENGTH; // MAX_N_LENGTH could also be an int declared somewhere else
return 0;
}
You can check whether it actually is a macro like this:
#ifdef MAX_N_LENGTH
printf("MAX_N_LENGTH is a macro.\n");
#else
printf("MAX_N_LENGTH is NOT macro.\n");
#endif // MAX_N_LENGTH
Of course, if that ever is an issue, I'd rethink my naming conventions.
You could try something like the following:
#ifndef MAX_N_LENGTH
#warning "MAX_N_LENGTH is undefined"
int array[16];
#else
int array[MAX_N_LENGTH + 1];
#endif
You can test if a macro is defined in a #if preprocessor expression with defined(ENABLE_SOMETHING):
#if !defined(ENABLE_SOMETHING)
#error ENABLE_SOMETHING is not defined
#endif
You can handle macros with an empty definition this way:
#if ENABLE_SOMETHING + 0 == 1
/* ENABLE_SOMETHING is defined and not 0 or empty */
#endif
I have following code in C:
int do_something(void);
#ifdef SOMETHING
#define DO_SOMETHING() do_something()
#else
#define DO_SOMETHING() 0
#endif
This code produced warning "statement with no effect" when compiled without SOMETHING defined. I am trying to fix it, but there is one problem - code which uses this macro sometimes checks that "return value" and sometimes ignores it. Because of this I cannot use the easiest solution - casting to void in macro itself.
Is it possible to write macro which allows to compare "returned value" and does not produce this warning when it is ignored?
I use gcc to compile my code.
Three possible solutions:
Define a do_nothing function, which will get optimized out by gcc:
int do_something(void);
int do_nothing(void) { return 0; }
#ifdef SOMETHING
#define DO_SOMETHING() do_something()
#else
#define DO_SOMETHING() do_nothing()
#endif
Or, modify the do_something implementation to move the #ifdef check there
int do_something(void)
{
#ifndef SOMETHING
return 0;
#endif
// Your implementation here
}
You can also ignore the warning using #pragma directives.
By the way, which version of gcc and with which flags are you compiling? gcc -Wall -pedantic with GCC 4.9 doesn't produce the warning.
#include <stdio.h>
int do_something(void);
#ifdef SOMETHING
#define DO_SOMETHING() do_something()
#define DO_SOMETHING_ASSIGN() do_something()
#else
#define DO_SOMETHING()
#define DO_SOMETHING_ASSIGN() 0
#endif
int do_something(void)
{
static int cnt=0;
/* code for do something here */
++cnt;
printf("do_something has been called %d %s!\n",cnt,
(cnt==1)?"time":"times");
return cnt;
}
void main(void)
{
int x;
DO_SOMETHING();
x=DO_SOMETHING_ASSIGN();
printf("%d %d\n",x,DO_SOMETHING_ASSIGN());
puts("end!!!");
}
I hope this is usefull to you!
If you save this code as main.c, you may compile it with:
gcc main.c -o main
when you run main, you obtain the following output:
0 0
end!!!
If you compile it with:
gcc main.c -DSOMETHING -o main
When you run main, you obtain the following output:
do_something has been called 1 time!
do_something has been called 2 times!
do_something has been called 3 times!
2 3
end!!!
If I define
#if SOMETHING
#endif
and I have not defined SOMETHING anywhere. Would the code inside #if compile ?
When the name used in the argument expression of #if is not defined as a macro (after all other macro replacement is finished), it is replaced with 0. This means that your #if SOMETHING will be interpreted as #if 0. The code under #if will be removed by preprocessor.
The rule applies to more complex expressions as well. You can do
#define A 42
#if A + B
which will evaluate to #if 42, since unknown name B is interpreted as 0
No, as long as SOMETHING is also not one of the predfined macros or a macro passed on the commandline to the compiler.
I think in C99 it defaults to 0 while in C89 it does not as here its giving me an error
Source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define A 42
#define B
int main(int argc, char *argv[]){
int c;
#if A
c = A + B;
#endif
printf("%d\n",c);
return 0;
}
Error :
error: expected expression before ‘;’ token
No, because as an undefined macro, it would not necessarily be replaced with anything... but that is probably compiler specifc, and not to be trusted.
You may want to use:
#ifdef DEFINED_WORD
#ifndef DEFINED_WORD
Where DEFINED_WORD is declared by a #define, like: #define DEFINED_WORD. The ifdef construct ONLY compiles the code between that and the #endif if DEFINED_WORD was defined. The opposite is true of #ifndef. You can nest them like so:
#ifdef DEFINED_WORD
printf ( "DEFINED_WORD is defined\n" );
#ifndef DEFINED_WORD
printf ( "This line will NEVER print\n" );
#endif
printf ( "This line will print if DEFINED_WORD is defined\n" );
#endif
printf ( "This line will ALWAYS print.\n" );
In my code, I have constructs like:
#ifdef DEBUG
printf ( "array[%d] = %d\n", i, array[i] );
#endif
To deactivate all of the DEBUG code, just comment out the line that you MUST have before calling that #ifdef (top of the source file, or in an included header which you included at the top of the source file, in good form). #define DEBUG
Hope that helps.
We have a function macro #define FOO(arg) foo(arg) with int foo(const char* bar);. When NDEBUG is defined FOO is defined as #define FOO(arg) 0, however this causes many compiler warnings because in many cases FOO's return value is not used. The solution should work with with ANSI C compilers and cause no warnings. I've tried:
(void)0: can't be assigend to variable
static int foo(const char* bar) { return 0; } : causes unused static function warning in some modules
static inline int foo(const char* bar) { return 0; } : only works with C99 compilers
Thanks for your help!
edit1:
It's somewhat like a trace macro and used all over the project. Mostly it's just used as a statement like FOO("function x called");, but in a few cases I saw if (FOO("condition a")) { /* some more debug output */ }. With NDEBUG defined and optimization enabled nothing should be left of FOO. I didn't come up with this, but I have to clean up this mess :).
edit2: I should add that for gcc release builds these flags are used: -O3 -Wall -ansi
edit3: For now I'm going with __inline int dummy() { return 0; }. __inline works with both VisualC and GCC in ansi mode.
I guess it's a little bit compiler dependent but this should work:
#ifndef NDEBUG
#define FOO(arg) foo(arg)
#else
#define FOO(arg) ((int)0)
#endif
It prevents the "expression has no effect" warning, it does nothing and its value when used is still 0.
EDITED
It seems it's something not so portable so (now) you have these conditions:
(0) or ((int)0) work at least on VC 2010.
__noop should work on any version of VC after 2003.
VC6 is not a problem because it doesn't emit the C4555 warning at all. For other compilers you may use:
((void)0, 0) It may work on a lot of compilers (maybe it's the more portable one?).
inline int foo(const char* bar) { return 0; } works with any other C99 compiler (as you wrote you may need to declare it as static on gcc).
For any other prehistoric C compiler use the solution pointed by #Jobs: abs(0)
What you could do to prevent the warning is the following:
#ifndef NDEBUG
#define FOO(arg) foo(arg)
#else
#define FOO(arg) abs(0)
#endif
I'm not saying this is ideal (you'd have to make sure stdlib.h is included everywhere, for example) but it does prevent the warning.
I'd do something that is dependent on the C version. In the header file:
#if __STDC_VERSION__ > 199900L
inline int foo(const char* bar) { return 0; }
#else
int foo(const char* bar);
#endif
in one compilation unit
#if __STDC_VERSION__ < 199900L
int foo(const char* bar) { return 0; }
#else
int foo(const char* bar);
#endif
or use for the oldish C version something like Job's answer, that is a function that is certain to be optimized out but that doesn't produce the warning.
I have the following code, and while compiling it with gcc-4.6 I get warning:
warning: variable ‘status’ set but not used [-Wunused-but-set-variable]
#if defined (_DEBUG_)
#define ASSERT assert
#else /* _DEBUG_ */
#define ASSERT( __exp__ )
#endif
static inline void cl_plock(cl_plock_t * const p_lock)
{
status_t status;
ASSERT(p_lock);
ASSERT(p_lock->state == INITIALIZED);
status = pthread_rwlock_unlock(&p_lock->lock);
ASSERT(status == 0);
}
When _DEBUG_
flag isn't set I get the warning.
Any ideas how can I workaround this warning?
You can change your ASSERT macro to:
#if defined (_DEBUG_)
#define ASSERT assert
#else /* _DEBUG_ */
#define ASSERT( exp ) ((void)(exp))
#endif
If the expression has no sideeffects, then it should still be optimised out, but it should also suppress the warning (if the expression does have side-effects, then you would get different results in debug and non-debug builds, which you don't want either!).
The compiler option to turn off unused variable warnings is -Wno-unused.
To get the same effect on a more granular level you can use diagnostic pragmas like this:
int main()
{
#pragma GCC diagnostic ignored "-Wunused-variable"
int a;
#pragma GCC diagnostic pop
// -Wunused-variable is on again
return 0;
}
This is, of course, not portable but you can use something similar for VS.
You could surround the variable declaration of status with a #ifdef clause.
#ifdef _DEBUG_
status_t status
#endif
EDIT: You have to surround the call also:
#ifdef _DEBUG_
status = pthread_rwlock_unlock(&p_lock->lock);
#else
pthread_rwlock_unlock(&p_lock->lock);
#endif
or you can switch off the error message.