Skip to content

Commit 1798aee

Browse files
committed
Fix compile time regression and memory leak
1 parent 3c233df commit 1798aee

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,17 +1896,17 @@ def LifetimeCaptureBy : DeclOrTypeAttr {
18961896
let Documentation = [LifetimeCaptureByDocs];
18971897
let AdditionalMembers = [{
18981898
private:
1899-
SmallVector<IdentifierInfo*, 1> ArgIdents;
1900-
SmallVector<SourceLocation, 1> ArgLocs;
1899+
SmallVector<IdentifierInfo*> ArgIdents;
1900+
SmallVector<SourceLocation> ArgLocs;
19011901

19021902
public:
19031903
static constexpr int THIS = 0;
19041904
static constexpr int INVALID = -1;
19051905
static constexpr int UNKNOWN = -2;
19061906
static constexpr int GLOBAL = -3;
19071907

1908-
void setArgs(SmallVector<IdentifierInfo*>&& Idents,
1909-
SmallVector<SourceLocation>&& Locs) {
1908+
void setArgs(SmallVector<IdentifierInfo*> Idents,
1909+
SmallVector<SourceLocation> Locs) {
19101910
assert(Idents.size() == Locs.size());
19111911
assert(Idents.size() == params_Size);
19121912
ArgIdents = std::move(Idents);

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3876,8 +3876,8 @@ LifetimeCaptureByAttr *Sema::ParseLifetimeCaptureByAttr(const ParsedAttr &AL,
38763876
<< AL.getRange();
38773877
return nullptr;
38783878
}
3879-
SmallVector<IdentifierInfo *, 1> ParamIdents;
3880-
SmallVector<SourceLocation, 1> ParamLocs;
3879+
SmallVector<IdentifierInfo *> ParamIdents;
3880+
SmallVector<SourceLocation> ParamLocs;
38813881
for (unsigned I = 0; I < AL.getNumArgs(); ++I) {
38823882
if (AL.isArgExpr(I)) {
38833883
Expr *E = AL.getArgAsExpr(I);
@@ -3894,15 +3894,15 @@ LifetimeCaptureByAttr *Sema::ParseLifetimeCaptureByAttr(const ParsedAttr &AL,
38943894
ParamIdents.push_back(IdLoc->Ident);
38953895
ParamLocs.push_back(IdLoc->Loc);
38963896
}
3897-
SmallVector<int, 1> FakeParamIndices(ParamIdents.size(),
3898-
LifetimeCaptureByAttr::INVALID);
3897+
SmallVector<int> FakeParamIndices(ParamIdents.size(),
3898+
LifetimeCaptureByAttr::INVALID);
38993899
LifetimeCaptureByAttr *CapturedBy = ::new (Context) LifetimeCaptureByAttr(
39003900
Context, AL, FakeParamIndices.data(), FakeParamIndices.size());
39013901
CapturedBy->setArgs(std::move(ParamIdents), std::move(ParamLocs));
39023902
return CapturedBy;
39033903
}
39043904

3905-
static void HandleLifetimeCaptureByAttr(Sema &S, Decl *D,
3905+
static void handleLifetimeCaptureByAttr(Sema &S, Decl *D,
39063906
const ParsedAttr &AL) {
39073907
// Do not allow multiple attributes.
39083908
if (D->hasAttr<LifetimeCaptureByAttr>()) {
@@ -3919,7 +3919,24 @@ static void HandleLifetimeCaptureByAttr(Sema &S, Decl *D,
39193919

39203920
void Sema::LazyProcessLifetimeCaptureByParams(FunctionDecl *FD) {
39213921
bool HasImplicitThisParam = isInstanceMethod(FD);
3922-
3922+
SmallVector<LifetimeCaptureByAttr *, 1> Attrs;
3923+
for (ParmVarDecl *PVD : FD->parameters())
3924+
if (auto *A = PVD->getAttr<LifetimeCaptureByAttr>())
3925+
Attrs.push_back(A);
3926+
if (HasImplicitThisParam) {
3927+
TypeSourceInfo *TSI = FD->getTypeSourceInfo();
3928+
if (!TSI)
3929+
return;
3930+
AttributedTypeLoc ATL;
3931+
for (TypeLoc TL = TSI->getTypeLoc();
3932+
(ATL = TL.getAsAdjusted<AttributedTypeLoc>());
3933+
TL = ATL.getModifiedLoc()) {
3934+
if (auto *A = ATL.getAttrAs<LifetimeCaptureByAttr>())
3935+
Attrs.push_back(const_cast<LifetimeCaptureByAttr *>(A));
3936+
}
3937+
}
3938+
if (Attrs.empty())
3939+
return;
39233940
llvm::StringMap<int> NameIdxMapping;
39243941
NameIdxMapping["global"] = LifetimeCaptureByAttr::GLOBAL;
39253942
NameIdxMapping["unknown"] = LifetimeCaptureByAttr::UNKNOWN;
@@ -3957,22 +3974,8 @@ void Sema::LazyProcessLifetimeCaptureByParams(FunctionDecl *FD) {
39573974
CapturedBy->setParamIdx(I, It->second);
39583975
}
39593976
};
3960-
for (ParmVarDecl *PVD : FD->parameters())
3961-
HandleCaptureBy(PVD->getAttr<LifetimeCaptureByAttr>());
3962-
if (!HasImplicitThisParam)
3963-
return;
3964-
TypeSourceInfo *TSI = FD->getTypeSourceInfo();
3965-
if (!TSI)
3966-
return;
3967-
AttributedTypeLoc ATL;
3968-
for (TypeLoc TL = TSI->getTypeLoc();
3969-
(ATL = TL.getAsAdjusted<AttributedTypeLoc>());
3970-
TL = ATL.getModifiedLoc()) {
3971-
auto *A = ATL.getAttrAs<LifetimeCaptureByAttr>();
3972-
if (!A)
3973-
continue;
3974-
HandleCaptureBy(const_cast<LifetimeCaptureByAttr *>(A));
3975-
}
3977+
for (auto *A : Attrs)
3978+
HandleCaptureBy(A);
39763979
}
39773980

39783981
static bool isFunctionLike(const Type &T) {
@@ -6753,7 +6756,7 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
67536756
handleCallbackAttr(S, D, AL);
67546757
break;
67556758
case ParsedAttr::AT_LifetimeCaptureBy:
6756-
HandleLifetimeCaptureByAttr(S, D, AL);
6759+
handleLifetimeCaptureByAttr(S, D, AL);
67576760
break;
67586761
case ParsedAttr::AT_CalledOnce:
67596762
handleCalledOnceAttr(S, D, AL);

0 commit comments

Comments
 (0)