Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 100 additions & 13 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1974,21 +1974,108 @@ AllocType IsAllocator(ConstFuncRef Fn) {
return INTEROP_RETURN(AllocType::Unknown);
}

bool IsDeallocator(ConstFuncRef Fn) {
INTEROP_TRACE(Fn);
if (!Fn)
INTEROP_RETURN(false);
const auto* D = unwrap<clang::Decl>(Fn);
if (const auto* FD = dyn_cast<FunctionDecl>(D)) {
if (FD->getBuiltinID() == Builtin::ID::BIfree)
return INTEROP_RETURN(true);
if (const auto* FDA = FD->getAttr<OwnershipAttr>()) {
if (FDA->getOwnKind() == OwnershipAttr::Takes)
return INTEROP_RETURN(true);
// We follow Clang's convention for ownership attributes, parameters are 1
// indexed. Therefore if a function has an implicit this, you should use
// cppDeallocDelete_2 to labeling first actual parameter, also it is not allowed
// to label implicit `this`.
// Function appends a DeallocType to the vector taken, for every argument in
// the function including implicit this. If an argument does not have an
// deallocation ownership, Opaque value is added.
bool IsDeallocator(ConstFuncRef Fn, std::vector<DeallocType>& VPP) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::DeallocType" is directly included [misc-include-cleaner]

icit `this` exists.
                                                                    ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "Cpp::DeallocType" is directly included [misc-include-cleaner]

que value is added.
                                                                    ^

INTEROP_TRACE(Fn, INTEROP_OUT(VPP));
const auto* FD = UnwrapToFunctionDecl(unwrap<clang::Decl>(Fn));
if (!FD)
return INTEROP_RETURN(false);
const auto* MD = dyn_cast<CXXMethodDecl>(FD);
const bool hasImplicitThis = MD && MD->isImplicitObjectMemberFunction();
const unsigned numParam = FD->getNumParams();
std::vector<DeallocType> newVPP(numParam + hasImplicitThis,
DeallocType::Opaque);
if (FD->getBuiltinID() == Builtin::ID::BIfree) {
VPP.push_back(DeallocType::Free);
return INTEROP_RETURN(true);
}
Comment on lines +1992 to +1997

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<DeallocType> newVPP(numParam + hasImplicitThis,
DeallocType::Opaque);
if (FD->getBuiltinID() == Builtin::ID::BIfree) {
VPP.push_back(DeallocType::Free);
return INTEROP_RETURN(true);
}
if (FD->getBuiltinID() == Builtin::ID::BIfree) {
VPP.push_back(DeallocType::Free);
return INTEROP_RETURN(true);
}
std::vector<DeallocType> newVPP(numParam + hasImplicitThis,
DeallocType::Opaque);

The vector can be create after this if

for (const auto* FDA : FD->specific_attrs<OwnershipAttr>()) {
// If it does not return, either holds or takes
if (FDA->getOwnKind() == OwnershipAttr::Returns)
continue;
// If GCC's ownership attrs are used, it is assumed to be free
for (const ParamIdx index : FDA->args())
// Self can not be attributed with ownership_attr
newVPP[index.getLLVMIndex()] = DeallocType::Free;
}
// Traverse annotate_attrs
for (const auto* FDA : FD->specific_attrs<AnnotateAttr>()) {
llvm::StringRef attrName = FDA->getAnnotation();
DeallocType kind;
if (attrName == "cppDeallocFree")
kind = DeallocType::Free;
else if (attrName == "cppDeallocDelete")
kind = DeallocType::Delete;
else if (attrName == "cppDeallocDeleteArr")
kind = DeallocType::DeleteArr;
else if (attrName == "cppDeallocNone")
kind = DeallocType::None;
// If annotate_attr is any other thing continue
else
continue;
// If attribute has no argument, it is assumed to attribute
// for first index (after implicit this)
if (FDA->args_size() == 0) {
if (numParam >= 1)
newVPP[hasImplicitThis] = kind;
continue;
}
// Traverse all args of annotate_attr
for (const auto* E : FDA->args()) {
// Since we support FTDs, we must do that check
if (E->isValueDependent())
continue;
Expr::EvalResult evalRes;
// Not int
if (!E->EvaluateAsInt(evalRes, FD->getASTContext()))
continue;
const int64_t index = evalRes.Val.getInt().getSExtValue();
if (index < 1 || (uint64_t)index > newVPP.size() ||
(index == 1 && hasImplicitThis))
continue;
newVPP[index - 1] = kind;
}
}

return INTEROP_RETURN(false);
// Traverse APINotes attrs
for (const auto* FDA : FD->specific_attrs<SwiftAttrAttr>()) {
llvm::StringRef attrName = FDA->getAttribute();
attrName.consume_front("returns_");
llvm::SmallVector<llvm::StringRef, 8> tokens;
attrName.split(tokens, ' ', -1, false);
for (llvm::StringRef token : tokens) {
DeallocType kind;
if (token.consume_front("cppDeallocDeleteArr"))
kind = DeallocType::DeleteArr;
else if (token.consume_front("cppDeallocDelete"))
kind = DeallocType::Delete;
else if (token.consume_front("cppDeallocFree"))
kind = DeallocType::Free;
else if (token.consume_front("cppDeallocNone"))
kind = DeallocType::None;
else
continue;
// If attribute is cppDealloc`DeallocType` without index,
// it is assumed to be attribute for first index (after implicit this)
if (token.empty()) {
if (numParam >= 1)
newVPP[hasImplicitThis] = kind;
continue;
}
unsigned index;
if (!token.consume_front("_") || token.getAsInteger(10, index) ||
index < 1 || index > newVPP.size() || (hasImplicitThis && index == 1))
continue;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cross-check this...

newVPP[index - 1] = kind;
}
}
VPP.insert(VPP.end(), newVPP.begin(), newVPP.end());
return INTEROP_RETURN(true);
}

bool IsFunctionProtoType(ConstTypeRef TyRef) {
Expand Down
12 changes: 10 additions & 2 deletions lib/CppInterOp/CppInterOp.td
Original file line number Diff line number Diff line change
Expand Up @@ -1365,10 +1365,18 @@ def IsAllocator : CppInterOpAPI {
}

def IsDeallocator : CppInterOpAPI {
let Doc = "Checks if the provided function is a deallocator function";
let Doc = [{Records the memory deallocation behaviour of each parameter of
the given function, as declared by its attributes. Appends one entry per
parameter to VPP, preceded by one entry for the object parameter if the
function has an implicit this; whatever VPP already holds is left
untouched. A parameter labelled cppDeallocNone is reported as None, while
one carrying no deallocation attribute is reported as Opaque. Labelling
the object parameter is not supported and is ignored. Returns false if
func is not a function, in which case VPP is not modified.}];
let ReturnType = "bool";
let Args = [
Arg<"ConstFuncRef", "func">
Arg<"ConstFuncRef", "func">,
OutArg<"std::vector<DeallocType>&", "VPP">
];
}

Expand Down
25 changes: 24 additions & 1 deletion unittests/CppInterOp/APINotes/TestHeader.apinotes
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,27 @@ Functions:
- Name: testNone
SwiftReturnOwnership: cppAllocNone
- Name: testWeirdAttr
SwiftReturnOwnership: someWeirdAttr
SwiftReturnOwnership: someWeirdAttr
- Name: testDeallocFree
SwiftReturnOwnership: cppDeallocFree_1
- Name: testDeallocDelete
SwiftReturnOwnership: cppDeallocDelete_1
- Name: testDeallocDeleteArr
SwiftReturnOwnership: cppDeallocDeleteArr_1
- Name: testDeallocNone
SwiftReturnOwnership: cppDeallocNone_1
- Name: testDeallocMulti
SwiftReturnOwnership: "cppDeallocDelete_1 cppDeallocFree_2"
- Name: testDeallocWeirdAttr
SwiftReturnOwnership: someWeirdDeallocAttr
Tags:
- Name: KlassNotes
Methods:
- Name: releaseArg
SwiftReturnOwnership: cppDeallocFree_2
- Name: releaseSelf
SwiftReturnOwnership: cppDeallocDelete_1
- Name: releaseBoth
SwiftReturnOwnership: "cppDeallocDelete_1 cppDeallocFree_2"
- Name: releaseBare
SwiftReturnOwnership: cppDeallocDelete
15 changes: 15 additions & 0 deletions unittests/CppInterOp/APINotes/TestHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ void* testOperatorNew();
void* testOperatorNewArr();
void* testNone();
void* testWeirdAttr();

void testDeallocFree(void* ptr);
void testDeallocDelete(void* ptr);
void testDeallocDeleteArr(void* ptr);
void testDeallocNone(void* ptr);
void testDeallocMulti(void* a, void* b);
void testDeallocWeirdAttr(void* ptr);

class KlassNotes {
public:
void releaseArg(void* ptr);
void releaseSelf();
void releaseBoth(void* ptr);
void releaseBare(void* ptr);
};
#endif
Loading
Loading