Skip to content

Commit

Permalink
Add UnconditionalSuppressMessageAttribute (dotnet/linker#1185)
Browse files Browse the repository at this point in the history
Co-authored-by: Marek Safar <[email protected]>
Co-authored-by: Vitek Karas <[email protected]>

Commit migrated from dotnet/linker@d51be36
  • Loading branch information
mateoatr authored May 29, 2020
1 parent 0479938 commit 4a60e5d
Show file tree
Hide file tree
Showing 25 changed files with 510 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ methodParams[argsParam] is ArrayValue arrayValue &&
message += " " + requiresUnreferencedCode.Url;
}

_context.LogMessage (MessageContainer.CreateWarningMessage (message, 2026, origin: MessageOrigin.TryGetOrigin (callingMethodBody.Method, operation.Offset)));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context, message, 2026, origin: MessageOrigin.TryGetOrigin (callingMethodBody.Method, operation.Offset)));
}

// To get good reporting of errors we need to track the origin of the value for all method calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public DynamicallyAccessedMemberTypes GetParameterAnnotation (MethodDefinition m
firstAppearance = false;
parameterAnnotation = Annotation;
} else if (ParamName == paramName && !firstAppearance) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"There are duplicate parameter names for '{paramName}' inside '{method.Name}' in '{_xmlDocumentLocation}'", 2024));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"There are duplicate parameter names for '{paramName}' inside '{method.Name}' in '{_xmlDocumentLocation}'", 2024, _xmlDocumentLocation));
}
}
}
Expand Down Expand Up @@ -78,9 +79,10 @@ public DynamicallyAccessedMemberTypes GetThisParameterAnnotation (MethodDefiniti
static DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMemberAttribute (ArrayBuilder<Attribute> attributes, LinkContext _context, string _xmlDocumentLocation)
{
foreach (var attribute in attributes.ToArray ()) {
if (attribute.attributeName == "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers" && attribute.arguments.Count == 1) {
if (attribute.attributeName == "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers") {
if (attribute.arguments.Count == 0) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"DynamicallyAccessedMembers attribute was specified but no argument was proportioned", 2020));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context, $"DynamicallyAccessedMembers attribute was specified but no argument was proportioned",
2020, _xmlDocumentLocation));
} else if (attribute.arguments.Count == 1) {
DynamicallyAccessedMemberTypes result;
foreach (var argument in attribute.arguments.ToArray ()) {
Expand All @@ -89,11 +91,13 @@ static DynamicallyAccessedMemberTypes GetMemberTypesForDynamicallyAccessedMember
if (Enum.TryParse (argument, false, out result)) {
return result;
} else {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not parse argument '{argument}' specified in '{_xmlDocumentLocation}' as a DynamicallyAccessedMemberTypes", 2021));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"Could not parse argument '{argument}' specified in '{_xmlDocumentLocation}' as a DynamicallyAccessedMemberTypes", 2021, _xmlDocumentLocation));
}
}
} else {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"DynamicallyAccessedMembers attribute was specified but there is more than one argument", 2022));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"DynamicallyAccessedMembers attribute was specified but there is more than one argument", 2022, _xmlDocumentLocation));
}
}
}
Expand Down Expand Up @@ -127,7 +131,8 @@ private void ProcessAssemblies (LinkContext context, XPathNodeIterator iterator)
AssemblyDefinition assembly = GetAssembly (context, GetAssemblyName (iterator.Current));

if (assembly == null) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not resolve assembly {GetAssemblyName (iterator.Current).Name} specified in {_xmlDocumentLocation}", 2007));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"Could not resolve assembly {GetAssemblyName (iterator.Current).Name} specified in {_xmlDocumentLocation}", 2007, _xmlDocumentLocation));
continue;
}
ProcessTypes (assembly, iterator.Current.SelectChildren ("type", string.Empty));
Expand Down Expand Up @@ -186,7 +191,7 @@ void ProcessTypes (AssemblyDefinition assembly, XPathNodeIterator iterator)
}

if (type == null) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not resolve type '{fullname}' specified in {_xmlDocumentLocation}", 2008));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context, $"Could not resolve type '{fullname}' specified in {_xmlDocumentLocation}", 2008, _xmlDocumentLocation));
continue;
}

Expand Down Expand Up @@ -316,7 +321,8 @@ void ProcessFieldSignature (TypeDefinition type, string signature, XPathNodeIter
{
FieldDefinition field = GetField (type, signature);
if (field == null) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not find field '{signature}' in type '{type.FullName}' specified in { _xmlDocumentLocation}", 2016));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"Could not find field '{signature}' in type '{type.FullName}' specified in { _xmlDocumentLocation}", 2016, _xmlDocumentLocation));
return;
}
ArrayBuilder<Attribute> attributes = ProcessAttributes (iterator);
Expand Down Expand Up @@ -356,7 +362,8 @@ void ProcessMethodSignature (TypeDefinition type, string signature, XPathNodeIte
{
MethodDefinition method = GetMethod (type, signature);
if (method == null) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not find method '{signature}' in type '{type.FullName}' specified in '{_xmlDocumentLocation}'", 2009));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"Could not find method '{signature}' in type '{type.FullName}' specified in '{_xmlDocumentLocation}'", 2009, _xmlDocumentLocation));
return;
}
ProcessMethodChildren (type, method, iterator);
Expand Down Expand Up @@ -388,7 +395,8 @@ void ProcessMethodChildren (TypeDefinition type, MethodDefinition method, XPathN
returnAnnotation = returnparamAnnotation;
}
} else {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"There is more than one return parameter specified for '{method.Name}' in '{_xmlDocumentLocation}'", 2023));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"There is more than one return parameter specified for '{method.Name}' in '{_xmlDocumentLocation}'", 2023, _xmlDocumentLocation));
}
if (returnAnnotation != 0 || parameterAnnotation.Count > 0)
_methods[method] = new AnnotatedMethod (returnAnnotation, parameterAnnotation.ToArray ());
Expand Down
20 changes: 11 additions & 9 deletions src/tools/illink/src/linker/Linker.Steps/BodySubstituterStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ void ProcessAssemblies (XPathNodeIterator iterator)
AssemblyDefinition assembly = Context.GetLoadedAssembly (name.Name);

if (assembly == null) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not resolve assembly {GetAssemblyName (iterator.Current).Name} specified in {_xmlDocumentLocation}", 2007));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context,
$"Could not resolve assembly {GetAssemblyName (iterator.Current).Name} specified in {_xmlDocumentLocation}", 2007, _xmlDocumentLocation));
continue;
}

Expand All @@ -114,7 +115,7 @@ void ProcessTypes (AssemblyDefinition assembly, XPathNodeIterator iterator)
TypeDefinition type = assembly.MainModule.GetType (fullname);

if (type == null) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not resolve type '{fullname}' specified in {_xmlDocumentLocation}", 2008));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context, $"Could not resolve type '{fullname}' specified in {_xmlDocumentLocation}", 2008, _xmlDocumentLocation));
continue;
}

Expand Down Expand Up @@ -163,7 +164,8 @@ void ProcessMethod (TypeDefinition type, XPathNodeIterator iterator)

MethodDefinition method = FindMethod (type, signature);
if (method == null) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not find method '{signature}' in type '{type.FullName}' specified in {_xmlDocumentLocation}", 2009));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context,
$"Could not find method '{signature}' in type '{type.FullName}' specified in {_xmlDocumentLocation}", 2009, _xmlDocumentLocation));
return;
}

Expand All @@ -176,7 +178,7 @@ void ProcessMethod (TypeDefinition type, XPathNodeIterator iterator)
string value = GetAttribute (iterator.Current, "value");
if (value != "") {
if (!TryConvertValue (value, method.ReturnType, out object res)) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Invalid value for '{signature}' stub", 2010));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context, $"Invalid value for '{signature}' stub", 2010, _xmlDocumentLocation));
return;
}

Expand All @@ -186,7 +188,7 @@ void ProcessMethod (TypeDefinition type, XPathNodeIterator iterator)
Annotations.SetAction (method, MethodAction.ConvertToStub);
return;
default:
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Unknown body modification '{action}' for '{signature}'", 2011));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context, $"Unknown body modification '{action}' for '{signature}'", 2011, _xmlDocumentLocation));
return;
}
}
Expand All @@ -199,22 +201,22 @@ void ProcessField (TypeDefinition type, XPathNodeIterator iterator)

var field = type.Fields.FirstOrDefault (f => f.Name == name);
if (field == null) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not find field '{name}' in type '{type.FullName}' specified in { _xmlDocumentLocation}", 2012));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context, $"Could not find field '{name}' in type '{type.FullName}' specified in { _xmlDocumentLocation}", 2012, _xmlDocumentLocation));
return;
}

if (!field.IsStatic || field.IsLiteral) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Substituted field '{name}' needs to be static field.", 2013));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context, $"Substituted field '{name}' needs to be static field.", 2013, _xmlDocumentLocation));
return;
}

string value = GetAttribute (iterator.Current, "value");
if (string.IsNullOrEmpty (value)) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Missing 'value' attribute for field '{field}'.", 2014));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context, $"Missing 'value' attribute for field '{field}'.", 2014, _xmlDocumentLocation));
return;
}
if (!TryConvertValue (value, field.FieldType, out object res)) {
Context.LogMessage (MessageContainer.CreateWarningMessage ($"Invalid value for '{field}': '{value}'.", 2015));
Context.LogMessage (MessageContainer.CreateWarningMessage (Context, $"Invalid value for '{field}': '{value}'.", 2015, _xmlDocumentLocation));
return;
}

Expand Down
15 changes: 10 additions & 5 deletions src/tools/illink/src/linker/Linker.Steps/MarkStep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ protected virtual void MarkUserDependency (MemberReference context, CustomAttrib
if (args.Count >= 3 && args[2].Value is string assemblyName) {
assembly = _context.GetLoadedAssembly (assemblyName);
if (assembly == null) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not resolve '{assemblyName}' assembly dependency specified in a `PreserveDependency` attribute that targets method '{context.FullName}'", 2003));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"Could not resolve '{assemblyName}' assembly dependency specified in a `PreserveDependency` attribute that targets method '{context.FullName}'", 2003, context.Resolve ()));
return;
}
} else {
Expand All @@ -620,7 +621,8 @@ protected virtual void MarkUserDependency (MemberReference context, CustomAttrib
td = (assembly ?? context.Module.Assembly).FindType (typeName);

if (td == null) {
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not resolve '{typeName}' type dependency specified in a `PreserveDependency` attribute that targets method '{context.FullName}'", 2004));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"Could not resolve '{typeName}' type dependency specified in a `PreserveDependency` attribute that targets method '{context.FullName}'", 2004, context.Resolve ()));
return;
}
} else {
Expand Down Expand Up @@ -653,7 +655,8 @@ protected virtual void MarkUserDependency (MemberReference context, CustomAttrib
if (MarkDependencyField (td, member, new DependencyInfo (DependencyKind.PreservedDependency, ca)))
return;

_context.LogMessage (MessageContainer.CreateWarningMessage ($"Could not resolve dependency member '{member}' declared in type '{td.FullName}' specified in a `PreserveDependency` attribute that targets method '{context.FullName}'", 2005));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context,
$"Could not resolve dependency member '{member}' declared in type '{td.FullName}' specified in a `PreserveDependency` attribute that targets method '{context.FullName}'", 2005, td));
}

bool MarkDependencyMethod (TypeDefinition type, string name, string[] signature, in DependencyInfo reason)
Expand Down Expand Up @@ -1442,6 +1445,8 @@ bool MarkSpecialCustomAttributeDependencies (CustomAttribute ca, ICustomAttribut
l.Parameters.Count == 1 && l.Parameters[0].ParameterType.IsTypeOf ("System", "Type"),
provider);
return true;
} else if (dt.Name == "UnconditionalSuppressMessageAttribute" && dt.Namespace == "System.Diagnostics.CodeAnalysis") {
_context.Suppressions.AddLocalSuppression (ca, provider);
}

return false;
Expand Down Expand Up @@ -1961,11 +1966,11 @@ void ApplyPreserveInfo (TypeDefinition type)
break;
case TypePreserve.Fields:
if (!MarkFields (type, true, new DependencyInfo (DependencyKind.TypePreserve, type), true))
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Type {type.FullName} has no fields to preserve", 2001));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context, $"Type {type.FullName} has no fields to preserve", 2001, type));
break;
case TypePreserve.Methods:
if (!MarkMethods (type, new DependencyInfo (DependencyKind.TypePreserve, type)))
_context.LogMessage (MessageContainer.CreateWarningMessage ($"Type {type.FullName} has no methods to preserve", 2002));
_context.LogMessage (MessageContainer.CreateWarningMessage (_context, $"Type {type.FullName} has no methods to preserve", 2002, type));
break;
}
}
Expand Down
Loading

0 comments on commit 4a60e5d

Please sign in to comment.