Skip to content

Commit

Permalink
Rename [RegexGenerator] to [GeneratedRegex] (#72434)
Browse files Browse the repository at this point in the history
Co-authored-by: Jose Perez Rodriguez <joperezr@microsoft.com>
  • Loading branch information
am11 and joperezr committed Jul 28, 2022
1 parent e96cfa9 commit b0090d0
Show file tree
Hide file tree
Showing 37 changed files with 486 additions and 486 deletions.
14 changes: 7 additions & 7 deletions docs/project/list-of-diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
| __`SYSLIB0033`__ | Rfc2898DeriveBytes.CryptDeriveKey is obsolete and is not supported. Use PasswordDeriveBytes.CryptDeriveKey instead. |
| __`SYSLIB0034`__ | CmsSigner(CspParameters) is obsolete and is not supported. Use an alternative constructor instead. |
| __`SYSLIB0035`__ | ComputeCounterSignature without specifying a CmsSigner is obsolete and is not supported. Use the overload that accepts a CmsSigner. |
| __`SYSLIB0036`__ | Regex.CompileToAssembly is obsolete and not supported. Use RegexGeneratorAttribute with the regular expression source generator instead. |
| __`SYSLIB0036`__ | Regex.CompileToAssembly is obsolete and not supported. Use GeneratedRegexAttribute with the regular expression source generator instead. |
| __`SYSLIB0037`__ | AssemblyName members HashAlgorithm, ProcessorArchitecture, and VersionCompatibility are obsolete and not supported. |
| __`SYSLIB0038`__ | SerializationFormat.Binary is obsolete and should not be used. See https://aka.ms/serializationformat-binary-obsolete for more information. |
| __`SYSLIB0039`__ | TLS versions 1.0 and 1.1 have known vulnerabilities and are not recommended. Use a newer TLS version instead, or use SslProtocols.None to defer to OS defaults. |
Expand Down Expand Up @@ -148,11 +148,11 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL
| __`SYSLIB1037`__ | JsonSourceGenerator encountered a type with init-only properties for which deserialization is not supported |
| __`SYSLIB1038`__ | JsonSourceGenerator encountered a property annotated with [JsonInclude] that has inaccessible accessors |
| __`SYSLIB1039`__ | *_`SYSLIB1039` reserved for System.Text.Json.SourceGeneration._* |
| __`SYSLIB1040`__ | Invalid RegexGenerator attribute |
| __`SYSLIB1041`__ | Multiple RegexGenerator attribute |
| __`SYSLIB1042`__ | Invalid RegexGenerator arguments |
| __`SYSLIB1043`__ | RegexGenerator method must have a valid signature |
| __`SYSLIB1044`__ | RegexGenerator only supports C# 10 and newer |
| __`SYSLIB1040`__ | Invalid GeneratedRegex attribute |
| __`SYSLIB1041`__ | Multiple GeneratedRegex attribute |
| __`SYSLIB1042`__ | Invalid GeneratedRegex arguments |
| __`SYSLIB1043`__ | GeneratedRegex method must have a valid signature |
| __`SYSLIB1044`__ | GeneratedRegex only supports C# 11 and newer |
| __`SYSLIB1045`__ | *_`SYSLIB1045`-`SYSLIB1049` reserved for System.Text.RegularExpressions.Generator._* |
| __`SYSLIB1046`__ | *_`SYSLIB1045`-`SYSLIB1049` reserved for System.Text.RegularExpressions.Generator._* |
| __`SYSLIB1047`__ | *_`SYSLIB1045`-`SYSLIB1049` reserved for System.Text.RegularExpressions.Generator._* |
Expand Down Expand Up @@ -183,4 +183,4 @@ The diagnostic id values reserved for .NET Libraries analyzer warnings are `SYSL

| Suppression ID | Suppressed Diagnostic ID | Description |
| :----------------- | :----------------------- | :---------- |
| __`SYSLIBSUPPRESS0001`__ | CA1822 | Do not offer to make methods static when the methods need to be instance methods for a custom marshaller shape. |
| __`SYSLIBSUPPRESS0001`__ | CA1822 | Do not offer to make methods static when the methods need to be instance methods for a custom marshaller shape. |
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/Obsoletions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal static class Obsoletions
internal const string SignerInfoCounterSigMessage = "ComputeCounterSignature without specifying a CmsSigner is obsolete and is not supported. Use the overload that accepts a CmsSigner.";
internal const string SignerInfoCounterSigDiagId = "SYSLIB0035";

internal const string RegexCompileToAssemblyMessage = "Regex.CompileToAssembly is obsolete and not supported. Use the RegexGeneratorAttribute with the regular expression source generator instead.";
internal const string RegexCompileToAssemblyMessage = "Regex.CompileToAssembly is obsolete and not supported. Use the GeneratedRegexAttribute with the regular expression source generator instead.";
internal const string RegexCompileToAssemblyDiagId = "SYSLIB0036";

internal const string AssemblyNameMembersMessage = "AssemblyName members HashAlgorithm, ProcessorArchitecture, and VersionCompatibility are obsolete and not supported.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public DesignerVerb(string text, EventHandler handler, CommandID startCommandID)
Properties["Text"] = text == null ? null : GetParameterReplacementRegex().Replace(text, "");
}

[RegexGenerator(@"\(\&.\)")]
[GeneratedRegex(@"\(\&.\)")]
private static partial Regex GetParameterReplacementRegex();

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ internal static bool TypeOfScriptObject_IsAssignableFrom(Type type) =>
public static readonly string NewObjectId = string.Empty;
public const string NullObjectId = null;
public const string FullSRSInternalsVisiblePattern = @"^[\s]*System\.Runtime\.Serialization[\s]*,[\s]*PublicKey[\s]*=[\s]*(?i:00240000048000009400000006020000002400005253413100040000010001008d56c76f9e8649383049f383c44be0ec204181822a6c31cf5eb7ef486944d032188ea1d3920763712ccb12d75fb77e9811149e6148e5d32fbaab37611c1878ddc19e20ef135d0cb2cff2bfec3d115810c3d9069638fe4be215dbf795861920e5ab6f7db2e2ceef136ac23d5dd2bf031700aec232f6c6b1c785b4305c123b37ab)[\s]*$";
[RegexGenerator(FullSRSInternalsVisiblePattern)]
[GeneratedRegex(FullSRSInternalsVisiblePattern)]
public static partial Regex FullSRSInternalsVisibleRegex();
public const string Space = " ";
public const string XsiPrefix = "i";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ private static bool MatchEnumeration(DateTime value, ArrayList enumeration, XmlS
internal sealed partial class StringFacetsChecker : FacetsChecker
{ //All types derived from string & anyURI

[RegexGenerator("^([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*$", RegexOptions.ExplicitCapture)]
[GeneratedRegex("^([a-zA-Z]{1,8})(-[a-zA-Z0-9]{1,8})*$", RegexOptions.ExplicitCapture)]
private static partial Regex LanguageRegex();

internal override Exception? CheckValueFacets(object value, XmlSchemaDatatype datatype)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ internal sealed partial class SourceInfo
{
//a[ia]
//((global::System.Xml.Serialization.XmlSerializerNamespaces)p[0])
[RegexGenerator("([(][(](?<t>[^)]+)[)])?(?<a>[^[]+)[[](?<ia>.+)[]][)]?")]
[GeneratedRegex("([(][(](?<t>[^)]+)[)])?(?<a>[^[]+)[[](?<ia>.+)[]][)]?")]
private static partial Regex Regex1();

//((global::Microsoft.CFx.Test.Common.TypeLibrary.IXSType_9)o), @"IXSType_9", @"", true, true);
[RegexGenerator("[(][(](?<cast>[^)]+)[)](?<arg>[^)]+)[)]")]
[GeneratedRegex("[(][(](?<cast>[^)]+)[)](?<arg>[^)]+)[)]")]
private static partial Regex Regex2();

private static readonly Lazy<MethodInfo> s_iListGetItemMethod = new Lazy<MethodInfo>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2704,10 +2704,10 @@ private void WriteSourceBeginTyped(string source, TypeDesc? typeDesc)
WriteSourceBegin(source);
}

[RegexGenerator("(?<locA1>[^ ]+) = .+EnsureArrayIndex[(](?<locA2>[^,]+), (?<locI1>[^,]+),[^;]+;(?<locA3>[^[]+)[[](?<locI2>[^+]+)[+][+][]]")]
[GeneratedRegex("(?<locA1>[^ ]+) = .+EnsureArrayIndex[(](?<locA2>[^,]+), (?<locI1>[^,]+),[^;]+;(?<locA3>[^[]+)[[](?<locI2>[^+]+)[+][+][]]")]
private static partial Regex EnsureArrayIndexRegex();

[RegexGenerator("(?<a>[^[]+)[[](?<ia>.+)[]]")]
[GeneratedRegex("(?<a>[^[]+)[[](?<ia>.+)[]]")]
private static partial Regex P0Regex();

private void WriteSourceBegin(string source)
Expand Down Expand Up @@ -3520,19 +3520,19 @@ private static void WriteLocalDecl(string variableName, SourceInfo initValue)
ReflectionAwareILGen.WriteLocalDecl(variableName, initValue);
}

[RegexGenerator("UnknownNode[(]null, @[\"](?<qnames>[^\"]*)[\"][)];")]
[GeneratedRegex("UnknownNode[(]null, @[\"](?<qnames>[^\"]*)[\"][)];")]
private static partial Regex UnknownNodeNullAnyTypeRegex();

[RegexGenerator("UnknownNode[(][(]object[)](?<o>[^,]+), @[\"](?<qnames>[^\"]*)[\"][)];")]
[GeneratedRegex("UnknownNode[(][(]object[)](?<o>[^,]+), @[\"](?<qnames>[^\"]*)[\"][)];")]
private static partial Regex UnknownNodeObjectEmptyRegex();

[RegexGenerator("UnknownNode[(][(]object[)](?<o>[^,]+), null[)];")]
[GeneratedRegex("UnknownNode[(][(]object[)](?<o>[^,]+), null[)];")]
private static partial Regex UnknownNodeObjectNullRegex();

[RegexGenerator("UnknownNode[(][(]object[)](?<o>[^)]+)[)];")]
[GeneratedRegex("UnknownNode[(][(]object[)](?<o>[^)]+)[)];")]
private static partial Regex UnknownNodeObjectRegex();

[RegexGenerator("paramsRead\\[(?<index>[0-9]+)\\]")]
[GeneratedRegex("paramsRead\\[(?<index>[0-9]+)\\]")]
private static partial Regex ParamsReadRegex();

private void ILGenElseString(string elseString)
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.Xml/src/System/Xml/XmlConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,10 @@ public partial class XmlConvert

private const int EncodedCharLength = 7; // ("_xFFFF_".Length);

[RegexGenerator("_[Xx][0-9a-fA-F]{4}(?:_|[0-9a-fA-F]{4}_)")]
[GeneratedRegex("_[Xx][0-9a-fA-F]{4}(?:_|[0-9a-fA-F]{4}_)")]
private static partial Regex DecodeCharRegex();

[RegexGenerator("(?<=_)[Xx][0-9a-fA-F]{4}(?:_|[0-9a-fA-F]{4}_)")]
[GeneratedRegex("(?<=_)[Xx][0-9a-fA-F]{4}(?:_|[0-9a-fA-F]{4}_)")]
private static partial Regex EncodeCharRegex();

private static int FromHex(char digit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2327,7 +2327,7 @@ public static IEnumerable<object[]> SystemTimeZonesTestData()

private const string IanaAbbreviationPattern = @"^(?:[A-Z][A-Za-z]+|[+-]\d{2}|[+-]\d{4})$";

[RegexGenerator(IanaAbbreviationPattern)]
[GeneratedRegex(IanaAbbreviationPattern)]
private static partial Regex IanaAbbreviationRegex();

// UTC aliases per https://github.com/unicode-org/cldr/blob/master/common/bcp47/timezone.xml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ namespace System.Text.RegularExpressions.Generator
{
internal static class DiagnosticDescriptors
{
private const string Category = "RegexGenerator";
private const string Category = "GeneratedRegex";

public static DiagnosticDescriptor InvalidRegexGeneratorAttribute { get; } = new DiagnosticDescriptor(
public static DiagnosticDescriptor InvalidGeneratedRegexAttribute { get; } = new DiagnosticDescriptor(
id: "SYSLIB1040",
title: new LocalizableResourceString(nameof(SR.InvalidRegexGeneratorAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.InvalidRegexGeneratorAttributeMessage), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
title: new LocalizableResourceString(nameof(SR.InvalidGeneratedRegexAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.InvalidGeneratedRegexAttributeMessage), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
category: Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
customTags: WellKnownDiagnosticTags.NotConfigurable);

public static DiagnosticDescriptor MultipleRegexGeneratorAttributes { get; } = new DiagnosticDescriptor(
public static DiagnosticDescriptor MultipleGeneratedRegexAttributes { get; } = new DiagnosticDescriptor(
id: "SYSLIB1041",
title: new LocalizableResourceString(nameof(SR.InvalidRegexGeneratorAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.MultipleRegexGeneratorAttributesMessage), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
title: new LocalizableResourceString(nameof(SR.InvalidGeneratedRegexAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.MultipleGeneratedRegexAttributesMessage), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
category: Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
customTags: WellKnownDiagnosticTags.NotConfigurable);

public static DiagnosticDescriptor InvalidRegexArguments { get; } = new DiagnosticDescriptor(
id: "SYSLIB1042",
title: new LocalizableResourceString(nameof(SR.InvalidRegexGeneratorAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
title: new LocalizableResourceString(nameof(SR.InvalidGeneratedRegexAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.InvalidRegexArgumentsMessage), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
category: Category,
DiagnosticSeverity.Error,
Expand All @@ -39,7 +39,7 @@ internal static class DiagnosticDescriptors

public static DiagnosticDescriptor RegexMethodMustHaveValidSignature { get; } = new DiagnosticDescriptor(
id: "SYSLIB1043",
title: new LocalizableResourceString(nameof(SR.InvalidRegexGeneratorAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
title: new LocalizableResourceString(nameof(SR.InvalidGeneratedRegexAttributeTitle), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
messageFormat: new LocalizableResourceString(nameof(SR.RegexMethodMustHaveValidSignatureMessage), SR.ResourceManager, typeof(FxResources.System.Text.RegularExpressions.Generator.SR)),
category: Category,
DiagnosticSeverity.Error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ 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 const string GeneratedRegexAttributeName = "System.Text.RegularExpressions.GeneratedRegexAttribute";

// 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(
Expand All @@ -27,9 +27,9 @@ public partial class RegexGenerator

Compilation compilation = sm.Compilation;
INamedTypeSymbol? regexSymbol = compilation.GetBestTypeByMetadataName(RegexName);
INamedTypeSymbol? regexGeneratorAttributeSymbol = compilation.GetBestTypeByMetadataName(RegexGeneratorAttributeName);
INamedTypeSymbol? generatedRegexAttributeSymbol = compilation.GetBestTypeByMetadataName(GeneratedRegexAttributeName);

if (regexSymbol is null || regexGeneratorAttributeSymbol is null)
if (regexSymbol is null || generatedRegexAttributeSymbol is null)
{
// Required types aren't available
return null;
Expand Down Expand Up @@ -59,25 +59,25 @@ public partial class RegexGenerator
int? matchTimeout = null;
foreach (AttributeData attributeData in boundAttributes)
{
if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, regexGeneratorAttributeSymbol))
if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, generatedRegexAttributeSymbol))
{
continue;
}

if (attributeData.ConstructorArguments.Any(ca => ca.Kind == TypedConstantKind.Error))
{
return Diagnostic.Create(DiagnosticDescriptors.InvalidRegexGeneratorAttribute, methodSyntax.GetLocation());
return Diagnostic.Create(DiagnosticDescriptors.InvalidGeneratedRegexAttribute, methodSyntax.GetLocation());
}

if (pattern is not null)
{
return Diagnostic.Create(DiagnosticDescriptors.MultipleRegexGeneratorAttributes, methodSyntax.GetLocation());
return Diagnostic.Create(DiagnosticDescriptors.MultipleGeneratedRegexAttributes, methodSyntax.GetLocation());
}

ImmutableArray<TypedConstant> items = attributeData.ConstructorArguments;
if (items.Length == 0 || items.Length > 3)
{
return Diagnostic.Create(DiagnosticDescriptors.InvalidRegexGeneratorAttribute, methodSyntax.GetLocation());
return Diagnostic.Create(DiagnosticDescriptors.InvalidGeneratedRegexAttribute, methodSyntax.GetLocation());
}

attributeFound = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
IncrementalValueProvider<ImmutableArray<object>> codeOrDiagnostics =
context.SyntaxProvider

// Find all MethodDeclarationSyntax nodes attributed with RegexGenerator and gather the required information.
// Find all MethodDeclarationSyntax nodes attributed with GeneratedRegex and gather the required information.
.ForAttributeWithMetadataName(
context,
RegexGeneratorAttributeName,
GeneratedRegexAttributeName,
(node, _) => node is MethodDeclarationSyntax,
GetSemanticTargetForGeneration)
.Where(static m => m is not null)
Expand Down
Loading

0 comments on commit b0090d0

Please sign in to comment.