]> granicus.if.org Git - clang/commitdiff
Fix for PR2747: allow pointer->int casts with a null base; these are
authorEli Friedman <eli.friedman@gmail.com>
Tue, 2 Sep 2008 09:37:00 +0000 (09:37 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 2 Sep 2008 09:37:00 +0000 (09:37 +0000)
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
test/Sema/init.c

index 7e60da5af35b2a8c0f958ef4041d809d281eb557..564f530191dc45a5906f0b74e96d170e70f5e23e 100644 (file)
@@ -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;
index 964118ce021cba4f47a0acb8ef2641e1318cc7cd..10eed146a0d12d81f321bb01220af63f0c55b2d0 100644 (file)
@@ -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
+};