Skip to content

Commit 1f47fa2

Browse files
committed
[ASTGen] Don't leave AST node caches after generate()
For non-migrated expression / decl attributes. Clean-up the slot after the use. Assert there's no existing element at the location.
1 parent 1de6819 commit 1f47fa2

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

include/swift/Parse/ASTGen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ class ASTGen {
235235
public:
236236
void addExpr(Expr *Expr, const SourceLoc Loc);
237237
bool hasExpr(const SourceLoc Loc) const;
238-
Expr *getExpr(const SourceLoc Loc) const;
238+
Expr *takeExpr(const SourceLoc Loc);
239239

240240
void addDeclAttributes(DeclAttributes attrs, const SourceLoc Loc);
241241
bool hasDeclAttributes(SourceLoc Loc) const;
242-
DeclAttributes getDeclAttributes(const SourceLoc Loc) const;
242+
DeclAttributes takeDeclAttributes(const SourceLoc Loc);
243243
};
244244
} // namespace swift
245245

lib/Parse/ASTGen.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ASTGen::generateDeclAttributes(const Syntax &D, SourceLoc Loc,
6363
if (auto firstTok = D.getFirstToken()) {
6464
auto declLoc = advanceLocBegin(Loc, *firstTok);
6565
if (hasDeclAttributes(declLoc))
66-
return getDeclAttributes(declLoc);
66+
return takeDeclAttributes(declLoc);
6767
}
6868
return DeclAttributes();
6969
}
@@ -234,7 +234,7 @@ Expr *ASTGen::generate(const ExprSyntax &E, const SourceLoc Loc) {
234234

235235
auto exprLoc = advanceLocBegin(Loc, E);
236236
if (hasExpr(exprLoc))
237-
return getExpr(exprLoc);
237+
return takeExpr(exprLoc);
238238

239239
if (auto identifierExpr = E.getAs<IdentifierExprSyntax>())
240240
result = generate(*identifierExpr, Loc);
@@ -1457,37 +1457,35 @@ TypeRepr *ASTGen::lookupType(TypeSyntax Type) {
14571457
}
14581458

14591459
void ASTGen::addExpr(Expr *E, const SourceLoc Loc) {
1460-
#ifndef NDEBUG
1461-
if (hasExpr(Loc)) {
1462-
bool PrevIsSubExpr = false;
1463-
Expr *Prev = Exprs.find(Loc)->second;
1464-
E->forEachChildExpr([&](Expr *Child) {
1465-
if (Child == Prev)
1466-
PrevIsSubExpr = true;
1467-
return Child;
1468-
});
1469-
assert(PrevIsSubExpr);
1470-
}
1471-
#endif
1472-
Exprs.insert({Loc, E});
1460+
assert(!hasExpr(Loc));
1461+
Exprs[Loc] = E;
14731462
}
14741463

14751464
bool ASTGen::hasExpr(const SourceLoc Loc) const {
14761465
return Exprs.find(Loc) != Exprs.end();
14771466
}
14781467

1479-
Expr *ASTGen::getExpr(const SourceLoc Loc) const {
1480-
return Exprs.find(Loc)->second;
1468+
Expr *ASTGen::takeExpr(const SourceLoc Loc) {
1469+
auto I = Exprs.find(Loc);
1470+
assert(I != Exprs.end());
1471+
auto expr = I->second;
1472+
Exprs.erase(I);
1473+
return expr;
14811474
}
14821475

14831476
void ASTGen::addDeclAttributes(DeclAttributes attrs, SourceLoc Loc) {
1484-
ParsedDeclAttrs.insert({Loc, attrs});
1477+
assert(!hasDeclAttributes(Loc));
1478+
ParsedDeclAttrs[Loc] = attrs;
14851479
}
14861480

14871481
bool ASTGen::hasDeclAttributes(SourceLoc Loc) const {
14881482
return ParsedDeclAttrs.find(Loc) != ParsedDeclAttrs.end();
14891483
}
14901484

1491-
DeclAttributes ASTGen::getDeclAttributes(SourceLoc Loc) const {
1492-
return ParsedDeclAttrs.find(Loc)->second;
1485+
DeclAttributes ASTGen::takeDeclAttributes(SourceLoc Loc) {
1486+
auto I = ParsedDeclAttrs.find(Loc);
1487+
assert(I != ParsedDeclAttrs.end());
1488+
auto attrs = I->second;
1489+
ParsedDeclAttrs.erase(I);
1490+
return attrs;
14931491
}

0 commit comments

Comments
 (0)