aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-01-24 21:29:22 +0000
committerChris Lattner <sabre@nondot.org>2009-01-24 21:29:22 +0000
commitda0274725667d1168867dc404417f2c68c8dc0c5 (patch)
treef46afbd619a7f0f67bfd7c056512c8ff73a8b06d
parent84394a50e307e2f056e270e1eeadd4f26913cd1e (diff)
Implement C99 6.5.3.4p1, rejecting sizeof(bitfield)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62936 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/clang/Basic/DiagnosticKinds.def4
-rw-r--r--lib/Sema/SemaExpr.cpp12
-rw-r--r--test/Sema/exprs.c7
3 files changed, 15 insertions, 8 deletions
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def
index a2a1ca4c9f..13f5403745 100644
--- a/include/clang/Basic/DiagnosticKinds.def
+++ b/include/clang/Basic/DiagnosticKinds.def
@@ -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,
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index 56ad4b5f26..d4babb4baa 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -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);
diff --git a/test/Sema/exprs.c b/test/Sema/exprs.c
index 5413757df7..db2daf1f7c 100644
--- a/test/Sema/exprs.c
+++ b/test/Sema/exprs.c
@@ -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;
}