Skip to content

Commit

Permalink
Skip any guarded platform that was suppressed by call site
Browse files Browse the repository at this point in the history
  • Loading branch information
buyaa-n committed Feb 10, 2025
1 parent f121b81 commit 1b5fece
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public sealed partial class PlatformCompatibilityAnalyzer : DiagnosticAnalyzer
private const string macOS = nameof(macOS);
private const string OSX = nameof(OSX);
private const string MacSlashOSX = "macOS/OSX";
private const string ios = nameof(ios);
private const string maccatalyst = nameof(maccatalyst);
private static readonly Version EmptyVersion = new(0, 0);

internal static readonly DiagnosticDescriptor OnlySupportedCsReachable = DiagnosticDescriptorHelper.Create(
Expand Down Expand Up @@ -638,16 +636,11 @@ static bool IsKnownValueGuarded(
continue;
}

// Skip maccatalyst check in case it was suppressed by callsite attribute
if (parent.AnalysisValues.Count == 1 && attributes.ContainsKey(ios) && !attributes.ContainsKey(maccatalyst))
// Skip the platform check that was originally in the list and suppressed by callsite attributes
if (parent.AnalysisValues.Count == 1 &&
IsPlatformSupportWasSuppresed((PlatformMethodValue)parent.AnalysisValues.First(), attributes, originalAttributes))
{
PlatformMethodValue parentValue = (PlatformMethodValue)parent.AnalysisValues.First();
if (!parentValue.Negated && parentValue.PlatformName.Equals(maccatalyst, StringComparison.OrdinalIgnoreCase) &&
originalAttributes.TryGetValue(maccatalyst, out Versions? macVersion) &&
parentValue.Version.IsGreaterThanOrEqualTo(macVersion.SupportedFirst))
{
continue;
}
continue;
}
}

Expand All @@ -662,6 +655,11 @@ static bool IsKnownValueGuarded(
return true;
}

static bool IsPlatformSupportWasSuppresed(PlatformMethodValue parentValue, SmallDictionary<string, Versions> attributes, SmallDictionary<string, Versions> originalAttributes)
=> !parentValue.Negated && !attributes.ContainsKey(parentValue.PlatformName) &&
originalAttributes.TryGetValue(parentValue.PlatformName, out Versions? version) &&
parentValue.Version.IsGreaterThanOrEqualTo(version.SupportedFirst);

static bool IsOnlySupportNeedsGuard(string platformName, SmallDictionary<string, Versions> attributes, SmallDictionary<string, Versions> csAttributes)
=> csAttributes.TryGetValue(platformName, out var versions) &&
AllowList(versions) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5398,6 +5398,37 @@ public void DoSomething() {}
await VerifyAnalyzerCSAsync(source, msBuildPlatforms);
}

[Fact, WorkItem(7530, "https://github.com/dotnet/roslyn-analyzers/issues/7530")]
public async Task OneOfCustomGuardsSuppressedByCallsite()
{
var source = @"
using System;
using System.Runtime.Versioning;
class TestType
{
[SupportedOSPlatform(""macos15.0"")]
[SupportedOSPlatform(""tvos12.2"")]
private void GetFilter ()
{
if (IsAtLeast)
DoSomething();
[|DoSomething()|]; // This call site is reachable on: 'macOS/OSX' 15.0 and later, 'tvos' 12.2 and later. 'TestType.DoSomething()' is only supported on: 'tvos' 15.0 and later.
}
[SupportedOSPlatform(""tvos15.0"")]
[SupportedOSPlatform(""macos15.0"")]
public void DoSomething() {}
[SupportedOSPlatformGuard (""macos15.0"")]
[SupportedOSPlatformGuard (""tvos15.0"")]
public bool IsAtLeast => false;
}";

string msBuildPlatforms = "build_property.TargetFramework=net8.0-maccatalyst12.0;\nbuild_property.TargetFrameworkIdentifier=.NETCoreApp\nbuild_property.TargetFrameworkVersion=v8.0";
await VerifyAnalyzerCSAsync(source, msBuildPlatforms);
}

private readonly string TargetTypesForTest = @"
namespace PlatformCompatDemo.SupportedUnupported
{
Expand Down

0 comments on commit 1b5fece

Please sign in to comment.