@@ -162,6 +162,20 @@ static bool ShouldDiagnoseAvailabilityInContext(
162
162
}
163
163
}
164
164
165
+ // In HLSL, skip emitting diagnostic if the diagnostic mode is not set to
166
+ // strict (-fhlsl-strict-availability), or if the target is library and the
167
+ // availability is restricted to a specific environment/shader stage.
168
+ // For libraries the availability will be checked later in
169
+ // DiagnoseHLSLAvailability class once where the specific environment/shader
170
+ // stage of the caller is known.
171
+ if (S.getLangOpts ().HLSL ) {
172
+ if (!S.getLangOpts ().HLSLStrictAvailability ||
173
+ (DeclEnv != nullptr &&
174
+ S.getASTContext ().getTargetInfo ().getTriple ().getEnvironment () ==
175
+ llvm::Triple::EnvironmentType::Library))
176
+ return false ;
177
+ }
178
+
165
179
// Checks if we should emit the availability diagnostic in the context of C.
166
180
auto CheckContext = [&](const Decl *C) {
167
181
if (K == AR_NotYetIntroduced) {
@@ -215,13 +229,16 @@ static bool ShouldDiagnoseAvailabilityInContext(
215
229
return true ;
216
230
}
217
231
218
- static bool
219
- shouldDiagnoseAvailabilityByDefault (const ASTContext &Context,
220
- const VersionTuple &DeploymentVersion,
221
- const VersionTuple &DeclVersion) {
232
+ static unsigned getAvailabilityDiagnosticKind (
233
+ const ASTContext &Context, const VersionTuple &DeploymentVersion,
234
+ const VersionTuple &DeclVersion, bool HasMatchingEnv) {
222
235
const auto &Triple = Context.getTargetInfo ().getTriple ();
223
236
VersionTuple ForceAvailabilityFromVersion;
224
237
switch (Triple.getOS ()) {
238
+ // For iOS, emit the diagnostic even if -Wunguarded-availability is
239
+ // not specified for deployment targets >= to iOS 11 or equivalent or
240
+ // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
241
+ // later.
225
242
case llvm::Triple::IOS:
226
243
case llvm::Triple::TvOS:
227
244
ForceAvailabilityFromVersion = VersionTuple (/* Major=*/ 11 );
@@ -233,16 +250,26 @@ shouldDiagnoseAvailabilityByDefault(const ASTContext &Context,
233
250
case llvm::Triple::MacOSX:
234
251
ForceAvailabilityFromVersion = VersionTuple (/* Major=*/ 10 , /* Minor=*/ 13 );
235
252
break ;
253
+ // For HLSL, use diagnostic from HLSLAvailability group which
254
+ // are reported as errors by default and in strict diagnostic mode
255
+ // (-fhlsl-strict-availability) and as warnings in relaxed diagnostic
256
+ // mode (-Wno-error=hlsl-availability)
236
257
case llvm::Triple::ShaderModel:
237
- // FIXME: This will be updated when HLSL strict diagnostic mode
238
- // is implemented (issue #90096)
239
- return false ;
258
+ return HasMatchingEnv ? diag::warn_hlsl_availability
259
+ : diag::warn_hlsl_availability_unavailable;
240
260
default :
241
- // New targets should always warn about availability.
242
- return Triple.getVendor () == llvm::Triple::Apple;
261
+ // New Apple targets should always warn about availability.
262
+ ForceAvailabilityFromVersion =
263
+ (Triple.getVendor () == llvm::Triple::Apple)
264
+ ? VersionTuple (/* Major=*/ 0 , 0 )
265
+ : VersionTuple (/* Major=*/ (unsigned )-1 , (unsigned )-1 );
243
266
}
244
- return DeploymentVersion >= ForceAvailabilityFromVersion ||
245
- DeclVersion >= ForceAvailabilityFromVersion;
267
+ if (DeploymentVersion >= ForceAvailabilityFromVersion ||
268
+ DeclVersion >= ForceAvailabilityFromVersion)
269
+ return HasMatchingEnv ? diag::warn_unguarded_availability_new
270
+ : diag::warn_unguarded_availability_unavailable_new;
271
+ return HasMatchingEnv ? diag::warn_unguarded_availability
272
+ : diag::warn_unguarded_availability_unavailable;
246
273
}
247
274
248
275
static NamedDecl *findEnclosingDeclToAnnotate (Decl *OrigCtx) {
@@ -415,26 +442,16 @@ static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
415
442
const TargetInfo &TI = S.getASTContext ().getTargetInfo ();
416
443
std::string PlatformName (
417
444
AvailabilityAttr::getPrettyPlatformName (TI.getPlatformName ()));
418
- llvm::StringRef TargetEnvironment (AvailabilityAttr::getPrettyEnviromentName (
419
- TI.getTriple ().getEnvironment ()));
445
+ llvm::StringRef TargetEnvironment (
446
+ llvm::Triple::getEnvironmentTypeName ( TI.getTriple ().getEnvironment ()));
420
447
llvm::StringRef AttrEnvironment =
421
- AA->getEnvironment () ? AvailabilityAttr::getPrettyEnviromentName (
422
- AvailabilityAttr::getEnvironmentType (
423
- AA->getEnvironment ()->getName ()))
424
- : " " ;
448
+ AA->getEnvironment () ? AA->getEnvironment ()->getName () : " " ;
425
449
bool UseEnvironment =
426
450
(!AttrEnvironment.empty () && !TargetEnvironment.empty ());
427
451
428
- bool UseNewWarning = shouldDiagnoseAvailabilityByDefault (
452
+ unsigned DiagKind = getAvailabilityDiagnosticKind (
429
453
S.Context , S.Context .getTargetInfo ().getPlatformMinVersion (),
430
- Introduced);
431
-
432
- unsigned DiagKind =
433
- EnvironmentMatchesOrNone
434
- ? (UseNewWarning ? diag::warn_unguarded_availability_new
435
- : diag::warn_unguarded_availability)
436
- : (UseNewWarning ? diag::warn_unguarded_availability_unavailable_new
437
- : diag::warn_unguarded_availability_unavailable);
454
+ Introduced, EnvironmentMatchesOrNone);
438
455
439
456
S.Diag (Loc, DiagKind) << OffendingDecl << PlatformName
440
457
<< Introduced.getAsString () << UseEnvironment
@@ -839,34 +856,19 @@ void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability(
839
856
OffendingDecl))
840
857
return ;
841
858
842
- // We would like to emit the diagnostic even if -Wunguarded-availability is
843
- // not specified for deployment targets >= to iOS 11 or equivalent or
844
- // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or
845
- // later.
846
- bool UseNewDiagKind = shouldDiagnoseAvailabilityByDefault (
847
- SemaRef.Context ,
848
- SemaRef.Context .getTargetInfo ().getPlatformMinVersion (), Introduced);
849
-
850
859
const TargetInfo &TI = SemaRef.getASTContext ().getTargetInfo ();
851
860
std::string PlatformName (
852
861
AvailabilityAttr::getPrettyPlatformName (TI.getPlatformName ()));
853
- llvm::StringRef TargetEnvironment (AvailabilityAttr::getPrettyEnviromentName (
854
- TI.getTriple ().getEnvironment ()));
862
+ llvm::StringRef TargetEnvironment (TI.getTriple ().getEnvironmentName ());
855
863
llvm::StringRef AttrEnvironment =
856
- AA->getEnvironment () ? AvailabilityAttr::getPrettyEnviromentName (
857
- AvailabilityAttr::getEnvironmentType (
858
- AA->getEnvironment ()->getName ()))
859
- : " " ;
864
+ AA->getEnvironment () ? AA->getEnvironment ()->getName () : " " ;
860
865
bool UseEnvironment =
861
866
(!AttrEnvironment.empty () && !TargetEnvironment.empty ());
862
867
863
- unsigned DiagKind =
864
- EnvironmentMatchesOrNone
865
- ? (UseNewDiagKind ? diag::warn_unguarded_availability_new
866
- : diag::warn_unguarded_availability)
867
- : (UseNewDiagKind
868
- ? diag::warn_unguarded_availability_unavailable_new
869
- : diag::warn_unguarded_availability_unavailable);
868
+ unsigned DiagKind = getAvailabilityDiagnosticKind (
869
+ SemaRef.Context ,
870
+ SemaRef.Context .getTargetInfo ().getPlatformMinVersion (), Introduced,
871
+ EnvironmentMatchesOrNone);
870
872
871
873
SemaRef.Diag (Range.getBegin (), DiagKind)
872
874
<< Range << D << PlatformName << Introduced.getAsString ()
0 commit comments