Skip to content
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

ClangTidy read line and row number from log file if exists #2373

Merged
merged 2 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 64 additions & 4 deletions src/Sarif.Converters/ClangTidyConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

using Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel;

Expand All @@ -15,6 +16,7 @@ namespace Microsoft.CodeAnalysis.Sarif.Converters
public class ClangTidyConverter : ToolFileConverterBase
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClangTidyConverter

Let's also update the md files, showing that we can read the file.yaml.log to understand the line number

{
private const string ToolInformationUri = "https://clang.llvm.org/extra/clang-tidy/";
private static readonly Regex ClangTidyLogRegex = new Regex("(.*):(\\d*):(\\d*): (warning|error): (.*) \\[(.*)\\]", RegexOptions.Compiled);

public override string ToolName => "Clang-Tidy";

Expand All @@ -25,9 +27,25 @@ public override void Convert(Stream input, IResultLogWriter output, OptionallyEm

IDeserializer deserializer = new DeserializerBuilder().Build();
using var textReader = new StreamReader(input);
ClangTidyLog log = deserializer.Deserialize<ClangTidyLog>(textReader);
ClangTidyReport report = deserializer.Deserialize<ClangTidyReport>(textReader);

(List<ReportingDescriptor>, List<Result>) rulesAndResults = ExtractRulesAndResults(log);
List<ClangTidyConsoleDiagnostic> logs = new List<ClangTidyConsoleDiagnostic>();
if (report != null)
{
string reportPath = (input as FileStream)?.Name;
if (reportPath != null)
{
string logPath = reportPath + ".log";
if (File.Exists(logPath))
{
logs = LoadLogFile(logPath);
}
}
}

AddLineNumberAndColumnNumber(report, logs);

(List<ReportingDescriptor>, List<Result>) rulesAndResults = ExtractRulesAndResults(report);

var run = new Run
{
Expand All @@ -46,6 +64,46 @@ public override void Convert(Stream input, IResultLogWriter output, OptionallyEm
PersistResults(output, rulesAndResults.Item2, run);
}

private void AddLineNumberAndColumnNumber(ClangTidyReport report, List<ClangTidyConsoleDiagnostic> logs)
{
if (report.Diagnostics.Count == logs.Count)
{
for (int i = 0; i < logs.Count; i++)
{
report.Diagnostics[i].DiagnosticMessage.LineNumber = logs[i].LineNumber;
report.Diagnostics[i].DiagnosticMessage.ColumnNumber = logs[i].ColumnNumber;
}
}
}

private List<ClangTidyConsoleDiagnostic> LoadLogFile(string logFilePath)
{
List<ClangTidyConsoleDiagnostic> returnValue = new List<ClangTidyConsoleDiagnostic>();

var logLines = File.ReadAllLines(logFilePath).ToList();
foreach (string line in logLines)
{
Match match = ClangTidyLogRegex.Match(line);

if (match.Success)
{
int lineNumber;
int columnNumber;
if (int.TryParse(match.Groups[2].Value, out lineNumber) && int.TryParse(match.Groups[3].Value, out columnNumber))
{
ClangTidyConsoleDiagnostic consoleDiagnostic = new ClangTidyConsoleDiagnostic()
{
LineNumber = lineNumber,
ColumnNumber = columnNumber
};
returnValue.Add(consoleDiagnostic);
}
}
}

return returnValue;
}

internal static Result CreateResult(ClangTidyDiagnostic entry)
{
entry = entry ?? throw new ArgumentNullException(nameof(entry));
Expand All @@ -63,6 +121,8 @@ internal static Result CreateResult(ClangTidyDiagnostic entry)
Region region = new Region()
{
CharOffset = entry.DiagnosticMessage.FileOffset,
StartLine = entry.DiagnosticMessage.LineNumber,
StartColumn = entry.DiagnosticMessage.ColumnNumber,
};

Uri analysisTargetUri = new Uri(entry.DiagnosticMessage.FilePath, UriKind.RelativeOrAbsolute);
Expand Down Expand Up @@ -127,12 +187,12 @@ internal static Result CreateResult(ClangTidyDiagnostic entry)
return result;
}

private static (List<ReportingDescriptor>, List<Result>) ExtractRulesAndResults(ClangTidyLog log)
private static (List<ReportingDescriptor>, List<Result>) ExtractRulesAndResults(ClangTidyReport report)
{
var rules = new Dictionary<string, ReportingDescriptor>(StringComparer.OrdinalIgnoreCase);
var results = new List<Result>();

foreach (ClangTidyDiagnostic diagnostic in log.Diagnostics)
foreach (ClangTidyDiagnostic diagnostic in report.Diagnostics)
{
string ruleId = diagnostic.DiagnosticName;
(ReportingDescriptor, Result) ruleAndResult = SarifRuleAndResultFromClangTidyDiagnostic(diagnostic);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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 ClangTidyConsoleDiagnostic
{
public string DiagnosticName { get; set; }
public string Message { get; set; }
public string FilePath { get; set; }
public int ColumnNumber { get; set; }
public int LineNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// 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 ClangTidyDiagnostic
{
public string DiagnosticName { get; set; }
public ClangTidyDiagnosticMessage DiagnosticMessage { get; set; }
public List<ClangTidyDiagnosticMessage> Notes { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class ClangTidyDiagnosticMessage
public string Message { get; set; }
public string FilePath { get; set; }
public int FileOffset { get; set; }
public int LineNumber { get; set; }
public int ColumnNumber { get; set; }
public List<ClangTidyReplacement> Replacements { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.CodeAnalysis.Sarif.Converters.ClangTidyObjectModel
{
public class ClangTidyLog
public class ClangTidyReport
{
public string MainSourceFile { get; set; }
public List<ClangTidyDiagnostic> Diagnostics { get; set; }
Expand Down
Loading