Skip to content

Commit 9b22359

Browse files
committed
Fix compile time regression and memory leak
1 parent 3c233df commit 9b22359

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
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: 25 additions & 26 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;
@@ -3936,9 +3953,7 @@ void Sema::LazyProcessLifetimeCaptureByParams(FunctionDecl *FD) {
39363953
Diag(PVD->getLocation(), diag::err_capture_by_param_uses_reserved_name)
39373954
<< (PVD->getName() == "unknown");
39383955
};
3939-
auto HandleCaptureBy = [&](LifetimeCaptureByAttr *CapturedBy) {
3940-
if (!CapturedBy)
3941-
return;
3956+
for (auto *CapturedBy : Attrs) {
39423957
const auto &Entities = CapturedBy->getArgIdents();
39433958
for (size_t I = 0; I < Entities.size(); ++I) {
39443959
StringRef Name = Entities[I]->getName();
@@ -3956,22 +3971,6 @@ void Sema::LazyProcessLifetimeCaptureByParams(FunctionDecl *FD) {
39563971
DisallowReservedParams(Name);
39573972
CapturedBy->setParamIdx(I, It->second);
39583973
}
3959-
};
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));
39753974
}
39763975
}
39773976

@@ -6753,7 +6752,7 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
67536752
handleCallbackAttr(S, D, AL);
67546753
break;
67556754
case ParsedAttr::AT_LifetimeCaptureBy:
6756-
HandleLifetimeCaptureByAttr(S, D, AL);
6755+
handleLifetimeCaptureByAttr(S, D, AL);
67576756
break;
67586757
case ParsedAttr::AT_CalledOnce:
67596758
handleCalledOnceAttr(S, D, AL);

0 commit comments

Comments
 (0)