A condition that must be true at the end of a method.
Inheritance: ContractElement, IPostcondition
Ejemplo n.º 1
0
        public override void TraverseChildren(IMethodDefinition method)
        {
            if (!MemberHelper.IsVisibleOutsideAssembly(method))
            {
                return;
            }
            var returnType = method.Type;

            if (returnType == this.host.PlatformType.SystemVoid ||
                returnType.IsEnum ||
                returnType.IsValueType
                )
            {
                return;
            }

            var newContract = new Microsoft.Cci.MutableContracts.MethodContract();
            var post        = new List <IPostcondition>();
            var p           = new Microsoft.Cci.MutableContracts.Postcondition()
            {
                Condition = new NotEquality()
                {
                    LeftOperand = new ReturnValue()
                    {
                        Type = returnType,
                    },
                    RightOperand = new CompileTimeConstant()
                    {
                        Type  = returnType,
                        Value = null,
                    },
                    Type = this.host.PlatformType.SystemBoolean,
                },
                OriginalSource = "result != null",
            };

            post.Add(p);
            newContract.Postconditions = post;

            var contract = this.contractProvider.GetMethodContractFor(method);

            if (contract != null)
            {
                Microsoft.Cci.MutableContracts.ContractHelper.AddMethodContract(newContract, contract);
            }
            this.contractProvider.AssociateMethodWithContract(method, newContract);

            base.TraverseChildren(method);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Visits the specified post condition.
 /// </summary>
 /// <param name="postCondition">The post condition.</param>
 protected virtual IPostcondition DeepCopy(Postcondition postCondition) {
   postCondition.Condition = this.Substitute(postCondition.Condition);
   if (postCondition.Description != null)
     postCondition.Description = this.Substitute(postCondition.Description);
   return postCondition;
 }
Ejemplo n.º 3
0
 private static List<IThrownException> FilterUserMessage(IContractAwareHost host, IEnumerable<IThrownException> thrownExceptions, ITypeDefinition typeDefinition) {
   int n = (int)IteratorHelper.EnumerableCount(thrownExceptions);
   var filteredThrownExceptions = new IThrownException[n];
   var i = 0;
   foreach (var te in thrownExceptions) {
     if (CanAccess(te.Postcondition.Description, typeDefinition)) {
       filteredThrownExceptions[i] = te;
     } else {
       var newP = new Postcondition(te.Postcondition);
       newP.Description = null;
       filteredThrownExceptions[i] = new ThrownException() {
         ExceptionType = te.ExceptionType,
         Postcondition = newP,
       };
     }
     i++;
   }
   return new List<IThrownException>(filteredThrownExceptions);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Get the mutable copy of a postcondition.
 /// </summary>
 /// <param name="postcondition"></param>
 /// <returns></returns>
 public virtual Postcondition GetMutableCopy(IPostcondition postcondition) {
   object cachedValue;
   if (this.cache.TryGetValue(postcondition, out cachedValue))
     return (Postcondition)cachedValue;
   var result = new Postcondition(postcondition);
   // Probably not necessary, no two postconditions are shared. 
   this.cache.Add(postcondition, result);
   this.cache.Add(result, result);
   return result;
 }
Ejemplo n.º 5
0
 private static List<IPostcondition> FilterUserMessage(IContractAwareHost host, IEnumerable<IPostcondition> postconditions, ITypeDefinition typeDefinition) {
   int n = (int)IteratorHelper.EnumerableCount(postconditions);
   var filteredPostconditions = new IPostcondition[n];
   var i = 0;
   foreach (var p in postconditions) {
     if (CanAccess(p.Description, typeDefinition)) {
       filteredPostconditions[i] = p;
     } else {
       var newP = new Postcondition(p);
       newP.Description = null;
       filteredPostconditions[i] = newP;
     }
     i++;
   }
   return new List<IPostcondition>(filteredPostconditions);
 }