From: Chris Lattner Date: Sat, 24 Jan 2009 20:17:12 +0000 (+0000) Subject: Fix PR3386 by handling GCC's rules for alignof, which are substantially X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=31e21e05623ce9d11b1a893fecb87ad349df6c7d;p=clang Fix PR3386 by handling GCC's rules for alignof, which are substantially different than those for sizeof. Reject alignof(bitfield) like gcc does. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62928 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def index 1858f2583b..5cb71fc9bf 100644 --- a/include/clang/Basic/DiagnosticKinds.def +++ b/include/clang/Basic/DiagnosticKinds.def @@ -1188,6 +1188,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_offsetof_record_type, ERROR, "offsetof requires struct, union, or class type, %0 invalid") DIAG(err_offsetof_array_type, ERROR, diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 40cfe8f193..a993ebe2cb 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1003,6 +1003,7 @@ public: 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); diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index fa4c9d620a..56ad4b5f26 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1035,6 +1035,27 @@ bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, ExprRange); } +bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, + const SourceRange &ExprRange) { + E = E->IgnoreParens(); + + // alignof decl is always ok. + if (isa(E)) + return false; + + if (MemberExpr *ME = dyn_cast(E)) { + if (FieldDecl *FD = dyn_cast(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 @@ Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, 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(); } diff --git a/test/Sema/exprs.c b/test/Sema/exprs.c index 73d73ebe7b..5413757df7 100644 --- a/test/Sema/exprs.c +++ b/test/Sema/exprs.c @@ -45,3 +45,12 @@ int test8(void) { __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}} +} +