mirror of
https://github.com/intel/llvm.git
synced 2026-01-16 05:32:28 +08:00
[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:
@@ -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;
|
||||
|
||||
8
clang/test/Sema/init-invalid-struct-array.c
Normal file
8
clang/test/Sema/init-invalid-struct-array.c
Normal 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, {}};
|
||||
Reference in New Issue
Block a user