From d467b4230c0c44229599b9466db330e1b4f1fb79 Mon Sep 17 00:00:00 2001 From: Matthew Leach Date: Wed, 10 Feb 2021 21:46:02 +0000 Subject: [PATCH] [src] fix various warnings Fix warnings, mostly consisting of the C++ code, using range-based for loops and not passing by reference. --- src/Makefile.am | 2 +- src/c.lang | 2 ++ src/cscript.c | 7 +++-- src/evaluate.c | 3 --- src/function-call.c | 55 ++++++++++++++++++++++++-------------- src/gc.c | 1 + src/genaccess.cpp | 6 ++--- src/genctypes.cpp | 2 +- src/gengc.cpp | 8 +++--- src/gentree.cpp | 2 +- src/gentypes.cpp | 2 +- src/lang.cpp | 2 +- src/tree-dump-primitives.h | 5 ++++ 13 files changed, 60 insertions(+), 37 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index a0b65ed..f34314c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,7 +22,7 @@ function-call.c EXTRA_DIST = c.lang DISTCLEANFILES = tree.def ctypes.def tree-base.h tree-access.h gc-internal.c \ -tree-dump.c spec-resolver.c tree-dump-dot.c +tree-dump.c spec-resolver.c tree-dump-dot.c ffi-types.c bic_SOURCES = main.c bic_LDADD = libbic.a $(GMP_LIBS) $(FFI_LIBS) diff --git a/src/c.lang b/src/c.lang index 361fa40..82aebba 100644 --- a/src/c.lang +++ b/src/c.lang @@ -7,6 +7,7 @@ (sz "size_t") (flag "bool") (ffi_cif "ffi_cif *") + (ffi_types "ffi_type **") (ffi_closure "ffi_closure *") (comp_decl_type "compound_type") (live_var_val "union value *")) @@ -189,6 +190,7 @@ (deftype T_CLOSURE "Closure Call" "tCLOSURE" ("ARG_TYPES" "RET_TYPE" "FN" ("CIF" ffi_cif) + ("TYPES" ffi_types) ("CLOSURE" ffi_closure))) (deftype T_FN_ARG "Function Argument" "tFNARG" diff --git a/src/cscript.c b/src/cscript.c index 2579ebb..52310a5 100644 --- a/src/cscript.c +++ b/src/cscript.c @@ -143,10 +143,13 @@ static void __evaluate_cscript(tree head, const char *fname, int argc, bool has_non_decls = false; for_each_tree(i, head) { - if (is_T_DECL(i) && chain_has_T_FN(tDECL_DECLS(i))) { - tree fn, id; + if (is_T_DECL(i)) { + tree fn = NULL, id; for_each_tree(fn, tDECL_DECLS(i)) break; + if (!is_T_FN(fn)) + continue; + id = tFN_DECL(fn); if (!is_T_IDENTIFIER(id)) diff --git a/src/evaluate.c b/src/evaluate.c index df3fb65..0860e82 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -1964,9 +1964,6 @@ static tree eval_cast(tree t, int depth) DEFCAST(D_T_PTR, D_T_ULONGLONG); DEFCAST(D_T_PTR, D_T_PTR); -#pragma GCC diagnostic pop -#pragma GCC diagnostic pop - eval_die(t, "Could not perform cast.\n"); } diff --git a/src/function-call.c b/src/function-call.c index 01ec670..5e9df69 100644 --- a/src/function-call.c +++ b/src/function-call.c @@ -10,11 +10,14 @@ #include "gc.h" #include "spec-resolver.h" -static ffi_type *type_stack[32]; -static void *val_stack[32]; -static int n = 0; - -static void push_arg(ffi_type *type, void* val, bool should_promote) +#define ARG_STACK_SZ 32 + +static void push_arg(ffi_type **type_stack, + void **val_stack, + int *n, + ffi_type *type, + void* val, + bool should_promote) { /* Acording to the 'default argument promotions' floats should be promotoed to doubles and TNAME, should_promote); \ + push_arg(types, vals, &n, \ + get_ffi_type(TNAME), &tLV_VAL(arg)->TNAME, should_promote); \ break; #include "ctypes.def" #undef DEFCTYPE @@ -84,14 +91,14 @@ void do_ext_call(void *function_address, tree args, tree ret_lv, { int *buf = alloca(sizeof(int)); *buf = mpz_get_si(tINT_VAL(arg)); - push_arg(&ffi_type_sint, buf, should_promote); + push_arg(types, vals, &n, &ffi_type_sint, buf, should_promote); break; } case T_FLOAT: { double *buf = alloca(sizeof(double)); *buf = mpf_get_d(tFLOAT_VAL(arg)); - push_arg(&ffi_type_double, buf, should_promote); + push_arg(types, vals, &n, &ffi_type_double, buf, should_promote); break; } default: @@ -104,11 +111,14 @@ void do_ext_call(void *function_address, tree args, tree ret_lv, ret_type = get_ffi_type(TYPE(tLV_TYPE(ret_lv))); if (variadic_pos) - ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, *variadic_pos, n, ret_type, &type_stack); + ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, *variadic_pos, n, ret_type, types); else - ffi_prep_cif(&cif, FFI_DEFAULT_ABI, n, ret_type, &type_stack); + ffi_prep_cif(&cif, FFI_DEFAULT_ABI, n, ret_type, types); + + ffi_call(&cif, function_address, ret_lv ? (void *)tLV_VAL(ret_lv) : NULL, vals); - ffi_call(&cif, function_address, ret_lv ? (void *)tLV_VAL(ret_lv) : NULL, val_stack); + free(types); + free(vals); enable_gc(); } @@ -156,8 +166,9 @@ void *get_entry_point_for_fn(tree fndef) closure = tree_make(T_CLOSURE); ffi_type *ffi_ret_type = &ffi_type_void; tCLOSURE_CIF(closure) = malloc(sizeof(*tCLOSURE_CIF(closure))); + tCLOSURE_TYPES(closure) = calloc(sizeof(*tCLOSURE_TYPES(closure)), ARG_STACK_SZ); - n = 0; + int n = 0; for_each_tree(arg, args) { tree arg_type; @@ -175,8 +186,11 @@ void *get_entry_point_for_fn(tree fndef) if (!is_T_IDENTIFIER(declarator)) eval_die(arg, "Unknown declarator type in closure allocation"); - type_stack[n++] = get_ffi_type(TYPE(arg_type)); + tCLOSURE_TYPES(closure)[n++] = get_ffi_type(TYPE(arg_type)); tree_chain(arg_type, arg_chain); + + if (n >= ARG_STACK_SZ) + eval_die(fndef, "Argument stack size exceeded"); } if (!is_D_T_VOID(ret_type)) @@ -188,7 +202,8 @@ void *get_entry_point_for_fn(tree fndef) tCLOSURE_CLOSURE(closure) = ffi_closure_alloc(sizeof(*closure), &ret); - ffi_prep_cif(tCLOSURE_CIF(closure), FFI_DEFAULT_ABI, n, ffi_ret_type, &type_stack); + ffi_prep_cif(tCLOSURE_CIF(closure), FFI_DEFAULT_ABI, n, ffi_ret_type, + tCLOSURE_TYPES(closure)); ffi_prep_closure_loc(tCLOSURE_CLOSURE(closure), tCLOSURE_CIF(closure), diff --git a/src/gc.c b/src/gc.c index e68ce52..3498258 100644 --- a/src/gc.c +++ b/src/gc.c @@ -104,6 +104,7 @@ static void dealloc_object(gc_obj obj) case T_CLOSURE: ffi_closure_free(tCLOSURE_CLOSURE(t)); free(tCLOSURE_CIF(t)); + free(tCLOSURE_TYPES(t)); break; default: /* All other types don't contain any other referencies to diff --git a/src/genaccess.cpp b/src/genaccess.cpp index c9e8eb2..6eb05b0 100644 --- a/src/genaccess.cpp +++ b/src/genaccess.cpp @@ -23,7 +23,7 @@ static void outputProps(FILE *f, const struct TreeType &t) fprintf(f, "/* Access macros for %s objects */\n", t.name.c_str()); - for (const auto prop : t.props) + for (const auto &prop : t.props) fprintf(f, "#define %s(obj) (_DATA( TREE_CHECK((obj), %s)).%s)\n", t.getPropAccessor(prop.first).c_str(), t.name.c_str(), prop.second.memberName.c_str()); @@ -34,10 +34,10 @@ static void outputProps(FILE *f, const struct TreeType &t) static void outputTreeAccessMacros(const struct lang &lang, FILE *f) { - for (const auto type : lang.treeTypes) + for (const auto &type : lang.treeTypes) outputProps(f, type); - for (const auto ctype : lang.treeCTypes) + for (const auto &ctype : lang.treeCTypes) outputProps(f, ctype); } diff --git a/src/genctypes.cpp b/src/genctypes.cpp index 7635243..9e47e27 100644 --- a/src/genctypes.cpp +++ b/src/genctypes.cpp @@ -10,7 +10,7 @@ static void outputTreeDefCtype(const struct lang &lang) " * Automatically generated by genctypes. DO NOT EDIT.\n" " */\n\n", f); - for (auto const ctype : lang.treeCTypes) + for (auto const &ctype : lang.treeCTypes) fprintf(f, "DEFCTYPE(%s, \"%s\", %s, %s, %s)\n", ctype.name.c_str(), ctype.friendly_name.c_str(), ctype.ctype.c_str(), ctype.format_string.c_str(), diff --git a/src/gengc.cpp b/src/gengc.cpp index 05752c3..a3a70b9 100644 --- a/src/gengc.cpp +++ b/src/gengc.cpp @@ -14,7 +14,7 @@ static void outputGCMarkProps(FILE *f, const struct TreeType &t) fprintf(f, " case %s:\n", t.name.c_str()); - for (const auto prop : t.props) + for (const auto &prop : t.props) if (prop.second.baseType.isTree) fprintf(f, " mark_tree(%s(t));\n", t.getPropAccessor(prop.first).c_str()); @@ -24,7 +24,7 @@ static void outputGCMarkProps(FILE *f, const struct TreeType &t) static void outputPropertylessTypes(FILE *f) { fputs(" /* These types have no properties to traverse */\n", f); - for (const auto prop : propertylessTypes) + for (const auto &prop : propertylessTypes) fprintf(f, " case %s:\n", prop.c_str()); fputs(" return;\n", f); @@ -33,10 +33,10 @@ static void outputPropertylessTypes(FILE *f) static void outputGCPropTraverse(const struct lang &lang, FILE *f) { - for (const auto type : lang.treeTypes) + for (const auto &type : lang.treeTypes) outputGCMarkProps(f, type); - for (const auto ctype : lang.treeCTypes) + for (const auto &ctype : lang.treeCTypes) outputGCMarkProps(f, ctype); outputPropertylessTypes(f); diff --git a/src/gentree.cpp b/src/gentree.cpp index 273664b..721994f 100644 --- a/src/gentree.cpp +++ b/src/gentree.cpp @@ -5,7 +5,7 @@ static void outputTreeDataUnion(const struct lang &lang, { fputs("struct tree_data {\n", f); - for (const auto typePool : lang.baseTypePools) + for (const auto &typePool : lang.baseTypePools) typePool.second.emitDeclarations(f); lang.treePool.emitDeclarations(f); diff --git a/src/gentypes.cpp b/src/gentypes.cpp index 340c79b..20f3473 100644 --- a/src/gentypes.cpp +++ b/src/gentypes.cpp @@ -10,7 +10,7 @@ static void outputTreeDef(const struct lang &lang) " * Automatically generated by gendef. DO NOT EDIT.\n" " */\n\n", f); - for (auto const type : lang.treeTypes) + for (auto const &type : lang.treeTypes) fprintf(f, "DEFTYPE(%s, \"%s\")\n", type.name.c_str(), type.friendly_name.c_str()); diff --git a/src/lang.cpp b/src/lang.cpp index b0defd3..9dbdd27 100644 --- a/src/lang.cpp +++ b/src/lang.cpp @@ -22,7 +22,7 @@ int yywrap(void) void TypePool::emitDeclarations(FILE *f) const { - for (const auto instantypatedType : pool_) + for (const auto &instantypatedType : pool_) fprintf(f, " %s %s;\n", instantypatedType.baseType.type.c_str(), instantypatedType.memberName.c_str()); } diff --git a/src/tree-dump-primitives.h b/src/tree-dump-primitives.h index b2a8d93..c134a9c 100644 --- a/src/tree-dump-primitives.h +++ b/src/tree-dump-primitives.h @@ -72,6 +72,11 @@ static inline void tree_dump_ffi_closure(tree t, ffi_closure *closure, enum DUMP { return; } + +static inline void tree_dump_ffi_types(tree t, ffi_type **types, + enum DUMP_TYPE dt) { + return; +} static inline void tree_dump_ffi_cif(tree t, ffi_cif *closure, enum DUMP_TYPE dt)