[llvm-dev] Identify c-style pointer to array

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Tue Jul 25 06:49:42 PDT 2017


Hi Alex,

On 24 July 2017 at 23:28, alex via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> As this style of passing arrays using pointers is widely used, I wonder
> if there is any chance to identify a pointer type function argument (or
> even pointer to pointer in the case of a 2d array) as array?

Not usually. C has a rarely used feature to tell the compiler how big
an array going into a function is:

    void foo(int arr[static 5]) {}

With this Clang will add a "dereferenceable(20)" attribute to the
parameter that does roughly what you're asking. But without that
you're not allowed to assume anything about the "array" so even
knowing it was an array is largely pointless at the LLVM level.

Another alternative if you control the C is to pass a pointer to an array:

    void foo(int (*arr)[5]) {}

This will come through to LLVM as "[5 x i32]*" which you can in the type system.

Cheers.

Tim.


More information about the llvm-dev mailing list