From b33581a3606d458e38426494ca77cf27b141289a Mon Sep 17 00:00:00 2001 From: Martin Baulig Date: Tue, 30 Apr 2019 18:28:37 +0200 Subject: [PATCH] Add option to the XML input to rewrite methods to throw / stubs. When using `feature=` to conditionalize a ``, there is now a new `action=` attribute (which currently allows `throw` and `stub`). You can use it like this: Then you run the linker with `--exclude-feature sre` and that method will be rewritten into a throw. This is intended for scenarios where that method would otherwise bring in types that we want to exclude. --- src/linker/Linker.Steps/ResolveFromXmlStep.cs | 45 +++++++++++++++---- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/src/linker/Linker.Steps/ResolveFromXmlStep.cs b/src/linker/Linker.Steps/ResolveFromXmlStep.cs index faac4f749e93..d734afc72554 100644 --- a/src/linker/Linker.Steps/ResolveFromXmlStep.cs +++ b/src/linker/Linker.Steps/ResolveFromXmlStep.cs @@ -402,22 +402,44 @@ void ProcessMethods (TypeDefinition type, XPathNodeIterator iterator) protected virtual void ProcessMethod (TypeDefinition type, XPathNodeIterator iterator) { - if (IsExcluded (iterator.Current)) - return; + string action = null; + if (IsExcluded (iterator.Current)) { + action = GetAttribute (iterator.Current, "action"); + if (String.IsNullOrEmpty (action)) + return; + } string value = GetSignature (iterator.Current); if (!String.IsNullOrEmpty (value)) - ProcessMethodSignature (type, value); + ProcessMethodSignature (type, value, action); value = GetAttribute (iterator.Current, "name"); if (!String.IsNullOrEmpty (value)) - ProcessMethodName (type, value); + ProcessMethodName (type, value, action); } - void ProcessMethodSignature (TypeDefinition type, string signature) + void ProcessMethodSignature (TypeDefinition type, string signature, string action) { MethodDefinition meth = GetMethod (type, signature); - MarkMethod (type, meth, signature); + if (action == null) + MarkMethod (type, meth, signature); + else + MarkMethodAction (meth, action); + } + + void MarkMethodAction (MethodDefinition method, string action) + { + switch (action) { + case "throw": + Annotations.SetAction (method, MethodAction.ConvertToThrow); + break; + case "stub": + Annotations.SetAction (method, MethodAction.ConvertToStub); + break; + default: + Context.LogMessage ($"Unknown method action `{action}`."); + break; + } } void MarkMethod (TypeDefinition type, MethodDefinition method, string signature) @@ -447,14 +469,19 @@ void MarkMethodIfNotNull (MethodDefinition method) MarkMethod (method); } - void ProcessMethodName (TypeDefinition type, string name) + void ProcessMethodName (TypeDefinition type, string name, string action) { if (!type.HasMethods) return; - foreach (MethodDefinition method in type.Methods) - if (name == method.Name) + foreach (MethodDefinition method in type.Methods) { + if (name != method.Name) + continue; + if (action == null) MarkMethod (type, method, name); + else + MarkMethodAction (method, action); + } } protected static MethodDefinition GetMethod (TypeDefinition type, string signature)