0

Is it possible to have the compiler error/warning diagnostic output a compile-time computed numeric value in C11 or C17 (i.e. not using templates)? The below link does this in C++ using template magic. The intention is to use this as a _Static_assert replacement which prints the values of the non-equal failed expression. Ideally, it should be able to evaluate the expression as true or false, and print only when it fails evaluation.

This is obviously compiler-dependent, so assume GCC.

Display integer at compile time in static_assert()

Jetski S-type
  • 1,138
  • 2
  • 16
  • 32
  • What does your "computed numeric value that is a compile-time integer value" look like? – Swordfish Nov 15 '18 at 00:53
  • e.g. sizeof(struct X) – Jetski S-type Nov 15 '18 at 00:55
  • C17 is really just C11 with some DRs fixed. No new functionality was included so it's unusual to see it referred to as a specific iteration of the standard. – paxdiablo Nov 15 '18 at 00:55
  • there is no way, even in c++. Only a compile-time string literal can be used as a second argument, no dynamic building. – Serge Nov 15 '18 at 03:32
  • @Serge You didn't click the link - click it, and see it done in C++ with templates. (Hint: not with static_assert keyword) – Jetski S-type Nov 15 '18 at 03:38
  • @JetskiS-type `static_assert` is defined in assert.h as `_Static_assert`. There is no way for it to print your information. It is possible though for some compilers to print diagnostic related to some situations with extra info, which was exploited in the link. It is not related to the keyword you asked for. Those depend on the compiler and are not standard in any way. – Serge Nov 15 '18 at 11:51
  • Apologies, @Serge, let me rephrase it: "to make our own _Static_assert replacement for GCC that prints out two numeric values that failed to be equal." Indeed, it is not literally using _Static_assert. Question edited to clarify this. – Jetski S-type Nov 15 '18 at 22:35
  • Displaying any compile-time integer which is an integer constant is no problem - could be done with "stringification macros". Displaying the result of sizeof specifically is another matter though. – Lundin Nov 19 '18 at 10:20

3 Answers3

1

This is surprisingly difficult to get GCC to do. I found this answer: https://stackoverflow.com/a/35261673/502399 which suggests something like this:

void error() {
    int array[sizeof(struct X)];
    __builtin_printf("%d", &array);
}

which outputs something like

foo.c: In function ‘error’:
foo.c:8:21: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int (*)[8]’ [-Wformat=]                      

  __builtin_printf("%d", &array);
                    ~^   ~~~~~~

as long as you can pass -Wformat or -Wall or something.

To see if there's an easier way, I grepped the GCC source for that message and found that the argument type was printed with a special GCC-specific %qT format string, so I looked for other uses of that string. Specifically I was looking for its use in errors, rather than warnings, so that it would work regardless of warning flags. I found a use in binary_op_error(), from which I made this example:

int array[sizeof(struct X)];
int error = 1 / &array;

which produces

foo.c:7:15: error: invalid operands to binary / (have ‘int’ and ‘int (*)[8]’)
 int error = 1 / &array;
               ^ ~~~~~~

other possibilities include

int array[sizeof(struct X)];
int error = __sync_fetch_and_add(&array, 1);

and

int error = _Generic((int (*)[sizeof(struct X)])0, int: 0);

and

int foo(double bar);
int error = foo((int (*)[sizeof(struct X)])0);

etc.

Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
  • That's great! %qT in the source is a great find. Few diagnostic messages have two %qTs to display two integer values in the expression, but `error: invalid operands to binary / (have ‘int(*)[10]’ and ‘int (*)[8]’)` similar to yours above is one way of doing it, with two array objects. The next step is *is there a way to suppress the error if the numbers are equal, but trigger the error if they are not?* :) – Jetski S-type Nov 15 '18 at 23:39
1

For just displaying normal integer constants, a simple stringification macro will do:

#define STRINGIFY(x) #x
#define STR(x) STRINGIFY(x)
...
STR(123)

For the sizeof operator specifically, it gets trickier as that one is evaluated later than when macro expansion takes place in the pre-processor. Since C11 is available, you could perhaps use _Generic instead.

You can create a temporary compound literal with the size of the struct, then have _Generic compare a pointer to the created type with a pointer to another array of the expected size.

For example we can create the compound literal (char[sizeof(type)]){0} where the type char doesn't really matter, then take the address of that, &(char[sizeof(type)]){0}. Compare this type with an array pointer to an array of the expected size:

_Generic( &(char[sizeof(type)]){0}, 
          char(*)[expected] : true )

Full example:

#include <stdbool.h>

#define static_size_assert(type, expected) \
  _Generic( &(char[sizeof(type)]){0}, char(*)[expected] : true)


int main (void)
{
  typedef struct { char s[3]; int i; } foo_t;

  if(static_size_assert(foo_t, 7))
  {
    // ...
  }
  return 0;
}

In case of the expected struct padding, this will cause a standard-compliant compiler to produce an error message like (from gcc):

error: '_Generic' selector of type 'char (*)[8]' is not compatible with any association

whereas static_size_assert(foo_t, 8) will compile cleanly and return true. It works as long as the number passed is a compile-time integer constant and not a variable.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • A "compile-time computed numeric value" is not possible with a stringification macro, so that doesn't apply here. However, _Generic is a great way to do an equality evaluation for this. – Jetski S-type Nov 21 '18 at 00:50
  • This is a _Static_assert replacement, so it doesn't belong in an if condition. The general macro solution that evaluates any two (positive) integers rather than just sizeof types is: `#define STATIC_ASSERT_EQUAL(VALUE, EXPECTED) (void)_Generic(&(char[VALUE]){0}, char(*)[EXPECTED] : 0)` – Jetski S-type Nov 21 '18 at 00:53
  • The following is the general solution that kind of works with negative numbers, except that it aliases at 0x7FFFFFFF, so make sure that value isn't used. And negative numbers should be handled with the programmer being aware that the expected value is negative - if they have the same sign, it will print the positive of the value, and if the signs are opposite, the value will have to be negated to print the positive value. – Jetski S-type Nov 21 '18 at 01:24
  • `#define STATIC_ASSERT_EQUAL(VALUE, EXPECTED) (void)_Generic(&(char[(EXPECTED) > 0 ? (VALUE) : (EXPECTED) < 0 ? -(VALUE) : (VALUE) == 0 ? 0x7FFFFFFF : (VALUE)]){0}, char(*)[(EXPECTED) > 0 ? (EXPECTED) : (EXPECTED) < 0 ? -(EXPECTED) : 0x7FFFFFFF ] : 0)` – Jetski S-type Nov 21 '18 at 01:24
  • @JetskiS-type It was you who wrote "it should be able to evaluate the expression as true or false and print only when it fails evaluation." Indeed that doesn't make much sense. – Lundin Nov 21 '18 at 07:31
  • I'm not sure what you're not understanding - _Static_assert does absolutely nothing if the condition is true, and spits out an error message and halts if the condition is false. I just wanted it to do the same thing with outputting the values. – Jetski S-type Nov 21 '18 at 22:59
1

Building off the answers of Lundin and Tavian, the generic solution macro for positive and negative values is as follows:

#define STATIC_ASSERT_EQUAL(VALUE, EXPECTED)                                   \
  (void)_Generic(&(char[(EXPECTED) > 0 ? (VALUE) : (EXPECTED) < 0 ?            \
                        -(VALUE) : (VALUE) == 0 ? 0x7FFFFFFF : (VALUE)]){0},   \
                 char(*)[(EXPECTED) > 0 ? (EXPECTED) : (EXPECTED) < 0 ?        \
                         -(EXPECTED) : 0x7FFFFFFF] : 0)

The macro must be used inside a function. The idea is that EXPECTED is a value known by the programmer, and VALUE is the unknown calculated value. This was tested with GCC 6.1.

These pass without error:

STATIC_ASSERT_EQUAL(-1, -1);
STATIC_ASSERT_EQUAL(0, 0);
STATIC_ASSERT_EQUAL(1, 1);

However, this also unfortunately passes without error, because 0 aliases with 0x7FFFFFFF:

STATIC_ASSERT_EQUAL(0x7FFFFFFF, 0);

The error case appears as:

STATIC_ASSERT_EQUAL(2, 1);
error: ‘_Generic’ selector of type ‘char (*)[2]’ is not compatible with any association

If you have certain warnings enabled:

STATIC_ASSERT_EQUAL(0, 1);
error: ISO C forbids zero-size array [-Werror=pedantic]

Since EXPECTED is not 0, then the programmer can assume VALUE == 0.

STATIC_ASSERT_EQUAL(-2, 1);
error: size of unnamed array is negative

The value is not displayed in this case, but the programmer can see the VALUE is negative when it shouldn't be, so negate VALUE, and it will display.

STATIC_ASSERT_EQUAL(2, -1);
error: size of unnamed array is negative

Similarly for above, the programmer knows the VALUE should be negative, but isn't, so negating VALUE will make it display.

STATIC_ASSERT_EQUAL(-2, -1);
error: ‘_Generic’ selector of type ‘char (*)[2]’ is not compatible with any association

The programmer knows it should be negative, and here it displays the positive equivalent of VALUE.

STATIC_ASSERT_EQUAL(2, 0);
error: ‘_Generic’ selector of type ‘char (*)[2]’ is not compatible with any association

The above zero case works as expected.

Jetski S-type
  • 1,138
  • 2
  • 16
  • 32