We can use _Generic from C11 and the __typeof__ extension from GCC to detect if something is a pointer or an array. First, we can define the macro

    #define IS_COMPATIBLE(x, y) _Generic((x), __typeof__(y) : true, default : false)

This will switch on the type of x and check if it is compatible with y. Next, detecting if something is an array can be done using

    #define IS_ARRAY(x) !IS_COMPATIBLE(&*x, x)

For pointers, & and * are inverses, so &*x and x will have the same type. For an array x of type T[N] however, &*x will have the type *T, which is incompatible with T[N]. For example,

    int x[2];
int *y;
static_assert(IS_ARRAY(x)); // will succeed!
static_assert(IS_ARRAY(y)); // will fail!
    void test(int x[2]) {
// will fail, since x is not an array even though it looks like one
static_assert(IS_ARRAY(x));
}

I think having a macro (or macros) specifically for copying arrays would be useful - it is certainly helpful in the patches Nico has based on this commit.

More _Generic macro magic can be read about here, which is the web page that inspired this post.
http://www.robertgamble.net/2012/01/c11-generic-selections.html

View Change

To view, visit change 32740. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Change-Id: I2b23066b3bcc29ff679e620f723ce1da5057aa03
Gerrit-Change-Number: 32740
Gerrit-PatchSet: 1
Gerrit-Owner: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: Aaron Durbin <adurbin@chromium.org>
Gerrit-Reviewer: Arthur Heymans <arthur@aheymans.xyz>
Gerrit-Reviewer: Furquan Shaikh <furquan@google.com>
Gerrit-Reviewer: Julius Werner <jwerner@chromium.org>
Gerrit-Reviewer: Kyösti Mälkki <kyosti.malkki@gmail.com>
Gerrit-Reviewer: Nico Huber <nico.h@gmx.de>
Gerrit-Reviewer: Patrick Georgi <pgeorgi@google.com>
Gerrit-Reviewer: Patrick Rudolph <siro@das-labor.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>
Gerrit-CC: Jacob Garber <jgarber1@ualberta.ca>
Gerrit-Comment-Date: Thu, 30 May 2019 18:44:21 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment