From: | Andres Freund <andres(at)anarazel(dot)de> |
---|---|
To: | pgsql-hackers(at)postgresql(dot)org |
Cc: | Thomas Munro <thomas(dot)munro(at)gmail(dot)com> |
Subject: | Re: pg_attribute_noreturn(), MSVC, C11 |
Date: | 2024-12-13 19:54:11 |
Message-ID: | gn676kuvc2ycqy5qorzwmx532bqiaonyqa77bxwttvv77nkam6@mmo3jqttg3g4 |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Hi,
On 2024-12-13 14:10:13 -0500, Andres Freund wrote:
> I just encountered another
> warning C4715: 'XYZ: not all control paths return a value
>
> with msvc in CI in a case where it should be trivial for the compiler to
> recognize that the function return isn't reachable.
>
> Which made me check if these days msvc has something like gcc's
> __attribute__((noreturn)).
>
> And it turns out that yes! The _Noreturn attribute has been added to C11 and
> msvc supports C11:
> https://learn.microsoft.com/en-us/cpp/c-language/noreturn?view=msvc-170
>
> Besides the _Noreturn keyword the standard also added a stdnoreturn.h which
> provides the 'noreturn' macro.
>
>
> I first thought we could just implement pg_attribute_noreturn() using
> _Noreturn if available. However, our current pg_attribute_noreturn() is in the
> wrong place for that to work :(. _Noreturn is to be placed with the return
> type, whereas function attributes with the __attribute__(()) syntax are after
> the parameter list.
The point about __attribute__(()) being after the parameter list is wrong, I
confused myself there. But that doesn't change that much, the common current
placement doesn't work for _Noreturn.
> C11 has been out a while, so I'm somewhat inclined to adopt _Noreturn/noreturn
> in a conditional way. Older compilers would still work, just not understand
> noreturn.
>
> One wrinkle: _Noreturn/noreturn have been deprecated in C23, because that
> adopted C++11's attribute syntax (i.e. [[noreturn]]). But that's at least in
> the same place as _Noreturn/return.
>
> We can't remove [[noreturn]] with preprocessor magic, so it's not really
> viable to use that for, uhm, quite a while.
>
> If we were to use _Noreturn, I think it could just be something like:
>
> I think it should suffice to do something like
>
> #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
> #define pg_noreturn _Noreturn
> #else
> #define pg_noreturn
> #endif
>
> (or alternatively include stdreturn if __STDC_VERSION__ indicates support and
> define a bare 'noreturn' if not)
Another wrinkle: While __attribute__((noreturn)) works for function pointers
(or function pointer typedefs) _Noreturn doesn't. Gah. We only use it that
way in two places, but still :(
Greetings,
Andres Freund
From | Date | Subject | |
---|---|---|---|
Next Message | Vladlen Popolitov | 2024-12-13 20:36:23 | Re: COPY performance on Windows |
Previous Message | Andres Freund | 2024-12-13 19:10:13 | pg_attribute_noreturn(), MSVC, C11 |