Skip to content

Commit f7f3dfc

Browse files
authored
[clang][bytecode] Refactor Program::createGlobalString (#125467)
Remove unnecesary narrow() calls, rename a variable and initialize the array as a whole instead of each element individually.
1 parent 951ba3e commit f7f3dfc

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

clang/lib/AST/ByteCode/Program.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const void *Program::getNativePointer(unsigned Idx) {
3535
unsigned Program::createGlobalString(const StringLiteral *S, const Expr *Base) {
3636
const size_t CharWidth = S->getCharByteWidth();
3737
const size_t BitWidth = CharWidth * Ctx.getCharBit();
38+
unsigned StringLength = S->getLength();
3839

3940
PrimType CharType;
4041
switch (CharWidth) {
@@ -55,15 +56,15 @@ unsigned Program::createGlobalString(const StringLiteral *S, const Expr *Base) {
5556
Base = S;
5657

5758
// Create a descriptor for the string.
58-
Descriptor *Desc = allocateDescriptor(Base, CharType, Descriptor::GlobalMD,
59-
S->getLength() + 1,
60-
/*isConst=*/true,
61-
/*isTemporary=*/false,
62-
/*isMutable=*/false);
59+
Descriptor *Desc =
60+
allocateDescriptor(Base, CharType, Descriptor::GlobalMD, StringLength + 1,
61+
/*isConst=*/true,
62+
/*isTemporary=*/false,
63+
/*isMutable=*/false);
6364

6465
// Allocate storage for the string.
6566
// The byte length does not include the null terminator.
66-
unsigned I = Globals.size();
67+
unsigned GlobalIndex = Globals.size();
6768
unsigned Sz = Desc->getAllocSize();
6869
auto *G = new (Allocator, Sz) Global(Ctx.getEvalID(), Desc, /*isStatic=*/true,
6970
/*isExtern=*/false);
@@ -74,33 +75,32 @@ unsigned Program::createGlobalString(const StringLiteral *S, const Expr *Base) {
7475

7576
// Construct the string in storage.
7677
const Pointer Ptr(G->block());
77-
for (unsigned I = 0, N = S->getLength(); I <= N; ++I) {
78-
Pointer Field = Ptr.atIndex(I).narrow();
79-
const uint32_t CodePoint = I == N ? 0 : S->getCodeUnit(I);
78+
for (unsigned I = 0; I <= StringLength; ++I) {
79+
Pointer Field = Ptr.atIndex(I);
80+
const uint32_t CodePoint = I == StringLength ? 0 : S->getCodeUnit(I);
8081
switch (CharType) {
8182
case PT_Sint8: {
8283
using T = PrimConv<PT_Sint8>::T;
8384
Field.deref<T>() = T::from(CodePoint, BitWidth);
84-
Field.initialize();
8585
break;
8686
}
8787
case PT_Uint16: {
8888
using T = PrimConv<PT_Uint16>::T;
8989
Field.deref<T>() = T::from(CodePoint, BitWidth);
90-
Field.initialize();
9190
break;
9291
}
9392
case PT_Uint32: {
9493
using T = PrimConv<PT_Uint32>::T;
9594
Field.deref<T>() = T::from(CodePoint, BitWidth);
96-
Field.initialize();
9795
break;
9896
}
9997
default:
10098
llvm_unreachable("unsupported character type");
10199
}
102100
}
103-
return I;
101+
Ptr.initialize();
102+
103+
return GlobalIndex;
104104
}
105105

106106
Pointer Program::getPtrGlobal(unsigned Idx) const {

0 commit comments

Comments
 (0)