[clang] 3fa0a03 - [clang] Check `expr` inside `InitListChecker::UpdateStructuredListElement()`
Aleksandr Platonov via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 12 12:14:01 PDT 2020
Author: Aleksandr Platonov
Date: 2020-08-12T22:12:43+03:00
New Revision: 3fa0a039ab6f856e39dab973df56831b63ed51c5
URL: https://github.com/llvm/llvm-project/commit/3fa0a039ab6f856e39dab973df56831b63ed51c5
DIFF: https://github.com/llvm/llvm-project/commit/3fa0a039ab6f856e39dab973df56831b63ed51c5.diff
LOG: [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
Added:
clang/test/Sema/init-invalid-struct-array.c
Modified:
clang/lib/Sema/SemaInit.cpp
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index f4cfae0f7d85..f63d600032ce 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -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;
diff --git a/clang/test/Sema/init-invalid-struct-array.c b/clang/test/Sema/init-invalid-struct-array.c
new file mode 100644
index 000000000000..e234d68039c0
--- /dev/null
+++ b/clang/test/Sema/init-invalid-struct-array.c
@@ -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, {}};
More information about the cfe-commits
mailing list