From 8995923a8d0c5eab77dd99488559f7bc8ce16d37 Mon Sep 17 00:00:00 2001 From: Michael Jabbour Date: Fri, 29 Nov 2024 16:49:56 +0100 Subject: [PATCH] Workaround for identifying header files in the CFamily analyzer Normally, the CFamily analyzer deduces that the file being analyzed is a header when there is no exact match for the file name in the given compilation database. This is a temporary workaround to communicate with the analyzer that the current file is a header file. This helps the analyzer avoid reporting some FPs. See https://sonarsource.atlassian.net/browse/CPP-2743 --- .../CFamily/VcxProject/FileConfigTests.cs | 3 +++ src/Integration.Vsix/CFamily/VcxProject/FileConfig.cs | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigTests.cs b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigTests.cs index 587e11f84..8465e4664 100644 --- a/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigTests.cs +++ b/src/Integration.Vsix.UnitTests/CFamily/VcxProject/FileConfigTests.cs @@ -174,6 +174,7 @@ public void TryGet_HeaderFileOptions_ReturnsValidConfig() // Assert request.Should().NotBeNull(); Assert.AreEqual("\"C:\\path\\cl.exe\" /Yu\"pch.h\" /FI\"pch.h\" /EHsc /RTCu \"c:\\dummy\\file.h\"", request.CDCommand); + Assert.AreEqual("non_existent_file", request.CDFile); // Arrange projectItemConfig.FileConfigProperties["CompileAs"] = "CompileAsC"; @@ -184,6 +185,7 @@ public void TryGet_HeaderFileOptions_ReturnsValidConfig() // Assert Assert.AreEqual("\"C:\\path\\cl.exe\" /FI\"FHeader.h\" /Yu\"pch.h\" /EHsc /RTCu /TC \"c:\\dummy\\file.h\"", request.CDCommand); + Assert.AreEqual("non_existent_file", request.CDFile); // Arrange projectItemConfig.FileConfigProperties["CompileAs"] = "CompileAsCpp"; @@ -193,6 +195,7 @@ public void TryGet_HeaderFileOptions_ReturnsValidConfig() // Assert Assert.AreEqual("\"C:\\path\\cl.exe\" /FI\"FHeader.h\" /Yu\"pch.h\" /EHsc /RTCu /TP \"c:\\dummy\\file.h\"", request.CDCommand); + Assert.AreEqual("non_existent_file", request.CDFile); } [TestMethod] diff --git a/src/Integration.Vsix/CFamily/VcxProject/FileConfig.cs b/src/Integration.Vsix/CFamily/VcxProject/FileConfig.cs index 93e11de61..1285eb00a 100644 --- a/src/Integration.Vsix/CFamily/VcxProject/FileConfig.cs +++ b/src/Integration.Vsix/CFamily/VcxProject/FileConfig.cs @@ -46,7 +46,8 @@ public static FileConfig TryGet(ILogger logger, ProjectItem dteProjectItem, stri return null; } - CmdBuilder cmdBuilder = new CmdBuilder(vcFile.ItemType == "ClInclude"); + var isHeader = vcFile.ItemType == "ClInclude"; + CmdBuilder cmdBuilder = new CmdBuilder(isHeader); var compilerPath = vcConfig.GetEvaluatedPropertyValue("ClCompilerPath"); if (string.IsNullOrEmpty(compilerPath)) @@ -75,7 +76,9 @@ public static FileConfig TryGet(ILogger logger, ProjectItem dteProjectItem, stri { CDDirectory = Path.GetDirectoryName(vcProject.ProjectFile), CDCommand = cmdBuilder.GetFullCmd(), - CDFile = absoluteFilePath, + // A hack to communicate with the CFamily analyzer that this is a header file. + // A long-term solution should be investigated in CPP-5898. + CDFile = isHeader ? "non_existent_file" : absoluteFilePath, EnvInclude = envINCLUDE, }; }