-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang] Introduce [[clang::lifetime_capture_by(X)]] #111499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
#include "clang/AST/ASTContext.h" | ||
#include "clang/AST/ASTMutationListener.h" | ||
#include "clang/AST/CXXInheritance.h" | ||
#include "clang/AST/Decl.h" | ||
#include "clang/AST/DeclCXX.h" | ||
#include "clang/AST/DeclObjC.h" | ||
#include "clang/AST/DeclTemplate.h" | ||
|
@@ -3867,6 +3868,113 @@ static void handleCallbackAttr(Sema &S, Decl *D, const ParsedAttr &AL) { | |
S.Context, AL, EncodingIndices.data(), EncodingIndices.size())); | ||
} | ||
|
||
LifetimeCaptureByAttr *Sema::ParseLifetimeCaptureByAttr(const ParsedAttr &AL, | ||
StringRef ParamName) { | ||
// Atleast one capture by is required. | ||
if (AL.getNumArgs() == 0) { | ||
Diag(AL.getLoc(), diag::err_capture_by_attribute_no_entity) | ||
<< AL.getRange(); | ||
return nullptr; | ||
} | ||
SmallVector<IdentifierInfo *, 1> ParamIdents; | ||
SmallVector<SourceLocation, 1> ParamLocs; | ||
for (unsigned I = 0; I < AL.getNumArgs(); ++I) { | ||
if (AL.isArgExpr(I)) { | ||
Expr *E = AL.getArgAsExpr(I); | ||
Diag(E->getExprLoc(), diag::err_capture_by_attribute_argument_unknown) | ||
<< E << E->getExprLoc(); | ||
continue; | ||
} | ||
assert(AL.isArgIdent(I)); | ||
IdentifierLoc *IdLoc = AL.getArgAsIdent(I); | ||
if (IdLoc->Ident->getName() == ParamName) { | ||
Diag(IdLoc->Loc, diag::err_capture_by_references_itself) << IdLoc->Loc; | ||
continue; | ||
} | ||
ParamIdents.push_back(IdLoc->Ident); | ||
ParamLocs.push_back(IdLoc->Loc); | ||
} | ||
SmallVector<int, 1> FakeParamIndices(ParamIdents.size(), | ||
LifetimeCaptureByAttr::INVALID); | ||
LifetimeCaptureByAttr *CapturedBy = ::new (Context) LifetimeCaptureByAttr( | ||
Context, AL, FakeParamIndices.data(), FakeParamIndices.size()); | ||
CapturedBy->setArgs(std::move(ParamIdents), std::move(ParamLocs)); | ||
return CapturedBy; | ||
} | ||
|
||
static void HandleLifetimeCaptureByAttr(Sema &S, Decl *D, | ||
const ParsedAttr &AL) { | ||
// Do not allow multiple attributes. | ||
if (D->hasAttr<LifetimeCaptureByAttr>()) { | ||
S.Diag(AL.getLoc(), diag::err_capture_by_attribute_multiple) | ||
<< AL.getRange(); | ||
return; | ||
} | ||
auto *PVD = dyn_cast<ParmVarDecl>(D); | ||
assert(PVD); | ||
auto *CaptureByAttr = S.ParseLifetimeCaptureByAttr(AL, PVD->getName()); | ||
if (CaptureByAttr) | ||
D->addAttr(CaptureByAttr); | ||
} | ||
|
||
void Sema::LazyProcessLifetimeCaptureByParams(FunctionDecl *FD) { | ||
bool HasImplicitThisParam = isInstanceMethod(FD); | ||
|
||
llvm::StringMap<int> NameIdxMapping; | ||
NameIdxMapping["global"] = LifetimeCaptureByAttr::GLOBAL; | ||
NameIdxMapping["unknown"] = LifetimeCaptureByAttr::UNKNOWN; | ||
int Idx = 0; | ||
if (HasImplicitThisParam) { | ||
NameIdxMapping["this"] = 0; | ||
Idx++; | ||
} | ||
for (const ParmVarDecl *PVD : FD->parameters()) | ||
NameIdxMapping[PVD->getName()] = Idx++; | ||
auto DisallowReservedParams = [&](StringRef Reserved) { | ||
for (const ParmVarDecl *PVD : FD->parameters()) | ||
if (PVD->getName() == Reserved) | ||
Diag(PVD->getLocation(), diag::err_capture_by_param_uses_reserved_name) | ||
<< (PVD->getName() == "unknown"); | ||
}; | ||
auto HandleCaptureBy = [&](LifetimeCaptureByAttr *CapturedBy) { | ||
if (!CapturedBy) | ||
return; | ||
const auto &Entities = CapturedBy->getArgIdents(); | ||
for (size_t I = 0; I < Entities.size(); ++I) { | ||
StringRef Name = Entities[I]->getName(); | ||
auto It = NameIdxMapping.find(Name); | ||
if (It == NameIdxMapping.end()) { | ||
auto Loc = CapturedBy->getArgLocs()[I]; | ||
if (!HasImplicitThisParam && Name == "this") | ||
Diag(Loc, diag::err_capture_by_implicit_this_not_available) << Loc; | ||
else | ||
Diag(Loc, diag::err_capture_by_attribute_argument_unknown) | ||
<< Entities[I] << Loc; | ||
continue; | ||
} | ||
if (Name == "unknown" || Name == "global") | ||
DisallowReservedParams(Name); | ||
CapturedBy->setParamIdx(I, It->second); | ||
} | ||
}; | ||
for (ParmVarDecl *PVD : FD->parameters()) | ||
HandleCaptureBy(PVD->getAttr<LifetimeCaptureByAttr>()); | ||
if (!HasImplicitThisParam) | ||
return; | ||
TypeSourceInfo *TSI = FD->getTypeSourceInfo(); | ||
if (!TSI) | ||
return; | ||
AttributedTypeLoc ATL; | ||
for (TypeLoc TL = TSI->getTypeLoc(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessarily for this PR, but I see this kind of loops many times in Sema code. I wonder if this is an indication that TSI should have a more convenient API to get a specific |
||
(ATL = TL.getAsAdjusted<AttributedTypeLoc>()); | ||
TL = ATL.getModifiedLoc()) { | ||
auto *A = ATL.getAttrAs<LifetimeCaptureByAttr>(); | ||
if (!A) | ||
continue; | ||
HandleCaptureBy(const_cast<LifetimeCaptureByAttr *>(A)); | ||
} | ||
} | ||
|
||
static bool isFunctionLike(const Type &T) { | ||
// Check for explicit function types. | ||
// 'called_once' is only supported in Objective-C and it has | ||
|
@@ -6644,6 +6752,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL, | |
case ParsedAttr::AT_Callback: | ||
handleCallbackAttr(S, D, AL); | ||
break; | ||
case ParsedAttr::AT_LifetimeCaptureBy: | ||
HandleLifetimeCaptureByAttr(S, D, AL); | ||
break; | ||
case ParsedAttr::AT_CalledOnce: | ||
handleCalledOnceAttr(S, D, AL); | ||
break; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUN: %clang_cc1 %s -ast-dump | FileCheck %s | ||
|
||
// Verify that we print the [[clang::lifetime_capture_by(X)]] attribute. | ||
|
||
struct S { | ||
void foo(int &a, int &b) [[clang::lifetime_capture_by(a, b, global)]]; | ||
}; | ||
|
||
// CHECK: CXXMethodDecl {{.*}}clang::lifetime_capture_by(a, b, global) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish we had one universal way of indexing parameters in Clang. I think probably most attributes start from 1, but there are APINotes that start from 0. Not really an actionable comment here, more like a rant.