~ubuntu-branches/ubuntu/wily/monodevelop/wily

« back to all changes in this revision

Viewing changes to external/mono-tools/gendarme/rules/Gendarme.Rules.Exceptions/NewExceptionRule.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// Gendarme.Rules.Exceptions.NewExceptionsRule
 
3
//
 
4
// Authors:
 
5
//      Daniel Abramov <ex@vingrad.ru>
 
6
//      Sebastien Pouliot <sebastien@ximian.com>
 
7
//
 
8
// Copyright (C) 2008 Daniel Abramov
 
9
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
12
// of this software and associated documentation files (the "Software"), to deal
 
13
// in the Software without restriction, including without limitation the rights
 
14
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
15
// copies of the Software, and to permit persons to whom the Software is
 
16
// furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included in
 
19
// all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
24
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
26
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
27
// THE SOFTWARE.
 
28
 
 
29
using System;
 
30
 
 
31
using Mono.Cecil;
 
32
using Mono.Cecil.Cil;
 
33
 
 
34
using Gendarme.Framework;
 
35
using Gendarme.Framework.Engines;
 
36
using Gendarme.Framework.Rocks;
 
37
 
 
38
namespace Gendarme.Rules.Exceptions {
 
39
 
 
40
        [EngineDependency (typeof (OpCodeEngine))]
 
41
        abstract public class NewExceptionsRule : Rule, IMethodRule {
 
42
 
 
43
                public override void Initialize (IRunner runner)
 
44
                {
 
45
                        base.Initialize (runner);
 
46
                        // if the module does not reference any of these types, don't analyze it
 
47
                        // (unless this is corlib itself since they're defined in it :-)
 
48
                        Runner.AnalyzeModule += delegate (object o, RunnerEventArgs e) {
 
49
                                Active = (e.CurrentAssembly.Name.Name == "mscorlib" ||
 
50
                                        e.CurrentModule.AnyTypeReference ((TypeReference tr) => {
 
51
                                                return CheckException (tr);
 
52
                                        }));
 
53
                        };
 
54
                }
 
55
 
 
56
                abstract protected bool CheckException (TypeReference type);
 
57
                abstract protected Severity Severity { get; }
 
58
 
 
59
                public RuleResult CheckMethod (MethodDefinition method)
 
60
                {
 
61
                        // if method has no IL, the rule doesn't apply
 
62
                        if (!method.HasBody)
 
63
                                return RuleResult.DoesNotApply;
 
64
 
 
65
                        // and when the IL contains a NewObj instruction
 
66
                        if (!OpCodeEngine.GetBitmask (method).Get (Code.Newobj))
 
67
                                return RuleResult.DoesNotApply;
 
68
 
 
69
                        // look for newobj instructions
 
70
                        foreach (Instruction ins in method.Body.Instructions) {
 
71
                                if (ins.OpCode.Code != Code.Newobj)
 
72
                                        continue;
 
73
 
 
74
                                // obtain a reference to the constructor's type
 
75
                                TypeReference ctype = (ins.Operand as MethodReference).DeclaringType;
 
76
 
 
77
                                // report a defect if an offending exception type is found
 
78
                                if (CheckException (ctype))
 
79
                                        Runner.Report (method, ins, Severity, Confidence.High, ctype.Name);
 
80
                        }
 
81
 
 
82
                        return Runner.CurrentRuleResult;
 
83
                }
 
84
        }
 
85
}