Esempio n. 1
0
        public static List <TextSpan> GetReturnStatementSpans(MethodDeclarationSyntax method)
        {
            var rsf = new ReturnStatementFinder();

            rsf.Visit(method);
            return(rsf.spans);
        }
Esempio n. 2
0
            bool IOutputResults.EmitOutcome(Witness witness, string format, params object[] args)
            {
                if (this.ShowOnlyAnswersToAskClousot)
                {
                    return(false);
                }

                string tag;

                switch (witness.Outcome)
                {
                case ProofOutcome.Top: tag = "warning"; break;

                case ProofOutcome.False: tag = "error"; break;

                default: tag = ""; break;
                }
                ICodeAction action     = null; // No action for a warning/error. Suggestions have actions.
                var         msg        = String.Format("{0}: {1}", tag, String.Format(format, args));
                var         startIndex = 0;
                var         length     = 0;
                var         pc         = witness.PC.PrimaryMethodLocation();

                if (pc.HasRealSourceContext)
                {
                    startIndex = pc.Block.SourceStartIndex(pc);
                    length     = pc.Block.SourceLength(pc);

                    var startLine   = pc.Block.SourceStartLine(pc);
                    var startColumn = pc.Block.SourceStartColumn(pc);

                    this.results.Add(new ClousotOutput(witness.Outcome, msg, new TextSpan(startIndex, length), new Tuple <int, int>(startLine, startColumn),
                                                       action, ClousotOutput.ExtraInfo.None));
                    return(true);
                }
                if (pc.InsideEnsuresInMethod) // postcondition failures don't have good locations
                {
                    MethodReferenceAdaptor meth;
                    var ok = pc.TryGetContainingMethod(out meth);
                    if (ok && IteratorHelper.EnumerableIsNotEmpty(meth.reference.Locations))
                    {
                        var l = IteratorHelper.First(meth.reference.Locations) as ISourceLocation;
                        if (l != null)
                        {
                            startIndex = l.StartIndex;
                            length     = l.Length;
                            var s = new TextSpan(startIndex, length);
                            BaseMethodDeclarationSyntax m;
                            if (this.span2Method.TryGetValue(s, out m))
                            {
                                List <TextSpan> returnSpans = null;
                                if (m is MethodDeclarationSyntax) // otherwise it is a constructor
                                {
                                    returnSpans = ReturnStatementFinder.GetReturnStatementSpans(m as MethodDeclarationSyntax);
                                }
                                if (returnSpans == null || returnSpans.Count == 0)
                                {
                                    if (m.Body == null)
                                    {
                                        this.results.Add(new ClousotOutput(witness.Outcome, msg, m.GetLastToken().Span, null, action, ClousotOutput.ExtraInfo.None));
                                        return(true);
                                    }
                                    else
                                    {
                                        // just use method's closing curly
                                        this.results.Add(new ClousotOutput(witness.Outcome, msg, m.Body.CloseBraceToken.Span, null, action, ClousotOutput.ExtraInfo.None));
                                        return(true);
                                    }
                                }
                                else
                                {
                                    foreach (var returnSpan in returnSpans)
                                    {
                                        this.results.Add(new ClousotOutput(witness.Outcome, msg, returnSpan, null, action, ClousotOutput.ExtraInfo.None));
                                    }
                                    return(true);
                                }
                            }
                        }
                    }
                }
                return(false);
            }