[PATCH] D137082: [clang][Interp] Fix dereferencing arrays with no offset applied

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 1 08:52:42 PDT 2022


tbaeder added a comment.

In D137082#3899032 <https://reviews.llvm.org/D137082#3899032>, @aaron.ballman wrote:

>> There is a difference between a Pointer and a "Pointer to the first element of an array".
>
> I'm pretty confused because this statement is false per the language standard (http://eel.is/c++draft/expr.sub#2). Basically, array subscripting works through pointer arithmetic, so `&array[0]` and `array` (decayed to a pointer) have the same value. Why do we need to offset to get to the first element in the interpreter?

That's just an implementation detail in the `Pointer` class. For primitive arrays, we need the `sizeof(InitMap*)` applied, which happens via `atIndex()`. Otherwise, `deref()` will look at the first few bytes of the `InitMap*` pointer.

I've added some documentation about this in https://reviews.llvm.org/D135750 (and the `MetadataSize` added to `Descriptor` there could be used to clean this up I think).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137082/new/

https://reviews.llvm.org/D137082



More information about the cfe-commits mailing list