Example #1
0
        private static void AddFix(
            CodeFix fix, UnifiedSuggestedAction suggestedAction,
            IDictionary <CodeFixGroupKey, IList <UnifiedSuggestedAction> > map,
            ArrayBuilder <CodeFixGroupKey> order)
        {
            var groupKey = GetGroupKey(fix);

            if (!map.ContainsKey(groupKey))
            {
                order.Add(groupKey);
                map[groupKey] = ImmutableArray.CreateBuilder <UnifiedSuggestedAction>();
            }

            map[groupKey].Add(suggestedAction);
            return;
Example #2
0
 public CodeFixSuggestedAction(
     Workspace workspace,
     ITextBuffer subjectBuffer,
     ICodeActionEditHandlerService editHandler,
     IWaitIndicator waitIndicator,
     CodeFix fix,
     CodeAction action,
     object provider,
     SuggestedActionSet fixAllSuggestedActionSet,
     IAsynchronousOperationListener operationListener)
     : base(workspace, subjectBuffer, editHandler, waitIndicator, action, provider, operationListener)
 {
     _fix = fix;
     _fixAllSuggestedActionSet = fixAllSuggestedActionSet;
 }
Example #3
0
            private static void AddFix(
                CodeFix fix, SuggestedAction suggestedAction,
                IDictionary <CodeFixGroupKey, IList <SuggestedAction> > map,
                ArrayBuilder <CodeFixGroupKey> order)
            {
                var diag = fix.GetPrimaryDiagnosticData();

                var groupKey = new CodeFixGroupKey(diag, fix.Action.Priority);

                if (!map.ContainsKey(groupKey))
                {
                    order.Add(groupKey);
                    map[groupKey] = ImmutableArray.CreateBuilder <SuggestedAction>();
                }

                map[groupKey].Add(suggestedAction);
            }
Example #4
0
 public CodeFixSuggestedAction(
     IThreadingContext threadingContext,
     SuggestedActionsSourceProvider sourceProvider,
     Workspace workspace,
     Solution originalSolution,
     ITextBuffer subjectBuffer,
     CodeFix fix,
     object provider,
     CodeAction action,
     SuggestedActionSet fixAllFlavors)
     : base(threadingContext,
            sourceProvider,
            workspace,
            originalSolution,
            subjectBuffer,
            provider,
            action,
            fixAllFlavors)
 {
     CodeFix = fix;
 }
        private static void SuggestPostConditionViaPex <Local, Parameter, Method, Field, Property, Event, Type, Expression, Variable, Attribute, Assembly>(IExpressionContext <Local, Parameter, Method, Field, Type, Expression, Variable> context, int rank, string symptom, IOutput output, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, string post) where Type : IEquatable <Type>
        {
#if INCLUDE_PEXINTEGRATION
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            if (feedbackManager != null)
            {
                MethodDefinitionName target = Translate(context.MethodContext.CurrentMethod, output, mdDecoder);

                string     group = Guid.NewGuid().ToString();
                var        microsoftContractsAssembly = ShortAssemblyName.FromName("Microsoft.Contracts");
                CodeUpdate update =
                    CodeUpdate.InsertCheck("Ensures", target, MakePostconditionString(post), new[] { "System.Diagnostics.Contracts" }, new[] { microsoftContractsAssembly }, "Clousot");

                CodeFix fix = CodeFix.FromUpdate("Clousot", symptom, group, update, rank, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                }
                catch { }
            }
#endif
        }
        private static bool TryAddRegressionAttributeViaPex <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly>(Assembly assembly, string message, IDecodeMetaData <Local, Parameter, Method, Field, Property, Event, Type, Attribute, Assembly> mdDecoder, System.Type attributeType)
        {
            IFeedbackManager feedbackManager = GetFeedbackConnection();
            bool             success         = true;

            if (feedbackManager != null)
            {
                ConstructorInfo attrCtor = attributeType.GetConstructor(new System.Type[1] {
                    typeof(string)
                });

#if DEBUG_PEX_BY_XML
                SafeSimpleXmlWriter writer = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + ".xml"), true);
                target.WriteXml(writer, "method");
                writer.Close();

                SafeSimpleXmlWriter writer2 = SafeSimpleXmlWriter.Create(new StreamWriter(@"C:\temp\" + mdDecoder.Name(method) + "2.xml"), true);
                attributeCtorName2.WriteXml(writer2, "method");
                writer2.Close();
#endif
                string           group = Guid.NewGuid().ToString();
                ICustomAttribute ca    = new CustomAttributeBuilder(MetadataFromReflection.GetMethod(attrCtor), MetadataExpression.String(message));

                CodeUpdate update =
                    CodeUpdate.AddAttribute("Regression",
                                            new Microsoft.ExtendedReflection.Metadata.Names.ShortAssemblyName(mdDecoder.Name(assembly), null),
                                            ca);

                CodeFix fix = CodeFix.FromUpdate("ClousotRegression", "missing regression attribute", group, update, 100, CodeFixImage.Message);
                try
                {
                    feedbackManager.AddFix(fix);
                    success = true;
                }
                catch { }
            }
            return(success);
        }
Example #7
0
 public FixableDiagnostic(Parsing.TextSpan span, DiagnosticLevel level, string code, string message, CodeFix fix)
     : base(span, level, code, message)
 {
     this.fix             = fix;
     this.additionalFixes = null;
 }
Example #8
0
 public static ImmutableArray <DiagnosticData> GetDiagnosticData(this CodeFix fix)
 {
     return(fix.Diagnostics.SelectAsArray(d => d.ToDiagnosticData(fix.Project)));
 }
Example #9
0
 public static DiagnosticData GetPrimaryDiagnosticData(this CodeFix fix)
 {
     return(fix.PrimaryDiagnostic.ToDiagnosticData(fix.Project));
 }
Example #10
0
//	public static string Translate(string code){
//		Convert_Click ();
//		return Result;

//	}

    void Update()
    {
        instance = this;
    }
Example #11
0
 private static CommandOrCodeAction CreateQuickFix(DocumentUri uri, CompilationContext context, CodeFix fix)
 {
     return(new CodeAction
     {
         Kind = CodeActionKind.QuickFix,
         Title = fix.Description,
         IsPreferred = fix.IsPreferred,
         Edit = new WorkspaceEdit
         {
             Changes = new Dictionary <DocumentUri, IEnumerable <TextEdit> >
             {
                 [uri] = fix.Replacements.Select(replacement => new TextEdit
                 {
                     Range = replacement.ToRange(context.LineStarts),
                     NewText = replacement.Text
                 })
             }
         }
     });
 }
Example #12
0
 public FixableErrorDiagnostic(Parser.TextSpan span, string code, string message, CodeFix fix, params CodeFix[] additionalFixes)
     : this(span, code, message, fix)
 {
     this.additionalFixes = additionalFixes;
 }
Example #13
0
 public FixableErrorDiagnostic(Parser.TextSpan span, string code, string message, CodeFix fix)
     : base(span, code, message)
 {
     this.fix             = fix;
     this.additionalFixes = null;
 }
Example #14
0
 public FixableDiagnostic(Parsing.TextSpan span, DiagnosticLevel level, string code, string message, Uri?documentationUri, DiagnosticStyling styling, CodeFix fix)
     : base(span, level, code, message, documentationUri, styling)
 {
     this.fix             = fix;
     this.additionalFixes = null;
 }
Example #15
0
 public FixableErrorDiagnostic(Parsing.TextSpan span, string code, string message, Uri?documentationUri, DiagnosticLabel?label, CodeFix fix)
     : base(span, code, message, documentationUri, label)
 {
     this.fix             = fix;
     this.additionalFixes = null;
 }
Example #16
0
            void IOutput.Suggestion(string kind, APC pc, string suggestion, List <uint> causes)
            {
                var isExtractMethodSuggestion = kind.Contains("for the extracted method");
                var isInvariantAtSuggestion   = kind.Contains("abstract state");

                Contract.Assert(!isExtractMethodSuggestion || isExtractMethodSuggestion != isInvariantAtSuggestion);

                var extraInfo = ClousotOutput.ExtraInfo.None;

                if (isExtractMethodSuggestion)
                {
                    extraInfo = ClousotOutput.ExtraInfo.IsExtractMethodSuggestion;
                }
                if (isInvariantAtSuggestion)
                {
                    extraInfo = ClousotOutput.ExtraInfo.IsInvariantAtSuggestion;
                }

                var msg = String.Format("Suggestion: {0}", suggestion);

                if (kind.Contains("Code fix") && pc.HasRealSourceContext)
                {
                    var si       = pc.Block.SourceStartIndex(pc);
                    var l        = pc.Block.SourceLength(pc);
                    var fixIndex = suggestion.IndexOf("Fix:");
                    if (fixIndex == -1)
                    { // TODO: cater to other kinds of fixes (adds, delete)
                        return;
                    }
                    var newText = suggestion.Substring(fixIndex + 5);
                    var sp      = new TextSpan(si, l);
                    var a       = new CodeFix(this.document, sp, newText);
                    this.results.Add(new ClousotOutput(null /* no outcome */, msg, new TextSpan(si, l), null, a, ClousotOutput.ExtraInfo.None));

                    return;
                }

                // TODO: It would be better to have a different way to tell the issue provider
                // that this issue is related to the method as a whole and not any particular
                // location so that it could decide on what span to attach the issue to.
                var startIndex = 0;
                var length     = 0;
                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 span           = new TextSpan(startIndex, length); // should be the span of the method name
                var st             = this.document.GetSyntaxTree();
                var roslynLocation = st.GetLocation(span);
                BaseMethodDeclarationSyntax method;

                if (this.span2Method.TryGetValue(span, out method))
                {
                    if (!isExtractMethodSuggestion && !isInvariantAtSuggestion)
                    {
                        if (method.ContainsSuggestion(suggestion))
                        {
                            return;
                        }

                        var action = new ContractInjector(this.document, kind.ToContractKind(), suggestion, method);
                        this.results.Add(new ClousotOutput(null /* no outcome */, msg, span, null, (ICodeAction)action, extraInfo));
                    }
                    else
                    {
                        this.results.Add(new ClousotOutput(null /* no outcome */, msg, span, null, (ICodeAction)null, extraInfo));
                    }
                }
                else
                {
                    this.results.Add(new ClousotOutput(null /* no outcome */, msg, span, null, (ICodeAction)null, extraInfo));
                }
            }
Example #17
0
 public FixableDiagnostic(Parsing.TextSpan span, DiagnosticLevel level, string code, string message, Uri?documentationUri, DiagnosticStyling styling, CodeFix fix, params CodeFix[] additionalFixes)
     : this(span, level, code, message, documentationUri, styling, fix)
 {
     this.additionalFixes = additionalFixes;
 }
Example #18
0
 public FixableErrorDiagnostic(Parsing.TextSpan span, string code, string message, Uri?documentationUri, DiagnosticLabel?label, CodeFix fix, params CodeFix[] additionalFixes)
     : this(span, code, message, documentationUri, label, fix)
 {
     this.additionalFixes = additionalFixes;
 }
Example #19
0
 public FixableDiagnostic(Parsing.TextSpan span, DiagnosticLevel level, string code, string message, CodeFix fix, params CodeFix[] additionalFixes)
     : this(span, level, code, message, fix)
 {
     this.additionalFixes = additionalFixes;
 }