https://github.com/cyndyishida updated 
https://github.com/llvm/llvm-project/pull/138610

>From d642c83835743409395a2b7a091eac8bf5c948c4 Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ish...@apple.com>
Date: Mon, 5 May 2025 16:09:00 -0700
Subject: [PATCH 1/2] [clang] Remove "unknown" from availability diags

Previously, diagnostics like `error: 'fNew' is unavailable: introduced in macOS 
11 unknown` were getting emitted
when the active target triple didn't have a enviornment tied to it. Instead, 
add a guard against this to avoid the `unknown`.
---
 clang/lib/AST/DeclBase.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index fead99c5f28a9..47857e7fd523b 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -695,11 +695,13 @@ static AvailabilityResult CheckAvailability(ASTContext 
&Context,
   if (!A->getIntroduced().empty() &&
       EnclosingVersion < A->getIntroduced()) {
     IdentifierInfo *IIEnv = A->getEnvironment();
-    StringRef TargetEnv =
-        Context.getTargetInfo().getTriple().getEnvironmentName();
-    StringRef EnvName = llvm::Triple::getEnvironmentTypeName(
-        Context.getTargetInfo().getTriple().getEnvironment());
-    // Matching environment or no environment on attribute
+    auto &Triple = Context.getTargetInfo().getTriple();
+    StringRef TargetEnv = Triple.getEnvironmentName();
+    StringRef EnvName =
+        Triple.hasEnvironment()
+            ? llvm::Triple::getEnvironmentTypeName(Triple.getEnvironment())
+            : "";
+    // Matching environment or no environment on attribute.
     if (!IIEnv || (!TargetEnv.empty() && IIEnv->getName() == TargetEnv)) {
       if (Message) {
         Message->clear();
@@ -709,7 +711,7 @@ static AvailabilityResult CheckAvailability(ASTContext 
&Context,
             << EnvName << HintMessage;
       }
     }
-    // Non-matching environment or no environment on target
+    // Non-matching environment or no environment on target.
     else {
       if (Message) {
         Message->clear();

>From 9ee9dd6b9bd5e10bebacbfb840baf80af90bcb97 Mon Sep 17 00:00:00 2001
From: Cyndy Ishida <cyndy_ish...@apple.com>
Date: Thu, 8 May 2025 09:15:38 -0700
Subject: [PATCH 2/2] Address review comments

---
 clang/lib/AST/DeclBase.cpp                     | 18 ++++++++++--------
 .../Driver/attr-availability-erroneous-diags.c | 10 ++++++++++
 2 files changed, 20 insertions(+), 8 deletions(-)
 create mode 100644 clang/test/Driver/attr-availability-erroneous-diags.c

diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index 47857e7fd523b..a01c769ee9bcf 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -698,17 +698,17 @@ static AvailabilityResult CheckAvailability(ASTContext 
&Context,
     auto &Triple = Context.getTargetInfo().getTriple();
     StringRef TargetEnv = Triple.getEnvironmentName();
     StringRef EnvName =
-        Triple.hasEnvironment()
-            ? llvm::Triple::getEnvironmentTypeName(Triple.getEnvironment())
-            : "";
+        llvm::Triple::getEnvironmentTypeName(Triple.getEnvironment());
     // Matching environment or no environment on attribute.
-    if (!IIEnv || (!TargetEnv.empty() && IIEnv->getName() == TargetEnv)) {
+    if (!IIEnv || (Triple.hasEnvironment() && IIEnv->getName() == TargetEnv)) {
       if (Message) {
         Message->clear();
         llvm::raw_string_ostream Out(*Message);
         VersionTuple VTI(A->getIntroduced());
-        Out << "introduced in " << PrettyPlatformName << " " << VTI << " "
-            << EnvName << HintMessage;
+        Out << "introduced in " << PrettyPlatformName << " " << VTI;
+        if (Triple.hasEnvironment())
+          Out << " " << EnvName;
+        Out << HintMessage;
       }
     }
     // Non-matching environment or no environment on target.
@@ -716,8 +716,10 @@ static AvailabilityResult CheckAvailability(ASTContext 
&Context,
       if (Message) {
         Message->clear();
         llvm::raw_string_ostream Out(*Message);
-        Out << "not available on " << PrettyPlatformName << " " << EnvName
-            << HintMessage;
+        Out << "not available on " << PrettyPlatformName;
+        if (Triple.hasEnvironment())
+          Out << " " << EnvName;
+        Out << HintMessage;
       }
     }
 
diff --git a/clang/test/Driver/attr-availability-erroneous-diags.c 
b/clang/test/Driver/attr-availability-erroneous-diags.c
new file mode 100644
index 0000000000000..5e67a461f3e19
--- /dev/null
+++ b/clang/test/Driver/attr-availability-erroneous-diags.c
@@ -0,0 +1,10 @@
+// RUN: not %clang -target x86_64-apple-darwin9 -fsyntax-only %s 2>&1 | 
FileCheck %s
+
+// CHECK: error:
+// CHECK-SAME: 'f0' is unavailable: introduced in macOS 11
+// CHECK-NOT: unknown 
+
+void f0(void) __attribute__((availability(macosx,strict,introduced=11)));
+
+void client(void) {
+f0(); }

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to