Skip to content

Commit

Permalink
[clang][Interp] Emit dummy pointers for unknown static locals
Browse files Browse the repository at this point in the history
We used to emit dummy pointers for unknown declarations in certain
cases in C, but this is also necessary in C++.

I'm limiting this to static local variables for now.
  • Loading branch information
tbaederr committed Feb 15, 2024
1 parent 9c6a2de commit 54826d4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
11 changes: 8 additions & 3 deletions clang/lib/AST/Interp/ByteCodeExprGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3239,9 +3239,14 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
return this->emitGetPtrThisField(Offset, E);
}

// Lazily visit global declarations we haven't seen yet.
// This happens in C.
if (!Ctx.getLangOpts().CPlusPlus) {
// 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);
}
} else {
if (const auto *VD = dyn_cast<VarDecl>(D);
VD && VD->getAnyInitializer() && VD->getType().isConstQualified()) {
if (!this->visitVarDecl(VD))
Expand Down
20 changes: 20 additions & 0 deletions clang/test/AST/Interp/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,23 @@ namespace Move {
constexpr int A = std::move(5);
static_assert(A == 5, "");
}

namespace StaticLocals {
void test() {
static int j; // both-note {{declared here}}
static_assert(&j != nullptr, ""); // both-warning {{always true}}

static_assert(j == 0, ""); // both-error {{not an integral constant expression}} \
// both-note {{read of non-const variable 'j'}}

static int k = 0; // both-note {{declared here}}
static_assert(k == 0, ""); // both-error {{not an integral constant expression}} \
// both-note {{read of non-const variable 'k'}}

static const int l = 12;
static_assert(l == 12, "");

static const int m; // both-error {{default initialization}}
static_assert(m == 0, "");
}
}

0 comments on commit 54826d4

Please sign in to comment.