Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

MaxKellermann
Copy link
Contributor

This is my proposal for keeping header dependencies small and correct.

See #10220 for the discussion.

@cmb69 cmb69 mentioned this pull request Jan 16, 2023
@cmb69
Copy link
Member

cmb69 commented Jan 16, 2023

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?

@cmb69 cmb69 added the RFC label Jan 16, 2023
Comment on lines 271 to 272
1. Use forward declarations to eliminate `#include` directives if
possible.
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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...

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Member

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).

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@MaxKellermann
Copy link
Contributor Author

Do you have already written to the internals mailing list in this regard?

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.
@MaxKellermann
Copy link
Contributor Author

Draft PR to "revert the revert" (and for your review while we discuss this PR): #10345

@MaxKellermann
Copy link
Contributor Author

@dstogov, in your review here, you objected to forward declarations. Yet I found lots of forward declarations authored by you, for example:

typedef struct _zend_func_info zend_func_info;
typedef struct _zend_call_info zend_call_info;

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):

typedef struct _zend_jit_trace_rec zend_jit_trace_rec;
typedef struct _zend_jit_trace_stack_frame zend_jit_trace_stack_frame;
typedef struct _sym_node zend_sym_node;

typedef struct _zend_class_entry zend_class_entry;
typedef union _zend_function zend_function;

typedef struct _zend_vm_stack *zend_vm_stack;
typedef struct _zend_ini_entry zend_ini_entry;

These findings make it difficult for me to understand your objections.

@dstogov
Copy link
Member

dstogov commented Jan 20, 2023

@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).

@MaxKellermann
Copy link
Contributor Author

MaxKellermann commented Jan 20, 2023

@dstogov, look at commit 8a6d73b which adds a forward declaration of zend_ini_entry to zend_globals.h, because the commit adds _zend_executor_globals.error_reporting_ini_entry. You could have just added #include "zend_ini.h", but you decided to add a forward declaration. I agree with that, that was a good idea. But it contradicts with what I understood is your opinion about forward declarations. I don't get it.

If the majority of voters decides that forward declarations are bad, we need to change this forward declaration to #include, don't we?

P.S. this commit demonstrates how 2 of the forward declarations added by you can easily be avoided: CM4all@ef0c995
(Most of the "noise" in this commit is due to the chaotic nature of PHP's headers, it really fixes header bugs necessary to remove the forward declarations, something my work seeks to fix.)

MaxKellermann added a commit to CM4all/php-src that referenced this pull request Jan 20, 2023
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 3, 2023
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.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 7, 2023
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.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 7, 2023
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.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 9, 2023
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.
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 13, 2023
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.
@MaxKellermann
Copy link
Contributor Author

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.
https://wiki.php.net/rfc/include_cleanup

@MaxKellermann MaxKellermann deleted the include_coding_standards branch February 17, 2023 10:48
MaxKellermann added a commit to CM4all/php-src that referenced this pull request Feb 21, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants