llvm / llvm/llvm-project

[Clang] Assignment to a member array of a comma operator result

Open
#184,747 5 comments 0 reactions 0 assignees View on GitHub
clang:codegen clang:diagnostics undefined behaviour
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

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

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))
```llvm
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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.