Skip to content

Commit

Permalink
Do not warn when invoking APIs that has SupportedOSPlatformGuard attr…
Browse files Browse the repository at this point in the history
…ibute
  • Loading branch information
buyaa-n committed Jun 11, 2024
1 parent 4d5fd9d commit 094e3b0
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1407,14 +1407,18 @@ void CheckOperationAttributes(ISymbol symbol, bool checkParents)
platformSpecificOperations.TryAdd(new KeyValuePair<IOperation, ISymbol>(operation, symbol), (notSuppressedAttributes, callSiteAttributes.Platforms));
}
}
else if (TryCopyAttributesNotSuppressedByMsBuild(operationAttributes.Platforms!, msBuildPlatforms, out var copiedAttributes))
else if (!OperationHasOnlyAssemblyAttributesAndCalledFromSameAssembly(operationAttributes, symbol, containingSymbol) &&
TryCopyAttributesNotSuppressedByMsBuild(operationAttributes.Platforms!, msBuildPlatforms, out var copiedAttributes))
{
platformSpecificOperations.TryAdd(new KeyValuePair<IOperation, ISymbol>(operation, symbol), (copiedAttributes, null));
}
}
}
}

private static bool OperationHasOnlyAssemblyAttributesAndCalledFromSameAssembly(PlatformAttributes operationAttributes, ISymbol symbol, ISymbol containingSymbol) =>
operationAttributes.IsAssemblyAttribute && containingSymbol.ContainingAssembly == symbol.ContainingAssembly;

private static bool UsedInCreatingNotSupportedException(IArgumentOperation operation, ITypeSymbol? notSupportedExceptionType)
{
if (operation.Parent is IObjectCreationOperation creation &&
Expand Down Expand Up @@ -1799,11 +1803,7 @@ static void MergePlatformAttributes(ImmutableArray<AttributeData> immediateAttri

if (attribute.AttributeClass.Name is SupportedOSPlatformGuardAttribute or UnsupportedOSPlatformGuardAttribute)
{
if (!parentAttributes.IsAssemblyAttribute)
{
parentAttributes = new PlatformAttributes();
}

parentAttributes = new PlatformAttributes(); // The API is for guard, clear parent attributes
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;

Expand Down Expand Up @@ -1216,6 +1217,76 @@ static void M1()
await VerifyAnalyzerCSAsync(csSource);
}

[Fact]
public async Task CallGuardFromPlatformSpecificAssembly()
{
string csDependencyCode = @"
public class Library
{
[System.Runtime.Versioning.SupportedOSPlatformGuard(""windows"")]
[System.Runtime.Versioning.SupportedOSPlatformGuard(""linux"")]
public static bool IsSupported => false;
public static void AMethod() { }
}";
csDependencyCode = @$"[assembly: System.Runtime.Versioning.SupportedOSPlatform(""windows"")]
[assembly: System.Runtime.Versioning.SupportedOSPlatform(""linux"")]
{csDependencyCode}";

string csCurrentAssemblyCode = @"
using System;
public class Program
{
public void ProgramMethod()
{
[|Library.AMethod()|]; // Not guarded, warns
if (Library.IsSupported)
{
Library.AMethod();
}
}
}";
var test = SetupDependencyAndTestCSWithOneSourceFile(csCurrentAssemblyCode, csDependencyCode);
test.TestState.AnalyzerConfigFiles.Add(("/.editorconfig", $@"root = true
[*]
build_property.TargetFramework = net5
"));
await test.RunAsync();
}

private static VerifyCS.Test SetupDependencyAndTestCSWithOneSourceFile(string csInput, string csDependencyCode)
{
return new VerifyCS.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp10,
MarkupOptions = MarkupOptions.UseFirstDescriptor,
TestState =
{
Sources =
{
csInput
},
AdditionalProjects =
{
["PreviewAssembly"] =
{
Sources =
{
("/PreviewAssembly/AssemblyInfo.g.cs", csDependencyCode)
},
},
},
AdditionalProjectReferences =
{
"PreviewAssembly",
},
},
};
}

[Fact]
public async Task GuardedWith_RuntimeInformation_IsOSPlatform_SimpleIfElseAsync()
{
Expand Down Expand Up @@ -4181,7 +4252,7 @@ public async Task GuardCallingCachedValue_CallSiteHasAssemblyAttributeAsync()
[assembly: SupportedOSPlatform(""ios10.0"")]
class Test
{
static bool s_isiOS11OrNewer => false;
static bool s_isiOS11OrNewer = false;
[SupportedOSPlatformGuard(""ios11.0"")]
private bool IsIos11Supported() => s_isiOS11OrNewer; // should not warn
Expand Down

0 comments on commit 094e3b0

Please sign in to comment.