-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3231 from sharwell/generate-lightup
Use code generation for IOperation in the light-up layer
- Loading branch information
Showing
143 changed files
with
10,907 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,075 changes: 1,075 additions & 0 deletions
1,075
StyleCop.Analyzers/StyleCop.Analyzers.CodeGeneration/OperationLightupGenerator.cs
Large diffs are not rendered by default.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
StyleCop.Analyzers/StyleCop.Analyzers.CodeGeneration/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: CLSCompliant(false)] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] |
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions
21
...eCop.Analyzers/StyleCop.Analyzers.CodeGeneration/StyleCop.Analyzers.CodeGeneration.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<CodeAnalysisRuleSet>..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SignAssembly>true</SignAssembly> | ||
<AssemblyOriginatorKeyFile>..\..\build\keys\StyleCopAnalyzers.snk</AssemblyOriginatorKeyFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0-5.final" /> | ||
</ItemGroup> | ||
|
||
</Project> |
83 changes: 83 additions & 0 deletions
83
StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp7/Lightup/OperationKindExTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Test.CSharp7.Lightup | ||
{ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
using StyleCop.Analyzers.Lightup; | ||
using Xunit; | ||
|
||
public class OperationKindExTests | ||
{ | ||
private static readonly Dictionary<OperationKind, string> OperationKindToName; | ||
private static readonly Dictionary<string, OperationKind> NameToOperationKind; | ||
|
||
static OperationKindExTests() | ||
{ | ||
var renamedOperations = | ||
new Dictionary<string, string>() | ||
{ | ||
{ "BinaryOperator", "Binary" }, | ||
{ "ConstructorBodyOperation", "ConstructorBody" }, | ||
{ "MethodBodyOperation", "MethodBody" }, | ||
{ "TupleBinaryOperator", "TupleBinary" }, | ||
{ "UnaryOperator", "Unary" }, | ||
}; | ||
|
||
OperationKindToName = new Dictionary<OperationKind, string>(); | ||
NameToOperationKind = new Dictionary<string, OperationKind>(); | ||
|
||
foreach (var field in typeof(OperationKind).GetTypeInfo().DeclaredFields) | ||
{ | ||
if (!field.IsStatic) | ||
{ | ||
continue; | ||
} | ||
|
||
var value = (OperationKind)field.GetRawConstantValue(); | ||
var name = field.Name; | ||
if (renamedOperations.TryGetValue(name, out var newName)) | ||
{ | ||
name = newName; | ||
} | ||
|
||
if (!OperationKindToName.ContainsKey(value)) | ||
{ | ||
OperationKindToName[value] = name; | ||
} | ||
|
||
if (!NameToOperationKind.ContainsKey(name)) | ||
{ | ||
NameToOperationKind.Add(name, value); | ||
} | ||
} | ||
} | ||
|
||
public static IEnumerable<object[]> OperationKinds | ||
{ | ||
get | ||
{ | ||
foreach (var field in typeof(OperationKindEx).GetTypeInfo().DeclaredFields) | ||
{ | ||
yield return new object[] { field.Name, (OperationKind)field.GetRawConstantValue() }; | ||
} | ||
} | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(OperationKinds))] | ||
public void TestOperationKind(string name, OperationKind operationKind) | ||
{ | ||
if (OperationKindToName.TryGetValue(operationKind, out var expectedName)) | ||
{ | ||
Assert.Equal(expectedName, name); | ||
} | ||
else | ||
{ | ||
Assert.False(NameToOperationKind.TryGetValue(name, out _)); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp8/Lightup/OperationKindExTestsCSharp8.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Test.CSharp8.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.CSharp7.Lightup; | ||
|
||
public class OperationKindExTestsCSharp8 : OperationKindExTests | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
StyleCop.Analyzers/StyleCop.Analyzers.Test.CSharp9/Lightup/OperationKindExTestsCSharp9.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Test.CSharp9.Lightup | ||
{ | ||
using StyleCop.Analyzers.Test.CSharp8.Lightup; | ||
|
||
public class OperationKindExTestsCSharp9 : OperationKindExTestsCSharp8 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...yleCop.Analyzers.CodeGeneration.OperationLightupGenerator/IAddressOfOperationWrapper.g.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Lightup | ||
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal readonly struct IAddressOfOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IAddressOfOperation"; | ||
private static readonly Type WrappedType; | ||
private static readonly Func<IOperation, IOperation> ReferenceAccessor; | ||
private readonly IOperation operation; | ||
static IAddressOfOperationWrapper() | ||
{ | ||
WrappedType = OperationWrapperHelper.GetWrappedType(typeof(IAddressOfOperationWrapper)); | ||
ReferenceAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Reference)); | ||
} | ||
|
||
private IAddressOfOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
public IOperation Reference => ReferenceAccessor(this.WrappedOperation); | ||
public static IAddressOfOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IAddressOfOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...nalyzers.CodeGeneration.OperationLightupGenerator/IAnonymousFunctionOperationWrapper.g.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. | ||
// Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
||
namespace StyleCop.Analyzers.Lightup | ||
{ | ||
using System; | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal readonly struct IAnonymousFunctionOperationWrapper : IOperationWrapper | ||
{ | ||
internal const string WrappedTypeName = "Microsoft.CodeAnalysis.Operations.IAnonymousFunctionOperation"; | ||
private static readonly Type WrappedType; | ||
private static readonly Func<IOperation, IMethodSymbol> SymbolAccessor; | ||
private static readonly Func<IOperation, IOperation> BodyAccessor; | ||
private readonly IOperation operation; | ||
static IAnonymousFunctionOperationWrapper() | ||
{ | ||
WrappedType = OperationWrapperHelper.GetWrappedType(typeof(IAnonymousFunctionOperationWrapper)); | ||
SymbolAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IMethodSymbol>(WrappedType, nameof(Symbol)); | ||
BodyAccessor = LightupHelpers.CreateOperationPropertyAccessor<IOperation, IOperation>(WrappedType, nameof(Body)); | ||
} | ||
|
||
private IAnonymousFunctionOperationWrapper(IOperation operation) | ||
{ | ||
this.operation = operation; | ||
} | ||
|
||
public IOperation WrappedOperation => this.operation; | ||
public ITypeSymbol Type => this.WrappedOperation.Type; | ||
public IMethodSymbol Symbol => SymbolAccessor(this.WrappedOperation); | ||
public IBlockOperationWrapper Body => IBlockOperationWrapper.FromOperation(BodyAccessor(this.WrappedOperation)); | ||
public static IAnonymousFunctionOperationWrapper FromOperation(IOperation operation) | ||
{ | ||
if (operation == null) | ||
{ | ||
return default; | ||
} | ||
|
||
if (!IsInstance(operation)) | ||
{ | ||
throw new InvalidCastException($"Cannot cast '{operation.GetType().FullName}' to '{WrappedTypeName}'"); | ||
} | ||
|
||
return new IAnonymousFunctionOperationWrapper(operation); | ||
} | ||
|
||
public static bool IsInstance(IOperation operation) | ||
{ | ||
return operation != null && LightupHelpers.CanWrapOperation(operation, WrappedType); | ||
} | ||
} | ||
} |
Oops, something went wrong.