From bc8f02bd92a5dc3ea5d64610106273a849f7b3ba Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Thu, 8 Sep 2016 20:07:35 -0500 Subject: [PATCH] [generator] Invalidate automatically generated events that will have invalid name. (#69) Addresses https://bugzilla.xamarin.com/show_bug.cgi?id=40172 setOn123Listener(On123Listener) will generate event named 123, but that is not a valid C# name and will result in compilation error. So check that name, warn it, and do not generate event. --- tools/generator/InterfaceGen.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/generator/InterfaceGen.cs b/tools/generator/InterfaceGen.cs index 59f98b013cc..a25b5b58021 100644 --- a/tools/generator/InterfaceGen.cs +++ b/tools/generator/InterfaceGen.cs @@ -468,6 +468,10 @@ public void GenerateEventsOrPropertiesForListener (StreamWriter sw, string inden Report.Warning (0, Report.WarningInterfaceGen + 1, "empty event name in {0}.{1}.", FullName, method.Name); continue; } + if (opt.GetSafeIdentifier (name) != name) { + Report.Warning (0, Report.WarningInterfaceGen + 4, "event name for {0}.{1} is invalid. `eventName' or `argsType` can be used to assign a valid member name.", FullName, method.Name); + continue; + } var prop = target.Properties.FirstOrDefault (p => p.Setter == method); if (prop != null) { string setter = "__Set" + prop.Name; @@ -523,6 +527,8 @@ public void GenerateEventOrProperty (StreamWriter sw, string indent, ClassGen ta void GenerateEventOrProperty (Method m, StreamWriter sw, string indent, ClassGen target, CodeGenerationOptions opt, string name, string connector_fmt, string add, string remove) { + if (m.EventName == string.Empty) + return; string nameSpec = Methods.Count > 1 ? m.AdjustedName : String.Empty; int idx = FullName.LastIndexOf ("."); int start = Name.StartsWith ("IOn") ? 3 : 1; @@ -534,9 +540,16 @@ void GenerateEventOrProperty (Method m, StreamWriter sw, string indent, ClassGen else full_delegate_name += "Handler"; if (m.RetVal.IsVoid || m.IsEventHandlerWithHandledProperty) { - if (m.EventName != string.Empty) + if (opt.GetSafeIdentifier (name) != name) { + Report.Warning (0, Report.WarningInterfaceGen + 5, "event name for {0}.{1} is invalid. `eventName' or `argsType` can be used to assign a valid member name.", FullName, name); + return; + } else GenerateEvent (sw, indent, opt, name, nameSpec, m.AdjustedName, full_delegate_name, !m.Parameters.HasSender, connector_fmt, add, remove); } else { + if (opt.GetSafeIdentifier (name) != name) { + Report.Warning (0, Report.WarningInterfaceGen + 6, "event property name for {0}.{1} is invalid. `eventName' or `argsType` can be used to assign a valid member name.", FullName, name); + return; + } sw.WriteLine ("{0}WeakReference weak_implementor_{1};", indent, name); sw.WriteLine ("{0}{1}Implementor Impl{2} {{", indent, opt.GetOutputName (FullName), name); sw.WriteLine ("{0}\tget {{", indent);