This is an archive of the discontinued LLVM Phabricator instance.

Detect an incompatible VLA pointer assignment
ClosedPublic

Authored by jmorse on Jun 1 2018, 6:05 AM.

Details

Summary

For pointer assignments of VLA types, Clang currently detects when array
dimensions _lower_ than a variable dimension differ, and reports a warning.
However it does not do the same when the _higher_ dimensions differ, a
case that GCC does catch.

These two pointer types

int (*foo)[1][bar][3];
int (*baz)[1][2][3];

are compatible with each another, and the program is well formed if
bar == 2, a matter that is the programmers problem. However the following:

int (*qux)[2][2][3];

would not be compatible with either, because the upper dimension differs
in size. Clang reports baz is incompatible with qux, but not that foo is
incompatible with qux because it doesn't check those higher dimensions.

Fix this by comparing array sizes on higher dimensions: if both are
constants but unequal then report incompatibility; if either dimension is
variable then we can't know either way.

Diff Detail

Repository
rL LLVM

Event Timeline

jmorse created this revision.Jun 1 2018, 6:05 AM
efriedma added inline comments.
lib/AST/ASTContext.cpp
8588 ↗(On Diff #149436)

E && E->isIntegerConstantExpr?

8603 ↗(On Diff #149436)

The != will hit an assertion failure if LSize and RSize don't have the same bitwidth.

jmorse updated this revision to Diff 149688.Jun 4 2018, 2:13 AM

Avoid un-necessary call, use APInt::isSameValue rather than operator!=

jmorse marked 2 inline comments as done.Jun 4 2018, 2:14 AM
jmorse added inline comments.
lib/AST/ASTContext.cpp
8588 ↗(On Diff #149436)

Done

8603 ↗(On Diff #149436)

Erk -- replaced with APInt::isSameValue.

This revision is now accepted and ready to land.Jun 4 2018, 10:48 AM
This revision was automatically updated to reflect the committed changes.
jmorse marked 2 inline comments as done.