internal int SuggestNonNullFieldsForConstructors(APC pc, Type t, IOutputResults output)
        {
            Contract.Ensures(Contract.Result <int>() >= 0);

            var md    = this.metadataDecoder;
            var count = 0;

            // No suggestions for structs
            if (md.IsStruct(t))
            {
                return(count);
            }

            var result = GenerateObjectInvariantsForType(t);

            if (result.Any())
            {
                foreach (var f in result)
                {
                    var contract  = string.Format("Contract.Invariant({0} != null);", md.Name(f.Item1));
                    var extraInfo = new ClousotSuggestion.ExtraSuggestionInfo()
                    {
                        SuggestedCode = contract, TypeDocumentId = md.DocumentationId(t)
                    };
                    var str = String.Format("Consider adding an object invariant {0} to the type {1}", contract, md.Name(t));
                    output.Suggestion(ClousotSuggestion.Kind.ObjectInvariant, ClousotSuggestion.Kind.ObjectInvariant.ToString(), pc, str, null, extraInfo);
                    count++;
                }
            }

            this.typesWeSuggestedNonNullFields.Add(t);
            return(count);
        }
Beispiel #2
0
        private void Emit(APC pc, IEnumerable <Field> fieldsOnlyModifiedInConstructors, IOutput output)
        {
            Contract.Requires(fieldsOnlyModifiedInConstructors != null);
            Contract.Requires(output != null);

            var md = this.mdDecoder;

            // Suggest only for fields that we can see in the analysis
            foreach (var f in fieldsOnlyModifiedInConstructors.Where(field => !md.IsVisibleOutsideAssembly(field)))
            {
                var declaringType = md.DeclaringType(f);

                var extraInfo = new ClousotSuggestion.ExtraSuggestionInfo()
                {
                    CalleeDocumentId = md.DocumentationId(f),
                    TypeDocumentId   = md.DocumentationId(declaringType)
                };

                var suggestion = string.Format("Field {0}, declared in type {1}, is only updated in constructors. Consider marking it as readonly",
                                               md.Name(f),
                                               md.Name(declaringType));

                output.Suggestion(ClousotSuggestion.Kind.ReadonlyField, ClousotSuggestion.Kind.ReadonlyField.Message(), pc, suggestion, null, extraInfo);
            }
        }
        private void SetExtraInfo(Method m, string kind, BoxedExpression exp, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
        {
            Contract.Requires(kind != null);

            var md = mdriver.MetaDataDecoder;

            extraInfo.CalleeDocumentId = md.DocumentationId(m);
            extraInfo.CalleeMemberKind = kind;
            extraInfo.TypeDocumentId   = md.DocumentationId(md.DeclaringType(m));
            extraInfo.SuggestedCode    = GetPostconditionString(exp);
            extraInfo.CalleeIsDeclaredInTheSameAssembly = md.DeclaringAssembly(m).Equals(md.DeclaringAssembly(mdriver.CurrentMethod)).ToString();
        }
        public void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
        {
            EnsureMethodStart();

            // show suggestion
            output.Suggestion(type, kind, pc, suggestion, causes, extraInfo);

            // put it into baseline
            EmitOutcomeToBaseLine(WarningKind.Informational, ProofOutcome.Bottom, pc, suggestion);
        }
Beispiel #5
0
 public override void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
 {
     if (kind.StartsWith("Code fix"))
     {
         return;
     }
     base.Suggestion(type, kind, pc, suggestion, causes, extraInfo);
 }
 public void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
 {
     throw new NotImplementedException();
 }
 public override void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
 {
     if (causes == null)
     {
         base.Suggestion(type, kind, pc, suggestion, causes, extraInfo);
     }
     else
     {
         var suggestionTuple = new Tuple <string, APC, string, ClousotSuggestion.ExtraSuggestionInfo>(kind, pc, suggestion, extraInfo);
         foreach (var id in causes)
         {
             Info info;
             if (this.DB.TryGetValue(id, out info))
             {
                 // Works with side effects
                 info.Add(suggestionTuple);
             }
         }
     }
 }
        private bool TrySuggestNecessaryPostconditions(Method method, BoxedExpression exp, out ClousotSuggestion.Kind kind, out string result, out ClousotSuggestion.ExtraSuggestionInfo extraInfo)
        {
            result    = null;
            kind      = default(ClousotSuggestion.Kind);
            extraInfo = new ClousotSuggestion.ExtraSuggestionInfo();

            var md = this.mdriver.MetaDataDecoder;

            if (ShouldInferNecessaryPostconditionFor(method) && IsSuitableNecessaryPostcondition(exp))
            {
                #region Interface methods
                // Is it an interface method?
                if (md.IsInterface(md.DeclaringType(method)))
                {
                    kind = ClousotSuggestion.Kind.EnsuresNecessary;
                    this.SetExtraInfo(method, StringConstants.XML.Interface, exp, extraInfo);
                    result = string.Format("The caller expects the postcondition {0} to hold for the interface member {1}. Consider adding such postcondition to enforce all implementations to guarantee it", extraInfo.SuggestedCode, md.Name(method));

                    return(true);
                }
                // Is it an implementation of an interface method?
                if (md.IsImplicitImplementation(method))
                {
                    var implemented = md.ImplementedMethods(method).ToArray();
                    Contract.Assume(implemented.Length > 0);
                    kind = ClousotSuggestion.Kind.EnsuresNecessary;
                    this.SetExtraInfo(implemented[0], StringConstants.XML.Interface, exp, extraInfo); // we just pick the first one
                    if (implemented.Length == 1)
                    {
                        result = string.Format("The caller expects the postcondition {0} to hold for the interface member {1}. Consider adding the postcondition to enforce all implementations to guarantee it", extraInfo.SuggestedCode, md.Name(method));
                    }
                    else
                    {
                        extraInfo.CalleeDocumentId = String.Join(" or ", implemented.Select(m => md.DocumentationId(m)).ToArray());
                        result = string.Format("Add a postcondition {0} to one of the interface members {1}", extraInfo.SuggestedCode, md.Name(method));
                    }

                    return(true);
                }
                #endregion

                #region Abstract methods

                if (md.IsAbstract(method))
                {
                    kind = ClousotSuggestion.Kind.EnsuresNecessary;
                    this.SetExtraInfo(method, StringConstants.XML.Abstract, exp, extraInfo);
                    result = string.Format("The caller expects the postcondition {0} to hold for the abstract member {1}. Consider adding the postcondition to enforce all overrides to guarantee it", extraInfo.SuggestedCode, md.Name(method));

                    return(true);
                }
                #endregion

                #region External methods

                if (md.IsExtern(method))
                {
                    kind = ClousotSuggestion.Kind.EnsuresNecessary;
                    this.SetExtraInfo(method, StringConstants.XML.Extern, exp, extraInfo);
                    result = string.Format("The caller expects the postcondition {0} to hold for the external member {1}. Consider adding a postcondition or an assume to document it", extraInfo.SuggestedCode, md.Name(method));

                    return(true);
                }
                #endregion

                #region Proper methods

                kind = ClousotSuggestion.Kind.EnsuresNecessary;
                this.SetExtraInfo(method, StringConstants.XML.ProperMember, exp, extraInfo);
                result = string.Format("The caller expects the postcondition {0} to hold for the member {1}. Consider adding such postcondition to make sure all implementations satisfy it", extraInfo.SuggestedCode, md.Name(method));

                #endregion
            }

            return(false);
        }
Beispiel #9
0
 public void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
 {
     this.WriteLine("{0}: message : Suggested {1}: {2}", pc.PrimarySourceContext(), kind, suggestion);
 }
        public override void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
        {
            if (this.methodmask != null)
            {
                if (this.methodmask.IsMasked(suggestion))
                {
                    this.methodmask.SignalMasked(suggestion);

                    return;
                }
            }

            base.Suggestion(type, kind, pc, suggestion, causes, extraInfo);
        }
        public void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
        {
            int primaryILOffset, methodILOffset;

            GetILOffsets(pc, this.currentMethod, out primaryILOffset, out methodILOffset);
            if (!this.expectedMethodOutcomes.CheckOutcome(WarningKind.Informational, output, this.currentMethod, ProofOutcome.Bottom, suggestion, primaryILOffset, methodILOffset, this.mdDecoder, this.cacheManager))
            {
                // show suggestion
                output.Suggestion(type, kind, pc, suggestion, causes, extraInfo);
                // don't count as an error
            }
        }
Beispiel #12
0
 public void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
 {
 }
 public override void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
 {
     if (this.isMethodOk)
     {
         base.Suggestion(type, kind, pc, suggestion, causes, extraInfo);
     }
 }
Beispiel #14
0
        public override void Suggestion(ClousotSuggestion.Kind type, string kind, APC pc, string suggestion, List <uint> causes, ClousotSuggestion.ExtraSuggestionInfo extraInfo)
        {
            if (this.skipCurrentMethod)
            {
                return;
            }

            if (this.options.IncludeSuggestionMessagesInRegression && !this.expectedOutcomes.CheckOutcome(suggestion))
            {
                base.Suggestion(type, kind, pc, suggestion, causes, extraInfo);
            }
        }