Skip to content

Commit

Permalink
Merge pull request #1668 from rudolfgrauberger/vc-log-improvements
Browse files Browse the repository at this point in the history
Improves Visual studio/MSBUILD log file analysis
  • Loading branch information
guwirth authored Mar 9, 2019
2 parents 39fb933 + 1ed7419 commit a3e16ef
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cxx-squid/src/main/java/org/sonar/cxx/CxxConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ public void setCompilationPropertiesWithBuildLog(@Nullable List<File> reports,
if ("Visual C++".equals(fileFormat)) {
cxxVCppParser.parseVCppLog(buildLog, baseDir, charsetName);
LOG.info("Parse build log '" + buildLog.getAbsolutePath()
+ "' added includes: '" + uniqueIncludes.size()
+ "', added defines: '" + uniqueDefines.size() + "'");
+ "' added includes: '" + getIncludeDirectories().size()
+ "', added defines: '" + getDefines().size() + "'");
if (LOG.isDebugEnabled()) {
for (List<String> allIncludes : uniqueIncludes.values()) {
if (!allIncludes.isEmpty()) {
Expand Down
12 changes: 7 additions & 5 deletions cxx-squid/src/main/java/org/sonar/cxx/CxxVCppBuildLogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class CxxVCppBuildLogParser {
private static final Pattern TOOLSET_V141_PATTERN = Pattern
.compile("^.*VC\\\\Tools\\\\MSVC\\\\14\\.1\\d\\.\\d+\\\\bin\\\\HostX(86|64)\\\\x(86|64)\\\\CL.exe.*$");

// It seems that the required line in any language has these elements: "ClCompile" and (*.vcxproj)
private static final Pattern PATH_TO_VCXPROJ = Pattern.compile("^(?:\\S+)\\s(?:\"ClCompile\").*\"(.+vcxproj)\".*$");

private final Map<String, List<String>> uniqueIncludes;
private final Map<String, Set<String>> uniqueDefines;
private String platformToolset = "V120";
Expand Down Expand Up @@ -143,12 +146,11 @@ public void parseVCppLog(File buildLog, String baseDir, String charsetName) {
// from project
// "D:\Development\SonarQube\cxx\sonar-cxx\integration-tests\testdata\googletest_bullseye_vs_project\
// PathHandling.Test\PathHandling.Test.vcxproj" (target "_ClCompile" depends on it):
if (line.contains("Target \"ClCompile\" in file")) {
String pathProject = line.split("\" from project \"")[1].split("\\s+")[0].replace("\"", "");
if (pathProject.endsWith(":")) {
pathProject = pathProject.substring(0, pathProject.length() - 2);
}
if (PATH_TO_VCXPROJ.matcher(line).matches())
{
String pathProject = getMatches(PATH_TO_VCXPROJ, line).get(0);
currentProjectPath = Paths.get(pathProject).getParent();

if (currentProjectPath == null) {
currentProjectPath = Paths.get(baseDir);
}
Expand Down
113 changes: 113 additions & 0 deletions cxx-squid/src/test/java/org/sonar/cxx/CxxVCppBuildLogParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2019 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx;

import org.assertj.core.api.SoftAssertions;
import org.junit.BeforeClass;
import org.junit.Test;
import org.sonar.api.internal.apachecommons.lang.SystemUtils;

import java.io.File;
import java.util.*;

import static org.junit.Assert.*;

/**
* These tests ensure that the relative paths in the INCLUDES are correctly converted to absolute paths.
* The project directory is used as the base directory.
* The project directory is extracted regardless of the language of the log file.
*
* @author rudolfgrauberger
*/
public class CxxVCppBuildLogParserTest {

private static final String VC_CHARSET = "UTF8";

public static final String OVERALLINCLUDEKEY = "CxxOverallInclude";
public static final String OVERALLDEFINEKEY = "CxxOverallDefine";
public static final String REFERENCE_DETAILED_LOG = "src/test/resources/logfile/msbuild-detailed-en.txt";
public static final String UNIQUE_FILE = "D:\\Development\\Source\\Cpp\\Dummy\\src\\main.cpp";

@BeforeClass
public static void init()
{
org.junit.Assume.assumeTrue(SystemUtils.IS_OS_WINDOWS);
}

@Test
public void relativeIncludesFromReferenceLog() {

List<String> includes = getIncludesForReferenceLogFile();

SoftAssertions softly = new SoftAssertions();

// Absolute path
softly.assertThat(includes).contains("D:\\Development\\Source\\ThirdParty\\VS2017\\Firebird-2.5.8\\include");
// Relative paths
softly.assertThat(includes).contains("D:\\Development\\Source\\ThirdParty\\VS2017\\Boost-1.67.0");
softly.assertThat(includes).contains("D:\\Development\\Source\\Cpp\\Dummy\\includes");
softly.assertThat(includes).contains("D:\\Development\\Source\\Cpp\\Dummy\\release");
softly.assertThat(includes).hasSize(4);
softly.assertAll();
}

@Test
public void relativeIncludesFromGermanLog() {

List<String> refIncludes = getIncludesForReferenceLogFile();
List<String> includes = getIncludesForUniqueFile("src/test/resources/logfile/msbuild-detailed-de.txt");

SoftAssertions softly = new SoftAssertions();

softly.assertThat(includes).containsExactlyInAnyOrderElementsOf(refIncludes);
softly.assertAll();
}

@Test
public void relativeIncludesFromFrenchLog() {

List<String> refIncludes = getIncludesForReferenceLogFile();
List<String> includes = getIncludesForUniqueFile("src/test/resources/logfile/msbuild-detailed-fr.txt");

SoftAssertions softly = new SoftAssertions();

softly.assertThat(includes).containsExactlyInAnyOrderElementsOf(refIncludes);
softly.assertAll();
}

private List<String> getIncludesForReferenceLogFile() {
return getIncludesForUniqueFile(REFERENCE_DETAILED_LOG);
}

private List<String> getIncludesForUniqueFile(String log) {
Map<String, List<String>> uniqueIncludes = new HashMap<>();
uniqueIncludes.put(OVERALLINCLUDEKEY, new ArrayList<>());
Map<String, Set<String>> uniqueDefines = new HashMap<>();
uniqueDefines.put(OVERALLDEFINEKEY, new HashSet<>());

File logFile = new File(log);

CxxVCppBuildLogParser parser = new CxxVCppBuildLogParser(uniqueIncludes, uniqueDefines);
parser.parseVCppLog(logFile, ".", VC_CHARSET);

List<String> includes = uniqueIncludes.get(UNIQUE_FILE);
return includes;
}
}
3 changes: 3 additions & 0 deletions cxx-squid/src/test/resources/logfile/msbuild-detailed-de.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Ziel "ClCompile" in Datei "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets" aus Projekt "D:\Development\Source\Cpp\Dummy\MyProject.vcxproj" (Ziel "_ClCompile" ist davon abhängig):
CL-Aufgabe
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\CL.exe /c /I"D:\Development\Source\ThirdParty\VS2017\Firebird-2.5.8\include" /I"..\..\ThirdParty\VS2017\Boost-1.67.0" /Iincludes /Irelease /Zi /nologo /W3 /WX- /diagnostics:classic /MP4 /Ox /Oy- /GL /D _CONSOLE /D UNICODE /D WIN32 /D _SCL_SECURE_NO_WARNINGS /D _CRT_SECURE_NO_WARNINGS /D _WIN32_WINNT=0x0600 /D WIN32_LEAN_AND_MEAN /D VC_EXTRALEAN /D BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE /D NDEBUG /Gm- /EHsc /MD /GS /Gy /arch:SSE2 /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"release\\" /Fd"release\vc141.pdb" /Gd /TP /analyze- /FC /errorReport:queue /we4700 /we4715 /we4150 -Zm200 /std:c++17 /w14265 /Zc:rvalueCast /Gw -w34100 -w34189 src\MyClass.cpp src\main.cpp
3 changes: 3 additions & 0 deletions cxx-squid/src/test/resources/logfile/msbuild-detailed-en.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Target "ClCompile" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets" from project "D:\Development\Source\Cpp\Dummy\MyProject.vcxproj" (target "_ClCompile" depends on it):
Task "CL"
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\CL.exe /c /I"D:\Development\Source\ThirdParty\VS2017\Firebird-2.5.8\include" /I"..\..\ThirdParty\VS2017\Boost-1.67.0" /Iincludes /Irelease /Zi /nologo /W3 /WX- /diagnostics:classic /MP4 /Ox /Oy- /GL /D _CONSOLE /D UNICODE /D WIN32 /D _SCL_SECURE_NO_WARNINGS /D _CRT_SECURE_NO_WARNINGS /D _WIN32_WINNT=0x0600 /D WIN32_LEAN_AND_MEAN /D VC_EXTRALEAN /D BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE /D NDEBUG /Gm- /EHsc /MD /GS /Gy /arch:SSE2 /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"release\\" /Fd"release\vc141.pdb" /Gd /TP /analyze- /FC /errorReport:queue /we4700 /we4715 /we4150 -Zm200 /std:c++17 /w14265 /Zc:rvalueCast /Gw -w34100 -w34189 src\MyClass.cpp src\main.cpp
3 changes: 3 additions & 0 deletions cxx-squid/src/test/resources/logfile/msbuild-detailed-fr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Cible "ClCompile" dans le fichier "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\VC\VCTargets\Microsoft.CppCommon.targets" du projet "D:\Development\Source\Cpp\Dummy\MyProject.vcxproj" (la cible "_ClCompile" en dépend) :
Tâche "CL"
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\CL.exe /c /I"D:\Development\Source\ThirdParty\VS2017\Firebird-2.5.8\include" /I"..\..\ThirdParty\VS2017\Boost-1.67.0" /Iincludes /Irelease /Zi /nologo /W3 /WX- /diagnostics:classic /MP4 /Ox /Oy- /GL /D _CONSOLE /D UNICODE /D WIN32 /D _SCL_SECURE_NO_WARNINGS /D _CRT_SECURE_NO_WARNINGS /D _WIN32_WINNT=0x0600 /D WIN32_LEAN_AND_MEAN /D VC_EXTRALEAN /D BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE /D NDEBUG /Gm- /EHsc /MD /GS /Gy /arch:SSE2 /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"release\\" /Fd"release\vc141.pdb" /Gd /TP /analyze- /FC /errorReport:queue /we4700 /we4715 /we4150 -Zm200 /std:c++17 /w14265 /Zc:rvalueCast /Gw -w34100 -w34189 src\MyClass.cpp src\main.cpp

0 comments on commit a3e16ef

Please sign in to comment.