Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Pattern.cs « Gendarme.Rules.Smells « rules « gendarme - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f19d3e7b4fa723e2184391c24d8ea74602c65cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// Gendarme.Rules.Smells.Pattern class
//
// Authors:
//	Néstor Salceda <nestor.salceda@gmail.com>
//	Sebastien Pouliot  <sebastien@ximian.com>
//
// (C) 2008 Néstor Salceda
// Copyright (C) 2010 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;
using Mono.Cecil.Cil;
using Gendarme.Framework.Rocks;

namespace Gendarme.Rules.Smells {
	internal sealed class Pattern {
		Instruction[] instructions;
		int[] prefixes;
		bool? compilerGeneratedBlock;
		bool? extractableToMethodBlock;

		internal Pattern (Instruction[] instructions) {
			if (instructions == null)
				throw new ArgumentNullException ("instructions");
			this.instructions = instructions;
		}

		// look for: isinst System.IDisposable
		static bool IsInstanceOfIDisposable (Instruction ins)
		{
			if (ins.OpCode.Code != Code.Isinst)
				return false;
			return ((ins.Operand as TypeReference).FullName == "System.IDisposable");
		}

		// look for a virtual call to a specific method
		static bool IsCallVirt (Instruction ins, string typeName, string methodName)
		{
			if (ins.OpCode.Code != Code.Callvirt)
				return false;
			MethodReference mr = (ins.Operand as MethodReference);
			return ((mr != null) && (mr.Name == methodName) && (mr.DeclaringType.FullName == typeName));
		}

		// look for:
		//	callvirt System.Void System.IDisposable::Dispose()
		//	endfinally 
		static bool IsIDisposableDisposePattern (Instruction ins)
		{
			if (!IsCallVirt (ins, "System.IDisposable", "Dispose"))
				return false;
			return ins.Next.Is (Code.Endfinally);
		}

		// IIRC older xMCS generated that quite often
		bool IsDoubleReturn {
			get {
				return ((Count > 1) && (instructions[Count - 1].OpCode.Code == Code.Ret) &&
					(instructions[Count - 2].OpCode.Code == Code.Ret));
			}
		}

		// small patterns that highly suggest they were compiler generated
		bool ComputeUnlikelyUserPatterns ()
		{
			bool call = false;
			for (int i = 0; i < Count; i++) {
				Instruction ins = instructions [i];
				// foreach
				if (IsCallVirt (ins, "System.Collections.IEnumerator", "get_Current"))
					return true;
				if (IsCallVirt (ins, "System.Collections.IEnumerator", "MoveNext"))
					return !call;
				// if there's a unknown call then it's likely not (totally) compiler generated
				call |= (ins.OpCode.FlowControl == FlowControl.Call);
				// foreach
				if (IsInstanceOfIDisposable (ins))
					return true;
				// foreach, using
				if (IsIDisposableDisposePattern (ins))
					return true;
			}
			return false;
		}
		
		internal bool IsCompilerGeneratedBlock {
			get {
				if (compilerGeneratedBlock == null)
					compilerGeneratedBlock = ComputeUnlikelyUserPatterns () || IsDoubleReturn;
				return (bool) compilerGeneratedBlock;
			}
		}

		bool IsReturningCode {
			get {
				return ((Count == 4 &&
					instructions[0].OpCode.StackBehaviourPush == StackBehaviour.Push1 &&
					(instructions [1].OpCode.Code == Code.Brtrue || instructions [1].OpCode.Code == Code.Brtrue_S) &&
					instructions[2].OpCode.StackBehaviourPush == StackBehaviour.Pushi && 
					instructions[3].OpCode.Code == Code.Ret)
					||
					(Count > 1 &&
					instructions[Count - 1].OpCode.Code == Code.Ret &&
					instructions[Count - 2].OpCode.FlowControl == FlowControl.Cond_Branch));
			}
		}

		internal bool IsExtractableToMethodBlock {
			get {
				if (extractableToMethodBlock == null) 
					extractableToMethodBlock = !IsReturningCode;
				return (bool) extractableToMethodBlock;
			}
		}
			
		internal void ComputePrefixes (MethodDefinition method)
		{
			MethodDefinition target = InstructionMatcher.Target;
			InstructionMatcher.Target = method;
			try {
				int offset = 0;
				if ((prefixes == null) || (prefixes.Length < instructions.Length))
					prefixes = new int [instructions.Length];

				for (int index = 1; index < instructions.Length; index++) {
					while (offset > 0 &&
						!InstructionMatcher.AreEquivalent (instructions [offset], instructions [index]))
						offset = prefixes [offset - 1];

					if (InstructionMatcher.AreEquivalent (instructions [offset], instructions [index]))
						offset++;

					prefixes [index] = offset;
				}
			}
			finally {
				InstructionMatcher.Target = target;
			}
		}

		internal int Count {
			get {
				return instructions.Length;
			}
		}
		
		internal Instruction this[int index] {
			get {
				return instructions[index];
			}
		}

		internal int[] Prefixes {
			get {
				return prefixes;
			}
		}
	}
}