https://github.com/JacksonAllan/CC/blob/42a7d810274a698dff87...
Specifically, if (arg)==(arg) is not a constant expression, then it could have side effects.
However, this mechanism does generate some annoying false positives, as shown below:
// Create a map with int keys and values that are vectors of floats:
map( int, vec( float ) ) our_map;
init( &our_map );
// Create a vector of floats:
vec( float ) our_vec;
init( &our_vec );
push( &our_vec, 1.23f );
// Insert the vector into the map.
insert( &our_map, 456, our_vec );
// Generates a warning because get checks its first argument for side
// effects and the compiler can't tell that the first argument of the
// outermost get has none:
printf( "%f", *get( get( &our_map, 456 ), 0 ) );
// The "proper", albeit cumbersome, way to achieve the same thing without a
// warning:
vec( float ) *itr = get( &our_map, 456 );
printf( "%f", *get( itr, 0 ) );
warning: expression in static assertion is not an integer constant expression [-Wpedantic]
Instead you can use the sizeof + compound literal with array type, use the comma operator to preserve the type of the expression and cast the result of sizeof to void to suppress the warning: #define C(x) ( (void)sizeof( (char [(int)(x) || 1]){0} ), (x) )
The only problem is that it doesn't support floating point expressionsThank you! Separately, but related: fuck you, Google! (every time I have to deal with protobuf in C++, I curse Google and their "we don't give a shit about signed vs unsigned comparisons").
In my fork of chibicc (a small C11 compiler) there are plenty of additional logic that were implemented solely to play nice with C tricks from real world projects that could have been easier if they target later standards. The most recent being how curl's build script determines the size of primitive types: they use (sizeof(T) == N) as a case index and expect the compiler to error on case-duplication[1], I had to add a backtracking loop to check exactly that[2]. I'm not complaining as more error checks isn't a bad thing, however, I'll advise programmers willing to invest in obscure tricks to actually test them on obscure compilers (instead of just flipping -std).
[1]: https://github.com/curl/curl/blob/339464432555b9bd71a5e4a4c4...
[2]: https://github.com/fuhsnn/slimcc/blob/54563ecae8480f836a0bb2...
Why not push/pop warnings to ignore in the library?
_Pragma("GCC diagnostic push")
_Pragma("GCC diagnostic ignored \"-Wshadow\"")
int a = 1;
{
int a = 2;
}
_Pragma("GCC diagnostic pop")
I seem to remember there's actually a solution for this .. at least on clang and I think MSVC .. you can programmatically turn warnings on/on with the _Pragma() macro. I don't remember exactly what you put in it, but it's designed specifically for this kind of macro nonsense
Y_Y•4h ago
https://en.cppreference.com/w/c/language/constexpr
lpribis•3h ago
cmptrnerd6•3h ago
Y_Y•5m ago
Aardwolf•3h ago