Replace static LookupResult caches with per-instance members in VisitorBase - #1838
Replace static LookupResult caches with per-instance members in VisitorBase#1838Elvand-Lie wants to merge 3 commits into
Conversation
GetCladTapePush, GetCladTapePop, GetCladTapeBack, and GetCladZeroInit each used a function-local static llvm::Optional<LookupResult> to cache their lookup. LookupResult objects hold pointers into the current ASTContext/Sema, so static locals cause stale AST references when multiple translation units are processed in the same process (e.g., in Cling's interactive mode). Replace all four statics with per-instance llvm::Optional<LookupResult> members on VisitorBase: m_TapePushLookup, m_TapePopLookup, m_TapeBackLookup, and m_TapeZeroInitLookup. Each lookup is performed lazily on first use and dies with the visitor instance. Fixes: vgvassilev#1837
08fb226 to
35c5fa5
Compare
|
|
||
| /// Per-instance cache for tape lookup results. These are tied to the | ||
| /// current Sema/ASTContext and must not be shared across translation units. | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapePushLookup; |
There was a problem hiding this comment.
warning: member variable 'm_TapePushLookup' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]
clad_compat::llvm_Optional<clang::LookupResult> m_TapePushLookup;
^| /// Per-instance cache for tape lookup results. These are tied to the | ||
| /// current Sema/ASTContext and must not be shared across translation units. | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapePushLookup; | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapePopLookup; |
There was a problem hiding this comment.
warning: member variable 'm_TapePopLookup' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]
clad_compat::llvm_Optional<clang::LookupResult> m_TapePopLookup;
^| /// current Sema/ASTContext and must not be shared across translation units. | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapePushLookup; | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapePopLookup; | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapeBackLookup; |
There was a problem hiding this comment.
warning: member variable 'm_TapeBackLookup' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]
clad_compat::llvm_Optional<clang::LookupResult> m_TapeBackLookup;
^| clad_compat::llvm_Optional<clang::LookupResult> m_TapePushLookup; | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapePopLookup; | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapeBackLookup; | ||
| clad_compat::llvm_Optional<clang::LookupResult> m_TapeZeroInitLookup; |
There was a problem hiding this comment.
warning: member variable 'm_TapeZeroInitLookup' has protected visibility [cppcoreguidelines-non-private-member-variables-in-classes]
clad_compat::llvm_Optional<clang::LookupResult> m_TapeZeroInitLookup;
^| if (!Result) | ||
| Result = LookupCladTapeMethod("pop"); | ||
| return clad_compat::llvm_Optional_GetValue(Result); | ||
| if (!m_TapePopLookup) |
There was a problem hiding this comment.
warning: use auto when initializing with a template cast to avoid duplicating the type name [modernize-use-auto]
| if (!m_TapePopLookup) | |
| auto* pushDRE = m_Sema.BuildDeclarationNameExpr(CSS, pushLR, false) |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Fixes #1837
Problem
GetCladTapePush(),GetCladTapePop(),GetCladTapeBack(), andGetCladZeroInit()inVisitorBaseeach use a function-localstaticvariable to cache theirLookupResult. SinceLookupResultholds pointers into the currentASTContext/Sema, these statics become dangling when multiple translation units are processed in the same process (e.g., in Cling).Solution
Replace all four
static llvm::Optional<LookupResult>locals with per-instance members onVisitorBase:m_TapePushLookupm_TapePopLookupm_TapeBackLookupm_TapeZeroInitLookupEach lookup is performed lazily on first use and dies with the visitor instance, so there is no cross-TU contamination.
Files changed
include/clad/Differentiator/VisitorBase.h-- add fourllvm_Optional<LookupResult>members, add#include <vector>lib/Differentiator/VisitorBase.cpp-- replacestaticcaches with member access in all four functionsTesting
This is an internal refactor that preserves external behavior. The stale-pointer bug requires a multi-TU interactive session (Cling) to reproduce, which is not covered by the existing test suite.