[cfe-commits] r41681 - in /cfe/trunk: Sema/Sema.h Sema/SemaDecl.cpp Sema/SemaExpr.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/array-init.c
Steve Naroff
snaroff at apple.com
Sun Sep 2 18:24:23 PDT 2007
Author: snaroff
Date: Sun Sep 2 20:24:23 2007
New Revision: 41681
URL: http://llvm.org/viewvc/llvm-project?rev=41681&view=rev
Log:
Finish getting "array-init.c" to work properly.
Array scalar initialization is now is reasonable shape.
Next step, structure and array of structure initializers.
Modified:
cfe/trunk/Sema/Sema.h
cfe/trunk/Sema/SemaDecl.cpp
cfe/trunk/Sema/SemaExpr.cpp
cfe/trunk/include/clang/Basic/DiagnosticKinds.def
cfe/trunk/test/Sema/array-init.c
Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=41681&r1=41680&r2=41681&view=diff
==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Sun Sep 2 20:24:23 2007
@@ -442,10 +442,11 @@
IdentifierInfo &Comp, SourceLocation CmpLoc);
/// type checking declaration initializers (C99 6.7.8)
- QualType CheckInitializer(Expr *simpleInit_or_initList, QualType declType,
- bool isStatic);
+ bool CheckInitializer(Expr *simpleInit_or_initList, QualType &declType,
+ bool isStatic);
bool CheckSingleInitializer(Expr *simpleInit, QualType declType);
- bool CheckInitList(InitListExpr *IList, QualType DType, bool isStatic);
+ void CheckInitList(InitListExpr *IList, QualType DType, bool isStatic,
+ int &nInitializers, int maxElements, bool &hadError);
/// ConvertIntegerToTypeWarnOnOverflow - Convert the specified APInt to have
/// the specified width and sign. If an overflow occurs, detect it and emit
Modified: cfe/trunk/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaDecl.cpp?rev=41681&r1=41680&r2=41681&view=diff
==============================================================================
--- cfe/trunk/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/Sema/SemaDecl.cpp Sun Sep 2 20:24:23 2007
@@ -289,15 +289,17 @@
return false;
}
-bool Sema::CheckInitList(InitListExpr *IList, QualType DType, bool isStatic) {
- bool hadError = false;
+void Sema::CheckInitList(InitListExpr *IList, QualType DType,
+ bool isStatic, int &nInitializers, int maxElements,
+ bool &hadError) {
for (unsigned i = 0; i < IList->getNumInits(); i++) {
Expr *expr = IList->getInit(i);
if (InitListExpr *InitList = dyn_cast<InitListExpr>(expr))
- CheckInitList(InitList, DType, isStatic);
+ CheckInitList(InitList, DType, isStatic, nInitializers, maxElements,
+ hadError);
else {
- SourceLocation loc;
+ SourceLocation loc = expr->getLocStart();
if (isStatic && !expr->isConstantExpr(Context, &loc)) { // C99 6.7.8p4.
Diag(loc, diag::err_init_element_not_constant, expr->getSourceRange());
@@ -305,41 +307,71 @@
} else if (CheckSingleInitializer(expr, DType)) {
hadError = true; // types didn't match.
}
+ // Does the element fit?
+ nInitializers++;
+ if ((maxElements >= 0) && (nInitializers > maxElements))
+ Diag(loc, diag::warn_excess_initializers, expr->getSourceRange());
}
}
- return hadError;
+ return;
}
-QualType Sema::CheckInitializer(Expr *Init, QualType DeclType, bool isStatic) {
+bool Sema::CheckInitializer(Expr *Init, QualType &DeclType, bool isStatic) {
InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
- if (!InitList) {
- return CheckSingleInitializer(Init, DeclType) ? QualType() : DeclType;
- }
+ if (!InitList)
+ return CheckSingleInitializer(Init, DeclType);
+
// We have an InitListExpr, make sure we set the type.
Init->setType(DeclType);
+
+ bool hadError = false;
+ int nInits = 0;
// C99 6.7.8p3: The type of the entity to be initialized shall be an array
// of unknown size ("[]") or an object type that is not a variable array type.
if (const VariableArrayType *VAT = DeclType->getAsVariableArrayType()) {
Expr *expr = VAT->getSizeExpr();
- if (expr) {
- Diag(expr->getLocStart(), diag::err_variable_object_no_init,
- expr->getSourceRange());
- return QualType();
- }
- }
- if (const ArrayType *Ary = DeclType->getAsArrayType()) {
- // We have a ConstantArrayType or VariableArrayType with unknown size.
- QualType ElmtType = Ary->getElementType();
+ if (expr)
+ return Diag(expr->getLocStart(), diag::err_variable_object_no_init,
+ expr->getSourceRange());
+
+ // We have a VariableArrayType with unknown size.
+ QualType ElmtType = VAT->getElementType();
// If we have a multi-dimensional array, navigate to the base type.
- while ((Ary = ElmtType->getAsArrayType()))
- ElmtType = Ary->getElementType();
-
- CheckInitList(InitList, ElmtType, isStatic);
+ while ((VAT = ElmtType->getAsVariableArrayType())) {
+ ElmtType = VAT->getElementType();
+ }
+ CheckInitList(InitList, ElmtType, isStatic, nInits, -1, hadError);
+
+ if (!hadError) {
+ // Return a new array type from the number of initializers (C99 6.7.8p22).
+ llvm::APSInt ConstVal(32);
+ ConstVal = nInits;
+ DeclType = Context.getConstantArrayType(ElmtType, ConstVal,
+ ArrayType::Normal, 0);
+ }
+ return hadError;
+ }
+ if (const ConstantArrayType *CAT = DeclType->getAsConstantArrayType()) {
+ QualType ElmtType = CAT->getElementType();
+ unsigned numElements = CAT->getSize().getZExtValue();
+
+ // If we have a multi-dimensional array, navigate to the base type. Also
+ // compute the absolute size of the array, so we can detect excess elements.
+ while ((CAT = ElmtType->getAsConstantArrayType())) {
+ ElmtType = CAT->getElementType();
+ numElements *= CAT->getSize().getZExtValue();
+ }
+ CheckInitList(InitList, ElmtType, isStatic, nInits, numElements, hadError);
+ return hadError;
+ }
+ if (DeclType->isScalarType()) { // C99 6.7.8p11
+ CheckInitList(InitList, DeclType, isStatic, nInits, 1, hadError);
+ return hadError;
}
// FIXME: Handle struct/union types.
- return DeclType;
+ return hadError;
}
Sema::DeclTy *
Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=41681&r1=41680&r2=41681&view=diff
==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Sun Sep 2 20:24:23 2007
@@ -644,10 +644,8 @@
SourceLocation RBraceLoc) {
Expr **InitList = reinterpret_cast<Expr**>(initlist);
- // FIXME: add semantic analysis (C99 6.7.8). This involves
- // knowledge of the object being intialized. As a result, the code for
- // doing the semantic analysis will likely be located elsewhere (i.e. in
- // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
+ // Semantic analysis for initializers is done by ParseDeclarator() and
+ // CheckInitializer() - it requires knowledge of the object being intialized.
InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
e->setType(Context.VoidTy); // FIXME: just a place holder for now.
Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=41681&r1=41680&r2=41681&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sun Sep 2 20:24:23 2007
@@ -550,6 +550,8 @@
"'extern' variable has an initializer")
DIAG(err_variable_object_no_init, ERROR,
"variable-sized object may not be initialized")
+DIAG(warn_excess_initializers, WARNING,
+ "excess elements in array initializer")
DIAG(err_redefinition_of_label, ERROR,
"redefinition of label '%0'")
Modified: cfe/trunk/test/Sema/array-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/array-init.c?rev=41681&r1=41680&r2=41681&view=diff
==============================================================================
--- cfe/trunk/test/Sema/array-init.c (original)
+++ cfe/trunk/test/Sema/array-init.c Sun Sep 2 20:24:23 2007
@@ -7,16 +7,16 @@
extern int fileScopeExtern[3] = { 1, 3, 5 }; // expected-warning{{'extern' variable has an initializer}}
-static int ary3[] = { 1, "abc", 3 }; // expected-warning{{incompatible types assigning 'char *' to 'int'}}
+static int ary3[] = { 1, "abc", 3, 4 }; // expected-warning{{incompatible types assigning 'char *' to 'int'}}
void func() {
int x = 1;
- //int x2[] = { 1, 3, 5 };
+ int xComputeSize[] = { 1, 3, 5 };
int x3[x] = { 1, 2 }; // expected-error{{variable-sized object may not be initialized}}
- int x4 = { 1, 2 }; // gcc-warning {{excess elements in array initializer}}
+ int x4 = { 1, 2 }; // // expected-warning{{excess elements in array initializer}}
int y[4][3] = {
{ 1, 3, 5 },
@@ -28,6 +28,14 @@
1, 3, 5, 2, 4, 6, 3, 5, 7
};
+ int y3[4][3] = {
+ { 1, 3, 5 },
+ { 2, 4, 6 },
+ { 3, 5, 7 },
+ { 4, 6, 8 },
+ { 5 }, // expected-warning{{excess elements in array initializer}}
+ };
+
struct threeElements {
int a,b,c;
} z = { 1 };
More information about the cfe-commits
mailing list