Implement C99 6.5.3.4p1, rejecting sizeof(bitfield)

llvm-svn: 62936
This commit is contained in:
Chris Lattner 2009-01-24 21:29:22 +00:00
parent 4723fb70a9
commit 5eca6ada3a
3 changed files with 15 additions and 8 deletions

View File

@ -1192,8 +1192,8 @@ DIAG(err_sizeof_incomplete_type, ERROR,
"invalid application of 'sizeof' to an incomplete type %0")
DIAG(err_alignof_incomplete_type, ERROR,
"invalid application of '__alignof' to an incomplete type %0")
DIAG(err_alignof_bitfield, ERROR,
"invalid application of '__alignof' to bitfield")
DIAG(err_sizeof_alignof_bitfield, ERROR,
"invalid application of '%select{sizeof|__alignof}0' to bitfield")
DIAG(err_offsetof_record_type, ERROR,
"offsetof requires struct, union, or class type, %0 invalid")
DIAG(err_offsetof_array_type, ERROR,

View File

@ -1046,7 +1046,7 @@ bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc,
if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) {
if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) {
if (FD->isBitField()) {
Diag(OpLoc, diag::err_alignof_bitfield) << ExprRange;
Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange;
return true;
}
// Other fields are ok.
@ -1082,10 +1082,14 @@ Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType,
// Verify that the operand is valid.
bool isInvalid;
if (isSizeof)
isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true);
else
if (!isSizeof) {
isInvalid = CheckAlignOfExpr(ArgEx, OpLoc, Range);
} else if (ArgEx->isBitField()) { // C99 6.5.3.4p1.
Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0;
isInvalid = true;
} else {
isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true);
}
if (isInvalid) {
DeleteExpr(ArgEx);

View File

@ -50,7 +50,10 @@ int test8(void) {
// PR3386
struct f { int x : 4; float y[]; };
int test9(struct f *P) {
return __alignof(P->x) + // expected-error {{invalid application of '__alignof' to bitfield}} expected-warning {{extension used}}
__alignof(P->y); // ok. expected-warning {{extension used}}
int R;
R = __alignof(P->x); // expected-error {{invalid application of '__alignof' to bitfield}} expected-warning {{extension used}}
R = __alignof(P->y); // ok. expected-warning {{extension used}}
R = sizeof(P->x); // expected-error {{invalid application of 'sizeof' to bitfield}}
return R;
}