From: Richard Smith Date: Mon, 17 Oct 2011 23:29:39 +0000 (+0000) Subject: Perform lvalue-to-rvalue conversions on __builtin_offsetof array argument index X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ea0114313fbfba99f7067a2464c523e96a08e365;p=clang Perform lvalue-to-rvalue conversions on __builtin_offsetof array argument index before typechecking, as suggested by John. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@142308 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index d3dec96d44..549939e1c2 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -8393,22 +8393,22 @@ ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, } else CurrentType = Context.DependentTy; + ExprResult IdxRval = DefaultLvalueConversion(static_cast(OC.U.E)); + if (IdxRval.isInvalid()) + return ExprError(); + Expr *Idx = IdxRval.take(); + // The expression must be an integral expression. // FIXME: An integral constant expression? - Expr *Idx = static_cast(OC.U.E); if (!Idx->isTypeDependent() && !Idx->isValueDependent() && !Idx->getType()->isIntegerType()) return ExprError(Diag(Idx->getLocStart(), diag::err_typecheck_subscript_not_integer) << Idx->getSourceRange()); - ExprResult IdxRvalue = DefaultLvalueConversion(Idx); - if (IdxRvalue.isInvalid()) - return ExprError(); - // Record this array index. Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); - Exprs.push_back(IdxRvalue.take()); + Exprs.push_back(Idx); continue; } diff --git a/test/SemaCXX/offsetof.cpp b/test/SemaCXX/offsetof.cpp index 17cee62d16..a5f5d347c2 100644 --- a/test/SemaCXX/offsetof.cpp +++ b/test/SemaCXX/offsetof.cpp @@ -66,3 +66,10 @@ struct bar : public foo { }; int anonstruct[__builtin_offsetof(bar, x) == 0 ? 1 : -1]; + +struct LtoRCheck { + int a[10]; + int f(); +}; +int ltor = __builtin_offsetof(struct LtoRCheck, a[LtoRCheck().f]); // \ + expected-error {{reference to non-static member function must be called}}