Skip to content

Enable unguarded availability diagnostic on instantiated template functions #91699

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 2 commits into from
May 24, 2024
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
5 changes: 0 additions & 5 deletions clang/lib/Sema/SemaAvailability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,11 +928,6 @@ void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) {
Stmt *Body = nullptr;

if (auto *FD = D->getAsFunction()) {
// FIXME: We only examine the pattern decl for availability violations now,
// but we should also examine instantiated templates.
if (FD->isTemplateInstantiation())
return;

Body = FD->getBody();

if (auto *CD = dyn_cast<CXXConstructorDecl>(FD))
Expand Down
20 changes: 16 additions & 4 deletions clang/test/SemaObjC/unguarded-availability.m
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,28 @@ void justAtAvailable(void) {

#ifdef OBJCPP

int f(char) AVAILABLE_10_12;
int f(char) AVAILABLE_10_12; // #f_char_def
int f(int);

template <class T> int use_f() {
// FIXME: We should warn here!
return f(T());
Copy link
Member

Choose a reason for hiding this comment

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

Could you check that we don't emit a warning if there is an availability attribute on the enclosing function or if the use is guarded by an if (@available(...)) check? IIRC that that was what I was concerned about when I wrote this. @jansvoboda11 : someone at Apple should probably review this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the quick response! I have added the test cases you requested. No additional warnings are emitted.

if (@available(macos 10.12, *)) {
return f(T()); // no warning expected
} else {
// expected-warning@#f_call {{'f' is only available on macOS 10.12 or newer}}
// expected-note@#f_char_inst {{in instantiation of function template specialization 'use_f<char>' requested here}}
// expected-note@#f_char_def {{'f' has been marked as being introduced in macOS 10.12 here, but the deployment target is macOS 10.9}}
// expected-note@#f_call {{enclose 'f' in an @available check to silence this warning}}
return f(T()); // #f_call
}
}

int a = use_f<int>();
int b = use_f<char>();
int b = use_f<char>(); // #f_char_inst

int use_f2() AVAILABLE_10_12 {
int c = use_f<int>();
int d = use_f<char>(); // no warning expected
}

template <class> int use_at_available() {
if (@available(macos 10.12, *))
Expand Down