| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: init.c,v 1.185 2021/04/01 14:20:30 rillig Exp $ */ | | 1 | /* $NetBSD: init.c,v 1.186 2021/04/02 08:38:44 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.185 2021/04/01 14:20:30 rillig Exp $"); | | 41 | __RCSID("$NetBSD: init.c,v 1.186 2021/04/02 08:38:44 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 }; |
| @@ -182,44 +182,47 @@ debug_enter(const char *func) | | | @@ -182,44 +182,47 @@ debug_enter(const char *func) |
182 | } | | 182 | } |
183 | | | 183 | |
184 | static void __printflike(1, 2) | | 184 | static void __printflike(1, 2) |
185 | debug_step(const char *fmt, ...) | | 185 | debug_step(const char *fmt, ...) |
186 | { | | 186 | { |
187 | va_list va; | | 187 | va_list va; |
188 | | | 188 | |
189 | debug_indent(); | | 189 | debug_indent(); |
190 | va_start(va, fmt); | | 190 | va_start(va, fmt); |
191 | vfprintf(stdout, fmt, va); | | 191 | vfprintf(stdout, fmt, va); |
192 | va_end(va); | | 192 | va_end(va); |
193 | printf("\n"); | | 193 | printf("\n"); |
194 | } | | 194 | } |
| | | 195 | #define debug_step0 debug_step |
| | | 196 | #define debug_step1 debug_step |
| | | 197 | #define debug_step2 debug_step |
195 | | | 198 | |
196 | static void | | 199 | static void |
197 | debug_leave(const char *func) | | 200 | debug_leave(const char *func) |
198 | { | | 201 | { |
199 | | | 202 | |
200 | printf("%*s- %s\n", 2 * --debug_indentation, "", func); | | 203 | printf("%*s- %s\n", 2 * --debug_indentation, "", func); |
201 | } | | 204 | } |
202 | | | 205 | |
203 | #define debug_enter() (debug_enter)(__func__) | | 206 | #define debug_enter() (debug_enter)(__func__) |
204 | #define debug_leave() (debug_leave)(__func__) | | 207 | #define debug_leave() (debug_leave)(__func__) |
205 | | | 208 | |
206 | #else | | 209 | #else |
207 | | | 210 | |
208 | /* TODO: This is C99 */ | | | |
209 | #define debug_printf(fmt, ...) do { } while (false) | | | |
210 | #define debug_indent() do { } while (false) | | 211 | #define debug_indent() do { } while (false) |
211 | #define debug_enter() do { } while (false) | | 212 | #define debug_enter() do { } while (false) |
212 | #define debug_step(fmt, ...) do { } while (false) | | 213 | #define debug_step0(msg) do { } while (false) |
| | | 214 | #define debug_step1(fmt, arg0) do { } while (false) |
| | | 215 | #define debug_step2(fmt, arg1, arg2) do { } while (false) |
213 | #define debug_leave() do { } while (false) | | 216 | #define debug_leave() do { } while (false) |
214 | | | 217 | |
215 | #endif | | 218 | #endif |
216 | | | 219 | |
217 | | | 220 | |
218 | static void * | | 221 | static void * |
219 | unconst_cast(const void *p) | | 222 | unconst_cast(const void *p) |
220 | { | | 223 | { |
221 | void *r; | | 224 | void *r; |
222 | | | 225 | |
223 | memcpy(&r, &p, sizeof r); | | 226 | memcpy(&r, &p, sizeof r); |
224 | return r; | | 227 | return r; |
225 | } | | 228 | } |
| @@ -382,27 +385,27 @@ check_init_expr(const type_t *tp, sym_t | | | @@ -382,27 +385,27 @@ check_init_expr(const type_t *tp, sym_t |
382 | /* Create a temporary node for the left side. */ | | 385 | /* Create a temporary node for the left side. */ |
383 | ln = tgetblk(sizeof *ln); | | 386 | ln = tgetblk(sizeof *ln); |
384 | ln->tn_op = NAME; | | 387 | ln->tn_op = NAME; |
385 | ln->tn_type = tduptyp(tp); | | 388 | ln->tn_type = tduptyp(tp); |
386 | ln->tn_type->t_const = false; | | 389 | ln->tn_type->t_const = false; |
387 | ln->tn_lvalue = true; | | 390 | ln->tn_lvalue = true; |
388 | ln->tn_sym = sym; | | 391 | ln->tn_sym = sym; |
389 | | | 392 | |
390 | tn = cconv(tn); | | 393 | tn = cconv(tn); |
391 | | | 394 | |
392 | lt = ln->tn_type->t_tspec; | | 395 | lt = ln->tn_type->t_tspec; |
393 | rt = tn->tn_type->t_tspec; | | 396 | rt = tn->tn_type->t_tspec; |
394 | | | 397 | |
395 | debug_step("typeok '%s', '%s'", | | 398 | debug_step2("typeok '%s', '%s'", |
396 | type_name(ln->tn_type), type_name(tn->tn_type)); | | 399 | type_name(ln->tn_type), type_name(tn->tn_type)); |
397 | if (!typeok(INIT, 0, ln, tn)) | | 400 | if (!typeok(INIT, 0, ln, tn)) |
398 | return; | | 401 | return; |
399 | | | 402 | |
400 | /* | | 403 | /* |
401 | * Preserve the tree memory. This is necessary because otherwise | | 404 | * Preserve the tree memory. This is necessary because otherwise |
402 | * expr() would free it. | | 405 | * expr() would free it. |
403 | */ | | 406 | */ |
404 | tmem = tsave(); | | 407 | tmem = tsave(); |
405 | expr(tn, true, false, true, false); | | 408 | expr(tn, true, false, true, false); |
406 | trestor(tmem); | | 409 | trestor(tmem); |
407 | | | 410 | |
408 | check_bit_field_init(ln, lt, rt); | | 411 | check_bit_field_init(ln, lt, rt); |
| @@ -864,27 +867,27 @@ initialization_add_designator(struct ini | | | @@ -864,27 +867,27 @@ initialization_add_designator(struct ini |
864 | */ | | 867 | */ |
865 | static bool | | 868 | static bool |
866 | initialization_expr_using_assign(struct initialization *in, tnode_t *rn) | | 869 | initialization_expr_using_assign(struct initialization *in, tnode_t *rn) |
867 | { | | 870 | { |
868 | tnode_t *ln, *tn; | | 871 | tnode_t *ln, *tn; |
869 | | | 872 | |
870 | if (!has_automatic_storage_duration(in->in_sym)) | | 873 | if (!has_automatic_storage_duration(in->in_sym)) |
871 | return false; | | 874 | return false; |
872 | if (in->in_brace_level != NULL) | | 875 | if (in->in_brace_level != NULL) |
873 | return false; | | 876 | return false; |
874 | if (in->in_sym->s_type->t_tspec == ARRAY) | | 877 | if (in->in_sym->s_type->t_tspec == ARRAY) |
875 | return false; | | 878 | return false; |
876 | | | 879 | |
877 | debug_step("handing over to ASSIGN"); | | 880 | debug_step0("handing over to ASSIGN"); |
878 | | | 881 | |
879 | ln = new_name_node(in->in_sym, 0); | | 882 | ln = new_name_node(in->in_sym, 0); |
880 | ln->tn_type = tduptyp(ln->tn_type); | | 883 | ln->tn_type = tduptyp(ln->tn_type); |
881 | ln->tn_type->t_const = false; | | 884 | ln->tn_type->t_const = false; |
882 | | | 885 | |
883 | tn = build(ASSIGN, ln, rn); | | 886 | tn = build(ASSIGN, ln, rn); |
884 | expr(tn, false, false, false, false); | | 887 | expr(tn, false, false, false, false); |
885 | | | 888 | |
886 | return true; | | 889 | return true; |
887 | } | | 890 | } |
888 | | | 891 | |
889 | /* Initialize a character array or wchar_t array with a string literal. */ | | 892 | /* Initialize a character array or wchar_t array with a string literal. */ |
890 | static bool | | 893 | static bool |
| @@ -966,27 +969,27 @@ initialization_expr(struct initializatio | | | @@ -966,27 +969,27 @@ initialization_expr(struct initializatio |
966 | * Hack to accept initializations with omitted braces, see | | 969 | * Hack to accept initializations with omitted braces, see |
967 | * c99_6_7_8_p28_example5 in test d_c99_init.c. Since both GCC and | | 970 | * c99_6_7_8_p28_example5 in test d_c99_init.c. Since both GCC and |
968 | * Clang already warn about this at level -Wall, there is no point | | 971 | * Clang already warn about this at level -Wall, there is no point |
969 | * in repeating the same check in lint. If needed, support for these | | 972 | * in repeating the same check in lint. If needed, support for these |
970 | * edge cases could be added, but that would increase the complexity. | | 973 | * edge cases could be added, but that would increase the complexity. |
971 | */ | | 974 | */ |
972 | if (is_scalar(tn->tn_type->t_tspec) && | | 975 | if (is_scalar(tn->tn_type->t_tspec) && |
973 | (tp->t_tspec == ARRAY || is_struct_or_union(tp->t_tspec)) && | | 976 | (tp->t_tspec == ARRAY || is_struct_or_union(tp->t_tspec)) && |
974 | bl != NULL) { | | 977 | bl != NULL) { |
975 | bl->bl_confused = true; | | 978 | bl->bl_confused = true; |
976 | goto done; | | 979 | goto done; |
977 | } | | 980 | } |
978 | | | 981 | |
979 | debug_step("expecting '%s', expression has '%s'", | | 982 | debug_step2("expecting '%s', expression has '%s'", |
980 | type_name(tp), type_name(tn->tn_type)); | | 983 | type_name(tp), type_name(tn->tn_type)); |
981 | check_init_expr(tp, in->in_sym, tn); | | 984 | check_init_expr(tp, in->in_sym, tn); |
982 | | | 985 | |
983 | advance: | | 986 | advance: |
984 | if (bl != NULL) | | 987 | if (bl != NULL) |
985 | brace_level_advance(bl); | | 988 | brace_level_advance(bl); |
986 | done: | | 989 | done: |
987 | if (bl != NULL) | | 990 | if (bl != NULL) |
988 | designation_reset(&bl->bl_designation); | | 991 | designation_reset(&bl->bl_designation); |
989 | | | 992 | |
990 | initialization_debug(in); | | 993 | initialization_debug(in); |
991 | debug_leave(); | | 994 | debug_leave(); |
992 | } | | 995 | } |
| @@ -1005,49 +1008,49 @@ current_init(void) | | | @@ -1005,49 +1008,49 @@ current_init(void) |
1005 | | | 1008 | |
1006 | sym_t ** | | 1009 | sym_t ** |
1007 | current_initsym(void) | | 1010 | current_initsym(void) |
1008 | { | | 1011 | { |
1009 | | | 1012 | |
1010 | return ¤t_init()->in_sym; | | 1013 | return ¤t_init()->in_sym; |
1011 | } | | 1014 | } |
1012 | | | 1015 | |
1013 | void | | 1016 | void |
1014 | begin_initialization(sym_t *sym) | | 1017 | begin_initialization(sym_t *sym) |
1015 | { | | 1018 | { |
1016 | struct initialization *in; | | 1019 | struct initialization *in; |
1017 | | | 1020 | |
1018 | debug_step("begin initialization of '%s'", type_name(sym->s_type)); | | 1021 | debug_step1("begin initialization of '%s'", type_name(sym->s_type)); |
1019 | #ifdef DEBUG | | 1022 | #ifdef DEBUG |
1020 | debug_indentation++; | | 1023 | debug_indentation++; |
1021 | #endif | | 1024 | #endif |
1022 | | | 1025 | |
1023 | in = initialization_new(sym); | | 1026 | in = initialization_new(sym); |
1024 | in->in_enclosing = init; | | 1027 | in->in_enclosing = init; |
1025 | init = in; | | 1028 | init = in; |
1026 | } | | 1029 | } |
1027 | | | 1030 | |
1028 | void | | 1031 | void |
1029 | end_initialization(void) | | 1032 | end_initialization(void) |
1030 | { | | 1033 | { |
1031 | struct initialization *in; | | 1034 | struct initialization *in; |
1032 | | | 1035 | |
1033 | in = init; | | 1036 | in = init; |
1034 | init = in->in_enclosing; | | 1037 | init = in->in_enclosing; |
1035 | initialization_free(in); | | 1038 | initialization_free(in); |
1036 | | | 1039 | |
1037 | #ifdef DEBUG | | 1040 | #ifdef DEBUG |
1038 | debug_indentation--; | | 1041 | debug_indentation--; |
1039 | #endif | | 1042 | #endif |
1040 | debug_step("end initialization"); | | 1043 | debug_step0("end initialization"); |
1041 | } | | 1044 | } |
1042 | | | 1045 | |
1043 | void | | 1046 | void |
1044 | add_designator_member(sbuf_t *sb) | | 1047 | add_designator_member(sbuf_t *sb) |
1045 | { | | 1048 | { |
1046 | | | 1049 | |
1047 | initialization_add_designator(current_init(), sb->sb_name, 0); | | 1050 | initialization_add_designator(current_init(), sb->sb_name, 0); |
1048 | } | | 1051 | } |
1049 | | | 1052 | |
1050 | void | | 1053 | void |
1051 | add_designator_subscript(range_t range) | | 1054 | add_designator_subscript(range_t range) |
1052 | { | | 1055 | { |
1053 | | | 1056 | |