Skip to content

Commit

Permalink
[src] fix various warnings
Browse files Browse the repository at this point in the history
Fix warnings, mostly consisting of the C++ code, using range-based for loops and
not passing by reference.
  • Loading branch information
hexagonal-sun committed Feb 10, 2021
1 parent 7748c44 commit d467b42
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/c.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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 *"))
Expand Down Expand Up @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions src/cscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 0 additions & 3 deletions src/evaluate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
55 changes: 35 additions & 20 deletions src/function-call.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <int should be promoted to int when being passed as a
Expand All @@ -34,23 +37,25 @@ static void push_arg(ffi_type *type, void* val, bool should_promote)
PROMOTE(ffi_type_ushort, unsigned short, D_T_UINT, unsigned int, ffi_type_uint);
#undef PROMOTE

if (n == 32) {
if (*n == ARG_STACK_SZ) {
fprintf(stderr , "Max num args reached.\n");
exit(1);
}

type_stack[n] = type;
val_stack[n] = val;
type_stack[*n] = type;
val_stack[*n] = val;

n++;
(*n)++;
}

void do_ext_call(void *function_address, tree args, tree ret_lv,
int *variadic_pos)
{
tree fn_arg;
ffi_cif cif;
n = 0;
int n = 0;
ffi_type **types = calloc(sizeof(*types), ARG_STACK_SZ);
void **vals = calloc(sizeof(*vals), ARG_STACK_SZ);
ffi_type *ret_type = &ffi_type_void;

// Prevent GC here, we could potentially create temporary LV for the default
Expand All @@ -66,13 +71,15 @@ void do_ext_call(void *function_address, tree args, tree ret_lv,

switch(TYPE(arg)) {
case T_STRING:
push_arg(&ffi_type_pointer, &tSTRING_VAL(arg), should_promote);
push_arg(types, vals, &n,
&ffi_type_pointer, &tSTRING_VAL(arg), should_promote);
break;
case T_LIVE_VAR:
switch(TYPE(tLV_TYPE(arg))) {
#define DEFCTYPE(TNAME, DESC, STDINTSZ, FMT, FFMEM) \
case TNAME: \
push_arg(get_ffi_type(TNAME), &tLV_VAL(arg)->TNAME, should_promote); \
push_arg(types, vals, &n, \
get_ffi_type(TNAME), &tLV_VAL(arg)->TNAME, should_promote); \
break;
#include "ctypes.def"
#undef DEFCTYPE
Expand All @@ -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:
Expand All @@ -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();
}
Expand Down Expand Up @@ -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;
Expand All @@ -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))
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/genaccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/genctypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 4 additions & 4 deletions src/gengc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/gentree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/gentypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
2 changes: 1 addition & 1 deletion src/lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
5 changes: 5 additions & 0 deletions src/tree-dump-primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d467b42

Please sign in to comment.