Skip to content

Commit

Permalink
SLVS-1669 Handle the case of C/C++ files outside the projects (#5890)
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaskos-sonar authored and georgii-borovinskikh-sonarsource committed Dec 24, 2024
1 parent 9bb2983 commit 1e7563b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
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.

2 changes: 2 additions & 0 deletions src/SLCore/SLCoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -179,5 +179,7 @@
</data>
<data name="UnknowProxyType" xml:space="preserve">
<value>Proxy type can not be determined from scheme '{0}'. Returning HTTP proxy type.</value>
<data name="CompilationDatabaseNotFound" xml:space="preserve">
<value>[SLCoreAnalyzer] No compilation database found for file: {0}. Check that the file is part of a supported project type in the current solution.</value>
</data>
</root>

0 comments on commit 1e7563b

Please sign in to comment.