Author: spouliot
Date: 2008-02-17 16:35:47 -0500 (Sun, 17 Feb 2008)
New Revision: 96003

Added:
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/BasicIgnoreList.cs
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/IIgnoreList.cs
Modified:
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/IRule.cs
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/Rule.cs
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs
Log:
2008-02-17  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * BasicIgnoreList.cs: Basic implementation, with storage, for
        keeping an ignore list.
        * IIgnoreList.cs: New. Interface to ignorance ;-)
        * IRule.cs: Add FullName.
        * Rule.cs: Implement FullName using the type full name.
        * Runner.cs: Add support to ignore some known defects (or false 
        positives).
        * TestRunner.cs: Implement IIgnoreList.



Added: trunk/mono-tools/gendarme/framework/Gendarme.Framework/BasicIgnoreList.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/BasicIgnoreList.cs   
2008-02-17 21:18:17 UTC (rev 96002)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/BasicIgnoreList.cs   
2008-02-17 21:35:47 UTC (rev 96003)
@@ -0,0 +1,210 @@
+//
+// BasicIgnoreList
+//
+// Authors:
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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.Generic;
+
+using Mono.Cecil;
+
+using Gendarme.Framework.Rocks;
+
+namespace Gendarme.Framework {
+
+       /// <summary>
+       /// Basic ignore list implementation.
+       /// </summary>
+       public class BasicIgnoreList : IIgnoreList {
+
+               class Metadata {
+                       List<AssemblyDefinition> assemblies;
+                       List<TypeDefinition> types;
+                       List<MethodDefinition> methods;
+
+                       public List<AssemblyDefinition> Assemblies {
+                               get {
+                                       if (assemblies == null)
+                                               assemblies = new 
List<AssemblyDefinition> ();
+                                       return assemblies;
+                               }
+                       }
+
+                       public List<TypeDefinition> Types {
+                               get {
+                                       if (types == null)
+                                               types = new 
List<TypeDefinition> ();
+                                       return types;
+                               }
+                       }
+
+                       public List<MethodDefinition> Methods {
+                               get {
+                                       if (methods == null)
+                                               methods = new 
List<MethodDefinition> ();
+                                       return methods;
+                               }
+                       }
+               }
+
+               private SortedList<string, Metadata> list;
+               private IRunner runner;
+
+               // note: we should keep statistics here
+               // e.g. # of times a rule is ignored because it's inactive
+               // e.g. # of times a rule is ignored because of users directives
+
+               public BasicIgnoreList (IRunner runner)
+               {
+                       this.runner = runner;
+               }
+
+               private Metadata GetMetadata (string rule, bool create)
+               {
+                       if (rule == null)
+                               throw new ArgumentNullException ("rule");
+
+                       if (list == null)
+                               list = new SortedList<string, Metadata> ();
+                       else if (list.ContainsKey (rule))
+                               return list [rule];
+
+                       if (!create)
+                               return null;
+
+                       // ensure rule exists
+                       foreach (IRule value in runner.Rules) {
+                               if (rule == value.FullName) {
+                                       Metadata meta = new Metadata ();
+                                       list.Add (rule, meta);
+                                       return meta;
+                               }
+                       }
+                       return null;
+               }
+
+               private void Add (string rule, AssemblyDefinition assembly)
+               {
+                       GetMetadata (rule, true).Assemblies.Add (assembly);
+               }
+
+               private void Add (string rule, TypeDefinition type)
+               {
+                       GetMetadata (rule, true).Types.Add (type);
+               }
+
+               private void Add (string rule, MethodDefinition method)
+               {
+                       GetMetadata (rule, true).Methods.Add (method);
+               }
+
+               protected bool AddAssembly (string rule, string assembly)
+               {
+                       foreach (AssemblyDefinition definition in 
runner.Assemblies) {
+                               if (definition.Name.FullName == assembly) {
+                                       Add (rule, definition);
+                                       return true;
+                               }
+                       }
+                       return false;
+               }
+
+               protected bool AddType (string rule, string type)
+               {
+                       foreach (AssemblyDefinition assembly in 
runner.Assemblies) {
+                               foreach (ModuleDefinition module in 
assembly.Modules) {
+                                       TypeDefinition definition = 
module.Types [type];
+                                       if (definition != null) {
+                                               Add (rule, definition);
+                                               return true;
+                                       }
+                               }
+                       }
+                       return false;
+               }
+
+               protected bool AddMethod (string rule, string method)
+               {
+                       foreach (AssemblyDefinition assembly in 
runner.Assemblies) {
+                               foreach (ModuleDefinition module in 
assembly.Modules) {
+                                       foreach (TypeDefinition type in 
module.Types) {
+                                               foreach (MethodDefinition 
definition in type.AllMethods ()) {
+                                                       if (method == 
definition.ToString ()) {
+                                                               Add (rule, 
definition);
+                                                               return true;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+                       return false;
+               }
+
+               private static bool CheckRule (IRule rule)
+               {
+                       return ((rule == null) || !rule.Active);
+               }
+
+               public bool IsIgnored (IRule rule, AssemblyDefinition assembly)
+               {
+                       if (CheckRule (rule))
+                               return true;
+
+                       Metadata data = GetMetadata (rule.FullName, false);
+                       if (data == null)
+                               return false;
+                       return data.Assemblies.Contains (assembly);
+               }
+
+               public bool IsIgnored (IRule rule, TypeDefinition type)
+               {
+                       if (CheckRule (rule))
+                               return true;
+
+                       Metadata data = GetMetadata (rule.FullName, false);
+                       if (data == null)
+                               return false;
+                       if (data.Types.Contains (type))
+                               return true;
+                       return (data.Assemblies.Contains 
(type.Module.Assembly));
+               }
+
+               public bool IsIgnored (IRule rule, MethodDefinition method)
+               {
+                       if (CheckRule (rule))
+                               return true;
+
+                       Metadata data = GetMetadata (rule.FullName, false);
+                       if (data == null)
+                               return false;
+                       if (data.Methods.Contains (method))
+                               return true;
+                       if (data.Types.Contains (method.DeclaringType.Resolve 
()))
+                               return true;
+                       return (data.Assemblies.Contains 
(method.DeclaringType.Module.Assembly));
+               }
+       }
+}

Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog    
2008-02-17 21:18:17 UTC (rev 96002)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog    
2008-02-17 21:35:47 UTC (rev 96003)
@@ -1,3 +1,14 @@
+2008-02-17  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * BasicIgnoreList.cs: Basic implementation, with storage, for
+       keeping an ignore list.
+       * IIgnoreList.cs: New. Interface to ignorance ;-)
+       * IRule.cs: Add FullName.
+       * Rule.cs: Implement FullName using the type full name.
+       * Runner.cs: Add support to ignore some known defects (or false 
+       positives).
+       * TestRunner.cs: Implement IIgnoreList.
+
 2008-02-16  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * AssemblyResolver.cs: Fix NRE when resolving some methods

Added: trunk/mono-tools/gendarme/framework/Gendarme.Framework/IIgnoreList.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/IIgnoreList.cs       
2008-02-17 21:18:17 UTC (rev 96002)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/IIgnoreList.cs       
2008-02-17 21:35:47 UTC (rev 96003)
@@ -0,0 +1,42 @@
+//
+// IIgnoreList interface
+//
+// Authors:
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// 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 Mono.Cecil;
+
+namespace Gendarme.Framework {
+
+       /// <summary>
+       /// This interface defines how to query the ignore list, not how to add 
items nor it's internal storage.
+       /// </summary>
+       public interface IIgnoreList {
+               bool IsIgnored (IRule rule, MethodDefinition method);
+               bool IsIgnored (IRule rule, TypeDefinition type);
+               bool IsIgnored (IRule rule, AssemblyDefinition assembly);
+       }
+}

Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/IRule.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/IRule.cs     
2008-02-17 21:18:17 UTC (rev 96002)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/IRule.cs     
2008-02-17 21:35:47 UTC (rev 96003)
@@ -47,11 +47,16 @@
                IRunner Runner { get; }
 
                /// <summary>
-               /// Unique name for the rule.
+               /// Short name for the rule.
                /// </summary>
                string Name { get; }
 
                /// <summary>
+               /// Unique name for the rule.
+               /// </summary>
+               string FullName { get; }
+
+               /// <summary>
                /// URI to the rule documentation.
                /// </summary>
                Uri Uri { get; }

Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/Rule.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/Rule.cs      
2008-02-17 21:18:17 UTC (rev 96002)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/Rule.cs      
2008-02-17 21:35:47 UTC (rev 96003)
@@ -60,7 +60,7 @@
                }
 
                /// <summary>
-               /// Return the name of the rule.
+               /// Return the short name of the rule.
                /// By default this returns the name of the current class.
                /// </summary>
                public virtual string Name {
@@ -71,6 +71,14 @@
                        }
                }
 
+               /// <summary>
+               /// Return the full name of the rule.
+               /// By default this returns the full name of the current class.
+               /// </summary>
+               public virtual string FullName {
+                       get { return GetType ().ToString (); }
+               }
+
                public virtual string Problem { 
                        get {
                                if (problem == null) {

Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs    
2008-02-17 21:18:17 UTC (rev 96002)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs    
2008-02-17 21:35:47 UTC (rev 96003)
@@ -27,7 +27,6 @@
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
-using System.IO;
 using System.Linq;
 using System.Runtime.InteropServices;
 
@@ -40,7 +39,6 @@
 
        abstract public class Runner : IRunner {
 
-               private Dictionary<IRule, string> ignore_list = new 
Dictionary<IRule, string> ();
                private Collection<Defect> defect_list = new Collection<Defect> 
();
 
                private Collection<IRule> rules = new Collection<IRule> ();
@@ -53,10 +51,10 @@
 
                private IRule currentRule;
                private IMetadataTokenProvider currentTarget;
-
+               private IIgnoreList ignoreList;
                private int defectCountBeforeCheck;
 
-               public event EventHandler<RunnerEventArgs> AnalyzeAssembly;     
// ??? ProcessAssembly ???
+               public event EventHandler<RunnerEventArgs> AnalyzeAssembly;
                public event EventHandler<RunnerEventArgs> AnalyzeModule;
                public event EventHandler<RunnerEventArgs> AnalyzeType;
                public event EventHandler<RunnerEventArgs> AnalyzeMethod;
@@ -71,10 +69,19 @@
                        set { currentTarget = value; }
                }
 
+               protected IIgnoreList IgnoreList {
+                       get {
+                               if (ignoreList == null)
+                                       throw new InvalidOperationException 
("No IgnoreList has been set for this runner.");
+                               return ignoreList;
+                       }
+                       set { ignoreList = value; }
+               }
+
                public Collection<IRule> Rules {
                        get { return rules; }
                }
-
+ 
                public Collection<AssemblyDefinition> Assemblies {
                        get { return assemblies; }
                }
@@ -88,7 +95,6 @@
                        set { verbose_level = value; }
                }
 
-
                // once every assembly are loaded *and* all the rules are known 
-> we initialized all rules.
                // this ensure that the list of assemblies is available at rule 
initialization time
                // which allows caching information and treating the assemblies 
as "a set"
@@ -98,11 +104,8 @@
                                try {
                                        assembly.MainModule.LoadSymbols ();
                                }
-                               catch (FileNotFoundException) {
-                                       // this happens if MDB is missing
-                               }
                                catch (TypeLoadException) {
-                                       // this happens if the 
Mono.Cecil.Mdb.dll assembly is not found
+                                       // this happens if a Mono.Cecil.Mdb.dll 
is not found
                                }
                                catch (COMException) {
                                        // this happens if a PDB is missing
@@ -126,40 +129,6 @@
                        method_rules = rules.OfType<IMethodRule> ();
                }
 
-               public bool IsIgnored (IRule rule, AssemblyDefinition assembly)
-               {
-                       if ((rule == null) || !rule.Active)
-                               return true;
-                       if (assembly == null)
-                               throw new ArgumentNullException ("assembly");
-
-                       return false; // (ignore_list.Contains (rule, 
method.ToString ()));
-               }
-
-               public bool IsIgnored (IRule rule, TypeDefinition type)
-               {
-                       if ((rule == null) || !rule.Active)
-                               return true;
-                       if (type == null)
-                               throw new ArgumentNullException ("type");
-
-                       // check for type itself (full match)
-                       // otherwise check for assembly
-                       return IsIgnored (rule, type.Module.Assembly);
-               }
-
-               public bool IsIgnored (IRule rule, MethodDefinition method)
-               {
-                       if ((rule == null) || !rule.Active)
-                               return true;
-                       if (method == null)
-                               throw new ArgumentNullException ("method");
-
-                       // check for method itself (full match)
-                       // otherwise check for its type
-                       return IsIgnored (rule, (method.DeclaringType as 
TypeDefinition));
-               }
-
                public void Report (Defect defect)
                {
                        if (defect == null)
@@ -217,7 +186,7 @@
                        OnEvent (AnalyzeAssembly, e);
 
                        foreach (IAssemblyRule rule in assembly_rules) {
-                               if (IsIgnored (rule, e.CurrentAssembly))
+                               if (IgnoreList.IsIgnored (rule, 
e.CurrentAssembly))
                                        continue;
                                currentRule = rule;
                                defectCountBeforeCheck = Defects.Count;
@@ -240,7 +209,7 @@
                        OnEvent (AnalyzeType, e);
 
                        foreach (ITypeRule rule in type_rules) {
-                               if (IsIgnored (rule, e.CurrentType))
+                               if (IgnoreList.IsIgnored (rule, e.CurrentType))
                                        continue;
                                currentRule = rule;
                                defectCountBeforeCheck = Defects.Count;
@@ -253,7 +222,7 @@
                        OnEvent (AnalyzeMethod, e);
 
                        foreach (IMethodRule rule in method_rules) {
-                               if (IsIgnored (rule, e.CurrentMethod))
+                               if (IgnoreList.IsIgnored (rule, 
e.CurrentMethod))
                                        continue;
 
                                currentRule = rule;

Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs        
2008-02-17 21:18:17 UTC (rev 96002)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs        
2008-02-17 21:35:47 UTC (rev 96003)
@@ -1,5 +1,5 @@
 // 
-// Gendarme.Framework.ITypeRule interface
+// Gendarme.Framework.TestRunner
 //
 // Authors:
 //     Sebastien Pouliot <[EMAIL PROTECTED]>
@@ -44,12 +44,13 @@
        /// before each Check[Assembly|Type|Method] calls so we can easily
        /// Assert on Defects.Count.
        /// </summary>
-       public class TestRunner : Runner {
+       public class TestRunner : Runner, IIgnoreList {
 
                public TestRunner (IRule rule)
                {
                        CurrentRule = rule;
                        CurrentRule.Initialize (this);
+                       IgnoreList = this;
                }
 
                private void PreCheck (IMetadataTokenProvider obj)
@@ -77,5 +78,20 @@
                        PreCheck (method);
                        return (CurrentRule as IMethodRule).CheckMethod 
(method);
                }
+
+               public bool IsIgnored (IRule rule, MethodDefinition method)
+               {
+                       return !rule.Active;
+               }
+
+               public bool IsIgnored (IRule rule, TypeDefinition type)
+               {
+                       return !rule.Active;
+               }
+
+               public bool IsIgnored (IRule rule, AssemblyDefinition assembly)
+               {
+                       return !rule.Active;
+               }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to