In the following C code the code analysys (the one from C/C++ vscode extension) highlights an error on DEFINE_INDEXING_FUNCTION
macro, but when I compile the code with GCC (gcc -Wall -Wextra filename.c; .\a
), it works just fine.
#include <stdlib.h>#define MOD(a, b) ((a) % (b) + (b)) % (b)#define DEFINE_INDEXING_FUNCTION(type, array, length)\ type *__array_indexing_##array = array;\ type array##_get(long long index) {\ return __array_indexing_##array[MOD(index, length-1)];\ }int array1[] = {1, 2, 3, 4, 5};DEFINE_INDEXING_FUNCTION(int, array1, sizeof(array1)/sizeof(int))#include <stdio.h>void test() { int *array2 = malloc(10); // expected a ";" DEFINE_INDEXING_FUNCTION(int, array2, 10) // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ printf("%d\n", array2_get(-2)); printf("%d\n", array2_get(2));}int main() { printf("%d\n", array1_get(-2)); printf("%d\n", array1_get(2)); test();}
My guess is that it doesn't know nested functions, which are not a part of c standard and are exclusive to gcc.
So, what do you think?