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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
//
// ContractExtractor.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2011 Alexander Chebaturkin
//
// 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.CodeContracts.Static.AST;
using Mono.CodeContracts.Static.AST.Visitors;
namespace Mono.CodeContracts.Static.ContractExtraction {
class ContractExtractor : DefaultNodeVisitor {
private readonly AssemblyNode assembly;
private readonly ContractNodes contract_nodes;
private readonly bool verbose;
private readonly Dictionary<Method, Method> visited_methods;
public ContractExtractor (ContractNodes contractNodes, AssemblyNode assembly)
: this (contractNodes, assembly, DebugOptions.Debug)
{
}
public ContractExtractor (ContractNodes contractNodes, AssemblyNode assembly, bool verbose)
{
this.visited_methods = new Dictionary<Method, Method> ();
this.contract_nodes = contractNodes;
this.assembly = assembly;
this.verbose = verbose;
}
public override AssemblyNode VisitAssembly (AssemblyNode node)
{
if (node == null)
return null;
if (this.verbose)
Console.WriteLine ("Extracting from '{0}'", this.assembly.FullName);
return base.VisitAssembly (node);
}
public override Method VisitMethod (Method node)
{
if (node == null)
return null;
if (this.visited_methods.ContainsKey (node))
return node;
this.visited_methods.Add (node, node);
node.ContractProvider = ExtractContractsFromMethod;
return node;
}
public void ExtractContractsFromMethod (Method method)
{
MethodContract contract = method.MethodContract = new MethodContract (method);
List<Requires> preconditions = null;
List<Ensures> postconditions = null;
if (method.IsAbstract)
return;
if (method.Body != null && method.Body.Statements != null) {
if (this.verbose)
Console.WriteLine (method.FullName);
ExtractContractsInternal (method, ref preconditions, ref postconditions);
contract.Requires = preconditions;
contract.Ensures = postconditions;
}
}
private void ExtractContractsInternal (Method method, ref List<Requires> preconditions, ref List<Ensures> postconditions)
{
if (method == null)
return;
if (this.verbose)
Console.WriteLine ("Method: " + method.FullName);
Block body = method.Body;
if (body == null || body.Statements == null || body.Statements.Count <= 0)
return;
int lastBlockContainingContract;
int lastStatementContainingContract;
int begin = 0;
bool contractsFound = FindLastBlockWithContracts (body.Statements, begin,
out lastBlockContainingContract, out lastStatementContainingContract);
if (!contractsFound) {
if (this.verbose)
Console.WriteLine ("\tNo contracts found");
return;
}
List<Statement> contractSection = HelperMethods.ExtractContractBlocks (body.Statements, begin, 0,
lastBlockContainingContract, lastStatementContainingContract);
preconditions = new List<Requires> ();
postconditions = new List<Ensures> ();
ExtractPrePostConditionsFromContractSection (contractSection, preconditions, postconditions);
}
private bool ExtractPrePostConditionsFromContractSection (List<Statement> contractSection,
List<Requires> preconditions, List<Ensures> postconditions)
{
List<Statement> blocks = contractSection;
int firstBlockIndex = 0;
int blocksCount = blocks.Count;
int firstStmtIndex = HelperMethods.FindNextRealStatement (((Block) blocks [firstBlockIndex]).Statements, 0);
bool wasEndContractBlock = false;
for (int lastBlockIndex = firstBlockIndex; lastBlockIndex < blocksCount; ++lastBlockIndex) {
var block = (Block) blocks [lastBlockIndex];
if (block == null)
continue;
int cnt = block.Statements == null ? 0 : block.Statements.Count;
for (int lastStmtIndex = 0; lastStmtIndex < cnt; ++lastStmtIndex) {
Statement s = block.Statements [lastStmtIndex];
if (s == null)
continue;
Method calledMethod = HelperMethods.IsMethodCall (s);
if (!this.contract_nodes.IsContractMethod (calledMethod))
continue;
if (wasEndContractBlock) {
if (DebugOptions.Debug)
Console.WriteLine ("Contract call after prior ContractBlock");
break;
}
if (this.contract_nodes.IsEndContractBlock (calledMethod)) {
wasEndContractBlock = true;
continue;
}
//here we definitely know that s is ExpressionStatement of (MethodCall). see HelperMethods.IsMethodCall(s)
var methodCall = ((ExpressionStatement) s).Expression as MethodCall;
Expression assertionExpression = methodCall.Arguments [0];
Expression expression;
if (firstBlockIndex == lastBlockIndex && firstStmtIndex == lastStmtIndex)
expression = assertionExpression;
else {
block.Statements [lastStmtIndex] = new ExpressionStatement (assertionExpression);
List<Statement> contractBlocks = HelperMethods.ExtractContractBlocks (blocks, firstBlockIndex, firstStmtIndex,
lastBlockIndex, lastStmtIndex);
var b = new Block (contractBlocks);
expression = new BlockExpression (b);
}
MethodContractElement methodContractElement = null;
if (this.contract_nodes.IsPlainPrecondition (calledMethod)) {
var requires = new Requires (expression);
methodContractElement = requires;
} else if (this.contract_nodes.IsPostCondition (calledMethod))
methodContractElement = new Ensures (expression);
if (methodContractElement == null)
throw new InvalidOperationException ("Unrecognized contract method");
if (methodCall.Arguments.Count > 1) {
Expression userMessage = methodCall.Arguments [1];
methodContractElement.UserMessage = userMessage;
}
switch (methodContractElement.NodeType) {
case NodeType.Requires:
var requires = (Requires) methodContractElement;
preconditions.Add (requires);
break;
case NodeType.Ensures:
var ensures = (Ensures) methodContractElement;
postconditions.Add (ensures);
break;
}
}
}
return true;
}
private bool FindLastBlockWithContracts (List<Statement> statements, int beginning,
out int lastBlockContainingContract, out int lastStatementContainingContract)
{
lastBlockContainingContract = -1;
lastStatementContainingContract = -1;
for (int i = statements.Count - 1; i >= beginning; i--) {
var block = statements [i] as Block;
if (block == null || block.Statements == null || block.Statements.Count <= 0)
continue;
for (int j = block.Statements.Count - 1; j >= 0; j--) {
if (this.contract_nodes.IsContractCall (block.Statements [j]) == null)
continue;
lastBlockContainingContract = i;
lastStatementContainingContract = j;
return true;
}
}
return false;
}
}
}
|