style: make conversion of version string to int public

* src/parse-gram.y (str_to_version): Rename as/move to...
* src/strversion.h, src/strversion.c (strversion_to_int): these new
files.
This commit is contained in:
Akim Demaille
2020-11-11 08:19:31 +01:00
parent 14c65a35f0
commit d8b49e2b73
4 changed files with 99 additions and 48 deletions

View File

@@ -103,6 +103,8 @@ src_bison_SOURCES = \
src/state.h \
src/state-item.c \
src/state-item.h \
src/strversion.c \
src/strversion.h \
src/symlist.c \
src/symlist.h \
src/symtab.c \

View File

@@ -42,8 +42,6 @@
#include "system.h"
#include <c-ctype.h>
#include <errno.h>
#include <intprops.h>
#include <quotearg.h>
#include <vasnprintf.h>
#include <xmemdup0.h>
@@ -57,6 +55,7 @@
#include "reader.h"
#include "scan-code.h"
#include "scan-gram.h"
#include "strversion.h"
/* Pretend to be at least that version, to check features published
in that version while developping it. */
@@ -1043,56 +1042,11 @@ handle_pure_parser (location const *loc, char const *directive)
}
/* Convert VERSION into an int (MAJOR * 10000 + MINOR * 100 + MICRO).
E.g., "3.7.4" => 30704, "3.8" => 30800.
Return -1 on errors. */
static int
str_to_version (char const *version)
{
IGNORE_TYPE_LIMITS_BEGIN
int res = 0;
errno = 0;
char *cp = NULL;
{
long major = strtol (version, &cp, 10);
if (errno || cp == version || *cp != '.' || major < 0
|| INT_MULTIPLY_WRAPV (major, 10000, &res))
return -1;
}
{
++cp;
char *prev = cp;
long minor = strtol (cp, &cp, 10);
if (errno || cp == prev || (*cp != '\0' && *cp != '.')
|| ! (0 <= minor && minor < 100)
|| INT_MULTIPLY_WRAPV (minor, 100, &minor)
|| INT_ADD_WRAPV (minor, res, &res))
return -1;
}
if (*cp == '.')
{
++cp;
char *prev = cp;
long micro = strtol (cp, &cp, 10);
if (errno || cp == prev || (*cp != '\0' && *cp != '.')
|| ! (0 <= micro && micro < 100)
|| INT_ADD_WRAPV (micro, res, &res))
return -1;
}
IGNORE_TYPE_LIMITS_END
return res;
}
static void
handle_require (location const *loc, char const *version_quoted)
{
char *version = unquote (version_quoted);
required_version = str_to_version (version);
required_version = strversion_to_int (version);
if (required_version == -1)
{
complain (loc, complaint, _("invalid version requirement: %s"),

67
src/strversion.c Normal file
View File

@@ -0,0 +1,67 @@
/* Convert version string to int.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
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/>. */
#include <config.h>
#include "system.h"
#include "strversion.h"
#include <errno.h>
#include <intprops.h>
int
strversion_to_int (char const *version)
{
IGNORE_TYPE_LIMITS_BEGIN
int res = 0;
errno = 0;
char *cp = NULL;
{
long major = strtol (version, &cp, 10);
if (errno || cp == version || *cp != '.' || major < 0
|| INT_MULTIPLY_WRAPV (major, 10000, &res))
return -1;
}
{
++cp;
char *prev = cp;
long minor = strtol (cp, &cp, 10);
if (errno || cp == prev || (*cp != '\0' && *cp != '.')
|| ! (0 <= minor && minor < 100)
|| INT_MULTIPLY_WRAPV (minor, 100, &minor)
|| INT_ADD_WRAPV (minor, res, &res))
return -1;
}
if (*cp == '.')
{
++cp;
char *prev = cp;
long micro = strtol (cp, &cp, 10);
if (errno || cp == prev || (*cp != '\0' && *cp != '.')
|| ! (0 <= micro && micro < 100)
|| INT_ADD_WRAPV (micro, res, &res))
return -1;
}
IGNORE_TYPE_LIMITS_END
return res;
}

28
src/strversion.h Normal file
View File

@@ -0,0 +1,28 @@
/* Convert version string to int.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of Bison, the GNU Compiler Compiler.
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/>. */
#ifndef STRVERSION_H_
# define STRVERSION_H_
/* Convert VERSION into an int (MAJOR * 10000 + MINOR * 100 + MICRO).
E.g., "3.7.4" => 30704, "3.8" => 30800.
Return -1 on errors. */
int strversion_to_int (char const *version);
#endif