Author: spouliot
Date: 2008-01-11 20:24:14 -0500 (Fri, 11 Jan 2008)
New Revision: 92712
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
Log:
2008-01-11 Sebastien Pouliot <[EMAIL PROTECTED]>
* DontDestroyStackTrace.cs: Use new Location ctors. Change some
methods to static.
* DontSwallowErrorsCatchingNonspecificExceptionsRule.cs: Don't
create MessageCollection until needed. Use new Location ctors. Change
a method to static.
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
2008-01-12 01:21:37 UTC (rev 92711)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/ChangeLog
2008-01-12 01:24:14 UTC (rev 92712)
@@ -1,3 +1,11 @@
+2008-01-11 Sebastien Pouliot <[EMAIL PROTECTED]>
+
+ * DontDestroyStackTrace.cs: Use new Location ctors. Change some
+ methods to static.
+ * DontSwallowErrorsCatchingNonspecificExceptionsRule.cs: Don't
+ create MessageCollection until needed. Use new Location ctors. Change
+ a method to static.
+
2007-11-29 Sebastien Pouliot <[EMAIL PROTECTED]>
* DontDestroyStackTrace.cs: Don't use a static field (it fails a rule)
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
2008-01-12 01:21:37 UTC (rev 92711)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontDestroyStackTrace.cs
2008-01-12 01:24:14 UTC (rev 92712)
@@ -12,20 +12,18 @@
private TypeReference void_reference;
private ArrayList warned_offsets_in_method;
- public DontDestroyStackTrace ()
- {
- }
-
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);
- ExceptionBlockParser ebp = new ExceptionBlockParser ();
- ISEHGuardedBlock[] guardedBlocks =
ebp.GetExceptionBlocks (method);
+ ISEHGuardedBlock[] guardedBlocks =
ExceptionBlockParser.GetExceptionBlocks (method);
foreach (ISEHGuardedBlock guardedBlock in
guardedBlocks) {
foreach (ISEHHandlerBlock handlerBlock in
guardedBlock.SEHHandlerBlocks) {
@@ -105,8 +103,7 @@
// we're rethrowing it.This is
deemed naughty...
if
(!warned_offsets_in_method.Contains(cur.Offset)) {
Location loc =
- new Location
(method.DeclaringType.FullName,
-
method.Name,cur.Offset);
+ new Location
(method, cur.Offset);
Message msg = new
Message (
"Throwing
original exception - destroys stack trace!",
loc,
@@ -132,7 +129,7 @@
return;
}
- private int GetNumPops (Instruction instr)
+ private static int GetNumPops (Instruction instr)
{
switch (instr.OpCode.StackBehaviourPop) {
case StackBehaviour.Pop0:
@@ -198,7 +195,7 @@
return 0;
}
- private int GetVarIndex (Instruction instr)
+ private static int GetVarIndex (Instruction instr)
{
if (instr.OpCode == OpCodes.Stloc_0 || instr.OpCode ==
OpCodes.Ldloc_0)
return 0;
@@ -209,7 +206,7 @@
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_0)
+ instr.OpCode == OpCodes.Ldloc_S || instr.OpCode
== OpCodes.Ldloc)
{
VariableDefinition varDef =
(VariableDefinition)instr.Operand;
return varDef.Index;
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
2008-01-12 01:21:37 UTC (rev 92711)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.Exceptions/DontSwallowErrorsCatchingNonspecificExceptionsRule.cs
2008-01-12 01:24:14 UTC (rev 92712)
@@ -53,7 +53,7 @@
return false;
}
- private bool ThrowsGeneralException (ExceptionHandler
exceptionHandler)
+ private static bool ThrowsGeneralException (ExceptionHandler
exceptionHandler)
{
for (Instruction currentInstruction =
exceptionHandler.HandlerStart; currentInstruction !=
exceptionHandler.HandlerEnd; currentInstruction = currentInstruction.Next) {
if (currentInstruction.OpCode.Code ==
Code.Rethrow)
@@ -64,25 +64,27 @@
public MessageCollection CheckMethod (MethodDefinition
methodDefinition, Runner runner)
{
- MessageCollection messageCollection = new
MessageCollection ();
- if (methodDefinition.HasBody) {
- 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.Name, methodDefinition.Name,
exceptionHandler.HandlerStart.Offset);
- Message message
= new Message ("Do not swallow errors catching nonspecific exceptions.",
location, MessageType.Error);
+ if (!methodDefinition.HasBody)
+ return runner.RuleSuccess;
+
+ 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);
- }
}
}
}
}
- if (messageCollection.Count == 0)
- return null;
return messageCollection;
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches