Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add register method for analyzing embedded languages #63533

Open
JamesNK opened this issue Aug 23, 2022 · 7 comments
Open

Add register method for analyzing embedded languages #63533

JamesNK opened this issue Aug 23, 2022 · 7 comments
Assignees
Labels
Area-Analyzers Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Milestone

Comments

@JamesNK
Copy link
Member

JamesNK commented Aug 23, 2022

Background and Motivation

There should be support for analyzing an embedded language with DiagnosticAnalyzer. For example, analyzers that inspect:

  • Regex strings
  • JSON strings
  • Date/time format strings
  • ASP.NET Core route strings

There are some analyzers for regex strings and JSON strings today that implement their own solution. They are currently editor only analyzers, and don't run during command line build (I'm not sure if that was intentional or a technical limitation)

ASP.NET Core route strings has its own solution that uses a lot of copy and paste (StringFormatAttribute detection and VirtualChar).

Providing a built-in, official solution would make it must easier for future embedded language analyzers to be written and to improve existing analyzers.

Proposed API

namespace Microsoft.CodeAnalysis.Diagnostics
{
     public class AnalysisContext
     {
+        public void RegisterEmbeddedLanguageAction(Action<EmbeddedLanguageAnalysisContext> action, ImmutableArray<string> stringFormats);
     }

+   public struct EmbeddedLanguageAnalysisContext
+   {
+       public CancellationToken CancellationToken { get; }
+       public Compilation Compilation { get; }
+       public ISymbol ContainingSymbol { get; }
+       public SemanticModel SemanticModel { get; }
+       public VirtualCharSequence Text { get; }
+   }

+   // Plus VirtualCharSequence & VirtualChar

Usage Examples

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class RoutePatternAnalyzer : DiagnosticAnalyzer
{
    public override void Initialize(AnalysisContext context)
    {
        context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
        context.EnableConcurrentExecution();

        context.RegisterEmbeddedLanguageAction(Analyze, ImmutableArray.Create("Route"));
    }

    public void Analyze(EmbeddedLanguageAnalysisContext context)
    {
        var tree = RoutePatternParser.TryParse(context.Text);
        if (tree == null)
        {
            continue;
        }

        foreach (var diag in tree.Diagnostics)
        {
            context.ReportDiagnostic(Diagnostic.Create(
                DiagnosticDescriptors.RoutePatternIssue,
                Location.Create(context.SemanticModel.SyntaxTree, diag.Span),
                DiagnosticDescriptors.RoutePatternIssue.DefaultSeverity,
                additionalLocations: null,
                properties: null,
                diag.Message));
        }
    }
}

Alternative Designs

Risks

@JamesNK JamesNK added Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request labels Aug 23, 2022
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Analyzers untriaged Issues and PRs which have not yet been triaged by a lead labels Aug 23, 2022
@JamesNK
Copy link
Member Author

JamesNK commented Aug 23, 2022

cc @CyrusNajmabadi

@ryzngard ryzngard removed the untriaged Issues and PRs which have not yet been triaged by a lead label Aug 23, 2022
@ryzngard ryzngard added this to the Backlog milestone Aug 23, 2022
@CyrusNajmabadi CyrusNajmabadi removed their assignment Sep 7, 2022
@CyrusNajmabadi
Copy link
Member

This is really up to compiler/analyzer to decide if htey want to do something here. I think it would be nice for there to be an official compiler API for understanding the contents of strings. However, it's not something i'm really interested in championing as my main use case is the IDE, and we have a story there. The desire to bring this stuff to things like the command-line experience is decidedly outside of that for me :)

@jaredpar for thoughts.

@jaredpar
Copy link
Member

jaredpar commented Sep 7, 2022

How does the embedded language detection work today? Essentially how do we map a given string literal and say "ah this is for embedded language X"? Or do we just throw every string at every embedded language analyzer and let them sort it out?

@CyrusNajmabadi
Copy link
Member

We use the [StringSyntaxAttribute("lang-name")] attribute to make the determination. We also special case legacy Regex/Json apis (which predate that attribute), but that's not exposed/allowed for any other languages.

@jaredpar
Copy link
Member

jaredpar commented Sep 7, 2022

Does that attribute get placed on the API parameter? So basically developer defines

public void Method([StringsSyntaxAttribute("awesome-lang")] p)

Then calls like Method("blah") git passed to the analyzer associated with "awesome-lang"?

@CyrusNajmabadi
Copy link
Member

@jaredpar Correct :) Also things like this.AnnotatedProperty = "blah" or [SomeAttr("blah")], etc. etc.

@mavasani mavasani assigned jaredpar and unassigned mavasani Sep 19, 2022
@mavasani
Copy link
Member

@jaredpar Assigned to you for triage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Analyzers Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Projects
None yet
Development

No branches or pull requests

5 participants