Skip to content

Add IsAllocator and IsDeallocator API functions#1020

Open
keremsahn wants to merge 3 commits into
compiler-research:mainfrom
keremsahn:is-allocator
Open

Add IsAllocator and IsDeallocator API functions#1020
keremsahn wants to merge 3 commits into
compiler-research:mainfrom
keremsahn:is-allocator

Conversation

@keremsahn

Copy link
Copy Markdown
Contributor

Adds enum classes for allocation and deallocation types, creates API functions for queries.

@keremsahn

Copy link
Copy Markdown
Contributor Author

Adds enum classes for allocation and deallocation types, creates API functions for queries.

@Vipul-Cariappa this PR is a prototype, I just searched for most obvious attributes there are more attributes should be added, please provide me feedback. Also it seems not to be possible to detect allocation-deallocation type via attributes. So it may not to be a good idea to build query functions based on attribute searching

@codecov

codecov Bot commented Jun 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.54839% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.71%. Comparing base (91a0c1e) to head (a93b347).

Files with missing lines Patch % Lines
lib/CppInterOp/CppInterOp.cpp 93.54% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1020      +/-   ##
==========================================
+ Coverage   86.62%   86.71%   +0.08%     
==========================================
  Files          23       23              
  Lines        5946     5977      +31     
==========================================
+ Hits         5151     5183      +32     
+ Misses        795      794       -1     
Files with missing lines Coverage Δ
lib/CppInterOp/CppInterOp.cpp 89.16% <93.54%> (+0.04%) ⬆️

... and 1 file with indirect coverage changes

Files with missing lines Coverage Δ
lib/CppInterOp/CppInterOp.cpp 89.16% <93.54%> (+0.04%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@keremsahn keremsahn force-pushed the is-allocator branch 2 times, most recently from 3b0f128 to 4b90462 Compare June 2, 2026 02:44
@Vipul-Cariappa Vipul-Cariappa self-requested a review June 2, 2026 09:10

@Vipul-Cariappa Vipul-Cariappa left a comment

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.

This looks good. Nice job. Some minor comments.

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment on lines +1563 to +1567
assert(Fn && "Not func");
auto* D = static_cast<const Decl*>(Fn);
assert(D && "Not func");
auto* FD = dyn_cast<FunctionDecl>(D);
assert(FD && "Not func");

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.

We don't want to crash the application if Fn = nullptr, instead if return a sensible default. In this case, false. You should remove the the asserts.
Asserts are used to detect bugs within the CppInterOp codebase itself. In this case, it is the user providing a null value.

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment on lines +1585 to +1589
assert(Fn && "Not func");
auto* D = static_cast<const Decl*>(Fn);
assert(D && "Not func");
auto* FD = dyn_cast<FunctionDecl>(D);
assert(FD && "Not func");

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.

Please remove the asserts. As explained above.

Comment thread include/CppInterOp/CppInterOpTypes.h Outdated
Comment on lines +239 to +253
enum class AllocType : unsigned char {
New,
NewArr,
Malloc,
None,
Unknown // Could not detect type, for now
};

enum class DeallocType : unsigned char {
Delete,
DeleteArr,
Free,
None,
Unknown // Could not detect type, for now
};

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.

These are not yet used. So let's remove them from this PR. But this looks good. We should model based on this. Are there attributes that differentiate between malloc and new?
Also looking at https://clang.llvm.org/docs/AttributeReference.html#ownership-holds-ownership-returns-ownership-takes-clang-static-analyzer. There is ownership_holds. I believe the DeallocType should also express that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah this is a problem we can not differentiate allocation-deallocation types from attributes. I will change the PR by your suggestions next week.

@keremsahn keremsahn Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These are not yet used. So let's remove them from this PR. But this looks good. We should model based on this. Are there attributes that differentiate between malloc and new? Also looking at https://clang.llvm.org/docs/AttributeReference.html#ownership-holds-ownership-returns-ownership-takes-clang-static-analyzer. There is ownership_holds. I believe the DeallocType should also express that.

I did not handle OwnershipHolds in current commit but this can be another ownership category, probably cppyy will handle this same as a OwnershipTakes but it may be better to distinguish them. What do you think

@Vipul-Cariappa

Copy link
Copy Markdown
Collaborator

Please consider rebasing the PR before continuing the work.

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
return INTEROP_RETURN(false);
}

bool IsDeallocator(TCppConstFunction_t Fn) {

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added for malloc and free, we may extend it for realloc etc. later

@keremsahn keremsahn force-pushed the is-allocator branch 2 times, most recently from 34e247f to e11ab32 Compare June 11, 2026 20:00

@github-actions github-actions Bot left a comment

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.

clang-tidy made some suggestions

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
if (Fn) {
if (auto* D = unwrap<clang::Decl>(Fn)) {
if (auto* FD = dyn_cast<FunctionDecl>(D)) {
if (FD->getBuiltinID() == Builtin::ID::BImalloc)

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 "clang::Builtin::ID" is directly included [misc-include-cleaner]

lib/CppInterOp/CppInterOp.cpp:15:

+ #include <clang/Basic/Builtins.h>

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
@keremsahn keremsahn marked this pull request as draft June 16, 2026 16:00
@keremsahn keremsahn force-pushed the is-allocator branch 2 times, most recently from 6968612 to b4e1fd8 Compare June 16, 2026 16:23
@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@keremsahn

Copy link
Copy Markdown
Contributor Author

@vgvassilev @aaronj0 @Vipul-Cariappa Hello all, can you review the PR, it is getting larger and harder to debug for me

@keremsahn keremsahn marked this pull request as ready for review June 16, 2026 22:02

@github-actions github-actions Bot left a comment

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.

clang-tidy made some suggestions

Comment thread unittests/CppInterOp/APINotes/TestHeader.h
@keremsahn keremsahn marked this pull request as draft June 16, 2026 22:23

@github-actions github-actions Bot left a comment

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.

clang-tidy made some suggestions

@@ -0,0 +1,7 @@
#ifndef UNITTESTS_CPPINTEROP_APINOTES_TESTHEADER_H
#define UNITTESTS_CPPINTEROP_APINOTES_TESTHEADER_H

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: header guard does not follow preferred style [llvm-header-guard]

Suggested change
#define UNITTESTS_CPPINTEROP_APINOTES_TESTHEADER_H
#ifndef GITHUB_WORKSPACE_UNITTESTS_CPPINTEROP_APINOTES_TESTHEADER_H
#define GITHUB_WORKSPACE_UNITTESTS_CPPINTEROP_APINOTES_TESTHEADER_H

@keremsahn keremsahn marked this pull request as ready for review June 17, 2026 09:48

@Vipul-Cariappa Vipul-Cariappa left a comment

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.

Minor comments:

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment on lines +1598 to +1599
if (FDA->getSemanticSpelling() != RestrictAttr::Declspec_restrict)
return INTEROP_RETURN(true);

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
if (FDA->getSemanticSpelling() != RestrictAttr::Declspec_restrict)
return INTEROP_RETURN(true);
return INTEROP_RETURN(true);

Shouldn't this be the following?
I am looking at the definition of RestrictAttr::getSemanticSpelling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

https://learn.microsoft.com/en-us/cpp/cpp/restrict?view=msvc-170
The definition of declspec(restrict) is kinda different from gnu's restrict. It implies pointer returned by the function has no alias, so it is not a neccesity for function to allocate memory.

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
@keremsahn keremsahn requested a review from Vipul-Cariappa June 26, 2026 08:27

@Vipul-Cariappa Vipul-Cariappa left a comment

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.

LGTM! Just a small comment. You also need to rebase, so that I can merge.

Comment thread lib/CppInterOp/CppInterOp.cpp Outdated
Comment on lines +1571 to +1572
// FIXME: These attributes are not currently compatible with our memory
// representation, they are added for APINotes' test

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
// FIXME: These attributes are not currently compatible with our memory
// representation, they are added for APINotes' test

Note required here. This FIXME should be part of the other PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants