Thank you for reviewing my patches.
I have replied to your comments inline below.
I ran into preprocessor assertions when I was trying to build an older version of Grub which is what prompted my implementation.
I’d like to get feedback and consensus on whether we should add “GCC Preprocessor Assertions” to clang. This gcc extension has been deprecated since gcc-3, here’s a quote from gcc 3.1 docs, (bold added by me):
Assertions are a deprecated alternative to macros in writing conditionals to test what sort of computer or system the compiled program will run on. Assertions are usually predefined, but you can define them with preprocessing directives or command-line options.
Assertions were intended to provide a more systematic way to describe the compiler’s target system. However, in practice they are just as unpredictable as the system-specific predefined macros. In addition, they are not part of any standard, and only a few compilers support them. Therefore, the use of assertions is less portable than the use of system-specific predefined macros. We recommend you do not use them at all.
Do note that, after checking on http://ideone.com, gcc still supports them on gcc-4.3.4.
Andrew did great work on implementing the feature; apart from the preprocessor changes, he added support for them in the preprocessing record and serialization/deserialization support in the ASTReader/ASTWriter.
This is a significant amount of code that we have to maintain and make sure it works well with other parts of the codebase, and I’m worried about the maintenance burden that this deprecated feature will impose, e.g.
-it reserves 1 bit out of the IdentifierInfo object. There’s only 1 bit left currently so afterwards there will be non left to easily use for other purposes, without doubling the IdentifierInfo object’s size.
If anyone has suggestions on how to avoid this, I am happy to consider reworking my patch to incorporate such an improvement.
-it increases the size of the IdentifierInfo data that are stored in the serialized AST.
I think this is unavoidable if the feature is going to be implemented, but again suggestions on how to improve things are most welcome.
Although the changes are extensive, there may be more that are needed:
-gcc docs say that you can specify assertions by command-line options; I did not see this implemented in Andrew’s patches
I am aware the command-line option (-A) to assert a key-value pair is not implemented in the patches I have submitted. I didn’t write this support yet because clang uses -A for other things and I wasn’t sure if that needed to be changed or if we were going to use a different flag - the actual patch will be relatively small. If the feature is accepted and guidance is provided as to what flag we should use I am happy to write and contribute this patch
-the feature was not integrated into our PCH validation, to be more specific, validating and giving errors when assertions from PCH clash with predefined assertions or assertions from the command-line
I do not believe there is anything that needs to be validated in terms of PCH vs command-line vs source preprocessor assertions. You can assert multiple values for a given key quite correctly (unlike macros) and assertions are designed to be added and removed using all of these features and it is not wrong to do so. Further, asserting the same key-value pair twice is not an error and so I don’t think there needs to be validation of clashes since, by design, these clashes are permitted.
-It is not clear to me how this feature should interact with modules.
I am not sure how these would interact since the reference implementation, GCC, does not support modules in the versions I have tested. Doug, do you have any thoughts on this since you have worked on modules? Since the behavior is undefined we can probably do whatever is easiest and makes the most sense.
So, to recap, the question is this, should we implement and maintain a gcc extension that was deprecated since gcc 3 ?
I think clang should implement and maintain this feature because
- It’s widely supported by existing compilers including GCC 4.6.X (), Intel C/C++ (), and ARM’s toolchain () 2) Preprocessor assertions are an optional extension defined in System V Release 4 (SVR4) see the System V Interface manual Vol 3 page 270 () and so should be implemented to support SVR4 systems which have chosen to implement this feature. 3) It was clearly identified as a missing feature by FIXMEs in Chris’ original commit of PPDirectives.cpp and currently the compiler produces only cryptic errors when this feature is encountered. 4) It is not possible to work around this feature without having to modify the source and doing so with an automated sed or awk script is risky and difficult to integrate into some build processes. I did consider trying to emulate assertions using macros, but I found this wasn’t possible using only the preprocessor. Asserts and macros exist in different namespaces and so you can define and assert the same symbol. In many cases this is not a problem because all we want to do is test if a macro is defined, but if the value of the macro is important then a collision in the names would cause a problem. The assertion tests themselves would have to be found and rewritten using a sed or awk script which is a fragile hack rather than a proper fix and having to run this script before a compile would complicate the build process etc. This kind of workaround also assumes that the source can be modified prior to compilation. 5) Clang has publicly stated that it will aim for GCC compatibility where possible and these patches make compatibility with this feature possible.