public static IRule ConvertToRuleDescriptor(this Diagnostic diagnostic) { // TODO we should consume the standard Roslyn->SARIF emit code here. DiagnosticDescriptor diagnosticDescriptor = diagnostic.Descriptor; var rule = new Rule(); rule.MessageFormats = new Dictionary<string, string>(); rule.MessageFormats["Default"] = diagnosticDescriptor.MessageFormat.ToString(); rule.FullDescription = diagnosticDescriptor.Description.ToString(); rule.HelpUri = new Uri(diagnosticDescriptor.HelpLinkUri); rule.Id = diagnosticDescriptor.Id; // TODO: review this decision rule.Name = diagnostic.GetType().Name; foreach (string tag in diagnosticDescriptor.CustomTags) { rule.Tags.Add(tag); } rule.DefaultLevel = diagnosticDescriptor.DefaultSeverity.ConvertToResultLevel(); rule.SetProperty("Category", diagnosticDescriptor.Category); rule.SetProperty("IsEnabledByDefault", diagnosticDescriptor.IsEnabledByDefault.ToString()); rule.ShortDescription = diagnosticDescriptor.Title.ToString(); // No Roslyn analog for these available from diagnostic //rule.Options return rule; }
public bool ValueEquals(Rule other) => ValueComparer.Equals(this, other);
public static bool TryGetRule(this Run run, string ruleId, string ruleKey, out IRule rule) { rule = null; if (run != null && run.Rules != null && (ruleId != null || ruleKey != null)) { if (ruleKey != null) { rule = run.Rules[ruleKey]; } else { rule = run.Rules[ruleId]; } } else if (ruleId != null) { // No rule in log file. // If the rule is a PREfast rule. create a "fake" rule using the external rule metadata file. if (RuleMetadata[ruleId] != null) { string ruleName = null; if (RuleMetadata[ruleId]["heading"] != null) { ruleName = RuleMetadata[ruleId]["heading"].Value<string>(); } Uri helpUri = null; if (RuleMetadata[ruleId]["url"] != null) { helpUri = new Uri(RuleMetadata[ruleId]["url"].Value<string>()); } if (ruleName != null || helpUri != null) { rule = new Rule(ruleId, ruleName, null, null, null, RuleConfiguration.Unknown, ResultLevel.Warning, helpUri, null); } } } return rule != null; }
public void SarifLogger_ScrapesFilesFromResult() { var sb = new StringBuilder(); using (var textWriter = new StringWriter(sb)) { using (var sarifLogger = new SarifLogger( textWriter, analysisTargets: null, verbose: false, computeTargetsHash: true, logEnvironment: false, prereleaseInfo: null, invocationTokensToRedact: null)) { string ruleId = "RuleId"; var rule = new Rule() { Id = ruleId }; var result = new Result() { RuleId = ruleId, Locations = new[] { new Location { AnalysisTarget = new PhysicalLocation { Uri = new Uri(@"file:///file0.cpp")}, ResultFile = new PhysicalLocation { Uri = new Uri(@"file:///file1.cpp")} }, }, Fixes = new[] { new Fix { FileChanges = new[] { new FileChange { Uri = new Uri(@"file:///file2.cpp") } } } }, RelatedLocations = new[] { new AnnotatedCodeLocation { PhysicalLocation = new PhysicalLocation { Uri = new Uri(@"file:///file3.cpp")} } }, Stacks = new[] { new Stack { Frames = new[] { new StackFrame { Uri = new Uri(@"file:///file4.cpp") } } } }, CodeFlows = new[] { new CodeFlow { Locations = new[] { new AnnotatedCodeLocation { PhysicalLocation = new PhysicalLocation { Uri = new Uri(@"file:///file5.cpp")} } } } } }; sarifLogger.Log(rule, result); } } string logText = sb.ToString(); var sarifLog = JsonConvert.DeserializeObject<SarifLog>(logText); int fileCount = 6; for (int i = 0; i < fileCount; ++i) { string fileName = @"file" + i + ".cpp"; string fileDataKey = "file:///" + fileName; sarifLog.Runs[0].Files.Should().ContainKey(fileDataKey, "file data for " + fileName + " should exist in files collection"); } sarifLog.Runs[0].Files.Count.Should().Be(fileCount); }
public void SarifLogger_ResultAndRuleIdMismatch() { var sb = new StringBuilder(); using (var writer = new StringWriter(sb)) using (var sarifLogger = new SarifLogger(writer, verbose: true)) { var rule = new Rule() { Id = "ActualId" }; var result = new Result() { RuleId = "IncorrectRuleId", Message = "test message" }; sarifLogger.Log(rule, result); } }
/// <summary> /// Initializes a new instance of the <see cref="Rule" /> class from the specified instance. /// </summary> /// <param name="other"> /// The instance from which the new instance is to be initialized. /// </param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="other" /> is null. /// </exception> public Rule(Rule other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } Init(other.Id, other.Name, other.ShortDescription, other.FullDescription, other.MessageFormats, other.Configuration, other.DefaultLevel, other.HelpUri, other.Properties); }