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

SLVS-1697 Integrate Reproducer command with SLCore analysis #5905

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
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 @@ -24,11 +24,9 @@
using NSubstitute.ExceptionExtensions;
using NSubstitute.ReturnsExtensions;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.CFamily.Analysis;
using SonarLint.VisualStudio.Infrastructure.VS;
using SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject;
using static SonarLint.VisualStudio.Integration.Vsix.CFamily.UnitTests.CFamilyTestUtility;
using System.IO.Abstractions;
using SonarLint.VisualStudio.Core.SystemAbstractions;

namespace SonarLint.VisualStudio.Integration.UnitTests.CFamily.VcxProject
Expand Down Expand Up @@ -94,29 +92,18 @@ public void Get_FailsToRetrieveProjectItem_NonCriticalException_ExceptionCaughtA
{
dte.Solution.ThrowsForAnyArgs<NotImplementedException>();

var result = testSubject.Get(SourceFilePath, new CFamilyAnalyzerOptions());
var result = testSubject.Get(SourceFilePath);

result.Should().BeNull();
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()
{
dte.Solution.ThrowsForAnyArgs<DivideByZeroException>();

Action act = () => testSubject.Get(SourceFilePath, new CFamilyAnalyzerOptions());
Action act = () => testSubject.Get(SourceFilePath);

act.Should().Throw<DivideByZeroException>();
}
Expand All @@ -126,7 +113,7 @@ public void Get_NoProjectItem_ReturnsNull()
{
dte.Solution.FindProjectItem(SourceFilePath).ReturnsNull();

testSubject.Get(SourceFilePath, null)
testSubject.Get(SourceFilePath)
.Should().BeNull();

Received.InOrder(() =>
Expand All @@ -143,7 +130,7 @@ public void Get_ProjectItemNotInSolution_ReturnsNull()
dte.Solution.FindProjectItem(SourceFilePath).Returns(mockProjectItem.Object);
fileInSolutionIndicator.IsFileInSolution(mockProjectItem.Object).Returns(false);

testSubject.Get(SourceFilePath, null)
testSubject.Get(SourceFilePath)
.Should().BeNull();

Received.InOrder(() =>
Expand All @@ -160,7 +147,7 @@ public void Get_SuccessfulConfig_ConfigReturned()
var mockProjectItem = CreateMockProjectItem(SourceFilePath);
dte.Solution.FindProjectItem(SourceFilePath).Returns(mockProjectItem.Object);

testSubject.Get(SourceFilePath, null)
testSubject.Get(SourceFilePath)
.Should().NotBeNull();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void MefCtor_CheckIsExported()
[TestMethod]
public void CreateOrNull_NoFileConfig_ReturnsNull()
{
fileConfigProvider.Get(SourceFilePath, default).ReturnsNull();
fileConfigProvider.Get(SourceFilePath).ReturnsNull();
var testSubject = new VCXCompilationDatabaseProvider(
storage,
envVarProvider,
Expand All @@ -82,7 +82,7 @@ public void CreateOrNull_NoFileConfig_ReturnsNull()
public void CreateOrNull_FileConfig_CantStore_ReturnsNull()
{
var fileConfig = GetFileConfig();
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
fileConfigProvider.Get(SourceFilePath).Returns(fileConfig);
storage.CreateDatabase(default, default, default, default).ReturnsNullForAnyArgs();
var testSubject = new VCXCompilationDatabaseProvider(
storage,
Expand All @@ -99,7 +99,7 @@ public void CreateOrNull_FileConfig_CantStore_ReturnsNull()
public void CreateOrNull_FileConfig_StoresAndReturnsHandle()
{
var fileConfig = GetFileConfig();
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
fileConfigProvider.Get(SourceFilePath).Returns(fileConfig);
var compilationDatabaseHandle = Substitute.For<ICompilationDatabaseHandle>();
storage.CreateDatabase(CDFile, CDDirectory, CDCommand, Arg.Any<IEnumerable<string>>()).Returns(compilationDatabaseHandle);
var testSubject = new VCXCompilationDatabaseProvider(
Expand All @@ -115,7 +115,7 @@ public void CreateOrNull_FileConfig_StoresAndReturnsHandle()
public void CreateOrNull_NoEnvIncludeInFileConfig_UsesStatic()
{
var fileConfig = GetFileConfig(null);
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
fileConfigProvider.Get(SourceFilePath).Returns(fileConfig);
envVarProvider.GetAll().Returns([("Var1", "Value1"), ("INCLUDE", "static"), ("Var2", "Value2")]);
var testSubject = new VCXCompilationDatabaseProvider(
storage,
Expand All @@ -132,7 +132,7 @@ public void CreateOrNull_NoEnvIncludeInFileConfig_UsesStatic()
public void CreateOrNull_FileConfigHasEnvInclude_UsesDynamic()
{
var fileConfig = GetFileConfig(EnvInclude);
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
fileConfigProvider.Get(SourceFilePath).Returns(fileConfig);
envVarProvider.GetAll().Returns([("Var1", "Value1"), ("INCLUDE", "static"), ("Var2", "Value2")]);
var testSubject = new VCXCompilationDatabaseProvider(
storage,
Expand All @@ -150,7 +150,7 @@ public void CreateOrNull_FileConfigHasEnvInclude_UsesDynamic()
public void CreateOrNull_NoStaticInclude_UsesDynamic()
{
var fileConfig = GetFileConfig(EnvInclude);
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
fileConfigProvider.Get(SourceFilePath).Returns(fileConfig);
envVarProvider.GetAll().Returns([("Var1", "Value1"), ("Var2", "Value2")]);
var testSubject = new VCXCompilationDatabaseProvider(
storage,
Expand All @@ -168,7 +168,7 @@ public void CreateOrNull_NoStaticInclude_UsesDynamic()
public void CreateOrNull_StaticEnvVarsAreCached()
{
var fileConfig = GetFileConfig();
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
fileConfigProvider.Get(SourceFilePath).Returns(fileConfig);
envVarProvider.GetAll().Returns([("Var1", "Value1"), ("Var2", "Value2")]);
var testSubject = new VCXCompilationDatabaseProvider(
storage,
Expand All @@ -187,7 +187,7 @@ public void CreateOrNull_StaticEnvVarsAreCached()
public void CreateOrNull_EnvVarsContainHeaderPropertyForHeaderFiles()
{
var fileConfig = GetFileConfig(EnvInclude, true);
fileConfigProvider.Get(SourceFilePath, default).Returns(fileConfig);
fileConfigProvider.Get(SourceFilePath).Returns(fileConfig);
envVarProvider.GetAll().Returns([("Var1", "Value1"), ("Var2", "Value2")]);
var testSubject = new VCXCompilationDatabaseProvider(
storage,
Expand Down
1 change: 1 addition & 0 deletions src/Integration.Vsix/CFamily/CFamilyReproducerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ private void TriggerReproducer()
{
var options = new CFamilyAnalyzerOptions
{
IsOnOpen = false,

Choose a reason for hiding this comment

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

Is this really needed? If so, I would add a comment explaining why (by default bool properties are initialized with false and if I would ever touch this code I will tend to remove the explicit initialization).
From my point of view adding a test case to catch if this value changes would be better.

CreateReproducer = true
};

Expand Down
111 changes: 35 additions & 76 deletions src/Integration.Vsix/CFamily/VcxProject/FileConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,105 +18,64 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using System.ComponentModel.Composition;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell.Interop;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.CFamily.Analysis;
using SonarLint.VisualStudio.Infrastructure.VS;
using SonarLint.VisualStudio.Integration.Helpers;
using System.IO.Abstractions;
using SonarLint.VisualStudio.Core.SystemAbstractions;

namespace SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject
namespace SonarLint.VisualStudio.Integration.Vsix.CFamily.VcxProject;

internal interface IFileConfigProvider
{
IFileConfig Get(string analyzedFilePath);
}

[Export(typeof(IFileConfigProvider))]
[PartCreationPolicy(CreationPolicy.Shared)]
[method: ImportingConstructor]
internal class FileConfigProvider(
IVsUIServiceOperation uiServiceOperation,
IFileInSolutionIndicator fileInSolutionIndicator,
IFileSystemService fileSystem,
ILogger logger,
IThreadHandling threadHandling) : IFileConfigProvider
{
internal interface IFileConfigProvider

public IFileConfig Get(string analyzedFilePath)
{
IFileConfig Get(string analyzedFilePath, CFamilyAnalyzerOptions analyzerOptions);
return uiServiceOperation.Execute<SDTE, DTE2, IFileConfig>(dte =>
GetInternal(analyzedFilePath, dte));
}

[Export(typeof(IFileConfigProvider))]
[PartCreationPolicy(CreationPolicy.Shared)]
[method: ImportingConstructor]
internal class FileConfigProvider(
IVsUIServiceOperation uiServiceOperation,
IFileInSolutionIndicator fileInSolutionIndicator,
IFileSystemService fileSystem,
ILogger logger,
IThreadHandling threadHandling) : IFileConfigProvider
private FileConfig GetInternal(string analyzedFilePath, DTE2 dte)
{
private static readonly NoOpLogger noOpLogger = new NoOpLogger();

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

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

private FileConfig GetInternal(string analyzedFilePath, DTE2 dte, ILogger analysisLogger)
try
{
threadHandling.ThrowIfNotOnUIThread();
var projectItem = dte.Solution.FindProjectItem(analyzedFilePath);

try
if (projectItem == null)
{
var projectItem = dte.Solution.FindProjectItem(analyzedFilePath);

if (projectItem == null)
{
return null;
}

if (!fileInSolutionIndicator.IsFileInSolution(projectItem))
{
analysisLogger.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, fileSystem);
}
catch (Exception ex) when (!Microsoft.VisualStudio.ErrorHandler.IsCriticalException(ex))
{
analysisLogger.WriteLine(CFamilyStrings.ERROR_CreatingConfig, analyzedFilePath, ex);
return null;
}
}

private ILogger GetAnalysisLogger(CFamilyAnalyzerOptions analyzerOptions)
{
if (analyzerOptions is CFamilyAnalyzerOptions cFamilyAnalyzerOptions &&
cFamilyAnalyzerOptions.CreatePreCompiledHeaders)
if (!fileInSolutionIndicator.IsFileInSolution(projectItem))
{
// 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;
logger.LogVerbose($"[VCX:FileConfigProvider] The file is not part of a VCX project. File: {analyzedFilePath}");
return null;
}

return logger;
// 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(logger, projectItem, analyzedFilePath, fileSystem);
}


private class NoOpLogger : ILogger
catch (Exception ex) when (!Microsoft.VisualStudio.ErrorHandler.IsCriticalException(ex))
{
public void WriteLine(string message)
{
// no-op
}

public void WriteLine(string messageFormat, params object[] args)
{
// no-op
}

public void LogVerbose(string message, params object[] args)
{
// no-op
}
logger.WriteLine(CFamilyStrings.ERROR_CreatingConfig, analyzedFilePath, ex);
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public VCXCompilationDatabaseProvider(
}

public ICompilationDatabaseHandle CreateOrNull(string filePath) =>
fileConfigProvider.Get(filePath, null) is { } fileConfig
fileConfigProvider.Get(filePath) is { } fileConfig
? storage.CreateDatabase(fileConfig.CDFile, fileConfig.CDDirectory, fileConfig.CDCommand, GetEnvironmentEntries(fileConfig).Select(x => x.FormattedEntry))
: null;

Expand Down
19 changes: 19 additions & 0 deletions src/SLCore.UnitTests/Analysis/SLCoreAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

using NSubstitute.ExceptionExtensions;
using SonarLint.VisualStudio.CFamily.Analysis;
using SonarLint.VisualStudio.Core;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.Core.CFamily;
Expand Down Expand Up @@ -182,6 +183,24 @@ public void ExecuteAnalysis_ForCFamily_PassesCompilationDatabaseAsExtraPropertie
compilationDatabaseHandle.Received().Dispose();
}

[TestMethod]

Choose a reason for hiding this comment

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

Please also add a test case for when the reproducer is not enabled.

public void ExecuteAnalysis_CFamilyReproducerEnabled_SetsExtraProperty()
{
const string filePath = @"C:\file\path\myclass.cpp";
SetUpCompilationDatabaseLocator(filePath, CreateCompilationDatabaseHandle("somepath"));
SetUpInitializedConfigScope();
var cFamilyAnalyzerOptions = Substitute.For<ICFamilyAnalyzerOptions>();
cFamilyAnalyzerOptions.IsOnOpen.Returns(false);

Choose a reason for hiding this comment

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

Please extract the mock of ICFamilyAnalyzerOptions into a separate method to avoid creating too much noise in the test (lines 192-194)

cFamilyAnalyzerOptions.CreateReproducer.Returns(true);

testSubject.ExecuteAnalysis(filePath, analysisId, [AnalysisLanguage.CFamily], cFamilyAnalyzerOptions, default);

analysisService.Received().AnalyzeFilesAndTrackAsync(Arg.Is<AnalyzeFilesAndTrackParams>(a =>
a.extraProperties != null
&& a.extraProperties["sonar.cfamily.reproducer"] == filePath),
Arg.Any<CancellationToken>());
}

[TestMethod]
public void ExecuteAnalysis_ForCFamily_AnalysisThrows_CompilationDatabaaseDisposed()
{
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
Loading