public RuleResult CheckMethod (MethodDefinition method) { // Check property getters/setters. In order to prevent the property from // being reported twice, setters are only checked if the property has no getter. PropertyDefinition property = method.IsProperty () ? method.GetPropertyByAccessor () : null; if (property != null) { // however do not exclude automatic properties (getter/setter marked a generated code) if ((method.IsSetter && property.GetMethod != null) || property.IsGeneratedCode ()) return RuleResult.DoesNotApply; if (!IsOkay (property.PropertyType, property.Name)) Runner.Report (property, Severity.Medium, Confidence.Normal); } else { // exclude generated code like webservices if (method.IsGeneratedCode ()) return RuleResult.DoesNotApply; // Check the method's parameters. if (method.HasParameters) CheckParameters (method); // Check the method's return type. if (!IsOkay (method.ReturnType, method.Name)) Runner.Report (method, Severity.Medium, Confidence.Normal); } return Runner.CurrentRuleResult; }
public RuleResult CheckMethod (MethodDefinition method) { //does not apply if method has no parameter, is a property, or a p/invoke if (!method.HasParameters || method.IsProperty () || method.IsPInvokeImpl) return RuleResult.DoesNotApply; //if this is a constructor or override, the method name is dependent if (method.IsConstructor || method.IsOverride ()) return RuleResult.DoesNotApply; ParameterDefinition p0 = method.Parameters [0]; string name = p0.ParameterType.Name; //param is out/ref, it is already not obvious (there is a rule for that) if (p0.IsOut || p0.IsRef ()) return RuleResult.DoesNotApply; string method_name = method.Name; if (name.Length == 1 || method_name.Length <= name.Length) return RuleResult.DoesNotApply; if ((method_name.Length - name.Length) < 4 && IsVaguePrefix (method_name)) //suggestion would be too vague anyway (Get/Set/Is) return RuleResult.DoesNotApply; if (!char.IsUpper (name [0])) //non-compliant naming, cannot go further (PascalWords needed) return RuleResult.DoesNotApply; //if the method return the parameter type it is most likely clearer to have it in the name if (method.ReturnType == p0.ParameterType) return RuleResult.Success; //if starting with name it is most likely on purpose if (method_name.StartsWith (name, StringComparison.Ordinal)) return RuleResult.Success; int pos = method_name.LastIndexOf (name); if (-1 == pos) return RuleResult.Success; Confidence confidence = Confidence.Normal; if (pos >= method_name.Length - name.Length) //suffix, most common and most verbose case confidence = Confidence.High; else if (!char.IsUpper (method_name [pos + name.Length])) //not the end of a 'PascalWord' return RuleResult.Success; //if IgnoreAlienNamespaces is True, then check if parameter type is from one of the analyzed namespaces if (IgnoreAlienNamespaces && IsTypeFromAlienNamespace (p0.ParameterType)) return RuleResult.Success; //ignored/out-of-reach, so this is a success //main goal is to keep the API as simple as possible so this is more severe for visible methods Severity severity = method.IsVisible () ? Severity.Medium : Severity.Low; string suggestion = GetSuggestionMethodName (method, name, pos); string msg; if (method.IsStatic) { //we already have a rule that checks if the method should be static string memberKind = GetSuggestionMemberKind (method); msg = String.Format ("Consider renaming method to '{2}', or extracting method to type '{0}' as {1} '{2}', or making an extension method of that type.", p0.ParameterType, memberKind, suggestion); } else { msg = String.Format ("Consider renaming method to '{0}'.", suggestion); } Runner.Report (method, severity, confidence, msg); return RuleResult.Failure; }
public RuleResult CheckMethod (MethodDefinition method) { if (!method.HasBody || !method.HasParameters || method.IsCompilerControlled) return RuleResult.DoesNotApply; if (method.IsProperty () || method.IsGeneratedCode () || method.IsEventCallback ()) return RuleResult.DoesNotApply; // we cannot change parameter types if: // - we're overriding a base virtual method; or // - they were specified by an interface if (IsSignatureDictated (method)) return RuleResult.DoesNotApply; int pcount = method.Parameters.Count; if (pcount > types_least.Length) { // that should be quite rare (does not happen for mono 2.0 class libs) types_least = new TypeReference [pcount]; depths_least = new int [pcount]; } CheckParameters (method); CheckParametersSpecializationDelta (method); Array.Clear (types_least, 0, types_least.Length); Array.Clear (depths_least, 0, depths_least.Length); return Runner.CurrentRuleResult; }
private void CheckMethodBody (MethodDefinition method) { var synchronizedEvents = new Dictionary<MethodReference, List<MethodReference>> (); var thisSynchronized = new List<TypeReference> (); foreach (Instruction ins in method.Body.Instructions) { MethodReference candidate = null; switch (ins.OpCode.Code) { case Code.Newobj: if (ins.Previous != null && ins.Previous.OpCode.Code == Code.Ldftn) { MethodReference ctor = (MethodReference) ins.Operand; TypeReference type = ctor.DeclaringType; if (type.IsDelegate ()) { string nspace = type.Namespace; // ldftn entry-point // newobj System.Void System.Threading.XXX::.ctor (System.Object,System.IntPtr) // i.e. creation of a System.Threading delegate if (nspace == "System.Threading") { string name = type.Name; if (name == "ThreadStart" || name == "ParameterizedThreadStart" || name == "WaitCallback" || name == "WaitOrTimerCallback" || name == "TimerCallback") { candidate = (MethodReference) ins.Previous.Operand; } // ldftn entry-point // newobj System.Void System.AsyncCallback::.ctor (System.Object,System.IntPtr) // i.e. creation of a async delegate } else if (nspace == "System") { if (type.Name == "AsyncCallback") { candidate = (MethodReference) ins.Previous.Operand; } // ldftn entry-point // newobj System.Void ThreadedDelegate::.ctor (System.Object,System.IntPtr) // i.e. creation of a delegate which is decorated with a threading attribute } else if (!ThreadRocks.ThreadedNamespace (nspace)) { // Delegates must be able to call the methods they are bound to. MethodDefinition target = ((MethodReference) ins.Previous.Operand).Resolve (); if (target != null) { ThreadModel callerModel = type.ThreadingModel (); if (!target.IsGeneratedCode () || target.IsProperty ()) { ThreadModel targetModel = target.ThreadingModel (); if (!IsValidCall (callerModel, targetModel)) { string mesg = string.Format ("{0} delegate cannot be bound to {1} {2} method.", callerModel, targetModel, target.Name); ++DefectCount; Log.WriteLine (this, "Defect: {0}", mesg); Defect defect = new Defect (this, method, method, ins, Severity.High, Confidence.High, mesg); Runner.Report (defect); } } else if (!callerModel.Is (ThreadModel.MainThread)) { anonymous_entry_points.Add (target); } } } } } break; case Code.Call: case Code.Callvirt: if (!method.IsGeneratedCode () || method.IsProperty ()) CheckForLegalCall (method, ins); // ldftn entry-point // newobj XXX // callvirt System.Void SynchronizedType::add_Name (XXX) // i.e. adding a delegate to an event in a type which uses SynchronizingObject MethodReference call = (MethodReference) ins.Operand; TypeReference call_type = call.DeclaringType; if (ins.Previous.Is (Code.Newobj) && ins.Previous.Previous.Is (Code.Ldftn)) { // A few events are blacklisted because they do not use SynchronizingObject and // are therefore always threaded. if (IsNonSynchronizedSetter (call)) { candidate = (MethodReference) ins.Previous.Previous.Operand; // But most events do use SynchronizingObject and therefore their threading // depends on whether and how SynchronizingObject is initialized. } else if (HasSynchronizingObject (call_type)) { List<MethodReference> methods; if (!synchronizedEvents.TryGetValue (call, out methods)) { methods = new List<MethodReference> (); synchronizedEvents.Add (call, methods); } methods.AddIfNew ((MethodReference) ins.Previous.Previous.Operand); // Misc threaded events. } else if (call_type.FullName == "System.ComponentModel.BackgroundWorker") { if (call.Name == "add_DoWork") { candidate = (MethodReference) ins.Previous.Previous.Operand; } } // callvirt System.Void System.Diagnostics.Process::set_SynchronizingObject (System.ComponentModel.ISynchronizeInvoke) } else if (SetSynchronizingObject.Matches (call)) { if (ins.Previous.OpCode.Code == Code.Ldarg_0) { thisSynchronized.Add (call_type); } } break; } if (candidate != null) { Log.WriteLine (this, "{0} is a thread entry point", candidate); CheckEntryPoint (candidate); } } // For every method added to a threaded event, ThreadModel? method_model = null; foreach (KeyValuePair<MethodReference, List<MethodReference>> entry in synchronizedEvents) { // if the event is synchronized on this then the target must have the same thread // as the current method's type or better and it should not be treated as a entry point. if (thisSynchronized.Contains (entry.Key.DeclaringType)) { if (method_model == null) method_model = method.DeclaringType.ThreadingModel (); foreach (MethodReference mr in entry.Value) { MethodDefinition target = mr.Resolve (); if (target != null) { ThreadModel targetModel = target.ThreadingModel (); if (!IsValidCall (method_model.Value, targetModel)) { string mesg = string.Format ("{0} {1} cannot be bound to {2} {3} method.", method_model, entry.Key, targetModel, target.Name); ReportDefect (method, Severity.High, Confidence.High, mesg); } } } // otherwise the method has to be treated as a thread entry point. } else { foreach (MethodReference mr in entry.Value) { Log.WriteLine (this, "{0} is a thread entry point", mr); CheckEntryPoint (mr); } } } }
// note: we need to be consistant with some stuff we propose in other rules private static bool CheckPublicMethod (MethodDefinition method) { // handle things like operators - but not properties if (method.IsSpecialName && !method.IsProperty ()) return true; // handle non-virtual Equals, e.g. Equals(type) string name = method.Name; if (method.HasParameters && (name == "Equals")) { IList<ParameterDefinition> pdc = method.Parameters; if ((pdc.Count == 1) && (pdc [0].ParameterType == method.DeclaringType)) return true; } // check if this method is needed to satisfy an interface TypeDefinition type = (method.DeclaringType as TypeDefinition); if (type.HasInterfaces) { foreach (TypeReference tr in type.Interfaces) { TypeDefinition intf = tr.Resolve (); if (intf != null) { foreach (MethodReference member in intf.Methods) { if (name == member.Name) return true; } } } } return false; }
private void Report (MethodDefinition method, Instruction ins, string name) { Severity severity = ((name == "value") && method.IsProperty () && !method.Name.EndsWith ("value", StringComparison.InvariantCultureIgnoreCase)) ? Severity.Low : Severity.High; Runner.Report (method, ins, severity, Confidence.Normal); }
// Method Visible Non-Visible // ------------------------------------------------ // Parameters High Medium // ReturnType High Medium // Variables Low Low // Method calls Medium* Low* // Fields access Medium* Low* // * target visibility public RuleResult CheckMethod(MethodDefinition method) { // [Obsolete] cannot be applied to property or event accessors if (method.IsProperty () || method.IsAddOn || method.IsRemoveOn || method.IsFire) return RuleResult.DoesNotApply; // if the method is obsolete (directly or because it's type is) if (method.HasAttribute ("System", "ObsoleteAttribute") || method.DeclaringType.HasAttribute ("System", "ObsoleteAttribute")) return RuleResult.DoesNotApply; // check method signature (parameters, return value) if (method.HasParameters) CheckParameters (method); CheckReturnType (method); // then check what the IL calls/access if (method.HasBody) { MethodBody body = method.Body; if (body.HasVariables) CheckVariables (method); foreach (Instruction ins in body.Instructions) { switch (ins.OpCode.Code) { case Code.Newarr: case Code.Newobj: case Code.Call: case Code.Callvirt: CheckMethodCall (method, ins, (ins.Operand as MethodReference)); break; case Code.Initobj: CheckTypeCreation (method, ins, (ins.Operand as TypeReference)); break; case Code.Ldfld: case Code.Ldflda: case Code.Ldsfld: case Code.Ldsflda: case Code.Stfld: case Code.Stsfld: CheckFieldAccess (method, ins, (ins.Operand as FieldReference)); break; } } } return Runner.CurrentRuleResult; }