Issue 184747
Summary [Clang] Assignment to a member array of a comma operator result
Labels clang, clang:codegen
Assignees
Reporter s-watanabe314
    In the test program below, the return value of the comma operator is the `obj2` struct, and an assignment is made to its member array. However, when the value is referenced later, the value has not been assigned. This behavior started after patch #133472.

- test.c (Original program: [0044_0007.c](https://github.com/fujitsu/compiler-test-suite/blob/main/C/0044/0044_0007.c))
```C
#include <stdio.h>

struct test02 {
  int array[10];
};

int main()
{
  struct test02 obj2 = { 0 };
  (0,obj2).array[3] = 5;

  printf("%d\n", obj2.array[3]);

  return 0;
}
```

- Result
```
$ clang -v
clang version 23.0.0git (https://github.com/llvm/llvm-project.git 7b26069828aa88064b92f73f764b708754e441ec)
$ clang test.c -O0 -o test.out
$ ./test.out 
0
```


In the LLVM IR, the `store` and `load` operations were performed on different pointers.

- test.ll ([godbolt](https://godbolt.org/z/5q6hjEMc6))
```
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca %struct.test02, align 4
  %3 = alloca %struct.test02, align 4
  store i32 0, ptr %1, align 4
 call void @llvm.memset.p0.i64(ptr align 4 %2, i8 0, i64 40, i1 false)
  call void @llvm.memcpy.p0.p0.i64(ptr align 4 %3, ptr align 4 %2, i64 40, i1 false)
  %4 = getelementptr inbounds nuw %struct.test02, ptr %3, i32 0, i32 0
  %5 = getelementptr inbounds [10 x i32], ptr %4, i64 0, i64 3
  store i32 5, ptr %5, align 4
  %6 = getelementptr inbounds nuw %struct.test02, ptr %2, i32 0, i32 0
  %7 = getelementptr inbounds [10 x i32], ptr %6, i64 0, i64 3
  %8 = load i32, ptr %7, align 4
  %9 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %8)
  ret i32 0
}
```

Is this the expected behavior based on the C language standard? Should Clang issue a compile-time error or warning?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to