[clang] Fix a crash on invalid auto.

Summary:
The crash is triggered on accessing a null InitExpr.

For group declaration, e.g. `auto c = a, &d = {a};`, what's happening:

1. each VarDecl is built separately during the parsing stage.
2. perform the semantic analysis (Sema::BuildDeclaratorGroup) to check
whether the type of the two VarDecl is the same, if not mark it as invalid.

in step 1, VarDecl c and d are built, both of them are valid (after D77395),
but d is without the InitExpr attached (under -fno-recovery-ast), crash
happens in step 2 when accessing the source range of d's InitExpr.

Reviewers: sammccall

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D79473
This commit is contained in:
Haojian Wu 2020-05-06 08:56:42 +02:00
parent fb4574df52
commit c6e1fd70fb
2 changed files with 18 additions and 7 deletions

View File

@ -13208,13 +13208,15 @@ Sema::BuildDeclaratorGroup(MutableArrayRef<Decl *> Group) {
DeducedDecl = D;
} else if (!Context.hasSameType(DT->getDeducedType(), Deduced)) {
auto *AT = dyn_cast<AutoType>(DT);
Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
auto Dia = Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
diag::err_auto_different_deductions)
<< (AT ? (unsigned)AT->getKeyword() : 3)
<< Deduced << DeducedDecl->getDeclName()
<< DT->getDeducedType() << D->getDeclName()
<< DeducedDecl->getInit()->getSourceRange()
<< D->getInit()->getSourceRange();
<< (AT ? (unsigned)AT->getKeyword() : 3) << Deduced
<< DeducedDecl->getDeclName() << DT->getDeducedType()
<< D->getDeclName();
if (DeducedDecl->hasInit())
Dia << DeducedDecl->getInit()->getSourceRange();
if (D->getInit())
Dia << D->getInit()->getSourceRange();
D->setInvalidDecl();
break;
}

View File

@ -0,0 +1,9 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fno-recovery-ast -verify %s
namespace std {
template <typename>
class initializer_list{};
int a;
auto c = a, &d = {a}; // expected-error {{'auto' deduced as 'int'}} \
expected-error {{non-const lvalue reference to type}}
} // namespace std