[cfe-commits] r62928 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def lib/Sema/Sema.h lib/Sema/SemaExpr.cpp test/Sema/exprs.c

Chris Lattner sabre at nondot.org
Sat Jan 24 12:17:12 PST 2009


Author: lattner
Date: Sat Jan 24 14:17:12 2009
New Revision: 62928

URL: http://llvm.org/viewvc/llvm-project?rev=62928&view=rev
Log:
Fix PR3386 by handling GCC's rules for alignof, which are substantially
different than those for sizeof.  Reject alignof(bitfield) like gcc does.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/Sema/exprs.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=62928&r1=62927&r2=62928&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sat Jan 24 14:17:12 2009
@@ -1188,6 +1188,8 @@
      "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_offsetof_record_type, ERROR,
      "offsetof requires struct, union, or class type, %0 invalid")
 DIAG(err_offsetof_array_type, ERROR,

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=62928&r1=62927&r2=62928&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat Jan 24 14:17:12 2009
@@ -1003,6 +1003,7 @@
     ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType,
                            void *TyOrEx, const SourceRange &ArgRange);
 
+  bool CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, const SourceRange &R);
   bool CheckSizeOfAlignOfOperand(QualType type, SourceLocation OpLoc,
                                  const SourceRange &R, bool isSizeof);
 

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=62928&r1=62927&r2=62928&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Jan 24 14:17:12 2009
@@ -1035,6 +1035,27 @@
                                 ExprRange);
 }
 
+bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc,
+                            const SourceRange &ExprRange) {
+  E = E->IgnoreParens();
+  
+  // alignof decl is always ok. 
+  if (isa<DeclRefExpr>(E))
+    return false;
+  
+  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;
+        return true;
+      }
+      // Other fields are ok.
+      return false;
+    }
+  }
+  return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false);
+}
+
 /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and
 /// the same for @c alignof and @c __alignof
 /// Note that the ArgRange is invalid if isType is false.
@@ -1060,7 +1081,13 @@
     ArgTy = ArgEx->getType();
     
     // Verify that the operand is valid.
-    if (CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, isSizeof)) {
+    bool isInvalid;
+    if (isSizeof)
+      isInvalid = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, Range, true);
+    else
+      isInvalid = CheckAlignOfExpr(ArgEx, OpLoc, Range);
+    
+    if (isInvalid) {
       DeleteExpr(ArgEx);
       return ExprError();
     }

Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=62928&r1=62927&r2=62928&view=diff

==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Sat Jan 24 14:17:12 2009
@@ -45,3 +45,12 @@
   __builtin_choose_expr (0, 42, i) = 10;  // expected-warning {{extension used}}
   return i;
 }
+
+
+// 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}}
+}
+





More information about the cfe-commits mailing list