Skip to contentSkip to conversation

test/hash: Fix warning in two-dimensional array init

PATCH In review Geert Uytterhoeven 2 participants 5 messages May 29, 2016 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 test/hash: Fix warning in two-dimensional array init

lib/test_hash.c: In function 'test_hash_init':
lib/test_hash.c:146:2: warning: missing braces around initializer [-Wmissing-braces]

Fixes 468a9428521e7d00 ("<linux/hash.h>: Add support for architecture-specific functions")
---
 lib/test_hash.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/test_hash.c +1 -1
index c9549c8b49090d7e..fd7a677100ebe935 100644
@@ -143,7 +143,7 @@ static int __init
 test_hash_init(void)
 {
 	char buf[SIZE+1];
-	u32 string_or = 0, hash_or[2][33] = { 0 };
+	u32 string_or = 0, hash_or[2][33] = { { 0, } };
 	unsigned tests = 0;
 	unsigned long long h64 = 0;
 	int i, j;
Signature · 1 line
1.9.1
#1 lore raw

PATCH 2/2 test/hash: Fix warning in preprocessor symbol evaluation

Some versions of gcc don't like tests for the value of an undefined preprocessor symbol, even in the #else branch of an #ifndef:

    lib/test_hash.c:224:7: warning: "HAVE_ARCH__HASH_32" is not defined [-Wundef]
     #elif HAVE_ARCH__HASH_32 != 1
	   ^
    lib/test_hash.c:229:7: warning: "HAVE_ARCH_HASH_32" is not defined [-Wundef]
     #elif HAVE_ARCH_HASH_32 != 1
	   ^
    lib/test_hash.c:234:7: warning: "HAVE_ARCH_HASH_64" is not defined [-Wundef]
     #elif HAVE_ARCH_HASH_64 != 1
	   ^

Seen with gcc 4.9, not seen with 4.1.2.

Change the logic to only check the value inside an #ifdef to fix this.

Fixes 468a9428521e7d00 ("<linux/hash.h>: Add support for architecture-specific functions")
---
 lib/test_hash.c | 24 +++++++++++++++---------
 1 file changed, 15 insertions(+), 9 deletions(-)
lib/test_hash.c +15 -9
index fd7a677100ebe935..a06ac379ad429c6b 100644
@@ -219,21 +219,27 @@ test_hash_init(void)
 	}
 
 	/* Issue notices about skipped tests. */
-#ifndef HAVE_ARCH__HASH_32
-	pr_info("__hash_32() has no arch implementation to test.");
-#elif HAVE_ARCH__HASH_32 != 1
+#ifdef HAVE_ARCH__HASH_32
+#if HAVE_ARCH__HASH_32 != 1
 	pr_info("__hash_32() is arch-specific; not compared to generic.");
 #endif
-#ifndef HAVE_ARCH_HASH_32
-	pr_info("hash_32() has no arch implementation to test.");
-#elif HAVE_ARCH_HASH_32 != 1
+#else
+	pr_info("__hash_32() has no arch implementation to test.");
+#endif
+#ifdef HAVE_ARCH_HASH_32
+#if HAVE_ARCH_HASH_32 != 1
 	pr_info("hash_32() is arch-specific; not compared to generic.");
 #endif
-#ifndef HAVE_ARCH_HASH_64
-	pr_info("hash_64() has no arch implementation to test.");
-#elif HAVE_ARCH_HASH_64 != 1
+#else
+	pr_info("hash_32() has no arch implementation to test.");
+#endif
+#ifdef HAVE_ARCH_HASH_64
+#if HAVE_ARCH_HASH_64 != 1
 	pr_info("hash_64() is arch-specific; not compared to generic.");
 #endif
+#else
+	pr_info("hash_64() has no arch implementation to test.");
+#endif
 
 	pr_notice("%u tests passed.", tests);
 
Signature · 1 line
1.9.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] test/hash: Fix warning in preprocessor symbol evaluation

Quoted 2 lines · Geert Uytterhoeven

Some versions of gcc don't like tests for the value of an undefined preprocessor symbol, even in the #else branch of an #ifndef:

Damn, I had hoped that would work universally; I tried to avoid the uglier #if-inside-#ifdef construction. GCC 6 is quite happy wth it.

But no objections.

If you want:

But here's an alternative. Geert, what do you think of this?

lib/test_hash.c +3 -3
index c9549c8b..8ea1d2ca 100644
@@ -221,17 +221,17 @@ test_hash_init(void)
 	/* Issue notices about skipped tests. */
 #ifndef HAVE_ARCH__HASH_32
 	pr_info("__hash_32() has no arch implementation to test.");
-#elif HAVE_ARCH__HASH_32 != 1
+#elif HAVE_ARCH__HASH_32 + 0 != 1
 	pr_info("__hash_32() is arch-specific; not compared to generic.");
 #endif
 #ifndef HAVE_ARCH_HASH_32
 	pr_info("hash_32() has no arch implementation to test.");
-#elif HAVE_ARCH_HASH_32 != 1
+#elif HAVE_ARCH_HASH_32  + 0 != 1
 	pr_info("hash_32() is arch-specific; not compared to generic.");
 #endif
 #ifndef HAVE_ARCH_HASH_64
 	pr_info("hash_64() has no arch implementation to test.");
-#elif HAVE_ARCH_HASH_64 != 1
+#elif HAVE_ARCH_HASH_64 + 0 != 1
 	pr_info("hash_64() is arch-specific; not compared to generic.");
 #endif
 
#3 lore raw

Re: [PATCH 2/2] test/hash: Fix warning in preprocessor symbol evaluation

Quoted 1 line

But here's an alternative. Geert, what do you think of this?

Never mind; that doesn't work. Plain gcc-4.9 passes, it, but it's the "-Wundef" flag that the kernel uses that triggers it.

So we're back to Geert's original suggestion.

==>$ cat foo.c
#ifndef FOO
This is a test.
#elif FOO == 1
This is another test
#elif FOO + 0 == 1
who knows
#endif

==>$ gcc-6 -W -Wall -Wundef -E foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 31 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 32 "<command-line>" 2
# 1 "foo.c"

This is a test.

==>$ gcc-4.9 -W -Wall -Wundef -E foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "foo.c"

foo.c:3:7: warning: "FOO" is not defined [-Wundef]
#elif FOO == 1

       ^
foo.c:5:7: warning: "FOO" is not defined [-Wundef]
 #elif FOO + 0 == 1
       ^

This is a test.