]> granicus.if.org Git - clang/commitdiff
Implement C99 6.5.3.4p1, rejecting sizeof(bitfield)
authorChris Lattner <sabre@nondot.org>
Sat, 24 Jan 2009 21:29:22 +0000 (21:29 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 24 Jan 2009 21:29:22 +0000 (21:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62936 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticKinds.def
lib/Sema/SemaExpr.cpp
test/Sema/exprs.c

index a2a1ca4c9fa57343401e301a0cf4a23a24dae5fa..13f5403745111b865d08df90ef9df395f3596516 100644 (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,
index 56ad4b5f26175458b8df42e39438cdb192697294..d4babb4baa4f945ad43c1f63e616077322dc1ce2 100644 (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);
index 5413757df79a558c25505cd86ce78c339190266d..db2daf1f7ca875b465a143ad3b3c64a220db7861 100644 (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;
 }