Author: Timm Bäder Date: 2024-02-15T18:12:47+01:00 New Revision: 54826d4980db5782dac2d7326c29ad60cd017e56
URL: https://github.com/llvm/llvm-project/commit/54826d4980db5782dac2d7326c29ad60cd017e56 DIFF: https://github.com/llvm/llvm-project/commit/54826d4980db5782dac2d7326c29ad60cd017e56.diff LOG: [clang][Interp] Emit dummy pointers for unknown static locals 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. Added: Modified: clang/lib/AST/Interp/ByteCodeExprGen.cpp clang/test/AST/Interp/functions.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index 25208f7dafbcd6..db6d818eef334b 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -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)) diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp index 320691336bdd99..bf582362460ebc 100644 --- a/clang/test/AST/Interp/functions.cpp +++ b/clang/test/AST/Interp/functions.cpp @@ -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, ""); + } +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits