Home > OS >  How to use typeof in clang compiler without warning?
How to use typeof in clang compiler without warning?

Time:01-03

I want to use typeof in clang but not __typeof__. If I use typeof without declaring it I get warning the following warning

vector.c:14:5: warning: extension used [-Wlanguage-extension-token]
    vec_init(&a,5);
    ^
./vector.h:27:26: note: expanded from macro 'vec_init'
    ((vec_ptr)->data) = (typeof((vec_ptr)->data)) malloc((capacity) * (sizeof((*((vec_ptr)->data)))))      \
                         ^
1 warning generated.

then I thought defining it might solve it so I added the following code:

#ifndef typeof
#ifdef __clang__
#define typeof(x) __typeof__(x)
#endif
#endif

With the added pre-processor definition I get the following error:

In file included from vector.c:1:
./vector.h:5:9: warning: keyword is hidden by macro definition [-Wkeyword-macro]
#define typeof(x) __typeof__(x)
        ^
1 warning generated.

but I get no error if I use __typeof__ instead. How to solve this warning issue on clang?

CodePudding user response:

At the point of posting this answer, typeof is not a standard feature but an experimental language extension. As such, if you insist on using it, do so with the awareness that you aren't writing standard C - and get warned accordingly from strict compilers.

However, typeof used to be a GNU extension but it will be included in C23. Although compiler support for adding this to strict standard C is still shaky (neither clang 15 nor gcc 12.2 will allow it when compiling as -std=c2x).

Either way, typeof is not not a preprocessor #define but a keyword, so you can't use it with #ifndef.

For now, you can compile in clang or gcc with -std=gnu2x, until compiler support for C23 settles. But you still can't compile with -pedantic or you'll get a warning for using extensions.

CodePudding user response:

Anytime you want to deactivate a warning just feed a flag to the compiler that says -Wno-[warning-name]

In your case: -Wno-language-extension-token

Also, please, do NOT try to sweep this under the rug with clever code, either you acknowledge the warning and keep your code standardized, or you still acknowledge it but de-activate it.

That way if anyone uses your code with all warnings enabled they will still be warned that you are using extensions.

CodePudding user response:

At the time of writing, the typeof and __typeof__ operators are not in the language standard, but rather a compiler extension. (see this). typeof and __typeof__ are only standard in C23 and onwards.

So in the first example, the compiler is trying to warn you that you are using a compiler extension, thus losing portability.

In the second example, you are defining typeof, using a macro, and that hides typeof (which is a keyword), so the compilers give a warning as well.

You can discard it using the -Wno-language-extension-token flag (in neervana answer, note that this will turn off warnings for all language-extension-token warnings, not just this one), use the -std=gnu2x flag (in Lundin answer note that it will still not compile if you are using -pendanic,) or defining your own using _Generic.

Or, resolve your problem by don't cast the result of malloc in your vector code (as tstanisl said).

  • Related