llvm / llvm/llvm-project

miscompile performing list initialization via constructor from embed expression

Open
#222,796 2 comments 0 reactions 0 assignees View on GitHub
c++26 clang:frontend embed miscompilation
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

[Testcase](https://clang.godbolt.org/z/7nMshEa7f):
```c++
#include

struct X {
template
constexpr X(T ...t) : sum((... + int(t))), size(sizeof...(T)) {}
int sum;
int size;
} x = {
#embed __FILE__
};

int main() {
std::print("file size: {0}, content sum: {1}", x.size, x.sum);
}
```
reports
```console
file size: 1, content sum: 35
```
It looks like only the first element of the embed expression actually gets passed to the constructor (strangely we have an `EmbedExpr` of type `int` here covering more than one element of the file):
```clangast
|-VarDecl line:8:3 used x 'struct X' cinit external-linkage
| `-CXXConstructExpr 'struct X' 'void (int)' list
| `-EmbedExpr 'int'
| |-begin: 0
| `-number of elements: 262
```
We probably have to expand the embed to one expression per function argument here, [like we do for direct non-list initialization](https://clang.godbolt.org/z/v431aeTsx).

Contributor guide

Open the contributing guide

Research direction

Start with the linked Compiler Explorer test case and compare its AST with the direct non-list initialization example. Trace how the EmbedExpr with multiple elements is handled when constructing X from a braced initializer. Done means each embedded element reaches the constructor as a separate argument and the reported size and sum match the file contents.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.