[Cfp-interest 2906] Re: Why are the standard (and TS) #pragma's tied to header files?

Vincent Lefevre vincent at vinc17.net
Thu Oct 5 05:55:50 PDT 2023


On 2023-10-04 21:57:11 +0000, Rajan Bhakta wrote:
> To summarize the question we’ve been discussing in previous CFP
> meetings (and messages such as CFP2790), why do standard pragmas
> have associated header files?

This comes from C99, and I couldn't find anything in the rationale.

> Currently the C standard has a number of standard pragmas that all
> require a header file to be included as per the pragma synopsis. For
> example, in 7.3.4, the CX_LIMITED_RANGE pragma, the synopsis is as
> follows:
> 
> #include <complex.h>
> #pragma STDC CX_LIMITED_RANGE on-off-switch
> 
> Since the pragma is a language feature, why does a header need to be
> included at all?

Well, I don't think that header inclusion is required. This is in the
synopsis, but the standard does not say how this should be understood.
In particular, the standard does not say that pragmas are defined in
headers, contrary to what it says for types, macros and function
prototypes. This list is even explicit:

    7.1.2 Standard headers

  1 Each library function is declared, with a type that includes a
    prototype, in a header,159) whose contents are made available
    by the #include preprocessing directive. The header declares a
    set of related functions, plus any necessary types and additional
    macros needed to facilitate their use.

and in p4:

    [...] If used, a header shall be included outside of any external
    declaration or definition, and it shall first be included before
    the first reference to any of the functions or objects it declares,
    or to any of the types or macros it defines. [...]

(from ISO C99).

Nothing about pragmas. And when the standard mentions pragmas,
there's nothing about headers, except in the synopsis and in 6.5
with "The FP_CONTRACT pragma in <math.h>", but there is nothing
about the behavior related to header inclusion there.

Note also that the standard says that there are no macro replacements
in standard pragmas (the pragmas where the preprocessing token STDC
immediately follows "pragma"). So, header inclusion will never affect
the content of such pragmas.

The C23 draft seems similar.

-- 
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


More information about the Cfp-interest mailing list