static SourceContext ContextForTextExtraction(MethodContractElement mce) { if (mce.DefSite.IsValid) { return(mce.DefSite); } return(mce.SourceContext); }
private void HandleError(Method method, int number, string message, MethodContractElement contract) { Contract.Requires(contract != null); Contract.Requires(message != null); // F: this.errorHandler(new Error(number, message, contract.SourceContext)); if (contract.DefSite.IsValid) { this.errorHandler(new Warning(0, " location related to previous warning.", contract.DefSite)); } }
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); }