diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-03-09 02:00:01 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-03-09 02:00:01 +0000 |
commit | 69cf59ed26c1a382960dd206ecffe104b45c755f (patch) | |
tree | 0a5253773ef49ed216037278f4b392e3688199e4 | |
parent | ce5f2d3d420a6a399e756b565e5a8890de9c167d (diff) | |
download | bcm5719-llvm-69cf59ed26c1a382960dd206ecffe104b45c755f.tar.gz bcm5719-llvm-69cf59ed26c1a382960dd206ecffe104b45c755f.zip |
PR36645: Go looking for an appropriate array bound when constant-evaluating a
name of an array object.
llvm-svn: 327099
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 16 | ||||
-rw-r--r-- | clang/test/SemaCXX/constant-expression-cxx11.cpp | 4 |
2 files changed, 16 insertions, 4 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index c3e41658c29..851a03aa2ed 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -63,14 +63,22 @@ namespace { static QualType getType(APValue::LValueBase B) { if (!B) return QualType(); - if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) + if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { // FIXME: It's unclear where we're supposed to take the type from, and - // this actually matters for arrays of unknown bound. Using the type of - // the most recent declaration isn't clearly correct in general. Eg: + // this actually matters for arrays of unknown bound. Eg: // // extern int arr[]; void f() { extern int arr[3]; }; // constexpr int *p = &arr[1]; // valid? - return cast<ValueDecl>(D->getMostRecentDecl())->getType(); + // + // For now, we take the array bound from the most recent declaration. + for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl; + Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) { + QualType T = Redecl->getType(); + if (!T->isIncompleteArrayType()) + return T; + } + return D->getType(); + } const Expr *Base = B.get<const Expr*>(); diff --git a/clang/test/SemaCXX/constant-expression-cxx11.cpp b/clang/test/SemaCXX/constant-expression-cxx11.cpp index 51dd6199e63..fe4c54e7e6f 100644 --- a/clang/test/SemaCXX/constant-expression-cxx11.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx11.cpp @@ -629,6 +629,10 @@ namespace ArrayOfUnknownBound { extern const int carr[]; // expected-note {{here}} constexpr int n = carr[0]; // expected-error {{constant}} expected-note {{non-constexpr variable}} + + constexpr int local_extern[] = {1, 2, 3}; + void f() { extern const int local_extern[]; } + static_assert(local_extern[1] == 2, ""); } namespace DependentValues { |