[clang] Enable unguarded availability diagnostic on instantiated template functions (PR #91699)

Helena Kotas via cfe-commits cfe-commits at lists.llvm.org
Fri May 10 12:13:44 PDT 2024


https://github.com/hekota updated https://github.com/llvm/llvm-project/pull/91699

>From 6220a5f61f92c3161772fd7c2be9d387169f8e03 Mon Sep 17 00:00:00 2001
From: Helena Kotas <hekotas at microsoft.com>
Date: Thu, 9 May 2024 22:14:05 -0700
Subject: [PATCH 1/2] Enable unguarded availability diagnostic on instantiated
 template functions

---
 clang/lib/Sema/SemaAvailability.cpp          |  5 -----
 clang/test/SemaObjC/unguarded-availability.m | 11 +++++++----
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Sema/SemaAvailability.cpp b/clang/lib/Sema/SemaAvailability.cpp
index 846a31a796730..5b0f34d1aa3aa 100644
--- a/clang/lib/Sema/SemaAvailability.cpp
+++ b/clang/lib/Sema/SemaAvailability.cpp
@@ -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))
diff --git a/clang/test/SemaObjC/unguarded-availability.m b/clang/test/SemaObjC/unguarded-availability.m
index d0e23eabcb598..3a38c3b40c4d6 100644
--- a/clang/test/SemaObjC/unguarded-availability.m
+++ b/clang/test/SemaObjC/unguarded-availability.m
@@ -177,16 +177,19 @@ 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());
+  // 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
 
 template <class> int use_at_available() {
   if (@available(macos 10.12, *))

>From 57306ebc1f9b1cbb61142ad698942a4e81c4f2f3 Mon Sep 17 00:00:00 2001
From: Helena Kotas <hekotas at microsoft.com>
Date: Fri, 10 May 2024 12:13:21 -0700
Subject: [PATCH 2/2] Add more test cases

---
 clang/test/SemaObjC/unguarded-availability.m | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/clang/test/SemaObjC/unguarded-availability.m b/clang/test/SemaObjC/unguarded-availability.m
index 3a38c3b40c4d6..ecd91990174ae 100644
--- a/clang/test/SemaObjC/unguarded-availability.m
+++ b/clang/test/SemaObjC/unguarded-availability.m
@@ -181,16 +181,25 @@ void justAtAvailable(void) {
 int f(int);
 
 template <class T> int use_f() {
+  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
+    return f(T()); // #f_call
+  }
 }
 
 int a = use_f<int>();
 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, *))
     return func_10_12();



More information about the cfe-commits mailing list