Sujet : Re: C23 thoughts and opinions
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 26. May 2024, 14:12:14
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <a4d1a54e-5380-4ef9-b61e-d455ee344c71@gmail.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12
User-Agent : Mozilla Thunderbird
Em 5/26/2024 8:09 AM, David Brown escreveu:
That's the idea. In theory, C pre-processors and C compilers are independent programs with a standardised format between them - in practice, they are often part of the same binary, and almost invariably come from the same developers. The "cpp" program may have to generate standard preprocessed output, and the "cc" program may have to accept standard preprocessed output, but there is nothing to stop the pair of programs supporting extended formats that are more efficient.
I will never stop saying, preprocessor and compiler should be integrated in a way programmers will a most not notice.
The way preprocessor sends information to the compiler is using special tokens . For instance when pragma is used.
But how to send information to latter phases like static analysis that uses AST? One solution is to use existing parts of AST and attaching some extra information carried by these especial tokens. But this is very confusing. What is the rule what is the grammar? What parts of the AST will have pragma tokens ?
The solution I adopt in cake is to make pragma part of the grammar and then participating on AST. So pragma survives the preprocessor arriving at compiler as an especial token, and compiler includes the pragma inside the AST as if it was part of the grammar. (I put pragma into the grammar similarly of static_assert, it can be extended to other usages if necessary)
I don´t know how GCC works, but I suspect it is doing something similar because I checked with this sample
void f(int
#warning message
i)
{}
void f2(int
#pragma GCC diagnostic push /*FAIL*/
i)
{}
In this sample we have this error
<source>:7:9: error: expected ';', ',' or ')' before '#pragma'
7 | #pragma GCC diagnostic push
https://godbolt.org/z/WEEK8desqThen this, in my view, is telling that GCC is using something similar of what I did.