Skip to content

Commit

Permalink
SLVS-1668 Include header file language in the VCX command (#5861)
Browse files Browse the repository at this point in the history
[SLVS-1668](https://sonarsource.atlassian.net/browse/SLVS-1668)

Part of SLVS-1637

See the linked ticket for details about the problems we identified. This
PR introduces the following changes:

- To communicate the header file language, I am adding the relevant
switches to the generated command in the header case as well. This helps
analyze C headers as such when they are located inside C VCX projects.
- Since `HeaderFileLanguage` is no longer needed, I am removing it from
`FileConfig`.

[SLVS-1668]:
https://sonarsource.atlassian.net/browse/SLVS-1668?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
michael-jabbour-sonarsource authored Dec 10, 2024
1 parent 4b7a22b commit 37eb38b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
13 changes: 5 additions & 8 deletions src/Integration.Vsix.UnitTests/CFamily/CmdBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,23 +314,21 @@ public void AddOptFromProperties(string input, string output, string cmd)
cmdBuilder.AddOptFromProperties(settingsMock.Object);

cmdBuilder.GetFullCmd().Should().Be(cmd);
cmdBuilder.HeaderFileLang.Should().Be("");
}

[TestMethod]
[DataRow("Default", "cpp")]
[DataRow("CompileAsC", "c")]
[DataRow("CompileAsCpp", "cpp")]
public void HeaderFileLang(string compileAs, string lang)
[DataRow("Default", "")]
[DataRow("CompileAsC", "/TC ")]
[DataRow("CompileAsCpp", "/TP ")]
public void HeaderFileLang(string compileAs, string cmd)
{
var cmdBuilder = new CmdBuilder(true);
var settingsMock = new Mock<IVCRulePropertyStorage>();
settingsMock.Setup(x => x.GetEvaluatedPropertyValue(It.IsAny<string>())).Returns<string>(s => s == "CompileAs" ? compileAs : "");

cmdBuilder.AddOptFromProperties(settingsMock.Object);

cmdBuilder.GetFullCmd().Should().Be("");
cmdBuilder.HeaderFileLang.Should().Be(lang);
cmdBuilder.GetFullCmd().Should().Be(cmd);
}

[TestMethod]
Expand Down Expand Up @@ -391,7 +389,6 @@ public void PCHUse()

cmdBuilder.AddOptFromProperties(settingsMock.Object);
cmdBuilder.GetFullCmd().Should().Be("/Yc\"C:\\pch.h\" ");
cmdBuilder.HeaderFileLang.Should().Be("");
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public void TryGet_Full_Cmd()
// Assert
request.Should().NotBeNull();
Assert.AreEqual("\"C:\\path\\cl.exe\" /permissive- /std:c++17 /EHsc /arch:AVX512 /MT /RTCu /Zp8 /TP /DA \"c:\\dummy\\file.cpp\"", request.CDCommand);
Assert.AreEqual("", request.HeaderFileLanguage);
Assert.AreEqual("C:\\path\\includeDir1;C:\\path\\includeDir2;C:\\path\\includeDir3;", request.EnvInclude);
Assert.AreEqual("c:\\dummy\\file.cpp", request.CDFile);
Assert.AreEqual("c:\\foo", request.CDDirectory);
Expand Down Expand Up @@ -175,7 +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("cpp", request.HeaderFileLanguage);
Assert.AreEqual("c:\\dummy\\file.h", request.CDFile);

// Arrange
projectItemConfig.FileConfigProperties["CompileAs"] = "CompileAsC";
Expand All @@ -185,8 +184,18 @@ public void TryGet_HeaderFileOptions_ReturnsValidConfig()
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h");

// Assert
Assert.AreEqual("\"C:\\path\\cl.exe\" /FI\"FHeader.h\" /Yu\"pch.h\" /EHsc /RTCu \"c:\\dummy\\file.h\"", request.CDCommand);
Assert.AreEqual("c", request.HeaderFileLanguage);
Assert.AreEqual("\"C:\\path\\cl.exe\" /FI\"FHeader.h\" /Yu\"pch.h\" /EHsc /RTCu /TC \"c:\\dummy\\file.h\"", request.CDCommand);
Assert.AreEqual("c:\\dummy\\file.h", request.CDFile);

// Arrange
projectItemConfig.FileConfigProperties["CompileAs"] = "CompileAsCpp";

// Act
request = FileConfig.TryGet(testLogger, projectItemMock.Object, "c:\\dummy\\file.h");

// Assert
Assert.AreEqual("\"C:\\path\\cl.exe\" /FI\"FHeader.h\" /Yu\"pch.h\" /EHsc /RTCu /TP \"c:\\dummy\\file.h\"", request.CDCommand);
Assert.AreEqual("c:\\dummy\\file.h", request.CDFile);
}

[TestMethod]
Expand Down
10 changes: 1 addition & 9 deletions src/Integration.Vsix/CFamily/VcxProject/CmdBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ internal class CmdBuilder

StringBuilder Cmd { get; set; } = new StringBuilder();
bool IsHeader { get; set; }
public string HeaderFileLang { get; set; } = "";

public CmdBuilder(bool isHeader)
{
Expand Down Expand Up @@ -167,14 +166,7 @@ internal void AddOptFromProperties(IVCRulePropertyStorage properties)
AddCmdOpt(ConvertStructMemberAlignment(structMemberAlignment));

var compileAs = properties.GetEvaluatedPropertyValue("CompileAs");
if (IsHeader)
{
HeaderFileLang = (compileAs == "CompileAsC") ? "c" : "cpp";
}
else
{
AddCmdOpt(ConvertCompileAsAndGetLanguage(compileAs));
}
AddCmdOpt(ConvertCompileAsAndGetLanguage(compileAs));

// Additional options
var additionalOptions = properties.GetEvaluatedPropertyValue("AdditionalOptions");
Expand Down
4 changes: 2 additions & 2 deletions src/Integration.Vsix/CFamily/VcxProject/FileConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public static FileConfig TryGet(ILogger logger, ProjectItem dteProjectItem, stri
// Not supported
return null;
}

CmdBuilder cmdBuilder = new CmdBuilder(vcFile.ItemType == "ClInclude");

var compilerPath = vcConfig.GetEvaluatedPropertyValue("ClCompilerPath");
Expand Down Expand Up @@ -76,7 +77,6 @@ public static FileConfig TryGet(ILogger logger, ProjectItem dteProjectItem, stri
CDCommand = cmdBuilder.GetFullCmd(),
CDFile = absoluteFilePath,
EnvInclude = envINCLUDE,
HeaderFileLanguage = cmdBuilder.HeaderFileLang,
};
}

Expand Down Expand Up @@ -134,7 +134,7 @@ private static IVCRulePropertyStorage GetVcFileSettings(ILogger logger, string a
public string CDCommand { get; set; }
public string CDFile { get; set; }
public string EnvInclude { get; set; }
public string HeaderFileLanguage { get; set; }

#endregion

}
Expand Down
1 change: 0 additions & 1 deletion src/Integration.Vsix/CFamily/VcxProject/IFileConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ internal interface IFileConfig
string CDCommand { get; }
string CDFile { get; }
string EnvInclude { get; }
string HeaderFileLanguage { get; }
}
}

0 comments on commit 37eb38b

Please sign in to comment.