-
Notifications
You must be signed in to change notification settings - Fork 7.8k
CODING_STANDARDS.md: add rules for #include
directives
#10338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for the PR! Given that the changes have been controversial so far, it seems to be a good idea to pursue the RFC process. Do you have already written to the internals mailing list in this regard? |
CODING_STANDARDS.md
Outdated
1. Use forward declarations to eliminate `#include` directives if | ||
possible. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of includes is too avoid code duplication. This statement says it's better to duplicate code.
May be you had a bit different thoughts, but in current state this looks improper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to write a single line of forward declaration instead of including a whole header (which in turn includes a dozen other headers, each of which includes another dozen of more headers).
This doesn't duplicate code any more than an #include
duplicates code.
It's the same amount of code (one line), but the forward declaration is better because it's trivial to process by the compiler, expresses exactly what it does, comes without namespace pollution, without I/O, without parsing more code, and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to write a single line of forward declaration instead of including a whole header
and then fix this forward declaration in all places instead of a single place...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What needs to be fixed, in your opinion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The C declaration code. It's not always once and forever. Functions may change their prototypes, unions may be turned into structures, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these updates can be automated, and apparently that is possible (never used that tool, though), it might not be that bad. :)
Actually, I was in favor of this include style, if at least system headers also adhered to it – but obviously, they don't (at least most of them).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean, and I should clarify that only structs and unions (and their typedefs) should be forward-declared, not functions - function prototypes (which are another kind of forward declaration) should not, because prototypes are too complex and indeed too much in motion.
If something gets switched between union and struct (which I think is very rare), then you'll get compiler errors which are trivial to fix (a simple sed
will fix it) - just like you'd get compiler errors for all callers if you change a function prototype. The latter (which is not affected by my proposal) is not trivial to fix, but still happens all the time and you manage it easily.
Means: there's no reason to believe that struct/union switches are too difficult to do just because there are forward declarations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if at least system headers also adhered to it – but obviously, they don't
Many projects like glibc, GCC (libstdc++) and Boost are aware of the problem, and each new version improves - which, of course, causes breakages in lots of buggy software that relies on header bloat in system headers and forgets to include necessary headers. For example, several of my projects received bug reports from people trying to compile my code with GCC13....
But anyway, even if system headers are too bloaty, that's just one more reason why doing it correctly in one's own software. Fewer includes means fewer indirect includes, which means more verification for correctness.
My proposal is really common sense. I'm really surprised about the resistance that suddenly emerges here.
I've just posted: https://marc.info/?l=php-internals&m=167387060703714&w=2 |
This is my proposal for keeping header dependencies small and correct. See php#10220 for the discussion.
ed286f9
to
a3a3f32
Compare
Draft PR to "revert the revert" (and for your review while we discuss this PR): #10345 |
@dstogov, in your review here, you objected to forward declarations. Yet I found lots of forward declarations authored by you, for example: php-src/Zend/Optimizer/zend_func_info.h Lines 44 to 45 in bff7a56
These two look very much like the code I submitted, which you asked to revert. You added it in commit c88ffa9 More forward declarations authored by you (excerpt): php-src/ext/opcache/jit/zend_jit.h Lines 90 to 92 in bff7a56
Lines 86 to 87 in bff7a56
Lines 68 to 69 in bff7a56
These findings make it difficult for me to understand your objections. |
@MaxKellermann some of if these declarations are done on purpose (to break dependencies), or declare recursive data structures (e.g. zend_class_entry and zend_function declarations contain pointers to them selves), others might stay in respect to the old code ( I didn't try to change source code without a practical reason). |
@dstogov, look at commit 8a6d73b which adds a forward declaration of If the majority of voters decides that forward declarations are bad, we need to change this forward declaration to P.S. this commit demonstrates how 2 of the forward declarations added by you can easily be avoided: CM4all@ef0c995 |
These were added by commit c88ffa9, but @dstogov himself opposed to the concept of forward declarations, see php#10338 (comment) (which I don't agree with - in my opinion, forward declarations are a useful tool to reduce header dependencies). Removing these unnecessary forward declarations requires some fiddling with include directives, because they were inconsistent and fragile previously. (They still are, but this PR does not attempt to change this; it only makes problems caused by the forward declaration removal go away.) Please merge this PR if (and only if) my RFC "include cleanup" (https://wiki.php.net/rfc/include_cleanup) gets rejected, to fix the PHP code base according to the RFC decision.
These were added by commit c88ffa9, but @dstogov himself opposed to the concept of forward declarations, see php#10338 (comment) (which I don't agree with - in my opinion, forward declarations are a useful tool to reduce header dependencies). Removing these unnecessary forward declarations requires some fiddling with include directives, because they were inconsistent and fragile previously. (They still are, but this PR does not attempt to change this; it only makes problems caused by the forward declaration removal go away.) Please merge this PR if (and only if) my RFC "include cleanup" (https://wiki.php.net/rfc/include_cleanup) gets rejected, to fix the PHP code base according to the RFC decision.
These were added by commit c88ffa9, but @dstogov himself opposed to the concept of forward declarations, see php#10338 (comment) (which I don't agree with - in my opinion, forward declarations are a useful tool to reduce header dependencies). Removing these unnecessary forward declarations requires some fiddling with include directives, because they were inconsistent and fragile previously. (They still are, but this PR does not attempt to change this; it only makes problems caused by the forward declaration removal go away.) Please merge this PR if (and only if) my RFC "include cleanup" (https://wiki.php.net/rfc/include_cleanup) gets rejected, to fix the PHP code base according to the RFC decision.
These were added by commit c88ffa9, but @dstogov himself opposed to the concept of forward declarations, see php#10338 (comment) (which I don't agree with - in my opinion, forward declarations are a useful tool to reduce header dependencies). Removing these unnecessary forward declarations requires some fiddling with include directives, because they were inconsistent and fragile previously. (They still are, but this PR does not attempt to change this; it only makes problems caused by the forward declaration removal go away.) Please merge this PR if (and only if) my RFC "include cleanup" (https://wiki.php.net/rfc/include_cleanup) gets rejected, to fix the PHP code base according to the RFC decision.
These were added by commit c88ffa9, but @dstogov himself opposed to the concept of forward declarations, see php#10338 (comment) (which I don't agree with - in my opinion, forward declarations are a useful tool to reduce header dependencies). Removing these unnecessary forward declarations requires some fiddling with include directives, because they were inconsistent and fragile previously. (They still are, but this PR does not attempt to change this; it only makes problems caused by the forward declaration removal go away.) Please merge this PR if (and only if) my RFC "include cleanup" (https://wiki.php.net/rfc/include_cleanup) gets rejected, to fix the PHP code base according to the RFC decision.
The RFC was accepted by a majority of 52%, but a supermajority was necessary, therefore the RFC was declined and this PR will not be merged. |
These were added by commit c88ffa9, but @dstogov himself opposed to the concept of forward declarations, see php#10338 (comment) (which I don't agree with - in my opinion, forward declarations are a useful tool to reduce header dependencies). Removing these unnecessary forward declarations requires some fiddling with include directives, because they were inconsistent and fragile previously. (They still are, but this PR does not attempt to change this; it only makes problems caused by the forward declaration removal go away.) Please merge this PR if (and only if) my RFC "include cleanup" (https://wiki.php.net/rfc/include_cleanup) gets rejected, to fix the PHP code base according to the RFC decision.
This is my proposal for keeping header dependencies small and correct.
See #10220 for the discussion.