Author: nestor
Date: 2007-08-31 09:57:19 -0400 (Fri, 31 Aug 2007)
New Revision: 85104
Added:
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedParametersRule.cs
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedParametersTest.cs
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Makefile.am
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
Log:
Merging some work of the summer of code 2007. Added a new rule called
AvoidUnusedParametersRule.
* AvoidUnusedParametersRule.cs: New rule to check for unused parameters
in a method.
* Makefile.am: Added the sources of the rule and the unit tests.
* Test/AvoidUnusedParametersTest.cs: New. Unit tests for
AvoidUnusedParametersRule.
Added:
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedParametersRule.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedParametersRule.cs
2007-08-31 13:50:46 UTC (rev 85103)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedParametersRule.cs
2007-08-31 13:57:19 UTC (rev 85104)
@@ -0,0 +1,130 @@
+//
+// Gendarme.Rules.Performance.AvoidUnusedParameters class
+//
+// Authors:
+// Néstor Salceda <[EMAIL PROTECTED]>
+//
+// (C) 2007 Néstor Salceda
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.Performance {
+
+ public class AvoidUnusedParametersRule : IMethodRule {
+
+ private bool UseParameter (MethodDefinition method,
ParameterDefinition parameter)
+ {
+ foreach (Instruction instruction in
method.Body.Instructions) {
+ switch (instruction.OpCode.Code) {
+ case Code.Ldarg_0:
+ if (method.Parameters.IndexOf
(parameter) == 0)
+ return true;
+ break;
+ case Code.Ldarg_1:
+ if (method.Parameters.IndexOf
(parameter) == (method.IsStatic? 1 : 0))
+ return true;
+ break;
+ case Code.Ldarg_2:
+ if (method.Parameters.IndexOf
(parameter) == (method.IsStatic? 2 : 1))
+ return true;
+ break;
+ case Code.Ldarg_3:
+ if (method.Parameters.IndexOf
(parameter) == (method.IsStatic? 3 : 2))
+ return true;
+ break;
+ case Code.Ldarg_S:
+ if (instruction.Operand.Equals
(parameter))
+ return true;
+ break;
+ default:
+ break;
+ }
+ }
+ return false;
+ }
+
+ private bool ContainsReferenceDelegateInstructionFor
(MethodDefinition method, MethodDefinition delegateMethod)
+ {
+ if (method.HasBody) {
+ foreach (Instruction instruction in
method.Body.Instructions) {
+ if (instruction.OpCode.Code ==
Code.Ldftn)
+ return
instruction.Operand.Equals (delegateMethod);
+ }
+ }
+ return false;
+ }
+
+ private bool IsReferencedByDelegate (MethodDefinition
delegateMethod)
+ {
+ if (delegateMethod.DeclaringType is TypeDefinition) {
+ TypeDefinition type = (TypeDefinition)
delegateMethod.DeclaringType;
+ foreach (MethodDefinition method in
type.Methods) {
+ if
(ContainsReferenceDelegateInstructionFor (method, delegateMethod))
+ return true;
+ }
+
+ foreach (MethodDefinition method in
type.Constructors) {
+ if
(ContainsReferenceDelegateInstructionFor (method, delegateMethod))
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private bool IsExaminable (MethodDefinition method)
+ {
+ return !(method.IsAbstract || method.IsVirtual ||
method.Overrides.Count != 0
+ || method.PInvokeInfo != null ||
IsReferencedByDelegate (method));
+ }
+
+ private ICollection GetUnusedParameters (MethodDefinition
method)
+ {
+ ArrayList unusedParameters = new ArrayList ();
+ if (IsExaminable (method)) {
+ foreach (ParameterDefinition parameter in
method.Parameters) {
+ if (!UseParameter (method, parameter))
+ unusedParameters.Add
(parameter);
+ }
+ }
+ return unusedParameters;
+ }
+
+ public MessageCollection CheckMethod (MethodDefinition method,
Runner runner)
+ {
+ MessageCollection messageCollection = new
MessageCollection ();
+ foreach (ParameterDefinition parameter in
GetUnusedParameters (method)) {
+ Location location = new Location
(method.DeclaringType.Name, method.Name, 0);
+ Message message = new Message (String.Format
("The parameter {0} is never used.", parameter.Name),location,
MessageType.Error);
+ messageCollection.Add (message);
+ }
+ if (messageCollection.Count == 0)
+ return null;
+ return messageCollection;
+ }
+ }
+}
Property changes on:
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/AvoidUnusedParametersRule.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
2007-08-31 13:50:46 UTC (rev 85103)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/ChangeLog
2007-08-31 13:57:19 UTC (rev 85104)
@@ -1,3 +1,9 @@
+2007-09-31 Nestor Salceda <[EMAIL PROTECTED]>
+
+ * AvoidUnusedParametersRule.cs: New rule to check for unused parameters
+ in a method.
+ * Makefile.am: Added the sources of the rule and the unit tests.
+
2006-10-15 Sebastien Pouliot <[EMAIL PROTECTED]>
* Gendarme.Rules.Performance.xml.in: Update Uri for rules (to generate
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Makefile.am
2007-08-31 13:50:46 UTC (rev 85103)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Makefile.am
2007-08-31 13:57:19 UTC (rev 85104)
@@ -10,7 +10,7 @@
performance_rules_sources_in = ../../AssemblyInfo.cs.in
performance_rules_generated_sources = $(performance_rules_sources_in:.in=)
performance_rules_sources = EmptyDestructorRule.cs
IDisposableWithDestructorWithoutSuppressFinalizeRule.cs \
- UseStringEmptyRule.cs
+ UseStringEmptyRule.cs AvoidUnusedParametersRule.cs
performance_rules_build_sources = $(addprefix $(srcdir)/,
$(performance_rules_sources))
performance_rules_build_sources += $(performance_rules_generated_sources)
@@ -33,7 +33,7 @@
done;
performance_test_sources = EmptyDestructorTest.cs
IDisposableWithDestructorWithoutSuppressFinalizeTest.cs \
- UseStringEmptyTest.cs
+ UseStringEmptyTest.cs AvoidUnusedParametersTest.cs
performance_test_build_sources = $(addprefix $(srcdir)/Test/,
$(performance_test_sources))
Test.Rules.Performance.dll: $(performance_rules_build_sources)
Added:
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedParametersTest.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedParametersTest.cs
2007-08-31 13:50:46 UTC (rev 85103)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedParametersTest.cs
2007-08-31 13:57:19 UTC (rev 85104)
@@ -0,0 +1,283 @@
+//
+// Unit Test for AvoidUnusedParameters Rule.
+//
+// Authors:
+// Néstor Salceda <[EMAIL PROTECTED]>
+//
+// (C) 2007 Néstor Salceda
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections;
+using System.Reflection;
+using System.Runtime.InteropServices;
+
+using Mono.Cecil;
+using NUnit.Framework;
+using Gendarme.Framework;
+using Gendarme.Rules.Performance;
+
+namespace Test.Rules.Performance {
+
+ public abstract class AbstractClass {
+ public abstract void AbstractMethod (int x);
+ }
+
+ public class VirtualClass {
+ public virtual void VirtualMethod (int x)
+ {
+ }
+ }
+
+ public class OverrideClass : VirtualClass {
+ public override void VirtualMethod (int x)
+ {
+ }
+ }
+
+ [TestFixture]
+ public class AvoidUnusedParametersTest {
+
+ private IMethodRule rule;
+ private AssemblyDefinition assembly;
+ private MethodDefinition method;
+ private MessageCollection messageCollection;
+
+ public void PrintBannerUsingParameter (Version version)
+ {
+ Console.WriteLine ("Welcome to the foo program {0}",
version);
+ }
+
+ public void PrintBannerUsingAssembly (Version version)
+ {
+ Console.WriteLine ("Welcome to the foo program {0}",
Assembly.GetExecutingAssembly ().GetName ().Version);
+ }
+
+ public void PrintBannerWithoutParameters ()
+ {
+ Console.WriteLine ("Welcome to the foo program {0}",
Assembly.GetExecutingAssembly ().GetName ().Version);
+ }
+
+ public void MethodWithUnusedParameters (IEnumerable enumerable,
int x)
+ {
+ Console.WriteLine ("Method with unused parameters");
+ }
+
+ public void MethodWith5UsedParameters (int x, IEnumerable
enumerable, string foo, char c, float f)
+ {
+ Console.WriteLine (f);
+ Console.WriteLine (c);
+ Console.WriteLine (foo);
+ Console.WriteLine (enumerable);
+ Console.WriteLine (x);
+ }
+
+ public static void StaticMethodWithUnusedParameters (int x,
string foo)
+ {
+ }
+
+ public static void StaticMethodWithUsedParameters (int x,
string foo)
+ {
+ Console.WriteLine (x);
+ Console.WriteLine (foo);
+ }
+
+ public static void StaticMethodWith5UsedParameters (int x,
string foo, IEnumerable enumerable, char c, float f)
+ {
+ Console.WriteLine (f);
+ Console.WriteLine (c);
+ Console.WriteLine (foo);
+ Console.WriteLine (enumerable);
+ Console.WriteLine (x);
+ }
+
+ public static void StaticMethodWith5UnusedParameters (int x,
string foo, IEnumerable enumerable, char c, float f)
+ {
+ }
+
+ [DllImport ("libc.so")]
+ private static extern double cos (double x);
+
+ public delegate void SimpleCallback (int x);
+ public void SimpleCallbackImpl (int x)
+ {
+ }
+
+ public delegate void SimpleEventHandler (int x);
+ public event SimpleEventHandler SimpleEvent;
+ public void OnSimpleEvent (int x)
+ {
+ }
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ rule = new AvoidUnusedParametersRule ();
+ messageCollection = null;
+ }
+
+ [Test]
+ public void PrintBannerUsingParameterTest ()
+ {
+ method = GetMethodForTest ("PrintBannerUsingParameter");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void PrintBannerUsingAssemblyTest ()
+ {
+ method = GetMethodForTest ("PrintBannerUsingAssembly");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (1, messageCollection.Count);
+ }
+
+ [Test]
+ public void PrintBannerWithoutParametersTest ()
+ {
+ method = GetMethodForTest
("PrintBannerWithoutParameters");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void MethodWithUnusedParametersTest ()
+ {
+ method = GetMethodForTest
("MethodWithUnusedParameters");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (2, messageCollection.Count);
+ }
+
+ [Test]
+ public void AbstractMethodTest ()
+ {
+ method = GetMethodForTestFrom
("Test.Rules.Performance.AbstractClass", "AbstractMethod");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void VirtualMethodTest ()
+ {
+ method = GetMethodForTestFrom
("Test.Rules.Performance.VirtualClass", "VirtualMethod");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void OverrideMethodTest ()
+ {
+ method = GetMethodForTestFrom
("Test.Rules.Performance.OverrideClass", "VirtualMethod");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void ExternMethodTest ()
+ {
+ method = GetMethodForTest ("cos");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void DelegateMethodTest ()
+ {
+ SimpleCallback callback = new SimpleCallback
(SimpleCallbackImpl);
+ method = GetMethodForTest ("SimpleCallbackImpl");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void EventTest ()
+ {
+ SimpleEvent += new SimpleEventHandler (OnSimpleEvent);
+ method = GetMethodForTest ("OnSimpleEvent");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void MethodWith5UsedParametersTest ()
+ {
+ method = GetMethodForTest ("MethodWith5UsedParameters");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void StaticMethodWithUnusedParametersTest ()
+ {
+ method = GetMethodForTest
("StaticMethodWithUnusedParameters");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (2, messageCollection.Count);
+ }
+
+ [Test]
+ public void StaticMethodWithUsedParametersTest ()
+ {
+ method = GetMethodForTest
("StaticMethodWithUsedParameters");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void StaticMethodWith5UsedParametersTest ()
+ {
+ method = GetMethodForTest
("StaticMethodWith5UsedParameters");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNull (messageCollection);
+ }
+
+ [Test]
+ public void StaticMethodWith5UnusedParametersTest ()
+ {
+ method = GetMethodForTest
("StaticMethodWith5UnusedParameters");
+ messageCollection = rule.CheckMethod (method, new
MinimalRunner ());
+ Assert.IsNotNull (messageCollection);
+ Assert.AreEqual (5, messageCollection.Count);
+ }
+
+ private MethodDefinition GetMethodForTest (string methodName)
+ {
+ return GetMethodForTestFrom
("Test.Rules.Performance.AvoidUnusedParametersTest", methodName);
+ }
+
+ private MethodDefinition GetMethodForTestFrom (string
fullTypeName, string methodName)
+ {
+ TypeDefinition type =
assembly.MainModule.Types[fullTypeName];
+ if (type != null) {
+ foreach (MethodDefinition method in
type.Methods) {
+ if (method.Name == methodName)
+ return method;
+ }
+ }
+ return null;
+ }
+ }
+}
Property changes on:
trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnusedParametersTest.cs
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
2007-08-31 13:50:46 UTC (rev 85103)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
2007-08-31 13:57:19 UTC (rev 85104)
@@ -1,3 +1,8 @@
+2007-09-31 Nestor Salceda <[EMAIL PROTECTED]>
+
+ * AvoidUnusedParametersTest.cs: New. Unit tests for
+ AvoidUnusedParametersRule.
+
2006-09-28 Sebastien Pouliot <[EMAIL PROTECTED]>
* Makefile.am: Adjust EXTRA_DIST to include project files in tarballs.
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches