[gcab] Add GCC warnings flags, fix warnings



commit 5da1c9079c438a2dc3a72778432fc85d6e093f2c
Author: Marc-Andrà Lureau <marcandre lureau gmail com>
Date:   Fri Feb 8 16:28:33 2013 +0100

    Add GCC warnings flags, fix warnings

 Makefile.am            |    9 ++-
 configure.ac           |   19 ++++-
 gcab.c                 |   22 +++--
 libgcab/cabinet.c      |    5 +-
 libgcab/gcab-cabinet.c |   18 ++--
 libgcab/gcab-folder.c  |    2 +-
 m4/manywarnings.m4     |  224 ++++++++++++++++++++++++++++++++++++++++++++++++
 m4/warnings.m4         |   61 +++++++++++++
 8 files changed, 335 insertions(+), 25 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 8481286..18d4339 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,18 +3,21 @@ BUILT_SOURCES =
 CLEANFILES =
 DISTCLEANFILES =
 ACLOCAL_AMFLAGS = -I m4
-AM_MAKEFLAGS = --no-print-directory
+
+makeflags_ = $(am__v_lt_$(AM_DEFAULT_VERBOSITY))
+makeflags_0 = --no-print-directory -s
+AM_MAKEFLAGS = $(makeflags_$(V))
 
 SUBDIRS = docs po libgcab
 
+AM_CFLAGS = $(WARN_CFLAGS)
 AM_CPPFLAGS =					\
         -I$(top_srcdir)				\
         -DG_LOG_DOMAIN=\"gcab\"			\
 	-DLOCALEDIR="\"$(datadir)/locale\""	\
+	$(GLIB_CFLAGS)				\
 	$(NULL)
 
-AM_CFLAGS = $(GLIB_CFLAGS)
-
 libgcab_includedir = $(includedir)/libgcab-1.0
 libgcab_priv_includedir = $(libgcab_includedir)/libgcab
 libgcab_include_HEADERS = libgcab.h
diff --git a/configure.ac b/configure.ac
index a2e0591..748e804 100644
--- a/configure.ac
+++ b/configure.ac
@@ -29,9 +29,26 @@ AM_PROG_VALAC([0.14])
 AC_PATH_PROG(VAPIGEN, vapigen, no)
 AC_SUBST(VAPIGEN)
 AM_CONDITIONAL([VAPI], [test "x$VAPIGEN" != xno])
-
 AC_CHECK_HEADERS([zlib.h], [], [AC_MSG_ERROR([zlib required])])
 
+gl_MANYWARN_ALL_GCC([warnings])
+nw=
+nw="$nw -Wsystem-headers"       # Don't let system headers trigger warnings
+nw="$nw -Wundef"                # All compiler preprocessors support #if UNDEF
+nw="$nw -Wtraditional"          # All compilers nowadays support ANSI C
+nw="$nw -Wconversion"           # These warnings usually don't point to mistakes.
+nw="$nw -Wsign-conversion"      # Likewise.
+nw="$nw -Wunused-parameter"     # Likewise.
+nw="$nw -Wunused-local-typedefs" # Likewise.
+nw="$nw -Wunsafe-loop-optimizations" # Likewise.
+# Enable all GCC warnings not in this list.
+gl_MANYWARN_COMPLEMENT([warnings], [$warnings], [$nw])
+for w in $warnings; do
+  gl_WARN_ADD([$w])
+done
+gl_WARN_ADD([-Wno-sign-compare])
+gl_WARN_ADD([-Wno-unused-parameter])
+
 AM_MISSING_PROG([AUTOM4TE], [autom4te])
 AC_CONFIG_TESTDIR([.])
 AC_CONFIG_FILES([atlocal])
diff --git a/gcab.c b/gcab.c
index cb927c2..7d9ec32 100644
--- a/gcab.c
+++ b/gcab.c
@@ -47,7 +47,7 @@ file_callback (GCabFile *cabfile, gpointer data)
 static const gchar *
 remove_leading_path (gchar *name)
 {
-    int i;
+    unsigned i;
 
     i = 0;
     if (name[0] == G_DIR_SEPARATOR)
@@ -128,6 +128,10 @@ individual files from the archive.\
 
     GCancellable *cancellable = g_cancellable_new ();
     GCabCabinet *cabinet = gcab_cabinet_new ();
+    GCabFolder *folder;
+    GFile *outputfile;
+    GOutputStream *output;
+    GFile *cwd;
 
     if (list || extract) {
         GFile *file = g_file_new_for_commandline_arg (args[0]);
@@ -141,10 +145,10 @@ individual files from the archive.\
         if (list) {
             GPtrArray *folders = gcab_cabinet_get_folders (cabinet);
             for (i = 0; i < folders->len; i++) {
-                GSList *l, *list = gcab_folder_get_files (g_ptr_array_index (folders, i));
-                for (l = list; l != NULL; l = l->next)
+                GSList *l, *files = gcab_folder_get_files (g_ptr_array_index (folders, i));
+                for (l = files; l != NULL; l = l->next)
                     g_print ("%s\n", gcab_file_get_name (GCAB_FILE (l->data)));
-                g_slist_free (list);
+                g_slist_free (files);
             }
         } else if (extract) {
             g_object_unref (file);
@@ -164,7 +168,7 @@ individual files from the archive.\
     if (args[1] == NULL)
         gcab_error (_("please specify input files."));
 
-    GCabFolder *folder = gcab_folder_new (compress ? GCAB_COMPRESSION_MSZIP : 0);
+    folder = gcab_folder_new (compress ? GCAB_COMPRESSION_MSZIP : 0);
 
     for (i = 1; args[i]; i++) {
         GFile *file = g_file_new_for_commandline_arg (args[i]);
@@ -185,13 +189,13 @@ individual files from the archive.\
     if (gcab_folder_get_nfiles (folder) == 0)
         gcab_error (_("no files to be archived."));
 
-    GFile *outputfile = g_file_new_for_commandline_arg (args[0]);
-    GOutputStream *output = G_OUTPUT_STREAM (g_file_replace (outputfile, NULL, FALSE,
-                                                             0, NULL, &error));
+    outputfile = g_file_new_for_commandline_arg (args[0]);
+    output = G_OUTPUT_STREAM (g_file_replace (outputfile, NULL, FALSE,
+                                              0, NULL, &error));
     if (error)
         gcab_error (_("can't create cab file %s: %s"), args[0], error->message);
 
-    GFile *cwd = g_file_new_for_commandline_arg (".");
+    cwd = g_file_new_for_commandline_arg (".");
     if (!gcab_cabinet_add_folder (cabinet, folder, &error))
         gcab_error (_("can't add folder to cabinet: %s"), args[0], error->message);
 
diff --git a/libgcab/cabinet.c b/libgcab/cabinet.c
index 6e8523c..518026c 100644
--- a/libgcab/cabinet.c
+++ b/libgcab/cabinet.c
@@ -433,7 +433,8 @@ cdata_read (cdata_t *cd, u1 res_data, gint comptype,
     gboolean success = FALSE;
     int ret, zret = Z_OK;
     gint compression = comptype & GCAB_COMPRESSION_MASK;
-    gchar *buf = compression == GCAB_COMPRESSION_NONE ? cd->out : cd->in;
+    guint8 *buf = compression == GCAB_COMPRESSION_NONE ? cd->out : cd->in;
+    CHECKSUM datacsum;
 
     if (compression > GCAB_COMPRESSION_MSZIP &&
         compression != GCAB_COMPRESSION_LZX) {
@@ -449,7 +450,7 @@ cdata_read (cdata_t *cd, u1 res_data, gint comptype,
     RN (cd->reserved, res_data);
     RN (buf, cd->ncbytes);
 
-    CHECKSUM datacsum = compute_checksum(buf, cd->ncbytes, 0);
+    datacsum = compute_checksum(buf, cd->ncbytes, 0);
     g_return_val_if_fail (cd->checksum == compute_checksum ((guint8*)&cd->ncbytes, 4, datacsum), FALSE);
 
     if (g_getenv ("GCAB_DEBUG")) {
diff --git a/libgcab/gcab-cabinet.c b/libgcab/gcab-cabinet.c
index d06d648..e5f10ce 100644
--- a/libgcab/gcab-cabinet.c
+++ b/libgcab/gcab-cabinet.c
@@ -1,3 +1,4 @@
+#include <glib/gi18n-lib.h>
 #include "gcab-priv.h"
 
 /**
@@ -196,14 +197,19 @@ gcab_cabinet_write (GCabCabinet *self,
     GInputStream *in = NULL;
     GDataOutputStream *dstream = NULL;
     gboolean success = FALSE;
+    gssize len, offset = 0;
+    cdata_t block = { 0, };
+    guint8 data[DATABLOCKSIZE];
+    gsize written;
+    size_t sumstr = 0;
+    GSList *l, *files;
+    cfile_t *prevf = NULL;
     int i;
 
     dstream = g_data_output_stream_new (out);
     g_data_output_stream_set_byte_order (dstream, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
 
-    size_t sumstr = 0;
-    GSList *l, *files = gcab_folder_get_files (cabfolder);
-
+    files = gcab_folder_get_files (cabfolder);
     for (l = files; l != NULL; l = l->next)
         sumstr += strlen (GCAB_FILE (l->data)->name) + 1;
 
@@ -216,10 +222,6 @@ gcab_cabinet_write (GCabCabinet *self,
         if (!g_data_output_stream_put_byte (dstream, 0, cancellable, error))
             goto end;
 
-    gssize len, offset = 0;
-    cdata_t block = { 0, };
-    guint8 data[DATABLOCKSIZE];
-    gsize written;
     for (l = files; l != NULL; l = l->next) {
         file = GCAB_FILE (l->data);
         if (file_callback)
@@ -264,7 +266,6 @@ gcab_cabinet_write (GCabCabinet *self,
     if (!cfolder_write (&folder, dstream, cancellable, error))
         goto end;
 
-    cfile_t *prevf = NULL;
     for (l = files; l != NULL; l = l->next) {
         file = GCAB_FILE (l->data);
         file->cfile.uoffset = prevf ? prevf->uoffset + prevf->usize : 0;
@@ -442,7 +443,6 @@ gcab_cabinet_extract (GCabCabinet *self,
         }
     }
 
-end:
     return success;
 }
 
diff --git a/libgcab/gcab-folder.c b/libgcab/gcab-folder.c
index ac51afa..a140e2c 100644
--- a/libgcab/gcab-folder.c
+++ b/libgcab/gcab-folder.c
@@ -338,6 +338,7 @@ gcab_folder_extract (GCabFolder *self,
     GFileOutputStream *out = NULL;
     GSList *f, *files = NULL;
     cdata_t cdata = { 0, };
+    u4 nubytes = 0;
 
     data = g_data_input_stream_new (self->stream);
     g_data_input_stream_set_byte_order (data, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
@@ -348,7 +349,6 @@ gcab_folder_extract (GCabFolder *self,
 
     files = g_slist_sort (g_slist_copy (self->files), (GCompareFunc)sort_by_offset);
 
-    u4 nubytes = 0;
     for (f = files; f != NULL; f = f->next) {
         GCabFile *file = f->data;
 
diff --git a/m4/manywarnings.m4 b/m4/manywarnings.m4
new file mode 100644
index 0000000..45a30af
--- /dev/null
+++ b/m4/manywarnings.m4
@@ -0,0 +1,224 @@
+# manywarnings.m4 serial 5
+dnl Copyright (C) 2008-2013 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl From Simon Josefsson
+
+# gl_MANYWARN_COMPLEMENT(OUTVAR, LISTVAR, REMOVEVAR)
+# --------------------------------------------------
+# Copy LISTVAR to OUTVAR except for the entries in REMOVEVAR.
+# Elements separated by whitespace.  In set logic terms, the function
+# does OUTVAR = LISTVAR \ REMOVEVAR.
+AC_DEFUN([gl_MANYWARN_COMPLEMENT],
+[
+  gl_warn_set=
+  set x $2; shift
+  for gl_warn_item
+  do
+    case " $3 " in
+      *" $gl_warn_item "*)
+        ;;
+      *)
+        gl_warn_set="$gl_warn_set $gl_warn_item"
+        ;;
+    esac
+  done
+  $1=$gl_warn_set
+])
+
+# gl_MANYWARN_ALL_GCC(VARIABLE)
+# -----------------------------
+# Add all documented GCC warning parameters to variable VARIABLE.
+# Note that you need to test them using gl_WARN_ADD if you want to
+# make sure your gcc understands it.
+AC_DEFUN([gl_MANYWARN_ALL_GCC],
+[
+  dnl First, check for some issues that only occur when combining multiple
+  dnl gcc warning categories.
+  AC_REQUIRE([AC_PROG_CC])
+  if test -n "$GCC"; then
+
+    dnl Check if -W -Werror -Wno-missing-field-initializers is supported
+    dnl with the current $CC $CFLAGS $CPPFLAGS.
+    AC_MSG_CHECKING([whether -Wno-missing-field-initializers is supported])
+    AC_CACHE_VAL([gl_cv_cc_nomfi_supported], [
+      gl_save_CFLAGS="$CFLAGS"
+      CFLAGS="$CFLAGS -W -Werror -Wno-missing-field-initializers"
+      AC_COMPILE_IFELSE(
+        [AC_LANG_PROGRAM([[]], [[]])],
+        [gl_cv_cc_nomfi_supported=yes],
+        [gl_cv_cc_nomfi_supported=no])
+      CFLAGS="$gl_save_CFLAGS"])
+    AC_MSG_RESULT([$gl_cv_cc_nomfi_supported])
+
+    if test "$gl_cv_cc_nomfi_supported" = yes; then
+      dnl Now check whether -Wno-missing-field-initializers is needed
+      dnl for the { 0, } construct.
+      AC_MSG_CHECKING([whether -Wno-missing-field-initializers is needed])
+      AC_CACHE_VAL([gl_cv_cc_nomfi_needed], [
+        gl_save_CFLAGS="$CFLAGS"
+        CFLAGS="$CFLAGS -W -Werror"
+        AC_COMPILE_IFELSE(
+          [AC_LANG_PROGRAM(
+             [[void f (void)
+               {
+                 typedef struct { int a; int b; } s_t;
+                 s_t s1 = { 0, };
+               }
+             ]],
+             [[]])],
+          [gl_cv_cc_nomfi_needed=no],
+          [gl_cv_cc_nomfi_needed=yes])
+        CFLAGS="$gl_save_CFLAGS"
+      ])
+      AC_MSG_RESULT([$gl_cv_cc_nomfi_needed])
+    fi
+
+    dnl Next, check if -Werror -Wuninitialized is useful with the
+    dnl user's choice of $CFLAGS; some versions of gcc warn that it
+    dnl has no effect if -O is not also used
+    AC_MSG_CHECKING([whether -Wuninitialized is supported])
+    AC_CACHE_VAL([gl_cv_cc_uninitialized_supported], [
+      gl_save_CFLAGS="$CFLAGS"
+      CFLAGS="$CFLAGS -Werror -Wuninitialized"
+      AC_COMPILE_IFELSE(
+        [AC_LANG_PROGRAM([[]], [[]])],
+        [gl_cv_cc_uninitialized_supported=yes],
+        [gl_cv_cc_uninitialized_supported=no])
+      CFLAGS="$gl_save_CFLAGS"])
+    AC_MSG_RESULT([$gl_cv_cc_uninitialized_supported])
+
+  fi
+
+  # List all gcc warning categories.
+  gl_manywarn_set=
+  for gl_manywarn_item in \
+    -W \
+    -Wabi \
+    -Waddress \
+    -Wall \
+    -Warray-bounds \
+    -Wattributes \
+    -Wbad-function-cast \
+    -Wbuiltin-macro-redefined \
+    -Wcast-align \
+    -Wchar-subscripts \
+    -Wclobbered \
+    -Wcomment \
+    -Wcomments \
+    -Wcoverage-mismatch \
+    -Wcpp \
+    -Wdeprecated \
+    -Wdeprecated-declarations \
+    -Wdisabled-optimization \
+    -Wdiv-by-zero \
+    -Wdouble-promotion \
+    -Wempty-body \
+    -Wendif-labels \
+    -Wenum-compare \
+    -Wextra \
+    -Wformat-contains-nul \
+    -Wformat-extra-args \
+    -Wformat-nonliteral \
+    -Wformat-security \
+    -Wformat-y2k \
+    -Wformat-zero-length \
+    -Wformat=2 \
+    -Wfree-nonheap-object \
+    -Wignored-qualifiers \
+    -Wimplicit \
+    -Wimplicit-function-declaration \
+    -Wimplicit-int \
+    -Winit-self \
+    -Winline \
+    -Wint-to-pointer-cast \
+    -Winvalid-memory-model \
+    -Winvalid-pch \
+    -Wjump-misses-init \
+    -Wlogical-op \
+    -Wmain \
+    -Wmaybe-uninitialized \
+    -Wmissing-braces \
+    -Wmissing-declarations \
+    -Wmissing-field-initializers \
+    -Wmissing-format-attribute \
+    -Wmissing-include-dirs \
+    -Wmissing-noreturn \
+    -Wmissing-parameter-type \
+    -Wmissing-prototypes \
+    -Wmudflap \
+    -Wmultichar \
+    -Wnarrowing \
+    -Wnested-externs \
+    -Wnonnull \
+    -Wnormalized=nfc \
+    -Wold-style-declaration \
+    -Wold-style-definition \
+    -Woverflow \
+    -Woverlength-strings \
+    -Woverride-init \
+    -Wpacked \
+    -Wpacked-bitfield-compat \
+    -Wparentheses \
+    -Wpointer-arith \
+    -Wpointer-sign \
+    -Wpointer-to-int-cast \
+    -Wpragmas \
+    -Wreturn-type \
+    -Wsequence-point \
+    -Wshadow \
+    -Wsizeof-pointer-memaccess \
+    -Wstack-protector \
+    -Wstrict-aliasing \
+    -Wstrict-overflow \
+    -Wstrict-prototypes \
+    -Wsuggest-attribute=const \
+    -Wsuggest-attribute=format \
+    -Wsuggest-attribute=noreturn \
+    -Wsuggest-attribute=pure \
+    -Wswitch \
+    -Wswitch-default \
+    -Wsync-nand \
+    -Wsystem-headers \
+    -Wtrampolines \
+    -Wtrigraphs \
+    -Wtype-limits \
+    -Wuninitialized \
+    -Wunknown-pragmas \
+    -Wunreachable-code \
+    -Wunsafe-loop-optimizations \
+    -Wunused \
+    -Wunused-but-set-parameter \
+    -Wunused-but-set-variable \
+    -Wunused-function \
+    -Wunused-label \
+    -Wunused-local-typedefs \
+    -Wunused-macros \
+    -Wunused-parameter \
+    -Wunused-result \
+    -Wunused-value \
+    -Wunused-variable \
+    -Wvarargs \
+    -Wvariadic-macros \
+    -Wvector-operation-performance \
+    -Wvla \
+    -Wvolatile-register-var \
+    -Wwrite-strings \
+    \
+    ; do
+    gl_manywarn_set="$gl_manywarn_set $gl_manywarn_item"
+  done
+
+  # Disable specific options as needed.
+  if test "$gl_cv_cc_nomfi_needed" = yes; then
+    gl_manywarn_set="$gl_manywarn_set -Wno-missing-field-initializers"
+  fi
+
+  if test "$gl_cv_cc_uninitialized_supported" = no; then
+    gl_manywarn_set="$gl_manywarn_set -Wno-uninitialized"
+  fi
+
+  $1=$gl_manywarn_set
+])
diff --git a/m4/warnings.m4 b/m4/warnings.m4
new file mode 100644
index 0000000..4b2ac38
--- /dev/null
+++ b/m4/warnings.m4
@@ -0,0 +1,61 @@
+# warnings.m4 serial 7
+dnl Copyright (C) 2008-2013 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl From Simon Josefsson
+
+# gl_AS_VAR_APPEND(VAR, VALUE)
+# ----------------------------
+# Provide the functionality of AS_VAR_APPEND if Autoconf does not have it.
+m4_ifdef([AS_VAR_APPEND],
+[m4_copy([AS_VAR_APPEND], [gl_AS_VAR_APPEND])],
+[m4_define([gl_AS_VAR_APPEND],
+[AS_VAR_SET([$1], [AS_VAR_GET([$1])$2])])])
+
+
+# gl_COMPILER_OPTION_IF(OPTION, [IF-SUPPORTED], [IF-NOT-SUPPORTED],
+#                       [PROGRAM = AC_LANG_PROGRAM()])
+# -----------------------------------------------------------------
+# Check if the compiler supports OPTION when compiling PROGRAM.
+#
+# FIXME: gl_Warn must be used unquoted until we can assume Autoconf
+# 2.64 or newer.
+AC_DEFUN([gl_COMPILER_OPTION_IF],
+[AS_VAR_PUSHDEF([gl_Warn], [gl_cv_warn_[]_AC_LANG_ABBREV[]_$1])dnl
+AS_VAR_PUSHDEF([gl_Flags], [_AC_LANG_PREFIX[]FLAGS])dnl
+AC_CACHE_CHECK([whether _AC_LANG compiler handles $1], m4_defn([gl_Warn]), [
+  gl_save_compiler_FLAGS="$gl_Flags"
+  gl_AS_VAR_APPEND(m4_defn([gl_Flags]), [" $1"])
+  AC_COMPILE_IFELSE([m4_default([$4], [AC_LANG_PROGRAM([])])],
+                    [AS_VAR_SET(gl_Warn, [yes])],
+                    [AS_VAR_SET(gl_Warn, [no])])
+  gl_Flags="$gl_save_compiler_FLAGS"
+])
+AS_VAR_IF(gl_Warn, [yes], [$2], [$3])
+AS_VAR_POPDEF([gl_Flags])dnl
+AS_VAR_POPDEF([gl_Warn])dnl
+])
+
+
+# gl_WARN_ADD(OPTION, [VARIABLE = WARN_CFLAGS],
+#             [PROGRAM = AC_LANG_PROGRAM()])
+# ---------------------------------------------
+# Adds parameter to WARN_CFLAGS if the compiler supports it when
+# compiling PROGRAM.  For example, gl_WARN_ADD([-Wparentheses]).
+#
+# If VARIABLE is a variable name, AC_SUBST it.
+AC_DEFUN([gl_WARN_ADD],
+[gl_COMPILER_OPTION_IF([$1],
+  [gl_AS_VAR_APPEND(m4_if([$2], [], [[WARN_CFLAGS]], [[$2]]), [" $1"])],
+  [],
+  [$3])
+m4_ifval([$2],
+         [AS_LITERAL_IF([$2], [AC_SUBST([$2])])],
+         [AC_SUBST([WARN_CFLAGS])])dnl
+])
+
+# Local Variables:
+# mode: autoconf
+# End:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]