Hi!

I've got something working:

        $ cat len.c 
        #include <stdio.h>

        int
        main(int argc, char *argv[argc + 1])
        {
                int     a[42];
                size_t  n;

                (void) argv;

                //n = _Lengthof(argv);
                //printf("_Lengthof(argv) == %zu\n", n);

                n = _Lengthof(a);
                printf("%zu\n", n);

                n = _Lengthof(long [99]);
                printf("%zu\n", n);

                n = _Lengthof(short [n - 10]);
                printf("%zu\n", n);

                int  b[n / 2];
                n = _Lengthof(b);
                printf("%zu\n", n);
        }
        $ /opt/local/gnu/gcc/lengthof/bin/gcc -Wall -Wextra len.c 
        $ ./a.out 
        42
        99
        89
        44

I'd like to be able to uncomment those two lines in the future, but we
probably want to do that in a separate commit.

Does anyone know if we have the information available for getting that
value from the 'tree' object?  Or do we need some refactor first in
order to keep that information?

Also, I've reused much of sizeof's code, and maybe I should change some
of that.  It's my first contribution to gcc of this size, and I don't
yet know what some of those internals mean.  While my implementation
seems to be working (for the test above), I guess it's not ideal in
terms of readability, and may also not work well with C++ or some corner
cases.  Can somebody with more experience have a look at the code?

I suspect this is not yet ready, and thus doesn't have a ChangeLog.

Have a lovely day!
Alex


Alejandro Colomar (2):
  Merge definitions of array_type_nelts_top()
  c: Add _Lengthof() operator

 gcc/Makefile.in               |  1 +
 gcc/c-family/c-common.cc      | 20 +++++++++
 gcc/c-family/c-common.def     |  4 ++
 gcc/c-family/c-common.h       |  2 +
 gcc/c/c-parser.cc             | 35 +++++++++++----
 gcc/c/c-tree.h                |  4 ++
 gcc/c/c-typeck.cc             | 84 +++++++++++++++++++++++++++++++++++
 gcc/cp/cp-tree.h              |  1 -
 gcc/cp/operators.def          |  1 +
 gcc/cp/tree.cc                | 13 ------
 gcc/ginclude/stdlength.h      | 35 +++++++++++++++
 gcc/rust/backend/rust-tree.cc | 13 ------
 gcc/rust/backend/rust-tree.h  |  2 -
 gcc/target.h                  |  3 ++
 gcc/tree.cc                   | 13 ++++++
 gcc/tree.h                    |  1 +
 16 files changed, 195 insertions(+), 37 deletions(-)
 create mode 100644 gcc/ginclude/stdlength.h

Range-diff against v0:
-:  ----------- > 1:  507f5a51e17 Merge definitions of array_type_nelts_top()
-:  ----------- > 2:  e5835b982af c: Add _Lengthof() operator
-- 
2.45.2

Attachment: signature.asc
Description: PGP signature

Reply via email to