Код: Выделить всё
class A
{
public static int KeySize = 32;
public static int GetKeySize()
{
return KeySize;
}
}
class test
{
.
.
int keySize = A.getKeySize();
SomeCryptoAPIInovcationExpression(keySize);
.
.
.
}
Как мне достичь этот? Я попробовал ValueContentanalysis, но не смог получить ценности. Может, я сделал что-то не так, вы можете помочь?using System.Collections.Immutable;
using Analyzer.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.FlowAnalysis;
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow;
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.PointsToAnalysis;
using Microsoft.CodeAnalysis.FlowAnalysis.DataFlow.ValueContentAnalysis;
using Microsoft.CodeAnalysis.Operations;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class MyCustomAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = "MyCustomAnalyzer";
private static readonly LocalizableString Title = "Title of the analyzer";
private static readonly LocalizableString MessageFormat = "Message format of the analyzer";
private static readonly LocalizableString Description = "Description of the analyzer";
private const string Category = "Naming";
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description);
public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule);
public DiagnosticDescriptor AlwaysTrueFalseOrNullRule { get; private set; }
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics );
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(AnalyzeInvocations,SyntaxKind.InvocationExpression);
}
public static void AnalyzeInvocations(SyntaxNodeAnalysisContext context)
{
var containingSymbol = context.ContainingSymbol;
var compilation = context.Compilation;
var semanticModel = context.SemanticModel;
var wellKnownTypeProvider = WellKnownTypeProvider.GetOrCreate(compilation);
var methodNode = context.Node.Ancestors().OfType().First();
var cfg = ControlFlowGraph.Create(methodNode, semanticModel);
var valueContent = ValueContentAnalysis.TryGetOrComputeResult
(
cfg,
containingSymbol,
wellKnownTypeProvider,
new AnalyzerOptions(new()),
Rule,
PointsToAnalysisKind.Complete,
InterproceduralAnalysisKind.ContextSensitive,
default
);
foreach (var child in context.Node.DescendantNodes().OfType())
{
var data = valueContent[OperationKind.Invocation, child]; // breakpoint to see output
}
}
}
< /code>
Любая небольшая помощь или предложение высоко ценится. Спасибо.
Моя главная цель - выполнить Dataflowanaysis в C#, и я хотел посмотреть, полезны ли Roslyn или нет.
Подробнее здесь: https://stackoverflow.com/questions/794 ... rp-library
Мобильная версия