-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating SARIF2004 #1995
Updating SARIF2004 #1995
Changes from 8 commits
802fb3f
8cce235
ea68e1f
2206514
0dcb03d
fe26896
f4a8b41
84bde22
9ca8409
e2872fe
7bb27aa
77b8154
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public static class Extensions | ||
{ | ||
public static bool RefersToDriver(this ToolComponentReference toolComponent, string driverGuid) | ||
{ | ||
if (toolComponent.Index == -1) | ||
{ | ||
if (toolComponent.Guid == null) | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
return toolComponent.Guid.Equals(driverGuid, StringComparison.OrdinalIgnoreCase); | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,20 +41,107 @@ public class OptimizeFileSize : SarifValidationSkimmerBase | |
/// some scenarios (for example, when assessing compliance with policy), the 'artifacts' array | ||
/// might be used to record the full set of artifacts that were analyzed. In such a scenario, | ||
/// the 'artifacts' array should be retained even if it contains only location information. | ||
/// | ||
/// In addition to the avoiding unnecessary arrays, there are other ways to optimize the | ||
/// size of SARIF log files. | ||
/// | ||
/// Prefer the result object properties 'ruleId' and 'ruleIndex' to the nested object-valued | ||
/// property 'result.rule', unless the rule comes from a tool component other than the driver | ||
/// (in which case only 'result.rule' can accurately point to the metadata for the rule). | ||
/// The 'ruleId' and 'ruleIndex' properties are shorter and just as clear. | ||
/// | ||
/// Do not specify the result object's 'analysisTarget' property unless it differs from the | ||
/// result location. The canonical scenario for using 'result.analysisTarget' is a C/C++ language | ||
/// analyzer that is instructed to analyze example.c, and detects a result in the included file | ||
/// example.h. In this case, 'analysisTarget' is example.c, and the result location is in example.h. | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF2004_OptimizeFileSize_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeAnalysisTarget_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeResultRuleInformation_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_EliminateLocationOnlyArtifacts_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_EliminateIdOnlyRules_Text) | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_EliminateIdOnlyRules_Text), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_PreferRuleId_Text) | ||
}; | ||
|
||
public override FailureLevel DefaultLevel => FailureLevel.Warning; | ||
|
||
private string driverGuid; | ||
|
||
protected override void Analyze(Run run, string runPointer) | ||
{ | ||
AnalyzeLocationOnlyArtifacts(run, runPointer); | ||
AnalyzeIdOnlyRules(run, runPointer); | ||
|
||
this.driverGuid = run.Tool.Driver?.Guid; | ||
} | ||
|
||
protected override void Analyze(Result result, string resultPointer) | ||
{ | ||
ReportUnnecessaryAnalysisTarget(result, resultPointer); | ||
ReportRuleDuplication(result, resultPointer); | ||
} | ||
|
||
private void ReportRuleDuplication(Result result, string resultPointer) | ||
{ | ||
if (result.Rule != null) | ||
{ | ||
if (result.Rule.ToolComponent != null) | ||
{ | ||
if (result.Rule.ToolComponent.RefersToDriver(this.driverGuid)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can shorten this:
Note that you need the explicit comparison |
||
{ | ||
// {0}: This result uses the 'rule' property to specify the rule | ||
// metadata, but the 'ruleId' property suffices because the rule | ||
// is defined by 'tool.driver'. Prefer 'result.ruleId' because it | ||
// is shorter and just as clear. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New message: The result at '{0}' uses the 'rule' property to specify the violated rule, but this is not necessary because the rule is defined by 'tool.driver'. Use the 'ruleId' and 'ruleIndex' instead, because they are shorter and just as clear. #Closed |
||
LogResult( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This logic is backwards. Take a look at the notes I attached to the chat. The idea is: If toolComponent is present, and if it does not refer to the driver -- which means that it has to be present! -- then Here's what the notes said:
|
||
resultPointer, | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_PreferRuleId_Text)); | ||
} | ||
else | ||
{ | ||
if (!string.IsNullOrWhiteSpace(result.RuleId) || result.RuleIndex >= 0) | ||
{ | ||
// {0}: This result specifies both 'result.ruleId' and 'result.rule'. | ||
// Prefer 'result.ruleId' because it is shorter and just as clear. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will provide new strings. #Closed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New message: The result at '{0}' uses the 'rule' property to specify the violated rule, so it is not necessary also to specify 'ruleId' or 'ruleIndex'. Remove the 'ruleId' and 'ruleIndex' properties. In reply to: 455809589 [](ancestors = 455809589) |
||
LogResult( | ||
resultPointer, | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeResultRuleInformation_Text)); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
// {0}: This result uses the 'rule' property to specify the rule | ||
// metadata, but the 'ruleId' property suffices because the rule | ||
// is defined by 'tool.driver'. Prefer 'result.ruleId' because it | ||
// is shorter and just as clear. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same message as on Line 94 above ("The result at '{0}' uses the 'rule' property to specify the violated rule, but this is not necessary because ..."). #Closed |
||
LogResult( | ||
resultPointer, | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_PreferRuleId_Text)); | ||
} | ||
} | ||
} | ||
|
||
private void ReportUnnecessaryAnalysisTarget(Result result, string resultPointer) | ||
{ | ||
if (result.Locations != null && result.AnalysisTarget != null) | ||
{ | ||
foreach (Location location in result.Locations) | ||
{ | ||
if (location?.PhysicalLocation?.ArtifactLocation?.Uri == result.AnalysisTarget.Uri) | ||
{ | ||
// {0}: The 'analysisTarget' property '{1}' is unnecessary because it is the same as | ||
// the result location. Remove the 'analysisTarget' property. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The 'analysisTarget' property '{1}' at '{0}' is unnecessary because it is the same as the result location. Remove the 'analysisTarget' property. #Closed |
||
LogResult( | ||
resultPointer.AtProperty(SarifPropertyName.AnalysisTarget), | ||
nameof(RuleResources.SARIF2004_OptimizeFileSize_Warning_AvoidDuplicativeAnalysisTarget_Text), | ||
result.AnalysisTarget.Uri.OriginalString); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was marked Resolved but it wasn't actually fixed. I think you tried to fix it with the In reply to: 455252796 [](ancestors = 455252796) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void AnalyzeLocationOnlyArtifacts(Run run, string runPointer) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"In the result at '{0}', the 'analysisTarget' property '{1}' is unnecessary..." #Closed