diff options
author | Anders Carlsson <andersca@mac.com> | 2009-02-19 06:30:50 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-02-19 06:30:50 +0000 |
commit | 1c17689a6703331c656ae79bb756bf606d63f71a (patch) | |
tree | 4d7090bb16cd536e006c8cb0359b550e68e1f713 /lib/Sema/SemaDecl.cpp | |
parent | 005b235fbc8449142befa81ec30e8e4c654182c5 (diff) |
Emit the correct diagnostics when we constant fold an array size to a negative value.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65023 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaDecl.cpp')
-rw-r--r-- | lib/Sema/SemaDecl.cpp | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 294be70488..f3eb201fa2 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -3397,11 +3397,14 @@ void Sema::ActOnTagFinishDefinition(Scope *S, DeclTy *TagD) { /// types into constant array types in certain situations which would otherwise /// be errors (for GCC compatibility). static QualType TryToFixInvalidVariablyModifiedType(QualType T, - ASTContext &Context) { + ASTContext &Context, + bool &SizeIsNegative) { // This method tries to turn a variable array into a constant // array even when the size isn't an ICE. This is necessary // for compatibility with code that depends on gcc's buggy // constant expression folding, like struct {char x[(int)(char*)2];} + SizeIsNegative = false; + const VariableArrayType* VLATy = dyn_cast<VariableArrayType>(T); if (!VLATy) return QualType(); @@ -3415,6 +3418,8 @@ static QualType TryToFixInvalidVariablyModifiedType(QualType T, if (Res >= llvm::APSInt(Res.getBitWidth(), Res.isUnsigned())) return Context.getConstantArrayType(VLATy->getElementType(), Res, ArrayType::Normal, 0); + + SizeIsNegative = true; return QualType(); } @@ -3463,12 +3468,17 @@ Sema::DeclTy *Sema::ActOnField(Scope *S, DeclTy *TagD, // C99 6.7.2.1p8: A member of a structure or union may have any type other // than a variably modified type. if (T->isVariablyModifiedType()) { - QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context); + bool SizeIsNegative; + QualType FixedTy = TryToFixInvalidVariablyModifiedType(T, Context, + SizeIsNegative); if (!FixedTy.isNull()) { Diag(Loc, diag::warn_illegal_constant_array_size); T = FixedTy; } else { - Diag(Loc, diag::err_typecheck_field_variable_size); + if (SizeIsNegative) + Diag(Loc, diag::err_typecheck_negative_array_size); + else + Diag(Loc, diag::err_typecheck_field_variable_size); T = Context.IntTy; InvalidDecl = true; } |