diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp index 4eae308ef5b34..b1fd93b23f38c 100644 --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -40,7 +40,11 @@ static_assert( "Type is insufficiently aligned"); APValue::LValueBase::LValueBase(const ValueDecl *P, unsigned I, unsigned V) - : Ptr(P ? cast(P->getCanonicalDecl()) : nullptr), Local{I, V} {} + : Ptr(P ? cast(P->getCanonicalDecl()->isInvalidDecl() + ? P + : P->getCanonicalDecl()) + : nullptr), + Local{I, V} {} APValue::LValueBase::LValueBase(const Expr *P, unsigned I, unsigned V) : Ptr(P), Local{I, V} {} @@ -73,7 +77,7 @@ QualType APValue::LValueBase::getType() const { for (auto *Redecl = cast(D->getMostRecentDecl()); Redecl; Redecl = cast_or_null(Redecl->getPreviousDecl())) { QualType T = Redecl->getType(); - if (!T->isIncompleteArrayType()) + if (!T->isIncompleteArrayType() && !Redecl->isInvalidDecl()) return T; } return D->getType(); diff --git a/clang/test/AST/invalid-decl-no-crash.cpp b/clang/test/AST/invalid-decl-no-crash.cpp new file mode 100644 index 0000000000000..2b35ef702ae55 --- /dev/null +++ b/clang/test/AST/invalid-decl-no-crash.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -fsyntax-only %s -verify + +a[i] = b[i]; // expected-error {{use of undeclared identifier 'i'}} \ + expected-error {{a type specifier is required for all declarations}} \ + expected-error {{use of undeclared identifier 'b'}} \ + expected-error {{use of undeclared identifier 'i'}} +extern char b[]; +extern char a[]; + +void foo(int j) { + a[j] = b[j]; +}