From b529d830b3b1667157da92ab0b5c32e1acfafbab Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 2 Sep 2008 09:37:00 +0000 Subject: [PATCH] Fix for PR2747: allow pointer->int casts with a null base; these are offset-of-like expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55627 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 7 +++++++ test/Sema/init.c | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 7e60da5af3..564f530191 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1163,6 +1163,13 @@ bool Sema::CheckArithmeticConstantExpression(const Expr* Init) { if (SubExpr->getType()->isArithmeticType()) return CheckArithmeticConstantExpression(SubExpr); + if (SubExpr->getType()->isPointerType()) { + const Expr* Base = FindExpressionBaseAddress(SubExpr); + // If the pointer has a null base, this is an offsetof-like construct + if (!Base) + return CheckAddressConstantExpression(SubExpr); + } + Diag(Init->getExprLoc(), diag::err_init_element_not_constant, Init->getSourceRange()); return true; diff --git a/test/Sema/init.c b/test/Sema/init.c index 964118ce02..10eed146a0 100644 --- a/test/Sema/init.c +++ b/test/Sema/init.c @@ -58,3 +58,11 @@ union { float f; unsigned u; } u = { 1.0f }; int f3(int x) { return x; } typedef void (*vfunc)(void); void *bar = (vfunc) f3; + +// PR2747 +struct sym_reg { + char nc_gpreg; +}; +int sym_fw1a_scr[] = { + ((int)(&((struct sym_reg *)0)->nc_gpreg)) & 0 +}; -- 2.40.0