https://github.com/ojhunt requested changes to this pull request.
Ok, I think I have an idea for how we can make this less miserable (please
excuse coding in GitHub comment field).
One thing that _might_ be possible is lowering the `optional<Expr*> ArraySize`
parameter to just an `Expr*` inside the function, e.g. rename (in the
definition only) the `ArraySize` parameter to something like `OptArraySize` and
declaring a local `Expr *ArraySize = OptArraySize.value_or(nullptr)`. That
would remove the need for constant `ArraySize && *ArraySize` checks, and the
`(*ArraySize)->` stuff. In an ideal world that would work, but it is _possible_
that we intentionally get an optional<Expr*> that has the value null as a
signal of something else (some error meaning that we are building an array
alloc but the size expression has an error). So if you do try making that
change, run all the tests with just that bit to make sure it doesn't change
behavior anywhere first.
```cpp
SourceRange ArraySizeDiagRange;
// Note for Roberto - this local variable is just so we don't have to do
.getBegin() everywhere
SourceLocation ArraySizeDiagLoc;
QualType ArraySizeType;
auto UpdateArraySize = [&](Expr *NewSize) {
ArraySize = NewSize;
if (!ArraySize || !*ArraySize) {
ArraySizeDiagRange = TypeRange;
ArraySizeDiagLoc = ArraySizeDiagRange.getBegin();
ArraySizeType = QualType();
}
ArraySizeDiagRange = (*ArraySize)->getSourceRange();
ArraySizeDiagLoc = ArraySizeDiagRange.getBegin();
ArraySizeType = (*ArraySize)->getType();
};
```
Now replace any time we assign to `ArraySize` with `UpdateArraySize(the new
size)`, and all the diagnostics can just use `ArraySizeDiagLoc` and
`ArraySizeDiagRange` instead of the constant conditional nonsense.
https://github.com/llvm/llvm-project/pull/186617
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits