diff mbox

[05/13] add get_nth1_arg()

Message ID 20170305112047.3411-6-luc.vanoostenryck@gmail.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Luc Van Oostenryck March 5, 2017, 11:20 a.m. UTC
A backend may need to know the size or the type of an
argument and there is no easy way to access to this info.

Fix this by adding an helper returning the symbol associated
to the nth argument (starting at 1).

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 symbol.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

Christopher Li March 6, 2017, 2:40 p.m. UTC | #1
On Sun, Mar 5, 2017 at 7:20 PM, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> A backend may need to know the size or the type of an
> argument and there is no easy way to access to this info.

Actually, there is a way to do it with two list iterative. Will comment
it on the next patch that use the get_nth1_arg().


> +static inline struct symbol *get_nth1_arg(struct symbol *fn, int idx)
> +{
> +       struct symbol_list *args = fn->ctype.base_type->arguments;
> +       struct symbol *arg;
> +       int i = 0;
> +       FOR_EACH_PTR(args, arg) {
> +               if (++i == idx)
> +                       return arg;
> +       } END_FOR_EACH_PTR(arg);
> +
> +       return NULL;

As I said, I am not sure this is necessary in the particular case.
However, the nth list entry is some what useful. If we add that,
we should add it to ptrlist.c. Then every time of ptr_list can have it.

Also, there is no need to do "++i", it should use the list->nr to skip
over the list node for the case where n is relative large. Then only do the
counting inside the list node. If list is packed, it is every easier, we can
just return from the index. Depend on we want to allow unpacked list
or not.

Chris
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Luc Van Oostenryck March 6, 2017, 4:52 p.m. UTC | #2
On Mon, Mar 06, 2017 at 10:40:17PM +0800, Christopher Li wrote:
> On Sun, Mar 5, 2017 at 7:20 PM, Luc Van Oostenryck
> <luc.vanoostenryck@gmail.com> wrote:
> > A backend may need to know the size or the type of an
> > argument and there is no easy way to access to this info.
> 
> Actually, there is a way to do it with two list iterative. Will comment
> it on the next patch that use the get_nth1_arg().
> 
> 
> > +static inline struct symbol *get_nth1_arg(struct symbol *fn, int idx)
> > +{
> > +       struct symbol_list *args = fn->ctype.base_type->arguments;
> > +       struct symbol *arg;
> > +       int i = 0;
> > +       FOR_EACH_PTR(args, arg) {
> > +               if (++i == idx)
> > +                       return arg;
> > +       } END_FOR_EACH_PTR(arg);
> > +
> > +       return NULL;
> 
> As I said, I am not sure this is necessary in the particular case.
> However, the nth list entry is some what useful. If we add that,
> we should add it to ptrlist.c. Then every time of ptr_list can have it.
> 
> Also, there is no need to do "++i", it should use the list->nr to skip
> over the list node for the case where n is relative large. Then only do the
> counting inside the list node. If list is packed, it is every easier, we can
> just return from the index. Depend on we want to allow unpacked list
> or not.

Absolutely, this can be optimized.

But this serie is a work in progress to help Dibyendu and I won't
otherwise spend much time on sparse-llvm.

It should also be noted that, in general, counting the list entries
via the list->nr can be problematic as some part of the code store
NULL pointers to remove entries (but it's not the case here with
the arguments).

Luc Van Oostenryck
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/symbol.h b/symbol.h
index 36f8345b5..2bc0e21a0 100644
--- a/symbol.h
+++ b/symbol.h
@@ -416,6 +416,19 @@  static inline int get_sym_type(struct symbol *type)
 	return type->type;
 }
 
+static inline struct symbol *get_nth1_arg(struct symbol *fn, int idx)
+{
+	struct symbol_list *args = fn->ctype.base_type->arguments;
+	struct symbol *arg;
+	int i = 0;
+	FOR_EACH_PTR(args, arg) {
+		if (++i == idx)
+			return arg;
+	} END_FOR_EACH_PTR(arg);
+
+	return NULL;
+}
+
 static inline struct symbol *lookup_keyword(struct ident *ident, enum namespace ns)
 {
 	if (!ident->keyword)