Fri Apr 2 10:13:03 2021 UTC ()
lint: name memory allocation functions consistently

No functional change.


(rillig)
diff -r1.208 -r1.209 src/usr.bin/xlint/lint1/cgram.y
diff -r1.167 -r1.168 src/usr.bin/xlint/lint1/decl.c
diff -r1.97 -r1.98 src/usr.bin/xlint/lint1/externs1.h
diff -r1.98 -r1.99 src/usr.bin/xlint/lint1/func.c
diff -r1.187 -r1.188 src/usr.bin/xlint/lint1/init.c
diff -r1.40 -r1.41 src/usr.bin/xlint/lint1/mem1.c
diff -r1.256 -r1.257 src/usr.bin/xlint/lint1/tree.c

cvs diff -r1.208 -r1.209 src/usr.bin/xlint/lint1/cgram.y (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/cgram.y 2021/04/02 09:52:36 1.208
+++ src/usr.bin/xlint/lint1/cgram.y 2021/04/02 10:13:03 1.209
@@ -1,15 +1,15 @@ @@ -1,15 +1,15 @@
1%{ 1%{
2/* $NetBSD: cgram.y,v 1.208 2021/04/02 09:52:36 rillig Exp $ */ 2/* $NetBSD: cgram.y,v 1.209 2021/04/02 10:13:03 rillig Exp $ */
3 3
4/* 4/*
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl 6 * Copyright (c) 1994, 1995 Jochen Pohl
7 * All Rights Reserved. 7 * All Rights Reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the 15 * notice, this list of conditions and the following disclaimer in the
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */ 34 */
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if defined(__RCSID) && !defined(lint) 37#if defined(__RCSID) && !defined(lint)
38__RCSID("$NetBSD: cgram.y,v 1.208 2021/04/02 09:52:36 rillig Exp $"); 38__RCSID("$NetBSD: cgram.y,v 1.209 2021/04/02 10:13:03 rillig Exp $");
39#endif 39#endif
40 40
41#include <limits.h> 41#include <limits.h>
42#include <stdlib.h> 42#include <stdlib.h>
43#include <string.h> 43#include <string.h>
44 44
45#include "lint1.h" 45#include "lint1.h"
46 46
47extern char *yytext; 47extern char *yytext;
48 48
49/* 49/*
50 * Contains the level of current declaration, used for symbol table entries. 50 * Contains the level of current declaration, used for symbol table entries.
51 * 0 is the top-level, > 0 is inside a function body. 51 * 0 is the top-level, > 0 is inside a function body.
@@ -1868,27 +1868,27 @@ expr: @@ -1868,27 +1868,27 @@ expr:
1868 ; 1868 ;
1869 1869
1870term: 1870term:
1871 T_NAME { 1871 T_NAME {
1872 /* XXX really necessary? */ 1872 /* XXX really necessary? */
1873 if (yychar < 0) 1873 if (yychar < 0)
1874 yychar = yylex(); 1874 yychar = yylex();
1875 $$ = new_name_node(getsym($1), yychar); 1875 $$ = new_name_node(getsym($1), yychar);
1876 } 1876 }
1877 | string { 1877 | string {
1878 $$ = new_string_node($1); 1878 $$ = new_string_node($1);
1879 } 1879 }
1880 | T_CON { 1880 | T_CON {
1881 $$ = new_constant_node(gettyp($1->v_tspec), $1); 1881 $$ = expr_new_constant(gettyp($1->v_tspec), $1);
1882 } 1882 }
1883 | T_LPAREN expr T_RPAREN { 1883 | T_LPAREN expr T_RPAREN {
1884 if ($2 != NULL) 1884 if ($2 != NULL)
1885 $2->tn_parenthesized = true; 1885 $2->tn_parenthesized = true;
1886 $$ = $2; 1886 $$ = $2;
1887 } 1887 }
1888 | T_LPAREN compound_statement_lbrace declaration_list 1888 | T_LPAREN compound_statement_lbrace declaration_list
1889 expr_statement_list { 1889 expr_statement_list {
1890 block_level--; 1890 block_level--;
1891 mem_block_level--; 1891 mem_block_level--;
1892 begin_initialization(mktempsym(duptyp($4->tn_type))); 1892 begin_initialization(mktempsym(duptyp($4->tn_type)));
1893 mem_block_level++; 1893 mem_block_level++;
1894 block_level++; 1894 block_level++;

cvs diff -r1.167 -r1.168 src/usr.bin/xlint/lint1/decl.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/decl.c 2021/03/30 14:25:28 1.167
+++ src/usr.bin/xlint/lint1/decl.c 2021/04/02 10:13:03 1.168
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: decl.c,v 1.167 2021/03/30 14:25:28 rillig Exp $ */ 1/* $NetBSD: decl.c,v 1.168 2021/04/02 10:13:03 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(__RCSID) && !defined(lint) 40#if defined(__RCSID) && !defined(lint)
41__RCSID("$NetBSD: decl.c,v 1.167 2021/03/30 14:25:28 rillig Exp $"); 41__RCSID("$NetBSD: decl.c,v 1.168 2021/04/02 10:13:03 rillig Exp $");
42#endif 42#endif
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <limits.h> 45#include <limits.h>
46#include <stdlib.h> 46#include <stdlib.h>
47#include <string.h> 47#include <string.h>
48 48
49#include "lint1.h" 49#include "lint1.h"
50 50
51const char *unnamed = "<unnamed>"; 51const char *unnamed = "<unnamed>";
52 52
53/* shared type structures for arithmetic types and void */ 53/* shared type structures for arithmetic types and void */
54static type_t *typetab; 54static type_t *typetab;
@@ -152,27 +152,27 @@ duptyp(const type_t *tp) @@ -152,27 +152,27 @@ duptyp(const type_t *tp)
152 *ntp = *tp; 152 *ntp = *tp;
153 return ntp; 153 return ntp;
154} 154}
155 155
156/* 156/*
157 * Use tduptyp() instead of duptyp() inside expressions (if the 157 * Use tduptyp() instead of duptyp() inside expressions (if the
158 * allocated memory should be freed after the expr). 158 * allocated memory should be freed after the expr).
159 */ 159 */
160type_t * 160type_t *
161tduptyp(const type_t *tp) 161tduptyp(const type_t *tp)
162{ 162{
163 type_t *ntp; 163 type_t *ntp;
164 164
165 ntp = tgetblk(sizeof *ntp); 165 ntp = expr_zalloc(sizeof *ntp);
166 *ntp = *tp; 166 *ntp = *tp;
167 return ntp; 167 return ntp;
168} 168}
169 169
170/* 170/*
171 * Returns whether the argument is void or an incomplete array, 171 * Returns whether the argument is void or an incomplete array,
172 * struct, union or enum type. 172 * struct, union or enum type.
173 */ 173 */
174bool 174bool
175is_incomplete(const type_t *tp) 175is_incomplete(const type_t *tp)
176{ 176{
177 tspec_t t; 177 tspec_t t;
178 178
@@ -1005,29 +1005,29 @@ check_type(sym_t *sym) @@ -1005,29 +1005,29 @@ check_type(sym_t *sym)
1005 * a better warning is printed in funcdef(). 1005 * a better warning is printed in funcdef().
1006 */ 1006 */
1007 if (t == FUNC && !tp->t_proto && 1007 if (t == FUNC && !tp->t_proto &&
1008 !(to == NOTSPEC && sym->s_osdef)) { 1008 !(to == NOTSPEC && sym->s_osdef)) {
1009 if (sflag && hflag) 1009 if (sflag && hflag)
1010 /* function declaration is not a prototype */ 1010 /* function declaration is not a prototype */
1011 warning(287); 1011 warning(287);
1012 } 1012 }
1013 if (to == FUNC) { 1013 if (to == FUNC) {
1014 if (t == FUNC || t == ARRAY) { 1014 if (t == FUNC || t == ARRAY) {
1015 /* function returns illegal type */ 1015 /* function returns illegal type */
1016 error(15); 1016 error(15);
1017 if (t == FUNC) { 1017 if (t == FUNC) {
1018 *tpp = incref(*tpp, PTR); 1018 *tpp = derive_type(*tpp, PTR);
1019 } else { 1019 } else {
1020 *tpp = incref((*tpp)->t_subt, PTR); 1020 *tpp = derive_type((*tpp)->t_subt, PTR);
1021 } 1021 }
1022 return; 1022 return;
1023 } else if (tp->t_const || tp->t_volatile) { 1023 } else if (tp->t_const || tp->t_volatile) {
1024 if (sflag) { /* XXX or better !tflag ? */ 1024 if (sflag) { /* XXX or better !tflag ? */
1025 /* function cannot return const... */ 1025 /* function cannot return const... */
1026 warning(228); 1026 warning(228);
1027 } 1027 }
1028 } 1028 }
1029 } if (to == ARRAY) { 1029 } if (to == ARRAY) {
1030 if (t == FUNC) { 1030 if (t == FUNC) {
1031 /* array of function is illegal */ 1031 /* array of function is illegal */
1032 error(16); 1032 error(16);
1033 *tpp = gettyp(INT); 1033 *tpp = gettyp(INT);
@@ -1173,27 +1173,27 @@ declarator_1_struct_union(sym_t *dsym) @@ -1173,27 +1173,27 @@ declarator_1_struct_union(sym_t *dsym)
1173 rmsym(dcs->d_redeclared_symbol); 1173 rmsym(dcs->d_redeclared_symbol);
1174 } 1174 }
1175 } 1175 }
1176 1176
1177 check_type(dsym); 1177 check_type(dsym);
1178 1178
1179 t = (tp = dsym->s_type)->t_tspec; 1179 t = (tp = dsym->s_type)->t_tspec;
1180 1180
1181 if (dsym->s_bitfield) { 1181 if (dsym->s_bitfield) {
1182 declare_bit_field(dsym, &t, &tp); 1182 declare_bit_field(dsym, &t, &tp);
1183 } else if (t == FUNC) { 1183 } else if (t == FUNC) {
1184 /* function illegal in structure or union */ 1184 /* function illegal in structure or union */
1185 error(38); 1185 error(38);
1186 dsym->s_type = tp = incref(tp, t = PTR); 1186 dsym->s_type = tp = derive_type(tp, t = PTR);
1187 } 1187 }
1188 1188
1189 /* 1189 /*
1190 * bit-fields of length 0 are not warned about because length() 1190 * bit-fields of length 0 are not warned about because length()
1191 * does not return the length of the bit-field but the length 1191 * does not return the length of the bit-field but the length
1192 * of the type the bit-field is packed in (it's ok) 1192 * of the type the bit-field is packed in (it's ok)
1193 */ 1193 */
1194 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { 1194 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1195 if (t == ARRAY && dsym->s_type->t_dim == 0) { 1195 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1196 /* zero sized array in struct is a C99 extension: %s */ 1196 /* zero sized array in struct is a C99 extension: %s */
1197 c99ism(39, dsym->s_name); 1197 c99ism(39, dsym->s_name);
1198 } 1198 }
1199 } 1199 }
@@ -2406,32 +2406,32 @@ declare_argument(sym_t *sym, bool initfl @@ -2406,32 +2406,32 @@ declare_argument(sym_t *sym, bool initfl
2406 2406
2407 if (!sym->s_arg) { 2407 if (!sym->s_arg) {
2408 /* declared argument %s is missing */ 2408 /* declared argument %s is missing */
2409 error(53, sym->s_name); 2409 error(53, sym->s_name);
2410 sym->s_arg = true; 2410 sym->s_arg = true;
2411 } 2411 }
2412 2412
2413 if (initflg) { 2413 if (initflg) {
2414 /* cannot initialize parameter: %s */ 2414 /* cannot initialize parameter: %s */
2415 error(52, sym->s_name); 2415 error(52, sym->s_name);
2416 } 2416 }
2417 2417
2418 if ((t = sym->s_type->t_tspec) == ARRAY) { 2418 if ((t = sym->s_type->t_tspec) == ARRAY) {
2419 sym->s_type = incref(sym->s_type->t_subt, PTR); 2419 sym->s_type = derive_type(sym->s_type->t_subt, PTR);
2420 } else if (t == FUNC) { 2420 } else if (t == FUNC) {
2421 if (tflag) 2421 if (tflag)
2422 /* a function is declared as an argument: %s */ 2422 /* a function is declared as an argument: %s */
2423 warning(50, sym->s_name); 2423 warning(50, sym->s_name);
2424 sym->s_type = incref(sym->s_type, PTR); 2424 sym->s_type = derive_type(sym->s_type, PTR);
2425 } else if (t == FLOAT) { 2425 } else if (t == FLOAT) {
2426 if (tflag) 2426 if (tflag)
2427 sym->s_type = gettyp(DOUBLE); 2427 sym->s_type = gettyp(DOUBLE);
2428 } 2428 }
2429 2429
2430 if (dcs->d_inline) 2430 if (dcs->d_inline)
2431 /* argument declared inline: %s */ 2431 /* argument declared inline: %s */
2432 warning(269, sym->s_name); 2432 warning(269, sym->s_name);
2433 2433
2434 /* 2434 /*
2435 * Arguments must have complete types. lengths() prints the needed 2435 * Arguments must have complete types. lengths() prints the needed
2436 * error messages (null dimension is impossible because arrays are 2436 * error messages (null dimension is impossible because arrays are
2437 * converted to pointers). 2437 * converted to pointers).
@@ -3324,27 +3324,27 @@ to_int_constant(tnode_t *tn, bool requir @@ -3324,27 +3324,27 @@ to_int_constant(tnode_t *tn, bool requir
3324 3324
3325 if (tn == NULL) { 3325 if (tn == NULL) {
3326 i = 1; 3326 i = 1;
3327 goto done; 3327 goto done;
3328 } 3328 }
3329 3329
3330 /* 3330 /*
3331 * Abstract declarations are used inside expression. To free 3331 * Abstract declarations are used inside expression. To free
3332 * the memory would be a fatal error. 3332 * the memory would be a fatal error.
3333 * We don't free blocks that are inside casts because these 3333 * We don't free blocks that are inside casts because these
3334 * will be used later to match types. 3334 * will be used later to match types.
3335 */ 3335 */
3336 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT) 3336 if (tn->tn_op != CON && dcs->d_ctx != ABSTRACT)
3337 tfreeblk(); 3337 expr_free_all();
3338 3338
3339 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) { 3339 if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
3340 i = (int)v->v_ldbl; 3340 i = (int)v->v_ldbl;
3341 /* integral constant expression expected */ 3341 /* integral constant expression expected */
3342 error(55); 3342 error(55);
3343 } else { 3343 } else {
3344 i = (int)v->v_quad; 3344 i = (int)v->v_quad;
3345 if (is_uinteger(t)) { 3345 if (is_uinteger(t)) {
3346 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) { 3346 if ((uint64_t)v->v_quad > (uint64_t)TARG_INT_MAX) {
3347 /* integral constant too large */ 3347 /* integral constant too large */
3348 warning(56); 3348 warning(56);
3349 } 3349 }
3350 } else { 3350 } else {

cvs diff -r1.97 -r1.98 src/usr.bin/xlint/lint1/externs1.h (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/externs1.h 2021/04/02 09:52:36 1.97
+++ src/usr.bin/xlint/lint1/externs1.h 2021/04/02 10:13:03 1.98
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: externs1.h,v 1.97 2021/04/02 09:52:36 rillig Exp $ */ 1/* $NetBSD: externs1.h,v 1.98 2021/04/02 10:13:03 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -95,31 +95,31 @@ extern int yylex(void); @@ -95,31 +95,31 @@ extern int yylex(void);
95 */ 95 */
96extern const char *record_filename(const char *, size_t); 96extern const char *record_filename(const char *, size_t);
97extern int get_filename_id(const char *); 97extern int get_filename_id(const char *);
98extern void add_directory_replacement(char *); 98extern void add_directory_replacement(char *);
99extern const char *transform_filename(const char *, size_t); 99extern const char *transform_filename(const char *, size_t);
100 100
101extern void initmem(void); 101extern void initmem(void);
102 102
103extern void *getblk(size_t); 103extern void *getblk(size_t);
104extern void *getlblk(size_t, size_t); 104extern void *getlblk(size_t, size_t);
105extern void freeblk(void); 105extern void freeblk(void);
106extern void freelblk(int); 106extern void freelblk(int);
107 107
108extern void *tgetblk(size_t); 108extern void *expr_zalloc(size_t);
109extern tnode_t *expr_zalloc_tnode(void); 109extern tnode_t *expr_zalloc_tnode(void);
110extern void tfreeblk(void); 110extern void expr_free_all(void);
111extern struct memory_block *tsave(void); 111extern struct memory_block *expr_save_memory(void);
112extern void trestor(struct memory_block *); 112extern void expr_restore_memory(struct memory_block *);
113 113
114/* 114/*
115 * err.c 115 * err.c
116 */ 116 */
117extern int nerr; 117extern int nerr;
118extern int sytxerr; 118extern int sytxerr;
119extern const char *msgs[]; 119extern const char *msgs[];
120 120
121extern void msglist(void); 121extern void msglist(void);
122extern void error(int, ...); 122extern void error(int, ...);
123extern void warning(int, ...); 123extern void warning(int, ...);
124extern void message(int, ...); 124extern void message(int, ...);
125extern void gnuism(int, ...); 125extern void gnuism(int, ...);
@@ -186,29 +186,29 @@ extern void global_clean_up(void); @@ -186,29 +186,29 @@ extern void global_clean_up(void);
186extern sym_t *declare_1_abstract(sym_t *); 186extern sym_t *declare_1_abstract(sym_t *);
187extern void check_size(sym_t *); 187extern void check_size(sym_t *);
188extern void mark_as_set(sym_t *); 188extern void mark_as_set(sym_t *);
189extern void mark_as_used(sym_t *, bool, bool); 189extern void mark_as_used(sym_t *, bool, bool);
190extern void check_usage(dinfo_t *); 190extern void check_usage(dinfo_t *);
191extern void check_usage_sym(bool, sym_t *); 191extern void check_usage_sym(bool, sym_t *);
192extern void check_global_symbols(void); 192extern void check_global_symbols(void);
193extern void print_previous_declaration(int, const sym_t *); 193extern void print_previous_declaration(int, const sym_t *);
194extern int to_int_constant(tnode_t *, bool); 194extern int to_int_constant(tnode_t *, bool);
195 195
196/* 196/*
197 * tree.c 197 * tree.c
198 */ 198 */
199extern type_t *incref(type_t *, tspec_t); 199extern type_t *derive_type(type_t *, tspec_t);
200extern type_t *tincref(type_t *, tspec_t); 200extern type_t *expr_derive_type(type_t *, tspec_t);
201extern tnode_t *new_constant_node(type_t *, val_t *); 201extern tnode_t *expr_new_constant(type_t *, val_t *);
202extern tnode_t *new_name_node(sym_t *, int); 202extern tnode_t *new_name_node(sym_t *, int);
203extern tnode_t *new_string_node(strg_t *); 203extern tnode_t *new_string_node(strg_t *);
204extern sym_t *struct_or_union_member(tnode_t *, op_t, sym_t *); 204extern sym_t *struct_or_union_member(tnode_t *, op_t, sym_t *);
205extern tnode_t *build(op_t, tnode_t *, tnode_t *); 205extern tnode_t *build(op_t, tnode_t *, tnode_t *);
206extern tnode_t *cconv(tnode_t *); 206extern tnode_t *cconv(tnode_t *);
207extern bool is_typeok_bool_operand(const tnode_t *); 207extern bool is_typeok_bool_operand(const tnode_t *);
208extern bool typeok(op_t, int, const tnode_t *, const tnode_t *); 208extern bool typeok(op_t, int, const tnode_t *, const tnode_t *);
209extern tnode_t *promote(op_t, bool, tnode_t *); 209extern tnode_t *promote(op_t, bool, tnode_t *);
210extern tnode_t *convert(op_t, int, type_t *, tnode_t *); 210extern tnode_t *convert(op_t, int, type_t *, tnode_t *);
211extern void convert_constant(op_t, int, const type_t *, val_t *, val_t *); 211extern void convert_constant(op_t, int, const type_t *, val_t *, val_t *);
212extern tnode_t *build_sizeof(const type_t *); 212extern tnode_t *build_sizeof(const type_t *);
213extern tnode_t *build_offsetof(const type_t *, const sym_t *); 213extern tnode_t *build_offsetof(const type_t *, const sym_t *);
214extern tnode_t *build_alignof(const type_t *); 214extern tnode_t *build_alignof(const type_t *);

cvs diff -r1.98 -r1.99 src/usr.bin/xlint/lint1/func.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/func.c 2021/03/26 20:31:07 1.98
+++ src/usr.bin/xlint/lint1/func.c 2021/04/02 10:13:03 1.99
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: func.c,v 1.98 2021/03/26 20:31:07 rillig Exp $ */ 1/* $NetBSD: func.c,v 1.99 2021/04/02 10:13:03 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: func.c,v 1.98 2021/03/26 20:31:07 rillig Exp $"); 40__RCSID("$NetBSD: func.c,v 1.99 2021/04/02 10:13:03 rillig Exp $");
41#endif 41#endif
42 42
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
45 45
46#include "lint1.h" 46#include "lint1.h"
47#include "cgram.h" 47#include "cgram.h"
48 48
49/* 49/*
50 * Contains a pointer to the symbol table entry of the current function 50 * Contains a pointer to the symbol table entry of the current function
51 * definition. 51 * definition.
52 */ 52 */
53sym_t *funcsym; 53sym_t *funcsym;
@@ -531,27 +531,27 @@ check_case_label(tnode_t *tn, cstk_t *ci @@ -531,27 +531,27 @@ check_case_label(tnode_t *tn, cstk_t *ci
531} 531}
532 532
533void 533void
534case_label(tnode_t *tn) 534case_label(tnode_t *tn)
535{ 535{
536 cstk_t *ci; 536 cstk_t *ci;
537 537
538 /* find the innermost switch statement */ 538 /* find the innermost switch statement */
539 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding) 539 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
540 continue; 540 continue;
541 541
542 check_case_label(tn, ci); 542 check_case_label(tn, ci);
543 543
544 tfreeblk(); 544 expr_free_all();
545 545
546 set_reached(true); 546 set_reached(true);
547} 547}
548 548
549void 549void
550default_label(void) 550default_label(void)
551{ 551{
552 cstk_t *ci; 552 cstk_t *ci;
553 553
554 /* find the innermost switch statement */ 554 /* find the innermost switch statement */
555 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding) 555 for (ci = cstmt; ci != NULL && !ci->c_switch; ci = ci->c_surrounding)
556 continue; 556 continue;
557 557
@@ -873,27 +873,27 @@ for1(tnode_t *tn1, tnode_t *tn2, tnode_t @@ -873,27 +873,27 @@ for1(tnode_t *tn1, tnode_t *tn2, tnode_t
873 /* loop not entered at top */ 873 /* loop not entered at top */
874 warning(207); 874 warning(207);
875 set_reached(true); 875 set_reached(true);
876 } 876 }
877 877
878 begin_control_statement(CS_FOR); 878 begin_control_statement(CS_FOR);
879 cstmt->c_loop = true; 879 cstmt->c_loop = true;
880 880
881 /* 881 /*
882 * Store the tree memory for the reinitialization expression. 882 * Store the tree memory for the reinitialization expression.
883 * Also remember this expression itself. We must check it at 883 * Also remember this expression itself. We must check it at
884 * the end of the loop to get "used but not set" warnings correct. 884 * the end of the loop to get "used but not set" warnings correct.
885 */ 885 */
886 cstmt->c_for_expr3_mem = tsave(); 886 cstmt->c_for_expr3_mem = expr_save_memory();
887 cstmt->c_for_expr3 = tn3; 887 cstmt->c_for_expr3 = tn3;
888 cstmt->c_for_expr3_pos = curr_pos; 888 cstmt->c_for_expr3_pos = curr_pos;
889 cstmt->c_for_expr3_csrc_pos = csrc_pos; 889 cstmt->c_for_expr3_csrc_pos = csrc_pos;
890 890
891 if (tn1 != NULL) 891 if (tn1 != NULL)
892 expr(tn1, false, false, true, false); 892 expr(tn1, false, false, true, false);
893 893
894 if (tn2 != NULL) 894 if (tn2 != NULL)
895 tn2 = check_controlling_expression(tn2); 895 tn2 = check_controlling_expression(tn2);
896 if (tn2 != NULL) 896 if (tn2 != NULL)
897 expr(tn2, false, true, true, false); 897 expr(tn2, false, true, true, false);
898 898
899 cstmt->c_maybe_endless = tn2 == NULL || is_nonzero(tn2); 899 cstmt->c_maybe_endless = tn2 == NULL || is_nonzero(tn2);
@@ -910,42 +910,42 @@ for1(tnode_t *tn1, tnode_t *tn2, tnode_t @@ -910,42 +910,42 @@ for1(tnode_t *tn1, tnode_t *tn2, tnode_t
910void 910void
911for2(void) 911for2(void)
912{ 912{
913 pos_t cpos, cspos; 913 pos_t cpos, cspos;
914 tnode_t *tn3; 914 tnode_t *tn3;
915 915
916 if (cstmt->c_continue) 916 if (cstmt->c_continue)
917 set_reached(true); 917 set_reached(true);
918 918
919 cpos = curr_pos; 919 cpos = curr_pos;
920 cspos = csrc_pos; 920 cspos = csrc_pos;
921 921
922 /* Restore the tree memory for the reinitialization expression */ 922 /* Restore the tree memory for the reinitialization expression */
923 trestor(cstmt->c_for_expr3_mem); 923 expr_restore_memory(cstmt->c_for_expr3_mem);
924 tn3 = cstmt->c_for_expr3; 924 tn3 = cstmt->c_for_expr3;
925 curr_pos = cstmt->c_for_expr3_pos; 925 curr_pos = cstmt->c_for_expr3_pos;
926 csrc_pos = cstmt->c_for_expr3_csrc_pos; 926 csrc_pos = cstmt->c_for_expr3_csrc_pos;
927 927
928 /* simply "statement not reached" would be confusing */ 928 /* simply "statement not reached" would be confusing */
929 if (!reached && warn_about_unreachable) { 929 if (!reached && warn_about_unreachable) {
930 /* end-of-loop code not reached */ 930 /* end-of-loop code not reached */
931 warning(223); 931 warning(223);
932 set_reached(true); 932 set_reached(true);
933 } 933 }
934 934
935 if (tn3 != NULL) { 935 if (tn3 != NULL) {
936 expr(tn3, false, false, true, false); 936 expr(tn3, false, false, true, false);
937 } else { 937 } else {
938 tfreeblk(); 938 expr_free_all();
939 } 939 }
940 940
941 curr_pos = cpos; 941 curr_pos = cpos;
942 csrc_pos = cspos; 942 csrc_pos = cspos;
943 943
944 /* An endless loop without break will never terminate */ 944 /* An endless loop without break will never terminate */
945 /* TODO: What if the loop contains a 'return'? */ 945 /* TODO: What if the loop contains a 'return'? */
946 set_reached(cstmt->c_break || !cstmt->c_maybe_endless); 946 set_reached(cstmt->c_break || !cstmt->c_maybe_endless);
947 947
948 end_control_statement(CS_FOR); 948 end_control_statement(CS_FOR);
949} 949}
950 950
951/* 951/*
@@ -1024,42 +1024,42 @@ do_return(tnode_t *tn) @@ -1024,42 +1024,42 @@ do_return(tnode_t *tn)
1024 op_t op; 1024 op_t op;
1025 1025
1026 for (ci = cstmt; ci->c_surrounding != NULL; ci = ci->c_surrounding) 1026 for (ci = cstmt; ci->c_surrounding != NULL; ci = ci->c_surrounding)
1027 continue; 1027 continue;
1028 1028
1029 if (tn != NULL) 1029 if (tn != NULL)
1030 ci->c_had_return_value = true; 1030 ci->c_had_return_value = true;
1031 else 1031 else
1032 ci->c_had_return_noval = true; 1032 ci->c_had_return_noval = true;
1033 1033
1034 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) { 1034 if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) {
1035 /* void function %s cannot return value */ 1035 /* void function %s cannot return value */
1036 error(213, funcsym->s_name); 1036 error(213, funcsym->s_name);
1037 tfreeblk(); 1037 expr_free_all();
1038 tn = NULL; 1038 tn = NULL;
1039 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) { 1039 } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) {
1040 /* 1040 /*
1041 * Assume that the function has a return value only if it 1041 * Assume that the function has a return value only if it
1042 * is explicitly declared. 1042 * is explicitly declared.
1043 */ 1043 */
1044 if (!funcsym->s_return_type_implicit_int) 1044 if (!funcsym->s_return_type_implicit_int)
1045 /* function %s expects to return value */ 1045 /* function %s expects to return value */
1046 warning(214, funcsym->s_name); 1046 warning(214, funcsym->s_name);
1047 } 1047 }
1048 1048
1049 if (tn != NULL) { 1049 if (tn != NULL) {
1050 1050
1051 /* Create a temporary node for the left side */ 1051 /* Create a temporary node for the left side */
1052 ln = tgetblk(sizeof *ln); 1052 ln = expr_zalloc(sizeof *ln);
1053 ln->tn_op = NAME; 1053 ln->tn_op = NAME;
1054 ln->tn_type = tduptyp(funcsym->s_type->t_subt); 1054 ln->tn_type = tduptyp(funcsym->s_type->t_subt);
1055 ln->tn_type->t_const = false; 1055 ln->tn_type->t_const = false;
1056 ln->tn_lvalue = true; 1056 ln->tn_lvalue = true;
1057 ln->tn_sym = funcsym; /* better than nothing */ 1057 ln->tn_sym = funcsym; /* better than nothing */
1058 1058
1059 tn = build(RETURN, ln, tn); 1059 tn = build(RETURN, ln, tn);
1060 1060
1061 if (tn != NULL) { 1061 if (tn != NULL) {
1062 rn = tn->tn_right; 1062 rn = tn->tn_right;
1063 while ((op = rn->tn_op) == CVT || op == PLUS) 1063 while ((op = rn->tn_op) == CVT || op == PLUS)
1064 rn = rn->tn_left; 1064 rn = rn->tn_left;
1065 if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME && 1065 if (rn->tn_op == ADDR && rn->tn_left->tn_op == NAME &&

cvs diff -r1.187 -r1.188 src/usr.bin/xlint/lint1/init.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/init.c 2021/04/02 09:39:25 1.187
+++ src/usr.bin/xlint/lint1/init.c 2021/04/02 10:13:03 1.188
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: init.c,v 1.187 2021/04/02 09:39:25 rillig Exp $ */ 1/* $NetBSD: init.c,v 1.188 2021/04/02 10:13:03 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * Copyright (c) 2021 Roland Illig 5 * Copyright (c) 2021 Roland Illig
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
@@ -28,27 +28,27 @@ @@ -28,27 +28,27 @@
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(__RCSID) && !defined(lint) 40#if defined(__RCSID) && !defined(lint)
41__RCSID("$NetBSD: init.c,v 1.187 2021/04/02 09:39:25 rillig Exp $"); 41__RCSID("$NetBSD: init.c,v 1.188 2021/04/02 10:13:03 rillig Exp $");
42#endif 42#endif
43 43
44#include <stdlib.h> 44#include <stdlib.h>
45#include <string.h> 45#include <string.h>
46 46
47#include "lint1.h" 47#include "lint1.h"
48 48
49 49
50/* 50/*
51 * Initialization of global or local objects, like in: 51 * Initialization of global or local objects, like in:
52 * 52 *
53 * int number = 12345; 53 * int number = 12345;
54 * int number_with_braces = { 12345 }; 54 * int number_with_braces = { 12345 };
@@ -373,50 +373,50 @@ check_no_auto_aggregate(const sym_t *sym @@ -373,50 +373,50 @@ check_no_auto_aggregate(const sym_t *sym
373 /* no automatic aggregate initialization in trad. C */ 373 /* no automatic aggregate initialization in trad. C */
374 warning(188); 374 warning(188);
375 } 375 }
376} 376}
377 377
378static void 378static void
379check_init_expr(const type_t *tp, sym_t *sym, tnode_t *tn) 379check_init_expr(const type_t *tp, sym_t *sym, tnode_t *tn)
380{ 380{
381 tnode_t *ln; 381 tnode_t *ln;
382 tspec_t lt, rt; 382 tspec_t lt, rt;
383 struct memory_block *tmem; 383 struct memory_block *tmem;
384 384
385 /* Create a temporary node for the left side. */ 385 /* Create a temporary node for the left side. */
386 ln = tgetblk(sizeof *ln); 386 ln = expr_zalloc(sizeof *ln);
387 ln->tn_op = NAME; 387 ln->tn_op = NAME;
388 ln->tn_type = tduptyp(tp); 388 ln->tn_type = tduptyp(tp);
389 ln->tn_type->t_const = false; 389 ln->tn_type->t_const = false;
390 ln->tn_lvalue = true; 390 ln->tn_lvalue = true;
391 ln->tn_sym = sym; 391 ln->tn_sym = sym;
392 392
393 tn = cconv(tn); 393 tn = cconv(tn);
394 394
395 lt = ln->tn_type->t_tspec; 395 lt = ln->tn_type->t_tspec;
396 rt = tn->tn_type->t_tspec; 396 rt = tn->tn_type->t_tspec;
397 397
398 debug_step2("typeok '%s', '%s'", 398 debug_step2("typeok '%s', '%s'",
399 type_name(ln->tn_type), type_name(tn->tn_type)); 399 type_name(ln->tn_type), type_name(tn->tn_type));
400 if (!typeok(INIT, 0, ln, tn)) 400 if (!typeok(INIT, 0, ln, tn))
401 return; 401 return;
402 402
403 /* 403 /*
404 * Preserve the tree memory. This is necessary because otherwise 404 * Preserve the tree memory. This is necessary because otherwise
405 * expr() would free it. 405 * expr() would free it.
406 */ 406 */
407 tmem = tsave(); 407 tmem = expr_save_memory();
408 expr(tn, true, false, true, false); 408 expr(tn, true, false, true, false);
409 trestor(tmem); 409 expr_restore_memory(tmem);
410 410
411 check_bit_field_init(ln, lt, rt); 411 check_bit_field_init(ln, lt, rt);
412 412
413 /* 413 /*
414 * XXX: Is it correct to do this conversion _after_ the typeok above? 414 * XXX: Is it correct to do this conversion _after_ the typeok above?
415 */ 415 */
416 if (lt != rt || (tp->t_bitfield && tn->tn_op == CON)) 416 if (lt != rt || (tp->t_bitfield && tn->tn_op == CON))
417 tn = convert(INIT, 0, unconst_cast(tp), tn); 417 tn = convert(INIT, 0, unconst_cast(tp), tn);
418 418
419 check_non_constant_initializer(tn, sym); 419 check_non_constant_initializer(tn, sym);
420} 420}
421 421
422 422

cvs diff -r1.40 -r1.41 src/usr.bin/xlint/lint1/mem1.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/mem1.c 2021/04/02 09:52:36 1.40
+++ src/usr.bin/xlint/lint1/mem1.c 2021/04/02 10:13:03 1.41
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: mem1.c,v 1.40 2021/04/02 09:52:36 rillig Exp $ */ 1/* $NetBSD: mem1.c,v 1.41 2021/04/02 10:13:03 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,27 +27,27 @@ @@ -27,27 +27,27 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: mem1.c,v 1.40 2021/04/02 09:52:36 rillig Exp $"); 40__RCSID("$NetBSD: mem1.c,v 1.41 2021/04/02 10:13:03 rillig Exp $");
41#endif 41#endif
42 42
43#include <sys/types.h> 43#include <sys/types.h>
44#include <sys/param.h> 44#include <sys/param.h>
45#include <stdlib.h> 45#include <stdlib.h>
46#include <string.h> 46#include <string.h>
47#include <unistd.h> 47#include <unistd.h>
48 48
49#include "lint1.h" 49#include "lint1.h"
50 50
51/* 51/*
52 * Filenames allocated by record_filename are shared. 52 * Filenames allocated by record_filename are shared.
53 */ 53 */
@@ -326,67 +326,70 @@ void @@ -326,67 +326,70 @@ void
326freeblk(void) 326freeblk(void)
327{ 327{
328 328
329 freelblk(mem_block_level); 329 freelblk(mem_block_level);
330} 330}
331 331
332static memory_block *tmblk; 332static memory_block *tmblk;
333 333
334/* 334/*
335 * Return zero-initialized memory that is freed at the end of the current 335 * Return zero-initialized memory that is freed at the end of the current
336 * expression. 336 * expression.
337 */ 337 */
338void * 338void *
339tgetblk(size_t s) 339expr_zalloc(size_t s)
340{ 340{
341 341
342 return xgetblk(&tmblk, s); 342 return xgetblk(&tmblk, s);
343} 343}
344 344
345/* Return a freshly allocated tree node. */ 345/*
 346 * Return a freshly allocated tree node that is freed at the end of the
 347 * current expression.
 348 */
346tnode_t * 349tnode_t *
347expr_zalloc_tnode(void) 350expr_zalloc_tnode(void)
348{ 351{
349 tnode_t *tn = tgetblk(sizeof *tn); 352 tnode_t *tn = expr_zalloc(sizeof *tn);
350 tn->tn_from_system_header = in_system_header; 353 tn->tn_from_system_header = in_system_header;
351 return tn; 354 return tn;
352} 355}
353 356
354/* Free all memory which is allocated by the current expression. */ 357/* Free all memory which is allocated by the current expression. */
355void 358void
356tfreeblk(void) 359expr_free_all(void)
357{ 360{
358 361
359 xfreeblk(&tmblk); 362 xfreeblk(&tmblk);
360} 363}
361 364
362/* 365/*
363 * Save the memory which is used by the current expression. This memory 366 * Save the memory which is used by the current expression. This memory
364 * is not freed by the next tfreeblk() call. The pointer returned can be 367 * is not freed by the next expr_free_all() call. The pointer returned can be
365 * used to restore the memory. 368 * used to restore the memory.
366 */ 369 */
367memory_block * 370memory_block *
368tsave(void) 371expr_save_memory(void)
369{ 372{
370 memory_block *tmem; 373 memory_block *tmem;
371 374
372 tmem = tmblk; 375 tmem = tmblk;
373 tmblk = NULL; 376 tmblk = NULL;
374 return tmem; 377 return tmem;
375} 378}
376 379
377/* 380/*
378 * Free all memory used for the current expression and the memory used 381 * Free all memory used for the current expression and the memory used
379 * be a previous expression and saved by tsave(). The next call to 382 * be a previous expression and saved by expr_save_memory(). The next call to
380 * tfreeblk() frees the restored memory. 383 * expr_free_all() frees the restored memory.
381 */ 384 */
382void 385void
383trestor(memory_block *tmem) 386expr_restore_memory(memory_block *tmem)
384{ 387{
385 388
386 tfreeblk(); 389 expr_free_all();
387 if (tmblk != NULL) { 390 if (tmblk != NULL) {
388 free(tmblk->blk); 391 free(tmblk->blk);
389 free(tmblk); 392 free(tmblk);
390 } 393 }
391 tmblk = tmem; 394 tmblk = tmem;
392} 395}

cvs diff -r1.256 -r1.257 src/usr.bin/xlint/lint1/tree.c (expand / switch to unified diff)

--- src/usr.bin/xlint/lint1/tree.c 2021/04/02 09:52:36 1.256
+++ src/usr.bin/xlint/lint1/tree.c 2021/04/02 10:13:03 1.257
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: tree.c,v 1.256 2021/04/02 09:52:36 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.257 2021/04/02 10:13:03 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -27,40 +27,40 @@ @@ -27,40 +27,40 @@
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: tree.c,v 1.256 2021/04/02 09:52:36 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.257 2021/04/02 10:13:03 rillig Exp $");
41#endif 41#endif
42 42
43#include <float.h> 43#include <float.h>
44#include <limits.h> 44#include <limits.h>
45#include <math.h> 45#include <math.h>
46#include <signal.h> 46#include <signal.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <string.h> 48#include <string.h>
49 49
50#include "lint1.h" 50#include "lint1.h"
51#include "cgram.h" 51#include "cgram.h"
52 52
53static tnode_t *new_integer_constant_node(tspec_t, int64_t); 53static tnode_t *expr_new_integer_constant(tspec_t, int64_t);
54static void check_pointer_comparison(op_t, 54static void check_pointer_comparison(op_t,
55 const tnode_t *, const tnode_t *); 55 const tnode_t *, const tnode_t *);
56static bool check_assign_types_compatible(op_t, int, 56static bool check_assign_types_compatible(op_t, int,
57 const tnode_t *, const tnode_t *); 57 const tnode_t *, const tnode_t *);
58static void check_bad_enum_operation(op_t, 58static void check_bad_enum_operation(op_t,
59 const tnode_t *, const tnode_t *); 59 const tnode_t *, const tnode_t *);
60static void check_enum_type_mismatch(op_t, int, 60static void check_enum_type_mismatch(op_t, int,
61 const tnode_t *, const tnode_t *); 61 const tnode_t *, const tnode_t *);
62static void check_enum_int_mismatch(op_t, int, 62static void check_enum_int_mismatch(op_t, int,
63 const tnode_t *, const tnode_t *); 63 const tnode_t *, const tnode_t *);
64static tnode_t *new_tnode(op_t, type_t *, tnode_t *, tnode_t *); 64static tnode_t *new_tnode(op_t, type_t *, tnode_t *, tnode_t *);
65static void balance(op_t, tnode_t **, tnode_t **); 65static void balance(op_t, tnode_t **, tnode_t **);
66static void warn_incompatible_types(op_t, const type_t *, tspec_t, 66static void warn_incompatible_types(op_t, const type_t *, tspec_t,
@@ -134,83 +134,81 @@ debug_node(const tnode_t *tn, int indent @@ -134,83 +134,81 @@ debug_node(const tnode_t *tn, int indent
134 printf(", unknown value\n"); 134 printf(", unknown value\n");
135 else if (op == STRING) 135 else if (op == STRING)
136 printf(", length %zu\n", tn->tn_string->st_len); 136 printf(", length %zu\n", tn->tn_string->st_len);
137 else { 137 else {
138 printf("\n"); 138 printf("\n");
139 139
140 debug_node(tn->tn_left, indent + 1); 140 debug_node(tn->tn_left, indent + 1);
141 if (modtab[op].m_binary || tn->tn_right != NULL) 141 if (modtab[op].m_binary || tn->tn_right != NULL)
142 debug_node(tn->tn_right, indent + 1); 142 debug_node(tn->tn_right, indent + 1);
143 } 143 }
144} 144}
145#endif 145#endif
146 146
147/* 147/* Build 'pointer to tp', 'array of tp' or 'function returning tp'. */
148 * Increase degree of reference. 
149 * This is most often used to change type "T" in type "pointer to T". 
150 */ 
151type_t * 148type_t *
152incref(type_t *tp, tspec_t t) 149derive_type(type_t *tp, tspec_t t)
153{ 150{
154 type_t *tp2; 151 type_t *tp2;
155 152
156 tp2 = getblk(sizeof *tp2); 153 tp2 = getblk(sizeof *tp2);
157 tp2->t_tspec = t; 154 tp2->t_tspec = t;
158 tp2->t_subt = tp; 155 tp2->t_subt = tp;
159 return tp2; 156 return tp2;
160} 157}
161 158
162/* 159/*
163 * same for use in expressions 160 * Build 'pointer to tp', 'array of tp' or 'function returning tp'. The
 161 * memory is freed at the end of the current expression.
164 */ 162 */
165type_t * 163type_t *
166tincref(type_t *tp, tspec_t t) 164expr_derive_type(type_t *tp, tspec_t t)
167{ 165{
168 type_t *tp2; 166 type_t *tp2;
169 167
170 tp2 = tgetblk(sizeof *tp2); 168 tp2 = expr_zalloc(sizeof *tp2);
171 tp2->t_tspec = t; 169 tp2->t_tspec = t;
172 tp2->t_subt = tp; 170 tp2->t_subt = tp;
173 return tp2; 171 return tp2;
174} 172}
175 173
176/* 174/*
177 * Create a node for a constant. 175 * Create a node for a constant.
178 */ 176 */
179tnode_t * 177tnode_t *
180new_constant_node(type_t *tp, val_t *v) 178expr_new_constant(type_t *tp, val_t *v)
181{ 179{
182 tnode_t *n; 180 tnode_t *n;
183 181
184 n = expr_zalloc_tnode(); 182 n = expr_zalloc_tnode();
185 n->tn_op = CON; 183 n->tn_op = CON;
186 n->tn_type = tp; 184 n->tn_type = tp;
187 n->tn_val = tgetblk(sizeof *n->tn_val); 185 n->tn_val = expr_zalloc(sizeof *n->tn_val);
188 n->tn_val->v_tspec = tp->t_tspec; 186 n->tn_val->v_tspec = tp->t_tspec;
189 n->tn_val->v_ansiu = v->v_ansiu; 187 n->tn_val->v_ansiu = v->v_ansiu;
190 n->tn_val->v_u = v->v_u; 188 n->tn_val->v_u = v->v_u;
191 free(v); 189 free(v);
192 return n; 190 return n;
193} 191}
194 192
195static tnode_t * 193static tnode_t *
196new_integer_constant_node(tspec_t t, int64_t q) 194expr_new_integer_constant(tspec_t t, int64_t q)
197{ 195{
198 tnode_t *n; 196 tnode_t *n;
199 197
200 n = expr_zalloc_tnode(); 198 n = expr_zalloc_tnode();
201 n->tn_op = CON; 199 n->tn_op = CON;
202 n->tn_type = gettyp(t); 200 n->tn_type = gettyp(t);
203 n->tn_val = tgetblk(sizeof *n->tn_val); 201 n->tn_val = expr_zalloc(sizeof *n->tn_val);
204 n->tn_val->v_tspec = t; 202 n->tn_val->v_tspec = t;
205 n->tn_val->v_quad = q; 203 n->tn_val->v_quad = q;
206 return n; 204 return n;
207} 205}
208 206
209static void 207static void
210fallback_symbol(sym_t *sym) 208fallback_symbol(sym_t *sym)
211{ 209{
212 210
213 if (Tflag && strcmp(sym->s_name, "__lint_false") == 0) { 211 if (Tflag && strcmp(sym->s_name, "__lint_false") == 0) {
214 sym->s_scl = CTCONST; /* close enough */ 212 sym->s_scl = CTCONST; /* close enough */
215 sym->s_type = gettyp(BOOL); 213 sym->s_type = gettyp(BOOL);
216 sym->s_value.v_tspec = BOOL; 214 sym->s_value.v_tspec = BOOL;
@@ -222,36 +220,36 @@ fallback_symbol(sym_t *sym) @@ -222,36 +220,36 @@ fallback_symbol(sym_t *sym)
222 if (Tflag && strcmp(sym->s_name, "__lint_true") == 0) { 220 if (Tflag && strcmp(sym->s_name, "__lint_true") == 0) {
223 sym->s_scl = CTCONST; /* close enough */ 221 sym->s_scl = CTCONST; /* close enough */
224 sym->s_type = gettyp(BOOL); 222 sym->s_type = gettyp(BOOL);
225 sym->s_value.v_tspec = BOOL; 223 sym->s_value.v_tspec = BOOL;
226 sym->s_value.v_ansiu = false; 224 sym->s_value.v_ansiu = false;
227 sym->s_value.v_quad = 1; 225 sym->s_value.v_quad = 1;
228 return; 226 return;
229 } 227 }
230 228
231 if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 || 229 if (block_level > 0 && (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
232 strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) { 230 strcmp(sym->s_name, "__PRETTY_FUNCTION__") == 0)) {
233 /* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */ 231 /* __FUNCTION__/__PRETTY_FUNCTION__ is a GCC extension */
234 gnuism(316); 232 gnuism(316);
235 sym->s_type = incref(gettyp(CHAR), PTR); 233 sym->s_type = derive_type(gettyp(CHAR), PTR);
236 sym->s_type->t_const = true; 234 sym->s_type->t_const = true;
237 return; 235 return;
238 } 236 }
239 237
240 if (block_level > 0 && strcmp(sym->s_name, "__func__") == 0) { 238 if (block_level > 0 && strcmp(sym->s_name, "__func__") == 0) {
241 if (!Sflag) 239 if (!Sflag)
242 /* __func__ is a C9X feature */ 240 /* __func__ is a C9X feature */
243 warning(317); 241 warning(317);
244 sym->s_type = incref(gettyp(CHAR), PTR); 242 sym->s_type = derive_type(gettyp(CHAR), PTR);
245 sym->s_type->t_const = true; 243 sym->s_type->t_const = true;
246 return; 244 return;
247 } 245 }
248 246
249 /* '%s' undefined */ 247 /* '%s' undefined */
250 error(99, sym->s_name); 248 error(99, sym->s_name);
251} 249}
252 250
253/* 251/*
254 * Create a node for a name (symbol table entry). 252 * Create a node for a name (symbol table entry).
255 * follow_token is the token which follows the name. 253 * follow_token is the token which follows the name.
256 */ 254 */
257tnode_t * 255tnode_t *
@@ -261,76 +259,76 @@ new_name_node(sym_t *sym, int follow_tok @@ -261,76 +259,76 @@ new_name_node(sym_t *sym, int follow_tok
261 259
262 if (sym->s_scl == NOSCL) { 260 if (sym->s_scl == NOSCL) {
263 sym->s_scl = EXTERN; 261 sym->s_scl = EXTERN;
264 sym->s_def = DECL; 262 sym->s_def = DECL;
265 if (follow_token == T_LPAREN) { 263 if (follow_token == T_LPAREN) {
266 if (sflag) { 264 if (sflag) {
267 /* function implicitly declared to ... */ 265 /* function implicitly declared to ... */
268 warning(215); 266 warning(215);
269 } 267 }
270 /* 268 /*
271 * XXX if tflag is set the symbol should be 269 * XXX if tflag is set the symbol should be
272 * exported to level 0 270 * exported to level 0
273 */ 271 */
274 sym->s_type = incref(sym->s_type, FUNC); 272 sym->s_type = derive_type(sym->s_type, FUNC);
275 } else { 273 } else {
276 fallback_symbol(sym); 274 fallback_symbol(sym);
277 } 275 }
278 } 276 }
279 277
280 lint_assert(sym->s_kind == FVFT || sym->s_kind == FMEMBER); 278 lint_assert(sym->s_kind == FVFT || sym->s_kind == FMEMBER);
281 279
282 n = expr_zalloc_tnode(); 280 n = expr_zalloc_tnode();
283 n->tn_type = sym->s_type; 281 n->tn_type = sym->s_type;
284 if (sym->s_scl != CTCONST) { 282 if (sym->s_scl != CTCONST) {
285 n->tn_op = NAME; 283 n->tn_op = NAME;
286 n->tn_sym = sym; 284 n->tn_sym = sym;
287 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC) 285 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
288 n->tn_lvalue = true; 286 n->tn_lvalue = true;
289 } else { 287 } else {
290 n->tn_op = CON; 288 n->tn_op = CON;
291 n->tn_val = tgetblk(sizeof *n->tn_val); 289 n->tn_val = expr_zalloc(sizeof *n->tn_val);
292 *n->tn_val = sym->s_value; 290 *n->tn_val = sym->s_value;
293 } 291 }
294 292
295 return n; 293 return n;
296} 294}
297 295
298tnode_t * 296tnode_t *
299new_string_node(strg_t *strg) 297new_string_node(strg_t *strg)
300{ 298{
301 size_t len; 299 size_t len;
302 tnode_t *n; 300 tnode_t *n;
303 301
304 len = strg->st_len; 302 len = strg->st_len;
305 303
306 n = expr_zalloc_tnode(); 304 n = expr_zalloc_tnode();
307 305
308 n->tn_op = STRING; 306 n->tn_op = STRING;
309 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY); 307 n->tn_type = expr_derive_type(gettyp(strg->st_tspec), ARRAY);
310 n->tn_type->t_dim = len + 1; 308 n->tn_type->t_dim = len + 1;
311 n->tn_lvalue = true; 309 n->tn_lvalue = true;
312 310
313 n->tn_string = tgetblk(sizeof *n->tn_string); 311 n->tn_string = expr_zalloc(sizeof *n->tn_string);
314 n->tn_string->st_tspec = strg->st_tspec; 312 n->tn_string->st_tspec = strg->st_tspec;
315 n->tn_string->st_len = len; 313 n->tn_string->st_len = len;
316 314
317 if (strg->st_tspec == CHAR) { 315 if (strg->st_tspec == CHAR) {
318 n->tn_string->st_cp = tgetblk(len + 1); 316 n->tn_string->st_cp = expr_zalloc(len + 1);
319 (void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1); 317 (void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1);
320 free(strg->st_cp); 318 free(strg->st_cp);
321 } else { 319 } else {
322 size_t size = (len + 1) * sizeof *n->tn_string->st_wcp; 320 size_t size = (len + 1) * sizeof *n->tn_string->st_wcp;
323 n->tn_string->st_wcp = tgetblk(size); 321 n->tn_string->st_wcp = expr_zalloc(size);
324 (void)memcpy(n->tn_string->st_wcp, strg->st_wcp, size); 322 (void)memcpy(n->tn_string->st_wcp, strg->st_wcp, size);
325 free(strg->st_wcp); 323 free(strg->st_wcp);
326 } 324 }
327 free(strg); 325 free(strg);
328 326
329 return n; 327 return n;
330} 328}
331 329
332/* 330/*
333 * Returns a symbol which has the same name as the msym argument and is a 331 * Returns a symbol which has the same name as the msym argument and is a
334 * member of the struct or union specified by the tn argument. 332 * member of the struct or union specified by the tn argument.
335 */ 333 */
336sym_t * 334sym_t *
@@ -342,28 +340,29 @@ struct_or_union_member(tnode_t *tn, op_t @@ -342,28 +340,29 @@ struct_or_union_member(tnode_t *tn, op_t
342 bool eq; 340 bool eq;
343 tspec_t t; 341 tspec_t t;
344 342
345 /* 343 /*
346 * Remove the member if it was unknown until now, which means 344 * Remove the member if it was unknown until now, which means
347 * that no defined struct or union has a member with the same name. 345 * that no defined struct or union has a member with the same name.
348 */ 346 */
349 if (msym->s_scl == NOSCL) { 347 if (msym->s_scl == NOSCL) {
350 /* type '%s' does not have member '%s' */ 348 /* type '%s' does not have member '%s' */
351 error(101, type_name(msym->s_type), msym->s_name); 349 error(101, type_name(msym->s_type), msym->s_name);
352 rmsym(msym); 350 rmsym(msym);
353 msym->s_kind = FMEMBER; 351 msym->s_kind = FMEMBER;
354 msym->s_scl = MOS; 352 msym->s_scl = MOS;
355 msym->s_styp = tgetblk(sizeof *msym->s_styp); 353 msym->s_styp = expr_zalloc(sizeof *msym->s_styp);
356 msym->s_styp->sou_tag = tgetblk(sizeof *msym->s_styp->sou_tag); 354 msym->s_styp->sou_tag = expr_zalloc(
 355 sizeof *msym->s_styp->sou_tag);
357 msym->s_styp->sou_tag->s_name = unnamed; 356 msym->s_styp->sou_tag->s_name = unnamed;
358 msym->s_value.v_tspec = INT; 357 msym->s_value.v_tspec = INT;
359 return msym; 358 return msym;
360 } 359 }
361 360
362 /* Set str to the tag of which msym is expected to be a member. */ 361 /* Set str to the tag of which msym is expected to be a member. */
363 str = NULL; 362 str = NULL;
364 t = (tp = tn->tn_type)->t_tspec; 363 t = (tp = tn->tn_type)->t_tspec;
365 if (op == POINT) { 364 if (op == POINT) {
366 if (t == STRUCT || t == UNION) 365 if (t == STRUCT || t == UNION)
367 str = tp->t_str; 366 str = tp->t_str;
368 } else if (op == ARROW && t == PTR) { 367 } else if (op == ARROW && t == PTR) {
369 t = (tp = tp->t_subt)->t_tspec; 368 t = (tp = tp->t_subt)->t_tspec;
@@ -688,28 +687,28 @@ cconv(tnode_t *tn) @@ -688,28 +687,28 @@ cconv(tnode_t *tn)
688{ 687{
689 type_t *tp; 688 type_t *tp;
690 689
691 /* 690 /*
692 * Array-lvalue (array of type T) is converted into rvalue 691 * Array-lvalue (array of type T) is converted into rvalue
693 * (pointer to type T) 692 * (pointer to type T)
694 */ 693 */
695 if (tn->tn_type->t_tspec == ARRAY) { 694 if (tn->tn_type->t_tspec == ARRAY) {
696 if (!tn->tn_lvalue) { 695 if (!tn->tn_lvalue) {
697 /* XXX print correct operator */ 696 /* XXX print correct operator */
698 /* %soperand of '%s' must be lvalue */ 697 /* %soperand of '%s' must be lvalue */
699 gnuism(114, "", modtab[ADDR].m_name); 698 gnuism(114, "", modtab[ADDR].m_name);
700 } 699 }
701 tn = new_tnode(ADDR, tincref(tn->tn_type->t_subt, PTR), 700 tn = new_tnode(ADDR,
702 tn, NULL); 701 expr_derive_type(tn->tn_type->t_subt, PTR), tn, NULL);
703 } 702 }
704 703
705 /* 704 /*
706 * Expression of type function (function with return value of type T) 705 * Expression of type function (function with return value of type T)
707 * in rvalue-expression (pointer to function with return value 706 * in rvalue-expression (pointer to function with return value
708 * of type T) 707 * of type T)
709 */ 708 */
710 if (tn->tn_type->t_tspec == FUNC) 709 if (tn->tn_type->t_tspec == FUNC)
711 tn = build_address(tn, true); 710 tn = build_address(tn, true);
712 711
713 /* lvalue to rvalue */ 712 /* lvalue to rvalue */
714 if (tn->tn_lvalue) { 713 if (tn->tn_lvalue) {
715 tp = tduptyp(tn->tn_type); 714 tp = tduptyp(tn->tn_type);
@@ -2056,27 +2055,27 @@ convert(op_t op, int arg, type_t *tp, tn @@ -2056,27 +2055,27 @@ convert(op_t op, int arg, type_t *tp, tn
2056 check_pointer_conversion(op, tn, tp); 2055 check_pointer_conversion(op, tn, tp);
2057 } 2056 }
2058 2057
2059 ntn = expr_zalloc_tnode(); 2058 ntn = expr_zalloc_tnode();
2060 ntn->tn_op = CVT; 2059 ntn->tn_op = CVT;
2061 ntn->tn_type = tp; 2060 ntn->tn_type = tp;
2062 ntn->tn_cast = op == CVT; 2061 ntn->tn_cast = op == CVT;
2063 ntn->tn_from_system_header |= tn->tn_from_system_header; 2062 ntn->tn_from_system_header |= tn->tn_from_system_header;
2064 ntn->tn_right = NULL; 2063 ntn->tn_right = NULL;
2065 if (tn->tn_op != CON || nt == VOID) { 2064 if (tn->tn_op != CON || nt == VOID) {
2066 ntn->tn_left = tn; 2065 ntn->tn_left = tn;
2067 } else { 2066 } else {
2068 ntn->tn_op = CON; 2067 ntn->tn_op = CON;
2069 ntn->tn_val = tgetblk(sizeof *ntn->tn_val); 2068 ntn->tn_val = expr_zalloc(sizeof *ntn->tn_val);
2070 convert_constant(op, arg, ntn->tn_type, ntn->tn_val, 2069 convert_constant(op, arg, ntn->tn_type, ntn->tn_val,
2071 tn->tn_val); 2070 tn->tn_val);
2072 } 2071 }
2073 2072
2074 return ntn; 2073 return ntn;
2075} 2074}
2076 2075
2077/* 2076/*
2078 * Print a warning if a prototype causes a type conversion that is 2077 * Print a warning if a prototype causes a type conversion that is
2079 * different from what would happen to the same argument in the 2078 * different from what would happen to the same argument in the
2080 * absence of a prototype. 2079 * absence of a prototype.
2081 * 2080 *
2082 * Errors/warnings about illegal type combinations are already printed 2081 * Errors/warnings about illegal type combinations are already printed
@@ -2674,33 +2673,33 @@ build_struct_access(op_t op, tnode_t *ln @@ -2674,33 +2673,33 @@ build_struct_access(op_t op, tnode_t *ln
2674 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU); 2673 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU);
2675 2674
2676 /* 2675 /*
2677 * Remember if the left operand is an lvalue (structure members 2676 * Remember if the left operand is an lvalue (structure members
2678 * are lvalues if and only if the structure itself is an lvalue). 2677 * are lvalues if and only if the structure itself is an lvalue).
2679 */ 2678 */
2680 nolval = op == POINT && !ln->tn_lvalue; 2679 nolval = op == POINT && !ln->tn_lvalue;
2681 2680
2682 if (op == POINT) { 2681 if (op == POINT) {
2683 ln = build_address(ln, true); 2682 ln = build_address(ln, true);
2684 } else if (ln->tn_type->t_tspec != PTR) { 2683 } else if (ln->tn_type->t_tspec != PTR) {
2685 lint_assert(tflag); 2684 lint_assert(tflag);
2686 lint_assert(is_integer(ln->tn_type->t_tspec)); 2685 lint_assert(is_integer(ln->tn_type->t_tspec));
2687 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln); 2686 ln = convert(NOOP, 0, expr_derive_type(gettyp(VOID), PTR), ln);
2688 } 2687 }
2689 2688
2690 ctn = new_integer_constant_node(PTRDIFF_TSPEC, 2689 ctn = expr_new_integer_constant(PTRDIFF_TSPEC,
2691 rn->tn_sym->s_value.v_quad / CHAR_SIZE); 2690 rn->tn_sym->s_value.v_quad / CHAR_SIZE);
2692 2691
2693 ntn = new_tnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn); 2692 ntn = new_tnode(PLUS, expr_derive_type(rn->tn_type, PTR), ln, ctn);
2694 if (ln->tn_op == CON) 2693 if (ln->tn_op == CON)
2695 ntn = fold(ntn); 2694 ntn = fold(ntn);
2696 2695
2697 if (rn->tn_type->t_bitfield) { 2696 if (rn->tn_type->t_bitfield) {
2698 ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL); 2697 ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2699 } else { 2698 } else {
2700 ntn = new_tnode(INDIR, ntn->tn_type->t_subt, ntn, NULL); 2699 ntn = new_tnode(INDIR, ntn->tn_type->t_subt, ntn, NULL);
2701 } 2700 }
2702 2701
2703 if (nolval) 2702 if (nolval)
2704 ntn->tn_lvalue = false; 2703 ntn->tn_lvalue = false;
2705 2704
2706 return ntn; 2705 return ntn;
@@ -2709,53 +2708,55 @@ build_struct_access(op_t op, tnode_t *ln @@ -2709,53 +2708,55 @@ build_struct_access(op_t op, tnode_t *ln
2709/* 2708/*
2710 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF. 2709 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2711 */ 2710 */
2712static tnode_t * 2711static tnode_t *
2713build_prepost_incdec(op_t op, tnode_t *ln) 2712build_prepost_incdec(op_t op, tnode_t *ln)
2714{ 2713{
2715 tnode_t *cn, *ntn; 2714 tnode_t *cn, *ntn;
2716 2715
2717 lint_assert(ln != NULL); 2716 lint_assert(ln != NULL);
2718 2717
2719 if (ln->tn_type->t_tspec == PTR) { 2718 if (ln->tn_type->t_tspec == PTR) {
2720 cn = plength(ln->tn_type); 2719 cn = plength(ln->tn_type);
2721 } else { 2720 } else {
2722 cn = new_integer_constant_node(INT, (int64_t)1); 2721 cn = expr_new_integer_constant(INT, (int64_t)1);
2723 } 2722 }
2724 ntn = new_tnode(op, ln->tn_type, ln, cn); 2723 ntn = new_tnode(op, ln->tn_type, ln, cn);
2725 2724
2726 return ntn; 2725 return ntn;
2727} 2726}
2728 2727
2729/* 2728/*
2730 * Create a node for REAL, IMAG 2729 * Create a node for REAL, IMAG
2731 */ 2730 */
2732static tnode_t * 2731static tnode_t *
2733build_real_imag(op_t op, tnode_t *ln) 2732build_real_imag(op_t op, tnode_t *ln)
2734{ 2733{
2735 tnode_t *cn, *ntn; 2734 tnode_t *cn, *ntn;
2736 2735
2737 lint_assert(ln != NULL); 2736 lint_assert(ln != NULL);
2738 2737
2739 switch (ln->tn_type->t_tspec) { 2738 switch (ln->tn_type->t_tspec) {
2740 case LCOMPLEX: 2739 case LCOMPLEX:
2741 /* XXX: integer and LDOUBLE don't match. */ 2740 /* XXX: integer and LDOUBLE don't match. */
2742 cn = new_integer_constant_node(LDOUBLE, (int64_t)1); 2741 cn = expr_new_integer_constant(LDOUBLE, (int64_t)1);
2743 break; 2742 break;
2744 case DCOMPLEX: 2743 case DCOMPLEX:
2745 cn = new_integer_constant_node(DOUBLE, (int64_t)1); 2744 /* XXX: integer and DOUBLE don't match. */
 2745 cn = expr_new_integer_constant(DOUBLE, (int64_t)1);
2746 break; 2746 break;
2747 case FCOMPLEX: 2747 case FCOMPLEX:
2748 cn = new_integer_constant_node(FLOAT, (int64_t)1); 2748 /* XXX: integer and FLOAT don't match. */
 2749 cn = expr_new_integer_constant(FLOAT, (int64_t)1);
2749 break; 2750 break;
2750 default: 2751 default:
2751 /* __%s__ is illegal for type %s */ 2752 /* __%s__ is illegal for type %s */
2752 error(276, op == REAL ? "real" : "imag", 2753 error(276, op == REAL ? "real" : "imag",
2753 type_name(ln->tn_type)); 2754 type_name(ln->tn_type));
2754 return NULL; 2755 return NULL;
2755 } 2756 }
2756 ntn = new_tnode(op, cn->tn_type, ln, cn); 2757 ntn = new_tnode(op, cn->tn_type, ln, cn);
2757 ntn->tn_lvalue = true; 2758 ntn->tn_lvalue = true;
2758 2759
2759 return ntn; 2760 return ntn;
2760} 2761}
2761/* 2762/*
@@ -2770,27 +2771,27 @@ build_address(tnode_t *tn, bool noign) @@ -2770,27 +2771,27 @@ build_address(tnode_t *tn, bool noign)
2770 if (tflag) 2771 if (tflag)
2771 /* '&' before array or function: ignored */ 2772 /* '&' before array or function: ignored */
2772 warning(127); 2773 warning(127);
2773 return tn; 2774 return tn;
2774 } 2775 }
2775 2776
2776 /* eliminate &* */ 2777 /* eliminate &* */
2777 if (tn->tn_op == INDIR && 2778 if (tn->tn_op == INDIR &&
2778 tn->tn_left->tn_type->t_tspec == PTR && 2779 tn->tn_left->tn_type->t_tspec == PTR &&
2779 tn->tn_left->tn_type->t_subt == tn->tn_type) { 2780 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2780 return tn->tn_left; 2781 return tn->tn_left;
2781 } 2782 }
2782 2783
2783 return new_tnode(ADDR, tincref(tn->tn_type, PTR), tn, NULL); 2784 return new_tnode(ADDR, expr_derive_type(tn->tn_type, PTR), tn, NULL);
2784} 2785}
2785 2786
2786/* 2787/*
2787 * Create a node for operators PLUS and MINUS. 2788 * Create a node for operators PLUS and MINUS.
2788 */ 2789 */
2789static tnode_t * 2790static tnode_t *
2790build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn) 2791build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn)
2791{ 2792{
2792 tnode_t *ntn, *ctn; 2793 tnode_t *ntn, *ctn;
2793 type_t *tp; 2794 type_t *tp;
2794 2795
2795 /* If pointer and integer, then pointer to the lhs. */ 2796 /* If pointer and integer, then pointer to the lhs. */
2796 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) { 2797 if (rn->tn_type->t_tspec == PTR && is_integer(ln->tn_type->t_tspec)) {
@@ -3027,27 +3028,27 @@ plength(type_t *tp) @@ -3027,27 +3028,27 @@ plength(type_t *tp)
3027 lint_assert(elsz != -1); 3028 lint_assert(elsz != -1);
3028 } 3029 }
3029 break; 3030 break;
3030 } 3031 }
3031 3032
3032 if (elem == 0 && elsz != 0) { 3033 if (elem == 0 && elsz != 0) {
3033 /* cannot do pointer arithmetic on operand of unknown size */ 3034 /* cannot do pointer arithmetic on operand of unknown size */
3034 error(136); 3035 error(136);
3035 } 3036 }
3036 3037
3037 if (elsz == 0) 3038 if (elsz == 0)
3038 elsz = CHAR_SIZE; 3039 elsz = CHAR_SIZE;
3039 3040
3040 return new_integer_constant_node(PTRDIFF_TSPEC, 3041 return expr_new_integer_constant(PTRDIFF_TSPEC,
3041 (int64_t)(elem * elsz / CHAR_SIZE)); 3042 (int64_t)(elem * elsz / CHAR_SIZE));
3042} 3043}
3043 3044
3044/* 3045/*
3045 * XXX 3046 * XXX
3046 * Note: There appear to be a number of bugs in detecting overflow in 3047 * Note: There appear to be a number of bugs in detecting overflow in
3047 * this function. An audit and a set of proper regression tests are needed. 3048 * this function. An audit and a set of proper regression tests are needed.
3048 * --Perry Metzger, Nov. 16, 2001 3049 * --Perry Metzger, Nov. 16, 2001
3049 */ 3050 */
3050/* 3051/*
3051 * Do only as much as necessary to compute constant expressions. 3052 * Do only as much as necessary to compute constant expressions.
3052 * Called only if the operator allows folding and all operands are constants. 3053 * Called only if the operator allows folding and all operands are constants.
3053 */ 3054 */
@@ -3177,27 +3178,27 @@ fold(tnode_t *tn) @@ -3177,27 +3178,27 @@ fold(tnode_t *tn)
3177 lint_assert(/*CONSTCOND*/false); 3178 lint_assert(/*CONSTCOND*/false);
3178 } 3179 }
3179 3180
3180 /* XXX does not work for quads. */ 3181 /* XXX does not work for quads. */
3181 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 && 3182 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 &&
3182 (q & ~mask) != 0)) { 3183 (q & ~mask) != 0)) {
3183 if (hflag) 3184 if (hflag)
3184 /* integer overflow detected, op %s */ 3185 /* integer overflow detected, op %s */
3185 warning(141, modtab[tn->tn_op].m_name); 3186 warning(141, modtab[tn->tn_op].m_name);
3186 } 3187 }
3187 3188
3188 v->v_quad = xsign(q, t, -1); 3189 v->v_quad = xsign(q, t, -1);
3189 3190
3190 cn = new_constant_node(tn->tn_type, v); 3191 cn = expr_new_constant(tn->tn_type, v);
3191 if (tn->tn_left->tn_system_dependent) 3192 if (tn->tn_left->tn_system_dependent)
3192 cn->tn_system_dependent = true; 3193 cn->tn_system_dependent = true;
3193 if (modtab[tn->tn_op].m_binary && tn->tn_right->tn_system_dependent) 3194 if (modtab[tn->tn_op].m_binary && tn->tn_right->tn_system_dependent)
3194 cn->tn_system_dependent = true; 3195 cn->tn_system_dependent = true;
3195 3196
3196 return cn; 3197 return cn;
3197} 3198}
3198 3199
3199/* 3200/*
3200 * Fold constant nodes, as much as is needed for comparing the value with 0 3201 * Fold constant nodes, as much as is needed for comparing the value with 0
3201 * (test context, for controlling expressions). 3202 * (test context, for controlling expressions).
3202 */ 3203 */
3203static tnode_t * 3204static tnode_t *
@@ -3220,27 +3221,27 @@ fold_test(tnode_t *tn) @@ -3220,27 +3221,27 @@ fold_test(tnode_t *tn)
3220 warning(239); 3221 warning(239);
3221 v->v_quad = !l ? 1 : 0; 3222 v->v_quad = !l ? 1 : 0;
3222 break; 3223 break;
3223 case LOGAND: 3224 case LOGAND:
3224 v->v_quad = l && r ? 1 : 0; 3225 v->v_quad = l && r ? 1 : 0;
3225 break; 3226 break;
3226 case LOGOR: 3227 case LOGOR:
3227 v->v_quad = l || r ? 1 : 0; 3228 v->v_quad = l || r ? 1 : 0;
3228 break; 3229 break;
3229 default: 3230 default:
3230 lint_assert(/*CONSTCOND*/false); 3231 lint_assert(/*CONSTCOND*/false);
3231 } 3232 }
3232 3233
3233 return new_constant_node(tn->tn_type, v); 3234 return expr_new_constant(tn->tn_type, v);
3234} 3235}
3235 3236
3236/* 3237/*
3237 * Fold constant nodes having operands with floating point type. 3238 * Fold constant nodes having operands with floating point type.
3238 */ 3239 */
3239static tnode_t * 3240static tnode_t *
3240fold_float(tnode_t *tn) 3241fold_float(tnode_t *tn)
3241{ 3242{
3242 val_t *v; 3243 val_t *v;
3243 tspec_t t; 3244 tspec_t t;
3244 ldbl_t l, r = 0; 3245 ldbl_t l, r = 0;
3245 3246
3246 fpe = 0; 3247 fpe = 0;
@@ -3317,56 +3318,56 @@ fold_float(tnode_t *tn) @@ -3317,56 +3318,56 @@ fold_float(tnode_t *tn)
3317 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) { 3318 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
3318 /* floating point overflow detected, op %s */ 3319 /* floating point overflow detected, op %s */
3319 warning(142, modtab[tn->tn_op].m_name); 3320 warning(142, modtab[tn->tn_op].m_name);
3320 if (t == FLOAT) { 3321 if (t == FLOAT) {
3321 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX; 3322 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
3322 } else if (t == DOUBLE) { 3323 } else if (t == DOUBLE) {
3323 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX; 3324 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
3324 } else { 3325 } else {
3325 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX; 3326 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
3326 } 3327 }
3327 fpe = 0; 3328 fpe = 0;
3328 } 3329 }
3329 3330
3330 return new_constant_node(tn->tn_type, v); 3331 return expr_new_constant(tn->tn_type, v);
3331} 3332}
3332 3333
3333 3334
3334/* 3335/*
3335 * Create a constant node for sizeof. 3336 * Create a constant node for sizeof.
3336 */ 3337 */
3337tnode_t * 3338tnode_t *
3338build_sizeof(const type_t *tp) 3339build_sizeof(const type_t *tp)
3339{ 3340{
3340 int64_t size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE; 3341 int64_t size_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3341 tnode_t *tn = new_integer_constant_node(SIZEOF_TSPEC, size_in_bytes); 3342 tnode_t *tn = expr_new_integer_constant(SIZEOF_TSPEC, size_in_bytes);
3342 tn->tn_system_dependent = true; 3343 tn->tn_system_dependent = true;
3343 return tn; 3344 return tn;
3344} 3345}
3345 3346
3346/* 3347/*
3347 * Create a constant node for offsetof. 3348 * Create a constant node for offsetof.
3348 */ 3349 */
3349tnode_t * 3350tnode_t *
3350build_offsetof(const type_t *tp, const sym_t *sym) 3351build_offsetof(const type_t *tp, const sym_t *sym)
3351{ 3352{
3352 tspec_t t = tp->t_tspec; 3353 tspec_t t = tp->t_tspec;
3353 if (t != STRUCT && t != UNION) 3354 if (t != STRUCT && t != UNION)
3354 /* unacceptable operand of '%s' */ 3355 /* unacceptable operand of '%s' */
3355 error(111, "offsetof"); 3356 error(111, "offsetof");
3356 3357
3357 // XXX: wrong size, no checking for sym fixme 3358 // XXX: wrong size, no checking for sym fixme
3358 int64_t offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE; 3359 int64_t offset_in_bytes = type_size_in_bits(tp) / CHAR_SIZE;
3359 tnode_t *tn = new_integer_constant_node(SIZEOF_TSPEC, offset_in_bytes); 3360 tnode_t *tn = expr_new_integer_constant(SIZEOF_TSPEC, offset_in_bytes);
3360 tn->tn_system_dependent = true; 3361 tn->tn_system_dependent = true;
3361 return tn; 3362 return tn;
3362} 3363}
3363 3364
3364int64_t 3365int64_t
3365type_size_in_bits(const type_t *tp) 3366type_size_in_bits(const type_t *tp)
3366{ 3367{
3367 int elem, elsz; 3368 int elem, elsz;
3368 bool flex; 3369 bool flex;
3369 3370
3370 elem = 1; 3371 elem = 1;
3371 flex = false; 3372 flex = false;
3372 while (tp->t_tspec == ARRAY) { 3373 while (tp->t_tspec == ARRAY) {
@@ -3448,27 +3449,27 @@ build_alignof(const type_t *tp) @@ -3448,27 +3449,27 @@ build_alignof(const type_t *tp)
3448 if (tp->t_bitfield) { 3449 if (tp->t_bitfield) {
3449 /* cannot take size/alignment of bit-field */ 3450 /* cannot take size/alignment of bit-field */
3450 error(145); 3451 error(145);
3451 return 0; 3452 return 0;
3452 } 3453 }
3453 if (tp->t_tspec == VOID) { 3454 if (tp->t_tspec == VOID) {
3454 /* cannot take size/alignment of void */ 3455 /* cannot take size/alignment of void */
3455 error(146); 3456 error(146);
3456 return 0; 3457 return 0;
3457 } 3458 }
3458 break; 3459 break;
3459 } 3460 }
3460 3461
3461 return new_integer_constant_node(SIZEOF_TSPEC, 3462 return expr_new_integer_constant(SIZEOF_TSPEC,
3462 (int64_t)alignment_in_bits(tp) / CHAR_SIZE); 3463 (int64_t)alignment_in_bits(tp) / CHAR_SIZE);
3463} 3464}
3464 3465
3465/* 3466/*
3466 * Type casts. 3467 * Type casts.
3467 */ 3468 */
3468tnode_t * 3469tnode_t *
3469cast(tnode_t *tn, type_t *tp) 3470cast(tnode_t *tn, type_t *tp)
3470{ 3471{
3471 tspec_t nt, ot; 3472 tspec_t nt, ot;
3472 3473
3473 if (tn == NULL) 3474 if (tn == NULL)
3474 return NULL; 3475 return NULL;
@@ -3557,27 +3558,27 @@ cast(tnode_t *tn, type_t *tp) @@ -3557,27 +3558,27 @@ cast(tnode_t *tn, type_t *tp)
3557 * information about expected argument types. 3558 * information about expected argument types.
3558 */ 3559 */
3559tnode_t * 3560tnode_t *
3560new_function_argument_node(tnode_t *args, tnode_t *arg) 3561new_function_argument_node(tnode_t *args, tnode_t *arg)
3561{ 3562{
3562 tnode_t *ntn; 3563 tnode_t *ntn;
3563 3564
3564 /* 3565 /*
3565 * If there was a serious error in the expression for the argument, 3566 * If there was a serious error in the expression for the argument,
3566 * create a dummy argument so the positions of the remaining arguments 3567 * create a dummy argument so the positions of the remaining arguments
3567 * will not change. 3568 * will not change.
3568 */ 3569 */
3569 if (arg == NULL) 3570 if (arg == NULL)
3570 arg = new_integer_constant_node(INT, (int64_t)0); 3571 arg = expr_new_integer_constant(INT, 0);
3571 3572
3572 ntn = new_tnode(PUSH, arg->tn_type, arg, args); 3573 ntn = new_tnode(PUSH, arg->tn_type, arg, args);
3573 3574
3574 return ntn; 3575 return ntn;
3575} 3576}
3576 3577
3577/* 3578/*
3578 * Create the node for a function call. Also check types of 3579 * Create the node for a function call. Also check types of
3579 * function arguments and insert conversions, if necessary. 3580 * function arguments and insert conversions, if necessary.
3580 */ 3581 */
3581tnode_t * 3582tnode_t *
3582new_function_call_node(tnode_t *func, tnode_t *args) 3583new_function_call_node(tnode_t *func, tnode_t *args)
3583{ 3584{
@@ -3779,27 +3780,27 @@ is_constcond_false(const tnode_t *tn, ts @@ -3779,27 +3780,27 @@ is_constcond_false(const tnode_t *tn, ts
3779 * functions called by build(). These tests must be done here because 3780 * functions called by build(). These tests must be done here because
3780 * we need some information about the context in which the operations 3781 * we need some information about the context in which the operations
3781 * are performed. 3782 * are performed.
3782 * After all tests are performed and dofreeblk is true, expr() frees the 3783 * After all tests are performed and dofreeblk is true, expr() frees the
3783 * memory which is used for the expression. 3784 * memory which is used for the expression.
3784 */ 3785 */
3785void 3786void
3786expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk, bool constcond_false_ok) 3787expr(tnode_t *tn, bool vctx, bool tctx, bool dofreeblk, bool constcond_false_ok)
3787{ 3788{
3788 3789
3789 lint_assert(tn != NULL || nerr != 0); 3790 lint_assert(tn != NULL || nerr != 0);
3790 3791
3791 if (tn == NULL) { 3792 if (tn == NULL) {
3792 tfreeblk(); 3793 expr_free_all();
3793 return; 3794 return;
3794 } 3795 }
3795 3796
3796 /* expr() is also called in global initializations */ 3797 /* expr() is also called in global initializations */
3797 /* TODO: rename constcond_false_ok */ 3798 /* TODO: rename constcond_false_ok */
3798 if (dcs->d_ctx != EXTERN && !constcond_false_ok) 3799 if (dcs->d_ctx != EXTERN && !constcond_false_ok)
3799 check_statement_reachable(); 3800 check_statement_reachable();
3800 3801
3801 check_expr_misc(tn, vctx, tctx, !tctx, false, false, false); 3802 check_expr_misc(tn, vctx, tctx, !tctx, false, false, false);
3802 if (tn->tn_op == ASSIGN) { 3803 if (tn->tn_op == ASSIGN) {
3803 if (hflag && tctx) 3804 if (hflag && tctx)
3804 /* assignment in conditional context */ 3805 /* assignment in conditional context */
3805 warning(159); 3806 warning(159);
@@ -3814,27 +3815,27 @@ expr(tnode_t *tn, bool vctx, bool tctx,  @@ -3814,27 +3815,27 @@ expr(tnode_t *tn, bool vctx, bool tctx,
3814 if (!modtab[tn->tn_op].m_has_side_effect) { 3815 if (!modtab[tn->tn_op].m_has_side_effect) {
3815 /* 3816 /*
3816 * for left operands of COMMA this warning is already 3817 * for left operands of COMMA this warning is already
3817 * printed 3818 * printed
3818 */ 3819 */
3819 if (tn->tn_op != COMMA && !vctx && !tctx) 3820 if (tn->tn_op != COMMA && !vctx && !tctx)
3820 check_null_effect(tn); 3821 check_null_effect(tn);
3821 } 3822 }
3822 if (dflag) 3823 if (dflag)
3823 display_expression(tn, 0); 3824 display_expression(tn, 0);
3824 3825
3825 /* free the tree memory */ 3826 /* free the tree memory */
3826 if (dofreeblk) 3827 if (dofreeblk)
3827 tfreeblk(); 3828 expr_free_all();
3828} 3829}
3829 3830
3830static bool 3831static bool
3831has_side_effect(const tnode_t *tn) // NOLINT(misc-no-recursion) 3832has_side_effect(const tnode_t *tn) // NOLINT(misc-no-recursion)
3832{ 3833{
3833 op_t op = tn->tn_op; 3834 op_t op = tn->tn_op;
3834 3835
3835 if (modtab[op].m_has_side_effect) 3836 if (modtab[op].m_has_side_effect)
3836 return true; 3837 return true;
3837 3838
3838 if (op == CVT && tn->tn_type->t_tspec == VOID) 3839 if (op == CVT && tn->tn_type->t_tspec == VOID)
3839 return has_side_effect(tn->tn_left); 3840 return has_side_effect(tn->tn_left);
3840 3841