Module Name: src Committed By: rillig Date: Sat Jan 27 15:53:28 UTC 2024
Modified Files: src/usr.bin/xlint/lint1: lex.c Log Message: lint: split determining the type of an integer constant No functional change. To generate a diff of this commit: cvs rdiff -u -r1.201 -r1.202 src/usr.bin/xlint/lint1/lex.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/xlint/lint1/lex.c diff -u src/usr.bin/xlint/lint1/lex.c:1.201 src/usr.bin/xlint/lint1/lex.c:1.202 --- src/usr.bin/xlint/lint1/lex.c:1.201 Sat Jan 27 12:14:58 2024 +++ src/usr.bin/xlint/lint1/lex.c Sat Jan 27 15:53:27 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: lex.c,v 1.201 2024/01/27 12:14:58 rillig Exp $ */ +/* $NetBSD: lex.c,v 1.202 2024/01/27 15:53:27 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) -__RCSID("$NetBSD: lex.c,v 1.201 2024/01/27 12:14:58 rillig Exp $"); +__RCSID("$NetBSD: lex.c,v 1.202 2024/01/27 15:53:27 rillig Exp $"); #endif #include <ctype.h> @@ -502,6 +502,67 @@ is_unsigned_since_c90(tspec_t typ, uint6 return typ == LONG && ui > TARG_LONG_MAX; } +static tspec_t +integer_constant_type(tspec_t typ, uint64_t ui, int base, bool warned) +{ + switch (typ) { + case INT: + if (ui <= TARG_INT_MAX) { + /* ok */ + } else if (ui <= TARG_UINT_MAX && base != 10) { + typ = UINT; + } else if (ui <= TARG_LONG_MAX) { + typ = LONG; + } else { + typ = ULONG; + if (ui > TARG_ULONG_MAX && !warned) { + /* integer constant out of range */ + warning(252); + } + } + if ((typ == UINT || typ == ULONG) && !allow_c90) + typ = LONG; + break; + case UINT: + if (ui > TARG_UINT_MAX) { + typ = ULONG; + if (ui > TARG_ULONG_MAX && !warned) { + /* integer constant out of range */ + warning(252); + } + } + break; + case LONG: + if (ui > TARG_LONG_MAX && allow_c90) { + typ = ULONG; + if (ui > TARG_ULONG_MAX && !warned) { + /* integer constant out of range */ + warning(252); + } + } + break; + case ULONG: + if (ui > TARG_ULONG_MAX && !warned) { + /* integer constant out of range */ + warning(252); + } + break; + case LLONG: + if (ui > TARG_LLONG_MAX && allow_c90) + typ = ULLONG; + break; + case ULLONG: + if (ui > TARG_ULLONG_MAX && !warned) { + /* integer constant out of range */ + warning(252); + } + break; + default: + break; + } + return typ; +} + int lex_integer_constant(const char *yytext, size_t yyleng, int base) { @@ -543,7 +604,7 @@ lex_integer_constant(const char *yytext, /* suffix 'U' is illegal in traditional C */ warning(97); } - tspec_t typ = suffix_type[u_suffix][l_suffix]; + tspec_t ct = suffix_type[u_suffix][l_suffix]; bool warned = false; errno = 0; @@ -561,67 +622,13 @@ lex_integer_constant(const char *yytext, query_message(8, (int)len, cp); } - bool ansiu = is_unsigned_since_c90(typ, ui, base); - switch (typ) { - case INT: - if (ui <= TARG_INT_MAX) { - /* ok */ - } else if (ui <= TARG_UINT_MAX && base != 10) { - typ = UINT; - } else if (ui <= TARG_LONG_MAX) { - typ = LONG; - } else { - typ = ULONG; - if (ui > TARG_ULONG_MAX && !warned) { - /* integer constant out of range */ - warning(252); - } - } - if ((typ == UINT || typ == ULONG) && !allow_c90) - typ = LONG; - break; - case UINT: - if (ui > TARG_UINT_MAX) { - typ = ULONG; - if (ui > TARG_ULONG_MAX && !warned) { - /* integer constant out of range */ - warning(252); - } - } - break; - case LONG: - if (ui > TARG_LONG_MAX && allow_c90) { - typ = ULONG; - if (ui > TARG_ULONG_MAX && !warned) { - /* integer constant out of range */ - warning(252); - } - } - break; - case ULONG: - if (ui > TARG_ULONG_MAX && !warned) { - /* integer constant out of range */ - warning(252); - } - break; - case LLONG: - if (ui > TARG_LLONG_MAX && allow_c90) - typ = ULLONG; - break; - case ULLONG: - if (ui > TARG_ULLONG_MAX && !warned) { - /* integer constant out of range */ - warning(252); - } - break; - default: - break; - } + bool ansiu = is_unsigned_since_c90(ct, ui, base); - ui = (uint64_t)convert_integer((int64_t)ui, typ, 0); + tspec_t t = integer_constant_type(ct, ui, base, warned); + ui = (uint64_t)convert_integer((int64_t)ui, t, 0); yylval.y_val = xcalloc(1, sizeof(*yylval.y_val)); - yylval.y_val->v_tspec = typ; + yylval.y_val->v_tspec = t; yylval.y_val->v_unsigned_since_c90 = ansiu; yylval.y_val->u.integer = (int64_t)ui;