Skip to content

Commit

Permalink
[clang][Interp] Lazily visit constant locals in C++
Browse files Browse the repository at this point in the history
While we _do_ get them registered via visitInitializer(), they
are still local, so gone on the next call to e.g. evaluateAsRValue().

Visit them lazily, similarly like we do in C.
  • Loading branch information
tbaederr committed Feb 16, 2024
1 parent 7dcca62 commit a52c0c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
17 changes: 14 additions & 3 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3237,9 +3237,20 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
// Try to lazily visit (or emit dummy pointers for) declarations
// we haven't seen yet.
if (Ctx.getLangOpts().CPlusPlus) {
if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isStaticLocal()) {
if (std::optional<unsigned> I = P.getOrCreateDummy(D))
return this->emitGetPtrGlobal(*I, E);
if (const auto *VD = dyn_cast<VarDecl>(D)) {
// Dummy for static locals
if (VD->isStaticLocal()) {
if (std::optional<unsigned> I = P.getOrCreateDummy(D))
return this->emitGetPtrGlobal(*I, E);
return false;
}
// Visit local const variables like normal.
if (VD->isLocalVarDecl() && VD->getType().isConstQualified()) {
if (!this->visitVarDecl(VD))
return false;
// Retry.
return this->VisitDeclRefExpr(E);
}
}
} else {
if (const auto *VD = dyn_cast<VarDecl>(D);
Expand Down
8 changes: 8 additions & 0 deletions clang/test/AST/Interp/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,11 @@ namespace SelfComparison {
return s3->array[t.field] == s3->array[t.field]; // both-warning {{self-comparison always evaluates to true}}
};
}

namespace LocalIndex {
void test() {
const int const_subscript = 3;
int array[2]; // both-note {{declared here}}
array[const_subscript] = 0; // both-warning {{array index 3 is past the end of the array (that has type 'int[2]')}}
}
}

0 comments on commit a52c0c7

Please sign in to comment.