return 0;
}
+bool Sema::CheckSingleInitializer(Expr *Init, QualType DeclType) {
+ AssignmentCheckResult result;
+ SourceLocation loc = Init->getLocStart();
+ // Get the type before calling CheckSingleAssignmentConstraints(), since
+ // it can promote the expression.
+ QualType rhsType = Init->getType();
+
+ result = CheckSingleAssignmentConstraints(DeclType, Init);
+
+ // decode the result (notice that extensions still return a type).
+ switch (result) {
+ case Compatible:
+ break;
+ case Incompatible:
+ Diag(loc, diag::err_typecheck_assign_incompatible,
+ DeclType.getAsString(), rhsType.getAsString(),
+ Init->getSourceRange());
+ return true;
+ case PointerFromInt:
+ // check for null pointer constant (C99 6.3.2.3p3)
+ if (!Init->isNullPointerConstant(Context)) {
+ Diag(loc, diag::ext_typecheck_assign_pointer_int,
+ DeclType.getAsString(), rhsType.getAsString(),
+ Init->getSourceRange());
+ return true;
+ }
+ break;
+ case IntFromPointer:
+ Diag(loc, diag::ext_typecheck_assign_pointer_int,
+ DeclType.getAsString(), rhsType.getAsString(),
+ Init->getSourceRange());
+ break;
+ case IncompatiblePointer:
+ Diag(loc, diag::ext_typecheck_assign_incompatible_pointer,
+ DeclType.getAsString(), rhsType.getAsString(),
+ Init->getSourceRange());
+ break;
+ case CompatiblePointerDiscardsQualifiers:
+ Diag(loc, diag::ext_typecheck_assign_discards_qualifiers,
+ DeclType.getAsString(), rhsType.getAsString(),
+ Init->getSourceRange());
+ break;
+ }
+ return false;
+}
+
+QualType Sema::CheckInitializer(Expr *Init, QualType DeclType) {
+ InitListExpr *InitList = dyn_cast<InitListExpr>(Init);
+ if (!InitList) {
+ return CheckSingleInitializer(Init, DeclType) ? QualType() : DeclType;
+ }
+ // We have an InitListExpr, make sure we set the type.
+ Init->setType(DeclType);
+ // FIXME: Lot of checking still to do...
+ return DeclType;
+}
+
Sema::DeclTy *
Sema::ParseDeclarator(Scope *S, Declarator &D, ExprTy *init,
DeclTy *lastDeclarator) {
}
}
NewVD = new FileVarDecl(D.getIdentifierLoc(), II, R, SC, LastDeclarator);
- } else {
+ } else {
+ if (Init) {
+ CheckInitializer(Init, R);
+ }
// Block scope. C99 6.7p7: If an identifier for an object is declared with
// no linkage (C99 6.2.2p6), the type for the object shall be complete...
if (SC != VarDecl::Extern) {
NewVD = MergeVarDecl(NewVD, PrevDecl);
if (NewVD == 0) return 0;
}
- if (Init) {
- AssignmentCheckResult result;
- result = CheckSingleAssignmentConstraints(R, Init);
- // FIXME: emit errors if appropriate.
+ if (Init) { // FIXME: This will likely move up above...for now, it stays.
NewVD->setInit(Init);
}
New = NewVD;
Action::ExprResult Sema::
ParseInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
SourceLocation RBraceLoc) {
-// Expr **InitList = reinterpret_cast<Expr**>(initlist);
+ 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).
- //return new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
- return false; // FIXME instantiate an InitListExpr.
+ return new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
}
Action::ExprResult Sema::
--- /dev/null
+// RUN: clang -parse-ast-check -pedantic %s
+
+void func() {
+ int x = 1;
+
+ //int x2[] = { 1, 3, 5 };
+
+ int x3[x] = { 1, 2 }; // gcc-error {{variable-sized object may not be initialized}}
+
+ int x4 = { 1, 2 }; // gcc-warning {{excess elements in array initializer}}
+
+ int y[4][3] = {
+ { 1, 3, 5 },
+ { 2, 4, 6 },
+ { 3, 5, 7 },
+ };
+
+ int y2[4][3] = {
+ 1, 3, 5, 2, 4, 6, 3, 5, 7
+ };
+
+ struct threeElements {
+ int a,b,c;
+ } z = { 1 };
+
+ struct threeElements *p = 7; // expected-warning{{incompatible types assigning 'int' to 'struct threeElements *'}}
+}