コード例 #1
0
        private MethodDeclarationSyntax ExtractAndAddPsQs(ref CancellationToken cancellationToken, Microsoft.CodeAnalysis.Document localDocument, CompilationUnitSyntax newRoot, MethodDeclarationSyntax newExtractedMethod)
        {
            // Let Clousot see the rewritten unit

            var connectionToClousot = new RoslynToCCICodeModel.ClousotGlue();

            foreach (var result in connectionToClousot.AnalyzeMeAUnit(localDocument, newRoot, cancellationToken, this.options,
                                                                      new string[] { "-extractmethodmode", "-arrays:arraypurity", "-suggest:requires" /*"-memberNameSelect",*/ }))
            {
                Utils.Trace("Getting Clousot Result");

                String       condition;
                ContractKind kind;
                if (result.IsExtractMethodSuggestion && result.Message.TryParseSuggestion(out kind, out condition))
                {
                    Utils.Trace("Inserting Clousot result. New Condition: " + condition);

                    // Parse the condition
                    var newContract = SyntaxFactory.ParseStatement(condition + Environment.NewLine);

                    newExtractedMethod = newExtractedMethod.InsertStatements(newContract, kind);
                }
                else
                {
                    Utils.Trace("Skipped Clousot output : " + result.Message);
                }
            }

            return(newExtractedMethod);
        }
コード例 #2
0
        private MethodDeclarationSyntax ExtractAndAddPmQm(ref CancellationToken cancellationToken,
                                                          Microsoft.CodeAnalysis.Document localDocument, SyntaxNode oldRoot, MethodDeclarationSyntax newExtractedMethod)
        {
            var newText = oldRoot.ToFullString();

            Utils.DumpCSFile("BeforePmQm.cs", newText);

            var newSolution = localDocument.Project.Solution.WithDocumentText(localDocument.Id, new StringText(newText));
            //var newSolution = localDocument.Project.Solution.UpdateDocument(localDocument.Id, new StringText(newText));
            var newLocalDocument = newSolution.GetDocument(localDocument.Id);

            localDocument = null; // to be sure we do not use it

            var newTree = newLocalDocument.GetSyntaxTree();

            Utils.Trace("Running Clousot to infer <Pm, Qm> and <Pr, Qr>");

            var newRoot = (CompilationUnitSyntax)newTree.GetRoot();

            if (newRoot.ContainsDiagnostics)
            {
                Utils.Trace("Aborting: the new tree has syntax errors");

                return(null);
            }

            // Let Clousot see the rewritten unit
            var connectionToClousot = new RoslynToCCICodeModel.ClousotGlue();

            foreach (var result in connectionToClousot.AnalyzeMeAUnit(newLocalDocument, newRoot, cancellationToken, this.options,
                                                                      new string[] { "-extractmethodmoderefine", newExtractedMethod.Identifier.ValueText, "-premode", "combined", "-suggest", "methodensures", "-suggest:requires" }))
            {
                Utils.Trace("Getting Clousot Result");

                String       condition;
                ContractKind kind;
                if (result.Message.TryParseSuggestion(out kind, out condition))
                {
                    Utils.Trace("Inserting Clousot result. New Condition: " + condition);

                    // Parse the condition
                    var newContract = SyntaxFactory.ParseStatement(condition + Environment.NewLine);

                    if (!newContract.ContainsDiagnostics)
                    {
                        newExtractedMethod = newExtractedMethod.InsertStatements(newContract, kind);
                    }
                    else
                    {
                        Utils.Trace("\tskipped, as it contains syntax errors");
                    }
                }
            }

            return(newExtractedMethod);
        }
コード例 #3
0
    private MethodDeclarationSyntax ExtractAndAddPmQm(ref CancellationToken cancellationToken,
      Microsoft.CodeAnalysis.Document localDocument, SyntaxNode oldRoot, MethodDeclarationSyntax newExtractedMethod) {
      var newText = oldRoot.ToFullString();

      cancellationToken.ThrowIfCancellationRequested();

      Utils.DumpCSFile("BeforePmQm.cs", newText);

      var newSolution = localDocument.Project.Solution.WithDocumentText(localDocument.Id, SourceText.From(newText));
      //var newSolution = localDocument.Project.Solution.UpdateDocument(localDocument.Id, SourceText.From(newText));
      var newLocalDocument = newSolution.GetDocument(localDocument.Id);

      localDocument = null; // to be sure we do not use it

      var newTree = newLocalDocument.GetSyntaxTree();

      Utils.Trace("Running Clousot to infer <Pm, Qm> and <Pr, Qr>");

      var newRoot = (CompilationUnitSyntax)newTree.GetRoot();
      if (newRoot.ContainsDiagnostics) {
        Utils.Trace("Aborting: the new tree has syntax errors");

        return null;
      }

      // Let Clousot see the rewritten unit
      var connectionToClousot = new RoslynToCCICodeModel.ClousotGlue();

      var results = connectionToClousot.AnalyzeMeAUnit(newLocalDocument, newRoot, cancellationToken, this.options,
        new string[] { "-extractmethodmoderefine", newExtractedMethod.Identifier.ValueText, "-premode", "combined", "-suggest", "methodensures", "-suggest:requires" });

      if (results == null) return null;

      foreach (var result in results)
      {
        cancellationToken.ThrowIfCancellationRequested();

        Utils.Trace("Getting Clousot Result");

        String condition;
        ContractKind kind;
        if (result.Message.TryParseSuggestion(out kind, out condition)) {
          Utils.Trace("Inserting Clousot result. New Condition: " + condition);

          // Parse the condition
          var newContract = SyntaxFactory.ParseStatement(condition + Environment.NewLine);

          if (!newContract.ContainsDiagnostics) {
            newExtractedMethod = newExtractedMethod.InsertStatements(newContract, kind);
          } else {
            Utils.Trace("\tskipped, as it contains syntax errors");
          }
        }
      }

      return newExtractedMethod;
    }
コード例 #4
0
    private MethodDeclarationSyntax ExtractAndAddPsQs(ref CancellationToken cancellationToken, Microsoft.CodeAnalysis.Document localDocument, CompilationUnitSyntax newRoot, MethodDeclarationSyntax newExtractedMethod)
    {
      // Let Clousot see the rewritten unit

      var connectionToClousot = new RoslynToCCICodeModel.ClousotGlue();
      var results = connectionToClousot.AnalyzeMeAUnit(localDocument, newRoot, cancellationToken, this.options,
        new string[] { "-extractmethodmode", "-arrays:arraypurity", "-suggest:requires" /*"-memberNameSelect",*/ });
      if (results == null) return null;
      foreach (var result in results)
      {
        cancellationToken.ThrowIfCancellationRequested();
        Utils.Trace("Getting Clousot Result");

        String condition;
        ContractKind kind;
        if (result.IsExtractMethodSuggestion && result.Message.TryParseSuggestion(out kind, out condition))
        {
          Utils.Trace("Inserting Clousot result. New Condition: " + condition);

          // Parse the condition
          var newContract = SyntaxFactory.ParseStatement(condition + Environment.NewLine);

          newExtractedMethod = newExtractedMethod.InsertStatements(newContract, kind);
        }
        else
        {
          Utils.Trace("Skipped Clousot output : " + result.Message);
        }
      }

      return newExtractedMethod;
    }
コード例 #5
0
        // TODO: refactor this code to share with DoTheWork
        private string DoTheWorkToInferInvariants()
        {
            // We should move this in the UI
            var selectionStart = this.wpfTextView.Selection.Start.Position.Position;
            var selectionEnd   = this.wpfTextView.Selection.End.Position.Position;

            if (selectionStart == selectionEnd)
            {
                Trace.WriteLine(string.Format("Selection: {0}", selectionStart));

                var container = wpfTextView.TextBuffer.AsTextContainer();
                //var text = wpfTextView.TextBuffer.CurrentSnapshot.AsText();
                Workspace workspace;
                Workspace.TryGetWorkspace(container, out workspace);
                //var workspaceDiscovery = Helper.GetMefService<IWorkspaceDiscoveryService>();
                //var workspace = workspaceDiscovery.GetWorkspace(container);

                if (workspace != null)
                {
                    Document doc;
                    var      docIds = workspace.GetRelatedDocumentIds(container);
                    if (docIds.Any())
                    {
                        var docId = docIds.First();
                        doc = Helper.OpenDocument(workspace, docId);
                        var syntax = doc.GetSyntaxTree();
                        var source = syntax.GetText().ToString();

                        // Generate the new text
                        var newText = GenerateAdornedSource(doc, selectionStart);

                        var newSolution = doc.Project.Solution.WithDocumentText(doc.Id, SourceText.From(newText));
                        //var newSolution = doc.Project.Solution.UpdateDocument(doc.Id, StringText.From(newText));
                        var locDocument = newSolution.GetDocument(doc.Id);
                        var newTree     = (CompilationUnitSyntax)locDocument.GetSyntaxTree().GetRoot();

                        if (!newTree.ContainsDiagnostics)
                        {
                            try
                            {
                                Trace.WriteLine(string.Format("Annotated tree for the state:\n{0}", newText));

                                var clousotConnection = new RoslynToCCICodeModel.ClousotGlue();
                                var run = clousotConnection.AnalyzeMeAUnit(locDocument,
                                                                           newTree,
                                                                           new System.Threading.CancellationToken(),
                                                                           this.options,
                                                                           new string[] { "-warnscores:false", "-sortwarns:false", "-suggest:!!", "-invariantsuggestmode:true" },
                                                                           true // we want to filter all the messages but the answer of Clousot
                                                                           );
                                if (run == null)
                                {
                                    return(null);
                                }
                                foreach (var r in run)
                                {
                                    Trace.WriteLine(string.Format("{0}: {1}", r.Message, r.outcome));

                                    if (r.IsInvariantAtSuggestion)
                                    {
                                        return(PrettyFormat(r.Message));
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Trace.WriteLine(string.Format("Some exception was thrown inside Clousot...\nDetails:\n{0}", e.ToString()));
                            }
                        }
                        else
                        {
                            Trace.WriteLine("Compilation error from Roslyn");
                        }
                    }
                }
            }
            else
            {
                Trace.WriteLine(string.Format("start/end do not match {0} {1}", selectionStart, selectionEnd));
            }

            return(null);
        }
コード例 #6
0
        private ProofOutcome?DoTheWork(string query, IEnumerable <CodeAssumption> assumptions)
        {
            // We should move this in the UI
            var selectionStart = this.wpfTextView.Selection.Start.Position.Position;
            var selectionEnd   = this.wpfTextView.Selection.End.Position.Position;

            if (selectionStart == selectionEnd)
            {
                Trace.WriteLine(string.Format("Selection: {0}", selectionStart));

                var container = wpfTextView.TextBuffer.AsTextContainer();
                //var text = wpfTextView.TextBuffer.CurrentSnapshot.AsText();
                Workspace workspace;
                Workspace.TryGetWorkspace(container, out workspace);
                //var workspaceDiscovery = Helper.GetMefService<IWorkspaceDiscoveryService>();
                //var workspace = workspaceDiscovery.GetWorkspace(container);

                if (workspace != null)
                {
                    Document doc;
                    var      docIds = workspace.GetRelatedDocumentIds(container);
                    if (docIds.Any())
                    {
                        var docId = docIds.First();
                        doc = Helper.OpenDocument(workspace, docId);
                        var syntax = doc.GetSyntaxTree();
                        var source = syntax.GetText().ToString();

                        var newText = GenerateInstrumentedSourceText(query, assumptions, selectionStart, source);

                        var newSolution = doc.Project.Solution.WithDocumentText(doc.Id, SourceText.From(newText));
                        //var newSolution = doc.Project.Solution.UpdateDocument(doc.Id, StringText.From(newText));
                        var locDocument   = newSolution.GetDocument(doc.Id);
                        var newTree       = (Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax)locDocument.GetSyntaxTree().GetRoot();
                        var adornedSource = GenerateAdornedSource(doc, selectionStart);

                        if (!newTree.ContainsDiagnostics)
                        {
                            try
                            {
                                var clousotConnection = new RoslynToCCICodeModel.ClousotGlue();
                                var run = clousotConnection.AnalyzeMeAUnit(locDocument,
                                                                           newTree,
                                                                           new System.Threading.CancellationToken(),
                                                                           this.options,
                                                                           new string[] { "-warnscores:false", "-sortwarns:false", "-show:validations", "-show:unreached" },
                                                                           true // we want to filter all the messages but the answer of Clousot
                                                                           );
                                if (run == null)
                                {
                                    return(null);
                                }

                                foreach (var r in run)
                                {
                                    Trace.WriteLine(string.Format("{0}: {1}", r.Message, r.outcome));

                                    // if we have a non-null, then this is the outcome
                                    // We can have outcome == null for e.g., suggestions.
                                    // In this case we simply ignore them
                                    if (r.outcome.HasValue)
                                    {
                                        return(r.outcome);
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Trace.WriteLine(string.Format("Some exception was thrown inside Clousot...\nDetails:\n{0}", e.ToString()));
                            }
                        }
                        else
                        {
                            Trace.WriteLine("Compilation error from Roslyn");
                        }
                    }
                }
            }
            else
            {
                Trace.WriteLine(string.Format("start/end do not match {0} {1}", selectionStart, selectionEnd));
            }

            return(null);
        }