Skip to contentSkip to conversation

help: ensure that common-cmds.h is only included by help.c

PATCH In review David Aguilar 3 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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
index 9a4c9b9..99cd140 100755
@@ -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
index 7af65e2..abf1689 100644
@@ -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
#1 lore raw
Quoted 2 lines · David Aguilar

Add a #ifndef guard to ensure that common-cmds.h can only be included by help.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?

#2 lore raw
Quoted 8 lines · Perry Hutchison
Quoted 2 lines · David Aguilar

Add a #ifndef guard to ensure that common-cmds.h can only be included by help.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?

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.

#3 lore raw
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.h can only be included by help.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?

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.

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.h at the beginning of branch.h, for example, would mean git-compat-util.h ends up included at the beginning of branch.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
#4 lore raw
In reply to Junio C Hamano
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.h can only be included by help.c.

... If these definitions are intended to be private to help.c, why not put them there and eliminate common-cmds.h entirely?

Have you studied where common-cmds.h comes 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.

#5 lore raw
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.h can only be included by help.c.

... If these definitions are intended to be private to help.c, why not put them there and eliminate common-cmds.h entirely?

Have you studied where common-cmds.h comes 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.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.

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