Generate code for static variables that don't have initializers. Also, report an error if a static initializer is not constant.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@43058 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Sema/Sema.h b/Sema/Sema.h
index bb5d07f..abe9250 100644
--- a/Sema/Sema.h
+++ b/Sema/Sema.h
@@ -623,7 +623,8 @@
/// type checking declaration initializers (C99 6.7.8)
bool CheckInitializer(Expr *&simpleInit_or_initList, QualType &declType,
bool isStatic);
- bool CheckSingleInitializer(Expr *&simpleInit, QualType declType);
+ bool CheckSingleInitializer(Expr *&simpleInit, bool isStatic,
+ QualType declType);
bool CheckInitExpr(Expr *expr, InitListExpr *IList, unsigned slot,
bool isStatic, QualType ElementType);
void CheckVariableInitList(QualType DeclType, InitListExpr *IList,
diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp
index 9b2800f..390caaf 100644
--- a/Sema/SemaDecl.cpp
+++ b/Sema/SemaDecl.cpp
@@ -299,9 +299,20 @@
return 0;
}
-bool Sema::CheckSingleInitializer(Expr *&Init, QualType DeclType) {
+bool Sema::CheckSingleInitializer(Expr *&Init, bool isStatic,
+ QualType DeclType) {
+ SourceLocation loc;
+
+ // FIXME: Remove the isReferenceType check and handle assignment
+ // to a reference.
+ if (isStatic && !DeclType->isReferenceType() &&
+ !Init->isConstantExpr(Context, &loc)) { // C99 6.7.8p4.
+ Diag(loc, diag::err_init_element_not_constant, Init->getSourceRange());
+ return true;
+ }
+
AssignmentCheckResult result;
- SourceLocation loc = Init->getLocStart();
+ loc = Init->getLocStart();
// Get the type before calling CheckSingleAssignmentConstraints(), since
// it can promote the expression.
QualType rhsType = Init->getType();
@@ -357,7 +368,7 @@
if (isStatic && !expr->isConstantExpr(Context, &loc)) { // C99 6.7.8p4.
Diag(loc, diag::err_init_element_not_constant, expr->getSourceRange());
return true;
- } else if (CheckSingleInitializer(expr, ElementType)) {
+ } else if (CheckSingleInitializer(expr, isStatic, ElementType)) {
return true; // types weren't compatible.
}
if (savExpr != expr) // The type was promoted, update initializer list.
@@ -437,8 +448,8 @@
bool Sema::CheckInitializer(Expr *&Init, QualType &DeclType, bool isStatic) {
InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
if (!InitList)
- return CheckSingleInitializer(Init, DeclType);
-
+ return CheckSingleInitializer(Init, isStatic, DeclType);
+
// We have an InitListExpr, make sure we set the type.
Init->setType(DeclType);