-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from naminodarie/feature/analyzer
v1.0.2
- Loading branch information
Showing
39 changed files
with
3,041 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<Authors>naminodarie</Authors> | ||
<RepositoryType>git</RepositoryType> | ||
<PackageProjectUrl>https://github.com/naminodarie/ac-library-csharp</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/naminodarie/ac-library-csharp</RepositoryUrl> | ||
|
||
<Version>1.0.2</Version> | ||
<AssemblyVersion>1.0.2.101</AssemblyVersion> | ||
<RepositoryCommit Condition="'$(GIT_COMMIT)' != ''">$(GIT_COMMIT)</RepositoryCommit> | ||
|
||
<SignAssembly>True</SignAssembly> | ||
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)key.snk</AssemblyOriginatorKeyFile> | ||
|
||
<PackageOutputPath>$(MSBuildThisFileDirectory)bin\Packages\$(Configuration)\</PackageOutputPath> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" /> | ||
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" /> | ||
</packageSources> | ||
<disabledPackageSources> | ||
<clear /> | ||
</disabledPackageSources> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
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<DiagnosticDescriptor> SupportedDiagnostics | ||
=> ImmutableArray.Create( | ||
DiagnosticDescriptors.AC0001_MultiplyOverflowInt32, | ||
DiagnosticDescriptors.AC0002_LeftShiftOverflowInt32); | ||
|
||
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; | ||
|
||
DiagnosticDescriptor descriptor = node.Kind() switch | ||
{ | ||
SyntaxKind.MultiplyExpression => DiagnosticDescriptors.AC0001_MultiplyOverflowInt32, | ||
SyntaxKind.LeftShiftExpression => DiagnosticDescriptors.AC0002_LeftShiftOverflowInt32, | ||
_ => throw new InvalidOperationException(), | ||
}; | ||
|
||
for (; node is not null; node = GetParent(node)) | ||
{ | ||
if (semanticModel.GetTypeInfo(node, cancellationToken: context.CancellationToken) | ||
.ConvertedType.SpecialType == SpecialType.System_Int64) | ||
{ | ||
var diagnostic = Diagnostic.Create( | ||
descriptor, | ||
context.Node.GetLocation(), | ||
context.Node.ToString()); | ||
|
||
context.ReportDiagnostic(diagnostic); | ||
return; | ||
} | ||
} | ||
|
||
static SyntaxNode GetParent(SyntaxNode node) | ||
{ | ||
var parent = node.Parent; | ||
if (parent is BinaryExpressionSyntax or ParenthesizedExpressionSyntax) | ||
return parent; | ||
return null; | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
Source/AtCoderAnalyzer/AC0001_AC0002_IntToLongCodeFixProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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<string> FixableDiagnosticIds | ||
=> ImmutableArray.Create( | ||
DiagnosticDescriptors.AC0001_MultiplyOverflowInt32.Id, | ||
DiagnosticDescriptors.AC0002_LeftShiftOverflowInt32.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<Document> 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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.