git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 1/2] config.c: avoid duplicated global static variables
@ 2016-08-09 14:16 Johannes Sixt
  2016-08-09 14:17 ` [PATCH 2/2] commit-slab.h: " Johannes Sixt
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Sixt @ 2016-08-09 14:16 UTC (permalink / raw)
  To: Git Mailing List

Repeating the definition of a static variable seems to be valid in C.
Nevertheless, it is bad style because it can cause confusion, definitely
when it becomes necessary to change the type.

d64ec16 (git config: reorganize to use parseopt, 2009-02-21) added two
static variables near the top of the file config.c without removing the
definitions of the two variables that occurs later in the file.

The two variables were needed earlier in the file in the newly
introduced parseopt structure. These references were removed later in
d0e08d6 (config: fix parsing of "git config --get-color some.key -1",
2014-11-20).

Remove the redundant, younger, definitions near the top of the file and
keep the original definitions that occur later.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 Why the heck are duplicated static variables not an error in C?
 Since they aren't (as it seems), this and the next patch are just
 a matter of taste.

 builtin/config.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/builtin/config.c b/builtin/config.c
index a991a53..6cbf733 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -25,7 +25,6 @@ static char term = '\n';
 static int use_global_config, use_system_config, use_local_config;
 static struct git_config_source given_config_source;
 static int actions, types;
-static const char *get_color_slot, *get_colorbool_slot;
 static int end_null;
 static int respect_includes = -1;
 static int show_origin;
-- 
2.9.2.935.gccae72a.dirty


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] commit-slab.h: avoid duplicated global static variables
  2016-08-09 14:16 [PATCH 1/2] config.c: avoid duplicated global static variables Johannes Sixt
@ 2016-08-09 14:17 ` Johannes Sixt
  2016-08-09 14:25   ` Johannes Sixt
  2016-08-09 17:17   ` Junio C Hamano
  0 siblings, 2 replies; 4+ messages in thread
From: Johannes Sixt @ 2016-08-09 14:17 UTC (permalink / raw)
  To: Git Mailing List

The gigantic define_commit_slab() macro repeats the definition of a
static variable that occurs earlier in the macro text. The purpose of
the repeated definition at the end of the macro is that it takes the
semicolon that occurs where the macro is used.

We cannot just remove the first definition of the variable because it
is referenced elsewhere in the macro text, and defining the macro later
would produce undefined identifier errors. We cannot have a "forward"
declaration, either. (This works only with "extern" global variables.)

The solution is to use a declaration of a struct that is already defined
earlier. This language construct can serve the same purpose as the
duplicated static variable definition, but without the confusion.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 commit-slab.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/commit-slab.h b/commit-slab.h
index f84b449..006a50b 100644
--- a/commit-slab.h
+++ b/commit-slab.h
@@ -126,16 +126,16 @@ static MAYBE_UNUSED elemtype *slabname## _peek(struct slabname *s,	\
 	return slabname##_at_peek(s, c, 0);				\
 }									\
 									\
-static int stat_ ##slabname## realloc
+struct slabname
 
 /*
- * Note that this seemingly redundant second declaration is required
+ * Note that this redundant forward declaration is required
  * to allow a terminating semicolon, which makes instantiations look
  * like function declarations.  I.e., the expansion of
  *
  *    define_commit_slab(indegree, int);
  *
- * ends in 'static int stat_indegreerealloc;'.  This would otherwise
+ * ends in 'struct indegree;'.  This would otherwise
  * be a syntax error according (at least) to ISO C.  It's hard to
  * catch because GCC silently parses it by default.
  */
-- 
2.9.2.935.gccae72a.dirty


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] commit-slab.h: avoid duplicated global static variables
  2016-08-09 14:17 ` [PATCH 2/2] commit-slab.h: " Johannes Sixt
@ 2016-08-09 14:25   ` Johannes Sixt
  2016-08-09 17:17   ` Junio C Hamano
  1 sibling, 0 replies; 4+ messages in thread
From: Johannes Sixt @ 2016-08-09 14:25 UTC (permalink / raw)
  To: Git Mailing List

BTW, these are all instances of duplicated global static variables that 
can be found in a standard Linux build.

How I found them? I waded through the error messages produced by compiling 
the code base as C++ code for the fun of it (basically CFLAGS='-x c++ 
-fpermissive').

-- Hannes


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] commit-slab.h: avoid duplicated global static variables
  2016-08-09 14:17 ` [PATCH 2/2] commit-slab.h: " Johannes Sixt
  2016-08-09 14:25   ` Johannes Sixt
@ 2016-08-09 17:17   ` Junio C Hamano
  1 sibling, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2016-08-09 17:17 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Git Mailing List

Johannes Sixt <j6t@kdbg.org> writes:

>   * like function declarations.  I.e., the expansion of
>   *
>   *    define_commit_slab(indegree, int);
>   *
> - * ends in 'static int stat_indegreerealloc;'.  This would otherwise
> + * ends in 'struct indegree;'.  This would otherwise
>   * be a syntax error according (at least) to ISO C.  It's hard to
>   * catch because GCC silently parses it by default.
>   */

I briefly wondered what "otherwise" and "it" in "silently parses it"
refer to; it is having a hanging semicolon after the definition of
the *_peek() function, i.e.

	...
        static int *indegree_peek(...)
        {
		return indegree_at_peek(s, c, 0);
	}
	;

so all is fine.

Needless to say, using "struct slabname;" is indeed a much nicer
solution you found to the original issue.  Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-08-09 17:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-09 14:16 [PATCH 1/2] config.c: avoid duplicated global static variables Johannes Sixt
2016-08-09 14:17 ` [PATCH 2/2] commit-slab.h: " Johannes Sixt
2016-08-09 14:25   ` Johannes Sixt
2016-08-09 17:17   ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).