Skip to content

Commit 90d1d23

Browse files
authored
[clang][bytecode] Overrride locs for certain CXXConstructExprs (#152185)
Do it only if we will end up skipping the initializer anyway because it's a trivial copy or move constructor.
1 parent fe0d67b commit 90d1d23

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5997,6 +5997,23 @@ bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
59975997
return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
59985998
}
59995999

6000+
static bool initNeedsOverridenLoc(const CXXCtorInitializer *Init) {
6001+
const Expr *InitExpr = Init->getInit();
6002+
6003+
if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
6004+
!isa<CXXConstructExpr>(InitExpr))
6005+
return true;
6006+
6007+
if (const auto *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
6008+
const CXXConstructorDecl *Ctor = CE->getConstructor();
6009+
if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
6010+
Ctor->isTrivial())
6011+
return true;
6012+
}
6013+
6014+
return false;
6015+
}
6016+
60006017
template <class Emitter>
60016018
bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
60026019
assert(!ReturnType);
@@ -6071,10 +6088,7 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
60716088
const Record::Field *F = R->getField(Member);
60726089

60736090
LocOverrideScope<Emitter> LOS(this, SourceInfo{},
6074-
!Init->isWritten() &&
6075-
!Init->isInClassMemberInitializer() &&
6076-
(!isa<CXXConstructExpr>(InitExpr) ||
6077-
Member->isAnonymousStructOrUnion()));
6091+
initNeedsOverridenLoc(Init));
60786092
if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
60796093
return false;
60806094
} else if (const Type *Base = Init->getBaseClass()) {
@@ -6104,10 +6118,7 @@ bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
61046118
return false;
61056119
} else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
61066120
LocOverrideScope<Emitter> LOS(this, SourceInfo{},
6107-
!Init->isWritten() &&
6108-
!Init->isInClassMemberInitializer() &&
6109-
!isa<CXXConstructExpr>(InitExpr));
6110-
6121+
initNeedsOverridenLoc(Init));
61116122
assert(IFD->getChainingSize() >= 2);
61126123

61136124
unsigned NestedFieldOffset = 0;

clang/lib/AST/ByteCode/InterpFrame.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ static bool shouldSkipInBacktrace(const Function *F) {
133133
MD && MD->getParent()->isAnonymousStructOrUnion())
134134
return true;
135135

136+
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(FD);
137+
Ctor && Ctor->isDefaulted() && Ctor->isTrivial() &&
138+
Ctor->isCopyOrMoveConstructor() && Ctor->inits().empty())
139+
return true;
140+
136141
return false;
137142
}
138143

clang/test/AST/ByteCode/cxx11.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,15 @@ namespace PR19010 {
318318
};
319319
void test() { constexpr Test t; }
320320
}
321+
322+
namespace ReadMutableInCopyCtor {
323+
struct G {
324+
struct X {};
325+
union U { X a; };
326+
mutable U u; // both-note {{declared here}}
327+
};
328+
constexpr G g1 = {};
329+
constexpr G g2 = g1; // both-error {{must be initialized by a constant expression}} \
330+
// both-note {{read of mutable member 'u'}} \
331+
// both-note {{in call to 'G(g1)'}}
332+
}

clang/test/SemaCXX/constexpr-value-init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 %s -Wno-uninitialized -std=c++17 -fsyntax-only -verify
2+
// RUN: %clang_cc1 %s -Wno-uninitialized -std=c++17 -fsyntax-only -verify -fexperimental-new-constant-interpreter
23

34
struct A {
45
constexpr A() : a(b + 1), b(a + 1) {} // expected-note 5{{outside its lifetime}}

0 commit comments

Comments
 (0)