Merge pull request #390 from hubuk/issue221
Added support for array creation in contract methods in Roslyn generated code.
This commit is contained in:
Коммит
75b1c47c4d
|
@ -2251,9 +2251,9 @@ namespace Microsoft.Contracts.Foxtrot
|
|||
// Roslyn is not introducing local variable for holding an array.
|
||||
// Instead of that it uses "dup" instruction and assignes parameter expression
|
||||
// directly to the stack slot.
|
||||
// This change should be addressed here, because otherwise ccrewrite will fail
|
||||
// This change is addressed here, because otherwise ccrewrite will fail
|
||||
// with an error.
|
||||
if (idxr != null && (idxr.Object is Local || ExpressionTreeInitialization(assignment)))
|
||||
if (idxr != null && (idxr.Object is Local || IsConstructedArrayIndexer(idxr) || ExpressionTreeInitialization(assignment)))
|
||||
targetIsLocal = true;
|
||||
|
||||
// Assignments to locals that are structs show up as address deference
|
||||
|
@ -2284,7 +2284,7 @@ namespace Microsoft.Contracts.Foxtrot
|
|||
|
||||
base.VisitAssignmentStatement(assignment);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns true the assignment was done from local variable that holds parameter expression
|
||||
/// to the indexer.
|
||||
|
@ -2297,7 +2297,35 @@ namespace Microsoft.Contracts.Foxtrot
|
|||
var sourceAsLocal = assignment.Source as Local;
|
||||
return NameUtils.IsExpressionTreeLocal(sourceAsLocal);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified indexer refers to an array
|
||||
/// created in the currently validated contract.
|
||||
/// </summary>
|
||||
/// <param name="indexer">
|
||||
/// An indexer which references object origin shall be determined.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// <see langword="true"/> if the specified indexer refers to an array
|
||||
/// created in the currently validated contract;
|
||||
/// <see langword="false"/>, otherwise.
|
||||
/// </returns>
|
||||
private bool IsConstructedArrayIndexer(Indexer indexer)
|
||||
{
|
||||
if (!seenConstructArray || !seenDup)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
Node obj = indexer.Object;
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return obj.NodeType == NodeType.Pop;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that method calls are to pure methods.
|
||||
/// </summary>
|
||||
|
@ -2374,37 +2402,50 @@ namespace Microsoft.Contracts.Foxtrot
|
|||
}
|
||||
|
||||
private bool seenDup;
|
||||
private bool seenConstructArray;
|
||||
|
||||
public override void VisitEnsuresExceptional(EnsuresExceptional exceptional)
|
||||
{
|
||||
seenDup = false;
|
||||
seenConstructArray = false;
|
||||
base.VisitEnsuresExceptional(exceptional);
|
||||
}
|
||||
|
||||
public override void VisitEnsuresNormal(EnsuresNormal normal)
|
||||
{
|
||||
seenDup = false;
|
||||
seenConstructArray = false;
|
||||
base.VisitEnsuresNormal(normal);
|
||||
}
|
||||
|
||||
public override void VisitInvariant(Invariant invariant)
|
||||
{
|
||||
seenDup = false;
|
||||
seenConstructArray = false;
|
||||
base.VisitInvariant(invariant);
|
||||
}
|
||||
|
||||
public override void VisitRequiresOtherwise(RequiresOtherwise otherwise)
|
||||
{
|
||||
seenDup = false;
|
||||
seenConstructArray = false;
|
||||
base.VisitRequiresOtherwise(otherwise);
|
||||
}
|
||||
|
||||
public override void VisitRequiresPlain(RequiresPlain plain)
|
||||
{
|
||||
seenDup = false;
|
||||
seenConstructArray = false;
|
||||
base.VisitRequiresPlain(plain);
|
||||
}
|
||||
|
||||
public override void VisitConstructArray(ConstructArray consArr)
|
||||
{
|
||||
this.seenConstructArray = true;
|
||||
|
||||
base.VisitConstructArray(consArr);
|
||||
}
|
||||
|
||||
public void VisitPop()
|
||||
{
|
||||
Contract.Assume(this.CurrentMethod != null);
|
||||
|
|
|
@ -116,6 +116,7 @@
|
|||
<Compile Include="RewriterTests\AsyncTestsDataSource.cs" />
|
||||
<Compile Include="RewriterTests\RewriterTest.cs" />
|
||||
<TestSources Include="Sources\OptimizedIterator.cs" />
|
||||
<TestSources Include="Sources\ArrayCreationInPrecondition.cs" />
|
||||
<Compile Include="TestDriver.cs" />
|
||||
<TestSources Include="Sources\InheritExists.cs" />
|
||||
<TestSources Include="Sources\InheritGenericExists.cs" />
|
||||
|
|
|
@ -990,6 +990,17 @@ namespace Tests
|
|||
useBinDir: false,
|
||||
useExe: true,
|
||||
mustSucceed: true);
|
||||
yield return new Options(
|
||||
sourceFile: @"Foxtrot\Tests\Sources\ArrayCreationInPrecondition.cs",
|
||||
foxtrotOptions: @"",
|
||||
useContractReferenceAssemblies: true,
|
||||
compilerOptions: null,
|
||||
references: new string[0],
|
||||
libPaths: new[] { @"Microsoft.Research\RegressionTest\ClousotTestHarness\bin\debug" },
|
||||
compilerCode: "CS",
|
||||
useBinDir: false,
|
||||
useExe: true,
|
||||
mustSucceed: true);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
// CodeContracts
|
||||
//
|
||||
// Copyright (c) Microsoft Corporation
|
||||
//
|
||||
// All rights reserved.
|
||||
//
|
||||
// MIT License
|
||||
//
|
||||
// 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 System.Text;
|
||||
using System.Diagnostics.Contracts;
|
||||
|
||||
namespace Tests.Sources
|
||||
{
|
||||
partial class TestMain
|
||||
{
|
||||
void Test(object trigger)
|
||||
{
|
||||
// both trigger malformed contract error
|
||||
Contract.Requires(new object[] { string.Empty } != null);
|
||||
Contract.Requires(new object[] { 0 } != null);
|
||||
// this is OK
|
||||
Contract.Requires(new object[] { null } != null);
|
||||
Contract.Requires(trigger != null);
|
||||
}
|
||||
|
||||
partial void Run()
|
||||
{
|
||||
if (behave)
|
||||
{
|
||||
this.Test(new object());
|
||||
}
|
||||
else
|
||||
{
|
||||
this.Test(null);
|
||||
}
|
||||
}
|
||||
|
||||
public ContractFailureKind NegativeExpectedKind = ContractFailureKind.Precondition;
|
||||
public string NegativeExpectedCondition = "trigger != null";
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче