From mboxrd@z Thu Jan 1 00:00:00 1970 Content-Type: multipart/mixed; boundary="===============1300842659638716388==" MIME-Version: 1.0 From: Chih-Hung Hsieh To: elfutils-devel@lists.fedorahosted.org Subject: [PATCH] Move nested functions in libdw_visit_scopes.c to file scope. Date: Wed, 14 Oct 2015 12:23:34 -0700 Message-ID: <1444850614-38944-1-git-send-email-chh@google.com> --===============1300842659638716388== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable * No nested functions to compile with clang/llvm. Signed-off-by: Chih-Hung Hsieh --- libdw/ChangeLog | 5 ++ libdw/libdw_visit_scopes.c | 163 +++++++++++++++++++++++++----------------= ---- 2 files changed, 95 insertions(+), 73 deletions(-) diff --git a/libdw/ChangeLog b/libdw/ChangeLog index 39c49aa..56ee808 100644 --- a/libdw/ChangeLog +++ b/libdw/ChangeLog @@ -1,3 +1,8 @@ +2015-10-14 Chih-Hung Hsieh + + * libdw_visit_scopes.c (__libdw_visit_scopes): Move recursive nested + function 'walk_children' to file scope; inline 'recurse' at its call site. + 2015-10-09 Josh Stone = * dwarf_begin.c (dwarf_begin): Replace stat64 and fstat64 with stat diff --git a/libdw/libdw_visit_scopes.c b/libdw/libdw_visit_scopes.c index 5e5c26f..9cd5bad 100644 --- a/libdw/libdw_visit_scopes.c +++ b/libdw/libdw_visit_scopes.c @@ -64,6 +64,22 @@ may_have_scopes (Dwarf_Die *die) return false; } = +struct walk_children_state { + /* Parameters of __libdw_visit_scopes. */ + unsigned int depth; + struct Dwarf_Die_Chain *imports; + int (*previsit) (unsigned int depth, struct Dwarf_Die_Chain *, void *); + int (*postvisit) (unsigned int depth, struct Dwarf_Die_Chain *, void *); + void *arg; + /* Return value of recursive walker. */ + int ret; + /* Extra local variables for the walker. */ + struct Dwarf_Die_Chain child; +}; + +static inline int +walk_children (struct walk_children_state *state); + int internal_function __libdw_visit_scopes (unsigned int depth, struct Dwarf_Die_Chain *root, @@ -76,95 +92,96 @@ __libdw_visit_scopes (unsigned int depth, struct Dwarf_= Die_Chain *root, void *), void *arg) { - struct Dwarf_Die_Chain child; - int ret; - - child.parent =3D root; - if ((ret =3D INTUSE(dwarf_child) (&root->die, &child.die)) !=3D 0) - return ret < 0 ? -1 : 0; // Having zero children is legal. - - inline int recurse (void) + struct walk_children_state state =3D { - return __libdw_visit_scopes (depth + 1, &child, imports, - previsit, postvisit, arg); - } - - /* Checks the given DIE hasn't been imported yet to prevent cycles. */ - inline bool imports_contains (Dwarf_Die *die) - { - for (struct Dwarf_Die_Chain *import =3D imports; import !=3D NULL; - import =3D import->parent) - if (import->die.addr =3D=3D die->addr) - return true; - - return false; - } + .depth =3D depth, + .imports =3D imports, + .previsit =3D previsit, + .postvisit =3D postvisit, + .arg =3D arg + }; + + state.child.parent =3D root; + if ((state.ret =3D INTUSE(dwarf_child) (&root->die, &state.child.die)) != =3D 0) + return state.ret < 0 ? -1 : 0; // Having zero children is legal. + + return walk_children (&state); +} = - inline int walk_children (void) +static inline int +walk_children (struct walk_children_state *state) { - do - { - /* For an imported unit, it is logically as if the children of - that unit are siblings of the other children. So don't do - a full recursion into the imported unit, but just walk the - children in place before moving to the next real child. */ - while (INTUSE(dwarf_tag) (&child.die) =3D=3D DW_TAG_imported_unit) - { - Dwarf_Die orig_child_die =3D child.die; - Dwarf_Attribute attr_mem; - Dwarf_Attribute *attr =3D INTUSE(dwarf_attr) (&child.die, - DW_AT_import, - &attr_mem); - if (INTUSE(dwarf_formref_die) (attr, &child.die) !=3D NULL - && INTUSE(dwarf_child) (&child.die, &child.die) =3D=3D 0) - { - if (imports_contains (&orig_child_die)) - { - __libdw_seterrno (DWARF_E_INVALID_DWARF); - return -1; - } - struct Dwarf_Die_Chain *orig_imports =3D imports; - struct Dwarf_Die_Chain import =3D { .die =3D orig_child_die, - .parent =3D orig_imports }; - imports =3D &import; - int result =3D walk_children (); - imports =3D orig_imports; - if (result !=3D DWARF_CB_OK) - return result; - } - - /* Any "real" children left? */ - if ((ret =3D INTUSE(dwarf_siblingof) (&orig_child_die, - &child.die)) !=3D 0) - return ret < 0 ? -1 : 0; - }; - - child.prune =3D false; + do + { + /* For an imported unit, it is logically as if the children of + that unit are siblings of the other children. So don't do + a full recursion into the imported unit, but just walk the + children in place before moving to the next real child. */ + while (INTUSE(dwarf_tag) (&state->child.die) =3D=3D DW_TAG_imported_= unit) + { + Dwarf_Die orig_child_die =3D state->child.die; + Dwarf_Attribute attr_mem; + Dwarf_Attribute *attr =3D INTUSE(dwarf_attr) (&state->child.die, + DW_AT_import, + &attr_mem); + if (INTUSE(dwarf_formref_die) (attr, &state->child.die) !=3D NULL + && INTUSE(dwarf_child) (&state->child.die, &state->child.die) =3D= =3D 0) + { + /* Checks the given DIE hasn't been imported yet + to prevent cycles. */ + bool imported =3D false; + for (struct Dwarf_Die_Chain *import =3D state->imports; import !=3D= NULL; + import =3D import->parent) + if (import->die.addr =3D=3D orig_child_die.addr) + { + imported =3D true; + break; + } + if (imported) + { + __libdw_seterrno (DWARF_E_INVALID_DWARF); + return -1; + } + struct Dwarf_Die_Chain *orig_imports =3D state->imports; + struct Dwarf_Die_Chain import =3D { .die =3D orig_child_die, + .parent =3D orig_imports }; + state->imports =3D &import; + int result =3D walk_children (state); + state->imports =3D orig_imports; + if (result !=3D DWARF_CB_OK) + return result; + } + + /* Any "real" children left? */ + if ((state->ret =3D INTUSE(dwarf_siblingof) (&orig_child_die, + &state->child.die)) !=3D 0) + return state->ret < 0 ? -1 : 0; + }; + + state->child.prune =3D false; = /* previsit is declared NN */ - int result =3D (*previsit) (depth + 1, &child, arg); + int result =3D (*state->previsit) (state->depth + 1, &state->child, state= ->arg); if (result !=3D DWARF_CB_OK) return result; = - if (!child.prune && may_have_scopes (&child.die) - && INTUSE(dwarf_haschildren) (&child.die)) + if (!state->child.prune && may_have_scopes (&state->child.die) + && INTUSE(dwarf_haschildren) (&state->child.die)) { - result =3D recurse (); + result =3D __libdw_visit_scopes (state->depth + 1, &state->child, sta= te->imports, + state->previsit, state->postvisit, state->arg); if (result !=3D DWARF_CB_OK) return result; } = - if (postvisit !=3D NULL) + if (state->postvisit !=3D NULL) { - result =3D (*postvisit) (depth + 1, &child, arg); + result =3D (*state->postvisit) (state->depth + 1, &state->child, stat= e->arg); if (result !=3D DWARF_CB_OK) return result; } - } - while ((ret =3D INTUSE(dwarf_siblingof) (&child.die, &child.die)) =3D= =3D 0); - - return ret < 0 ? -1 : 0; - } + } + while ((state->ret =3D INTUSE(dwarf_siblingof) (&state->child.die, &stat= e->child.die)) =3D=3D 0); = - return walk_children (); + return state->ret < 0 ? -1 : 0; } -- = 2.6.0.rc2.230.g3dd15c0 --===============1300842659638716388==--