forked from OSchip/llvm-project
[clang] Never wrap a nullptr in CXXNewExpr::getArraySize()
Otherwise callers of these functions have to check both the return value for and the contents of the returned llvm::Optional. Fixes #53742 Differential Revision: https://reviews.llvm.org/D119525
This commit is contained in:
parent
5c4f749429
commit
f8cedc642d
|
@ -54,6 +54,14 @@ Major New Features
|
||||||
There is an analogous ``zero_call_used_regs`` attribute to allow for finer
|
There is an analogous ``zero_call_used_regs`` attribute to allow for finer
|
||||||
control of this feature.
|
control of this feature.
|
||||||
|
|
||||||
|
Bug Fixes
|
||||||
|
------------------
|
||||||
|
- ``CXXNewExpr::getArraySize()`` previously returned a ``llvm::Optional``
|
||||||
|
wrapping a ``nullptr`` when the ``CXXNewExpr`` did not have an array
|
||||||
|
size expression. This was fixed and ``::getArraySize()`` will now always
|
||||||
|
either return ``None`` or a ``llvm::Optional`` wrapping a valid ``Expr*``.
|
||||||
|
This fixes `Issue #53742<https://github.com/llvm/llvm-project/issues/53742>`_.
|
||||||
|
|
||||||
Improvements to Clang's diagnostics
|
Improvements to Clang's diagnostics
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
@ -83,7 +91,8 @@ Attribute Changes in Clang
|
||||||
- Added support for parameter pack expansion in `clang::annotate`.
|
- Added support for parameter pack expansion in `clang::annotate`.
|
||||||
|
|
||||||
- The ``overloadable`` attribute can now be written in all of the syntactic
|
- The ``overloadable`` attribute can now be written in all of the syntactic
|
||||||
locations a declaration attribute may appear. Fixes PR53805.
|
locations a declaration attribute may appear.
|
||||||
|
This fixes `Issue #53805<https://github.com/llvm/llvm-project/issues/53805>`_.
|
||||||
|
|
||||||
Windows Support
|
Windows Support
|
||||||
---------------
|
---------------
|
||||||
|
|
|
@ -2261,15 +2261,32 @@ public:
|
||||||
|
|
||||||
bool isArray() const { return CXXNewExprBits.IsArray; }
|
bool isArray() const { return CXXNewExprBits.IsArray; }
|
||||||
|
|
||||||
|
/// This might return None even if isArray() returns true,
|
||||||
|
/// since there might not be an array size expression.
|
||||||
|
/// If the result is not-None, it will never wrap a nullptr.
|
||||||
Optional<Expr *> getArraySize() {
|
Optional<Expr *> getArraySize() {
|
||||||
if (!isArray())
|
if (!isArray())
|
||||||
return None;
|
return None;
|
||||||
return cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]);
|
|
||||||
|
if (auto *Result =
|
||||||
|
cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
|
||||||
|
return Result;
|
||||||
|
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This might return None even if isArray() returns true,
|
||||||
|
/// since there might not be an array size expression.
|
||||||
|
/// If the result is not-None, it will never wrap a nullptr.
|
||||||
Optional<const Expr *> getArraySize() const {
|
Optional<const Expr *> getArraySize() const {
|
||||||
if (!isArray())
|
if (!isArray())
|
||||||
return None;
|
return None;
|
||||||
return cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]);
|
|
||||||
|
if (auto *Result =
|
||||||
|
cast_or_null<Expr>(getTrailingObjects<Stmt *>()[arraySizeOffset()]))
|
||||||
|
return Result;
|
||||||
|
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned getNumPlacementArgs() const {
|
unsigned getNumPlacementArgs() const {
|
||||||
|
|
|
@ -9427,7 +9427,7 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
|
||||||
bool ValueInit = false;
|
bool ValueInit = false;
|
||||||
|
|
||||||
QualType AllocType = E->getAllocatedType();
|
QualType AllocType = E->getAllocatedType();
|
||||||
if (Optional<const Expr*> ArraySize = E->getArraySize()) {
|
if (Optional<const Expr *> ArraySize = E->getArraySize()) {
|
||||||
const Expr *Stripped = *ArraySize;
|
const Expr *Stripped = *ArraySize;
|
||||||
for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
|
for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
|
||||||
Stripped = ICE->getSubExpr())
|
Stripped = ICE->getSubExpr())
|
||||||
|
|
|
@ -2132,10 +2132,10 @@ void StmtPrinter::VisitCXXNewExpr(CXXNewExpr *E) {
|
||||||
if (E->isParenTypeId())
|
if (E->isParenTypeId())
|
||||||
OS << "(";
|
OS << "(";
|
||||||
std::string TypeS;
|
std::string TypeS;
|
||||||
if (Optional<Expr *> Size = E->getArraySize()) {
|
if (E->isArray()) {
|
||||||
llvm::raw_string_ostream s(TypeS);
|
llvm::raw_string_ostream s(TypeS);
|
||||||
s << '[';
|
s << '[';
|
||||||
if (*Size)
|
if (Optional<Expr *> Size = E->getArraySize())
|
||||||
(*Size)->printPretty(s, Helper, Policy);
|
(*Size)->printPretty(s, Helper, Policy);
|
||||||
s << ']';
|
s << ']';
|
||||||
}
|
}
|
||||||
|
|
|
@ -11912,9 +11912,9 @@ TreeTransform<Derived>::TransformCXXNewExpr(CXXNewExpr *E) {
|
||||||
|
|
||||||
// Transform the size of the array we're allocating (if any).
|
// Transform the size of the array we're allocating (if any).
|
||||||
Optional<Expr *> ArraySize;
|
Optional<Expr *> ArraySize;
|
||||||
if (Optional<Expr *> OldArraySize = E->getArraySize()) {
|
if (E->isArray()) {
|
||||||
ExprResult NewArraySize;
|
ExprResult NewArraySize;
|
||||||
if (*OldArraySize) {
|
if (Optional<Expr *> OldArraySize = E->getArraySize()) {
|
||||||
NewArraySize = getDerived().TransformExpr(*OldArraySize);
|
NewArraySize = getDerived().TransformExpr(*OldArraySize);
|
||||||
if (NewArraySize.isInvalid())
|
if (NewArraySize.isInvalid())
|
||||||
return ExprError();
|
return ExprError();
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
// RUN: %clang_cc1 -fsyntax-only %s -verify
|
||||||
|
|
||||||
|
struct Data {
|
||||||
|
char *a;
|
||||||
|
char *b;
|
||||||
|
bool *c;
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Data in;
|
||||||
|
in.a = new char[](); // expected-error {{cannot determine allocated array size from initializer}}
|
||||||
|
in.c = new bool[100]();
|
||||||
|
in.b = new char[100]();
|
||||||
|
}
|
Loading…
Reference in New Issue