Skip to content

Commit

Permalink
SLVS-1697 Integrate Reproducer command with SLCore analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
georgii-borovinskikh-sonarsource committed Dec 18, 2024
1 parent 044fcb8 commit 784f139
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@

namespace SonarLint.VisualStudio.CFamily.Analysis
{
public class CFamilyAnalyzerOptions : IAnalyzerOptions
public interface ICFamilyAnalyzerOptions : IAnalyzerOptions
{
bool CreateReproducer { get; set; }
}

public class CFamilyAnalyzerOptions : ICFamilyAnalyzerOptions
{
public bool CreateReproducer { get; set; }
public bool CreatePreCompiledHeaders { get; set; }
public bool IsOnOpen { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,6 @@ public void Get_FailsToRetrieveProjectItem_NonCriticalException_ExceptionCaughtA
logger.AssertPartialOutputStringExists(nameof(NotImplementedException), SourceFilePath);
}

[TestMethod]
public void Get_FailsToRetrieveProjectItem_NonCriticalException_Pch_ExceptionCaughtNotLoggedAndNullReturned()
{
dte.Solution.ThrowsForAnyArgs<NotImplementedException>();

var result = testSubject.Get(SourceFilePath, new CFamilyAnalyzerOptions{CreatePreCompiledHeaders = true});

result.Should().BeNull();
logger.AssertNoOutputMessages();
}

[TestMethod]
public void Get_FailsToRetrieveProjectItem_CriticalException_ExceptionThrown()
{
Expand Down
27 changes: 5 additions & 22 deletions src/Integration.Vsix/CFamily/VcxProject/FileConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@ internal class FileConfigProvider(
ILogger logger,
IThreadHandling threadHandling) : IFileConfigProvider
{
private static readonly NoOpLogger noOpLogger = new NoOpLogger();

public IFileConfig Get(string analyzedFilePath, CFamilyAnalyzerOptions analyzerOptions)
{
var analysisLogger = GetAnalysisLogger(analyzerOptions);

return uiServiceOperation.Execute<SDTE, DTE2, IFileConfig>(dte =>
GetInternal(analyzedFilePath, dte, analysisLogger));
GetInternal(analyzedFilePath, dte));
}

private FileConfig GetInternal(string analyzedFilePath, DTE2 dte, ILogger analysisLogger)
private FileConfig GetInternal(string analyzedFilePath, DTE2 dte)
{
threadHandling.ThrowIfNotOnUIThread();

Expand All @@ -69,35 +66,21 @@ private FileConfig GetInternal(string analyzedFilePath, DTE2 dte, ILogger analys

if (!fileInSolutionIndicator.IsFileInSolution(projectItem))
{
analysisLogger.LogVerbose($"[VCX:FileConfigProvider] The file is not part of a VCX project. File: {analyzedFilePath}");
logger.LogVerbose($"[VCX:FileConfigProvider] The file is not part of a VCX project. File: {analyzedFilePath}");
return null;
}
// Note: if the C++ tools are not installed then it's likely an exception will be thrown when
// the framework tries to JIT-compile the TryGet method (since it won't be able to find the MS.VS.VCProjectEngine
// types).
return FileConfig.TryGet(analysisLogger, projectItem, analyzedFilePath);
return FileConfig.TryGet(logger, projectItem, analyzedFilePath);
}
catch (Exception ex) when (!Microsoft.VisualStudio.ErrorHandler.IsCriticalException(ex))
{
analysisLogger.WriteLine(CFamilyStrings.ERROR_CreatingConfig, analyzedFilePath, ex);
logger.WriteLine(CFamilyStrings.ERROR_CreatingConfig, analyzedFilePath, ex);
return null;
}
}

private ILogger GetAnalysisLogger(CFamilyAnalyzerOptions analyzerOptions)
{
if (analyzerOptions is CFamilyAnalyzerOptions cFamilyAnalyzerOptions &&
cFamilyAnalyzerOptions.CreatePreCompiledHeaders)
{
// In case the requeset is coming from PCH generation, we don't log failures.
// This is to avoid redundant messages while navigating unsupported files.
return noOpLogger;
}

return logger;
}


private class NoOpLogger : ILogger
{
public void WriteLine(string message)
Expand Down
15 changes: 13 additions & 2 deletions src/SLCore/Analysis/SLCoreAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using System.ComponentModel.Composition;
using Microsoft.VisualStudio.Threading;
using SonarLint.VisualStudio.CFamily.Analysis;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.Core.CFamily;
Expand All @@ -36,6 +37,7 @@ namespace SonarLint.VisualStudio.SLCore.Analysis;
public class SLCoreAnalyzer : IAnalyzer
{
private const string CFamilyCompileCommandsProperty = "sonar.cfamily.compile-commands";
private const string CFamilyReproducerProperty = "sonar.cfamily.reproducer";

private readonly ISLCoreServiceProvider serviceProvider;
private readonly IActiveConfigScopeTracker activeConfigScopeTracker;
Expand Down Expand Up @@ -100,7 +102,7 @@ private async Task ExecuteAnalysisInternalAsync(
try
{
Dictionary<string, string> properties = [];
using var temporaryResourcesHandle = EnrichPropertiesForCFamily(properties, path, detectedLanguages);
using var temporaryResourcesHandle = EnrichPropertiesForCFamily(properties, path, detectedLanguages, analyzerOptions);

Stopwatch stopwatch = Stopwatch.StartNew();

Expand Down Expand Up @@ -133,13 +135,22 @@ [new FileUri(path)],
}
}

private IDisposable EnrichPropertiesForCFamily(Dictionary<string, string> properties, string path, IEnumerable<AnalysisLanguage> detectedLanguages)
private IDisposable EnrichPropertiesForCFamily(
Dictionary<string, string> properties,
string path,
IEnumerable<AnalysisLanguage> detectedLanguages,
IAnalyzerOptions analyzerOptions)
{
if (!IsCFamily(detectedLanguages))
{
return null;
}

if (analyzerOptions is ICFamilyAnalyzerOptions {CreateReproducer: true})
{
properties[CFamilyReproducerProperty] = path;
}

var compilationDatabaseHandle = compilationDatabaseLocator.GetOrNull(path);
if (compilationDatabaseHandle == null)
{
Expand Down

0 comments on commit 784f139

Please sign in to comment.