[PATCH] D15319: [Sema] Don't accept e.g. "(int *)(long)&x" as a constant expression if x is in address space != 0.

Manuel Jacob via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 7 17:13:59 PST 2015


mjacob created this revision.
mjacob added a reviewer: rsmith.
mjacob added subscribers: cfe-commits, alex.

The constant expression evaluator for pointers returns a base and an
offset if successful.  In this case, CodeGen generates a constant pointer cast,
casting the base (with offset applied if necessary) to the destination type.
However, that is different from casting the base to an integer and then to the
destination type if the destination points to another address space as the
base.  Supporting this would require a lot of work because it can't be
expressed as base + offset.  Changing the whole constant expression evaluator
is not worthline for supporting this special case.

http://reviews.llvm.org/D15319

Files:
  lib/AST/ExprConstant.cpp
  test/Sema/const-eval.c

Index: test/Sema/const-eval.c
===================================================================
--- test/Sema/const-eval.c
+++ test/Sema/const-eval.c
@@ -137,3 +137,6 @@
 void PR24622();
 struct PR24622 {} pr24622;
 EVAL_EXPR(52, &pr24622 == (void *)&PR24622); // expected-error {{must have a constant size}}
+
+int __attribute__((address_space(1))) addrspace1;
+int *addrspace0_ptr = (int *)(long)&addrspace1; // expected-error {{not a compile-time constant}}
Index: lib/AST/ExprConstant.cpp
===================================================================
--- lib/AST/ExprConstant.cpp
+++ lib/AST/ExprConstant.cpp
@@ -4995,7 +4995,11 @@
       Result.Designator.setInvalid();
       return true;
     } else {
-      // Cast is of an lvalue, no need to change value.
+      // Cast is of an lvalue, no need to change value, unless they have
+      // different address spaces.
+      if (Info.Ctx.getTargetAddressSpace(getType(Value.getLValueBase())) !=
+          Info.Ctx.getTargetAddressSpace(E->getType()->getPointeeType()))
+        return false;
       Result.setFrom(Info.Ctx, Value);
       return true;
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15319.42129.patch
Type: text/x-patch
Size: 1126 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151208/c3b961ec/attachment.bin>


More information about the cfe-commits mailing list