Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
6e03dca
Add the initial roslyn files
CyrusNajmabadi Jun 17, 2022
539b248
Merge remote-tracking branch 'upstream/main' into polyfill
CyrusNajmabadi Jun 17, 2022
e8f0a8e
IN progress
CyrusNajmabadi Jun 17, 2022
1c0fbf2
Builds
CyrusNajmabadi Jun 17, 2022
b533f26
Use api
CyrusNajmabadi Jun 17, 2022
c1a76b1
ifdef
CyrusNajmabadi Jun 17, 2022
2ef0717
Merge remote-tracking branch 'upstream/main' into polyfill
CyrusNajmabadi Jun 20, 2022
1f35a7e
Move using outside namespace
CyrusNajmabadi Jun 20, 2022
f5d8137
Move to debug assert
CyrusNajmabadi Jun 20, 2022
be4ef0f
Optimize common cases
CyrusNajmabadi Jun 20, 2022
2babd86
Merge remote-tracking branch 'upstream/main' into polyfill
CyrusNajmabadi Jun 20, 2022
1cdc428
Explain if'defed regions
CyrusNajmabadi Jun 20, 2022
cfce553
Explain if'defed regions
CyrusNajmabadi Jun 20, 2022
fd9a27e
Update System.Text.RegularExpressions.Generator.csproj
CyrusNajmabadi Jun 21, 2022
e2613b6
Port latest changes over
CyrusNajmabadi Jun 22, 2022
a0e9d88
Merge branch 'polyfill' of https://github.com/CyrusNajmabadi/runtime …
CyrusNajmabadi Jun 22, 2022
91672fe
Renames
CyrusNajmabadi Jun 28, 2022
86de769
Merge branch 'main' into polyfill
CyrusNajmabadi Jun 28, 2022
cc933a2
Update src/libraries/Common/src/Roslyn/CSharpSyntaxHelper.cs
CyrusNajmabadi Jun 28, 2022
8a5a8d0
Simplify
CyrusNajmabadi Jun 28, 2022
4eb00d1
Dispose builders
CyrusNajmabadi Jun 28, 2022
59cdea7
Dispose builders
CyrusNajmabadi Jun 28, 2022
e7a1f3e
Simplify by removing support for nested attributes
CyrusNajmabadi Jun 28, 2022
20d62a2
Simplify
CyrusNajmabadi Jun 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use api
  • Loading branch information
CyrusNajmabadi committed Jun 17, 2022
commit b533f261f271a96b07e16560087bc74487b54f95
Original file line number Diff line number Diff line change
@@ -1,58 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is hte change to the actual generator to use the new API.

// The .NET Foundation licenses this file to you under the MIT license.

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

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;

namespace System.Text.RegularExpressions.Generator
{
public partial class RegexGenerator
{
private const string RegexName = "System.Text.RegularExpressions.Regex";
private const string RegexGeneratorAttributeName = "System.Text.RegularExpressions.RegexGeneratorAttribute";

private static bool IsSyntaxTargetForGeneration(SyntaxNode node, CancellationToken cancellationToken) =>
// We don't have a semantic model here, so the best we can do is say whether there are any attributes.
node is MethodDeclarationSyntax { AttributeLists: { Count: > 0 } };

private static bool IsSemanticTargetForGeneration(SemanticModel semanticModel, MethodDeclarationSyntax methodDeclarationSyntax, CancellationToken cancellationToken)
{
foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists)
{
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
{
if (semanticModel.GetSymbolInfo(attributeSyntax, cancellationToken).Symbol is IMethodSymbol attributeSymbol &&
attributeSymbol.ContainingType.ToDisplayString() == RegexGeneratorAttributeName)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no more costly binding of every attribute in the world, as well as a costly ToDisplayString on all of them.

{
return true;
}
}
}

return false;
}

// Returns null if nothing to do, Diagnostic if there's an error to report, or RegexType if the type was analyzed successfully.
private static object? GetSemanticTargetForGeneration(GeneratorSyntaxContext context, CancellationToken cancellationToken)
private static object? GetSemanticTargetForGeneration(
GeneratorAttributeSyntaxContext context, CancellationToken cancellationToken)
{
var methodSyntax = (MethodDeclarationSyntax)context.Node;
var methodSyntax = (MethodDeclarationSyntax)context.TargetNode;
SemanticModel sm = context.SemanticModel;

if (!IsSemanticTargetForGeneration(sm, methodSyntax, cancellationToken))
{
return null;
}

Compilation compilation = sm.Compilation;
INamedTypeSymbol? regexSymbol = compilation.GetBestTypeByMetadataName(RegexName);
INamedTypeSymbol? regexGeneratorAttributeSymbol = compilation.GetBestTypeByMetadataName(RegexGeneratorAttributeName);
Expand All @@ -69,7 +41,7 @@ private static bool IsSemanticTargetForGeneration(SemanticModel semanticModel, M
return null;
}

IMethodSymbol? regexMethodSymbol = sm.GetDeclaredSymbol(methodSyntax, cancellationToken) as IMethodSymbol;
IMethodSymbol regexMethodSymbol = context.TargetSymbol as IMethodSymbol;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i considered doing a direct cast here to IMethodSybmol (since that is an invariant of roslyn given that we only examine MethodDeclarationSytnax). However, i wanted to preserve semantics here, including the high paranoia that this might not always be the same.

if (regexMethodSymbol is null)
{
return null;
Expand Down