2017-06-14 15 views
0

私はroslynを使用してC#のsonarQube解析のルールを作成しようとしているroslynとvisual studioの初心者です。Roslynアナライザでメソッド呼び出しを検出

ルールはSleep()メソッドを検出し、その使用を禁止する必要があります。私は自分のルールを記述するためにthis tutorial

this exampleに触発されましたが、私は、コンソールアプリケーションでそれをしようとすると、それが機能を検出しません。 問題を解決するにはどうすればよいですか?

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())); 
    } 
} 
} 

そして、これは私がコンソールプロジェクトでそれをテストするために使用される例である:ここでは、分析器のコードがあり、事前に

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"); 
    } 

感謝。

答えて

0

特定のメソッド呼び出しをチェックするためのルールがいくつかあります。これらのメソッドコールを読むと、問題を解決するのに役立ちます。特にthis classを参照してください。

+0

いただきありがとうございますが、私は試してみましたが、sonarqube(DoNotCallMethodsBase ....)にあまりにも多くのファイルを使用しているようです。 サンプルはありますか? – SabrinaS

関連する問題