threads / patch / 37554

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

Subject: [PATCH] help: ensure that common-cmds.h is only included by help.c

## tl;dr

6 messages between Sep 14, 2014 and Sep 14, 2014. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

David Aguilar· Sep 14, 2014, 01:11 UTC · lore

Add a #ifndef guard to ensure that common-cmds.h can only be included by help.c.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: David Aguilar <davvid@gmail.com>
---
 generate-cmdlist.sh | 4 ++++
 help.c              | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)
Show changes to 2 files +6 −1

generate-cmdlist.sh, help.c

diff --git a/generate-cmdlist.sh b/generate-cmdlist.sh
index 9a4c9b9..99cd140 100755
--- a/generate-cmdlist.sh
+++ b/generate-cmdlist.sh
@@ -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];
diff --git a/help.c b/help.c
index 7af65e2..abf1689 100644
--- a/help.c
+++ b/help.c
@@ -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)
 {
-- 
2.1.0.241.ga16d620
Perry Hutchison· Sep 14, 2014, 02:00 UTC · re: David Aguilar · lore

Re: [PATCH] help: ensure that common-cmds.h is only included by help.c

David Aguilar <davvid@gmail.com> wrote:
> 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?

Junio C Hamano· Sep 14, 2014, 05:23 UTC · re: Perry Hutchison · lore

Re: [PATCH] help: ensure that common-cmds.h is only included by help.c

On Sat, Sep 13, 2014 at 7:00 PM, Perry Hutchison <perryh@pluto.rain.com> wrote:
Show 8 quoted lines
> David Aguilar <davvid@gmail.com> wrote:
>> 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.

David Aguilar· Sep 14, 2014, 07:35 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: ensure that common-cmds.h is only included by help.c

On Sat, Sep 13, 2014 at 10:23:03PM -0700, Junio C Hamano wrote:
Show 22 quoted lines
> On Sat, Sep 13, 2014 at 7:00 PM, Perry Hutchison <perryh@pluto.rain.com> wrote:
> > David Aguilar <davvid@gmail.com> wrote:
> >> 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.

Good idea. I implemented this check and the tweaks to make it pass are small and focused. I'll send these patches shortly.

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

-- 
David
Perry Hutchison· Sep 14, 2014, 07:55 UTC · re: Junio C Hamano · lore

Re: [PATCH] help: ensure that common-cmds.h is only included by help.c

Junio C Hamano <gitster@pobox.com> wrote:
Show 9 quoted lines
> On Sat, Sep 13, 2014 at 7:00 PM, Perry Hutchison <perryh@pluto.rain.com> wrote:
> > David Aguilar <davvid@gmail.com> wrote:
> >> 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 :)
> 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.

David Aguilar· Sep 14, 2014, 08:44 UTC · re: Perry Hutchison · lore

Re: [PATCH] help: ensure that common-cmds.h is only included by help.c

On Sun, Sep 14, 2014 at 12:55:41AM -0700, Perry Hutchison wrote:
Show 30 quoted lines
> Junio C Hamano <gitster@pobox.com> wrote:
> > On Sat, Sep 13, 2014 at 7:00 PM, Perry Hutchison <perryh@pluto.rain.com> wrote:
> > > David Aguilar <davvid@gmail.com> wrote:
> > >> 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 :)
> 
> > 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.

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,
-- 
David

← back to recent threads