Skip to content

Commit

Permalink
Workaround for identifying header files in the CFamily analyzer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
michael-jabbour-sonarsource committed Nov 29, 2024
1 parent c3dd043 commit 8995923
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand All @@ -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]
Expand Down
7 changes: 5 additions & 2 deletions src/Integration.Vsix/CFamily/VcxProject/FileConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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,
};
}
Expand Down

0 comments on commit 8995923

Please sign in to comment.