-
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
Merged
Merged
Add Clang-Tidy converter #2367
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// 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 System.Linq; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel; | ||
|
||
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 Dictionary<string, ReportingDescriptor>(StringComparer.OrdinalIgnoreCase); | ||
var results = new List<Result>(); | ||
|
||
foreach (ClangTidyDiagnostic diagnostic in log.Diagnostics) | ||
{ | ||
string ruleId = diagnostic.DiagnosticName; | ||
(ReportingDescriptor, Result) ruleAndResult = SarifRuleAndResultFromClangTidyDiagnostic(diagnostic); | ||
if (!rules.ContainsKey(ruleId)) | ||
{ | ||
rules.Add(ruleId, ruleAndResult.Item1); | ||
} | ||
|
||
results.Add(ruleAndResult.Item2); | ||
} | ||
|
||
return (rules.Values.ToList(), results); | ||
} | ||
|
||
private static (ReportingDescriptor, Result) SarifRuleAndResultFromClangTidyDiagnostic(ClangTidyDiagnostic diagnostic) | ||
{ | ||
var reportingDescriptor = new ReportingDescriptor | ||
{ | ||
Id = diagnostic.DiagnosticName, | ||
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); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Sarif.Converters/ClangTidyObjectModel/ClangTidyDiagnostic.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/Sarif.Converters/ClangTidyObjectModel/ClangTidyDiagnosticMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Sarif.Converters/ClangTidyObjectModel/ClangTidyReplacement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/Test.UnitTests.Sarif.Converters/ClangTidyConverterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Test.UnitTests.Sarif.Converters/TestData/ClangTidy/ExpectedOutputs/NoResults.sarif
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.