internal ThreadFlowLocation CreateThreadFlowLocation(AnnotatedCodeLocationVersionOne v1AnnotatedCodeLocation)
        {
            ThreadFlowLocation threadFlowLocation = null;

            if (v1AnnotatedCodeLocation != null)
            {
                threadFlowLocation = new ThreadFlowLocation
                {
                    Importance   = Utilities.CreateThreadFlowLocationImportance(v1AnnotatedCodeLocation.Importance),
                    Location     = CreateLocation(v1AnnotatedCodeLocation),
                    Module       = v1AnnotatedCodeLocation.Module,
                    NestingLevel = _threadFlowLocationNestingLevel,
                    Properties   = v1AnnotatedCodeLocation.Properties,
                    State        = v1AnnotatedCodeLocation.State,
                };

                if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.Call)
                {
                    _threadFlowLocationNestingLevel++;
                }
                else if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.CallReturn)
                {
                    _threadFlowLocationNestingLevel--;
                }
            }

            return(threadFlowLocation);
        }
        internal ReportingDescriptor CreateRule(RuleVersionOne v1Rule)
        {
            ReportingDescriptor rule = null;

            if (v1Rule != null)
            {
                rule = new ReportingDescriptor
                {
                    FullDescription = CreateMultiformatMessageString(v1Rule.FullDescription),
                    HelpUri         = v1Rule.HelpUri,
                    Id               = v1Rule.Id,
                    MessageStrings   = v1Rule.MessageFormats.ConvertToMultiformatMessageStringsDictionary(),
                    Name             = v1Rule.Name,
                    Properties       = v1Rule.Properties,
                    ShortDescription = CreateMultiformatMessageString(v1Rule.ShortDescription)
                };

                FailureLevel level = Utilities.CreateReportingConfigurationDefaultLevel(v1Rule.DefaultLevel);

                rule.DefaultConfiguration = new ReportingConfiguration
                {
                    Level   = level,
                    Enabled = v1Rule.Configuration != RuleConfigurationVersionOne.Disabled
                };
            }

            return(rule);
        }
        internal Rule CreateRule(RuleVersionOne v1Rule)
        {
            Rule rule = null;

            if (v1Rule != null)
            {
                rule = new Rule
                {
                    FullDescription = CreateMessage(v1Rule.FullDescription),
                    HelpUri         = v1Rule.HelpUri,
                    Id               = v1Rule.Id,
                    MessageStrings   = v1Rule.MessageFormats,
                    Name             = CreateMessage(v1Rule.Name),
                    Properties       = v1Rule.Properties,
                    ShortDescription = CreateMessage(v1Rule.ShortDescription)
                };

                RuleConfigurationDefaultLevel level = Utilities.CreateRuleConfigurationDefaultLevel(v1Rule.DefaultLevel);

                if (v1Rule.Configuration == RuleConfigurationVersionOne.Enabled ||
                    level != RuleConfigurationDefaultLevel.Warning)
                {
                    rule.Configuration = new RuleConfiguration
                    {
                        DefaultLevel = level,
                        Enabled      = v1Rule.Configuration == RuleConfigurationVersionOne.Enabled
                    };
                }
            }

            return(rule);
        }
示例#4
0
        internal RuleVersionOne CreateRule(Rule v2Rule)
        {
            RuleVersionOne rule = null;

            if (v2Rule != null)
            {
                rule = new RuleVersionOne
                {
                    FullDescription = v2Rule.FullDescription?.Text,
                    HelpUri         = v2Rule.HelpUri,
                    Id               = v2Rule.Id,
                    MessageFormats   = v2Rule.MessageStrings,
                    Name             = v2Rule.Name?.Text,
                    Properties       = v2Rule.Properties,
                    ShortDescription = v2Rule.ShortDescription?.Text
                };

                if (v2Rule.Configuration != null)
                {
                    rule.Configuration = v2Rule.Configuration.Enabled ?
                                         RuleConfigurationVersionOne.Enabled :
                                         RuleConfigurationVersionOne.Disabled;
                    rule.DefaultLevel = Utilities.CreateResultLevelVersionOne(v2Rule.Configuration.DefaultLevel);
                }
            }

            return(rule);
        }
        internal static RuleVersionOne CreateRuleVersionOne(ReportingDescriptor v2ReportingDescriptor)
        {
            RuleVersionOne rule = null;

            if (v2ReportingDescriptor != null)
            {
                rule = new RuleVersionOne
                {
                    FullDescription = v2ReportingDescriptor.FullDescription?.Text,
                    HelpUri         = v2ReportingDescriptor.HelpUri,
                    Id               = v2ReportingDescriptor.Id,
                    MessageFormats   = ConvertToV1MessageStringsDictionary(v2ReportingDescriptor.MessageStrings),
                    Name             = v2ReportingDescriptor.Name,
                    Properties       = v2ReportingDescriptor.Properties,
                    ShortDescription = v2ReportingDescriptor.ShortDescription?.Text
                };

                if (v2ReportingDescriptor.DefaultConfiguration != null)
                {
                    rule.Configuration = v2ReportingDescriptor.DefaultConfiguration.Enabled ?
                                         RuleConfigurationVersionOne.Enabled :
                                         RuleConfigurationVersionOne.Disabled;
                    rule.DefaultLevel = Utilities.CreateResultLevelVersionOne(v2ReportingDescriptor.DefaultConfiguration.Level);
                }
            }

            return(rule);
        }
        internal CodeFlowLocation CreateCodeFlowLocation(AnnotatedCodeLocationVersionOne v1AnnotatedCodeLocation)
        {
            CodeFlowLocation codeFlowLocation = null;

            if (v1AnnotatedCodeLocation != null)
            {
                codeFlowLocation = new CodeFlowLocation
                {
                    Importance   = Utilities.CreateCodeFlowLocationImportance(v1AnnotatedCodeLocation.Importance),
                    Location     = CreateLocation(v1AnnotatedCodeLocation),
                    Module       = v1AnnotatedCodeLocation.Module,
                    NestingLevel = _codeFlowLocationNestingLevel,
                    Properties   = v1AnnotatedCodeLocation.Properties,
                    State        = v1AnnotatedCodeLocation.State,
                    Step         = v1AnnotatedCodeLocation.Step + _codeFlowLocationStepAdjustment
                };

                if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.Call)
                {
                    _codeFlowLocationNestingLevel++;
                }
                else if (v1AnnotatedCodeLocation.Kind == AnnotatedCodeLocationKindVersionOne.CallReturn)
                {
                    _codeFlowLocationNestingLevel--;
                }
            }

            return(codeFlowLocation);
        }
        internal AnnotatedCodeLocationVersionOne CreateAnnotatedCodeLocationVersionOne(ThreadFlowLocation v2ThreadFlowLocation)
        {
            AnnotatedCodeLocationVersionOne annotatedCodeLocation = null;

            if (v2ThreadFlowLocation != null)
            {
                annotatedCodeLocation = CreateAnnotatedCodeLocationVersionOne(v2ThreadFlowLocation.Location);
                annotatedCodeLocation = annotatedCodeLocation ?? new AnnotatedCodeLocationVersionOne();

                annotatedCodeLocation.Importance = Utilities.CreateAnnotatedCodeLocationImportance(v2ThreadFlowLocation.Importance);
                annotatedCodeLocation.Module     = v2ThreadFlowLocation.Module;
                annotatedCodeLocation.Properties = v2ThreadFlowLocation.Properties;
                annotatedCodeLocation.State      = v2ThreadFlowLocation.State;
                annotatedCodeLocation.Step       = v2ThreadFlowLocation.ExecutionOrder;
            }

            return(annotatedCodeLocation);
        }
        internal Notification CreateNotification(NotificationVersionOne v1Notification)
        {
            Notification notification = null;

            if (v1Notification != null)
            {
                notification = new Notification
                {
                    Exception        = CreateExceptionData(v1Notification.Exception),
                    Id               = v1Notification.Id,
                    Level            = Utilities.CreateFailureLevel(v1Notification.Level),
                    Message          = CreateMessage(v1Notification.Message),
                    PhysicalLocation = CreatePhysicalLocation(v1Notification.PhysicalLocation),
                    Properties       = v1Notification.Properties,
                    RuleId           = v1Notification.RuleId,
                    ThreadId         = v1Notification.ThreadId,
                    TimeUtc          = v1Notification.Time
                };
            }

            return(notification);
        }
        internal NotificationVersionOne CreateNotificationVersionOne(Notification v2Notification)
        {
            NotificationVersionOne notification = null;

            if (v2Notification != null)
            {
                notification = new NotificationVersionOne
                {
                    Exception        = CreateExceptionDataVersionOne(v2Notification.Exception),
                    Id               = v2Notification.Id,
                    Level            = Utilities.CreateNotificationLevelVersionOne(v2Notification.Level),
                    Message          = v2Notification.Message?.Text,
                    PhysicalLocation = CreatePhysicalLocationVersionOne(v2Notification.PhysicalLocation),
                    Properties       = v2Notification.Properties,
                    RuleId           = v2Notification.RuleId,
                    ThreadId         = v2Notification.ThreadId,
                    Time             = v2Notification.TimeUtc
                };
            }

            return(notification);
        }
        internal string AddLogicalLocation(LogicalLocation logicalLocation)
        {
            if (_currentRun.LogicalLocations == null)
            {
                _currentRun.LogicalLocations = new Dictionary <string, LogicalLocation>();
            }

            string fullyQualifiedName = logicalLocation.FullyQualifiedName;
            string logicalLocationKey = logicalLocation.FullyQualifiedName;
            int    disambiguator      = 0;

            while (_currentRun.LogicalLocations.ContainsKey(logicalLocationKey))
            {
                LogicalLocation logLoc = _currentRun.LogicalLocations[logicalLocationKey].DeepClone();
                logLoc.FullyQualifiedName = logLoc.FullyQualifiedName ?? fullyQualifiedName;
                logLoc.Name = logLoc.Name ?? GetLogicalLocationName(logLoc.FullyQualifiedName);

                // Compare only FQN and Name, since Kind, ParentKey, and DecoratedName on
                // our new LogicalLocation don't have values for those properties
                if (logicalLocation.FullyQualifiedName == logLoc.FullyQualifiedName &&
                    logicalLocation.Name == logLoc.Name)
                {
                    break;
                }

                logicalLocationKey = Utilities.CreateDisambiguatedName(fullyQualifiedName, disambiguator);
                disambiguator++;
            }

            if (!_currentRun.LogicalLocations.ContainsKey(logicalLocationKey))
            {
                _currentRun.LogicalLocations.Add(logicalLocationKey, logicalLocation);
                RemoveRedundantProperties(logicalLocationKey);
            }

            return(logicalLocationKey);
        }
示例#11
0
        internal Notification CreateNotification(NotificationVersionOne v1Notification)
        {
            Notification notification = null;

            if (v1Notification != null)
            {
                notification = new Notification
                {
                    Exception  = CreateExceptionData(v1Notification.Exception),
                    Level      = Utilities.CreateFailureLevel(v1Notification.Level),
                    Message    = CreateMessage(v1Notification.Message),
                    Locations  = CreateLocations(v1Notification.PhysicalLocation),
                    Properties = v1Notification.Properties,
                    ThreadId   = v1Notification.ThreadId,
                    TimeUtc    = v1Notification.Time
                };

                if (!string.IsNullOrWhiteSpace(v1Notification.Id))
                {
                    notification.Descriptor = new ReportingDescriptorReference
                    {
                        Id = v1Notification.Id,
                    };
                }

                if (!string.IsNullOrWhiteSpace(v1Notification.RuleId))
                {
                    notification.AssociatedRule = new ReportingDescriptorReference
                    {
                        Id = v1Notification.RuleId,
                    };
                }
            }

            return(notification);
        }
        internal Result CreateResult(ResultVersionOne v1Result)
        {
            Result result = null;

            if (v1Result != null)
            {
                result = new Result
                {
                    BaselineState     = Utilities.CreateBaselineState(v1Result.BaselineState),
                    CodeFlows         = v1Result.CodeFlows?.Select(CreateCodeFlow).ToList(),
                    Fixes             = v1Result.Fixes?.Select(CreateFix).ToList(),
                    InstanceGuid      = v1Result.Id,
                    Level             = Utilities.CreateFailureLevel(v1Result.Level),
                    Kind              = Utilities.CreateResultKind(v1Result.Level),
                    Locations         = v1Result.Locations?.Select(CreateLocation).ToList(),
                    Message           = CreateMessage(v1Result.Message),
                    Properties        = v1Result.Properties,
                    RelatedLocations  = v1Result.RelatedLocations?.Select(CreateLocation).ToList(),
                    Stacks            = v1Result.Stacks?.Select(CreateStack).ToList(),
                    SuppressionStates = Utilities.CreateSuppressionStates(v1Result.SuppressionStates)
                };

                // The v2 spec says that analysisTarget is required only if it differs from the result location.
                // On the other hand, the v1 spec says that if the result is found in the file that the tool
                // was instructed to scan, then analysisTarget should be present and resultFile should be
                // absent -- so we should _not_ populate the v2 analysisTarget in this case.
                LocationVersionOne v1Location = v1Result.Locations?[0];
                if (v1Location?.ResultFile != null && v1Location.AnalysisTarget?.Uri != v1Location.ResultFile.Uri)
                {
                    result.AnalysisTarget = CreateFileLocation(v1Result.Locations[0].AnalysisTarget);
                }

                result.RuleId = v1Result.RuleId;

                string ruleKey = v1Result.RuleKey ?? v1Result.RuleId;
                result.RuleIndex = GetRuleIndexForRuleKey(ruleKey, _v1RuleKeyToV2IndexMap);

                if (v1Result.FormattedRuleMessage != null)
                {
                    if (result.Message == null)
                    {
                        result.Message = new Message()
                        {
                            MessageId = v1Result.FormattedRuleMessage.FormatId
                        };
                    }

                    result.Message.Arguments = v1Result.FormattedRuleMessage.Arguments;
                }

                if (!string.IsNullOrWhiteSpace(v1Result.ToolFingerprintContribution))
                {
                    result.PartialFingerprints = new Dictionary <string, string>
                    {
                        { "Fingerprint", v1Result.ToolFingerprintContribution }
                    };
                }

                if (!string.IsNullOrWhiteSpace(v1Result.Snippet))
                {
                    if (result.Locations == null)
                    {
                        result.Locations = new List <Location>();
                    }

                    if (result.Locations.Count == 0)
                    {
                        result.Locations.Add(new Location());
                    }

                    if (result.Locations[0].PhysicalLocation == null)
                    {
                        result.Locations[0].PhysicalLocation = new PhysicalLocation();
                    }

                    if (result.Locations[0].PhysicalLocation.Region == null)
                    {
                        result.Locations[0].PhysicalLocation.Region = new Region();
                    }

                    result.Locations[0].PhysicalLocation.Region.Snippet = new ArtifactContent
                    {
                        Text = v1Result.Snippet
                    };
                }
            }

            return(result);
        }
        internal Result CreateResult(ResultVersionOne v1Result)
        {
            Result result = null;

            if (v1Result != null)
            {
                result = new Result
                {
                    BaselineState     = Utilities.CreateBaselineState(v1Result.BaselineState),
                    CodeFlows         = v1Result.CodeFlows?.Select(CreateCodeFlow).ToList(),
                    Fixes             = v1Result.Fixes?.Select(CreateFix).ToList(),
                    InstanceGuid      = v1Result.Id,
                    Level             = Utilities.CreateResultLevel(v1Result.Level),
                    Locations         = v1Result.Locations?.Select(CreateLocation).ToList(),
                    Message           = CreateMessage(v1Result.Message),
                    Properties        = v1Result.Properties,
                    RelatedLocations  = v1Result.RelatedLocations?.Select(CreateLocation).ToList(),
                    Stacks            = v1Result.Stacks?.Select(CreateStack).ToList(),
                    SuppressionStates = Utilities.CreateSuppressionStates(v1Result.SuppressionStates)
                };

                // The spec says that analysisTarget is required only if it differs from the result file.
                if (v1Result.Locations?[0]?.AnalysisTarget?.Uri != v1Result.Locations?[0]?.ResultFile?.Uri)
                {
                    result.AnalysisTarget = CreateFileLocation(v1Result.Locations[0].AnalysisTarget);
                }

                if (v1Result.RuleKey == null)
                {
                    result.RuleId = v1Result.RuleId;
                }
                else
                {
                    if (v1Result.RuleId == null)
                    {
                        result.RuleId = v1Result.RuleKey;
                    }
                    else
                    {
                        if (v1Result.RuleId == v1Result.RuleKey)
                        {
                            result.RuleId = v1Result.RuleId;
                        }
                        else
                        {
                            result.RuleId = v1Result.RuleKey;

                            if (_currentRun.Resources == null)
                            {
                                _currentRun.Resources = new Resources();
                            }

                            if (_currentRun.Resources.Rules == null)
                            {
                                _currentRun.Resources.Rules = new Dictionary <string, Rule>();
                            }

                            IDictionary <string, Rule> rules = _currentRun.Resources.Rules;

                            if (!rules.ContainsKey(v1Result.RuleKey))
                            {
                                rules.Add(v1Result.RuleKey, new Rule());
                            }

                            rules[v1Result.RuleKey].Id = v1Result.RuleId;
                        }
                    }
                }

                if (v1Result.FormattedRuleMessage != null)
                {
                    result.RuleMessageId = v1Result.FormattedRuleMessage.FormatId;

                    if (result.Message == null)
                    {
                        result.Message = new Message();
                    }

                    result.Message.Arguments = v1Result.FormattedRuleMessage.Arguments;
                }

                if (!string.IsNullOrWhiteSpace(v1Result.ToolFingerprintContribution))
                {
                    result.PartialFingerprints = new Dictionary <string, string>
                    {
                        { "Fingerprint", v1Result.ToolFingerprintContribution }
                    };
                }

                if (!string.IsNullOrWhiteSpace(v1Result.Snippet))
                {
                    if (result.Locations == null)
                    {
                        result.Locations = new List <Location>();
                    }

                    if (result.Locations.Count == 0)
                    {
                        result.Locations.Add(new Location());
                    }

                    if (result.Locations[0].PhysicalLocation == null)
                    {
                        result.Locations[0].PhysicalLocation = new PhysicalLocation();
                    }

                    if (result.Locations[0].PhysicalLocation.Region == null)
                    {
                        result.Locations[0].PhysicalLocation.Region = new Region();
                    }

                    result.Locations[0].PhysicalLocation.Region.Snippet = new FileContent
                    {
                        Text = v1Result.Snippet
                    };
                }
            }

            return(result);
        }
        internal ResultVersionOne CreateResultVersionOne(Result v2Result)
        {
            ResultVersionOne result = null;

            if (v2Result != null)
            {
                result = new ResultVersionOne
                {
                    BaselineState     = Utilities.CreateBaselineStateVersionOne(v2Result.BaselineState),
                    Fixes             = v2Result.Fixes?.Select(CreateFixVersionOne).ToList(),
                    Id                = v2Result.InstanceGuid,
                    Level             = Utilities.CreateResultLevelVersionOne(v2Result.Level, v2Result.Kind),
                    Locations         = v2Result.Locations?.Select(CreateLocationVersionOne).ToList(),
                    Message           = v2Result.Message?.Text,
                    Properties        = v2Result.Properties,
                    RelatedLocations  = v2Result.RelatedLocations?.Select(CreateAnnotatedCodeLocationVersionOne).ToList(),
                    Snippet           = v2Result.Locations?[0]?.PhysicalLocation?.Region?.Snippet?.Text,
                    Stacks            = v2Result.Stacks?.Select(CreateStackVersionOne).ToList(),
                    SuppressionStates = Utilities.CreateSuppressionStatesVersionOne(v2Result.SuppressionStates)
                };

                if (result.Fixes != null)
                {
                    // Null Fixes will be present in the case of unsupported encoding
                    (result.Fixes as List <FixVersionOne>).RemoveAll(f => f == null);

                    if (result.Fixes.Count == 0)
                    {
                        result.Fixes = null;
                    }
                }

                if (v2Result.AnalysisTarget != null)
                {
                    foreach (LocationVersionOne location in result.Locations)
                    {
                        location.AnalysisTarget = CreatePhysicalLocationVersionOne(v2Result.AnalysisTarget);
                    }
                }

                result.RuleId = v2Result.RuleId;
                string ruleKey = GetV1RuleKeyFromV2Index(v2Result.RuleIndex, _v2RuleIndexToV1KeyMap);

                // If the rules dictionary key is the same as the rule id, don't set result.RuleKey;
                // leave it null. This way, we don't unnecessarily persist ruleKey in the v1 SARIF file.
                // That is, we persist
                //
                //   "ruleId": "TST0001"
                //
                // instead of
                //
                //   "ruleId": "TST0001",
                //   "ruleKey": "TST0001"
                //
                if (ruleKey != result.RuleId)
                {
                    result.RuleKey = ruleKey;
                }

                if (!string.IsNullOrWhiteSpace(v2Result.Message?.MessageId))
                {
                    result.FormattedRuleMessage = new FormattedRuleMessageVersionOne
                    {
                        Arguments = v2Result.Message?.Arguments,
                        FormatId  = v2Result.Message.MessageId
                    };
                }
            }

            return(result);
        }
示例#15
0
        internal ResultVersionOne CreateResult(Result v2Result)
        {
            ResultVersionOne result = null;

            if (v2Result != null)
            {
                result = new ResultVersionOne
                {
                    BaselineState     = Utilities.CreateBaselineStateVersionOne(v2Result.BaselineState),
                    Fixes             = v2Result.Fixes?.Select(CreateFix).ToList(),
                    Id                = v2Result.InstanceGuid,
                    Level             = Utilities.CreateResultLevelVersionOne(v2Result.Level),
                    Locations         = v2Result.Locations?.Select(CreateLocation).ToList(),
                    Message           = v2Result.Message?.Text,
                    Properties        = v2Result.Properties,
                    RelatedLocations  = v2Result.RelatedLocations?.Select(CreateAnnotatedCodeLocation).ToList(),
                    Snippet           = v2Result.Locations?[0]?.PhysicalLocation?.Region?.Snippet?.Text,
                    Stacks            = v2Result.Stacks?.Select(CreateStack).ToList(),
                    SuppressionStates = Utilities.CreateSuppressionStatesVersionOne(v2Result.SuppressionStates)
                };

                if (result.Fixes != null)
                {
                    // Null Fixes will be present in the case of unsupported encoding
                    (result.Fixes as List <FixVersionOne>).RemoveAll(f => f == null);

                    if (result.Fixes.Count == 0)
                    {
                        result.Fixes = null;
                    }
                }

                if (v2Result.AnalysisTarget != null)
                {
                    foreach (LocationVersionOne location in result.Locations)
                    {
                        location.AnalysisTarget = CreatePhysicalLocation(v2Result.AnalysisTarget);
                    }
                }

                if (_currentV2Run.Resources?.Rules != null)
                {
                    IDictionary <string, Rule> rules = _currentV2Run.Resources.Rules;
                    Rule v2Rule;

                    if (v2Result.RuleId != null &&
                        rules.TryGetValue(v2Result.RuleId, out v2Rule) &&
                        v2Rule.Id != v2Result.RuleId)
                    {
                        result.RuleId  = v2Rule.Id;
                        result.RuleKey = v2Result.RuleId;
                    }
                    else
                    {
                        result.RuleId = v2Result.RuleId;
                    }
                }
                else
                {
                    result.RuleId = v2Result.RuleId;
                }

                if (!string.IsNullOrWhiteSpace(v2Result.Message?.MessageId))
                {
                    result.FormattedRuleMessage = new FormattedRuleMessageVersionOne
                    {
                        Arguments = v2Result.Message?.Arguments,
                        FormatId  = v2Result.Message.MessageId
                    };
                }
            }

            return(result);
        }