Skip to content

Commit dcc822a

Browse files
brchiuvlm
authored andcommitted
Fix duplicate type names generated in headers file
For example, there are many 'enum value_PR' and 'struct value' generated if a class is instantiated as many instances. typedef enum value_PR { value_PR_NOTHING, /* No components present */ ..... } value_PR; typedef struct ProtocolIE_Field_6563P5 { .... struct value { value_PR present; union value_u { } choice; /* Context for parsing across buffer boundaries */ asn_struct_ctx_t _asn_ctx; } value; /* Context for parsing across buffer boundaries */ asn_struct_ctx_t _asn_ctx; } ProtocolIE_Field_6563P5_t;
1 parent 3e2de69 commit dcc822a

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

libasn1compiler/asn1c_C.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,7 @@ asn1c_lang_C_OpenType(arg_t *arg, asn1c_ioc_table_and_objset_t *opt_ioc,
10651065
open_type_choice->meta_type = AMT_TYPE;
10661066
open_type_choice->expr_type = ASN_CONSTR_OPEN_TYPE;
10671067
open_type_choice->_type_unique_index = arg->expr->_type_unique_index;
1068+
open_type_choice->parent_expr = arg->expr->parent_expr;
10681069

10691070
for(size_t row = 0; row < opt_ioc->ioct->rows; row++) {
10701071
struct asn1p_ioc_cell_s *cell =
@@ -1073,6 +1074,8 @@ asn1c_lang_C_OpenType(arg_t *arg, asn1c_ioc_table_and_objset_t *opt_ioc,
10731074
if(!cell->value) continue;
10741075

10751076
asn1p_expr_t *m = asn1p_expr_clone(cell->value, 0);
1077+
if (asn1p_lookup_child(open_type_choice, m->Identifier))
1078+
m->_mark |= TM_SKIPinUNION;
10761079
asn1p_expr_add(open_type_choice, m);
10771080
}
10781081

@@ -1285,11 +1288,14 @@ asn1c_lang_C_type_SIMPLE_TYPE(arg_t *arg) {
12851288
}
12861289
}
12871290

1291+
if(!(expr->_mark & TM_SKIPinUNION))
1292+
OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
12881293

1289-
OUT("%s", asn1c_type_name(arg, arg->expr, tnfmt));
12901294
if(!expr->_anonymous_type) {
1291-
OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
1292-
OUT("%s", MKID_safe(expr));
1295+
if(!(expr->_mark & TM_SKIPinUNION)) {
1296+
OUT("%s", (expr->marker.flags&EM_INDIRECT)?"\t*":"\t ");
1297+
OUT("%s", MKID_safe(expr));
1298+
}
12931299
if((expr->marker.flags & (EM_DEFAULT & ~EM_INDIRECT))
12941300
== (EM_DEFAULT & ~EM_INDIRECT))
12951301
OUT("\t/* DEFAULT %s */",
@@ -2659,7 +2665,7 @@ emit_member_type_selector(arg_t *arg, asn1p_expr_t *expr, asn1c_ioc_table_and_ob
26592665

26602666
REDIR(OT_CODE);
26612667
OUT("static asn_type_selector_result_t\n");
2662-
OUT("select_%s_type(const asn_TYPE_descriptor_t *parent_type, const void *parent_sptr) {\n", MKID_safe(expr));
2668+
OUT("select_%s_type(const asn_TYPE_descriptor_t *parent_type, const void *parent_sptr) {\n", c_name(arg).compound_name);
26632669
INDENT(+1);
26642670

26652671
OUT("asn_type_selector_result_t result = {0, 0};\n");
@@ -2717,7 +2723,7 @@ emit_member_type_selector(arg_t *arg, asn1p_expr_t *expr, asn1c_ioc_table_and_ob
27172723
OUT("\n");
27182724

27192725
REDIR(save_target);
2720-
OUT("select_%s_type", MKID_safe(expr));
2726+
OUT("select_%s_type", c_name(arg).compound_name);
27212727

27222728
return 0;
27232729
}

libasn1compiler/asn1c_naming.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) {
173173
construct_base_name(&b_as_member, expr, 0, 1);
174174

175175
static abuf tmp_compoundable_part_name;
176+
static abuf compound_part_name;
176177
abuf_clear(&tmp_compoundable_part_name);
178+
abuf_clear(&compound_part_name);
177179
construct_base_name(&tmp_compoundable_part_name, expr, compound_names, 0);
180+
construct_base_name(&compound_part_name, expr, 1, 0);
178181

179182
if(!expr->_anonymous_type) {
180183
if(arg->embed) {
@@ -204,6 +207,7 @@ c_name_impl(arg_t *arg, asn1p_expr_t *expr, int avoid_keywords) {
204207
names.presence_name = b_presence_name.buffer;
205208
names.members_enum = b_members_enum.buffer;
206209
names.members_name = b_members_name.buffer;
210+
names.compound_name = compound_part_name.buffer;
207211

208212
/* A _subset_ of names is checked against being globally unique */
209213
register_global_name(expr, names.base_name);

libasn1compiler/asn1c_naming.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct c_names {
2323
const char *presence_name; /* "foo_PR" */
2424
const char *members_enum; /* "enum foo" */
2525
const char *members_name; /* "e_foo" */
26+
const char *compound_name; /* always contain "parent_foo" */
2627
};
2728

2829
struct c_names c_name(arg_t *);

libasn1parser/asn1p_expr.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ asn1p_expr_set_source(asn1p_expr_t *expr, asn1p_module_t *module, int lineno) {
3030

3131
int
3232
asn1p_expr_compare(const asn1p_expr_t *a, const asn1p_expr_t *b) {
33+
if((a && !b) || (!a && b)) {
34+
return -1;
35+
}
36+
3337
if(a->meta_type != b->meta_type || a->expr_type != b->expr_type) {
3438
return -1;
3539
}
@@ -185,6 +189,8 @@ asn1p_expr_clone_impl(asn1p_expr_t *expr, int skip_extensions, asn1p_expr_t *(*r
185189
CLCOPY(tag);
186190
CLCOPY(marker.flags); /* OPTIONAL/DEFAULT */
187191
CLCOPY(_mark);
192+
CLCOPY(parent_expr);
193+
CLCOPY(_type_unique_index);
188194

189195
clone->data = 0; /* Do not clone this */
190196
clone->data_free = 0; /* Do not clone this */
@@ -303,6 +309,23 @@ asn1p_expr_add_many(asn1p_expr_t *to, asn1p_expr_t *from_what) {
303309
TQ_CONCAT(&(to->members), &(from_what->members), next);
304310
}
305311

312+
/*
313+
* Lookup a child by its name.
314+
*/
315+
asn1p_expr_t *
316+
asn1p_lookup_child(asn1p_expr_t *tc, const char *name) {
317+
asn1p_expr_t *child_tc;
318+
319+
TQ_FOR(child_tc, &(tc->members), next) {
320+
if(child_tc->Identifier
321+
&& strcmp(child_tc->Identifier, name) == 0) {
322+
return child_tc;
323+
}
324+
}
325+
326+
errno = ESRCH;
327+
return NULL;
328+
}
306329

307330
/*
308331
* Destruct the types collection structure.

libasn1parser/asn1p_expr.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ typedef struct asn1p_expr_s {
251251
TM_BROKEN = (1<<1), /* A warning was already issued */
252252
TM_PERFROMCT = (1<<2), /* PER FROM() constraint tables emitted */
253253
TM_NAMECLASH = (1<<3), /* Name clash found, need to add module name to resolve */
254-
TM_NAMEGIVEN = (1<<4) /* The expression has already yielded a name */
254+
TM_NAMEGIVEN = (1<<4), /* The expression has already yielded a name */
255+
TM_SKIPinUNION = (1<<5) /* Do not include this identifier in union again due to name duplication,
256+
especially for OPENTYPE. */
255257
} _mark;
256258

257259
/*
@@ -280,6 +282,7 @@ asn1p_expr_t *asn1p_expr_clone_with_resolver(asn1p_expr_t *,
280282
void *resolver_arg);
281283
void asn1p_expr_add(asn1p_expr_t *to, asn1p_expr_t *what);
282284
void asn1p_expr_add_many(asn1p_expr_t *to, asn1p_expr_t *from_what);
285+
asn1p_expr_t *asn1p_lookup_child(asn1p_expr_t *tc, const char *name);
283286
int asn1p_expr_compare(const asn1p_expr_t *, const asn1p_expr_t *);
284287
void asn1p_expr_free(asn1p_expr_t *expr);
285288
void asn1p_expr_set_source(asn1p_expr_t *, asn1p_module_t *, int lineno);

0 commit comments

Comments
 (0)