help: ensure that common-cmds.h is only included by help.c
PATCH 1/1 help: ensure that common-cmds.h is only included by help.c
Add a #ifndef guard to ensure that common-cmds.h can only be included by help.c.
--- generate-cmdlist.sh | 4 ++++ help.c | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-)
generate-cmdlist.sh +4 -0
@@ -1,6 +1,10 @@ #!/bin/sh echo "/* Automatically generated by $0 */ +#ifndef GIT_HELP_INTERNAL +#error \"common-cmds.h can only be included by help.c\" +#endif + struct cmdname_help { char name[16]; char help[80];
help.c +2 -1
@@ -3,11 +3,12 @@#include "exec_cmd.h" #include "levenshtein.h" #include "help.h" -#include "common-cmds.h" #include "string-list.h" #include "column.h" #include "version.h" #include "refs.h" +#define GIT_HELP_INTERNAL +#include "common-cmds.h" void add_cmdname (struct cmdnames *cmds,const char *name,int len) {
Signature · 1 line
2.1.0.241.ga16d620
Quoted 2 lines · David Aguilar
Add a #ifndef guard to ensure that
common-cmds.hcan only be included byhelp.c.
This strikes me as a very peculiar, and sub-optimal, way of achieving the purpose. If these definitions are intended to be private to help.c, why not put them there and eliminate common-cmds.h entirely?
Quoted 8 lines · Perry Hutchison
Quoted 2 lines · David Aguilar
Add a #ifndef guard to ensure that
common-cmds.hcan only be included byhelp.c.This strikes me as a very peculiar, and sub-optimal, way of achieving the purpose. If these definitions are intended to be private to
help.c, why not put them there and eliminatecommon-cmds.hentirely?
Have you studied where common-cmds.h comes from? After you have done so, would you make the same suggestion?
Having said that, I also do not think this is such a good idea. Wouldn't the new "check" script added in this series a better place? For example, it may want to make sure that git-compat-util.h (or a couple of its equivalents) is the first file included in any mainline C source file, and such an inclusion is done unconditionally.
Which would mean that the checker would scan *.c files with grep or a Perl script. It would be trivial to enforce "nobody other than these small selected C files is allowed to include common-cmds.h" rule.
Regarding the other patch that butchers many *.h files, I am not still very enthused. Including cache.h at the beginning of branch.h, for example, would mean git-compat-util.h ends up included at the beginning of branch.h.
Quoted 19 lines · Junio C Hamano
Quoted 8 lines · Perry Hutchison
Quoted 2 lines · David Aguilar
Add a #ifndef guard to ensure that
common-cmds.hcan only be included byhelp.c.This strikes me as a very peculiar, and sub-optimal, way of achieving the purpose. If these definitions are intended to be private to
help.c, why not put them there and eliminatecommon-cmds.hentirely?Have you studied where
common-cmds.hcomes from? After you have done so, would you make the same suggestion?Having said that, I also do not think this is such a good idea. Wouldn't the new "check" script added in this series a better place? For example, it may want to make sure that
git-compat-util.h(or a couple of its equivalents) is the first file included in any mainline C source file, and such an inclusion is done unconditionally.
Quoted 3 lines
Which would mean that the checker would scan *.c files with grep or a Perl script. It would be trivial to enforce "nobody other than these small selected C files is allowed to include
common-cmds.h" rule.
Good idea. I implemented this check and the tweaks to make it pass are small and focused. I'll send these patches shortly.
Quoted 4 lines
Regarding the other patch that butchers many *.h files, I am not still very enthused. Including
cache.hat the beginning ofbranch.h, for example, would meangit-compat-util.hends up included at the beginning ofbranch.h.
I can look into Jonathan's forward-decl approach later too. That'll probably result in less of a butchering.
Signature · 1 line
David
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Perry Hutchison
Quoted 2 lines · David Aguilar
Add a #ifndef guard to ensure that
common-cmds.hcan only be included byhelp.c.... If these definitions are intended to be private to
help.c, why not put them there and eliminatecommon-cmds.hentirely?Have you studied where
common-cmds.hcomes from?
Not when I wrote that :)
Quoted 1 line
After you have done so, would you make the same suggestion?
Yes, as a matter of using C in a conventional and non-hackish manner. The normal and expected purpose of .h files is to share definitions among compilation units. If certain definitions are -- by design -- intended to be used in only a single compilation unit, they should not be put in a .h file; that only encourages other programmers who come along later to use those definitions in an incorrect way.
If it's too much trouble to have the auto-generation mechanism insert the definitions into help.c where they belong, at least name the auto-generated file something else, like commands-auto.res or command-list.txt. A #include file's name does not _have_ to end in .h, and avoiding the .h convention in a case such as this would make it blatantly obvious that something unconventional is being done.
Quoted 23 lines · Perry Hutchison
Quoted 9 lines · Junio C Hamano
Quoted 6 lines · Perry Hutchison
Quoted 2 lines · David Aguilar
Add a #ifndef guard to ensure that
common-cmds.hcan only be included byhelp.c.... If these definitions are intended to be private to
help.c, why not put them there and eliminatecommon-cmds.hentirely?Have you studied where
common-cmds.hcomes from?Not when I wrote that :)
Quoted 1 line
After you have done so, would you make the same suggestion?
Yes, as a matter of using C in a conventional and non-hackish manner. The normal and expected purpose of .h files is to share definitions among compilation units. If certain definitions are -- by design -- intended to be used in only a single compilation unit, they should not be put in a .h file; that only encourages other programmers who come along later to use those definitions in an incorrect way.
Quoted 7 lines
If it's too much trouble to have the auto-generation mechanism insert the definitions into
help.cwhere they belong, at least name the auto-generated file something else, like commands-auto.res or command-list.txt. A #include file's name does not _have_ to end in .h, and avoiding the .h convention in a case such as this would make it blatantly obvious that something unconventional is being done.
I would generally agree. Nonetheless, I was able to implement this check without touching any of these files so these should probably stay as-is.
Thanks,
Signature · 1 line
David