diff --git a/Source/AtCoderAnalyzer/.editorconfig b/Source/AtCoderAnalyzer/.editorconfig deleted file mode 100644 index fdbfdac8..00000000 --- a/Source/AtCoderAnalyzer/.editorconfig +++ /dev/null @@ -1,3 +0,0 @@ -[*.cs] -# IDE0090: Use 'new(...)' -csharp_style_implicit_object_creation_when_type_is_apparent = true diff --git a/Source/AtCoderAnalyzer/AC0001_AC0002_IntToLongAnalyzer.cs b/Source/AtCoderAnalyzer/AC0001_AC0002_IntToLongAnalyzer.cs deleted file mode 100644 index 44bf0107..00000000 --- a/Source/AtCoderAnalyzer/AC0001_AC0002_IntToLongAnalyzer.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.Collections.Immutable; -using AtCoderAnalyzer.Diagnostics; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Diagnostics; - -namespace AtCoderAnalyzer -{ - [DiagnosticAnalyzer(LanguageNames.CSharp)] - public class AC0001_AC0002_IntToLongAnalyzer : DiagnosticAnalyzer - { - public override ImmutableArray SupportedDiagnostics - => ImmutableArray.Create( - DiagnosticDescriptors.AC0001_MultiplyOverflowInt32_Descriptor, - DiagnosticDescriptors.AC0002_LeftShiftOverflowInt32_Descriptor); - - public override void Initialize(AnalysisContext context) - { - context.ConfigureGeneratedCodeAnalysis( - GeneratedCodeAnalysisFlags.Analyze | - GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.EnableConcurrentExecution(); - - context.RegisterSyntaxNodeAction(AnalyzeIntToLongSyntaxNode, - SyntaxKind.LeftShiftExpression, SyntaxKind.MultiplyExpression); - } - - private void AnalyzeIntToLongSyntaxNode(SyntaxNodeAnalysisContext context) - { - var semanticModel = context.SemanticModel; - var node = context.Node; - - var typeInfo = semanticModel.GetTypeInfo(node, cancellationToken: context.CancellationToken); - if (typeInfo.Type.SpecialType != SpecialType.System_Int32) - return; - - Diagnostic diagnostic = node.Kind() switch - { - SyntaxKind.MultiplyExpression => DiagnosticDescriptors.AC0001_MultiplyOverflowInt32(context.Node), - SyntaxKind.LeftShiftExpression => DiagnosticDescriptors.AC0002_LeftShiftOverflowInt32(context.Node), - _ => throw new InvalidOperationException(), - }; - - for (; node is not null; node = GetParent(node)) - { - if (semanticModel.GetTypeInfo(node, cancellationToken: context.CancellationToken) - .ConvertedType.SpecialType == SpecialType.System_Int64) - { - context.ReportDiagnostic(diagnostic); - return; - } - } - - static SyntaxNode GetParent(SyntaxNode node) - { - var parent = node.Parent; - if (parent is BinaryExpressionSyntax or ParenthesizedExpressionSyntax) - return parent; - return null; - } - } - } -} diff --git a/Source/AtCoderAnalyzer/AC0001_AC0002_IntToLongCodeFixProvider.cs b/Source/AtCoderAnalyzer/AC0001_AC0002_IntToLongCodeFixProvider.cs deleted file mode 100644 index 0c7c4455..00000000 --- a/Source/AtCoderAnalyzer/AC0001_AC0002_IntToLongCodeFixProvider.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System.Collections.Immutable; -using System.Composition; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using AtCoderAnalyzer.Diagnostics; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeActions; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace AtCoderAnalyzer -{ - [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AC0001_AC0002_IntToLongCodeFixProvider)), Shared] - public class AC0001_AC0002_IntToLongCodeFixProvider : CodeFixProvider - { - private const string title = "Cast int to long"; - public override ImmutableArray FixableDiagnosticIds - => ImmutableArray.Create( - DiagnosticDescriptors.AC0001_MultiplyOverflowInt32_Descriptor.Id, - DiagnosticDescriptors.AC0002_LeftShiftOverflowInt32_Descriptor.Id); - - public sealed override FixAllProvider GetFixAllProvider() - => WellKnownFixAllProviders.BatchFixer; - - public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) - { - if (await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) - is not CompilationUnitSyntax root) - return; - var diagnostic = context.Diagnostics[0]; - var diagnosticSpan = diagnostic.Location.SourceSpan; - - var node = root.FindNode(diagnosticSpan); - foreach (var nn in node.ChildNodes().Prepend(node)) - { - if (nn is BinaryExpressionSyntax b) - { - switch (b.Kind()) - { - case SyntaxKind.LeftShiftExpression: - case SyntaxKind.MultiplyExpression: - var action = CodeAction.Create(title: title, - createChangedDocument: c => CastLong(context.Document, b, c), - equivalenceKey: title); - context.RegisterCodeFix(action, diagnostic); - return; - } - } - } - } - private async Task CastLong(Document document, BinaryExpressionSyntax syntax, CancellationToken cancellationToken) - { - var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false); - while (syntax.Left is BinaryExpressionSyntax nx) - syntax = nx; - - if (syntax.Left is LiteralExpressionSyntax lx && lx.Token.Value is int num) - { - var longEx = SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal((long)num)); - return document.WithSyntaxRoot(root.ReplaceNode(syntax.Left, longEx)); - } - var castEx = SyntaxFactory.CastExpression(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.LongKeyword)), syntax.Left); - return document.WithSyntaxRoot(root.ReplaceNode(syntax.Left, castEx)); - } - } -} diff --git a/Source/AtCoderAnalyzer/AggressiveInliningAnalyzer.cs b/Source/AtCoderAnalyzer/AggressiveInliningAnalyzer.cs deleted file mode 100644 index 9f5999a7..00000000 --- a/Source/AtCoderAnalyzer/AggressiveInliningAnalyzer.cs +++ /dev/null @@ -1,136 +0,0 @@ -using System; -using System.Collections.Immutable; -using System.Linq; -using AtCoderAnalyzer.Diagnostics; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.Diagnostics; -using static AtCoderAnalyzer.Helpers.Constants; -using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; - -namespace AtCoderAnalyzer -{ - [DiagnosticAnalyzer(LanguageNames.CSharp)] - public class AggressiveInliningAnalyzer : DiagnosticAnalyzer - { - public override ImmutableArray SupportedDiagnostics - => ImmutableArray.Create(DiagnosticDescriptors.AC0007_AgressiveInlining_Descriptor); - - private class ContainingOperatorTypes - { - public INamedTypeSymbol MethodImplAttribute { get; } - public INamedTypeSymbol IsOperatorAttribute { get; } - - public ContainingOperatorTypes(INamedTypeSymbol methodImpl, INamedTypeSymbol isOperator) - { - MethodImplAttribute = methodImpl; - IsOperatorAttribute = isOperator; - } - public static bool TryParseTypes(Compilation compilation, out ContainingOperatorTypes types) - { - types = null; - var methodImpl = compilation.GetTypeByMetadataName(System_Runtime_CompilerServices_MethodImplAttribute); - if (methodImpl is null) - return false; - var isOperator = compilation.GetTypeByMetadataName(AtCoder_IsOperatorAttribute); - if (isOperator is null) - return false; - types = new ContainingOperatorTypes(methodImpl, isOperator); - return true; - } - } - public override void Initialize(AnalysisContext context) - { - context.EnableConcurrentExecution(); - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.RegisterCompilationStartAction(compilationStartContext => - { - if (ContainingOperatorTypes.TryParseTypes(compilationStartContext.Compilation, out var types)) - { - compilationStartContext.RegisterSyntaxNodeAction( - c => AnalyzeTypeDecra(c, types), - SyntaxKind.StructDeclaration, SyntaxKind.ClassConstraint); - } - }); - } - - private void AnalyzeTypeDecra(SyntaxNodeAnalysisContext context, ContainingOperatorTypes types) - { - if (context.SemanticModel.GetDeclaredSymbol(context.Node, context.CancellationToken) - is not INamedTypeSymbol symbol) - return; - var concurrentBuild = context.Compilation.Options.ConcurrentBuild; - - bool HasIsOperatorAttribute(INamedTypeSymbol symbol) - { - foreach (var at in symbol.ConstructedFrom.GetAttributes()) - if (SymbolEqualityComparer.Default.Equals(at.AttributeClass, types.IsOperatorAttribute)) - return true; - return false; - } - - if (concurrentBuild) - { - if (symbol.AllInterfaces - .AsParallel(context.CancellationToken) - .Any(HasIsOperatorAttribute)) - goto HasIsOperator; - } - else - { - if (symbol.AllInterfaces - .Do(_ => context.CancellationToken.ThrowIfCancellationRequested()) - .Any(HasIsOperatorAttribute)) - goto HasIsOperator; - } - return; - HasIsOperator: - - bool DoesNotHaveMethodImplInlining(IMethodSymbol m) - { - if (m.MethodKind is - not (MethodKind.ExplicitInterfaceImplementation or MethodKind.Ordinary)) - return false; - - if (m.GetAttributes() - .FirstOrDefault(at => SymbolEqualityComparer.Default.Equals(at.AttributeClass, types.MethodImplAttribute)) is not { } attr - || attr.ConstructorArguments.Length == 0) - return true; - - var arg = attr.ConstructorArguments[0]; - if (arg.Kind is TypedConstantKind.Primitive or TypedConstantKind.Enum) - try - { - return !((MethodImplOptions)Convert.ToInt32(arg.Value)).HasFlag(MethodImplOptions.AggressiveInlining); - } - catch - { - return true; - } - return true; - } - - string[] notMethodImplInliningMethods; - if (concurrentBuild) - notMethodImplInliningMethods = symbol.GetMembers() - .AsParallel(context.CancellationToken) - .OfType() - .Where(DoesNotHaveMethodImplInlining) - .Select(m => m.Name) - .ToArray(); - else - notMethodImplInliningMethods = symbol.GetMembers() - .Do(_ => context.CancellationToken.ThrowIfCancellationRequested()) - .OfType() - .Where(DoesNotHaveMethodImplInlining) - .Select(m => m.Name) - .ToArray(); - if (notMethodImplInliningMethods.Length == 0) - return; - - var diagnostic = DiagnosticDescriptors.AC0007_AgressiveInlining( - context.Node.GetLocation(), notMethodImplInliningMethods); - context.ReportDiagnostic(diagnostic); - } - } -} diff --git a/Source/AtCoderAnalyzer/AggressiveInliningCodeFixProvider.cs b/Source/AtCoderAnalyzer/AggressiveInliningCodeFixProvider.cs deleted file mode 100644 index 3fe719c0..00000000 --- a/Source/AtCoderAnalyzer/AggressiveInliningCodeFixProvider.cs +++ /dev/null @@ -1,170 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Composition; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using AtCoderAnalyzer.Diagnostics; -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeActions; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static AtCoderAnalyzer.Helpers.Constants; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; - -namespace AtCoderAnalyzer -{ - [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AggressiveInliningCodeFixProvider)), Shared] - public class AggressiveInliningCodeFixProvider : CodeFixProvider - { - private const string title = "Add AggressiveInlining"; - public override ImmutableArray FixableDiagnosticIds - => ImmutableArray.Create( - DiagnosticDescriptors.AC0007_AgressiveInlining_Descriptor.Id); - - public sealed override FixAllProvider GetFixAllProvider() - => WellKnownFixAllProviders.BatchFixer; - public override async Task RegisterCodeFixesAsync(CodeFixContext context) - { - if (await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) - is not CompilationUnitSyntax root) - return; - - var config = AtCoderAnalyzerConfig.Parse(context.Document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GlobalOptions); - var diagnostic = context.Diagnostics[0]; - var diagnosticSpan = diagnostic.Location.SourceSpan; - - if (root.FindNode(diagnosticSpan) - is not TypeDeclarationSyntax typeDeclarationSyntax) - return; - - var action = CodeAction.Create(title: title, - createChangedDocument: c => AddAggressiveInlining(context.Document, root, diagnosticSpan.Start, config, typeDeclarationSyntax, c), - equivalenceKey: title); - context.RegisterCodeFix(action, diagnostic); - } - - private async Task AddAggressiveInlining( - Document document, CompilationUnitSyntax root, - int postion, - AtCoderAnalyzerConfig config, - TypeDeclarationSyntax typeDeclarationSyntax, CancellationToken cancellationToken) - { - root = root.ReplaceNode(typeDeclarationSyntax, - new AddAggressiveInliningRewriter(await document.GetSemanticModelAsync(cancellationToken), postion, config) - .Visit(typeDeclarationSyntax)); - - return document.WithSyntaxRoot(root); - } - - private class AddAggressiveInliningRewriter : CSharpSyntaxRewriter - { - private readonly SemanticModel semanticModel; - private readonly int position; - private readonly INamedTypeSymbol methodImplAttribute; - private readonly INamedTypeSymbol methodImplOptions; - private readonly AttributeSyntax aggressiveInliningAttribute; - public AddAggressiveInliningRewriter(SemanticModel semanticModel, int position, AtCoderAnalyzerConfig config) : base(false) - { - this.semanticModel = semanticModel; - this.position = position; - methodImplAttribute = semanticModel.Compilation.GetTypeByMetadataName( - System_Runtime_CompilerServices_MethodImplAttribute); - methodImplOptions = semanticModel.Compilation.GetTypeByMetadataName( - System_Runtime_CompilerServices_MethodImplOptions); - aggressiveInliningAttribute = SyntaxHelpers.AggressiveInliningAttribute( - methodImplAttribute.ToMinimalDisplayString(semanticModel, position), - methodImplOptions.ToMinimalDisplayString(semanticModel, position), config.UseMethodImplNumeric); - } - - public override SyntaxNode VisitMethodDeclaration(MethodDeclarationSyntax node) - { - if (methodImplAttribute is null) - return node; - if (methodImplOptions is null) - return node; - if (semanticModel.GetDeclaredSymbol(node) is not IMethodSymbol m) - return node; - - if (m.MethodKind is - not (MethodKind.ExplicitInterfaceImplementation or MethodKind.Ordinary)) - return node; - - if (m.GetAttributes() - .FirstOrDefault(at => SymbolEqualityComparer.Default.Equals(at.AttributeClass, methodImplAttribute)) is not { } attr - || attr.ApplicationSyntaxReference?.GetSyntax() is not AttributeSyntax syntax) - return node.AddAttributeLists(AttributeList(SingletonSeparatedList(aggressiveInliningAttribute))); - - if (attr.ConstructorArguments.Length > 0) - { - var arg = attr.ConstructorArguments[0]; - if (arg.Kind is TypedConstantKind.Primitive or TypedConstantKind.Enum) - try - { - if (((MethodImplOptions)Convert.ToInt32(arg.Value)).HasFlag(MethodImplOptions.AggressiveInlining)) - return node; - } - catch - { - } - } - - var list = new List(node.AttributeLists.Count); - foreach (var attributeList in node.AttributeLists) - { - if (attributeList.Attributes.Contains(syntax)) - { - var replaced = attributeList.Attributes.Replace(syntax, AddAggressiveInlining(syntax, attr)); - list.Add(AttributeList(replaced)); - } - else - list.Add(attributeList); - } - return node.WithAttributeLists(new SyntaxList(list)); - } - - - private AttributeSyntax AddAggressiveInlining(AttributeSyntax syntax, AttributeData attributeData) - { - if (attributeData.ConstructorArguments.Length == 0) - { - return aggressiveInliningAttribute; - } - else - { - var argConst = attributeData.ConstructorArguments[0]; - var argSyntax = syntax.ArgumentList.Arguments[0]; - - if (argSyntax.NameEquals == null) - { - AttributeArgumentSyntax arg; - if (argConst.Type.SpecialType is SpecialType.System_Int16) - arg = argSyntax.WithExpression( - BinaryExpression( - SyntaxKind.BitwiseOrExpression, argSyntax.Expression, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(256)))); - else if (SymbolEqualityComparer.Default.Equals(argConst.Type, methodImplOptions)) - arg = argSyntax.WithExpression( - BinaryExpression( - SyntaxKind.BitwiseOrExpression, argSyntax.Expression, SyntaxHelpers.AggressiveInliningMember(methodImplOptions.ToMinimalDisplayString(semanticModel, position)))); - else - throw new InvalidProgramException("invalid MethodImplAttribute argument"); - - return syntax.WithArgumentList( - AttributeArgumentList( - syntax.ArgumentList.Arguments.Replace(syntax.ArgumentList.Arguments[0], arg))); - } - else - { - return syntax.WithArgumentList( - AttributeArgumentList( - syntax.ArgumentList.Arguments.Insert(0, AttributeArgument(SyntaxHelpers.AggressiveInliningMember(methodImplOptions.ToMinimalDisplayString(semanticModel, position)))))); - } - } - } - } - } -} diff --git a/Source/AtCoderAnalyzer/AnalyzerReleases.Shipped.md b/Source/AtCoderAnalyzer/AnalyzerReleases.Shipped.md deleted file mode 100644 index 460a52a9..00000000 --- a/Source/AtCoderAnalyzer/AnalyzerReleases.Shipped.md +++ /dev/null @@ -1,30 +0,0 @@ -; Shipped analyzer releases -; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md - -## Release 1.0.2 - -### New Rules -Rule ID | Category | Severity | Notes ---------|----------|----------|------- -AC0001 | Overflow | Warning | int multiply expression is assigned to long -AC0002 | Overflow | Warning | int left shift expression is assigned to long -AC0003 | Type Define | Error | Not defined IStaticMod -AC0004 | Type Define | Error | Not defined IDynamicModID -AC0005 | Type Define | Error | Not defined ISegtreeOperator -AC0006 | Type Define | Error | Not defined ILazySegtreeOperator -AC0007 | Type Define | Info | Some operator methods don't have `MethodImpl` attribute - -## Release 1.0.4 - -### New Rules -Rule ID | Category | Severity | Notes ---------|----------|----------|------- -AC0008 | Type Define | Error | Not defined operator type - -### Removed Rules -Rule ID | Category | Severity | Notes ---------|----------|----------|-------------------- -AC0003 | Type Define | Error | Not defined IStaticMod -AC0004 | Type Define | Error | Not defined IDynamicModID -AC0005 | Type Define | Error | Not defined ISegtreeOperator -AC0006 | Type Define | Error | Not defined ILazySegtreeOperator diff --git a/Source/AtCoderAnalyzer/AnalyzerReleases.Unshipped.md b/Source/AtCoderAnalyzer/AnalyzerReleases.Unshipped.md deleted file mode 100644 index 43574499..00000000 --- a/Source/AtCoderAnalyzer/AnalyzerReleases.Unshipped.md +++ /dev/null @@ -1,3 +0,0 @@ -; Unshipped analyzer release -; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md - diff --git a/Source/AtCoderAnalyzer/AtCoderAnalyzer.csproj b/Source/AtCoderAnalyzer/AtCoderAnalyzer.csproj deleted file mode 100644 index 632059e1..00000000 --- a/Source/AtCoderAnalyzer/AtCoderAnalyzer.csproj +++ /dev/null @@ -1,70 +0,0 @@ - - - - netstandard2.0 - 9 - true - ac-library-csharp;AtCoder;Analyzer - AtCoderAnalyzer - Analyzer For AtCoder library - MIT - $(NoWarn);CS1998 - false - - - true - false - false - $(TargetsForTfmSpecificContentInPackage);PackBuildOutputs - true - true - - - - - - - - - - - - - - all - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - runtime; build; native; contentfiles; analyzers - all - - - - - - - - True - True - DiagnosticsResources.resx - - - ResXFileCodeGenerator - DiagnosticsResources.Designer.cs - - - - - - - - - - - diff --git a/Source/AtCoderAnalyzer/AtCoderAnalyzerConfig.cs b/Source/AtCoderAnalyzer/AtCoderAnalyzerConfig.cs deleted file mode 100644 index 8e3e148b..00000000 --- a/Source/AtCoderAnalyzer/AtCoderAnalyzerConfig.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using Microsoft.CodeAnalysis.Diagnostics; - -namespace AtCoderAnalyzer -{ - public record AtCoderAnalyzerConfig(bool UseMethodImplNumeric) - { - public static AtCoderAnalyzerConfig Parse(AnalyzerConfigOptions analyzerConfigOptions) - { - var useMethodImplNumeric = analyzerConfigOptions.TryGetValue("build_property.AtCoderAnalyzer_UseMethodImplNumeric", out var v) && - StringComparer.OrdinalIgnoreCase.Equals(v, "true"); - - return new(useMethodImplNumeric); - } - } -} diff --git a/Source/AtCoderAnalyzer/CollectionExtensions.cs b/Source/AtCoderAnalyzer/CollectionExtensions.cs deleted file mode 100644 index 40919db3..00000000 --- a/Source/AtCoderAnalyzer/CollectionExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; - -namespace AtCoderAnalyzer -{ - public static class CollectionExtensions - { - public static IEnumerable Do(this IEnumerable collection, Action action) - { - foreach (var item in collection) - { - action(item); - yield return item; - } - } - public static ParallelQuery AsParallel(this IEnumerable collection, CancellationToken cancellationToken) - => collection.AsParallel().WithCancellation(cancellationToken); - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperatorAnalyzer.cs b/Source/AtCoderAnalyzer/CreateOperatorAnalyzer.cs deleted file mode 100644 index 873b9737..00000000 --- a/Source/AtCoderAnalyzer/CreateOperatorAnalyzer.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using AtCoderAnalyzer.Diagnostics; -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Diagnostics; - -namespace AtCoderAnalyzer -{ - [DiagnosticAnalyzer(LanguageNames.CSharp)] - public class CreateOperatorAnalyzer : DiagnosticAnalyzer - { - public override ImmutableArray SupportedDiagnostics - => ImmutableArray.Create( - DiagnosticDescriptors.AC0008_DefineOperatorType_Descriptor); - - public override void Initialize(AnalysisContext context) - { - context.EnableConcurrentExecution(); - context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); - context.RegisterCompilationStartAction(compilationStartContext => - { - if (OperatorTypesMatcher.TryParseTypes(compilationStartContext.Compilation, out var types)) - { - compilationStartContext.RegisterSyntaxNodeAction( - c => AnalyzeGenericNode(c, types), SyntaxKind.GenericName); - } - }); - } - private void AnalyzeGenericNode(SyntaxNodeAnalysisContext context, OperatorTypesMatcher types) - { - var semanticModel = context.SemanticModel; - if (context.Node is not GenericNameSyntax genericNode) - return; - - if (genericNode.TypeArgumentList.Arguments.Any(sy => sy.IsKind(SyntaxKind.OmittedTypeArgument))) - return; - var concurrentBuild = context.Compilation.Options.ConcurrentBuild; - - ImmutableArray originalTypes; - ImmutableArray writtenTypes; - switch (semanticModel.GetSymbolInfo(genericNode, context.CancellationToken).Symbol) - { - case INamedTypeSymbol symbol: - originalTypes = symbol.TypeParameters; - writtenTypes = symbol.TypeArguments; - break; - case IMethodSymbol symbol: - originalTypes = symbol.TypeParameters; - writtenTypes = symbol.TypeArguments; - break; - default: - return; - } - - if (originalTypes.Length == 0 || originalTypes.Length != writtenTypes.Length) - return; - - var notDefinedTypes = new List(); - for (int i = 0; i < originalTypes.Length; i++) - { - var originalType = originalTypes[i]; - var writtenType = writtenTypes[i]; - - if (concurrentBuild) - { - if (originalType.ConstraintTypes.OfType() - .AsParallel(context.CancellationToken) - .Any(s => types.IsMatch(s.ConstructedFrom))) - goto TypeMatch; - } - else - { - if (originalType.ConstraintTypes.OfType() - .Do(_ => context.CancellationToken.ThrowIfCancellationRequested()) - .Any(s => types.IsMatch(s.ConstructedFrom))) - goto TypeMatch; - } - continue; - - TypeMatch: - if (writtenType.TypeKind == TypeKind.Error) - notDefinedTypes.Add(writtenType.Name.ToString()); - } - if (notDefinedTypes.Count == 0) - return; - - var diagnostic = DiagnosticDescriptors.AC0008_DefineOperatorType( - genericNode.GetLocation(), notDefinedTypes); - context.ReportDiagnostic(diagnostic); - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs b/Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs deleted file mode 100644 index 6a00e39c..00000000 --- a/Source/AtCoderAnalyzer/CreateOperatorCodeFixProvider.cs +++ /dev/null @@ -1,175 +0,0 @@ -using System.Collections.Immutable; -using System.Composition; -using System.Linq; -using System.Threading.Tasks; -using AtCoderAnalyzer.CreateOperators; -using AtCoderAnalyzer.Diagnostics; -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeActions; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace AtCoderAnalyzer -{ - [ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CreateOperatorCodeFixProvider)), Shared] - public class CreateOperatorCodeFixProvider : CodeFixProvider - { - private const string title = "Create operator type"; - public override ImmutableArray FixableDiagnosticIds - => ImmutableArray.Create( - DiagnosticDescriptors.AC0008_DefineOperatorType_Descriptor.Id); - public sealed override FixAllProvider GetFixAllProvider() - => WellKnownFixAllProviders.BatchFixer; - - public override async Task RegisterCodeFixesAsync(CodeFixContext context) - { - if (await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false) - is not CompilationUnitSyntax root) - return; - var config = AtCoderAnalyzerConfig.Parse(context.Document.Project.AnalyzerOptions.AnalyzerConfigOptionsProvider.GlobalOptions); - var diagnostic = context.Diagnostics[0]; - var diagnosticSpan = diagnostic.Location.SourceSpan; - - if (root.FindNode(diagnosticSpan) - is not GenericNameSyntax genericNode) - return; - - var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false); - - ImmutableArray originalTypes; - ImmutableArray writtenTypes; - switch (semanticModel.GetSymbolInfo(genericNode, context.CancellationToken).Symbol) - { - case INamedTypeSymbol symbol: - originalTypes = symbol.TypeParameters; - writtenTypes = symbol.TypeArguments; - break; - case IMethodSymbol symbol: - originalTypes = symbol.TypeParameters; - writtenTypes = symbol.TypeArguments; - break; - default: - return; - } - - - var writtenTypeSyntaxes = genericNode.TypeArgumentList.Arguments; - - if (originalTypes.Length != writtenTypes.Length) - return; - - if (!OperatorTypesMatcher.TryParseTypes(semanticModel.Compilation, out var types)) - return; - - var defaultSet = ImmutableHashSet.Create(SymbolEqualityComparer.Default); - var genericDicBuilder = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); - var constraintDicBuilder = ImmutableDictionary.CreateBuilder>(); - for (int i = 0; i < originalTypes.Length; i++) - { - var writtenTypeSyntax = writtenTypeSyntaxes[i]; - var originalType = originalTypes[i]; - var constraintTypes = originalType.ConstraintTypes; - var writtenType = writtenTypes[i]; - - if (!constraintTypes - .OfType() - .Select(ty => ty.ConstructedFrom) - .Any(ty => types.IsMatch(ty))) - { - genericDicBuilder.Add(originalType, writtenType); - continue; - } - - if (writtenType.TypeKind == TypeKind.Error) - { - var name = writtenType.Name; - var typeSymbols = constraintDicBuilder.GetValueOrDefault(name, defaultSet); - constraintDicBuilder[name] = typeSymbols.Union(constraintTypes); - } - } - if (constraintDicBuilder.Count == 0) - return; - - var genericDic = genericDicBuilder.ToImmutable(); - var constraintArrayDic = ImmutableDictionary.CreateBuilder>(); - foreach (var p in constraintDicBuilder) - { - constraintArrayDic[p.Key] - = p.Value.Select(sy => SymbolHelpers.ReplaceGenericType(sy, genericDic)) - .OrderBy(sy => sy.ToDisplayString()) - .ToImmutableArray(); - } - - var action = CodeAction.Create(title: title, - createChangedDocument: c => new OperatorTypeSyntaxBuilder(semanticModel, config).AddOperatorType( - context.Document, - root, - constraintArrayDic.ToImmutable()), - equivalenceKey: title); - context.RegisterCodeFix(action, diagnostic); - } - - private class OperatorTypeSyntaxBuilder - { - private readonly SemanticModel semanticModel; - private readonly int origPosition; - private AtCoderAnalyzerConfig config; - public OperatorTypeSyntaxBuilder(SemanticModel semanticModel, AtCoderAnalyzerConfig config) - { - this.semanticModel = semanticModel; - origPosition = semanticModel.SyntaxTree.Length; - this.config = config; - } - - public async Task AddOperatorType( - Document document, - CompilationUnitSyntax root, - ImmutableDictionary> constraintDic) - { - bool hasMethod = false; - var usings = root.Usings.ToNamespaceHashSet(); - - MemberDeclarationSyntax[] newMembers = new MemberDeclarationSyntax[constraintDic.Count]; - foreach (var (p, i) in constraintDic.Select((p, i) => (p, i))) - { - bool m; - (newMembers[i], m) = Build(p.Key, p.Value); - hasMethod |= m; - } - - return document.WithSyntaxRoot(root.AddMembers(newMembers)); - - } - - private (StructDeclarationSyntax syntax, bool hasMethod) Build(string operatorTypeName, ImmutableArray constraints) - { - bool hasMethod = false; - var members = ImmutableList.CreateBuilder(); - var added = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); - - foreach (var constraint in constraints) - { - foreach (var baseType in constraint.AllInterfaces.Append(constraint)) - { - if (!added.Add(baseType)) - continue; - - foreach (var (member, isMethod) in EnumerateMember.Create(semanticModel, baseType, config).EnumerateMemberSyntax()) - { - members.Add(member); - hasMethod |= isMethod; - } - } - } - - var dec = SyntaxFactory.StructDeclaration(operatorTypeName) - .WithBaseList(SyntaxFactory.BaseList( - constraints.Select(c => (BaseTypeSyntax)SyntaxFactory.SimpleBaseType(c.ToTypeSyntax(semanticModel, origPosition))).ToSeparatedSyntaxList())) - .WithMembers(SyntaxFactory.List(members.Distinct(MemberDeclarationEqualityComparer.Default))); - return (dec, hasMethod); - } - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/EnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/EnumerateMember.cs deleted file mode 100644 index 04a952ef..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/EnumerateMember.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System.Collections.Generic; -using AtCoderAnalyzer.CreateOperators.Specified; -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.CreateOperators -{ - internal class EnumerateMember - { - protected SemanticModel SemanticModel { get; } - protected ITypeSymbol TypeSymbol { get; } - private readonly INamedTypeSymbol methodImplAttribute; - private readonly INamedTypeSymbol methodImplOptions; - private readonly AtCoderAnalyzerConfig config; - protected EnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) - { - SemanticModel = semanticModel; - TypeSymbol = typeSymbol; - methodImplAttribute = semanticModel.Compilation.GetTypeByMetadataName( - Constants.System_Runtime_CompilerServices_MethodImplAttribute); - methodImplOptions = semanticModel.Compilation.GetTypeByMetadataName( - Constants.System_Runtime_CompilerServices_MethodImplOptions); - this.config = config; - } - public static EnumerateMember Create(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) => typeSymbol.OriginalDefinition.ToDisplayString() switch - { - "AtCoder.Operators.IAdditionOperator" => new AdditionEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.ISubtractOperator" => new SubtractEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.IMultiplicationOperator" => new MultiplicationEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.IDivisionOperator" => new DivisionEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.IUnaryNumOperator" => new UnaryNumEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.ICompareOperator" => new CompareOperatorEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.IMinMaxValue" => new MinMaxValueEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.IShiftOperator" => new ShiftEnumerateMember(semanticModel, typeSymbol, config), - "AtCoder.Operators.ICastOperator" => new CastEnumerateMember(semanticModel, typeSymbol, config), - "System.Collections.Generic.IComparer" => new ComparerEnumerateMember(semanticModel, typeSymbol, config), - _ => new EnumerateMember(semanticModel, typeSymbol, config), - }; - - public IEnumerable<(MemberDeclarationSyntax Syntax, bool IsMethod)> EnumerateMemberSyntax() - { - foreach (var member in TypeSymbol.GetMembers()) - { - if (!member.IsAbstract) - continue; - if (member is IPropertySymbol property) - yield return (CreatePropertySyntax(property, member.IsStatic), false); - else if (member is IMethodSymbol method && method.MethodKind == MethodKind.Ordinary) - yield return (CreateMethodSyntax(method, member.IsStatic), true); - } - } - - private static SyntaxTokenList PublicModifiers(bool isStatic) - => isStatic ? TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)) : TokenList(Token(SyntaxKind.PublicKeyword)); - - protected virtual PropertyDeclarationSyntax CreatePropertySyntax(IPropertySymbol symbol, bool isStatic) - { - var dec = PropertyDeclaration(symbol.Type.ToTypeSyntax(SemanticModel, SemanticModel.SyntaxTree.Length - 1), symbol.Name) - .WithModifiers(PublicModifiers(isStatic)); - - if (symbol.SetMethod == null) - return dec - .WithExpressionBody(SyntaxHelpers.ArrowDefault) - .WithSemicolonToken(SyntaxHelpers.SemicolonToken); - else if (symbol.GetMethod == null) - return dec.AddAccessorListAccessors(new AccessorDeclarationSyntax[] - { - AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) - .WithSemicolonToken(SyntaxHelpers.SemicolonToken), - }); - - return dec.AddAccessorListAccessors(new AccessorDeclarationSyntax[] - { - AccessorDeclaration(SyntaxKind.SetAccessorDeclaration) - .WithSemicolonToken(SyntaxHelpers.SemicolonToken), - AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) - .WithSemicolonToken(SyntaxHelpers.SemicolonToken) - }); - } - protected virtual MethodDeclarationSyntax CreateMethodSyntax(IMethodSymbol symbol, bool isStatic) - { - if (symbol.ReturnsVoid) - return CreateMethodSyntax(SemanticModel, SemanticModel.SyntaxTree.Length - 1, symbol, isStatic, Block()); - else - return CreateMethodSyntax(SemanticModel, SemanticModel.SyntaxTree.Length - 1, symbol, isStatic, SyntaxHelpers.ArrowDefault); - } - - private MethodDeclarationSyntax CommonMethodDeclaration(IMethodSymbol symbol, SemanticModel semanticModel, int position, bool isStatic) - => MethodDeclaration(symbol.ReturnType.ToTypeSyntax(semanticModel, semanticModel.SyntaxTree.Length), symbol.Name) - .WithModifiers(PublicModifiers(isStatic)) - .WithAttributeLists(SingletonList( - AttributeList( - SingletonSeparatedList( - SyntaxHelpers.AggressiveInliningAttribute( - methodImplAttribute.ToMinimalDisplayString(semanticModel, position), - methodImplOptions.ToMinimalDisplayString(semanticModel, position), - config.UseMethodImplNumeric))))) - .WithParameterList(symbol.ToParameterListSyntax(semanticModel, semanticModel.SyntaxTree.Length)); - protected MethodDeclarationSyntax CreateMethodSyntax(SemanticModel semanticModel, int position, IMethodSymbol symbol, bool isStatic, BlockSyntax block) - { - return CommonMethodDeclaration(symbol, semanticModel, position, isStatic) - .WithBody(block); - } - protected MethodDeclarationSyntax CreateMethodSyntax(SemanticModel semanticModel, int position, IMethodSymbol symbol, bool isStatic, ArrowExpressionClauseSyntax arrowExpressionClause) - { - return CommonMethodDeclaration(symbol, semanticModel, position, isStatic) - .WithExpressionBody(arrowExpressionClause) - .WithSemicolonToken(SyntaxHelpers.SemicolonToken); - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/OperatorEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/OperatorEnumerateMember.cs deleted file mode 100644 index bd560497..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/OperatorEnumerateMember.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.CreateOperators -{ - internal abstract class OperatorEnumerateMember : EnumerateMember - { - protected OperatorEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - protected abstract SyntaxKind? GetSyntaxKind(IMethodSymbol symbol); - - protected override MethodDeclarationSyntax CreateMethodSyntax(IMethodSymbol symbol, bool isStatic) - { - if (GetSyntaxKind(symbol) is SyntaxKind kind) - { - var operatorCall = BinaryExpression( - kind, - IdentifierName(symbol.Parameters[0].Name), - IdentifierName(symbol.Parameters[1].Name)); - return CreateMethodSyntax( - SemanticModel, SemanticModel.SyntaxTree.Length - 1, - symbol, false, ArrowExpressionClause(operatorCall)); - } - return base.CreateMethodSyntax(symbol, isStatic); - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/AdditionEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/AdditionEnumerateMember.cs deleted file mode 100644 index 3a6246e4..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/AdditionEnumerateMember.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class AdditionEnumerateMember : OperatorEnumerateMember - { - internal AdditionEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - - protected override SyntaxKind? GetSyntaxKind(IMethodSymbol symbol) - => symbol switch - { - { Parameters: { Length: not 2 } } => null, - { Name: "Add" } => SyntaxKind.AddExpression, - _ => null, - }; - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/CastEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/CastEnumerateMember.cs deleted file mode 100644 index 32605b72..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/CastEnumerateMember.cs +++ /dev/null @@ -1,27 +0,0 @@ -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class CastEnumerateMember : EnumerateMember - { - internal CastEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - protected override MethodDeclarationSyntax CreateMethodSyntax(IMethodSymbol symbol, bool isStatic) - { - if (symbol is - { - Name: "Cast", - Parameters: { Length: 1 } - }) - { - return CreateMethodSyntax( - SemanticModel, SemanticModel.SyntaxTree.Length - 1, - symbol, isStatic, - ArrowExpressionClause(CastExpression(symbol.ReturnType.ToTypeSyntax(SemanticModel, SemanticModel.SyntaxTree.Length - 1), IdentifierName(symbol.Parameters[0].Name)))); - } - return base.CreateMethodSyntax(symbol, isStatic); - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/CompareOperatorEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/CompareOperatorEnumerateMember.cs deleted file mode 100644 index 631f4d77..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/CompareOperatorEnumerateMember.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class CompareOperatorEnumerateMember : OperatorEnumerateMember - { - internal CompareOperatorEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - - protected override SyntaxKind? GetSyntaxKind(IMethodSymbol symbol) - => symbol switch - { - { Parameters: { Length: not 2 } } => null, - { Name: "GreaterThan" } => SyntaxKind.GreaterThanExpression, - { Name: "GreaterThanOrEqual" } => SyntaxKind.GreaterThanOrEqualExpression, - { Name: "LessThan" } => SyntaxKind.LessThanExpression, - { Name: "LessThanOrEqual" } => SyntaxKind.LessThanOrEqualExpression, - _ => null, - }; - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/ComparerEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/ComparerEnumerateMember.cs deleted file mode 100644 index 25b91b26..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/ComparerEnumerateMember.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class ComparerEnumerateMember : EnumerateMember - { - internal ComparerEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - protected override MethodDeclarationSyntax CreateMethodSyntax(IMethodSymbol symbol, bool isStatic) - { - if (symbol is - { - Name: nameof(IComparer.Compare), - Parameters: { Length: 2 } - }) - { - var caller = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, - IdentifierName(symbol.Parameters[0].Name), - IdentifierName(nameof(IComparable.CompareTo))); - var args = ArgumentList(SingletonSeparatedList(Argument(IdentifierName(symbol.Parameters[1].Name)))); - var invocation = InvocationExpression(caller, args); - return CreateMethodSyntax( - SemanticModel, SemanticModel.SyntaxTree.Length - 1, - symbol, isStatic, ArrowExpressionClause(invocation)); - } - return base.CreateMethodSyntax(symbol, isStatic); - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/DivisionEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/DivisionEnumerateMember.cs deleted file mode 100644 index df27df5a..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/DivisionEnumerateMember.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class DivisionEnumerateMember : OperatorEnumerateMember - { - internal DivisionEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - - protected override SyntaxKind? GetSyntaxKind(IMethodSymbol symbol) - => symbol switch - { - { Parameters: { Length: not 2 } } => null, - { Name: "Divide" } => SyntaxKind.DivideExpression, - { Name: "Modulo" } => SyntaxKind.ModuloExpression, - _ => null, - }; - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/MinMaxValueEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/MinMaxValueEnumerateMember.cs deleted file mode 100644 index bc4f9e18..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/MinMaxValueEnumerateMember.cs +++ /dev/null @@ -1,30 +0,0 @@ -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class MinMaxValueEnumerateMember : EnumerateMember - { - internal MinMaxValueEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - protected override PropertyDeclarationSyntax CreatePropertySyntax(IPropertySymbol symbol, bool isStatic) - { - if (symbol is { Name: "MaxValue" or "MinValue" }) - { - var returnType = symbol.Type.ToTypeSyntax(SemanticModel, SemanticModel.SyntaxTree.Length - 1); - var property = MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - returnType, - IdentifierName(symbol.Name)); - - return PropertyDeclaration(returnType, symbol.Name) - .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))) - .WithExpressionBody(ArrowExpressionClause(property)) - .WithSemicolonToken(SyntaxHelpers.SemicolonToken); - } - return base.CreatePropertySyntax(symbol, isStatic); - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/MultiplicationEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/MultiplicationEnumerateMember.cs deleted file mode 100644 index 80bca94a..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/MultiplicationEnumerateMember.cs +++ /dev/null @@ -1,33 +0,0 @@ -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class MultiplicationEnumerateMember : OperatorEnumerateMember - { - internal MultiplicationEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - - protected override SyntaxKind? GetSyntaxKind(IMethodSymbol symbol) - => symbol switch - { - { Parameters: { Length: not 2 } } => null, - { Name: "Multiply" } => SyntaxKind.MultiplyExpression, - _ => null, - }; - - protected override PropertyDeclarationSyntax CreatePropertySyntax(IPropertySymbol symbol, bool isStatic) - { - if (symbol.Name == "MultiplyIdentity") - return PropertyDeclaration(symbol.Type.ToTypeSyntax(SemanticModel, SemanticModel.SyntaxTree.Length - 1), symbol.Name) - .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword))) - .WithExpressionBody(ArrowExpressionClause( - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(1))) - ) - .WithSemicolonToken(SyntaxHelpers.SemicolonToken); - return base.CreatePropertySyntax(symbol, isStatic); - } - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/ShiftEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/ShiftEnumerateMember.cs deleted file mode 100644 index 2452b206..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/ShiftEnumerateMember.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class ShiftEnumerateMember : OperatorEnumerateMember - { - internal ShiftEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - protected override SyntaxKind? GetSyntaxKind(IMethodSymbol symbol) - => symbol switch - { - { Parameters: { Length: not 2 } } => null, - { Name: "LeftShift" } => SyntaxKind.LeftShiftExpression, - { Name: "RightShift" } => SyntaxKind.RightShiftExpression, - _ => null, - }; - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/SubtractEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/SubtractEnumerateMember.cs deleted file mode 100644 index 8eb71176..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/SubtractEnumerateMember.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class SubtractEnumerateMember : OperatorEnumerateMember - { - internal SubtractEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - - protected override SyntaxKind? GetSyntaxKind(IMethodSymbol symbol) - => symbol switch - { - { Parameters: { Length: not 2 } } => null, - { Name: "Subtract" } => SyntaxKind.SubtractExpression, - _ => null, - }; - } -} diff --git a/Source/AtCoderAnalyzer/CreateOperators/Specified/UnaryNumEnumerateMember.cs b/Source/AtCoderAnalyzer/CreateOperators/Specified/UnaryNumEnumerateMember.cs deleted file mode 100644 index 68e6a708..00000000 --- a/Source/AtCoderAnalyzer/CreateOperators/Specified/UnaryNumEnumerateMember.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.CreateOperators.Specified -{ - internal class UnaryNumEnumerateMember : EnumerateMember - { - internal UnaryNumEnumerateMember(SemanticModel semanticModel, ITypeSymbol typeSymbol, AtCoderAnalyzerConfig config) : base(semanticModel, typeSymbol, config) { } - - protected override MethodDeclarationSyntax CreateMethodSyntax(IMethodSymbol symbol, bool isStatic) - { - if (symbol switch - { - { Parameters: { Length: not 1 } } => null, - { Name: "Minus" } => SyntaxKind.UnaryMinusExpression, - { Name: "Increment" } => SyntaxKind.PreIncrementExpression, - { Name: "Decrement" } => SyntaxKind.PreDecrementExpression, - _ => (SyntaxKind?)null, - } is SyntaxKind kind) - { - var operatorCall = PrefixUnaryExpression( - kind, - IdentifierName(symbol.Parameters[0].Name)); - return CreateMethodSyntax( - SemanticModel, SemanticModel.SyntaxTree.Length - 1, - symbol, isStatic, ArrowExpressionClause(operatorCall)); - } - return base.CreateMethodSyntax(symbol, isStatic); - } - } -} diff --git a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticDescriptors.cs b/Source/AtCoderAnalyzer/Diagnostics/DiagnosticDescriptors.cs deleted file mode 100644 index 83687831..00000000 --- a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticDescriptors.cs +++ /dev/null @@ -1,78 +0,0 @@ -using System.Collections.Generic; -using Microsoft.CodeAnalysis; - -namespace AtCoderAnalyzer.Diagnostics -{ - public static class DiagnosticDescriptors - { -#pragma warning disable IDE0090 // Avoid 'new(...)' for Shipped.md - internal static Diagnostic AC0001_MultiplyOverflowInt32(SyntaxNode node) - => Diagnostic.Create(AC0001_MultiplyOverflowInt32_Descriptor, node.GetLocation(), node.ToString()); - internal static readonly DiagnosticDescriptor AC0001_MultiplyOverflowInt32_Descriptor = new DiagnosticDescriptor( - "AC0001", - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0001_Title), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0001_AC0002_MessageFormat), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - "Overflow", - DiagnosticSeverity.Warning, - isEnabledByDefault: true - ); - internal static Diagnostic AC0002_LeftShiftOverflowInt32(SyntaxNode node) - => Diagnostic.Create(AC0002_LeftShiftOverflowInt32_Descriptor, node.GetLocation(), node.ToString()); - internal static readonly DiagnosticDescriptor AC0002_LeftShiftOverflowInt32_Descriptor = new DiagnosticDescriptor( - "AC0002", - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0002_Title), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0001_AC0002_MessageFormat), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - "Overflow", - DiagnosticSeverity.Warning, - isEnabledByDefault: true - ); - - internal static Diagnostic AC0007_AgressiveInlining(Location location, IEnumerable methods) - => Diagnostic.Create(AC0007_AgressiveInlining_Descriptor, location, string.Join(", ", methods)); - - internal static readonly DiagnosticDescriptor AC0007_AgressiveInlining_Descriptor = new DiagnosticDescriptor( - "AC0007", - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0007_Title), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0007_MessageFormat), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - "Type Define", - DiagnosticSeverity.Info, - isEnabledByDefault: true - ); - - internal static Diagnostic AC0008_DefineOperatorType(Location location, IEnumerable types) - => Diagnostic.Create(AC0008_DefineOperatorType_Descriptor, location, string.Join(", ", types)); - internal static readonly DiagnosticDescriptor AC0008_DefineOperatorType_Descriptor = new DiagnosticDescriptor( - "AC0008", - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0008_Title), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - new LocalizableResourceString( - nameof(DiagnosticsResources.AC0008_MessageFormat), - DiagnosticsResources.ResourceManager, - typeof(DiagnosticsResources)), - "Type Define", - DiagnosticSeverity.Error, - isEnabledByDefault: true - ); -#pragma warning restore IDE0090 - } -} diff --git a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.Designer.cs b/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.Designer.cs deleted file mode 100644 index e9393a2b..00000000 --- a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.Designer.cs +++ /dev/null @@ -1,198 +0,0 @@ -//------------------------------------------------------------------------------ -// -// このコードはツールによって生成されました。 -// ランタイム バージョン:4.0.30319.42000 -// -// このファイルへの変更は、以下の状況下で不正な動作の原因になったり、 -// コードが再生成されるときに損失したりします。 -// -//------------------------------------------------------------------------------ - -namespace AtCoderAnalyzer.Diagnostics { - using System; - - - /// - /// ローカライズされた文字列などを検索するための、厳密に型指定されたリソース クラスです。 - /// - // このクラスは StronglyTypedResourceBuilder クラスが ResGen - // または Visual Studio のようなツールを使用して自動生成されました。 - // メンバーを追加または削除するには、.ResX ファイルを編集して、/str オプションと共に - // ResGen を実行し直すか、または VS プロジェクトをビルドし直します。 - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class DiagnosticsResources { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal DiagnosticsResources() { - } - - /// - /// このクラスで使用されているキャッシュされた ResourceManager インスタンスを返します。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("AtCoderAnalyzer.Diagnostics.DiagnosticsResources", typeof(DiagnosticsResources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// すべてについて、現在のスレッドの CurrentUICulture プロパティをオーバーライドします - /// 現在のスレッドの CurrentUICulture プロパティをオーバーライドします。 - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - - /// - /// Implicit cast int to long: '{0}' に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0001_AC0002_MessageFormat { - get { - return ResourceManager.GetString("AC0001_AC0002_MessageFormat", resourceCulture); - } - } - - /// - /// int multiply expression is assigned to long に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0001_Title { - get { - return ResourceManager.GetString("AC0001_Title", resourceCulture); - } - } - - /// - /// int left shift expression is assigned to long に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0002_Title { - get { - return ResourceManager.GetString("AC0002_Title", resourceCulture); - } - } - - /// - /// Not defined IStaticMod: '{0}' に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0003_MessageFormat { - get { - return ResourceManager.GetString("AC0003_MessageFormat", resourceCulture); - } - } - - /// - /// Not defined IStaticMod に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0003_Title { - get { - return ResourceManager.GetString("AC0003_Title", resourceCulture); - } - } - - /// - /// Not defined IDynamicModID: '{0}' に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0004_MessageFormat { - get { - return ResourceManager.GetString("AC0004_MessageFormat", resourceCulture); - } - } - - /// - /// Not defined IDynamicModID に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0004_Title { - get { - return ResourceManager.GetString("AC0004_Title", resourceCulture); - } - } - - /// - /// Not defined ISegtreeOperator<T>: '{0}' に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0005_MessageFormat { - get { - return ResourceManager.GetString("AC0005_MessageFormat", resourceCulture); - } - } - - /// - /// Not defined ISegtreeOperator<T> に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0005_Title { - get { - return ResourceManager.GetString("AC0005_Title", resourceCulture); - } - } - - /// - /// Not defined ILazySegtreeOperator<T, F>: '{0}' に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0006_MessageFormat { - get { - return ResourceManager.GetString("AC0006_MessageFormat", resourceCulture); - } - } - - /// - /// Not defined ILazySegtreeOperator<T, F> に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0006_Title { - get { - return ResourceManager.GetString("AC0006_Title", resourceCulture); - } - } - - /// - /// Operator method '{0}' doesn't have MethodImpl attribute に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0007_MessageFormat { - get { - return ResourceManager.GetString("AC0007_MessageFormat", resourceCulture); - } - } - - /// - /// Operator method doesn't have MethodImpl attribute に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0007_Title { - get { - return ResourceManager.GetString("AC0007_Title", resourceCulture); - } - } - - /// - /// Not defined operator type: '{0}' に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0008_MessageFormat { - get { - return ResourceManager.GetString("AC0008_MessageFormat", resourceCulture); - } - } - - /// - /// Not defined operator type に類似しているローカライズされた文字列を検索します。 - /// - internal static string AC0008_Title { - get { - return ResourceManager.GetString("AC0008_Title", resourceCulture); - } - } - } -} diff --git a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.ja-JP.resx b/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.ja-JP.resx deleted file mode 100644 index c972e45f..00000000 --- a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.ja-JP.resx +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - intからlongへ暗黙的キャスト: '{0}' - - - intの乗算演算子がlongに代入されています - - - intの左シフト演算子がlongに代入されています - - \ No newline at end of file diff --git a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.resx b/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.resx deleted file mode 100644 index 28da9c1f..00000000 --- a/Source/AtCoderAnalyzer/Diagnostics/DiagnosticsResources.resx +++ /dev/null @@ -1,165 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Implicit cast int to long: '{0}' - - - int multiply expression is assigned to long - - - int left shift expression is assigned to long - - - Not defined IStaticMod: '{0}' - - - Not defined IStaticMod - - - Not defined IDynamicModID: '{0}' - - - Not defined IDynamicModID - - - Not defined ISegtreeOperator<T>: '{0}' - - - Not defined ISegtreeOperator<T> - - - Not defined ILazySegtreeOperator<T, F>: '{0}' - - - Not defined ILazySegtreeOperator<T, F> - - - Operator method '{0}' doesn't have MethodImpl attribute - - - Operator method doesn't have MethodImpl attribute - - - Not defined operator type: '{0}' - - - Not defined operator type - - \ No newline at end of file diff --git a/Source/AtCoderAnalyzer/Helpers/Constants.cs b/Source/AtCoderAnalyzer/Helpers/Constants.cs deleted file mode 100644 index 149235ee..00000000 --- a/Source/AtCoderAnalyzer/Helpers/Constants.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace AtCoderAnalyzer.Helpers -{ - internal static class Constants - { - public const string System_Runtime_CompilerServices_MethodImplAttribute - = "System.Runtime.CompilerServices.MethodImplAttribute"; - public const string System_Runtime_CompilerServices_MethodImplOptions - = "System.Runtime.CompilerServices.MethodImplOptions"; - public const string System_Collections_Generic_IComparer - = "System.Collections.Generic.IComparer`1"; - - public const string AtCoder_IsOperatorAttribute - = "AtCoder.IsOperatorAttribute"; - } -} diff --git a/Source/AtCoderAnalyzer/Helpers/MemberDeclarationEqualityComparer.cs b/Source/AtCoderAnalyzer/Helpers/MemberDeclarationEqualityComparer.cs deleted file mode 100644 index 03c2b140..00000000 --- a/Source/AtCoderAnalyzer/Helpers/MemberDeclarationEqualityComparer.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System.Collections.Generic; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -namespace AtCoderAnalyzer.Helpers -{ - internal class MemberDeclarationEqualityComparer : IEqualityComparer - { - public static MemberDeclarationEqualityComparer Default { get; } = new MemberDeclarationEqualityComparer(); - public bool Equals(MemberDeclarationSyntax x, MemberDeclarationSyntax y) => x.IsEquivalentTo(y, true); - public int GetHashCode(MemberDeclarationSyntax obj) => obj.RawKind; - } -} diff --git a/Source/AtCoderAnalyzer/Helpers/OperatorTypesMatcher.cs b/Source/AtCoderAnalyzer/Helpers/OperatorTypesMatcher.cs deleted file mode 100644 index c7ce15e3..00000000 --- a/Source/AtCoderAnalyzer/Helpers/OperatorTypesMatcher.cs +++ /dev/null @@ -1,42 +0,0 @@ -using Microsoft.CodeAnalysis; -using static AtCoderAnalyzer.Helpers.Constants; - -namespace AtCoderAnalyzer.Helpers -{ - public class OperatorTypesMatcher - { - public INamedTypeSymbol IsOperatorAttribute { get; } - public INamedTypeSymbol IComparer { get; } - - public OperatorTypesMatcher(INamedTypeSymbol isOperator, INamedTypeSymbol iComparer) - { - IsOperatorAttribute = isOperator; - IComparer = iComparer; - } - - public bool IsMatch(ITypeSymbol typeSymbol) - { - if (SymbolEqualityComparer.Default.Equals(typeSymbol, IComparer)) - return true; - - foreach (var at in typeSymbol.GetAttributes()) - { - if (SymbolEqualityComparer.Default.Equals(at.AttributeClass, IsOperatorAttribute)) - return true; - } - return false; - } - public static bool TryParseTypes(Compilation compilation, out OperatorTypesMatcher types) - { - types = null; - var isOperatorAttr = compilation.GetTypeByMetadataName(AtCoder_IsOperatorAttribute); - if (isOperatorAttr is null) - return false; - var iComparer = compilation.GetTypeByMetadataName(System_Collections_Generic_IComparer); - if (iComparer is null) - return false; - types = new OperatorTypesMatcher(isOperatorAttr, iComparer); - return true; - } - } -} diff --git a/Source/AtCoderAnalyzer/Helpers/SymbolHelpers.cs b/Source/AtCoderAnalyzer/Helpers/SymbolHelpers.cs deleted file mode 100644 index 51b1b478..00000000 --- a/Source/AtCoderAnalyzer/Helpers/SymbolHelpers.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System.Collections.Immutable; -using System.Linq; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.Helpers -{ - public static class SymbolHelpers - { - public static ITypeSymbol ReplaceGenericType(ITypeSymbol symbol, - ImmutableDictionary convDic) - { - if (symbol is ITypeParameterSymbol typeParam) - return convDic.TryGetValue(typeParam, out var newType) ? newType : typeParam; - if (symbol is not INamedTypeSymbol named) - return symbol; - - var typeArguments = named.TypeArguments; - if (typeArguments.Length == 0) - return symbol; - var newTypeArguments = new ITypeSymbol[typeArguments.Length]; - for (int i = 0; i < newTypeArguments.Length; i++) - { - newTypeArguments[i] = ReplaceGenericType(typeArguments[i], convDic); - } - - return named.ConstructedFrom.Construct(newTypeArguments); - } - - public static TypeSyntax ToTypeSyntax(this ITypeSymbol symbol, SemanticModel model, int position) - => ParseTypeName(symbol.ToMinimalDisplayString(model, position)); - - public static ParameterSyntax ToParameterSyntax(this IParameterSymbol symbol, SemanticModel model, int position) - { - var modifiers = symbol switch - { - { IsParams: true } => TokenList(Token(SyntaxKind.ParamsKeyword)), - { RefKind: RefKind.Out } => TokenList(Token(SyntaxKind.OutKeyword)), - { RefKind: RefKind.Ref } => TokenList(Token(SyntaxKind.RefKeyword)), - { RefKind: RefKind.In } => TokenList(Token(SyntaxKind.InKeyword)), - _ => TokenList(), - }; - return Parameter(Identifier(symbol.Name)) - .WithModifiers(modifiers) - .WithType(symbol.Type.ToTypeSyntax(model, position)); - } - - public static ParameterListSyntax ToParameterListSyntax(this IMethodSymbol symbol, SemanticModel model, int position) - { - if (symbol.Parameters.Length == 0) - return ParameterList(); - - return ParameterList(SeparatedList( - symbol.Parameters.Select(p => p.ToParameterSyntax(model, position)))); - } - } -} diff --git a/Source/AtCoderAnalyzer/Helpers/SyntaxHelpers.cs b/Source/AtCoderAnalyzer/Helpers/SyntaxHelpers.cs deleted file mode 100644 index e469aa5f..00000000 --- a/Source/AtCoderAnalyzer/Helpers/SyntaxHelpers.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Linq; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace AtCoderAnalyzer.Helpers -{ - internal static class SyntaxHelpers - { - public static ImmutableHashSet ToNamespaceHashSet(this SyntaxList usings) - => usings - .Where(sy => sy.Alias == null && sy.StaticKeyword.IsKind(SyntaxKind.None)) - .Select(sy => sy.Name.ToString().Trim()) - .ToImmutableHashSet(); - - public static SeparatedSyntaxList ToSeparatedSyntaxList(this IEnumerable nodes) where TNode : SyntaxNode - => SeparatedList(nodes); - public static CompilationUnitSyntax AddSystem_Runtime_CompilerServicesSyntax(CompilationUnitSyntax root) - { - return root.AddUsings( - UsingDirective( - QualifiedName( - QualifiedName( - IdentifierName("System"), - IdentifierName("Runtime") - ), - IdentifierName("CompilerServices") - ))); - } - - public static MemberAccessExpressionSyntax AggressiveInliningMember(string methodImplOptions) - => MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - ParseExpression(methodImplOptions), IdentifierName("AggressiveInlining")); - public static AttributeSyntax AggressiveInliningAttribute(string methodImplAttribute, string methodImplOptions, bool useNumeric) - => Attribute( - ParseName(methodImplAttribute.EndsWith("Attribute") - ? methodImplAttribute.Substring(0, methodImplAttribute.Length - "Attribute".Length) - : methodImplAttribute) - ).AddArgumentListArguments(AttributeArgument( - useNumeric ? LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(256)) : AggressiveInliningMember(methodImplOptions) - )); - - public static readonly ArrowExpressionClauseSyntax ArrowDefault - = ArrowExpressionClause( - LiteralExpression(SyntaxKind.DefaultLiteralExpression)); - - public static readonly SyntaxToken SemicolonToken = Token(SyntaxKind.SemicolonToken); - } -} diff --git a/Source/AtCoderAnalyzer/Properties/launchSettings.json b/Source/AtCoderAnalyzer/Properties/launchSettings.json deleted file mode 100644 index 6471791a..00000000 --- a/Source/AtCoderAnalyzer/Properties/launchSettings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "profiles": { - "AtCoderAnalyzer": { - "commandName": "DebugRoslynComponent", - "targetProject": "..\\ac-library-csharp\\ac-library-csharp.csproj" - } - } -} \ No newline at end of file diff --git a/Source/AtCoderAnalyzer/tools/install.ps1 b/Source/AtCoderAnalyzer/tools/install.ps1 deleted file mode 100644 index 8019b738..00000000 --- a/Source/AtCoderAnalyzer/tools/install.ps1 +++ /dev/null @@ -1,49 +0,0 @@ -param($installPath, $toolsPath, $package, $project) - -$analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers" ) * -Resolve - -foreach($analyzersPath in $analyzersPaths) -{ - # Install the language agnostic analyzers. - if (Test-Path $analyzersPath) - { - foreach ($analyzerFilePath in Get-ChildItem $analyzersPath -Filter *.dll) - { - if($project.Object.AnalyzerReferences) - { - $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName) - } - } - } -} - -$project.Type # gives the language name like (C# or VB.NET) -$languageFolder = "" -if($project.Type -eq "C#") -{ - $languageFolder = "cs" -} -if($project.Type -eq "VB.NET") -{ - $languageFolder = "vb" -} -if($languageFolder -eq "") -{ - return -} - -foreach($analyzersPath in $analyzersPaths) -{ - # Install language specific analyzers. - $languageAnalyzersPath = join-path $analyzersPath $languageFolder - if (Test-Path $languageAnalyzersPath) - { - foreach ($analyzerFilePath in Get-ChildItem $languageAnalyzersPath -Filter *.dll) - { - if($project.Object.AnalyzerReferences) - { - $project.Object.AnalyzerReferences.Add($analyzerFilePath.FullName) - } - } - } -} \ No newline at end of file diff --git a/Source/AtCoderAnalyzer/tools/uninstall.ps1 b/Source/AtCoderAnalyzer/tools/uninstall.ps1 deleted file mode 100644 index 65a86237..00000000 --- a/Source/AtCoderAnalyzer/tools/uninstall.ps1 +++ /dev/null @@ -1,65 +0,0 @@ -param($installPath, $toolsPath, $package, $project) - -if($project.Object.SupportsPackageDependencyResolution) -{ - if($project.Object.SupportsPackageDependencyResolution()) - { - # Do not uninstall analyzers via uninstall.ps1, instead let the project system handle it. - return - } -} - -$analyzersPaths = Join-Path (Join-Path (Split-Path -Path $toolsPath -Parent) "analyzers") * -Resolve - -foreach($analyzersPath in $analyzersPaths) -{ - # Uninstall the language agnostic analyzers. - if (Test-Path $analyzersPath) - { - foreach ($analyzerFilePath in Get-ChildItem -Path "$analyzersPath\*.dll" -Exclude *.resources.dll) - { - if($project.Object.AnalyzerReferences) - { - $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName) - } - } - } -} - -# $project.Type gives the language name like (C# or VB.NET) -$languageFolder = "" -if($project.Type -eq "C#") -{ - $languageFolder = "cs" -} -if($project.Type -eq "VB.NET") -{ - $languageFolder = "vb" -} -if($languageFolder -eq "") -{ - return -} - -foreach($analyzersPath in $analyzersPaths) -{ - # Uninstall language specific analyzers. - $languageAnalyzersPath = join-path $analyzersPath $languageFolder - if (Test-Path $languageAnalyzersPath) - { - foreach ($analyzerFilePath in Get-ChildItem -Path "$languageAnalyzersPath\*.dll" -Exclude *.resources.dll) - { - if($project.Object.AnalyzerReferences) - { - try - { - $project.Object.AnalyzerReferences.Remove($analyzerFilePath.FullName) - } - catch - { - - } - } - } - } -} \ No newline at end of file diff --git a/Source/ac-library-csharp/ac-library-csharp.csproj b/Source/ac-library-csharp/ac-library-csharp.csproj index 57ad6bf4..cd2f52d8 100644 --- a/Source/ac-library-csharp/ac-library-csharp.csproj +++ b/Source/ac-library-csharp/ac-library-csharp.csproj @@ -37,10 +37,6 @@ - - false - Analyzer - all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Test/AtCoderAnalyzer.Test/AC0001_AC0002_IntToLongTest.cs b/Test/AtCoderAnalyzer.Test/AC0001_AC0002_IntToLongTest.cs deleted file mode 100644 index 469f9e82..00000000 --- a/Test/AtCoderAnalyzer.Test/AC0001_AC0002_IntToLongTest.cs +++ /dev/null @@ -1,315 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Testing; -using Xunit; -using VerifyCS = AtCoderAnalyzer.Test.CSharpCodeFixVerifier< - AtCoderAnalyzer.AC0001_AC0002_IntToLongAnalyzer, - AtCoderAnalyzer.AC0001_AC0002_IntToLongCodeFixProvider>; - -namespace AtCoderAnalyzer.Test -{ - public class AC0001_AC0002_IntToLongTest - { - [Fact] - public async Task Empty() - { - var source = @" -class Program -{ - long Long = 0; - long LongProperty => 0; - long Return() => 0; - void Arg(long arg){ } - void Arg2(){ Arg(0); } -} -"; - - await VerifyCS.VerifyAnalyzerAsync(source); - } - - - [Fact] - public async Task Mixed() - { - var source = @" -class Program -{ - static void F(int num) - { - long v; - v = num * 2 * 3; - v = 4 * 5 * num; - v = num * 6L * 7; - v = num * 8 * 9L; - v += 10 * num; - v = 11 * num + num / 12 - num * 13 + 14 - 15; - - v *= num; - v <<= num; - } -} -"; - - var fixedSource = @" -class Program -{ - static void F(int num) - { - long v; - v = (long)num * 2 * 3; - v = 4L * 5 * num; - v = num * 6L * 7; - v = (long)num * 8 * 9L; - v += 10L * num; - v = 11L * num + num / 12 - (long)num * 13 + 14 - 15; - - v *= num; - v <<= num; - } -} -"; - await VerifyCS.VerifyCodeFixAsync(source, - new DiagnosticResult[] { - VerifyCS.Diagnostic("AC0001").WithSpan(7, 13, 7, 20).WithArguments("num * 2"), - VerifyCS.Diagnostic("AC0001").WithSpan(7, 13, 7, 24).WithArguments("num * 2 * 3"), - VerifyCS.Diagnostic("AC0001").WithSpan(8, 13, 8, 18).WithArguments("4 * 5"), - VerifyCS.Diagnostic("AC0001").WithSpan(8, 13, 8, 24).WithArguments("4 * 5 * num"), - VerifyCS.Diagnostic("AC0001").WithSpan(10, 13, 10, 20).WithArguments("num * 8"), - VerifyCS.Diagnostic("AC0001").WithSpan(11, 14, 11, 22).WithArguments("10 * num"), - VerifyCS.Diagnostic("AC0001").WithSpan(12, 13, 12, 21).WithArguments("11 * num"), - VerifyCS.Diagnostic("AC0001").WithSpan(12, 35, 12, 43).WithArguments("num * 13"), - }, - fixedSource); - } - - [Fact] - public async Task Literal() - { - var source = @" -class Multiply -{ - long Long = 1000 * 1001; - long LongProperty => 1002 * 1003; - long Return() => 1004 * 1005; - void Arg(long arg){ } - void Arg2(){ Arg(1006 * 1007); } -} -class LeftShift -{ - long Long = 1000 << 1; - long LongProperty => 1002 << 3; - long Return() => 1004 << 5; - void Arg(long arg){ } - void Arg2(){ Arg(1006 << 7); } -} -"; - - var fixedSource = @" -class Multiply -{ - long Long = 1000L * 1001; - long LongProperty => 1002L * 1003; - long Return() => 1004L * 1005; - void Arg(long arg){ } - void Arg2(){ Arg(1006L * 1007); } -} -class LeftShift -{ - long Long = 1000L << 1; - long LongProperty => 1002L << 3; - long Return() => 1004L << 5; - void Arg(long arg){ } - void Arg2(){ Arg(1006L << 7); } -} -"; - await VerifyCS.VerifyCodeFixAsync(source, - new DiagnosticResult[] { - VerifyCS.Diagnostic("AC0001").WithSpan(4, 17, 4, 28).WithArguments("1000 * 1001"), - VerifyCS.Diagnostic("AC0001").WithSpan(5, 26, 5, 37).WithArguments("1002 * 1003"), - VerifyCS.Diagnostic("AC0001").WithSpan(6, 22, 6, 33).WithArguments("1004 * 1005"), - VerifyCS.Diagnostic("AC0001").WithSpan(8, 22, 8, 33).WithArguments("1006 * 1007"), - VerifyCS.Diagnostic("AC0002").WithSpan(12, 17, 12, 26).WithArguments("1000 << 1"), - VerifyCS.Diagnostic("AC0002").WithSpan(13, 26, 13, 35).WithArguments("1002 << 3"), - VerifyCS.Diagnostic("AC0002").WithSpan(14, 22, 14, 31).WithArguments("1004 << 5"), - VerifyCS.Diagnostic("AC0002").WithSpan(16, 22, 16, 31).WithArguments("1006 << 7"), - }, - fixedSource); - } - - - [Fact] - public async Task Const() - { - var source = @" -class Multiply -{ - const int num = 0; - long Long = num * 1001; - long LongProperty => num * 1003; - long Return() => num * 1005; - void Arg(long arg){ } - void Arg2(){ Arg(num * 1007); } -} -class LeftShift -{ - const int num = 0; - long Long = num << 1; - long LongProperty => num << 3; - long Return() => num << 5; - void Arg(long arg){ } - void Arg2(){ Arg(num << 7); } -} -"; - - var fixedSource = @" -class Multiply -{ - const int num = 0; - long Long = (long)num * 1001; - long LongProperty => (long)num * 1003; - long Return() => (long)num * 1005; - void Arg(long arg){ } - void Arg2(){ Arg((long)num * 1007); } -} -class LeftShift -{ - const int num = 0; - long Long = (long)num << 1; - long LongProperty => (long)num << 3; - long Return() => (long)num << 5; - void Arg(long arg){ } - void Arg2(){ Arg((long)num << 7); } -} -"; - await VerifyCS.VerifyCodeFixAsync(source, - new DiagnosticResult[] { - VerifyCS.Diagnostic("AC0001").WithSpan(5, 17, 5, 27).WithArguments("num * 1001"), - VerifyCS.Diagnostic("AC0001").WithSpan(6, 26, 6, 36).WithArguments("num * 1003"), - VerifyCS.Diagnostic("AC0001").WithSpan(7, 22, 7, 32).WithArguments("num * 1005"), - VerifyCS.Diagnostic("AC0001").WithSpan(9, 22, 9, 32).WithArguments("num * 1007"), - VerifyCS.Diagnostic("AC0002").WithSpan(14, 17, 14, 25).WithArguments("num << 1"), - VerifyCS.Diagnostic("AC0002").WithSpan(15, 26, 15, 34).WithArguments("num << 3"), - VerifyCS.Diagnostic("AC0002").WithSpan(16, 22, 16, 30).WithArguments("num << 5"), - VerifyCS.Diagnostic("AC0002").WithSpan(18, 22, 18, 30).WithArguments("num << 7"), - }, - fixedSource); - } - - - [Fact] - public async Task Variable() - { - var source = @" -class Multiply -{ - static int num = 0; - long Long = num * 1001; - long LongProperty => num * 1003; - long Return() => num * 1005; - void Arg(long arg){ } - void Arg2(){ Arg(num * 1007); } -} -class LeftShift -{ - static int num = 0; - long Long = num << 1; - long LongProperty => num << 3; - long Return() => num << 5; - void Arg(long arg){ } - void Arg2(){ Arg(num << 7); } -} -"; - - var fixedSource = @" -class Multiply -{ - static int num = 0; - long Long = (long)num * 1001; - long LongProperty => (long)num * 1003; - long Return() => (long)num * 1005; - void Arg(long arg){ } - void Arg2(){ Arg((long)num * 1007); } -} -class LeftShift -{ - static int num = 0; - long Long = (long)num << 1; - long LongProperty => (long)num << 3; - long Return() => (long)num << 5; - void Arg(long arg){ } - void Arg2(){ Arg((long)num << 7); } -} -"; - await VerifyCS.VerifyCodeFixAsync(source, - new DiagnosticResult[] { - VerifyCS.Diagnostic("AC0001").WithSpan(5, 17, 5, 27).WithArguments("num * 1001"), - VerifyCS.Diagnostic("AC0001").WithSpan(6, 26, 6, 36).WithArguments("num * 1003"), - VerifyCS.Diagnostic("AC0001").WithSpan(7, 22, 7, 32).WithArguments("num * 1005"), - VerifyCS.Diagnostic("AC0001").WithSpan(9, 22, 9, 32).WithArguments("num * 1007"), - VerifyCS.Diagnostic("AC0002").WithSpan(14, 17, 14, 25).WithArguments("num << 1"), - VerifyCS.Diagnostic("AC0002").WithSpan(15, 26, 15, 34).WithArguments("num << 3"), - VerifyCS.Diagnostic("AC0002").WithSpan(16, 22, 16, 30).WithArguments("num << 5"), - VerifyCS.Diagnostic("AC0002").WithSpan(18, 22, 18, 30).WithArguments("num << 7"), - }, - fixedSource); - } - - [Fact] - public async Task VariableShort() - { - var source = @" -class Multiply -{ - static short num = 0; - long Long = num * 1001; - long LongProperty => num * 1003; - long Return() => num * 1005; - void Arg(long arg){ } - void Arg2(){ Arg(num * 1007); } -} -class LeftShift -{ - static short num = 0; - long Long = num << 1; - long LongProperty => num << 3; - long Return() => num << 5; - void Arg(long arg){ } - void Arg2(){ Arg(num << 7); } -} -"; - - var fixedSource = @" -class Multiply -{ - static short num = 0; - long Long = (long)num * 1001; - long LongProperty => (long)num * 1003; - long Return() => (long)num * 1005; - void Arg(long arg){ } - void Arg2(){ Arg((long)num * 1007); } -} -class LeftShift -{ - static short num = 0; - long Long = (long)num << 1; - long LongProperty => (long)num << 3; - long Return() => (long)num << 5; - void Arg(long arg){ } - void Arg2(){ Arg((long)num << 7); } -} -"; - await VerifyCS.VerifyCodeFixAsync(source, - new DiagnosticResult[] { - VerifyCS.Diagnostic("AC0001").WithSpan(5, 17, 5, 27).WithArguments("num * 1001"), - VerifyCS.Diagnostic("AC0001").WithSpan(6, 26, 6, 36).WithArguments("num * 1003"), - VerifyCS.Diagnostic("AC0001").WithSpan(7, 22, 7, 32).WithArguments("num * 1005"), - VerifyCS.Diagnostic("AC0001").WithSpan(9, 22, 9, 32).WithArguments("num * 1007"), - VerifyCS.Diagnostic("AC0002").WithSpan(14, 17, 14, 25).WithArguments("num << 1"), - VerifyCS.Diagnostic("AC0002").WithSpan(15, 26, 15, 34).WithArguments("num << 3"), - VerifyCS.Diagnostic("AC0002").WithSpan(16, 22, 16, 30).WithArguments("num << 5"), - VerifyCS.Diagnostic("AC0002").WithSpan(18, 22, 18, 30).WithArguments("num << 7"), - }, - fixedSource); - } - - } -} diff --git a/Test/AtCoderAnalyzer.Test/AggressiveInliningCodeFixProviderTest.cs b/Test/AtCoderAnalyzer.Test/AggressiveInliningCodeFixProviderTest.cs deleted file mode 100644 index 8dc059b4..00000000 --- a/Test/AtCoderAnalyzer.Test/AggressiveInliningCodeFixProviderTest.cs +++ /dev/null @@ -1,555 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis.Testing; -using Xunit; -using VerifyCS = AtCoderAnalyzer.Test.CSharpCodeFixVerifier< - AtCoderAnalyzer.AggressiveInliningAnalyzer, - AtCoderAnalyzer.AggressiveInliningCodeFixProvider>; - -namespace AtCoderAnalyzer.Test -{ - public class AggressiveInliningCodeFixProviderTest - { - [Fact] - public async Task Empty() - { - var source = @" -using System.Collections.Generic; -struct IntComparer : IComparer -{ - public int Compare(int x, int y) => x.CompareTo(y); -} -"; - await VerifyCS.VerifyAnalyzerAsync(source); - } - - [Fact] - public async Task NumOperator() - { - var source = @" -using AtCoder.Operators; -using System; -using System.Runtime.CompilerServices; -struct BoolOp : INumOperator -{ - public bool MinValue => true; - public bool MaxValue => false; - public bool Add(bool x, bool y) => x || y; - public int Compare(bool x, bool y) => x.CompareTo(y); - public bool Decrement(bool x) => false; - public bool Divide(bool x, bool y) => throw new NotImplementedException(); - public bool Equals(bool x, bool y) => x == y; - public int GetHashCode(bool obj) => obj.GetHashCode(); - public bool GreaterThan(bool x, bool y) => x && !y; - public bool GreaterThanOrEqual(bool x, bool y) => x || !y; - public bool Increment(bool x) => true; - public bool LessThan(bool x, bool y) => y && !x; - public bool LessThanOrEqual(bool x, bool y) => y || !x; - public bool Minus(bool x) => false; - public bool Modulo(bool x, bool y) => true ? true : throw new NotImplementedException(); - public bool Multiply(bool x, bool y) - { - throw new NotImplementedException(); - } - public bool Subtract(bool x, bool y) - { - return default(bool?) ?? throw new NotImplementedException(); - } -} -"; - - var fixedSource = @" -using AtCoder.Operators; -using System; -using System.Runtime.CompilerServices; -struct BoolOp : INumOperator -{ - public bool MinValue => true; - public bool MaxValue => false; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Add(bool x, bool y) => x || y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Compare(bool x, bool y) => x.CompareTo(y); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Decrement(bool x) => false; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Divide(bool x, bool y) => throw new NotImplementedException(); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Equals(bool x, bool y) => x == y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int GetHashCode(bool obj) => obj.GetHashCode(); - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool GreaterThan(bool x, bool y) => x && !y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool GreaterThanOrEqual(bool x, bool y) => x || !y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Increment(bool x) => true; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool LessThan(bool x, bool y) => y && !x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool LessThanOrEqual(bool x, bool y) => y || !x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Minus(bool x) => false; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Modulo(bool x, bool y) => true ? true : throw new NotImplementedException(); - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Multiply(bool x, bool y) - { - throw new NotImplementedException(); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Subtract(bool x, bool y) - { - return default(bool?) ?? throw new NotImplementedException(); - } -} -"; - await VerifyCS.VerifyCodeFixAsync(source, new DiagnosticResult[] { - VerifyCS.Diagnostic().WithSpan(5, 1, 30, 2).WithArguments( - "Add, Compare, Decrement, Divide, Equals, GetHashCode, GreaterThan, GreaterThanOrEqual, Increment, LessThan, LessThanOrEqual, Minus, Modulo, Multiply, Subtract"), - }, fixedSource); - } - - [Fact] - public async Task SegtreeOperator() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct OpSeg : ISegtreeOperator -{ - public int Identity => default; - public int Operate(int x, int y) => System.Math.Max(x, y); -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct OpSeg : ISegtreeOperator -{ - public int Identity => default; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Operate(int x, int y) => System.Math.Max(x, y); -} -"; - await VerifyCS.VerifyCodeFixAsync(source, new DiagnosticResult[] - { - VerifyCS.Diagnostic("AC0007").WithSpan(4, 1, 8, 2).WithArguments("Operate"), - }, fixedSource); - } - - [Fact] - public async Task SegtreeOperator_With_AggressiveInlining() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct OpSeg : ISegtreeOperator -{ - public int Identity => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Operate(int x, int y) => System.Math.Max(x, y); -} -"; - await VerifyCS.VerifyAnalyzerAsync(source); - } - - [Fact] - public async Task LazySegtreeOperator() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - public int Composition(int f, int g) => 0; - public long Mapping(int f, long x) => 0L; - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyCodeFixAsync(source, new DiagnosticResult[] - { - VerifyCS.Diagnostic().WithSpan(4, 1, 11, 2).WithArguments("Composition, Mapping, Operate"), - }, fixedSource); - } - - [Fact] - public async Task LazySegtreeOperator_Without_Using() - { - var source = @" -using AtCoder; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - public int Composition(int f, int g) => 0; - public long Mapping(int f, long x) => 0L; - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @" -using AtCoder; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyCodeFixAsync(source, new DiagnosticResult[] - { -VerifyCS.Diagnostic().WithSpan(3, 1, 10, 2).WithArguments("Composition, Mapping, Operate"), - }, fixedSource); - } - - [Fact] - public async Task LazySegtreeOperator_With_AggressiveInlining() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyAnalyzerAsync(source); - } - - [Fact] - public async Task LazySegtreeOperator_With_Qualified_AggressiveInlining() - { - var source = @" -using AtCoder; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyAnalyzerAsync(source); - } - - [Fact] - public async Task LazySegtreeOperator_Without_AggressiveInlining() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [MethodImpl(MethodImplOptions.NoOptimization)] - public int Composition(int f, int g) => 0; - [MethodImpl(0b1010000)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.NoOptimization)] - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - - [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [MethodImpl(0b1010000 | 256)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.NoOptimization)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyCodeFixAsync(source, new DiagnosticResult[] - { -VerifyCS.Diagnostic().WithSpan(4, 1, 14, 2).WithArguments("Composition, Mapping"), - }, fixedSource); - } - - [Fact] - public async Task LazySegtreeOperator_Without_AggressiveInlining_Equal_Colon() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [MethodImpl(methodImplOptions: MethodImplOptions.NoOptimization)] - public int Composition(int f, int g) => 0; - [MethodImpl(value: 0b1010000)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.NoOptimization, MethodCodeType = MethodCodeType.IL)] - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - - [MethodImpl(methodImplOptions: MethodImplOptions.NoOptimization | MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [MethodImpl(value: 0b1010000 | 256)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.AggressiveInlining, MethodCodeType = MethodCodeType.IL)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyCodeFixAsync(source, new DiagnosticResult[] - { -VerifyCS.Diagnostic().WithSpan(4, 1, 14, 2).WithArguments("Composition, Mapping, Operate"), - }, fixedSource); - } - - [Fact] - public async Task LazySegtreeOperator_With_ArgumentList() - { - var source = @" -using System; -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [MethodImpl] - public int Composition(int f, int g) => 0; - [My, MethodImpl, My] - public long Mapping(int f, long x) => 0L; - [My] - [My, MethodImpl(256|4)] - public long Operate(long x, long y) => 0L; -} -[AttributeUsage(AttributeTargets.All, AllowMultiple = true)] -sealed class MyAttribute : Attribute{} -"; - var fixedSource = @" -using System; -using AtCoder; -using System.Runtime.CompilerServices; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [My, MethodImpl(MethodImplOptions.AggressiveInlining), My] - public long Mapping(int f, long x) => 0L; - [My] - [My, MethodImpl(256|4)] - public long Operate(long x, long y) => 0L; -} -[AttributeUsage(AttributeTargets.All, AllowMultiple = true)] -sealed class MyAttribute : Attribute{} -"; - await VerifyCS.VerifyCodeFixAsync(source, new DiagnosticResult[] - { -VerifyCS.Diagnostic().WithSpan(5, 1, 16, 2).WithArguments("Composition, Mapping"), - }, fixedSource); - } - - [Fact] - public async Task LazySegtreeOperator_With_Alias_AggressiveInlining() - { - var source = @" -using AtCoder; -using MI = System.Runtime.CompilerServices.MethodImplAttribute; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [MI(256)] - public int Composition(int f, int g) => 0; - [MI(256)] - public long Mapping(int f, long x) => 0L; - [MI(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyAnalyzerAsync(source); - } - - - [Fact] - public async Task AnyDefinedType() - { - var source = @" -using AtCoder; -[IsOperator] -public interface IAny { - T Fun1(); - string Fun2(T v); -} -struct Def : IAny { - public T Fun1() => default; - public string Fun2(T v) - { - return v.ToString(); - } -} -"; - var fixedSource = @" -using AtCoder; -[IsOperator] -public interface IAny { - T Fun1(); - string Fun2(T v); -} -struct Def : IAny { - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public T Fun1() => default; - - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public string Fun2(T v) - { - return v.ToString(); - } -} -"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic().WithSpan(8, 1, 14, 2).WithArguments("Fun1, Fun2"), - fixedSource); - } - - [Fact] - public async Task UsingAlias() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -using MI = System.Runtime.CompilerServices.MethodImplAttribute; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - public int Composition(int f, int g) => 0; - public long Mapping(int f, long x) => 0L; - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -using MI = System.Runtime.CompilerServices.MethodImplAttribute; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - - [MI(MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [MI(MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [MI(MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic().WithSpan(5, 1, 12, 2).WithArguments("Composition, Mapping, Operate"), - fixedSource); - } - - [Fact] - public async Task MethodImpl256() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -using MI = System.Runtime.CompilerServices.MethodImplAttribute; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - public int Composition(int f, int g) => 0; - public long Mapping(int f, long x) => 0L; - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -using MI = System.Runtime.CompilerServices.MethodImplAttribute; -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - - [MI(256)] - public int Composition(int f, int g) => 0; - [MI(256)] - public long Mapping(int f, long x) => 0L; - [MI(256)] - public long Operate(long x, long y) => 0L; -} -"; - var test = new VerifyCS.Test - { - TestCode = source, - FixedCode = fixedSource, - TestState = - { - AnalyzerConfigFiles = - { - ("/.editorconfig", @" -is_global = true -build_property.AtCoderAnalyzer_UseMethodImplNumeric = true -"), - }, - }, - }; - test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic().WithSpan(5, 1, 12, 2).WithArguments("Composition, Mapping, Operate")); - await test.RunAsync(CancellationToken.None); - } - } -} diff --git a/Test/AtCoderAnalyzer.Test/AtCoderAnalyzer.Test.csproj b/Test/AtCoderAnalyzer.Test/AtCoderAnalyzer.Test.csproj deleted file mode 100644 index d6ecf7b6..00000000 --- a/Test/AtCoderAnalyzer.Test/AtCoderAnalyzer.Test.csproj +++ /dev/null @@ -1,41 +0,0 @@ - - - - netcoreapp3.1 - - $(NoWarn);CA1034;CS1998 - false - true - true - true - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - - diff --git a/Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs b/Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs deleted file mode 100644 index d4e9ed19..00000000 --- a/Test/AtCoderAnalyzer.Test/CreateOperatorCodeFixProviderTest.cs +++ /dev/null @@ -1,1205 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using Xunit; -using VerifyCS = AtCoderAnalyzer.Test.CSharpCodeFixVerifier< - AtCoderAnalyzer.CreateOperatorAnalyzer, - AtCoderAnalyzer.CreateOperatorCodeFixProvider>; - -namespace AtCoderAnalyzer.Test -{ - public class CreateOperatorCodeFixProviderTest - { - #region StaticModInt - [Fact] - public async Task StaticModInt_Using() - { - var source = @" -using AtCoder; -class Program -{ - StaticModInt notDefined; - StaticModInt defined; -} -"; - var fixedSource = @" -using AtCoder; -class Program -{ - StaticModInt notDefined; - StaticModInt defined; -} - -struct Op : IStaticMod -{ - public uint Mod => default; - - public bool IsPrime => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(5, 5, 5, 21).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task StaticModInt_Qualified_Using() - { - var source = @" -using AtCoder; -class Program -{ - AtCoder.StaticModInt notDefined; - AtCoder.StaticModInt defined; -} -"; - var fixedSource = @" -using AtCoder; -class Program -{ - AtCoder.StaticModInt notDefined; - AtCoder.StaticModInt defined; -} - -struct Op : IStaticMod -{ - public uint Mod => default; - - public bool IsPrime => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(5, 13, 5, 29).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task StaticModInt_Qualified() - { - var source = @" -class Program -{ - AtCoder.StaticModInt notDefined; - AtCoder.StaticModInt defined; -} -"; - var fixedSource = @" -class Program -{ - AtCoder.StaticModInt notDefined; - AtCoder.StaticModInt defined; -} - -struct Op : AtCoder.IStaticMod -{ - public uint Mod => default; - - public bool IsPrime => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(4, 13, 4, 29).WithArguments("Op"), - fixedSource); - } - #endregion StaticModInt - - #region Segtree - [Fact] - public async Task Segtree_Using() - { - var source = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} -"; - var fixedSource = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} - -struct OpSeg : ISegtreeOperator -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Operate(int x, int y) => default; - - public int Identity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(6, 5, 6, 24).WithArguments("OpSeg"), - fixedSource); - } - - [Fact] - public async Task Segtree_Qualified_Using() - { - var source = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - AtCoder.Segtree defined; - AtCoder.Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} -"; - var fixedSource = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - AtCoder.Segtree defined; - AtCoder.Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} - -struct OpSeg : ISegtreeOperator -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Operate(int x, int y) => default; - - public int Identity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(6, 13, 6, 32).WithArguments("OpSeg"), - fixedSource); - } - - [Fact] - public async Task Segtree_Qualified() - { - var source = @" -class Program -{ - AtCoder.Segtree defined; - AtCoder.Segtree notDefined; -} -struct MinOp : AtCoder.ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} -"; - var fixedSource = @" -class Program -{ - AtCoder.Segtree defined; - AtCoder.Segtree notDefined; -} -struct MinOp : AtCoder.ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} - -struct OpSeg : AtCoder.ISegtreeOperator -{ - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Operate(int x, int y) => default; - - public int Identity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(5, 13, 5, 32).WithArguments("OpSeg"), - fixedSource); - } - - [Fact] - public async Task Segtree_Using_With_System_Runtime_CompilerServices() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} - -struct OpSeg : ISegtreeOperator -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Operate(int x, int y) => default; - - public int Identity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(7, 5, 7, 24).WithArguments("OpSeg"), - fixedSource); - } - #endregion Segtree - - #region LazySegtree - [Fact] - public async Task LazySegtree_Using() - { - var source = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - LazySegtree defined; - LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - LazySegtree defined; - LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} - -struct OpSeg : ILazySegtreeOperator<(int v, int size), (int b, int c)> -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Operate((int v, int size) x, (int v, int size) y) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Mapping((int b, int c) f, (int v, int size) x) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int b, int c) Composition((int b, int c) nf, (int b, int c) cf) => default; - - public (int v, int size) Identity => default; - - public (int b, int c) FIdentity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(6, 5, 6, 58).WithArguments("OpSeg"), - fixedSource); - } - - [Fact] - public async Task LazySegtree_Qualified_Using() - { - var source = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - AtCoder.LazySegtree defined; - AtCoder.LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : AtCoder.ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - AtCoder.LazySegtree defined; - AtCoder.LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : AtCoder.ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} - -struct OpSeg : ILazySegtreeOperator<(int v, int size), (int b, int c)> -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Operate((int v, int size) x, (int v, int size) y) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Mapping((int b, int c) f, (int v, int size) x) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int b, int c) Composition((int b, int c) nf, (int b, int c) cf) => default; - - public (int v, int size) Identity => default; - - public (int b, int c) FIdentity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(6, 13, 6, 66).WithArguments("OpSeg"), - fixedSource); - } - - [Fact] - public async Task LazySegtree_Qualified() - { - var source = @"using System.Runtime.CompilerServices; - -class Program -{ - AtCoder.LazySegtree defined; - AtCoder.LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : AtCoder.ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @"using System.Runtime.CompilerServices; - -class Program -{ - AtCoder.LazySegtree defined; - AtCoder.LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : AtCoder.ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} - -struct OpSeg : AtCoder.ILazySegtreeOperator<(int v, int size), (int b, int c)> -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Operate((int v, int size) x, (int v, int size) y) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Mapping((int b, int c) f, (int v, int size) x) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int b, int c) Composition((int b, int c) nf, (int b, int c) cf) => default; - - public (int v, int size) Identity => default; - - public (int b, int c) FIdentity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(6, 13, 6, 66).WithArguments("OpSeg"), - fixedSource); - } - - [Fact] - public async Task LazySegtree_Using_With_System_Runtime_CompilerServices() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - LazySegtree defined; - LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - LazySegtree defined; - LazySegtree<(int v, int size), (int b, int c), OpSeg> notDefined; -} -struct Op : ILazySegtreeOperator -{ - public long Identity => 0L; - public int FIdentity => 0; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Composition(int f, int g) => 0; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Mapping(int f, long x) => 0L; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => 0L; -} - -struct OpSeg : ILazySegtreeOperator<(int v, int size), (int b, int c)> -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Operate((int v, int size) x, (int v, int size) y) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int v, int size) Mapping((int b, int c) f, (int v, int size) x) => default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (int b, int c) Composition((int b, int c) nf, (int b, int c) cf) => default; - - public (int v, int size) Identity => default; - - public (int b, int c) FIdentity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(7, 5, 7, 58).WithArguments("OpSeg"), - fixedSource); - } - #endregion LazySegtree - - #region Others - [Fact] - public async Task ICompare() - { - var source = @"using System; -using AtCoder; -public class Generic where TOp : System.Collections.Generic.IComparer { } -class Program -{ - Generic notDefined; - Type Type = typeof(Generic<,>); -} -"; - var fixedSource = @"using System; -using AtCoder; -public class Generic where TOp : System.Collections.Generic.IComparer { } -class Program -{ - Generic notDefined; - Type Type = typeof(Generic<,>); -} - -struct Op : System.Collections.Generic.IComparer -{ - [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] - public int Compare(short x, short y) => x.CompareTo(y); -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(6, 5, 6, 23).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task AnyDefinedType() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - void Init(ref T val, out bool success, params int[] nums); - T Prop1 { set; get; } - T Prop2 { get; set; } -} -public class Generic where TOp : IAny { } -class Program -{ - Generic<(int, long), Op> notDefined; - Generic<(int, long), Def<(int, long)>> defined; - System.Type Type = typeof(Generic<,>); -} -struct Def : IAny { - public void Init(ref T val, out bool success, params int[] nums) { success = true; } - public T Prop1 { set; get; } - public T Prop2 { set; get; } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - void Init(ref T val, out bool success, params int[] nums); - T Prop1 { set; get; } - T Prop2 { get; set; } -} -public class Generic where TOp : IAny { } -class Program -{ - Generic<(int, long), Op> notDefined; - Generic<(int, long), Def<(int, long)>> defined; - System.Type Type = typeof(Generic<,>); -} -struct Def : IAny { - public void Init(ref T val, out bool success, params int[] nums) { success = true; } - public T Prop1 { set; get; } - public T Prop2 { set; get; } -} - -struct Op : IAny<(int, long)> -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Init(ref (int, long) val, out bool success, params int[] nums) - { - } - - public (int, long) Prop1 { set; get; } - public (int, long) Prop2 { set; get; } -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(13, 5, 13, 29).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task AnyDefinedMethod() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - void Init(); - T Prop1 { set; get; } - T Prop2 { get; set; } -} -class Program -{ - static void M() where TOp : struct, IAny {} - static void Run() - { - M<(int n, long m), Op>(); - } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - void Init(); - T Prop1 { set; get; } - T Prop2 { get; set; } -} -class Program -{ - static void M() where TOp : struct, IAny {} - static void Run() - { - M<(int n, long m), Op>(); - } -} - -struct Op : IAny<(int n, long m)> -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Init() - { - } - - public (int n, long m) Prop1 { set; get; } - public (int n, long m) Prop2 { set; get; } -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(15, 9, 15, 31).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task Array() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; - -[IsOperator] -public interface IAny { - T Prop { get; } -} -class Program -{ - static void M() where TOp : struct, IAny {} - static void Run() - { - M(); - } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; - -[IsOperator] -public interface IAny { - T Prop { get; } -} -class Program -{ - static void M() where TOp : struct, IAny {} - static void Run() - { - M(); - } -} - -struct BigOp : IAny -{ - public System.Numerics.BigInteger[] Prop => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 47).WithArguments("BigOp"), - fixedSource); - } - - [Fact] - public async Task Generic() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; - -[IsOperator] -public interface IAny { - T Prop { get; } -} -class Program -{ - static void M() where TOp : struct, IAny {} - static void Run() - { - M, ModOp>(); - } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; - -[IsOperator] -public interface IAny { - T Prop { get; } -} -class Program -{ - static void M() where TOp : struct, IAny {} - static void Run() - { - M, ModOp>(); - } -} - -struct ModOp : IAny> -{ - public StaticModInt Prop => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(14, 9, 14, 46).WithArguments("ModOp"), - fixedSource); - } - - [Fact] - public async Task NumOperatorAndShiftOperator() - { - var source = @" -using AtCoder.Operators; -using System.Runtime.CompilerServices; -class Program -{ - void M() where T : INumOperator, IShiftOperator, INumOperator, ICastOperator { } - public void Main() - { - M(); - } -} -"; - var fixedSource = @" -using AtCoder.Operators; -using System.Runtime.CompilerServices; -class Program -{ - void M() where T : INumOperator, IShiftOperator, INumOperator, ICastOperator { } - public void Main() - { - M(); - } -} - -struct Op : ICastOperator, INumOperator, INumOperator, IShiftOperator -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public char Cast(short y) => (char)y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Add(float x, float y) => x + y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Subtract(float x, float y) => x - y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Divide(float x, float y) => x / y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Modulo(float x, float y) => x % y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Multiply(float x, float y) => x * y; - - public float MultiplyIdentity => 1; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Minus(float x) => -x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Increment(float x) => ++x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public float Decrement(float x) => --x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool GreaterThan(float x, float y) => x > y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool GreaterThanOrEqual(float x, float y) => x >= y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool LessThan(float x, float y) => x < y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool LessThanOrEqual(float x, float y) => x <= y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Compare(float x, float y) => x.CompareTo(y); - - public float MinValue => float.MinValue; - - public float MaxValue => float.MaxValue; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Add(int x, int y) => x + y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Subtract(int x, int y) => x - y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Divide(int x, int y) => x / y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Modulo(int x, int y) => x % y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Multiply(int x, int y) => x * y; - - public int MultiplyIdentity => 1; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Minus(int x) => -x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Increment(int x) => ++x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Decrement(int x) => --x; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool GreaterThan(int x, int y) => x > y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool GreaterThanOrEqual(int x, int y) => x >= y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool LessThan(int x, int y) => x < y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool LessThanOrEqual(int x, int y) => x <= y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int Compare(int x, int y) => x.CompareTo(y); - - public int MinValue => int.MinValue; - - public int MaxValue => int.MaxValue; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int LeftShift(int x, int y) => x << y; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public int RightShift(int x, int y) => x >> y; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(9, 9, 9, 14).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task UsingAlias() - { - var source = @"using AtCoder; -using System.Runtime.CompilerServices; -using ModInt = StaticModInt; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} -"; - var fixedSource = @"using AtCoder; -using System.Runtime.CompilerServices; -using ModInt = StaticModInt; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} - -struct OpSeg : ISegtreeOperator -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ModInt Operate(ModInt x, ModInt y) => default; - - public ModInt Identity => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(7, 5, 7, 27).WithArguments("OpSeg"), - fixedSource); - } - - [Fact] - public async Task MethodImplAlias() - { - var source = @"using AtCoder; -using System.Runtime.CompilerServices; -using MI = System.Runtime.CompilerServices.MethodImplAttribute; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} -"; - var fixedSource = @"using AtCoder; -using System.Runtime.CompilerServices; -using MI = System.Runtime.CompilerServices.MethodImplAttribute; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} - -struct OpSeg : ISegtreeOperator -{ - [MI(MethodImplOptions.AggressiveInlining)] - public long Operate(long x, long y) => default; - - public long Identity => default; -}"; - - var test = new VerifyCS.Test - { - TestCode = source, - FixedCode = fixedSource, - }; - test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic("AC0008").WithSpan(7, 5, 7, 25).WithArguments("OpSeg")); - await test.RunAsync(CancellationToken.None); - } - - [Fact] - public async Task MethodImpl256() - { - var source = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} -"; - var fixedSource = @"using AtCoder; -using System.Runtime.CompilerServices; -class Program -{ - Segtree defined; - Segtree notDefined; -} -struct MinOp : ISegtreeOperator -{ - public int Identity => 0; - - public int Operate(int x, int y) - { - return System.Math.Min(x, y); - } -} - -struct OpSeg : ISegtreeOperator -{ - [MethodImpl(256)] - public long Operate(long x, long y) => default; - - public long Identity => default; -}"; - - var test = new VerifyCS.Test - { - TestCode = source, - FixedCode = fixedSource, - TestState = - { - AnalyzerConfigFiles = - { - ("/.editorconfig", @" -is_global = true -build_property.AtCoderAnalyzer_UseMethodImplNumeric = true -") - }, - }, - }; - test.ExpectedDiagnostics.Add(VerifyCS.Diagnostic("AC0008").WithSpan(6, 5, 6, 25).WithArguments("OpSeg")); - await test.RunAsync(CancellationToken.None); - } - - - [Fact] - public async Task Virtual() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - void Run() { } -} -class Program -{ - static void M() where TOp : IAny {} - static void Run() - { - M(); - } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - void Run() { } -} -class Program -{ - static void M() where TOp : IAny {} - static void Run() - { - M(); - } -} - -struct Op : IAny -{ -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(13, 9, 13, 14).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task StaticAbstract() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - static abstract bool Init(); - static abstract T Prop1 { set; get; } - static abstract (T, T) Prop2 { get; } -} -class Program -{ - static void M() where TOp : IAny {} - static void Run() - { - M<(int n, long m), Op>(); - } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - static abstract bool Init(); - static abstract T Prop1 { set; get; } - static abstract (T, T) Prop2 { get; } -} -class Program -{ - static void M() where TOp : IAny {} - static void Run() - { - M<(int n, long m), Op>(); - } -} - -struct Op : IAny<(int n, long m)> -{ - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool Init() => default; - - public static (int n, long m) Prop1 { set; get; } - - public static ((int n, long m), (int n, long m)) Prop2 => default; -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(15, 9, 15, 31).WithArguments("Op"), - fixedSource); - } - - [Fact] - public async Task StaticVirtual() - { - var source = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - static virtual bool Init(); - static virtual T Prop1 { set; get; } - static virtual (T, T) Prop2 { get; } -} -class Program -{ - static void M() where TOp : IAny {} - static void Run() - { - M<(int n, long m), Op>(); - } -} -"; - var fixedSource = @" -using AtCoder; -using System.Runtime.CompilerServices; -[IsOperator] -public interface IAny { - static virtual bool Init(); - static virtual T Prop1 { set; get; } - static virtual (T, T) Prop2 { get; } -} -class Program -{ - static void M() where TOp : IAny {} - static void Run() - { - M<(int n, long m), Op>(); - } -} - -struct Op : IAny<(int n, long m)> -{ -}"; - await VerifyCS.VerifyCodeFixAsync(source, - VerifyCS.Diagnostic("AC0008").WithSpan(15, 9, 15, 31).WithArguments("Op"), - fixedSource); - } - #endregion Others - } -} diff --git a/Test/AtCoderAnalyzer.Test/SymbolHelperTest.cs b/Test/AtCoderAnalyzer.Test/SymbolHelperTest.cs deleted file mode 100644 index d1b3ea26..00000000 --- a/Test/AtCoderAnalyzer.Test/SymbolHelperTest.cs +++ /dev/null @@ -1,175 +0,0 @@ -using System.Collections.Immutable; -using System.Linq; -using System.Threading.Tasks; -using AtCoderAnalyzer.Helpers; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Xunit; - -namespace AtCoderAnalyzer.Test -{ - public class SymbolHelperTest - { - private readonly CSharpSyntaxTree[] trees = new CSharpSyntaxTree[1]; - private readonly CSharpCompilation compilation; - public SymbolHelperTest() - { - trees[0] = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(@"namespace TestAssembly.TA { - public class Foo { - static ConstraintGenerics> field1; - static IGenerics field2; - static IGenerics> field3; - } - public struct Bar { } - public interface IGenerics { } - public class ConstraintGenerics where Op : IGenerics { } - public class NestGenerics where Op : IGenerics> { } -} -"); - compilation = CSharpCompilation.Create("Asm", trees); - } - - [Fact] - public async Task TypeEqual() - { - var root = await trees[0].GetRootAsync(); - var semanticModel = compilation.GetSemanticModel(trees[0], false); - var nodes = root.DescendantNodes(); - - var constructedConstraintGenericsSymbol - = nodes - .OfType() - .Single(sy => sy.Identifier.Text == "Foo") - .DescendantNodes() - .OfType() - .Where(sy => sy.Declaration.Variables.Single().Identifier.Text == "field1") - .Select(sy => semanticModel.GetSymbolInfo(sy.Declaration.Type).Symbol) - .OfType() - .Single(); - - Assert.Equal( - constructedConstraintGenericsSymbol.TypeArguments[0], - ((INamedTypeSymbol)constructedConstraintGenericsSymbol.TypeArguments[2]).TypeArguments[0], - SymbolEqualityComparer.Default); - Assert.Equal( - constructedConstraintGenericsSymbol.TypeArguments[1], - ((INamedTypeSymbol)constructedConstraintGenericsSymbol.TypeArguments[2]).TypeArguments[1], - SymbolEqualityComparer.Default); - } - - [Fact] - public async Task GenericReplace() - { - var root = await trees[0].GetRootAsync(); - var semanticModel = compilation.GetSemanticModel(trees[0], false); - var nodes = root.DescendantNodes(); - - var declaredTypeSymbols - = nodes - .OfType() - .Select(sy => semanticModel.GetDeclaredSymbol(sy)) - .OfType() - .ToArray(); - - var constraintGenericsSymbol = declaredTypeSymbols - .Single(s => s.ToString() == "TestAssembly.TA.ConstraintGenerics"); - - var constraint = (INamedTypeSymbol)constraintGenericsSymbol.TypeParameters[2].ConstraintTypes.Single(); - - var builder = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); - builder.Add(constraintGenericsSymbol.TypeParameters[0], - declaredTypeSymbols.Single(s => s.ToString() == "TestAssembly.TA.Foo")); - builder.Add(constraintGenericsSymbol.TypeParameters[1], - declaredTypeSymbols.Single(s => s.ToString() == "TestAssembly.TA.Bar")); - - var got = SymbolHelpers.ReplaceGenericType(constraint, builder.ToImmutable()); - - Assert.Equal(nodes - .OfType() - .Single(sy => sy.Identifier.Text == "Foo") - .DescendantNodes() - .OfType() - .Where(sy => sy.Declaration.Variables.Single().Identifier.Text == "field2") - .Select(sy => semanticModel.GetSymbolInfo(sy.Declaration.Type).Symbol) - .OfType() - .Single() - , got, SymbolEqualityComparer.Default); - } - - [Fact] - public async Task NestGenericReplace() - { - var root = await trees[0].GetRootAsync(); - var semanticModel = compilation.GetSemanticModel(trees[0], false); - var nodes = root.DescendantNodes(); - - var declaredTypeSymbols - = nodes - .OfType() - .Select(sy => semanticModel.GetDeclaredSymbol(sy)) - .OfType() - .ToArray(); - - var constraintGenericsSymbol = declaredTypeSymbols - .Single(s => s.ToString() == "TestAssembly.TA.NestGenerics"); - - var constraint = (INamedTypeSymbol)constraintGenericsSymbol.TypeParameters[2].ConstraintTypes.Single(); - - var builder = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); - builder.Add(constraintGenericsSymbol.TypeParameters[0], - declaredTypeSymbols.Single(s => s.ToString() == "TestAssembly.TA.Foo")); - builder.Add(constraintGenericsSymbol.TypeParameters[1], - declaredTypeSymbols.Single(s => s.ToString() == "TestAssembly.TA.Bar")); - - var got = SymbolHelpers.ReplaceGenericType(constraint, builder.ToImmutable()); - - Assert.Equal(nodes - .OfType() - .Single(sy => sy.Identifier.Text == "Foo") - .DescendantNodes() - .OfType() - .Where(sy => sy.Declaration.Variables.Single().Identifier.Text == "field3") - .Select(sy => semanticModel.GetSymbolInfo(sy.Declaration.Type).Symbol) - .OfType() - .Single() - , got, SymbolEqualityComparer.Default); - } - - [Fact] - public async Task ToTypeSyntax() - { - var root = await trees[0].GetRootAsync(); - var semanticModel = compilation.GetSemanticModel(trees[0], false); - var nodes = root.DescendantNodes(); - var declaredTypeSymbols - = nodes - .OfType() - .Select(sy => semanticModel.GetDeclaredSymbol(sy)) - .OfType() - .ToArray(); - - var usings = ImmutableHashSet.Create("TestAssembly.TA"); - var ns = SyntaxFactory.QualifiedName(SyntaxFactory.IdentifierName("TestAssembly"), SyntaxFactory.IdentifierName("TA")); - - var tests = new (TypeSyntax got, TypeSyntax expected)[] - { - (declaredTypeSymbols[0].ToTypeSyntax(semanticModel, 0), - SyntaxFactory.QualifiedName(ns, (SimpleNameSyntax)SyntaxFactory.ParseName("Foo"))), - - (declaredTypeSymbols[1].ToTypeSyntax(semanticModel, 0), - SyntaxFactory.QualifiedName(ns, (SimpleNameSyntax)SyntaxFactory.ParseName("Bar"))), - - (declaredTypeSymbols[4].ToTypeSyntax(semanticModel, 0), - SyntaxFactory.QualifiedName(ns, - SyntaxFactory.GenericName("NestGenerics").AddTypeArgumentListArguments( - SyntaxFactory.ParseName("R"),SyntaxFactory.ParseName("S"),SyntaxFactory.ParseName("Op")))), - }; - - foreach (var (got, expected) in tests) - { - Assert.True(got.IsEquivalentTo(expected, true), $"{expected}"); - } - } - } -} diff --git a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpAnalyzerVerifier`1+Test.cs b/Test/AtCoderAnalyzer.Test/Verifiers/CSharpAnalyzerVerifier`1+Test.cs deleted file mode 100644 index 9ab24cc3..00000000 --- a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpAnalyzerVerifier`1+Test.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Microsoft.CodeAnalysis.CSharp.Testing; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Testing.Verifiers; - -namespace AtCoderAnalyzer.Test -{ - public static partial class CSharpAnalyzerVerifier - where TAnalyzer : DiagnosticAnalyzer, new() - { - public class Test : CSharpAnalyzerTest - { - public Test() - { - ReferenceAssemblies = ReferenceAssemblies.WithPackages(ReferencesHelper.Packages); - SolutionTransforms.Add((solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - compilationOptions = compilationOptions.WithSpecificDiagnosticOptions( - compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); - solution = solution.WithProjectCompilationOptions(projectId, compilationOptions); - - return solution; - }); - } - } - } -} diff --git a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpAnalyzerVerifier`1.cs b/Test/AtCoderAnalyzer.Test/Verifiers/CSharpAnalyzerVerifier`1.cs deleted file mode 100644 index e10afad9..00000000 --- a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpAnalyzerVerifier`1.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Testing; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Testing; -using Microsoft.CodeAnalysis.Testing.Verifiers; - -namespace AtCoderAnalyzer.Test -{ - public static partial class CSharpAnalyzerVerifier - where TAnalyzer : DiagnosticAnalyzer, new() - { - /// - public static DiagnosticResult Diagnostic() - => CSharpAnalyzerVerifier.Diagnostic(); - - /// - public static DiagnosticResult Diagnostic(string diagnosticId) - => CSharpAnalyzerVerifier.Diagnostic(diagnosticId); - - /// - public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) - => CSharpAnalyzerVerifier.Diagnostic(descriptor); - - /// - public static async Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) - { - var test = new Test - { - TestCode = source, - }; - - test.ExpectedDiagnostics.AddRange(expected); - await test.RunAsync(CancellationToken.None); - } - } -} diff --git a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpCodeFixVerifier`2+Test.cs b/Test/AtCoderAnalyzer.Test/Verifiers/CSharpCodeFixVerifier`2+Test.cs deleted file mode 100644 index c70f9354..00000000 --- a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpCodeFixVerifier`2+Test.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp.Testing; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Testing.Verifiers; - -namespace AtCoderAnalyzer.Test -{ - public static partial class CSharpCodeFixVerifier - where TAnalyzer : DiagnosticAnalyzer, new() - where TCodeFix : CodeFixProvider, new() - { - public class Test : CSharpCodeFixTest - { - public Test() - { - CompilerDiagnostics = Microsoft.CodeAnalysis.Testing.CompilerDiagnostics.None; - ReferenceAssemblies = ReferenceAssemblies.WithPackages(ReferencesHelper.Packages); - SolutionTransforms.Add((solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - compilationOptions = compilationOptions.WithSpecificDiagnosticOptions( - compilationOptions.SpecificDiagnosticOptions.SetItems(CSharpVerifierHelper.NullableWarnings)); - solution = solution.WithProjectCompilationOptions(projectId, compilationOptions); - - return solution; - }); - } - } - } -} diff --git a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpCodeFixVerifier`2.cs b/Test/AtCoderAnalyzer.Test/Verifiers/CSharpCodeFixVerifier`2.cs deleted file mode 100644 index 7b877c9c..00000000 --- a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpCodeFixVerifier`2.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CodeFixes; -using Microsoft.CodeAnalysis.CSharp.Testing; -using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Testing; -using Microsoft.CodeAnalysis.Testing.Verifiers; - -namespace AtCoderAnalyzer.Test -{ - public static partial class CSharpCodeFixVerifier - where TAnalyzer : DiagnosticAnalyzer, new() - where TCodeFix : CodeFixProvider, new() - { - /// - public static DiagnosticResult Diagnostic() - => CSharpCodeFixVerifier.Diagnostic(); - - /// - public static DiagnosticResult Diagnostic(string diagnosticId) - => CSharpCodeFixVerifier.Diagnostic(diagnosticId); - - /// - public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor) - => CSharpCodeFixVerifier.Diagnostic(descriptor); - - /// - public static async Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) - { - var test = new Test - { - TestCode = source, - }; - - test.ExpectedDiagnostics.AddRange(expected); - await test.RunAsync(CancellationToken.None); - } - - /// - public static async Task VerifyCodeFixAsync(string source, string fixedSource) - => await VerifyCodeFixAsync(source, DiagnosticResult.EmptyDiagnosticResults, fixedSource); - - /// - public static async Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource) - => await VerifyCodeFixAsync(source, new[] { expected }, fixedSource); - - /// - public static async Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource) - { - var test = new Test - { - TestCode = source, - FixedCode = fixedSource, - }; - - test.ExpectedDiagnostics.AddRange(expected); - await test.RunAsync(CancellationToken.None); - } - } -} diff --git a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpVerifierHelper.cs b/Test/AtCoderAnalyzer.Test/Verifiers/CSharpVerifierHelper.cs deleted file mode 100644 index 8feef764..00000000 --- a/Test/AtCoderAnalyzer.Test/Verifiers/CSharpVerifierHelper.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Collections.Immutable; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; - -namespace AtCoderAnalyzer.Test -{ - internal static class CSharpVerifierHelper - { - /// - /// By default, the compiler reports diagnostics for nullable reference types at - /// , and the analyzer test framework defaults to only validating - /// diagnostics at . This map contains all compiler diagnostic IDs - /// related to nullability mapped to , which is then used to enable all - /// of these warnings for default validation during analyzer and code fix tests. - /// - internal static ImmutableDictionary NullableWarnings { get; } = GetNullableWarningsFromCompiler(); - - private static ImmutableDictionary GetNullableWarningsFromCompiler() - { - string[] args = { "/warnaserror:nullable" }; - var commandLineArguments = CSharpCommandLineParser.Default.Parse(args, baseDirectory: Environment.CurrentDirectory, sdkDirectory: Environment.CurrentDirectory); - var nullableWarnings = commandLineArguments.CompilationOptions.SpecificDiagnosticOptions; - - // Workaround for https://github.com/dotnet/roslyn/issues/41610 - nullableWarnings = nullableWarnings - .SetItem("CS8632", ReportDiagnostic.Error) - .SetItem("CS8669", ReportDiagnostic.Error); - - return nullableWarnings; - } - } -} diff --git a/Test/AtCoderAnalyzer.Test/Verifiers/ReferencesHelper.cs b/Test/AtCoderAnalyzer.Test/Verifiers/ReferencesHelper.cs deleted file mode 100644 index ec94fec9..00000000 --- a/Test/AtCoderAnalyzer.Test/Verifiers/ReferencesHelper.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections.Immutable; -using Microsoft.CodeAnalysis.Testing; - -namespace AtCoderAnalyzer.Test -{ - internal static class ReferencesHelper - { - internal static ImmutableArray Packages - = ImmutableArray.Create(new PackageIdentity("ac-library-csharp", "1.8.0-alpha2")); - } -}