Closed ahatanak closed 1 year ago
@llvm/issue-subscribers-clang-frontend
clang doesn't crash if I revert 610ec954e1f81c0e8fcadedcd25afe643f5a094e
@nickdesaulniers
cc @efriedma-quic presumably this is related to the !Ctx.getLangOpts().CPlusPlus11
from the removed guard.
Slightly reduced test case:
struct {
int *ptr;
} const arr[] = {(int *)""};
void f() { *arr; }
The ASTs look the same between language modes.
ArrayExprEvaluator::VisitCXXParenListOrInitListExpr gets called twice; once where Result.isArray
is false for the top most InitListExpr. The second time it's true for c++98 but still false for c++11. I wonder why that is? (digging)
Not really sure where Result
is getting reset.
gdb watch points seem to think that performLifetimeExtension
is somehow resetting these objects, but if I add print statements to that code or breakpoints to that function, it seems they are never run.
@efriedma-quic can we just delete the assertion? Removing it doesn't seem to cause any test failures for clang.
The assertion was added in 1b9f2eb76; I think the idea is that zero-initialization is supposed to happen at a particular point, and not later.
If Result.getArrayInitializedElts()
is non-zero, that means there's something in the array already; what are the elements?
Result.dump()
:
Array size=1
`-element: Struct
`-field: LValue <todo>
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 60c80f2b0753..6109829cf20a 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2555,10 +2555,10 @@ APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes,
bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes,
IsConstantInitialization);
- // In C++11, this isn't a constant initializer if we produced notes. In that
+ // In C++, this isn't a constant initializer if we produced notes. In that
// case, we can't keep the result, because it may only be correct under the
// assumption that the initializer is a constant context.
- if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus11 &&
+ if (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus &&
!Notes.empty())
Result = false;
perhaps?
I believe the code directly below it is what's resetting the value, but only for C++11 (and newer). https://github.com/llvm/llvm-project/blob/0f052a972ebe9bdf2c1eb56bddf6abb04eec50d6/clang/lib/AST/Decl.cpp#L2555-L2569
$ cat test.cpp
$ clang++ -std=c++98 -c test.cpp Assertion failed: ((!Result.isArray() || Result.getArrayInitializedElts() == 0) && "zero-initialized array shouldn't have any initialized elts"), function VisitCXXParenListOrInitListExpr, file ExprConstant.cpp, line 11315