Really work around "uninitialized value" warning

Ever since 457f08a (git-rev-list: add --bisect-vars option.,
2007-03-21), Git's source code uses the following trick to fool GCC into
*not* warning about uninitialized values:

	int value = value;

We use this trick to silence the "warning: ‘x’ is used uninitialized in
this function [-Wuninitialized]" when the variables are not really used
uninitialized (but it is hard for the compiler to determine that).

This trick works well for GCC, and even Clang seems to appease that
workaround.

Not so Visual C. It does realize that this is just a trick to fool it,
and it simply refuses to be fooled.

The only way to silence the warning for Visual C would be to write
something like this:

	#pragma warning(suppress: 4700)
	int value;

Obviously this is not portable, and neither is that trick that fools
GCC. So let's just introduce a new macro that continues to fool GCC, but
simply initializes the values everywhere else.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin
2016-11-08 16:39:44 +01:00
parent 6e7dc7ca85
commit 8b1fc75921

View File

@@ -51,6 +51,23 @@
#endif
#endif
/*
* Under certain circumstances Git's source code is cleverer than the C
* compiler when the latter warns about some "uninitialized value", e.g. when
* a value is both initialized and used under the same condition.
*
* GCC can be fooled to not spit out this warning by using the construct:
* "int value = value;". Other C compilers are not that easily fooled and would
* require a #pragma (which is not portable, and would litter the source code).
*
* To keep things simple, we only fool GCC, and initialize such values instead
* when compiling with other C compilers.
*/
#ifdef __GNUC__
#define FAKE_INIT(a, b, c) a b = b
#else
#define FAKE_INIT(a, b, c) a b = c
#endif
/*
* BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.