Skip to content

Commit

Permalink
Only emit a single use statement per non-primitive
Browse files Browse the repository at this point in the history
Closes lcm-proj#18

This is a bit primitive but I really suspect it won't matter for small
number of members, which I suspect will be the typical case.
  • Loading branch information
neachdainn committed Jun 10, 2017
1 parent 7953ae4 commit e7cf032
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lcmgen/emit_rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,31 @@ static void emit_struct_def(lcmgen_t *lcmgen, FILE *f, lcm_struct_t *lcm_struct)
// Include non-primitive types
for (unsigned int mind = 0; mind < g_ptr_array_size(lcm_struct->members); mind++) {
lcm_member_t *lm = (lcm_member_t *)g_ptr_array_index(lcm_struct->members, mind);
if (!lcm_is_primitive_type(lm->type->lctypename) &&
if (!lcm_is_primitive_type(lm->type->lctypename) &&
strcmp(lm->type->lctypename, lcm_struct->structname->lctypename)) {
char *mapped_tn = map_type_name(lm->type);
char *other_pn = dots_to_double_colons(lm->type->package);
emit(0, "use %s::%s;", other_pn, mapped_tn);
free(other_pn);
free(mapped_tn);

// This is naive, but it is highly unlikely any of these lists will
// ever get very large.
int skip = 0;
for (unsigned int prev = 0; prev < mind; prev++) {
lcm_member_t * pm = (lcm_member_t *)g_ptr_array_index(lcm_struct->members, prev);
printf("%s vs %s\n", pm->type->lctypename, lm->type->lctypename);
if (!lcm_is_primitive_type(pm->type->lctypename) &&
strcmp(pm->type->lctypename, lm->type->lctypename) == 0) {
// We've already emitted a "use" statement for this type
skip = 1;
break;
}
}

if(!skip) {
// This is the first time "use"ing this type
char *mapped_tn = map_type_name(lm->type);
char *other_pn = dots_to_double_colons(lm->type->package);
emit(0, "use %s::%s;", other_pn, mapped_tn);
free(other_pn);
free(mapped_tn);
}
}
}

Expand Down

0 comments on commit e7cf032

Please sign in to comment.