-
Notifications
You must be signed in to change notification settings - Fork 90
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
Add Clang-Tidy converter #2367
Add Clang-Tidy converter #2367
Changes from 2 commits
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,171 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel; | ||
|
||
using YamlDotNet.RepresentationModel; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Converters | ||
{ | ||
public class ClangTidyConverter : ToolFileConverterBase | ||
{ | ||
private const string ToolInformationUri = "https://clang.llvm.org/extra/clang-tidy/"; | ||
|
||
public override string ToolName => "Clang-Tidy"; | ||
|
||
public override void Convert(Stream input, IResultLogWriter output, OptionallyEmittedData dataToInsert) | ||
{ | ||
input = input ?? throw new ArgumentNullException(nameof(input)); | ||
output = output ?? throw new ArgumentNullException(nameof(output)); | ||
|
||
IDeserializer deserializer = new DeserializerBuilder().Build(); | ||
using var textReader = new StreamReader(input); | ||
ClangTidyLog log = deserializer.Deserialize<ClangTidyLog>(textReader); | ||
|
||
(List<ReportingDescriptor>, List<Result>) rulesAndResults = ExtractRulesAndResults(log); | ||
|
||
var run = new Run | ||
{ | ||
Tool = new Tool | ||
{ | ||
Driver = new ToolComponent | ||
{ | ||
Name = ToolName, | ||
InformationUri = new Uri(ToolInformationUri), | ||
Rules = rulesAndResults.Item1, | ||
} | ||
}, | ||
Results = rulesAndResults.Item2, | ||
}; | ||
|
||
PersistResults(output, rulesAndResults.Item2, run); | ||
} | ||
|
||
internal static Result CreateResult(ClangTidyDiagnostic entry) | ||
{ | ||
entry = entry ?? throw new ArgumentNullException(nameof(entry)); | ||
|
||
Result result = new Result() | ||
{ | ||
RuleId = entry.DiagnosticName, | ||
Message = new Message { Text = entry.DiagnosticMessage.Message }, | ||
Kind = ResultKind.Fail | ||
}; | ||
|
||
// no level infomation in Clang-Tidy report | ||
result.Level = FailureLevel.Warning; | ||
|
||
Region region = new Region() | ||
{ | ||
CharOffset = entry.DiagnosticMessage.FileOffset, | ||
}; | ||
|
||
Uri analysisTargetUri = new Uri(entry.DiagnosticMessage.FilePath, UriKind.RelativeOrAbsolute); | ||
|
||
var physicalLocation = new PhysicalLocation | ||
{ | ||
ArtifactLocation = new ArtifactLocation | ||
{ | ||
Uri = analysisTargetUri | ||
}, | ||
Region = region | ||
}; | ||
|
||
Location location = new Location() | ||
{ | ||
PhysicalLocation = physicalLocation | ||
}; | ||
|
||
result.Locations = new List<Location>() | ||
{ | ||
location | ||
}; | ||
|
||
if (entry.DiagnosticMessage.Replacements.Count > 0) | ||
{ | ||
IList<Replacement> replacements = new List<Replacement>(); | ||
|
||
foreach (ClangTidyReplacement fix in entry.DiagnosticMessage.Replacements) | ||
{ | ||
Replacement replacement = new Replacement(); | ||
|
||
replacement.DeletedRegion = new Region | ||
{ | ||
CharLength = fix.Length, | ||
CharOffset = fix.Offset, | ||
}; | ||
|
||
if (!string.IsNullOrEmpty(fix.ReplacementText)) | ||
{ | ||
replacement.InsertedContent = new ArtifactContent | ||
{ | ||
Text = fix.ReplacementText | ||
}; | ||
} | ||
|
||
replacements.Add(replacement); | ||
} | ||
|
||
var sarifFileChange = new ArtifactChange | ||
{ | ||
ArtifactLocation = new ArtifactLocation | ||
{ | ||
Uri = analysisTargetUri | ||
}, | ||
Replacements = replacements | ||
}; | ||
|
||
Fix sarifFix = new Fix(description: null, artifactChanges: new List<ArtifactChange>() { sarifFileChange }, properties: null); | ||
result.Fixes = new List<Fix> { sarifFix }; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private static (List<ReportingDescriptor>, List<Result>) ExtractRulesAndResults(ClangTidyLog log) | ||
{ | ||
var rules = new List<ReportingDescriptor>(); | ||
var ruleIds = new HashSet<string>(); | ||
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. u could replace by dict<string, reportingdescriptor> #Resolved 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. add the stringcomparison to ignore case 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. sounds good, fixed |
||
var results = new List<Result>(); | ||
|
||
foreach (ClangTidyDiagnostic diagnostic in log.Diagnostics) | ||
{ | ||
string ruleId = diagnostic.DiagnosticName; | ||
(ReportingDescriptor, Result) ruleAndResult = SarifRuleAndResultFromClangTidyDiagnostic(diagnostic); | ||
if (!ruleIds.Contains(ruleId)) | ||
{ | ||
rules.Add(ruleAndResult.Item1); | ||
ruleIds.Add(ruleId); | ||
} | ||
|
||
results.Add(ruleAndResult.Item2); | ||
} | ||
|
||
return (rules, results); | ||
} | ||
|
||
private static (ReportingDescriptor, Result) SarifRuleAndResultFromClangTidyDiagnostic(ClangTidyDiagnostic diagnostic) | ||
{ | ||
var reportingDescriptor = new ReportingDescriptor | ||
{ | ||
Id = diagnostic.DiagnosticName, | ||
DefaultConfiguration = new ReportingConfiguration | ||
{ | ||
Level = FailureLevel.Warning, | ||
}, | ||
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 think u dont need to add this. #Resolved 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. sorry did not get you, removing 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. remove entire DefaultConfiguration 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. oh do you mean remove the whole DefaultConfiguration since no need. I am fixing it. DefaultConfiguration = new ReportingConfiguration 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. fixed 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. warning is default. so we dont need to express. |
||
HelpUri = diagnostic.DiagnosticName.Equals("clang-diagnostic-error", StringComparison.OrdinalIgnoreCase) | ||
? null | ||
: new Uri($"https://clang.llvm.org/extra/clang-tidy/checks/{diagnostic.DiagnosticName}.html") | ||
}; | ||
|
||
Result result = CreateResult(diagnostic); | ||
|
||
return (reportingDescriptor, result); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel | ||
{ | ||
public class ClangTidyDiagnostic | ||
{ | ||
public string DiagnosticName { get; set; } | ||
public ClangTidyDiagnosticMessage DiagnosticMessage { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel | ||
{ | ||
public class ClangTidyDiagnosticMessage | ||
{ | ||
public string Message { get; set; } | ||
public string FilePath { get; set; } | ||
public int FileOffset { get; set; } | ||
public List<ClangTidyReplacement> Replacements { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel | ||
{ | ||
public class ClangTidyLog | ||
{ | ||
public string MainSourceFile { get; set; } | ||
public List<ClangTidyDiagnostic> Diagnostics { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel | ||
{ | ||
public class ClangTidyReplacement | ||
{ | ||
public string FilePath { get; set; } | ||
public int Offset { get; set; } | ||
public int Length { get; set; } | ||
public string ReplacementText { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.IO; | ||
|
||
using FluentAssertions; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.Writers; | ||
|
||
using Xunit; | ||
|
||
using YamlDotNet.Core; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Converters | ||
{ | ||
public class ClangTidyConverterTests : ConverterTestsBase<ClangTidyConverter> | ||
{ | ||
private static readonly string NoOutputExpected = string.Empty; | ||
|
||
[Fact] | ||
public void Converter_RequiresInputStream() | ||
{ | ||
var converter = new ClangTidyConverter(); | ||
Action action = () => converter.Convert(input: null, output: new ResultLogObjectWriter(), dataToInsert: OptionallyEmittedData.None); | ||
action.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void Converter_RequiresResultLogWriter() | ||
{ | ||
var converter = new ClangTidyConverter(); | ||
Action action = () => converter.Convert(input: new MemoryStream(), output: null, dataToInsert: OptionallyEmittedData.None); | ||
action.Should().Throw<ArgumentNullException>(); | ||
} | ||
|
||
[Fact] | ||
public void Converter_WhenInputIsEmpty_ReturnsNoResults() | ||
{ | ||
string input = GetResourceText("Inputs.Empty.yaml"); | ||
string expectedOutput = GetResourceText("ExpectedOutputs.NoResults.sarif"); | ||
RunTestCase(input, expectedOutput); | ||
} | ||
|
||
[Fact] | ||
public void Converter_WhenResultRowIsInvalid_ThrowsExpectedException() | ||
{ | ||
string input = GetResourceText("Inputs.InvalidResult.yaml"); | ||
Action action = () => RunTestCase(input, NoOutputExpected); | ||
action.Should().Throw<YamlException>(); | ||
} | ||
|
||
[Fact] | ||
public void Converter_WhenInputContainsValidResults_ReturnsExpectedOutput() | ||
{ | ||
string input = GetResourceText("Inputs.ValidResults.yaml"); | ||
string expectedOutput = GetResourceText("ExpectedOutputs.ValidResults.sarif"); | ||
RunTestCase(input, expectedOutput); | ||
} | ||
|
||
private static readonly ResourceExtractor s_extractor = new ResourceExtractor(typeof(ClangTidyConverterTests)); | ||
private const string ResourceNamePrefix = ToolFormat.ClangTidy; | ||
|
||
private static string GetResourceText(string resourceNameSuffix) => | ||
s_extractor.GetResourceText($"TestData.{ResourceNamePrefix}.{resourceNameSuffix}"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"$schema": "https://schemastore.azurewebsites.net/schemas/json/sarif-2.1.0-rtm.5.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"results": [], | ||
"tool": { | ||
"driver": { | ||
"name": "Clang-Tidy", | ||
"informationUri": "https://clang.llvm.org/extra/clang-tidy/" | ||
} | ||
}, | ||
"columnKind": "utf16CodeUnits" | ||
} | ||
] | ||
} |
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.
from this, you can create the ReportingDescriptor #Closed
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.
each rule can point to its own documentation: https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-trailing-return-type.html
change modernize-use-trailing-return-type to another ruleid and it will work
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.
added the rules.