Author: Cyndy Ishida
Date: 2025-05-08T21:18:52-07:00
New Revision: 74e5a3b61e87c8c2b830286796b72dda67942b6b

URL: 
https://github.com/llvm/llvm-project/commit/74e5a3b61e87c8c2b830286796b72dda67942b6b
DIFF: 
https://github.com/llvm/llvm-project/commit/74e5a3b61e87c8c2b830286796b72dda67942b6b.diff

LOG: [clang] Remove "unknown" from availability diags (#138610)

Previously, diagnostics like `error: 'fNew' is unavailable: introduced
in macOS 11 unknown` were getting emitted when the active target triple
didn't have an environment tied to it. Instead, add a guard against this
to avoid the `unknown`.

Added: 
    clang/test/Driver/attr-availability-erroneous-diags.c

Modified: 
    clang/lib/AST/DeclBase.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp
index aea19c51401aa..2052c0c7cfe42 100644
--- a/clang/lib/AST/DeclBase.cpp
+++ b/clang/lib/AST/DeclBase.cpp
@@ -695,27 +695,31 @@ 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
-    if (!IIEnv || (!TargetEnv.empty() && IIEnv->getName() == TargetEnv)) {
+    auto &Triple = Context.getTargetInfo().getTriple();
+    StringRef TargetEnv = Triple.getEnvironmentName();
+    StringRef EnvName =
+        llvm::Triple::getEnvironmentTypeName(Triple.getEnvironment());
+    // Matching environment or no environment on attribute.
+    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
+    // Non-matching environment or no environment on target.
     else {
       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