changeset 37520:b46e093d1df7

assure: new module This works better than 'assert' when compiling with -DNDEBUG, as it avoids some compiler diagnostics in that case. Reported by Norihiro Tanaka in: http://lists.gnu.org/archive/html/bug-gnulib/2014-12/msg00215.html * MODULES.html.sh (func_all_modules): Add 'assure'. * lib/assure.h, modules/assure: New files. * lib/chdir-long.c, lib/cycle-check.c, lib/fchdir.c, lib/fts.c: * lib/poll.c, lib/savewd.c, lib/utimens.c, lib/xstrtol.c: Prefer 'assure' to 'assert'. * modules/chdir-long, modules/cycle-check, modules/fchdir: * modules/poll, modules/savewd, modules/utimens, modules/xstrtol: Depend on 'assure'.
author Paul Eggert <eggert@cs.ucla.edu>
date Sat, 20 Dec 2014 13:00:21 -0800
parents 4483d89bc1e8
children 047d9cf411ce
files ChangeLog MODULES.html.sh lib/assure.h lib/chdir-long.c lib/cycle-check.c lib/fchdir.c lib/fts.c lib/poll.c lib/savewd.c lib/utimens.c lib/xstrtol.c modules/assure modules/chdir-long modules/cycle-check modules/fchdir modules/poll modules/savewd modules/utimens modules/xstrtol
diffstat 19 files changed, 110 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Dec 19 06:56:42 2014 -0800
+++ b/ChangeLog	Sat Dec 20 13:00:21 2014 -0800
@@ -1,3 +1,19 @@
+2014-12-20  Paul Eggert  <eggert@cs.ucla.edu>
+
+	assure: new module
+	This works better than 'assert' when compiling with -DNDEBUG,
+	as it avoids some compiler diagnostics in that case.
+	Reported by Norihiro Tanaka in:
+	http://lists.gnu.org/archive/html/bug-gnulib/2014-12/msg00215.html
+	* MODULES.html.sh (func_all_modules): Add 'assure'.
+	* lib/assure.h, modules/assure: New files.
+	* lib/chdir-long.c, lib/cycle-check.c, lib/fchdir.c, lib/fts.c:
+	* lib/poll.c, lib/savewd.c, lib/utimens.c, lib/xstrtol.c:
+	Prefer 'assure' to 'assert'.
+	* modules/chdir-long, modules/cycle-check, modules/fchdir:
+	* modules/poll, modules/savewd, modules/utimens, modules/xstrtol:
+	Depend on 'assure'.
+
 2014-12-16  Paul Eggert  <eggert@cs.ucla.edu>
 
 	stdalign: port better to HP compilers
--- a/MODULES.html.sh	Fri Dec 19 06:56:42 2014 -0800
+++ b/MODULES.html.sh	Sat Dec 20 13:00:21 2014 -0800
@@ -1663,6 +1663,7 @@
 
   func_begin_table
   func_module assert
+  func_module assure
   func_module verify
   func_end_table
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/assure.h	Sat Dec 20 13:00:21 2014 -0800
@@ -0,0 +1,37 @@
+/* Run-time assert-like macros.
+
+   Copyright (C) 2014 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert.  */
+
+#ifndef _GL_ASSURE_H
+#define _GL_ASSURE_H
+
+#include <assert.h>
+
+/* Check E's value at runtime, and report an error and abort if not.
+   However, do nothng if NDEBUG is defined.
+
+   Unlike standard 'assert', this macro always compiles E even when NDEBUG
+   is defined, so as to catch typos and avoid some GCC warnings.  */
+
+#ifdef NDEBUG
+# define assure(E) ((void) (0 && (E)))
+#else
+# define assure(E) assert (E)
+#endif
+
+#endif
--- a/lib/chdir-long.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/chdir-long.c	Sat Dec 20 13:00:21 2014 -0800
@@ -20,7 +20,6 @@
 
 #include "chdir-long.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -28,6 +27,8 @@
 #include <string.h>
 #include <stdio.h>
 
+#include "assure.h"
+
 #ifndef PATH_MAX
 # error "compile this file only if your system defines PATH_MAX"
 #endif
@@ -60,7 +61,7 @@
   if (0 <= cdb->fd)
     {
       bool close_fail = close (cdb->fd);
-      assert (! close_fail);
+      assure (! close_fail);
     }
 }
 
@@ -122,8 +123,8 @@
 
     /* If DIR is the empty string, then the chdir above
        must have failed and set errno to ENOENT.  */
-    assert (0 < len);
-    assert (PATH_MAX <= len);
+    assure (0 < len);
+    assure (PATH_MAX <= len);
 
     /* Count leading slashes.  */
     n_leading_slash = strspn (dir, "/");
@@ -158,8 +159,8 @@
         dir += n_leading_slash;
       }
 
-    assert (*dir != '/');
-    assert (dir <= dir_end);
+    assure (*dir != '/');
+    assure (dir <= dir_end);
 
     while (PATH_MAX <= dir_end - dir)
       {
@@ -175,7 +176,7 @@
           }
 
         *slash = '\0';
-        assert (slash - dir < PATH_MAX);
+        assure (slash - dir < PATH_MAX);
         err = cdb_advance_fd (&cdb, dir);
         *slash = '/';
         if (err != 0)
--- a/lib/cycle-check.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/cycle-check.c	Sat Dec 20 13:00:21 2014 -0800
@@ -19,15 +19,15 @@
 
 #include <config.h>
 
+#include "cycle-check.h"
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdio.h>
-#include <assert.h>
 #include <stdlib.h>
-
 #include <stdbool.h>
 
-#include "cycle-check.h"
+#include "assure.h"
 
 #define CC_MAGIC 9827862
 
@@ -57,7 +57,7 @@
 bool
 cycle_check (struct cycle_check_state *state, struct stat const *sb)
 {
-  assert (state->magic == CC_MAGIC);
+  assure (state->magic == CC_MAGIC);
 
   /* If the current directory ever happens to be the same
      as the one we last recorded for the cycle detection,
--- a/lib/fchdir.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/fchdir.c	Sat Dec 20 13:00:21 2014 -0800
@@ -19,7 +19,6 @@
 /* Specification.  */
 #include <unistd.h>
 
-#include <assert.h>
 #include <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -29,6 +28,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include "assure.h"
 #include "dosname.h"
 #include "filenamecat.h"
 
@@ -132,7 +132,7 @@
 {
   struct stat statbuf;
 
-  assert (0 <= fd);
+  assure (0 <= fd);
   if (REPLACE_OPEN_DIRECTORY
       || (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode)))
     {
@@ -156,7 +156,7 @@
 int
 _gl_register_dup (int oldfd, int newfd)
 {
-  assert (0 <= oldfd && 0 <= newfd && oldfd != newfd);
+  assure (0 <= oldfd && 0 <= newfd && oldfd != newfd);
   if (oldfd < dirs_allocated && dirs[oldfd].name)
     {
       /* Duplicated a directory; must ensure newfd is allocated.  */
--- a/lib/fts.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/fts.c	Sat Dec 20 13:00:21 2014 -0800
@@ -189,7 +189,7 @@
 #endif
 
 #ifdef NDEBUG
-# define fts_assert(expr) ((void) 0)
+# define fts_assert(expr) ((void) (0 && (expr)))
 #else
 # define fts_assert(expr)       \
     do                          \
--- a/lib/poll.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/poll.c	Sat Dec 20 13:00:21 2014 -0800
@@ -33,7 +33,6 @@
 
 #include <errno.h>
 #include <limits.h>
-#include <assert.h>
 
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 # define WINDOWS_NATIVE
@@ -59,6 +58,8 @@
 
 #include <time.h>
 
+#include "assure.h"
+
 #ifndef INFTIM
 # define INFTIM (-1)
 #endif
@@ -480,7 +481,7 @@
         continue;
 
       h = (HANDLE) _get_osfhandle (pfd[i].fd);
-      assert (h != NULL);
+      assure (h != NULL);
       if (IsSocketHandle (h))
         {
           int requested = FD_CLOSE;
--- a/lib/savewd.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/savewd.c	Sat Dec 20 13:00:21 2014 -0800
@@ -23,7 +23,6 @@
 
 #include "savewd.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -33,6 +32,7 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
+#include "assure.h"
 #include "dosname.h"
 #include "fcntl-safer.h"
 
@@ -88,7 +88,7 @@
       break;
 
     default:
-      assert (false);
+      assure (false);
     }
 
   return false;
@@ -144,11 +144,11 @@
                 break;
 
               case FORKING_STATE:
-                assert (wd->val.child == 0);
+                assure (wd->val.child == 0);
                 break;
 
               default:
-                assert (false);
+                assure (false);
               }
         }
     }
@@ -205,7 +205,7 @@
           {
             int child_status;
             while (waitpid (child, &child_status, 0) < 0)
-              assert (errno == EINTR);
+              assure (errno == EINTR);
             wd->val.child = -1;
             if (! WIFEXITED (child_status))
               raise (WTERMSIG (child_status));
@@ -215,7 +215,7 @@
       break;
 
     default:
-      assert (false);
+      assure (false);
     }
 
   return 0;
@@ -236,11 +236,11 @@
       break;
 
     case FORKING_STATE:
-      assert (wd->val.child < 0);
+      assure (wd->val.child < 0);
       break;
 
     default:
-      assert (false);
+      assure (false);
     }
 
   wd->state = FINAL_STATE;
--- a/lib/utimens.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/utimens.c	Sat Dec 20 13:00:21 2014 -0800
@@ -24,7 +24,6 @@
 #define _GL_UTIMENS_INLINE _GL_EXTERN_INLINE
 #include "utimens.h"
 
-#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdbool.h>
@@ -87,7 +86,6 @@
 {
   int result = 0;
   int utime_omit_count = 0;
-  assert (timespec);
   if ((timespec[0].tv_nsec != UTIME_NOW
        && timespec[0].tv_nsec != UTIME_OMIT
        && ! (0 <= timespec[0].tv_nsec
--- a/lib/xstrtol.c	Fri Dec 19 06:56:42 2014 -0800
+++ b/lib/xstrtol.c	Sat Dec 20 13:00:21 2014 -0800
@@ -34,13 +34,13 @@
    need stderr defined if assertion checking is enabled.  */
 #include <stdio.h>
 
-#include <assert.h>
 #include <ctype.h>
 #include <errno.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
 
+#include "assure.h"
 #include "intprops.h"
 
 /* xstrtoll.c and xstrtoull.c, which include this file, require that
@@ -93,7 +93,7 @@
   __strtol_t tmp;
   strtol_error err = LONGINT_OK;
 
-  assert (0 <= strtol_base && strtol_base <= 36);
+  assure (0 <= strtol_base && strtol_base <= 36);
 
   p = (ptr ? ptr : &t_ptr);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/assure	Sat Dec 20 13:00:21 2014 -0800
@@ -0,0 +1,20 @@
+Description:
+Run-time assert-like macros.
+
+Files:
+lib/assure.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+
+Include:
+"assure.h"
+
+License:
+LGPLv2+
+
+Maintainer:
+Paul Eggert, Jim Meyering
--- a/modules/chdir-long	Fri Dec 19 06:56:42 2014 -0800
+++ b/modules/chdir-long	Sat Dec 20 13:00:21 2014 -0800
@@ -11,6 +11,7 @@
 unistd
 pathmax
 chdir
+assure          [test $gl_cv_have_arbitrary_file_name_length_limit = yes]
 atexit          [test $gl_cv_have_arbitrary_file_name_length_limit = yes]
 fchdir          [test $gl_cv_have_arbitrary_file_name_length_limit = yes]
 fcntl-h         [test $gl_cv_have_arbitrary_file_name_length_limit = yes]
--- a/modules/cycle-check	Fri Dec 19 06:56:42 2014 -0800
+++ b/modules/cycle-check	Sat Dec 20 13:00:21 2014 -0800
@@ -7,6 +7,7 @@
 m4/cycle-check.m4
 
 Depends-on:
+assure
 dev-ino
 same-inode
 stdbool
--- a/modules/fchdir	Fri Dec 19 06:56:42 2014 -0800
+++ b/modules/fchdir	Sat Dec 20 13:00:21 2014 -0800
@@ -7,6 +7,7 @@
 
 Depends-on:
 unistd
+assure           [test $HAVE_FCHDIR = 0]
 chdir            [test $HAVE_FCHDIR = 0]
 close            [test $HAVE_FCHDIR = 0]
 dirent           [test $HAVE_FCHDIR = 0]
--- a/modules/poll	Fri Dec 19 06:56:42 2014 -0800
+++ b/modules/poll	Sat Dec 20 13:00:21 2014 -0800
@@ -8,6 +8,7 @@
 Depends-on:
 poll-h
 alloca          [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1]
+assure          [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1]
 select          [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1]
 sockets         [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1]
 sys_select      [test $HAVE_POLL = 0 || test $REPLACE_POLL = 1]
--- a/modules/savewd	Fri Dec 19 06:56:42 2014 -0800
+++ b/modules/savewd	Sat Dec 20 13:00:21 2014 -0800
@@ -7,6 +7,7 @@
 m4/savewd.m4
 
 Depends-on:
+assure
 chdir
 dosname
 errno
--- a/modules/utimens	Fri Dec 19 06:56:42 2014 -0800
+++ b/modules/utimens	Sat Dec 20 13:00:21 2014 -0800
@@ -9,6 +9,7 @@
 m4/utimes.m4
 
 Depends-on:
+assure
 errno
 extern-inline
 fcntl-h
--- a/modules/xstrtol	Fri Dec 19 06:56:42 2014 -0800
+++ b/modules/xstrtol	Sat Dec 20 13:00:21 2014 -0800
@@ -9,6 +9,7 @@
 m4/xstrtol.m4
 
 Depends-on:
+assure
 exitfail
 error
 getopt-gnu