[clang] [Clang][HLSL] Add environment parameter to availability attribute (PR #89809)

Chris B via cfe-commits cfe-commits at lists.llvm.org
Tue May 14 08:07:44 PDT 2024


================
@@ -376,26 +400,46 @@ static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
     // not specified for deployment targets >= to iOS 11 or equivalent or
     // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
     // later.
-    const AvailabilityAttr *AA =
-        getAttrForPlatform(S.getASTContext(), OffendingDecl);
+    assert(AA != nullptr && "expecting valid availability attribute");
     VersionTuple Introduced = AA->getIntroduced();
+    bool EnvironmentMatchesOrNone =
+        hasMatchingEnvironmentOrNone(S.getASTContext(), AA);
+
+    const TargetInfo &TI = S.getASTContext().getTargetInfo();
+    std::string PlatformName(
+        AvailabilityAttr::getPrettyPlatformName(TI.getPlatformName()));
+    llvm::StringRef TargetEnvironment(AvailabilityAttr::getPrettyEnviromentName(
+        TI.getTriple().getEnvironmentName()));
+    llvm::StringRef AttrEnvironment =
+        AA->getEnvironment() ? AvailabilityAttr::getPrettyEnviromentName(
+                                   AA->getEnvironment()->getName())
+                             : "";
+    bool UseEnvironment =
+        (!AttrEnvironment.empty() && !TargetEnvironment.empty());
 
     bool UseNewWarning = shouldDiagnoseAvailabilityByDefault(
         S.Context, S.Context.getTargetInfo().getPlatformMinVersion(),
         Introduced);
-    unsigned Warning = UseNewWarning ? diag::warn_unguarded_availability_new
-                                     : diag::warn_unguarded_availability;
-
-    std::string PlatformName(AvailabilityAttr::getPrettyPlatformName(
-        S.getASTContext().getTargetInfo().getPlatformName()));
 
-    S.Diag(Loc, Warning) << OffendingDecl << PlatformName
-                         << Introduced.getAsString();
+    if (EnvironmentMatchesOrNone) {
+      unsigned DiagKind = UseNewWarning ? diag::warn_unguarded_availability_new
+                                        : diag::warn_unguarded_availability;
+
+      S.Diag(Loc, DiagKind)
+          << OffendingDecl << PlatformName << Introduced.getAsString()
+          << UseEnvironment << TargetEnvironment;
+    } else {
+      unsigned DiagKind =
+          UseNewWarning ? diag::warn_unguarded_availability_unavailable_new
+                        : diag::warn_unguarded_availability_unavailable;
+      S.Diag(Loc, DiagKind) << Loc << OffendingDecl;
+    }
----------------
llvm-beanz wrote:

What about something like this?
```suggestion
    unsigned Warning =
        EnvironmentMatchesOrNone
            ? (UseNewWarning ? diag::warn_unguarded_availability_new
                             : diag::warn_unguarded_availability)
            : (UseNewWarning ? diag::warn_unguarded_availability_unavailable_new
                             : diag::warn_unguarded_availability_unavailable);
    S.Diag(Loc, DiagKind) << OffendingDecl << PlatformName
                          << Introduced.getAsString() << UseEnvironment
                          << TargetEnvironment;
```

The the new `warn_unguarded_availability_unavailable` has strictly a subset of the arguments for the other diagnostic so we should be able to just substitute them like this.

https://github.com/llvm/llvm-project/pull/89809


More information about the cfe-commits mailing list