Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_Noreturn in a struct in c: error: expected specifier-qualifier-list before '_Noreturn'

Tags:

c

gcc

noreturn

c11

I am trying to compile a piece of code that contains _Noreturn:

#ifndef SOMEHEADER_H
#define SOMEHEADER_H

#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>

extern struct s {

    _Noreturn void (*somenoreturnfunc)(bool);
} svar;

#endif

Which gives me:

error: expected specifier-qualifier-list before '_Noreturn' on: _Noreturn void (*somenoreturnfunc)(bool);

So I tried the suggestion from here:

#ifndef SOMEHEADER_H
#define SOMEHEADER_H

#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>

#include "noreturn.h"

extern struct s {

    noreturn void (*somenoreturnfunc)(bool);
} svar;

#endif

noreturn.h:

#ifndef NO_RETURN_H
#define NO_RETURN_H
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
#define noreturn _Noreturn
#elif defined(__GNUC__)
#define noreturn __attribute__((noreturn))
#else
#define noreturn
#endif
#endif

But the error still happens:

In file included from ../include/someinclude.h:8:0,
                 from src/main.c:17:
../include/noreturn.h:4:18: error: expected specifier-qualifier-list before '_Noreturn'
 #define noreturn _Noreturn
                  ^
../include/someinclude.h:19:5: note: in expansion of macro 'noreturn'
     noreturn void (*somenoreturnfunc)(bool);
     ^

I am baffled because it is compiled with c11 so it should work:

make V=1
cc src/main.c
arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -std=c11 -Wall -pedantic -ffreestanding -static -I../libopencm3/include -I../include -DSTM32F1 -g -DDEBUG -DBUILD_STYLE=\"DEBUG\" -O0 -Iinclude -I../librt/include  -MMD -MT build/main.o -MF build/main.d -o build/main.o -c src/main.c
In file included...

GCC version is 5.4.1:

arm-none-eabi-gcc --version
arm-none-eabi-gcc (15:5.4.1+svn241155-1) 5.4.1 20160919
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

which should support all C11 features (_Noreturn is supported since 4.7).

What am I doing wrong and how could I resolve this error?

--
Edit: Maybe a self contained example can help:

main.c:

#include <stdalign.h>
#include <stdbool.h>
#include <stdint.h>

struct s {

    _Noreturn void (*somenoreturnfunc)(bool);
} svar;

int main()
{
        svar.somenoreturnfunc = 0;
        return 0;
}

Compiling this:

arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -std=c11 -Wall -pedantic -ffreestanding -static -DSTM32F1 -g -DDEBUG -DBUILD_STYLE=\"DEBUG\" -O0 -MMD -MT main.o -MF main.d -o main.o -c main.c
main.c:7:5: error: expected specifier-qualifier-list before '_Noreturn'
     _Noreturn void (*somenoreturnfunc)(bool);
     ^
main.c:5:8: warning: struct has no members [-Wpedantic]
 struct s {
        ^
main.c: In function 'main':
main.c:12:6: error: 'struct s' has no member named 'somenoreturnfunc'
  svar.somenoreturnfunc = 0;
      ^

Yet when compiling with the same command line and removing _Noreturn the compilation succeeds.

This also happens when compiling with gcc -std=c11 -o main main.c:

$ gcc -std=c11 -o main main.c
main.c:7:5: error: expected specifier-qualifier-list before ‘_Noreturn’
     _Noreturn void (*somenoreturnfunc)(bool);
     ^~~~~~~~~
main.c: In function ‘main’:
main.c:12:6: error: ‘struct s’ has no member named ‘somenoreturnfunc’
  svar.somenoreturnfunc = 0;
      ^
$ gcc --version
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
like image 584
Gizmo Avatar asked Mar 05 '23 16:03

Gizmo


1 Answers

In ISO C, _Noreturn cannot be used on the declaration of a function pointer. It can only be used on the declaration of a function. (Ref: C11 6.7.4/2).

You will have to abandon this idea. The function specifiers (inline and _Noreturn) are not part of a function's type.

There is another problem in your code: you define svar in the header, this will cause an ODR violation if the header is included in two or more translation units. It should only have a declaration in the header, and a definition in one unit.


To call through the function pointer and retain _Noreturn semantics , perhaps you could make a shim, e.g.:

inline _Noreturn void call_somefunc( struct s *ps, bool b )
{
    ps->somenoreturnfunc(b);
}

but there is no way to have the compiler enforce that when you assign to the function pointer, it is actually a _Noreturn function.

like image 197
M.M Avatar answered Mar 10 '23 09:03

M.M