Skip to content

🍒[Clang] Permit noescape on non-pointer types #10735

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 1 commit into from
May 28, 2025

Conversation

egorzhdan
Copy link

@egorzhdan egorzhdan commented May 23, 2025

Original PR: #9789

rdar://151918708

In modern C++ we often use span, string_view or other view objects instead of
raw pointers. This PR opens the door to mark those noescape. This can be useful
to document the API contracts, for interop with memory safe languages like
Swift or Rust, and possibly in the future to implement take this into account
in codegen.

The PR also adds a feature test macro. The goal is to help avoiding warnings
when the code is compiler by earlier versions of clang that does not permit
this attribute on non-pointer types.

rdar://140196481
@egorzhdan egorzhdan requested review from Xazax-hun and hnrklssn May 23, 2025 15:27
@egorzhdan
Copy link
Author

@swift-ci please test LLVM

@egorzhdan egorzhdan requested a review from rapidsna May 23, 2025 18:08
Copy link

@hnrklssn hnrklssn left a comment

Choose a reason for hiding this comment

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

I know this is just a cherry-pick, but the diagnostic is broken and we should really fix it

@@ -3,5 +3,11 @@
template<typename T>
void test1(T __attribute__((noescape)) arr, int size);

// expected-warning@+1 {{'noescape' attribute only applies to pointer arguments}}
void test2(int __attribute__((noescape)) arr, int size);

Choose a reason for hiding this comment

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

I'm guessing this is also the case in the original patch, but this diagnostic looks off to me. 0 is invalid doesn't really mean anything. We should fix this.

CC @Xazax-hun

Copy link
Author

Choose a reason for hiding this comment

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

I agree that we could improve the phrasing. Let's wait until @Xazax-hun can work on it – this PR is just making the rebranch CI happy.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, I just re-read your comment again, and I now agree we should fix the 0 being printed instead of the actual type, let me put up a PR.

Copy link
Author

Choose a reason for hiding this comment

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

Patch up: #10747

S.Diag(AL.getLoc(), diag::warn_attribute_pointers_only)
if (!isValidPointerAttrType(T, /* RefOkay */ true) && !T->isRecordType()) {
S.Diag(AL.getLoc(),
diag::warn_attribute_pointer_or_reference_or_record_only)
<< AL << AL.getRange() << 0;

Choose a reason for hiding this comment

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

This is where the 0 comes from in the diagnostic. The previous diagnostic looks like this:

def warn_attribute_pointers_only : Warning<
  "%0 attribute only applies to%select{| constant}1 pointer arguments">,
  InGroup<IgnoredAttributes>;

So we likely want to do something like << T instead of << 0.

@@ -281,4 +281,30 @@ StringLiteral *FormatMatchesAttr::getFormatString() const {
return cast<StringLiteral>(getExpectedFormat());
}

bool clang::isValidPointerAttrType(QualType T, bool RefOkay) {

Choose a reason for hiding this comment

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

Should some of the changes like this also go to upstream clang as well?

Copy link
Author

Choose a reason for hiding this comment

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

Yeap, I think so! Gabor already started the upstreaming work in llvm#117344.

@egorzhdan
Copy link
Author

@hnrklssn is it alright if I land this now, followed by a cherry-pick of #10747? The CI failures seem unrelated.

Copy link

@hnrklssn hnrklssn left a comment

Choose a reason for hiding this comment

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

oh yeah for sure!

@egorzhdan egorzhdan merged commit ac8c2f1 into next May 28, 2025
0 of 2 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/next-noescape branch May 28, 2025 13:38
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