2017-06-14 1 views
0

Je suis un débutant en roslyn et en studio visuel essayant d'écrire une règle pour l'analyse sonarQube de C# en utilisant roslyn.Détecter un appel de méthode avec l'analyseur Roslyn

La règle doit détecter la méthode Sleep() et interdire son utilisation. Je me suis inspiré par this tutorial

et this example

écrire ma règle, mais quand je l'essayer sur une application de la console, il ne détecte pas la fonction. Que dois-je corriger pour le faire fonctionner? Voici le code de l'analyseur:

using System; 
using System.Collections.Generic; 
using System.Collections.Immutable; 
using System.Linq; 
using System.Threading; 
using Microsoft.CodeAnalysis; 
using Microsoft.CodeAnalysis.CSharp; 
using Microsoft.CodeAnalysis.CSharp.Syntax; 
using Microsoft.CodeAnalysis.Diagnostics; 
using System.Diagnostics; 

namespace AnalyzerSleepMethodCs 
{ 
[DiagnosticAnalyzer(LanguageNames.CSharp)] 
public class AnalyzerSleepMethodCsAnalyzer : DiagnosticAnalyzer 
{ 
    public const string DiagnosticId = "AnalyzerSleepMethodCs"; 
    private const string Title = "Sleep method should not be used"; 
    private const string MessageFormat = "Delete this forbidden function 
sleep"; 
    private const string Description = "Do not use sleep function"; 
    private const string Category = "Usage"; 

    private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description); 

    public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get { return ImmutableArray.Create(Rule); } } 

    public override void Initialize(AnalysisContext context) 
    { 
     // TODO: Consider registering other actions that act on syntax instead of or in addition to symbols 
     // See https://github.com/dotnet/roslyn/blob/master/docs/analyzers/Analyzer%20Actions%20Semantics.md for more information 
context.RegisterSyntaxNodeAction(AnalyzeInvocationExpression, SyntaxKind.InvocationExpression); 
    } 

    private static void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context) 
    { 

     var invocation = (InvocationExpressionSyntax)context.Node; 
     var memberAccess = invocation.Expression as MemberAccessExpressionSyntax; 
     if (!memberAccess.IsKind(SyntaxKind.SimpleMemberAccessExpression)) 
     { 
      return; 
     } 

     Debug.Assert(memberAccess != null); 
     var ident = memberAccess.Name.Identifier; 
     if (ident.ToString().ToLower() != "sleep") 
     { 
      return; 
     } 

     var subject = memberAccess.Expression; 
     ITypeSymbol subjectType = context.SemanticModel.GetTypeInfo(subject).Type; 
     if(subjectType.ToString().ToLower() != "thread") 
     { 
      return; 
     } 

     context.ReportDiagnostic(Diagnostic.Create(Rule, context.Node.GetLocation())); 
    } 
} 
} 

Et voici l'exemple que je l'habitude de le tester sur un projet de console:

static void Main(string[] args) 
    { 
     Thread t = new Thread(new ThreadStart(ThreadWorker)); 
     t.Start(); 
     t.Join(); 
    } 
    private static void ThreadWorker() 
    { 
     Console.WriteLine("Prior to sleep"); 
     Thread.Sleep(100); 
     Console.WriteLine("After sleep sleep"); 
    } 

Merci à l'avance.

Répondre

0

Nous avons quelques règles de vérification pour un appel de méthode particulier, la lecture sur eux pourrait vous aider à enregistrer votre problème. Voir en particulier this class.

+0

Merci pour votre réponse, mais je l'ai essayé et il semble qu'il utilise trop de fichiers spécifiques à sonarqube (DoNotCallMethodsBase ....) Y at-il un échantillon pour les clients? – SabrinaS