Skip to content

Commit

Permalink
More warning cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-karas committed Jul 31, 2020
1 parent 8d8f14d commit 9e63945
Show file tree
Hide file tree
Showing 21 changed files with 292 additions and 105 deletions.
225 changes: 198 additions & 27 deletions docs/error-codes.md

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions src/linker/Linker.Dataflow/FlowAnnotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMembersAttrib
else
_context.LogWarning (
$"Attribute 'System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute' doesn't have the required number of parameters specified",
2028, locationMember ?? (provider as IMemberDefinition), subcategory: MessageSubCategory.TrimAnalysis);
2028, locationMember ?? (provider as IMemberDefinition));
}
return DynamicallyAccessedMemberTypes.None;
}
Expand Down Expand Up @@ -161,12 +161,16 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
paramAnnotations[0] = methodMemberTypes;
}
} else if (methodMemberTypes != DynamicallyAccessedMemberTypes.None) {
_context.LogWarning ($"The DynamicallyAccessedMembersAttribute is only allowed on method parameters, return value or generic parameters.", 2041, method);
_context.LogWarning (
$"The 'DynamicallyAccessedMembersAttribute' is not allowed on methods. It is allowed on method return value or method parameters though.",
2041, method, subcategory: MessageSubCategory.TrimAnalysis);
}
} else {
offset = 0;
if (methodMemberTypes != DynamicallyAccessedMemberTypes.None) {
_context.LogWarning ($"The DynamicallyAccessedMembersAttribute is only allowed on method parameters, return value or generic parameters.", 2041, method);
_context.LogWarning (
$"The 'DynamicallyAccessedMembersAttribute' is not allowed on methods. It is allowed on method return value or method parameters though.",
2041, method, subcategory: MessageSubCategory.TrimAnalysis);
}
}

Expand Down Expand Up @@ -290,7 +294,9 @@ TypeAnnotations BuildTypeAnnotations (TypeDefinition type)
FieldDefinition backingField;
if (backingFieldFromGetter != null && backingFieldFromSetter != null &&
backingFieldFromGetter != backingFieldFromSetter) {
_context.LogWarning ($"Could not find a unique backing field for property '{property.FullName}' to propagate 'DynamicallyAccessedMembersAttribute'. The backing fields from getter '{backingFieldFromGetter.FullName}' and setter '{backingFieldFromSetter.FullName}' are not the same.", 2042, property);
_context.LogWarning (
$"Could not find a unique backing field for property '{property.GetDisplayName ()}' to propagate 'DynamicallyAccessedMembersAttribute'.",
2042, property, subcategory: MessageSubCategory.TrimAnalysis);
backingField = null;
} else {
backingField = backingFieldFromGetter ?? backingFieldFromSetter;
Expand Down Expand Up @@ -459,11 +465,10 @@ void ValidateMethodGenericParametersHaveNoAnnotations (ref MethodAnnotations met
void LogValidationWarning (IMetadataTokenProvider provider, IMetadataTokenProvider baseProvider, IMemberDefinition origin)
{
_context.LogWarning (
$"DynamicallyAccessedMemberTypes in DynamicallyAccessedMembersAttribute on {DiagnosticUtilities.GetMetadataTokenDescriptionForErrorMessage (provider)} " +
$"'DynamicallyAccessedMemberTypes' in 'DynamicallyAccessedMembersAttribute' on {DiagnosticUtilities.GetMetadataTokenDescriptionForErrorMessage (provider)} " +
$"don't match overridden {DiagnosticUtilities.GetMetadataTokenDescriptionForErrorMessage (baseProvider)}. " +
$"All overridden members must have the same DynamicallyAccessedMembersAttribute usage.",
2047,
origin);
$"All overridden members must have the same 'DynamicallyAccessedMembersAttribute' usage.",
2047, origin, subcategory: MessageSubCategory.TrimAnalysis);
}

readonly struct TypeAnnotations
Expand Down
4 changes: 2 additions & 2 deletions src/linker/Linker.Dataflow/ReflectionMethodBodyScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ public override bool HandleCall (MethodBody callingMethodBody, MethodReference c
// For now we don't support tracking actual array elements, so we can't validate that the requirements are fulfilled.
reflectionContext.RecordUnrecognizedPattern (
2051,
$"Call to `{calledMethodDefinition.GetDisplayName ()}` can not be statically analyzed. " +
$"Call to '{calledMethodDefinition.GetDisplayName ()}' can not be statically analyzed. " +
$"It's not possible to guarantee the availability of requirements of the generic type.");
}
}
Expand All @@ -576,7 +576,7 @@ public override bool HandleCall (MethodBody callingMethodBody, MethodReference c
// We have no way to "include more" to fix this if we don't know, so we have to warn
reflectionContext.RecordUnrecognizedPattern (
2051,
$"Call to `{calledMethodDefinition.GetDisplayName ()}` can not be statically analyzed. " +
$"Call to '{calledMethodDefinition.GetDisplayName ()}' can not be statically analyzed. " +
$"It's not possible to guarantee the availability of requirements of the generic type.");
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/linker/Linker.Steps/BodySubstituterStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void ProcessResources (AssemblyDefinition assembly, XPathNodeIterator iterator)

string action = GetAttribute (nav, "action");
if (action != "remove") {
Context.LogWarning ($"Invalid 'action' attribute for resource '{name}'.", 2039, _xmlDocumentLocation);
Context.LogWarning ($"Invalid value '{action}' for attribute 'action' for resource '{name}'.", 2039, _xmlDocumentLocation);
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/linker/Linker.Steps/DynamicDependencyLookupStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void ProcessDynamicDependencyAttributes (IMemberDefinition member)
if (!IsPreserveDependencyAttribute (ca.AttributeType))
continue;
#if FEATURE_ILLINK
Context.LogWarning ($"PreserveDependencyAttribute is deprecated. Use DynamicDependencyAttribute instead.", 2033, member);
Context.LogWarning ($"'PreserveDependencyAttribute' is deprecated. Use 'DynamicDependencyAttribute' instead.", 2033, member);
#endif
if (ca.ConstructorArguments.Count != 3)
continue;
Expand All @@ -82,7 +82,7 @@ void ProcessDynamicDependencyAttributes (IMemberDefinition member)

var assembly = Context.Resolve (new AssemblyNameReference (dynamicDependency.AssemblyName, new Version ()));
if (assembly == null) {
Context.LogWarning ($"Unresolved assembly '{dynamicDependency.AssemblyName}' in DynamicDependencyAttribute on '{member}'", 2035, member);
Context.LogWarning ($"Unresolved assembly '{dynamicDependency.AssemblyName}' in 'DynamicDependencyAttribute'", 2035, member);
continue;
}

Expand Down
19 changes: 11 additions & 8 deletions src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ void MarkDynamicDependency (DynamicDependency dynamicDependency, IMemberDefiniti
if (dynamicDependency.AssemblyName != null) {
assembly = _context.GetLoadedAssembly (dynamicDependency.AssemblyName);
if (assembly == null) {
_context.LogWarning ($"Unresolved assembly '{dynamicDependency.AssemblyName}' in DynamicDependencyAttribute on '{context}'", 2035, context);
_context.LogWarning ($"Unresolved assembly '{dynamicDependency.AssemblyName}' in 'DynamicDependencyAttribute'", 2035, context);
return;
}
} else {
Expand All @@ -613,19 +613,19 @@ void MarkDynamicDependency (DynamicDependency dynamicDependency, IMemberDefiniti
if (dynamicDependency.TypeName is string typeName) {
type = DocumentationSignatureParser.GetTypeByDocumentationSignature (assembly, typeName);
if (type == null) {
_context.LogWarning ($"Unresolved type '{typeName}' in DynamicDependencyAttribute on '{context}'", 2036, context);
_context.LogWarning ($"Unresolved type '{typeName}' in DynamicDependencyAttribute", 2036, context);
return;
}
} else if (dynamicDependency.Type is TypeReference typeReference) {
type = typeReference.Resolve ();
if (type == null) {
_context.LogWarning ($"Unresolved type '{typeReference}' in DynamicDependencyAtribute on '{context}'", 2036, context);
_context.LogWarning ($"Unresolved type '{typeReference}' in DynamicDependencyAtribute", 2036, context);
return;
}
} else {
type = context.DeclaringType.Resolve ();
if (type == null) {
_context.LogWarning ($"Unresolved type '{context.DeclaringType}' in DynamicDependencyAttribute on '{context}'", 2036, context);
_context.LogWarning ($"Unresolved type '{context.DeclaringType}' in DynamicDependencyAttribute", 2036, context);
return;
}
}
Expand Down Expand Up @@ -1397,13 +1397,16 @@ internal protected virtual TypeDefinition MarkType (TypeReference reference, Dep
if (_context.Annotations.HasLinkerAttribute<RemoveAttributeInstancesAttribute> (type)) {
// Don't warn about references from the removed attribute itself (for example the .ctor on the attribute
// will call MarkType on the attribute type itself).
// If for some reason we do keep the attribute type (could be because of previous reference which would cause 2045
// If for some reason we do keep the attribute type (could be because of previous reference which would cause IL2045
// or because of a copy assembly with a reference and so on) then we should not spam the warnings due to the type itself.
if (sourceLocationMember.DeclaringType != type)
_context.LogWarning ($"Custom Attribute {type.GetDisplayName ()} is being referenced in code but the linker was " +
_context.LogWarning (
$"Attribute '{type.GetDisplayName ()}' is being referenced in code but the linker was " +
$"instructed to remove all instances of this attribute. If the attribute instances are necessary make sure to " +
$"either remove the linker attribute XML portion which removes the attribute instances, or to override this use " +
$"the linker XML descriptor to keep the attribute type (which in turn keeps all of its instances).", 2045, sourceLocationMember);
$"either remove the linker attribute XML portion which removes the attribute instances, " +
$"or override the removal by using the linker XML descriptor to keep the attribute type " +
$"(which in turn keeps all of its instances).",
2045, sourceLocationMember, subcategory: MessageSubCategory.TrimAnalysis);
}

if (CheckProcessed (type))
Expand Down
2 changes: 1 addition & 1 deletion src/linker/Linker.Steps/ResolveFromXmlStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void ProcessNamespaces (AssemblyDefinition assembly, XPathNodeIterator iterator)
}

if (!foundMatch) {
Context.LogWarning ($"Could not find any type in namespace '{fullname}' specified in '{_xmlDocumentLocation}'", 2044, _xmlDocumentLocation);
Context.LogWarning ($"Could not find any type in namespace '{fullname}'", 2044, _xmlDocumentLocation);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ void ValidateMethodRequiresUnreferencedCodeAreSame (MethodDefinition method, Met
if (annotations.HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (method) !=
annotations.HasLinkerAttribute<RequiresUnreferencedCodeAttribute> (baseMethod))
Context.LogWarning (
$"Presence of RequiresUnreferencedCodeAttribute on method '{method.GetDisplayName ()}' doesn't match overridden method '{baseMethod.GetDisplayName ()}'. " +
$"All overridden methods must have RequiresUnreferencedCodeAttribute.",
2046,
method);
$"Presence of 'RequiresUnreferencedCodeAttribute' on method '{method.GetDisplayName ()}' doesn't match overridden method '{baseMethod.GetDisplayName ()}'. " +
$"All overridden methods must have 'RequiresUnreferencedCodeAttribute'.",
2046, method, subcategory: MessageSubCategory.TrimAnalysis);
}
}
}
2 changes: 1 addition & 1 deletion src/linker/Linker/DynamicDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public DynamicDependency (DynamicallyAccessedMemberTypes memberTypes, string typ
if (dynamicDependency != null)
return dynamicDependency;

context.LogWarning ($"Invalid DynamicDependencyAttribute on '{member}'", 2034, member);
context.LogWarning ($"The 'DynamicDependencyAttribute' could not be analyzed", 2034, member);
return null;
}

Expand Down
7 changes: 3 additions & 4 deletions test/Mono.Linker.Tests.Cases/DataFlow/MethodThisDataFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,19 @@ static void TestAnnotationOnNonTypeMethod ()
{
var t = new NonTypeType ();
t.GetMethod ("foo");
NonTypeType.StaticMethod ();
}

class NonTypeType
{
[LogContains ("warning IL2041: Mono.Linker.Tests.Cases.DataFlow.MethodThisDataFlow.NonTypeType.GetMethod(String): " +
"The DynamicallyAccessedMembersAttribute is only allowed on method parameters, return value or generic parameters.")]
[ExpectedWarning ("IL2041")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public MethodInfo GetMethod (string name)
{
return null;
}

[LogContains ("warning IL2041: Mono.Linker.Tests.Cases.DataFlow.MethodThisDataFlow.NonTypeType.StaticMethod(): " +
"The DynamicallyAccessedMembersAttribute is only allowed on method parameters, return value or generic parameters.")]
[ExpectedWarning ("IL2041")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public static void StaticMethod ()
{
Expand Down
4 changes: 1 addition & 3 deletions test/Mono.Linker.Tests.Cases/DataFlow/PropertyDataFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ public void TestPropertyWithDifferentBackingFields ()
private Type PropertyWithDifferentBackingFields_SetterField;

[ExpectedWarning ("IL2042",
"System.Type Mono.Linker.Tests.Cases.DataFlow.PropertyDataFlow/TestAutomaticPropagationType::PropertyWithDifferentBackingFields()",
"System.Type Mono.Linker.Tests.Cases.DataFlow.PropertyDataFlow/TestAutomaticPropagationType::PropertyWithDifferentBackingFields_GetterField",
"System.Type Mono.Linker.Tests.Cases.DataFlow.PropertyDataFlow/TestAutomaticPropagationType::PropertyWithDifferentBackingFields_SetterField")]
"System.Type Mono.Linker.Tests.Cases.DataFlow.PropertyDataFlow/TestAutomaticPropagationType::PropertyWithDifferentBackingFields()")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicConstructors)]
Type PropertyWithDifferentBackingFields {
get {
Expand Down
Loading

0 comments on commit 9e63945

Please sign in to comment.