[clang] 20b731f - [LifetimeSafety][NFC] Collect the field origins of the implicit object once per class (#246494)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 27 00:10:22 PST 2026
Author: Gábor Horváth
Date: 2026-11-27T08:10:16Z
New Revision: 20b731f8e4fc6f6f2fc1473951ca9fe214602d1d
URL: https://github.com/llvm/llvm-project/commit/20b731f8e4fc6f6f2fc1473951ca9fe214602d1d
DIFF: https://github.com/llvm/llvm-project/commit/20b731f8e4fc6f6f2fc1473951ca9fe214602d1d.diff
LOG: [LifetimeSafety][NFC] Collect the field origins of the implicit object once per class (#246494)
Added:
Modified:
clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
index 5734f394548f65..1005a664a2dcca 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/FactsGenerator.h
@@ -197,6 +197,10 @@ class FactsGenerator : public ConstStmtVisitor<FactsGenerator> {
llvm::DenseMap<const ValueDecl *,
llvm::SmallVector<const MaterializeTemporaryExpr *, 1>>
ExtendedTemporaries;
+ // The origin lists of the fields of a class and its bases, for
+ // handleImplicitObjectFieldUses.
+ llvm::DenseMap<const CXXRecordDecl *, llvm::SmallVector<OriginList *>>
+ ImplicitObjectFieldLists;
const CFGBlock *CurrentBlock;
bool IsCMode = false;
};
diff --git a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
index a770373709a98d..52f4fa507979e9 100644
--- a/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp
@@ -1099,19 +1099,25 @@ void FactsGenerator::handleImplicitObjectFieldUses(const Expr *Call,
if (!ClassDecl)
return;
- const auto UseFields = [&](const CXXRecordDecl *RD) {
- for (const auto *Field : RD->fields())
- if (auto *FieldList = getOriginsList(*Field))
- CurrentBlockFacts.push_back(
- FactMgr.createFact<UseFact>(*Call, FieldList));
- };
-
- UseFields(ClassDecl);
+ // Walking the fields visits every member declaration of the class and its
+ // bases, so do it once per class.
+ auto [It, Inserted] = ImplicitObjectFieldLists.try_emplace(ClassDecl);
+ llvm::SmallVectorImpl<OriginList *> &FieldLists = It->second;
+ if (Inserted) {
+ const auto CollectFields = [&](const CXXRecordDecl *RD) {
+ for (const auto *Field : RD->fields())
+ if (auto *FieldList = getOriginsList(*Field))
+ FieldLists.push_back(FieldList);
+ };
+ CollectFields(ClassDecl);
+ ClassDecl->forallBases([&](const CXXRecordDecl *Base) {
+ CollectFields(Base);
+ return true;
+ });
+ }
- ClassDecl->forallBases([&](const CXXRecordDecl *Base) {
- UseFields(Base);
- return true;
- });
+ for (OriginList *FieldList : FieldLists)
+ CurrentBlockFacts.push_back(FactMgr.createFact<UseFact>(*Call, FieldList));
}
void FactsGenerator::handleLifetimeCaptureBy(const FunctionDecl *FD,
More information about the cfe-commits
mailing list