Пример #1
0
        internal IVsWindowFrame NavigateTo(int index, bool isPreview)
        {
            if (isPreview)
            {
                return(null);
            }

            SarifError sarifError = _errors[index];

            IVsWindowFrame result;

            CodeAnalysisResultManager.Instance.TryNavigateTo(sarifError, out result);
            return(result);
        }
Пример #2
0
        private static void CreateAnnotatedCodeLocationCollections(
            IEnumerable <IEnumerable <AnnotatedCodeLocation> > codeLocationCollections,
            AnnotatedCodeLocationKind annotatedCodeLocationKind,
            SarifError sarifError)
        {
            if (codeLocationCollections == null)
            {
                return;
            }

            foreach (IEnumerable <AnnotatedCodeLocation> codeLocations in codeLocationCollections)
            {
                CaptureAnnotatedCodeLocations(codeLocations, annotatedCodeLocationKind, sarifError);
            }
        }
Пример #3
0
        private void AddResult(Run runLog, List <SarifError> sarifErrors, string toolName, Result result, string category, string document, Region region)
        {
            IRule  rule;
            string shortMessage, fullMessage;

            rule         = GetRule(runLog, result.RuleId);
            shortMessage = result.GetMessageText(rule, concise: true);
            fullMessage  = result.GetMessageText(rule, concise: false);

            if (shortMessage == fullMessage)
            {
                fullMessage = null;
            }

            SarifError sarifError = new SarifError(document)
            {
                Region       = region,
                RuleId       = result.RuleId,
                RuleName     = rule?.Name,
                Kind         = result.Kind,
                Category     = category,
                ShortMessage = shortMessage,
                FullMessage  = fullMessage,
                Tool         = toolName,
                HelpLink     = rule?.HelpUri?.ToString()
            };

            IEnumerable <IEnumerable <AnnotatedCodeLocation> > stackLocations    = CreateAnnotatedCodeLocationsFromStacks(result.Stacks);
            IEnumerable <IEnumerable <AnnotatedCodeLocation> > codeFlowLocations = CreateAnnotatedCodeLocationsFromCodeFlows(result.CodeFlows);

            CreateAnnotatedCodeLocationCollections(stackLocations, AnnotatedCodeLocationKind.Stack, sarifError);
            CreateAnnotatedCodeLocationCollections(codeFlowLocations, AnnotatedCodeLocationKind.CodeFlow, sarifError);
            CaptureAnnotatedCodeLocations(result.RelatedLocations, AnnotatedCodeLocationKind.Stack, sarifError);

            if (region != null)
            {
                sarifError.ColumnNumber = region.StartColumn - 1;
                sarifError.LineNumber   = region.StartLine - 1;
            }

            sarifErrors.Add(sarifError);
        }
            public override void PreprocessNavigate(ITableEntryHandle entryHandle, TableEntryNavigateEventArgs e)
            {
                int index;
                ITableEntriesSnapshot snapshot;

                if (!entryHandle.TryGetSnapshot(out snapshot, out index))
                {
                    return;
                }

                var sarifSnapshot = snapshot as SarifSnapshot;

                if (sarifSnapshot == null)
                {
                    return;
                }

                e.Handled = true;
                DeselectItems(snapshot);

                SarifError sarifError = sarifSnapshot.GetItem(index);

                IVsWindowFrame frame;

                if (!CodeAnalysisResultManager.Instance.TryNavigateTo(sarifError, out frame))
                {
                    //return;
                }

                if (sarifError.HasDetails)
                {
                    OpenOrReplaceVerticalContent(frame, sarifError);
                }
                else
                {
                    CloseVerticalContent(frame, sarifError);
                }
            }
        internal bool TryNavigateTo(SarifError sarifError, string fileName, string mimeType, Region region, ResultTextMarker marker, out IVsWindowFrame result)
        {
            result = null;

            if (string.IsNullOrEmpty(fileName))
            {
                return(false);
            }

            string remappedName = fileName;

            if (!File.Exists(fileName))
            {
                remappedName = RebaselineFileName(fileName);

                if (!File.Exists(remappedName))
                {
                    return(false);
                }
            }
            CodeAnalysisResultManager.Instance.RemapFileNames(fileName, remappedName);
            fileName = remappedName;

            NewLineIndex newLineIndex = null;

            if (!sarifError.RegionPopulated && mimeType != MimeType.Binary)
            {
                if (!_fileToNewLineIndexMap.TryGetValue(fileName, out newLineIndex))
                {
                    _fileToNewLineIndexMap[fileName] = newLineIndex = new NewLineIndex(File.ReadAllText(fileName));
                }
                region.Populate(newLineIndex);
                sarifError.RegionPopulated = true;
            }
            marker.FullFilePath = remappedName;
            result = marker.NavigateTo(false, null, false);
            return(result != null);
        }
            private void OpenOrReplaceVerticalContent(IVsWindowFrame frame, SarifError error)
            {
                IVsTextView textView = GetTextViewFromFrame(frame);

                if (textView == null)
                {
                    return;
                }

                CodeLocations codeLocations = new CodeLocations();

                codeLocations.CurrentSarifError = error;

                // TODO: this needs to be a public API
                var type = textView.GetType();
                var mi   = type.GetMethod(
                    "ShowAdditionalContent",
                    new Type[] { typeof(FrameworkElement), typeof(String) });

                int cookie = (int)mi.Invoke(textView, new object[] { codeLocations, "Code Analysis Details" });

                cookieMap.Remove(textView);
                cookieMap.Add(textView, new StrongBox <int>(cookie));
            }
            private void CloseVerticalContent(IVsWindowFrame frame, SarifError item)
            {
                IVsTextView textView = GetTextViewFromFrame(frame);

                if (textView == null)
                {
                    return;
                }

                StrongBox <int> cookie;

                if (cookieMap.TryGetValue(textView, out cookie))
                {
                    // TODO: this needs to be a public API
                    var type = textView.GetType();
                    var mi   = type.GetMethod(
                        "HideVerticalContent",
                        new Type[] { typeof(int) });

                    mi.Invoke(textView, new object[] { cookie.Value });

                    cookieMap.Remove(textView);
                }
            }
 private void SelectItem(SarifError item)
 {
     // TODO
 }
Пример #9
0
        private static void CaptureAnnotatedCodeLocations(IEnumerable <AnnotatedCodeLocation> codeLocations, AnnotatedCodeLocationKind annotatedCodeLocationKind, SarifError sarifError)
        {
            if (codeLocations == null)
            {
                return;
            }

            int annotationCollectionCount = 0;

            foreach (AnnotatedCodeLocation codeLocation in codeLocations)
            {
                PhysicalLocation plc = codeLocation.PhysicalLocation;
                sarifError.Annotations.Add(new AnnotatedCodeLocationModel()
                {
                    Index    = annotationCollectionCount,
                    Kind     = annotatedCodeLocationKind,
                    Region   = plc.Region,
                    FilePath = plc.Uri.LocalPath,
                    Message  = codeLocation.Message
                });

                annotationCollectionCount++;
            }
        }
Пример #10
0
        private void WriteRunToErrorList(Run runLog)
        {
            List <SarifError> sarifErrors = new List <SarifError>();

            // Prefer optional fullName,  fall back to required Name property
            string toolName = runLog.Tool.FullName ?? runLog.Tool.Name;

            foreach (Result result in runLog.Results)
            {
                string       category, document, shortMessage, fullMessage;
                Region       region;
                NewLineIndex newLineIndex;
                IRule        rule = null;

                category = null;

                if (result.Properties != null)
                {
                    result.Properties.TryGetValue("category", out category);
                }

                foreach (Location location in result.Locations)
                {
                    region = null;

                    PhysicalLocation physicalLocation = null;
                    if (location.ResultFile != null)
                    {
                        physicalLocation = location.ResultFile;
                        document         = physicalLocation.Uri.LocalPath;
                        region           = physicalLocation.Region;
                    }
                    else if (location.AnalysisTarget != null)
                    {
                        physicalLocation = location.AnalysisTarget;
                        document         = physicalLocation.Uri.LocalPath;
                        region           = physicalLocation.Region;
                    }
                    else
                    {
                        document = location.FullyQualifiedLogicalName;
                    }

                    rule         = GetRule(runLog, result.RuleId);
                    shortMessage = result.GetMessageText(rule, concise: true);
                    fullMessage  = result.GetMessageText(rule, concise: false);

                    if (shortMessage == fullMessage)
                    {
                        fullMessage = null;
                    }

                    SarifError sarifError = new SarifError(document)
                    {
                        Region       = region,
                        RuleId       = result.RuleId,
                        RuleName     = rule?.Name,
                        Kind         = result.Kind,
                        Category     = category,
                        ShortMessage = shortMessage,
                        FullMessage  = fullMessage,
                        Tool         = toolName,
                        HelpLink     = rule?.HelpUri?.ToString()
                    };

                    CaptureAnnotatedCodeLocationCollections(result.Stacks, AnnotatedCodeLocationKind.Stack, sarifError);
                    CaptureAnnotatedCodeLocationCollections(result.CodeFlows, AnnotatedCodeLocationKind.CodeFlow, sarifError);
                    CaptureAnnotatedCodeLocations(result.RelatedLocations, AnnotatedCodeLocationKind.Stack, sarifError);

                    if (region != null)
                    {
                        sarifError.ColumnNumber = region.StartColumn - 1;
                        sarifError.LineNumber   = region.StartLine - 1;
                    }

                    sarifErrors.Add(sarifError);
                }

                // TODO zap this on implementing todo above
                if (result.RelatedLocations != null)
                {
                    foreach (AnnotatedCodeLocation annotation in result.RelatedLocations)
                    {
                        PhysicalLocation physicalLocation = annotation.PhysicalLocation;
                        region       = physicalLocation.Region;
                        shortMessage = annotation.Message;
                        document     = annotation.PhysicalLocation.Uri.LocalPath;

                        if (!this.documentToLineIndexMap.TryGetValue(document, out newLineIndex))
                        {
                            this.documentToLineIndexMap[document] = newLineIndex = new NewLineIndex(File.ReadAllText(document));
                        }

                        if (region != null)
                        {
                            region.Populate(newLineIndex);
                        }

                        SarifError sarifError = new SarifError(document)
                        {
                            Region       = region,
                            RuleId       = result.RuleId,
                            RuleName     = rule?.Name,
                            Kind         = ResultKind.Note,
                            Category     = "Related Location", // or should we prefer original result category?
                            ShortMessage = shortMessage,
                            FullMessage  = null,
                            Tool         = toolName
                        };

                        if (region != null)
                        {
                            sarifError.ColumnNumber = region.StartColumn - 1;
                            sarifError.LineNumber   = region.StartLine - 1;
                        }

                        sarifErrors.Add(sarifError);
                    }
                }

                CodeAnalysisResultManager.Instance.SarifErrors = sarifErrors;
                SarifTableDataSource.Instance.AddErrors(sarifErrors);
            }
        }
 internal static bool CanNavigateTo(SarifError sarifError)
 {
     throw new NotImplementedException();
 }
 internal bool TryNavigateTo(SarifError sarifError, out IVsWindowFrame result)
 {
     CodeAnalysisResultManager.Instance.CurrentSarifError = sarifError;
     return(TryNavigateTo(sarifError, sarifError.FileName, MimeType.Binary, sarifError.Region, sarifError.LineMarker, out result));
 }