]> Savannah Git Hosting - gnulib.git/commitdiff
assure: new module
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 20 Dec 2014 21:00:21 +0000 (13:00 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 20 Dec 2014 21:01:10 +0000 (13:01 -0800)
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'.

19 files changed:
ChangeLog
MODULES.html.sh
lib/assure.h [new file with mode: 0644]
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 [new file with mode: 0644]
modules/chdir-long
modules/cycle-check
modules/fchdir
modules/poll
modules/savewd
modules/utimens
modules/xstrtol

index 877a72155490d105fd9676509efc680de96ce912..3cf10e51e31d8761e1cc414aab8f9282a3f78f8a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
index db66253c937b98f88ce8fadcad1b47eabaca0c1a..ce76223c7245f6cad72d750ceef87a02740d3772 100755 (executable)
@@ -1663,6 +1663,7 @@ func_all_modules ()
 
   func_begin_table
   func_module assert
+  func_module assure
   func_module verify
   func_end_table
 
diff --git a/lib/assure.h b/lib/assure.h
new file mode 100644 (file)
index 0000000..a53e55f
--- /dev/null
@@ -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
index 5b1b18fc35a180db2dfd6df0455065bea176ba09..14c6733750182835da8b425237e0c7aa8193e4d6 100644 (file)
@@ -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 @@ cdb_free (struct cd_buf const *cdb)
   if (0 <= cdb->fd)
     {
       bool close_fail = close (cdb->fd);
-      assert (! close_fail);
+      assure (! close_fail);
     }
 }
 
@@ -122,8 +123,8 @@ chdir_long (char *dir)
 
     /* 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 @@ chdir_long (char *dir)
         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 @@ chdir_long (char *dir)
           }
 
         *slash = '\0';
-        assert (slash - dir < PATH_MAX);
+        assure (slash - dir < PATH_MAX);
         err = cdb_advance_fd (&cdb, dir);
         *slash = '/';
         if (err != 0)
index f7b3d078856d25cc6dcefad59064e3f08f5050e9..95a9bed1fffd91b1806e32d59510d39ad64875ab 100644 (file)
 
 #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 @@ cycle_check_init (struct cycle_check_state *state)
 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,
index 5d71377385d34c453f5213c8d14a6616b2df49a2..ab0be7ab7f5acb08974514d7eb70a7454d056a2e 100644 (file)
@@ -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 @@ _gl_register_fd (int fd, const char *filename)
 {
   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 @@ _gl_register_fd (int fd, const char *filename)
 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.  */
index 9851c53104c26f5f821e6cc723023763d360bea4..4e136c9834f602c18d6bc81ab461f5a9377936d7 100644 (file)
--- a/lib/fts.c
+++ b/lib/fts.c
@@ -189,7 +189,7 @@ enum Fts_stat
 #endif
 
 #ifdef NDEBUG
-# define fts_assert(expr) ((void) 0)
+# define fts_assert(expr) ((void) (0 && (expr)))
 #else
 # define fts_assert(expr)       \
     do                          \
index a3e0ab7e7d684fa3c5ff726b8201171aa4b11975..c2e5b4218f6265bd39c87047c6eb325c13313659 100644 (file)
@@ -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 @@ restart:
         continue;
 
       h = (HANDLE) _get_osfhandle (pfd[i].fd);
-      assert (h != NULL);
+      assure (h != NULL);
       if (IsSocketHandle (h))
         {
           int requested = FD_CLOSE;
index 88c5feff8a8d2caabc4f30b38ee4a4f8285e9861..e8c1c2db762d356dc3a97629860c781d06670f72 100644 (file)
@@ -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 @@ savewd_save (struct savewd *wd)
       break;
 
     default:
-      assert (false);
+      assure (false);
     }
 
   return false;
@@ -144,11 +144,11 @@ savewd_chdir (struct savewd *wd, char const *dir, int options,
                 break;
 
               case FORKING_STATE:
-                assert (wd->val.child == 0);
+                assure (wd->val.child == 0);
                 break;
 
               default:
-                assert (false);
+                assure (false);
               }
         }
     }
@@ -205,7 +205,7 @@ savewd_restore (struct savewd *wd, int status)
           {
             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 @@ savewd_restore (struct savewd *wd, int status)
       break;
 
     default:
-      assert (false);
+      assure (false);
     }
 
   return 0;
@@ -236,11 +236,11 @@ savewd_finish (struct savewd *wd)
       break;
 
     case FORKING_STATE:
-      assert (wd->val.child < 0);
+      assure (wd->val.child < 0);
       break;
 
     default:
-      assert (false);
+      assure (false);
     }
 
   wd->state = FINAL_STATE;
index dd3ec668f3753a7bc9cb70a8c0e15324d0c397c2..0444103763111903b9db452fe8d9988acc6455ec 100644 (file)
@@ -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 @@ validate_timespec (struct timespec timespec[2])
 {
   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
index f6d535a7f381f494dbbcb6f7725b0898b4b2cd71..544a74aa1c52f13db33f579adbf91dc530773307 100644 (file)
    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 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
   __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);
 
diff --git a/modules/assure b/modules/assure
new file mode 100644 (file)
index 0000000..3cfe1f8
--- /dev/null
@@ -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
index 74d9c33f7058e655d7d8fbce7f11912c93c7c696..82d4e8bb4290503a782b1202d30dd905c633ccd9 100644 (file)
@@ -11,6 +11,7 @@ Depends-on:
 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]
index 6a8618a41eac5aeb6dcec8644bdef2036a9451da..e8ef2caf7ea6b051d79d3140370986f8a0a8d05b 100644 (file)
@@ -7,6 +7,7 @@ lib/cycle-check.h
 m4/cycle-check.m4
 
 Depends-on:
+assure
 dev-ino
 same-inode
 stdbool
index af6cfa5689a93442ef94aec4d08fe79ac163b020..6e95e9a18a6c36fdf18be20b5d67141caeacde8e 100644 (file)
@@ -7,6 +7,7 @@ m4/fchdir.m4
 
 Depends-on:
 unistd
+assure           [test $HAVE_FCHDIR = 0]
 chdir            [test $HAVE_FCHDIR = 0]
 close            [test $HAVE_FCHDIR = 0]
 dirent           [test $HAVE_FCHDIR = 0]
index 8fa88fd4d1248e9ceacaf20e3b7cd4ee5d4103ef..b1d928dd5856587e3f14a60601c563d68df653b8 100644 (file)
@@ -8,6 +8,7 @@ m4/poll.m4
 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]
index b7a9feefbb8f35912b7451d10aa042ecb288bab5..a6c82672fb437f46ba2597ec4904591ae2c8bc5d 100644 (file)
@@ -7,6 +7,7 @@ lib/savewd.c
 m4/savewd.m4
 
 Depends-on:
+assure
 chdir
 dosname
 errno
index 8797774dcda36811385246ef0df161ec41a8ae0b..4aed50a1e16d498871ed2df9be02057b8e9dc0a6 100644 (file)
@@ -9,6 +9,7 @@ m4/utimens.m4
 m4/utimes.m4
 
 Depends-on:
+assure
 errno
 extern-inline
 fcntl-h
index 66e53427ebfce07f6dc2a63c0b17889424c5f08f..9c89aef089682dce1b3e518987d52023e4fda367 100644 (file)
@@ -9,6 +9,7 @@ lib/xstrtol-error.c
 m4/xstrtol.m4
 
 Depends-on:
+assure
 exitfail
 error
 getopt-gnu