Skip to content

Commit d02714c

Browse files
authored
[clang][bytecode] Return success for pointers to locals (#151980)
They aren't valid, but we leave them successful here and CheckLValueConstantExpression will diagnose them later.
1 parent 0206c0f commit d02714c

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/AST/ByteCode/EvalEmitter.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,14 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
233233
return false;
234234
}
235235
} else {
236+
// If this is pointing to a local variable, just return
237+
// the result, even if the pointer is dead.
238+
// This will later be diagnosed by CheckLValueConstantExpression.
239+
if (Ptr.isBlockPointer() && !Ptr.block()->isStatic()) {
240+
EvalResult.setValue(Ptr.toAPValue(Ctx.getASTContext()));
241+
return true;
242+
}
243+
236244
if (!Ptr.isLive() && !Ptr.isTemporary())
237245
return false;
238246

clang/test/AST/ByteCode/cxx11.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,11 @@ namespace NonConstLocal {
301301
}
302302
}
303303

304+
#define ATTR __attribute__((require_constant_initialization))
305+
int somefunc() {
306+
const int non_global = 42; // both-note {{declared here}}
307+
ATTR static const int &local_init = non_global; // both-error {{variable does not have a constant initializer}} \
308+
// both-note {{required by}} \
309+
// both-note {{reference to 'non_global' is not a constant expression}}
310+
}
304311

0 commit comments

Comments
 (0)