Suppressing -Wunused-value with gcc - c

Considering this piece of code:
#define STC_ASSERT(X,Msg) \
(!!sizeof(struct{char STC_ASSERT;_Static_assert((X),Msg "");}))
#define A 43
#define B 42
#define C (STC_ASSERT(A-B>=0,"")?(A-B):0)
enum { c = C };
int main()
{
STC_ASSERT(1,""); //can this not generate a warning on gcc?
}
Is there anything I can do inside the STC_ASSERT _Static_assert wrapper to prevent the line in main from generating an -Wunused-value warning when compiling with gcc -Wall -Wextra?
(_Pragma with GCC diagnostic push/pop doesn't work here. For a macro like #define FOO 42, embracing it in an expression statement ( ({ 42; }) ) would do the trick but that would prevent the macro from being used in contexts where an Integer Constant Expression is required (such as the enum definition))

Related

Undefining constant inside macro after single use

I would like to have macro which will undefine constant passed to it when called.
Something like this:
#define CONSTANT1 123
#define macro(const) \
#ifdef const \
const \
#undef const \
#else \
#error "constant already used once" \
#endif
int main(){
int a = macro(CONSTANT1); // a = 123
int b = macro(CONSTANT1); // <- preprocessor error "constant already used once"
return 0;
}
It is possible to archive this functionality with preprocessor?
I don't think it is possible to get it with standard C preprocessor but it is possible to do it with GCC/CLANG pragmas like push_macro/pop_macro:
// define a macro that will generate error on expansion
#define CONSTANT1 _Pragma("GCC error \"CONSTANT1 expanded more than once\"")
// save this macro and undefine it
#pragma push_macro("CONSTANT1")
#undef CONSTANT1
// let CONSTANT1 expand to 123, but replace with
// previous error-generation macro
#define CONSTANT1 123 _Pragma("pop_macro(\"CONSTANT1\")")
int a = CONSTANT1;
int b = CONSTANT1;
Compiling with gcc/clang produced:
prog.c:8:11: error: CONSTANT1 expanded more than once
8 | int b = CONSTANT1;
Note that pragmas push_macro/pop_macro are quite portable and they are supported by GCC,CLANG,MSVS and Intel C Compiler.
A bit more portable version of failing CONSTANT1 could be:
#define CONSTANT1 sizeof(struct {_Static_assert(0, "CONSTANT1 expanded more than once"); int x; })
It requires C11 compatible compiler.

C macros, what's the meaning of ((void)0)?

Given the following code written according to the C99 standard:
#define LOW 1
#define MEDIUM 2
#define HIGH 3
#define LOGGING_LEVEL HIGH
#if LOGGING_LEVEL >= MEDIUM
#define LOG_MEDIUM(message) printf(message)
#else
#define LOG_MEDIUM(message) ((void)0)
#endif
void load_configuration() {
//...
LOG_MEDIUM("Configuration loaded\n");
}
what's the purpose of ((void)0) I searched the web a lot but nothing found regarding this.
Plus, why didn't we wrote ; after using printf(message)
The void-cast fixes a compiler warning. Here's an analogous testcase:
int main(void)
{
0; // generates "foo.c:3:2: warning: statement with no effect"
(void)0;
return 0;
}
and (using a script to add gcc's warning flags) you see a warning for the line without a cast:
$ gcc-stricter -c foo.c
foo.c: In function ‘main’:
foo.c:3:2: warning: statement with no effect [-Wunused-value]
0;
^
The extra parentheses and lack of semicolon allow the macro's result to be used interchangeably with the printf.
Main idea is to exclude all LOG_MEDIUM if the criteria was not meet.
After compilation those calls will not affect functionality.

What is the best way to mark macro as deprecated?

I know how to mark enums/functions as deprecated by using
__attribute__ ((deprecated)). But how can I mark constant macro
as deprecated?
#define MACRO1 4 //This is deprecated macro
GCC (and possibly others)
__attribute__((deprecated))
For your particular example with just a constant expression, you can use this:
Change
#define X (4)
to
#define X_old (4)
and then add
const int dep __attribute__((deprecated)) = 0;
#define X ((void)dep, X_old)
Addition also works:
#define X (X_old + dep)
For a procedure macro you can do this:
#define P_old do { ... } while(0)
#define P do { (void)dep; P_old; } while(0)
The only function of (void) is to suppress warnings. Thanks Kevin.
#pragma message
Another solution is to put all deprecated macros in a separate header file and use pragma. You could combine this with #ifdef and such:
#pragma message ("This header contains deprecated macros")
All compilers
Unreferenced label
Use an unreferenced label:
#define P_old do { ... } while(0)
#define P do { P_IS_DEPRECATED: P_old; } while(0)
This does not work for constant expression macros and requires you to compile with -Wall to get a warning. Will trigger error if used more than once.
Unused variable:
#define P_old do { ... } while(0)
#define P do { int P_IS_DEPRECATED; P_old; } while(0)
Does not work on constant expressions either. Also requires -Wall but can be used more than once.
Sidenote
Remember to encapsulate constant expression macros in parentheses. The macro #define X 2+3 would make an expression like 2*X to expand to 2*2+3 instead of 2*(2+3).

gcc: how to detect bad `bool` usage

Is there some way to detect the bad usage of bool values in code like
#include <stdbool.h>
void *foo(void)
{
return false;
}
int bar(void)
{
return true;
}
Both functions are accepted by gcc (8.3.1) and clang (7.0.1) without any warnings
$ gcc -Wall -W -pedantic -c x.c
$ clang -Xclang -analyzer-checker=alpha --analyze -Wall -W -pedantic -c x.c
$ clang -Wall -W -pedantic -c x.c
$
Compiling as C++ code would detect the problem in foo() but is not an option but rest of code is C, not C++.
Are there other (-W) options or switches which would create diagnostics for these cases?
C defines the <stdbool.h> macros true and false as expanding to integer constant expressions of value 1 and 0 respectively. Since they're ints and bool (_Bool) in itself is an integer type, any such usage is equally valid. Even the value of the boolean expressions in C is an int and not a bool, so there is not much help for you with the bar function.
However, foo is a different beast - if the return value were true then it would be caught right away because 1 is not convertible to a pointer. false, having the integer constant value 0 is a null-pointer constant and will be converted to null pointer. You could perhaps catch the incorrect use by replacing the #include <stdbool.h> with something that does the inclusion but defines false as say 0.0 which is a falsy value but not an integer constant expression.
Make the example less trivial:
bool x;
void *foo(void)
{
return x;
}
int bar(void)
{
return x;
}
and it want compile at all.
usually true and false are just definitions and have value 1 and 0
From the stdbool.h header file
#ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#else /* __cplusplus */
in your first example you just return zero and most compilers will not warn as they treat it as NULL. Try to return true and you will get the warning.

Compile time assertion as part of an expression but without _Static_assert

Ultimately, I want a compile-time-const macro that in itself includes an assertion.
With a real _Static_assert, I can do something like
#define CEXPR_MACRO_WITH_ASSERTION(Assertion) sizeof(struct{char c; _Static_assert(Assertion,""); })?0:42
(meant for stuff like "compile-time-assert" that the macro value computation won't overflow on any target, and I'd like to keep the assertion in the macro so that it's tightly coupled with the value) but compilers like tcc don't have static asserts so I'd need to emulate it.
#define STATIC_ASSERT(Cexpr,Msg) extern STATIC_ASSERT[(Cexpr)?1:-1]
is a common way to do it but with that extern I can't use it in a struct so I could split it in two
#define STATIC_ASSERT(Cexpr,Msg) extern STATIC_ASSERT_(Cexpr,Msg)
#define STATIC_ASSERT_(Cexpr,Msg) char STATIC_ASSERT[sizeof(char [((Cexpr))?1:-1])] /*ignore Msg for simplicity's sake*/
and use the underscore version in the CEXPR_MACRO_WITH_ASSERTION, but in function context, this will give false positives on compilers that support structs with VLAs in them:
#define STATIC_ASSERT(Cexpr,Msg) extern STATIC_ASSERT_(Cexpr,Msg)
#define STATIC_ASSERT_(Cexpr,Msg) char STATIC_ASSERT[sizeof(char [((Cexpr))?1:-1])]
#define CEXPR_MACRO_WITH_ASSERTION(Assert) (sizeof(struct{char c; STATIC_ASSERT_(Assert,""); })?0:42)
int main(void)
{
int x = 0;
CEXPR_MACRO_WITH_ASSERTION(x);
} //compiles on tcc and gcc (clang rejects it because of the vla in a struct)
so I effectively need:
#define STATIC_ASSERT_(Cexpr,Msg) char STATIC_ASSERT[sizeof(char [((Cexpr)&&ENFORCE_ICEXPR(Cexpr))?1:-1])]
Now I realize on tcc in particular, ENFORCE_ICEXPR (enforce integer constant expression) could be simply replaced with __builtin_constant_p but I was curious if I could do it without the platform dependency.
So I thought I could test Cexpr by trying to assign it to an enum constant and I came up with:
#define ENFORCE_Z(X) _Generic(0LL+(X),ullong:(X),llong:(X)) /*could be just `+(X)` cuz I don't care about floats*/
#define ENFORCE_ICEXPR(X) sizeof( void (*)(enum { ENFORCE_ICEXPR = (int)ENFORCE_Z(X) } ) )
but this gets gcc and clang complaining (unsilencably in gcc's case) about the enum not being visible outside of the declaration (which, incidentally, was the intention here) so I resorted to
#define ENFORCE_ICEXPR(X) sizeof(enum { BX_cat(ENFORCE_ICEXPR__,__COUNTER__) = (int)ENFORCE_Z(X) })
relying on the nonstandard magic macro, __COUNTER__.
My question is, is there a better way to write ENFORCE_ICEXPR(X)?
Perl uses a bit-field instead of an array to define a static_assert fallback:
#define STATIC_ASSERT_2(COND, SUFFIX) \
typedef struct { \
unsigned int _static_assertion_failed_##SUFFIX : (COND) ? 1 : -1; \
} _static_assertion_failed_##SUFFIX PERL_UNUSED_DECL
#define STATIC_ASSERT_1(COND, SUFFIX) STATIC_ASSERT_2(COND, SUFFIX)
#define STATIC_ASSERT_DECL(COND) STATIC_ASSERT_1(COND, __LINE__)
No compiler implements variable length bit-fields.

Resources