Skip to content

Commit f39a8f0

Browse files
committed
Fix pointer initialization problems in tests
1 parent 9e9d048 commit f39a8f0

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ bool ByteCodeExprGen<Emitter>::visitArrayElemInit(unsigned ElemIndex,
802802
return false;
803803
if (!this->visitInitializer(Init))
804804
return false;
805-
return this->emitPopPtr(Init);
805+
return this->emitInitPtrPop(Init);
806806
}
807807

808808
template <class Emitter>

clang/lib/AST/Interp/ByteCodeExprGen.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
180180
if (!visitInitializer(Init))
181181
return false;
182182

183+
if (!this->emitInitPtr(Init))
184+
return false;
185+
183186
return this->emitPopPtr(Init);
184187
}
185188

@@ -191,6 +194,9 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
191194
if (!visitInitializer(Init))
192195
return false;
193196

197+
if (!this->emitInitPtr(Init))
198+
return false;
199+
194200
if ((Init->getType()->isArrayType() || Init->getType()->isRecordType()) &&
195201
!this->emitCheckGlobalCtor(Init))
196202
return false;
@@ -206,7 +212,7 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
206212
if (!visitInitializer(I))
207213
return false;
208214

209-
return this->emitPopPtr(I);
215+
return this->emitInitPtrPop(I);
210216
}
211217

212218
bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *E);

clang/lib/AST/Interp/Interp.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,12 @@ inline bool InitPtrPop(InterpState &S, CodePtr OpPC) {
12751275
return true;
12761276
}
12771277

1278+
inline bool InitPtr(InterpState &S, CodePtr OpPC) {
1279+
const Pointer &Ptr = S.Stk.peek<Pointer>();
1280+
Ptr.initialize();
1281+
return true;
1282+
}
1283+
12781284
inline bool VirtBaseHelper(InterpState &S, CodePtr OpPC, const RecordDecl *Decl,
12791285
const Pointer &Ptr) {
12801286
Pointer Base = Ptr;
@@ -1331,7 +1337,7 @@ bool Store(InterpState &S, CodePtr OpPC) {
13311337
const Pointer &Ptr = S.Stk.peek<Pointer>();
13321338
if (!CheckStore(S, OpPC, Ptr))
13331339
return false;
1334-
if (!Ptr.isRoot())
1340+
if (Ptr.canBeInitialized())
13351341
Ptr.initialize();
13361342
Ptr.deref<T>() = Value;
13371343
return true;
@@ -1343,7 +1349,7 @@ bool StorePop(InterpState &S, CodePtr OpPC) {
13431349
const Pointer &Ptr = S.Stk.pop<Pointer>();
13441350
if (!CheckStore(S, OpPC, Ptr))
13451351
return false;
1346-
if (!Ptr.isRoot())
1352+
if (Ptr.canBeInitialized())
13471353
Ptr.initialize();
13481354
Ptr.deref<T>() = Value;
13491355
return true;
@@ -1355,7 +1361,7 @@ bool StoreBitField(InterpState &S, CodePtr OpPC) {
13551361
const Pointer &Ptr = S.Stk.peek<Pointer>();
13561362
if (!CheckStore(S, OpPC, Ptr))
13571363
return false;
1358-
if (!Ptr.isRoot())
1364+
if (Ptr.canBeInitialized())
13591365
Ptr.initialize();
13601366
if (const auto *FD = Ptr.getField())
13611367
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));
@@ -1370,7 +1376,7 @@ bool StoreBitFieldPop(InterpState &S, CodePtr OpPC) {
13701376
const Pointer &Ptr = S.Stk.pop<Pointer>();
13711377
if (!CheckStore(S, OpPC, Ptr))
13721378
return false;
1373-
if (!Ptr.isRoot())
1379+
if (Ptr.canBeInitialized())
13741380
Ptr.initialize();
13751381
if (const auto *FD = Ptr.getField())
13761382
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getCtx()));

clang/lib/AST/Interp/Opcodes.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,8 @@ def GetPtrBasePop : Opcode {
317317
let Args = [ArgUint32];
318318
}
319319

320-
def InitPtrPop : Opcode {
321-
let Args = [];
322-
}
320+
def InitPtrPop : Opcode;
321+
def InitPtr : Opcode;
323322

324323
def GetPtrDerivedPop : Opcode {
325324
let Args = [ArgUint32];

clang/lib/AST/Interp/Pointer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,10 @@ class Pointer {
286286
bool isArrayElement() const { return inArray() && Base != Offset; }
287287
/// Pointer points directly to a block.
288288
bool isRoot() const {
289-
return (Base == 0 || Base == sizeof(InlineDescriptor) ||
290-
Base == RootPtrMark) &&
291-
Offset == 0;
289+
return (Base == 0 || Base == RootPtrMark) && Offset == 0;
292290
}
291+
/// If this pointer has an InlineDescriptor we can use to initialize.
292+
bool canBeInitialized() const { return Pointee && Base > 0; }
293293

294294
/// Returns the record descriptor of a class.
295295
const Record *getRecord() const { return getFieldDesc()->ElemRecord; }

clang/unittests/AST/Interp/Descriptor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ TEST(Descriptor, Primitives) {
5252
ASSERT_FALSE(GlobalDesc->asRecordDecl());
5353

5454
// Still true because this is a global variable.
55-
ASSERT_TRUE(GlobalDesc->getMetadataSize() == 0);
55+
ASSERT_TRUE(GlobalDesc->getMetadataSize() == sizeof(InlineDescriptor));
5656
ASSERT_FALSE(GlobalDesc->isPrimitiveArray());
5757
ASSERT_FALSE(GlobalDesc->isCompositeArray());
5858
ASSERT_FALSE(GlobalDesc->isZeroSizeArray());
@@ -114,8 +114,8 @@ TEST(Descriptor, Primitives) {
114114
ASSERT_TRUE(F4->Desc->ElemDesc->isPrimitiveArray());
115115

116116
// Check pointer stuff.
117-
// Global variables have no inline descriptor (yet).
118-
ASSERT_TRUE(GlobalPtr.isRoot());
117+
// Global variables have an inline descriptor.
118+
ASSERT_FALSE(GlobalPtr.isRoot());
119119
ASSERT_TRUE(GlobalPtr.isLive());
120120
ASSERT_FALSE(GlobalPtr.isZero());
121121
ASSERT_FALSE(GlobalPtr.isField());

0 commit comments

Comments
 (0)