From: Nick Alcock Date: Tue, 18 Nov 2025 10:25:23 +0000 (+0000) Subject: libctf, include: API review: ctf_add_qualifier X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=75e5f7b418ec2a20a09e8ecb9724bd9d65e554cb;p=thirdparty%2Fbinutils-gdb.git libctf, include: API review: ctf_add_qualifier We don't need three separate functions just to add cvr-quals. So replace ctf_add_const, ctf_add_restrict and ctf_add_volatile with one new function, ctf_add_qualifier, and use it in the deduplicator rather than its current use of the libctf-internal ctf_add_reftype. This also lets us move various pointer-only stuff like ptrtab updating into ctf_add_pointer, where it belongs. (We don't export ctf_add_reftype because pointers are too conceptually different from qualifiers to add with the same function.) --- diff --git a/include/ctf-api.h b/include/ctf-api.h index 42869211e4a..c2f8dcae4ac 100644 --- a/include/ctf-api.h +++ b/include/ctf-api.h @@ -298,6 +298,7 @@ typedef int ctf_func_type_flags_t; _CTF_ITEM (ECTF_NOTINTFP, "Type is not an integer, float, or enum.") \ _CTF_ITEM (ECTF_NOTARRAY, "Type is not an array.") \ _CTF_ITEM (ECTF_NOTREF, "Type does not reference another type.") \ + _CTF_ITEM (ECTF_NOTQUAL, "Type is not a cvr-qual.") \ _CTF_ITEM (ECTF_NAMELEN, "Buffer is too small to hold type name.") \ _CTF_ITEM (ECTF_NOTYPE, "No type found corresponding to name.") \ _CTF_ITEM (ECTF_SYNTAX, "Syntax error in type name.") \ @@ -1001,7 +1002,6 @@ extern ctf_dict_t *ctf_create (ctf_error_t *); extern ctf_id_t ctf_add_array (ctf_dict_t *, uint32_t, const ctf_arinfo_t *); -extern ctf_id_t ctf_add_const (ctf_dict_t *, uint32_t, ctf_id_t); /* enums are created signed by default: an encoding of NULL is permitted and means "use the default". If you want an unsigned enum, use @@ -1050,7 +1050,6 @@ extern ctf_id_t ctf_add_pointer (ctf_dict_t *, uint32_t, ctf_id_t); extern ctf_id_t ctf_add_type (ctf_dict_t *, ctf_dict_t *, ctf_id_t); extern ctf_id_t ctf_add_typedef (ctf_dict_t *, uint32_t, const char *, ctf_id_t); -extern ctf_id_t ctf_add_restrict (ctf_dict_t *, uint32_t, ctf_id_t); /* Add type and decl tags to whole types or (for decl tags) specific components of types (parameter count for functions, member count for structs @@ -1099,7 +1098,6 @@ extern ctf_id_t ctf_add_struct (ctf_dict_t *, uint32_t flag, const char *name, variables, etc, to point to. */ extern ctf_id_t ctf_add_unknown (ctf_dict_t *, uint32_t, const char *); -extern ctf_id_t ctf_add_volatile (ctf_dict_t *, uint32_t, ctf_id_t); /* Add an enumerator to an enum or enum64. If the enum is non-root, so are all the constants added to it by ctf_add_enumerator. */ @@ -1119,6 +1117,8 @@ extern ctf_ret_t ctf_add_member_bitfield (ctf_dict_t *, ctf_id_t souid, const char *, ctf_id_t type, size_t bit_offset, int bit_width); +extern ctf_id_t ctf_add_qualifier (ctf_dict_t *, uint32_t, ctf_kind_t cvr_qual, ctf_id_t); + /* ctf_add_variable adds variables to no datasec at all; ctf_add_section_variable adds them to the given datasec, or to no datasec at all if the datasec is NULL. */ diff --git a/libctf/ctf-create.c b/libctf/ctf-create.c index 675dd3abc94..0f5d735d4af 100644 --- a/libctf/ctf-create.c +++ b/libctf/ctf-create.c @@ -709,45 +709,27 @@ ctf_add_encoded (ctf_dict_t *fp, uint32_t flag, return dtd->dtd_type; } -ctf_id_t -ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, ctf_kind_t kind) +static ctf_id_t +ctf_add_reftype (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref, ctf_kind_t kind, + ctf_dict_t **reffp) { + ctf_dict_t *tmp = fp; ctf_dtdef_t *dtd; - ctf_dict_t *typedict = fp; - ctf_dict_t *refdict = fp; - int child = fp->ctf_flags & LCTF_CHILD; - uint32_t type_idx; - uint32_t ref_idx; + + if (ref != 0 && ctf_lookup_by_id (&tmp, ref, NULL) == NULL) + return CTF_ERR; /* errno is set for us. */ if (ref == CTF_ERR || ref > CTF_MAX_TYPE) return (ctf_set_typed_errno (fp, EINVAL)); - if (ref != 0 && ctf_lookup_by_id (&refdict, ref, NULL) == NULL) - return CTF_ERR; /* errno is set for us. */ - if ((dtd = ctf_add_generic (fp, flag, NULL, kind, 0, 0, 0, NULL)) == NULL) return CTF_ERR; /* errno is set for us. */ dtd->dtd_data->ctt_info = CTF_TYPE_INFO (kind, 0, 0); dtd->dtd_data->ctt_type = (uint32_t) ref; - if (kind != CTF_K_POINTER) - return dtd->dtd_type; - - /* If we are adding a pointer, update the ptrtab, pointing at this type from - the type it points to. Note that ctf_typemax is at this point one higher - than we want to check against, because it's just been incremented for the - addition of this type. The pptrtab is lazily-updated as needed, so is not - touched here. */ - - typedict = ctf_get_dict (fp, dtd->dtd_type); - - type_idx = ctf_type_to_index (typedict, dtd->dtd_type); - ref_idx = ctf_type_to_index (refdict, ref); - - if (ctf_type_ischild (fp, ref) == child - && ref_idx < fp->ctf_typemax) - fp->ctf_ptrtab[ref_idx] = type_idx; + if (reffp) + *reffp = tmp; return dtd->dtd_type; } @@ -827,7 +809,34 @@ ctf_add_btf_float (ctf_dict_t *fp, uint32_t flag, ctf_id_t ctf_add_pointer (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) { - return (ctf_add_reftype (fp, flag, ref, CTF_K_POINTER)); + int child = fp->ctf_flags & LCTF_CHILD; + + ctf_dict_t *typedict = fp; + ctf_dict_t *refdict; + ctf_id_t type; + uint32_t type_idx; + uint32_t ref_idx; + + if ((type = ctf_add_reftype (fp, flag, ref, + CTF_K_POINTER, &refdict)) == CTF_ERR) + return CTF_ERR; /* errno is set for us. */ + + /* Update the ptrtab, pointing at this type from the type it points to. + Note that ctf_typemax is at this point one higher than we want to check + against, because it's just been incremented for the addition of this + type. The pptrtab is lazily-updated as needed, so is not touched + here. */ + + typedict = ctf_get_dict (fp, type); + + type_idx = ctf_type_to_index (typedict, type); + ref_idx = ctf_type_to_index (refdict, ref); + + if (ctf_type_ischild (fp, ref) == child + && ref_idx < fp->ctf_typemax) + fp->ctf_ptrtab[ref_idx] = type_idx; + + return type; } ctf_id_t @@ -1404,21 +1413,17 @@ ctf_add_typedef (ctf_dict_t *fp, uint32_t flag, const char *name, } ctf_id_t -ctf_add_volatile (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) -{ - return (ctf_add_reftype (fp, flag, ref, CTF_K_VOLATILE)); -} - -ctf_id_t -ctf_add_const (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) -{ - return (ctf_add_reftype (fp, flag, ref, CTF_K_CONST)); -} - -ctf_id_t -ctf_add_restrict (ctf_dict_t *fp, uint32_t flag, ctf_id_t ref) +ctf_add_qualifier (ctf_dict_t *fp, uint32_t flag, ctf_kind_t cvr_qual, + ctf_id_t ref) { - return (ctf_add_reftype (fp, flag, ref, CTF_K_RESTRICT)); + if (cvr_qual != CTF_K_CONST && cvr_qual != CTF_K_VOLATILE && cvr_qual != CTF_K_RESTRICT) + { + ctf_err_warn (fp, 0, ECTF_NOTENUM, _("ctf_add_qualifier: kind %i is " + "not CTF_K_CONST, CTF_K_VOLATILE " + "or CTF_K_RESTRICT.\n"), cvr_qual); + return ctf_set_typed_errno (fp, ECTF_NOTQUAL); + } + return (ctf_add_reftype (fp, flag, ref, cvr_qual, NULL)); } ctf_ret_t @@ -2527,7 +2532,10 @@ ctf_add_type_internal (ctf_dict_t *dst_fp, ctf_dict_t *src_fp, ctf_id_t src_type if (src_type == CTF_ERR) return CTF_ERR; /* errno is set for us. */ - dst_type = ctf_add_reftype (dst_fp, flag, src_type, kind); + if (kind == CTF_K_POINTER) + dst_type = ctf_add_pointer (dst_fp, flag, src_type); + else + dst_type = ctf_add_qualifier (dst_fp, flag, kind, src_type); break; case CTF_K_ARRAY: diff --git a/libctf/ctf-dedup.c b/libctf/ctf-dedup.c index d0c374d767c..4a4172b7b3a 100644 --- a/libctf/ctf-dedup.c +++ b/libctf/ctf-dedup.c @@ -3599,9 +3599,19 @@ ctf_dedup_emit_type (const char *hval, ctf_dict_t *output, ctf_dict_t **inputs, parents, input, input_num, ref)) == CTF_ERR) goto err_input; /* errno is set for us. */ - - if ((new_type = ctf_add_reftype (target, isroot, ref, kind)) == CTF_ERR) - goto err_target; /* errno is set for us. */ + + if (kind == CTF_K_POINTER) + { + errtype = _("pointer"); + if ((new_type = ctf_add_pointer (target, isroot, ref)) == CTF_ERR) + goto err_target; /* errno is set for us. */ + } + else + { + errtype = _("cvr-qual"); + if ((new_type = ctf_add_qualifier (target, isroot, kind, ref)) == CTF_ERR) + goto err_target; /* errno is set for us. */ + } break; case CTF_K_TYPE_TAG: diff --git a/libctf/ctf-impl.h b/libctf/ctf-impl.h index 99971f6a327..763a7c9231f 100644 --- a/libctf/ctf-impl.h +++ b/libctf/ctf-impl.h @@ -785,8 +785,6 @@ extern ctf_dtdef_t *ctf_dynamic_type (const ctf_dict_t *, ctf_id_t); extern ctf_id_t ctf_add_encoded (ctf_dict_t *, uint32_t, const char *, const ctf_encoding_t *, ctf_kind_t kind); -extern ctf_id_t ctf_add_reftype (ctf_dict_t *, uint32_t, ctf_id_t, - ctf_kind_t kind); extern ctf_ret_t ctf_add_funcobjt_sym_forced (ctf_dict_t *, int is_function, const char *, ctf_id_t); diff --git a/libctf/libctf.ver b/libctf/libctf.ver index dda59ecb115..19cc16ac012 100644 --- a/libctf/libctf.ver +++ b/libctf/libctf.ver @@ -97,7 +97,6 @@ LIBCTF_2.0 { ctf_next_copy; ctf_add_array; - ctf_add_const; ctf_add_decl_tag; ctf_add_decl_type_tag; ctf_add_enum; @@ -108,16 +107,15 @@ LIBCTF_2.0 { ctf_add_function_linkage; ctf_add_integer; ctf_add_pointer; + ctf_add_qualifier; ctf_add_type; ctf_add_typedef; ctf_add_type_tag; - ctf_add_restrict; ctf_add_section_variable; ctf_add_slice; ctf_add_struct; ctf_add_unknown; ctf_add_variable; - ctf_add_volatile; ctf_add_enumerator; ctf_add_member;