Skip to content

[clang][Interp] Zero-init remaining string literal elements #66862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,8 +858,8 @@ bool ByteCodeExprGen<Emitter>::VisitStringLiteral(const StringLiteral *E) {

// If the initializer string is too long, a diagnostic has already been
// emitted. Read only the array length from the string literal.
unsigned N =
std::min(unsigned(CAT->getSize().getZExtValue()), E->getLength());
unsigned ArraySize = CAT->getSize().getZExtValue();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So getZExtValue() returns uint64_t yet we are using unsigned. I know this is already an existing problem that we make varying assumptions in different places but I am wondering what the long-term plan is for this this. Forgive me if I have asked this question in another context and I just forgot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit tricky since StringLiteral::getLength() returns unsigned again, so we can't use the extra bytes anyway and need to cast to unsigned for the std::min().

unsigned N = std::min(ArraySize, E->getLength());
size_t CharWidth = E->getCharByteWidth();

for (unsigned I = 0; I != N; ++I) {
Expand All @@ -878,6 +878,23 @@ bool ByteCodeExprGen<Emitter>::VisitStringLiteral(const StringLiteral *E) {
llvm_unreachable("unsupported character width");
}
}

// Fill up the rest of the char array with NUL bytes.
for (unsigned I = N; I != ArraySize; ++I) {
if (CharWidth == 1) {
this->emitConstSint8(0, E);
this->emitInitElemSint8(I, E);
} else if (CharWidth == 2) {
this->emitConstUint16(0, E);
this->emitInitElemUint16(I, E);
} else if (CharWidth == 4) {
this->emitConstUint32(0, E);
this->emitInitElemUint32(I, E);
} else {
llvm_unreachable("unsupported character width");
}
}

return true;
}

Expand Down
21 changes: 21 additions & 0 deletions clang/test/AST/Interp/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,24 @@ namespace ArrayInitLoop {
// expected-note {{15 == 6}}
#endif
}

namespace StringZeroFill {
struct A {
char c[6];
};
constexpr A a = { "abc" };
static_assert(a.c[0] == 'a', "");
static_assert(a.c[1] == 'b', "");
static_assert(a.c[2] == 'c', "");
static_assert(a.c[3] == '\0', "");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we just test all the bytes? It is small enough.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also test wide string literals etc?

static_assert(a.c[4] == '\0', "");
static_assert(a.c[5] == '\0', "");

constexpr char b[6] = "foo";
static_assert(b[0] == 'f', "");
static_assert(b[1] == 'o', "");
static_assert(b[2] == 'o', "");
static_assert(b[3] == '\0', "");
static_assert(b[4] == '\0', "");
static_assert(b[5] == '\0', "");
}