[clang] [analyzer] Fix getElementRegion to retain address space information (PR #151249)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 30 09:41:42 PDT 2025


================
@@ -0,0 +1,18 @@
+// RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown \
+// RUN: -analyzer-checker=core,unix.Malloc,debug.ExprInspection -verify \
+// RUN: -Wno-incompatible-pointer-types -Wno-unused-comparison %s
+
+// expected-no-diagnostics
+//
+// By default, pointers are 64-bits.
+#define ADDRESS_SPACE_32BITS __attribute__((address_space(3)))
+ADDRESS_SPACE_32BITS void *b();
+ADDRESS_SPACE_32BITS int *c();
+typedef struct {
+  ADDRESS_SPACE_32BITS int *e;
+} f;
+void g() {
+  ADDRESS_SPACE_32BITS void *h = b();
+  ADDRESS_SPACE_32BITS f *j = c();
+  j->e == h;
+}
----------------
NagyDonat wrote:

I experimented with this test and it turns out that the crux is that you compare an `ADDRESS_SPACE_32BITS int *` (the field `e`) and an `ADDRESS_SPACE_32BITS void *` (the pointer `h`). (This forces an implicit cast, which is represented by the `ElementRegion` that drops the address space information.)

I verified that (without your fix) the assertion is also triggered by
```suggestion
int test(ADDRESS_SPACE_32BITS int *p, ADDRESS_SPACE_32BITS void *q) {
  return p == q;
} 
```
so I think it would be better to use this instead of your (also correct, but more complex) example.

https://github.com/llvm/llvm-project/pull/151249


More information about the cfe-commits mailing list