Author: spouliot
Date: 2008-02-15 14:13:38 -0500 (Fri, 15 Feb 2008)
New Revision: 95791

Added:
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DoNotDestroyStackTraceRule.cs
Removed:
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
Modified:
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
   
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
   trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am
Log:
2008-02-15  Sebastien Pouliot  <[EMAIL PROTECTED]> 

        * DoNotDestroyStackTraceRule.cs: (renamed from DontDestroyStackTrace.cs)
        * DontSwallowErrorsCatchingNonspecificExceptionsRule.cs:
                Updated rules wrt framework changes.
        * Makefile.am: Adjusted for file name changes.



Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog 
2008-02-15 19:08:46 UTC (rev 95790)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog 
2008-02-15 19:13:38 UTC (rev 95791)
@@ -1,3 +1,10 @@
+2008-02-15  Sebastien Pouliot  <[EMAIL PROTECTED]> 
+
+       * DoNotDestroyStackTraceRule.cs: (renamed from DontDestroyStackTrace.cs)
+       * DontSwallowErrorsCatchingNonspecificExceptionsRule.cs:
+               Updated rules wrt framework changes.
+       * Makefile.am: Adjusted for file name changes.
+
 2008-01-11  Sebastien Pouliot  <[EMAIL PROTECTED]> 
 
        * DontDestroyStackTrace.cs: Use new Location ctors. Change some 

Copied: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DoNotDestroyStackTraceRule.cs
 (from rev 95783, 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs)
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
  2008-02-15 18:29:58 UTC (rev 95783)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DoNotDestroyStackTraceRule.cs
     2008-02-15 19:13:38 UTC (rev 95791)
@@ -0,0 +1,238 @@
+//
+// DoNotDestroyStackTraceRule
+//
+// 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 Mono.Cecil.Cil;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Exceptions.Impl;
+
+namespace Gendarme.Rules.Exceptions {
+
+       [Problem ("A catch block in the method throws back the caught exception 
which destroys the stack trace.")]
+       [Solution ("If you need to throw the exception caught by the catch 
block, use 'throw;' instead of 'throw ex;'")]
+       public class DontDestroyStackTrace : Rule, IMethodRule {
+
+               private TypeReference void_reference;
+               private List<int> warned_offsets_in_method = new List<int> ();
+
+               public RuleResult CheckMethod (MethodDefinition method)
+               {
+                       // rule only applies to methods with IL
+                       if (!method.HasBody)
+                               return RuleResult.DoesNotApply;
+
+                       ModuleDefinition module = method.DeclaringType.Module;
+                       if (void_reference == null)
+                               void_reference = module.Import (typeof (void));
+
+                       List<ExecutionPath> executionPaths = new 
List<ExecutionPath> ();
+                       ExecutionPathFactory epf = new ExecutionPathFactory 
(method);
+                       ISEHGuardedBlock[] guardedBlocks = 
ExceptionBlockParser.GetExceptionBlocks (method);
+                       foreach (ISEHGuardedBlock guardedBlock in 
guardedBlocks) {
+                               foreach (ISEHHandlerBlock handlerBlock in
+                                        guardedBlock.SEHHandlerBlocks) {
+                                       if (handlerBlock is SEHCatchBlock) {
+                                           ExecutionPath[] ret =
+                                               epf.CreatePaths 
(handlerBlock.Start,
+                                                                
handlerBlock.End);
+                                               executionPaths.AddRange (ret);
+                                       }
+                               }
+                       }
+
+                       warned_offsets_in_method.Clear ();
+
+                       // Look for paths that 'throw ex;' instead of 'throw'
+                       foreach (ExecutionPath catchPath in executionPaths)
+                               ProcessCatchPath (catchPath, method);
+
+                       return Runner.CurrentRuleResult;
+               }
+
+               private void ProcessCatchPath (ExecutionPath catchPath, 
MethodDefinition method)
+               {
+                       // Track original exception (top of stack at start) 
through to the final
+                       // return (be it throw, rethrow, leave, or leave.s)
+
+                       // Current stack position: 0 = top of stack
+                       int exStackPos = 0;
+                       // Local variable position: -1 = not stored in local 
variable
+                       int localVarPos = -1;
+
+                       foreach (ExecutionBlock block in catchPath) {
+                               Instruction cur = null;
+                               while (cur != block.Last) {
+                                       if (cur == null)
+                                               cur = block.First;
+                                       else
+                                               cur = cur.Next;
+
+                                       if (cur.OpCode == OpCodes.Rethrow)
+                                               // Rethrown exception - no 
problem!
+                                               return;
+
+                                       if (cur.OpCode == OpCodes.Stloc ||
+                                          cur.OpCode == OpCodes.Stloc_0 ||
+                                          cur.OpCode == OpCodes.Stloc_1 ||
+                                          cur.OpCode == OpCodes.Stloc_2 ||
+                                          cur.OpCode == OpCodes.Stloc_3 ||
+                                          cur.OpCode == OpCodes.Stloc_S) {
+
+                                               int varIndex = GetVarIndex 
(cur);
+                                               if (exStackPos == 0)
+                                               {
+                                                       // Storing argument on 
top of stack in local variable reference
+                                                       localVarPos = varIndex;
+                                                       exStackPos = -1;
+                                               } else if (localVarPos != -1 && 
varIndex == localVarPos)
+                                                       // Writing over orignal 
exception...
+                                                       localVarPos = -1;
+                                       } else if (localVarPos != -1 &&
+                                                  (cur.OpCode == OpCodes.Ldloc 
||
+                                                   cur.OpCode == 
OpCodes.Ldloc_0 ||
+                                                   cur.OpCode == 
OpCodes.Ldloc_1 ||
+                                                   cur.OpCode == 
OpCodes.Ldloc_2 ||
+                                                   cur.OpCode == 
OpCodes.Ldloc_3 ||
+                                                   cur.OpCode == 
OpCodes.Ldloc_S)) {
+
+                                               int varIndex = GetVarIndex 
(cur);
+                                               if (varIndex == localVarPos)
+                                                       // Loading exception 
from local var back onto stack
+                                                       exStackPos = 0;
+                                       } else if (cur.OpCode == OpCodes.Throw 
&& exStackPos == 0) {
+                                               // If our original exception is 
on top of the stack,
+                                               // we're rethrowing it.This is 
deemed naughty...
+                                               if 
(!warned_offsets_in_method.Contains(cur.Offset)) {
+                                                       Runner.Report (method, 
cur, Severity.Critical, Confidence.High, String.Empty);
+                                                       
warned_offsets_in_method.Add (cur.Offset);
+                                               }
+                                               return;
+                                       } else if (exStackPos != -1) {
+                                               // If we're still on the stack, 
track our position after
+                                               // this instruction
+                                               int numPops = GetNumPops (cur);
+                                               if (exStackPos < numPops) {
+                                                       // Popped ex off of 
stack
+                                                       exStackPos = -1;
+                                               } else {
+                                                       int numPushes = 
GetNumPushes (cur);
+                                                       exStackPos += numPushes 
- numPops;
+                                               }
+                                       }
+                               }
+                       }
+               }
+
+               private static int GetNumPops (Instruction instr)
+               {
+                       switch (instr.OpCode.StackBehaviourPop) {
+                       case StackBehaviour.Pop0:
+                               return 0;
+                       case StackBehaviour.Pop1:
+                       case StackBehaviour.Popi:
+                       case StackBehaviour.Popref:
+                               return 1;
+                       case StackBehaviour.Pop1_pop1:
+                       case StackBehaviour.Popi_pop1:
+                       case StackBehaviour.Popi_popi:
+                       case StackBehaviour.Popi_popi8:
+                       case StackBehaviour.Popi_popr4:
+                       case StackBehaviour.Popi_popr8:
+                       case StackBehaviour.Popref_pop1:
+                       case StackBehaviour.Popref_popi:
+                               return 2;
+                       case StackBehaviour.Popi_popi_popi:
+                       case StackBehaviour.Popref_popi_popi:
+                       case StackBehaviour.Popref_popi_popi8:
+                       case StackBehaviour.Popref_popi_popr4:
+                       case StackBehaviour.Popref_popi_popr8:
+                       case StackBehaviour.Popref_popi_popref:
+                               return 3;
+                       case StackBehaviour.Varpop:
+                               if(instr.Operand is MethodReference) {
+                                       // We have to determine from the call 
how many arguments will
+                                       // be popped from the stack
+                                       MethodReference callMethod = 
(MethodReference)instr.Operand;
+                                       return callMethod.Parameters.Count;
+                               } else {
+                                       throw new 
InvalidOperationException("Unexpected instruction: '" +
+                                       instr.OpCode.ToString() + "' at offset 
0x" +
+                                       instr.Offset.ToString("X"));
+                               }
+                       }
+
+                       return 0;
+               }
+
+               private int GetNumPushes (Instruction instr)
+               {
+                       switch (instr.OpCode.StackBehaviourPush) {
+                       case StackBehaviour.Push0:
+                               return 0;
+                       case StackBehaviour.Push1:
+                       case StackBehaviour.Pushi:
+                       case StackBehaviour.Pushi8:
+                       case StackBehaviour.Pushr4:
+                       case StackBehaviour.Pushr8:
+                       case StackBehaviour.Pushref:
+                               return 1;
+                       case StackBehaviour.Push1_push1:
+                               return 2;
+                       case StackBehaviour.Varpush:
+                               // We have to determine from the call how many 
arguments will
+                               // be pushed onto the stack
+                               MethodReference callMethod = 
(MethodReference)instr.Operand;
+                               return (callMethod.ReturnType.ReturnType == 
void_reference) ?
+                                       0 : 1;
+                       }
+
+                       return 0;
+               }
+
+               private static int GetVarIndex (Instruction instr)
+               {
+                       if (instr.OpCode == OpCodes.Stloc_0 || instr.OpCode == 
OpCodes.Ldloc_0)
+                               return 0;
+                       else if (instr.OpCode == OpCodes.Stloc_1 || 
instr.OpCode == OpCodes.Ldloc_1)
+                               return 1;
+                       else if (instr.OpCode == OpCodes.Stloc_2 || 
instr.OpCode == OpCodes.Ldloc_2)
+                               return 2;
+                       else if (instr.OpCode == OpCodes.Stloc_3 || 
instr.OpCode == OpCodes.Ldloc_3)
+                               return 3;
+                       else if (instr.OpCode == OpCodes.Stloc_S || 
instr.OpCode == OpCodes.Stloc ||
+                               instr.OpCode == OpCodes.Ldloc_S || instr.OpCode 
== OpCodes.Ldloc)
+                       {
+                               VariableDefinition varDef = 
(VariableDefinition)instr.Operand;
+                               return varDef.Index;
+                       }
+
+                       return -1;
+               }
+       }
+}

Deleted: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
  2008-02-15 19:08:46 UTC (rev 95790)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
  2008-02-15 19:13:38 UTC (rev 95791)
@@ -1,218 +0,0 @@
-using System;
-using System.Collections;
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-using Gendarme.Framework;
-using Gendarme.Rules.Exceptions.Impl;
-
-namespace Gendarme.Rules.Exceptions {
-
-       public class DontDestroyStackTrace : IMethodRule {
-
-               private TypeReference void_reference;
-               private ArrayList warned_offsets_in_method;
-
-               public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
-               {
-                       if (!method.HasBody)
-                               return runner.RuleSuccess;
-
-                       ModuleDefinition module = method.DeclaringType.Module;
-                       if (void_reference == null)
-                               void_reference = module.Import (typeof (void));
-
-                       ArrayList executionPaths = new ArrayList ();
-                       ExecutionPathFactory epf = new ExecutionPathFactory 
(method);
-                       ISEHGuardedBlock[] guardedBlocks = 
ExceptionBlockParser.GetExceptionBlocks (method);
-                       foreach (ISEHGuardedBlock guardedBlock in 
guardedBlocks) {
-                               foreach (ISEHHandlerBlock handlerBlock in
-                                        guardedBlock.SEHHandlerBlocks) {
-                                       if (handlerBlock is SEHCatchBlock) {
-                                           ExecutionPath[] ret =
-                                               epf.CreatePaths 
(handlerBlock.Start,
-                                                                
handlerBlock.End);
-                                               executionPaths.AddRange (ret);
-                                       }
-                               }
-                       }
-
-                       MessageCollection violations = new MessageCollection ();
-                       warned_offsets_in_method = new ArrayList ();
-
-                       // Look for paths that 'throw ex;' instead of 'throw'
-                       foreach (ExecutionPath catchPath in executionPaths)
-                               ProcessCatchPath (catchPath, method, 
violations);
-
-                       return (violations.Count == 0) ? null : violations;
-               }
-
-               private void ProcessCatchPath (ExecutionPath catchPath,
-                                              MethodDefinition method,
-                                              MessageCollection violations)
-               {
-                       // Track original exception (top of stack at start) 
through to the final
-                       // return (be it throw, rethrow, leave, or leave.s)
-
-                       // Current stack position: 0 = top of stack
-                       int exStackPos = 0;
-                       // Local variable position: -1 = not stored in local 
variable
-                       int localVarPos = -1;
-
-                       foreach (ExecutionBlock block in catchPath) {
-                               Instruction cur = null;
-                               while (cur != block.Last) {
-                                       if (cur == null)
-                                               cur = block.First;
-                                       else
-                                               cur = cur.Next;
-
-                                       if (cur.OpCode == OpCodes.Rethrow)
-                                               // Rethrown exception - no 
problem!
-                                               return;
-
-                                       if (cur.OpCode == OpCodes.Stloc ||
-                                          cur.OpCode == OpCodes.Stloc_0 ||
-                                          cur.OpCode == OpCodes.Stloc_1 ||
-                                          cur.OpCode == OpCodes.Stloc_2 ||
-                                          cur.OpCode == OpCodes.Stloc_3 ||
-                                          cur.OpCode == OpCodes.Stloc_S) {
-
-                                               int varIndex = GetVarIndex 
(cur);
-                                               if (exStackPos == 0)
-                                               {
-                                                       // Storing argument on 
top of stack in local variable reference
-                                                       localVarPos = varIndex;
-                                                       exStackPos = -1;
-                                               } else if (localVarPos != -1 && 
varIndex == localVarPos)
-                                                       // Writing over orignal 
exception...
-                                                       localVarPos = -1;
-                                       } else if (localVarPos != -1 &&
-                                                  (cur.OpCode == OpCodes.Ldloc 
||
-                                                   cur.OpCode == 
OpCodes.Ldloc_0 ||
-                                                   cur.OpCode == 
OpCodes.Ldloc_1 ||
-                                                   cur.OpCode == 
OpCodes.Ldloc_2 ||
-                                                   cur.OpCode == 
OpCodes.Ldloc_3 ||
-                                                   cur.OpCode == 
OpCodes.Ldloc_S)) {
-
-                                               int varIndex = GetVarIndex 
(cur);
-                                               if (varIndex == localVarPos)
-                                                       // Loading exception 
from local var back onto stack
-                                                       exStackPos = 0;
-                                       } else if (cur.OpCode == OpCodes.Throw 
&& exStackPos == 0) {
-                                               // If our original exception is 
on top of the stack,
-                                               // we're rethrowing it.This is 
deemed naughty...
-                                               if 
(!warned_offsets_in_method.Contains(cur.Offset)) {
-                                                       Location loc =
-                                                               new Location 
(method, cur.Offset);
-                                                       Message msg = new 
Message (
-                                                               "Throwing 
original exception - destroys stack trace!",
-                                                               loc,
-                                                               
MessageType.Error);
-                                                       violations.Add (msg);
-                                                       
warned_offsets_in_method.Add (cur.Offset);
-                                               }
-                                               return;
-                                       } else if (exStackPos != -1) {
-                                               // If we're still on the stack, 
track our position after
-                                               // this instruction
-                                               int numPops = GetNumPops (cur);
-                                               if (exStackPos < numPops) {
-                                                       // Popped ex off of 
stack
-                                                       exStackPos = -1;
-                                               } else {
-                                                       int numPushes = 
GetNumPushes (cur);
-                                                       exStackPos += numPushes 
- numPops;
-                                               }
-                                       }
-                               }
-                       }
-                       return;
-               }
-
-               private static int GetNumPops (Instruction instr)
-               {
-                       switch (instr.OpCode.StackBehaviourPop) {
-                       case StackBehaviour.Pop0:
-                               return 0;
-                       case StackBehaviour.Pop1:
-                       case StackBehaviour.Popi:
-                       case StackBehaviour.Popref:
-                               return 1;
-                       case StackBehaviour.Pop1_pop1:
-                       case StackBehaviour.Popi_pop1:
-                       case StackBehaviour.Popi_popi:
-                       case StackBehaviour.Popi_popi8:
-                       case StackBehaviour.Popi_popr4:
-                       case StackBehaviour.Popi_popr8:
-                       case StackBehaviour.Popref_pop1:
-                       case StackBehaviour.Popref_popi:
-                               return 2;
-                       case StackBehaviour.Popi_popi_popi:
-                       case StackBehaviour.Popref_popi_popi:
-                       case StackBehaviour.Popref_popi_popi8:
-                       case StackBehaviour.Popref_popi_popr4:
-                       case StackBehaviour.Popref_popi_popr8:
-                       case StackBehaviour.Popref_popi_popref:
-                               return 3;
-                       case StackBehaviour.Varpop:
-                               if(instr.Operand is MethodReference) {
-                                       // We have to determine from the call 
how many arguments will
-                                       // be popped from the stack
-                                       MethodReference callMethod = 
(MethodReference)instr.Operand;
-                                       return callMethod.Parameters.Count;
-                               } else {
-                                       throw new 
InvalidOperationException("Unexpected instruction: '" +
-                                       instr.OpCode.ToString() + "' at offset 
0x" +
-                                       instr.Offset.ToString("X"));
-                               }
-                       }
-
-                       return 0;
-               }
-
-               private int GetNumPushes (Instruction instr)
-               {
-                       switch (instr.OpCode.StackBehaviourPush) {
-                       case StackBehaviour.Push0:
-                               return 0;
-                       case StackBehaviour.Push1:
-                       case StackBehaviour.Pushi:
-                       case StackBehaviour.Pushi8:
-                       case StackBehaviour.Pushr4:
-                       case StackBehaviour.Pushr8:
-                       case StackBehaviour.Pushref:
-                               return 1;
-                       case StackBehaviour.Push1_push1:
-                               return 2;
-                       case StackBehaviour.Varpush:
-                               // We have to determine from the call how many 
arguments will
-                               // be pushed onto the stack
-                               MethodReference callMethod = 
(MethodReference)instr.Operand;
-                               return (callMethod.ReturnType.ReturnType == 
void_reference) ?
-                                       0 : 1;
-                       }
-
-                       return 0;
-               }
-
-               private static int GetVarIndex (Instruction instr)
-               {
-                       if (instr.OpCode == OpCodes.Stloc_0 || instr.OpCode == 
OpCodes.Ldloc_0)
-                               return 0;
-                       else if (instr.OpCode == OpCodes.Stloc_1 || 
instr.OpCode == OpCodes.Ldloc_1)
-                               return 1;
-                       else if (instr.OpCode == OpCodes.Stloc_2 || 
instr.OpCode == OpCodes.Ldloc_2)
-                               return 2;
-                       else if (instr.OpCode == OpCodes.Stloc_3 || 
instr.OpCode == OpCodes.Ldloc_3)
-                               return 3;
-                       else if (instr.OpCode == OpCodes.Stloc_S || 
instr.OpCode == OpCodes.Stloc ||
-                               instr.OpCode == OpCodes.Ldloc_S || instr.OpCode 
== OpCodes.Ldloc)
-                       {
-                               VariableDefinition varDef = 
(VariableDefinition)instr.Operand;
-                               return varDef.Index;
-                       }
-
-                       return -1;
-               }
-       }
-}

Modified: 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
===================================================================
--- 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
     2008-02-15 19:08:46 UTC (rev 95790)
+++ 
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
     2008-02-15 19:13:38 UTC (rev 95791)
@@ -33,8 +33,11 @@
 using Gendarme.Framework;
 
 namespace Gendarme.Rules.Exceptions {
-       public class DontSwallowErrorsCatchingNonspecificExceptionsRule : 
IMethodRule {
 
+       [Problem ("The method catch a nonspecific exception.")]
+       [Solution ("You can rethrow the original exception, to avoid destroying 
the stacktrace, or you can handle more specific exceptions.")]
+       public class DontSwallowErrorsCatchingNonspecificExceptionsRule : Rule, 
IMethodRule {
+
                //Added System.Object because is the code behind the following 
block:
                //try {
                //      File.Open (foo, bar);
@@ -53,39 +56,35 @@
                        return false;
                }
 
-               private static bool ThrowsGeneralException (ExceptionHandler 
exceptionHandler)
+               private static Instruction ThrowsGeneralException 
(ExceptionHandler exceptionHandler)
                {
                        for (Instruction currentInstruction = 
exceptionHandler.HandlerStart; currentInstruction != 
exceptionHandler.HandlerEnd; currentInstruction = currentInstruction.Next) {
                                if (currentInstruction.OpCode.Code == 
Code.Rethrow)
-                                       return true;
+                                       return null;
                        }
-                       return false;
+                       return exceptionHandler.HandlerStart;
                }
 
-               public MessageCollection CheckMethod (MethodDefinition 
methodDefinition, Runner runner)
+               public RuleResult CheckMethod (MethodDefinition 
methodDefinition)
                {
+                       // rule only applies to methods with IL
                        if (!methodDefinition.HasBody)
-                               return runner.RuleSuccess;
+                               return RuleResult.DoesNotApply;
 
-                       MessageCollection messageCollection = null;
                        ExceptionHandlerCollection exceptionHandlerCollection = 
methodDefinition.Body.ExceptionHandlers;
                        foreach (ExceptionHandler exceptionHandler in 
exceptionHandlerCollection) {
                                if (exceptionHandler.Type == 
ExceptionHandlerType.Catch) {
                                        string catchTypeName = 
exceptionHandler.CatchType.FullName;
                                        if (IsForbiddenTypeInCatches 
(catchTypeName)) {
-                                               if (!ThrowsGeneralException 
(exceptionHandler)) {
-                                                       Location location = new 
Location (methodDefinition, exceptionHandler.HandlerStart.Offset);
-                                                       Message message = new 
Message ("Do not swallow errors catching nonspecific exceptions.", location, 
MessageType.Error);
-                                                       if (messageCollection 
== null)
-                                                               
messageCollection = new MessageCollection (message);
-                                                       else
-                                                               
messageCollection.Add (message);
+                                               Instruction throw_instruction = 
ThrowsGeneralException (exceptionHandler);
+                                               if (throw_instruction != null) {
+                                                       Runner.Report 
(methodDefinition, throw_instruction, Severity.Medium, Confidence.High, 
String.Empty);
                                                }
                                        }
                                }
                        }
 
-                       return messageCollection;
+                       return Runner.CurrentRuleResult;
                }
        }
 }

Modified: trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am       
2008-02-15 19:08:46 UTC (rev 95790)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/Makefile.am       
2008-02-15 19:13:38 UTC (rev 95791)
@@ -10,7 +10,7 @@
 
 exceptions_rules_sources_in = ../../AssemblyInfo.cs.in
 exceptions_rules_generated_sources = $(exceptions_rules_sources_in:.in=)
-exceptions_rules_sources = DontDestroyStackTrace.cs ISEHCatchBlock.cs \
+exceptions_rules_sources = DoNotDestroyStackTraceRule.cs ISEHCatchBlock.cs \
        ISEHGuardedBlock.cs ISEHHandlerBlock.cs \
        SEHHandlerType.cs Impl/ExceptionBlockParser.cs Impl/ExecutionBlock.cs \
        Impl/ExecutionPath.cs Impl/ExecutionPathFactory.cs 
Impl/SEHCatchBlock.cs \
@@ -24,7 +24,7 @@
        $(GMCS) -debug -target:library 
-r:$(top_builddir)/gendarme/bin/Mono.Cecil.dll 
-r:../../bin/Gendarme.Framework.dll -out:$@ $(exceptions_rules_build_sources)
        cp Gendarme.Rules.*.xml ../../bin/
 
-exceptions_test_sources = TestPatterns.cs 
DontSwallowErrorsCatchingNonspecificExceptionsTest.cs 
+exceptions_test_sources = DontDestroyStackTraceTest.cs 
DontSwallowErrorsCatchingNonspecificExceptionsTest.cs 
 exceptions_test_build_sources = $(addprefix $(srcdir)/Test/, 
$(exceptions_test_sources))
 
 Test.Rules.Exceptions.dll: $(exceptions_test_build_sources) 
$(exceptions_rules_SCRIPTS)

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

Reply via email to