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-1669 Handle the case of C/C++ files outside the projects #5890

Merged
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
14 changes: 10 additions & 4 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.Core;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.Core.CFamily;
using SonarLint.VisualStudio.Core.ConfigurationScope;
Expand All @@ -43,6 +44,7 @@ public class SLCoreAnalyzerTests
private ICurrentTimeProvider currentTimeProvider;
private IAggregatingCompilationDatabaseProvider compilationDatabaseLocator;
private IAnalysisStatusNotifier notifier;
private ILogger logger;
private SLCoreAnalyzer testSubject;

[TestInitialize]
Expand All @@ -58,11 +60,13 @@ public void TestInitialize()
SetUpDefaultNotifier();
currentTimeProvider = Substitute.For<ICurrentTimeProvider>();
compilationDatabaseLocator = Substitute.For<IAggregatingCompilationDatabaseProvider>();
logger = new TestLogger();
testSubject = new SLCoreAnalyzer(slCoreServiceProvider,
activeConfigScopeTracker,
analysisStatusNotifierFactory,
currentTimeProvider,
compilationDatabaseLocator);
compilationDatabaseLocator,
logger);

void SetUpDefaultNotifier() => analysisStatusNotifierFactory.Create(nameof(SLCoreAnalyzer), FilePath, analysisId).Returns(notifier);
}
Expand All @@ -74,7 +78,8 @@ public void MefCtor_CheckIsExported() =>
MefTestHelpers.CreateExport<IActiveConfigScopeTracker>(),
MefTestHelpers.CreateExport<IAnalysisStatusNotifierFactory>(),
MefTestHelpers.CreateExport<ICurrentTimeProvider>(),
MefTestHelpers.CreateExport<IAggregatingCompilationDatabaseProvider>());
MefTestHelpers.CreateExport<IAggregatingCompilationDatabaseProvider>(),
MefTestHelpers.CreateExport<ILogger>());

[TestMethod]
public void MefCtor_CheckIsSingleton() => MefTestHelpers.CheckIsSingletonMefComponent<SLCoreAnalyzer>();
Expand Down Expand Up @@ -206,7 +211,7 @@ public void ExecuteAnalysis_ForCFamily_AnalysisThrows_CompilationDatabaaseDispos
}

[TestMethod]
public void ExecuteAnalysis_ForCFamily_WithoutCompilationDatabase_DoesNotPassExtraProperty()
public void ExecuteAnalysis_ForCFamily_WithoutCompilationDatabase_PassesEmptyStringAsExtraProperty()
{
const string filePath = @"C:\file\path\myclass.cpp";
SetUpCompilationDatabaseLocator(filePath, null);
Expand All @@ -216,7 +221,8 @@ public void ExecuteAnalysis_ForCFamily_WithoutCompilationDatabase_DoesNotPassExt

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

Expand Down
14 changes: 12 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.Core;
using SonarLint.VisualStudio.Core.Analysis;
using SonarLint.VisualStudio.Core.CFamily;
using SonarLint.VisualStudio.Core.ConfigurationScope;
Expand All @@ -41,20 +42,23 @@ public class SLCoreAnalyzer : IAnalyzer
private readonly IAnalysisStatusNotifierFactory analysisStatusNotifierFactory;
private readonly ICurrentTimeProvider currentTimeProvider;
private readonly IAggregatingCompilationDatabaseProvider compilationDatabaseLocator;
private readonly ILogger logger;

[ImportingConstructor]
public SLCoreAnalyzer(
ISLCoreServiceProvider serviceProvider,
IActiveConfigScopeTracker activeConfigScopeTracker,
IAnalysisStatusNotifierFactory analysisStatusNotifierFactory,
ICurrentTimeProvider currentTimeProvider,
IAggregatingCompilationDatabaseProvider compilationDatabaseLocator)
IAggregatingCompilationDatabaseProvider compilationDatabaseLocator,
ILogger logger)
{
this.serviceProvider = serviceProvider;
this.activeConfigScopeTracker = activeConfigScopeTracker;
this.analysisStatusNotifierFactory = analysisStatusNotifierFactory;
this.currentTimeProvider = currentTimeProvider;
this.compilationDatabaseLocator = compilationDatabaseLocator;
this.logger = logger;
}

public bool IsAnalysisSupported(IEnumerable<AnalysisLanguage> languages) => true;
Expand Down Expand Up @@ -134,7 +138,13 @@ private IDisposable EnrichPropertiesForCFamily(Dictionary<string, string> proper
}

var compilationDatabaseHandle = compilationDatabaseLocator.GetOrNull(path);
if (compilationDatabaseHandle != null)
if (compilationDatabaseHandle == null)
{
logger.WriteLine(SLCoreStrings.CompilationDatabaseNotFound, path);
// Pass empty compilation database path in order to get a more helpful message and not break the analyzer
properties[CFamilyCompileCommandsProperty] = "";
}
else
{
properties[CFamilyCompileCommandsProperty] = compilationDatabaseHandle.FilePath;
}
Expand Down
9 changes: 9 additions & 0 deletions src/SLCore/SLCoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/SLCore/SLCoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,7 @@
<data name="CertificateValidator_FailureReasonTemplate" xml:space="preserve">
<value>[CertificateChainValidator] {0}: {1}</value>
</data>
<data name="CompilationDatabaseNotFound" xml:space="preserve">
<value>[SLCoreAnalyzer] No compilation database found for file: {0}</value>
michael-jabbour-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
</data>
</root>
Loading