From d57fa94148963517a365e5bebdd9c72ab255c3b8 Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Thu, 30 Aug 2007 22:35:45 +0000 Subject: Final phase of array cleanup (for now), removing a FIXME from yesterday. Moved several array constraints checks from Sema::VerifyConstantArrayType() to Sema::GetTypeForDeclarator(). VerifyConstantArrayType() is now very simple, and could be removed eventually. Now, we get the following (correct) messages for BlockVarDecls:-) [dylan:~/llvm/tools/clang] admin% ../../Debug/bin/clang x.c -pedantic x.c:4:20: error: size of array has non-integer type 'float' int size_not_int[f]; ^ x.c:5:21: error: array size is negative int negative_size[1-2]; ^~~ x.c:6:17: warning: zero size arrays are an extension int zero_size[0]; ^ 3 diagnostics generated. llvm-svn: 41624 --- clang/Sema/SemaType.cpp | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'clang/Sema/SemaType.cpp') diff --git a/clang/Sema/SemaType.cpp b/clang/Sema/SemaType.cpp index d1d62a6ea20..17b34628b5f 100644 --- a/clang/Sema/SemaType.cpp +++ b/clang/Sema/SemaType.cpp @@ -202,13 +202,33 @@ QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) { D.setInvalidType(true); } } + // C99 6.7.5.2p1: The size expression shall have integer type. + if (ArraySize && !ArraySize->getType()->isIntegerType()) { + Diag(ArraySize->getLocStart(), diag::err_array_size_non_int, + ArraySize->getType().getAsString(), ArraySize->getSourceRange()); + D.setInvalidType(true); + } llvm::APSInt ConstVal(32); // If no expression was provided, we consider it a VLA. if (!ArraySize || !ArraySize->isIntegerConstantExpr(ConstVal, Context)) T = Context.getVariableArrayType(T, ArraySize, ASM, ATI.TypeQuals); - else + else { + // C99 6.7.5.2p1: If the expression is a constant expression, it shall + // have a value greater than zero. + if (ConstVal.isSigned()) { + if (ConstVal.isNegative()) { + Diag(ArraySize->getLocStart(), + diag::err_typecheck_negative_array_size, + ArraySize->getSourceRange()); + D.setInvalidType(true); + } else if (ConstVal == 0) { + // GCC accepts zero sized static arrays. + Diag(ArraySize->getLocStart(), diag::ext_typecheck_zero_array_size, + ArraySize->getSourceRange()); + } + } T = Context.getConstantArrayType(T, ConstVal, ASM, ATI.TypeQuals); - + } // If this is not C99, extwarn about VLA's and C99 array size modifiers. if (!getLangOptions().C99 && (ASM != ArrayType::Normal || -- cgit v1.2.3