Skip to content

Commit 8a0b359

Browse files
tbaederraaryanshukla
authored andcommitted
[clang][Interp] Fix modifying const objects in functions calls in ctors
The current frame might not be a constructor for the object we're initializing, but a parent frame might.
1 parent 033b226 commit 8a0b359

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

clang/lib/AST/Interp/Interp.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,16 @@ bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
405405

406406
// The This pointer is writable in constructors and destructors,
407407
// even if isConst() returns true.
408-
if (const Function *Func = S.Current->getFunction();
409-
Func && (Func->isConstructor() || Func->isDestructor()) &&
410-
Ptr.block() == S.Current->getThis().block()) {
411-
return true;
408+
// TODO(perf): We could be hitting this code path quite a lot in complex
409+
// constructors. Is there a better way to do this?
410+
if (S.Current->getFunction()) {
411+
for (const InterpFrame *Frame = S.Current; Frame; Frame = Frame->Caller) {
412+
if (const Function *Func = Frame->getFunction();
413+
Func && (Func->isConstructor() || Func->isDestructor()) &&
414+
Ptr.block() == Frame->getThis().block()) {
415+
return true;
416+
}
417+
}
412418
}
413419

414420
if (!Ptr.isBlockPointer())

clang/test/AST/Interp/records.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,3 +1512,28 @@ namespace OnePastEndAndBack {
15121512
constexpr const Base *d = c - 1;
15131513
static_assert(d == &a, "");
15141514
}
1515+
1516+
namespace BitSet {
1517+
class Bitset {
1518+
unsigned Bit = 0;
1519+
1520+
public:
1521+
constexpr Bitset() {
1522+
int Init[2] = {1,2};
1523+
for (auto I : Init)
1524+
set(I);
1525+
}
1526+
constexpr void set(unsigned I) {
1527+
this->Bit++;
1528+
this->Bit = 1u << 1;
1529+
}
1530+
};
1531+
1532+
struct ArchInfo {
1533+
Bitset DefaultExts;
1534+
};
1535+
1536+
constexpr ArchInfo ARMV8A = {
1537+
Bitset()
1538+
};
1539+
}

0 commit comments

Comments
 (0)