Search Unity

Question Roslyn Analyzer

Discussion in 'Testing & Automation' started by hugeandy, Aug 4, 2021.

  1. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    131
    Hi all,

    I'm trying to add some custom roslyn analyzers to my project. I have tried the following two methods, however I cannot seem to get it to work:
    1. Added the analyzer script directly into my project. I gave the script the asset label "RoslynAnalyzer".
    2. Created a standalone C# project, added the analyzer in there, built and imported the DLL into the Unity project. I gave the DLL the asset label "RoslynAnalyzer".

    In both cases I have reimported scripts after adding the analyzer to try to get it to run, and have also restarted Unity. I don't see any of the Debug.Logs I added either. I am on 2020.3.13f1.

    I have included the source of my analyzer below. The logic of it is just placeholder. I am hoping to get to a point where I can debug the analyzer to see whats what but I haven't been able to get to the point where it runs yet.

    Anyone got any ideas why this isn't working? The documentation around Roslyn analyzers seems extremely limited and I'm just shooting in the dark at the moment.

    Cheers,
    Andy

    Code (CSharp):
    1. using System.Collections.Immutable;
    2. using Microsoft.CodeAnalysis;
    3. using Microsoft.CodeAnalysis.Diagnostics;
    4. using Microsoft.CodeAnalysis.Semantics;
    5. using UnityEngine;
    6.  
    7. namespace analyzers {
    8.     [DiagnosticAnalyzer(LanguageNames.CSharp)]
    9.     public class MathfSmoothDampNoDt : DiagnosticAnalyzer {
    10.         public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
    11.        
    12.         public const string DiagnosticId = "HUGE0001";
    13.  
    14.         private const string Title = "Don't use Mathf.SmoothDamp";
    15.         private const string MessageFormat = "<Message Format>";
    16.         private const string Description = "<Description>";
    17.         private const string Category = "Usage";
    18.         private const DiagnosticSeverity Severity = DiagnosticSeverity.Error;
    19.        
    20.         public static readonly DiagnosticDescriptor Rule =
    21.             new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, Severity, isEnabledByDefault: true, description: Description);
    22.        
    23.         public override void Initialize(AnalysisContext context) {
    24.             Debug.Log("REGISTER");
    25.             context.EnableConcurrentExecution();
    26.             context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
    27.             context.RegisterOperationAction(AnalyzerInvocation, OperationKind.InvocationExpression);
    28.         }
    29.  
    30.         private void AnalyzerInvocation(OperationAnalysisContext context) {
    31.             Debug.Log("ANALYZE");
    32.             var invocation = (IInvocationExpression)context.Operation;
    33.             var type = invocation.Type;
    34.            
    35.             if(type.Name != "Mathf") return;
    36.        
    37.             var method = invocation.TargetMethod;
    38.             if (method.Name != "SmoothDamp") return;
    39.        
    40.             context.ReportDiagnostic(Diagnostic.Create(Rule, invocation.Syntax.GetLocation()));
    41.         }
    42.        
    43.     }
    44. }
     
  2. hugeandy

    hugeandy

    Joined:
    Nov 2, 2016
    Posts:
    131
    I have got it working with the DLL. I couldn't get it working with a script in the same project. This is reinforced with this setting in Player Settings.
    upload_2021-8-4_19-51-59.png
     
    Seromu and FICHEKK like this.