Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace System.Text.RegularExpressions
{
[RequiresDynamicCode("Compiling a RegEx requires dynamic code.")]
internal sealed class RegexLWCGCompiler : RegexCompiler
{
/// <summary>
Expand All @@ -31,7 +32,6 @@ internal sealed class RegexLWCGCompiler : RegexCompiler
private static int s_regexCount;

/// <summary>The top-level driver. Initializes everything then calls the Generate* methods.</summary>
[RequiresDynamicCode("Compiling a RegEx requires dynamic code.")]
public RegexRunnerFactory? FactoryInstanceFromCode(string pattern, RegexTree regexTree, RegexOptions options, bool hasTimeout)
{
if (!regexTree.Root.SupportsCompilation(out _))
Expand Down Expand Up @@ -67,7 +67,6 @@ internal sealed class RegexLWCGCompiler : RegexCompiler
}

/// <summary>Begins the definition of a new method (no args) with a specified return value.</summary>
[RequiresDynamicCode("Compiling a RegEx requires dynamic code.")]
private DynamicMethod DefineDynamicMethod(string methname, Type? returntype, Type hostType, Type[] paramTypes)
{
// We're claiming that these are static methods, but really they are instance methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public override void Initialize(AnalysisContext context)
foreach (var extraSyntaxNodeAction in ExtraSyntaxNodeActions)
context.RegisterSyntaxNodeAction(extraSyntaxNodeAction.Action, extraSyntaxNodeAction.SyntaxKind);

// Register the implicit base constructor analysis for all analyzers
context.RegisterSymbolAction(AnalyzeImplicitBaseCtor, SymbolKind.NamedType);

foreach (var extraSymbolAction in ExtraSymbolActions)
context.RegisterSymbolAction(extraSymbolAction.Action, extraSymbolAction.SymbolKind);

Expand Down Expand Up @@ -197,6 +200,33 @@ internal void CheckAndCreateRequiresDiagnostic(
CreateRequiresDiagnostic(member, requiresAttribute, diagnosticContext);
}

private void AnalyzeImplicitBaseCtor(SymbolAnalysisContext context)
{
var typeSymbol = (INamedTypeSymbol)context.Symbol;

if (typeSymbol.TypeKind != TypeKind.Class || typeSymbol.BaseType == null)
return;

if (typeSymbol.InstanceConstructors.Length != 1 || !typeSymbol.InstanceConstructors[0].IsImplicitlyDeclared)
return;

var implicitCtor = typeSymbol.InstanceConstructors[0];

var baseCtor = typeSymbol.BaseType.InstanceConstructors.FirstOrDefault(ctor => ctor.Parameters.IsEmpty);
if (baseCtor == null)
return;

var diagnosticContext = new DiagnosticContext(
typeSymbol.Locations[0],
context.ReportDiagnostic);

CheckAndCreateRequiresDiagnostic(
baseCtor,
implicitCtor,
ImmutableArray<ISymbol>.Empty,
diagnosticContext);
}

[Flags]
protected enum DiagnosticTargets
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public static void Test()
}
}

[ExpectedWarning("IL2026", "BaseWithRequires.BaseWithRequires()", "--BaseWithRequires--", Tool.Analyzer, "")]
[ExpectedWarning("IL2026", "BaseWithRequires.BaseWithRequires()", "--BaseWithRequires--", Tool.Trimmer | Tool.NativeAot, "", CompilerGeneratedCode = true)]
[ExpectedWarning("IL3050", "BaseWithRequires.BaseWithRequires()", "--BaseWithRequires--", Tool.Analyzer, "NativeAOT Specific warning")]
[ExpectedWarning("IL3050", "BaseWithRequires.BaseWithRequires()", "--BaseWithRequires--", Tool.NativeAot, "NativeAOT Specific warning", CompilerGeneratedCode = true)]
class DerivedWithoutRequires : BaseWithRequires
{
[ExpectedWarning("IL2026", "--Requires--")]
Expand All @@ -157,6 +161,9 @@ public static void Test()
{
StaticMethod();
DerivedStaticMethod();

// Instantiate for linker test consistency
new DerivedWithoutRequires();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public static void Main()
AttributeParametersAndProperties.Test();
MembersOnClassWithRequires<int>.Test();
ConstFieldsOnClassWithRequires.Test();

// Instantiate classes so linker warnings match analyzer warnings
new TestUnconditionalSuppressMessage();
new DerivedWithoutRequiresOnType();
}

[RequiresUnreferencedCode("Message for --ClassWithRequires--")]
Expand Down Expand Up @@ -128,6 +132,10 @@ public DerivedFromNestedInRequiresClass() { }
public static void StaticMethod() { }
}

[ExpectedWarning("IL2026", "ClassWithRequires.ClassWithRequires()", "--ClassWithRequires--", Tool.Analyzer, "")]
[ExpectedWarning("IL2026", "ClassWithRequires.ClassWithRequires()", "--ClassWithRequires--", Tool.Trimmer | Tool.NativeAot, "", CompilerGeneratedCode = true)]
[ExpectedWarning("IL3050", "ClassWithRequires.ClassWithRequires()", "--ClassWithRequires--", Tool.Analyzer, "NativeAOT Specific warning")]
[ExpectedWarning("IL3050", "ClassWithRequires.ClassWithRequires()", "--ClassWithRequires--", Tool.NativeAot, "NativeAOT Specific warning", CompilerGeneratedCode = true)]
class TestUnconditionalSuppressMessage : ClassWithRequires
{
public static void StaticMethodInTestSuppressionClass() { }
Expand Down Expand Up @@ -339,6 +347,10 @@ class BaseWithRequiresOnType
public virtual void Method() { }
}

[ExpectedWarning("IL2026", "BaseWithRequiresOnType.BaseWithRequiresOnType()", "RUC", Tool.Analyzer, "")]
[ExpectedWarning("IL2026", "BaseWithRequiresOnType.BaseWithRequiresOnType()", "RUC", Tool.Trimmer | Tool.NativeAot, "", CompilerGeneratedCode = true)]
[ExpectedWarning("IL3050", "BaseWithRequiresOnType.BaseWithRequiresOnType()", "RDC", Tool.Analyzer, "NativeAOT Specific warning")]
[ExpectedWarning("IL3050", "BaseWithRequiresOnType.BaseWithRequiresOnType()", "RDC", Tool.NativeAot, "NativeAOT Specific warning", CompilerGeneratedCode = true)]
class DerivedWithoutRequiresOnType : BaseWithRequiresOnType
{
public override void Method() { }
Expand Down Expand Up @@ -694,6 +706,10 @@ class WithRequiresOnlyInstanceFields
public int InstanceField;
}

[ExpectedWarning("IL2026", "WithRequires.WithRequires()", "--WithRequires--", Tool.Analyzer, "")]
[ExpectedWarning("IL2026", "WithRequires.WithRequires()", "--WithRequires--", Tool.Trimmer | Tool.NativeAot, "", CompilerGeneratedCode = true)]
[ExpectedWarning("IL3050", "WithRequires.WithRequires()", "--WithRequires--", Tool.Analyzer, "NativeAOT Specific warning")]
[ExpectedWarning("IL3050", "WithRequires.WithRequires()", "--WithRequires--", Tool.NativeAot, "NativeAOT Specific warning", CompilerGeneratedCode = true)]
class DerivedWithoutRequires : WithRequires
{
public static int DerivedStaticField;
Expand Down Expand Up @@ -831,6 +847,9 @@ public static void Test()
TestDAMOnTypeAccessInRUCScope(new DAMAnnotatedClassAccessedFromRUCScope());
TestDAMAccessOnOpenGeneric();
TestDAMAccessOnInstantiatedGeneric();

// Instantiate for linker test consistency
new DerivedWithoutRequires();
}
}

Expand All @@ -854,6 +873,10 @@ class DerivedRequires : WithRequires
private event EventHandler DerivedPrivateInstanceEvent;
}

[ExpectedWarning("IL2026", "WithRequires.WithRequires()", "--WithRequires--", Tool.Analyzer, "")]
[ExpectedWarning("IL2026", "WithRequires.WithRequires()", "--WithRequires--", Tool.Trimmer | Tool.NativeAot, "", CompilerGeneratedCode = true)]
[ExpectedWarning("IL3050", "WithRequires.WithRequires()", "--WithRequires--", Tool.Analyzer, "NativeAOT Specific warning")]
[ExpectedWarning("IL3050", "WithRequires.WithRequires()", "--WithRequires--", Tool.NativeAot, "NativeAOT Specific warning", CompilerGeneratedCode = true)]
class DerivedWithoutRequires : WithRequires
{
public static event EventHandler DerivedStaticEvent;
Expand Down Expand Up @@ -1029,6 +1052,9 @@ public static void Test()
DerivedRequiresPublicEvents();
DerivedRequiresNonPublicEvents();
DerivedRequiresAllEvents();

// Instantiate for linker test consistency
new DerivedWithoutRequires();
}
}

Expand All @@ -1050,6 +1076,10 @@ class WithRequiresOnlyInstanceProperties
public int InstanceProperty { get; set; }
}

[ExpectedWarning("IL2026", "WithRequires.WithRequires()", "--WithRequires--", Tool.Analyzer, "")]
[ExpectedWarning("IL2026", "WithRequires.WithRequires()", "--WithRequires--", Tool.Trimmer | Tool.NativeAot, "", CompilerGeneratedCode = true)]
[ExpectedWarning("IL3050", "WithRequires.WithRequires()", "--WithRequires--", Tool.Analyzer, "NativeAOT Specific warning")]
[ExpectedWarning("IL3050", "WithRequires.WithRequires()", "--WithRequires--", Tool.NativeAot, "NativeAOT Specific warning", CompilerGeneratedCode = true)]
class DerivedWithoutRequires : WithRequires
{
public static int DerivedStaticProperty { get; set; }
Expand Down Expand Up @@ -1217,6 +1247,9 @@ public static void Test()
TestDirectReflectionAccess();
TestDynamicDependencyAccess();
TestDAMOnTypeAccess(new DAMAnnotatedClass());

// Instantiate for linker test consistency
new DerivedWithoutRequires();
}
}

Expand Down
Loading