Mercurial > gnulib
changeset 19272:c20fd8143023
timespec: prefer ‘assume’ to ‘assure’
This avoids some runtime tests. The rest of the module makes
similar assumptions and there is little point to testing here.
* lib/timespec.h: Include verify.h instead of assure.h.
(timespec_cmp): Use ‘assume’, not ‘assure’.
Also, remove an unnecessary cast to ‘int’, as lots of other
code in this module now causes -Wconversion to complain, and
this is a problem with -Wconversion not with the code.
* modules/timespec (Depends-on): Depend on ‘verify’, not ‘assure’.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sun, 29 Oct 2017 16:22:41 -0700 |
parents | 6d9b9b82d0af |
children | 638b6d1fdf36 bd6ce52f2c6b |
files | ChangeLog lib/timespec.h modules/timespec |
diffstat | 3 files changed, 26 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Sun Oct 29 15:46:10 2017 -0700 +++ b/ChangeLog Sun Oct 29 16:22:41 2017 -0700 @@ -1,5 +1,16 @@ 2017-10-29 Paul Eggert <eggert@cs.ucla.edu> + timespec: prefer ‘assume’ to ‘assure’ + This avoids some runtime tests. The rest of the module makes + similar assumptions and there is little point to testing here. + * lib/timespec.h: Include verify.h instead of assure.h. + (timespec_cmp): Use ‘assume’, not ‘assure’. + Also, remove an unnecessary cast to ‘int’, as lots of other + code in this module now causes -Wconversion to complain, and + this is a problem with -Wconversion not with the code. + + * modules/timespec (Depends-on): Depend on ‘verify’, not ‘assure’. + Port recent gnulib-tool change to Dash * gnulib-tool (func_create_testdir): Don't assume that the shell retokenizes after expanding "$@" inside the call to
--- a/lib/timespec.h Sun Oct 29 15:46:10 2017 -0700 +++ b/lib/timespec.h Sun Oct 29 16:22:41 2017 -0700 @@ -33,7 +33,7 @@ extern "C" { #endif -#include "assure.h" +#include "verify.h" /* Resolution of timespec timestamps (in units per second), and log base 10 of the resolution. */ @@ -69,27 +69,29 @@ any platform of interest to the GNU project, since all such platforms have 32-bit int or wider. - Replacing "(int) (a.tv_nsec - b.tv_nsec)" with something like + Replacing "a.tv_nsec - b.tv_nsec" with something like "a.tv_nsec < b.tv_nsec ? -1 : a.tv_nsec > b.tv_nsec" would cause this function to work in some cases where the above assumption is violated, but not in all cases (e.g., a.tv_sec==1, a.tv_nsec==-2, b.tv_sec==0, b.tv_nsec==999999999) and is arguably not worth the extra instructions. Using a subtraction has the advantage of detecting some invalid cases on platforms that detect integer - overflow. - - The (int) cast avoids a gcc -Wconversion warning. */ + overflow. */ _GL_TIMESPEC_INLINE int _GL_ATTRIBUTE_PURE timespec_cmp (struct timespec a, struct timespec b) { - /* These assure calls teach gcc7 enough so that its - -Wstrict-overflow does not complain about the following code. */ - assure (-1 <= a.tv_nsec && a.tv_nsec <= 2 * TIMESPEC_RESOLUTION); - assure (-1 <= b.tv_nsec && b.tv_nsec <= 2 * TIMESPEC_RESOLUTION); - return (a.tv_sec < b.tv_sec ? -1 - : a.tv_sec > b.tv_sec ? 1 - : (int) (a.tv_nsec - b.tv_nsec)); + if (a.tv_sec < b.tv_sec) + return -1; + if (a.tv_sec > b.tv_sec) + return 1; + + /* Pacify gcc -Wstrict-overflow (bleeding-edge circa 2017-10-02). See: + http://lists.gnu.org/archive/html/bug-gnulib/2017-10/msg00006.html */ + assume (-1 <= a.tv_nsec && a.tv_nsec <= 2 * TIMESPEC_RESOLUTION); + assume (-1 <= b.tv_nsec && b.tv_nsec <= 2 * TIMESPEC_RESOLUTION); + + return a.tv_nsec - b.tv_nsec; } /* Return -1, 0, 1, depending on the sign of A. A.tv_nsec must be