简体   繁体   中英

How to define a NORETURN macro in Watcom C

Is it possible to do a #define NORETURN(x) macro that works with the Open Watcom C compiler (not C++)?

Based on the fine manual I have this file test.c :

#include <stdlib.h>

#ifdef __GNUC__
#define STATIC_NORETURN(sig) static void sig __attribute__((__noreturn__))
#define EXTERN_NORETURN(sig) extern void sig __attribute__((__noreturn__))
#endif

#ifdef __WATCOMC__
#pragma aux noreturn aborts;
#define STATIC_NORETURN(sig) static void __pragma("noreturn") sig
#define EXTERN_NORETURN(sig) extern void __pragma("noreturn") sig
#endif

STATIC_NORETURN(my_static_example(void));
EXTERN_NORETURN(my_extern_example(void));

static void my_static_example(void) {
    exit(0);
}

void my_extern_example(void) {
    my_static_example();
}

It compiles without any errors or warnings using GCC:

$ gcc -Wall -Wextra -pedantic -std=gnu99 -c test.c

But not using Open Watcom 1.9:

$ wine wcc386 -q -wx test.c
test.c(14): Error! E1009: Expecting ')' but found 'noreturn'
test.c(14): Error! E1026: Invalid declarator
test.c(14): Error! E1009: Expecting ',' but found 'noreturn'
test.c(14): Error! E1026: Invalid declarator
test.c(14): Error! E1009: Expecting ',' but found ')'
test.c(14): Error! E1024: Declared symbol 'my_static_example' is not in parameter list
test.c(15): Error! E1023: Storage class of parameter must be register or unspecified
test.c(15): Error! E1009: Expecting ')' but found 'noreturn'
test.c(15): Error! E1024: Declared symbol '__pragma' is not in parameter list
test.c(15): Error! E1009: Expecting ',' but found 'noreturn'
test.c(15): Error! E1026: Invalid declarator
test.c(15): Error! E1009: Expecting ',' but found ')'
test.c(15): Error! E1024: Declared symbol 'my_extern_example' is not in parameter list
test.c(17): Error! E1023: Storage class of parameter must be register or unspecified
test.c(17): Error! E1024: Declared symbol 'my_static_example' is not in parameter list
test.c(17): Error! E1076: Missing semicolon at end of declaration
test.c(22): Warning! W131: No prototype found for function 'my_static_example'
test.c(14): Warning! W202: Symbol '__pragma' has been defined, but not referenced

Upon closer reading, I think the manual says that __pragma works only in C++ code for some reason. But I'm not sure I understood that correctly. Is there any equivalent for C that doesn't require the use of #pragma at the site of each function definition? I'm trying to avoid ifdefs so it'd be nice to define a portable NORETURN macro in one central place.

Reading https://open-watcom.github.io/open-watcom-v2-wikidocs/cguide.html it's:

__declspec( noreturn )
    indicates to the C/C++ compiler that function does not return.
    Example:
    ...

Be compatible with stdnoreturn.h , don't come up with your own style. Do:

// possibly in some noreturn.h
// maybe even name it stdnoreturn.h
#if __STDC_VERSION__ >= 201110L
#include <stdnoreturn.h>
#elif defined(__WATCOMC__)
#define noreturn __declspec(noreturn)
#else
#error
#endif

#include <stdlib.h>

static noreturn void my_static_example(void) {
    exit(0);
}

noreturn void my_extern_example(void) {
    my_static_example();
}

Compiles fine on wcc386 -q -wx test.c with Version 2.0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM