[clang] Check `expr` inside `InitListChecker::UpdateStructuredListElement()`

- Prevent nullptr-deference at try to emit warning for invalid `expr`
- Simplify `InitListChecker::UpdateStructuredListElement()` usages. We do not need to check `expr` and increment `StructuredIndex` (for invalid `expr`) before the call anymore.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D85193
This commit is contained in:
Aleksandr Platonov 2020-08-12 22:10:54 +03:00
parent d6f0600c96
commit 3fa0a039ab
2 changed files with 17 additions and 15 deletions

View File

@ -1585,10 +1585,7 @@ void InitListChecker::CheckScalarType(const InitializedEntity &Entity,
IList->setInit(Index, ResultExpr);
}
}
if (hadError)
++StructuredIndex;
else
UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
++Index;
}
@ -1643,10 +1640,7 @@ void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
if (!VerifyOnly && expr)
IList->setInit(Index, expr);
if (hadError)
++StructuredIndex;
else
UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
++Index;
}
@ -1697,11 +1691,7 @@ void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
IList->setInit(Index, ResultExpr);
}
}
if (hadError)
++StructuredIndex;
else
UpdateStructuredListElement(StructuredList, StructuredIndex,
ResultExpr);
UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
++Index;
return;
}
@ -3100,8 +3090,12 @@ void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
StructuredIndex, expr)) {
// This initializer overwrites a previous initializer. Warn.
diagnoseInitOverride(PrevInit, expr->getSourceRange());
// This initializer overwrites a previous initializer.
// No need to diagnose when `expr` is nullptr because a more relevant
// diagnostic has already been issued and this diagnostic is potentially
// noise.
if (expr)
diagnoseInitOverride(PrevInit, expr->getSourceRange());
}
++StructuredIndex;

View File

@ -0,0 +1,8 @@
// RUN: %clang_cc1 %s -verify -fsyntax-only
struct S {
Unknown u; // expected-error {{unknown type name 'Unknown'}}
int i;
};
// Should not crash
struct S s[] = {[0].i = 0, [1].i = 1, {}};