Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Commit 9c045d2

Browse files
committed
Bugfix (speedup), yet another fix for #16, setting proper values to _MSC_VER and __cplusplus
1 parent 8801f0c commit 9c045d2

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

CPPCheckPlugin/AnalyzerCppcheck.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,36 @@ public override void analyze(List<SourceFile> filesToAnalyze, OutputWindowPane o
6565
cppheckargs += " \"" + file.FilePath + "\"";
6666
}
6767

68-
if (filesToAnalyze.Count > 1) // For single file only checking current configuration (for speed)
68+
// Creating the list of all different macros (no duplicates)
69+
HashSet<string> macros = new HashSet<string>();
70+
macros.Add("__cplusplus=199711L"); // At least in VS2012, this is still 199711L
71+
// Assuming all files passed here are from the same project / same toolset, which should be true, so peeking the first file for global settings
72+
switch (filesToAnalyze[0].vcCompilerVersion)
73+
{
74+
case SourceFile.VCCompilerVersion.vc2010:
75+
macros.Add("_MSC_VER=1600");
76+
break;
77+
case SourceFile.VCCompilerVersion.vc2012:
78+
macros.Add("_MSC_VER=1700");
79+
break;
80+
case SourceFile.VCCompilerVersion.vc2013:
81+
macros.Add("_MSC_VER=1800");
82+
break;
83+
case SourceFile.VCCompilerVersion.vcFuture:
84+
macros.Add("_MSC_VER=1900");
85+
break;
86+
}
87+
88+
if (filesToAnalyze.Count == 1) // For single file only checking current configuration (for speed)
6989
{
70-
// Creating the list of all different macros (no duplicates)
71-
HashSet<string> macros = new HashSet<string>();
7290
foreach (var file in filesToAnalyze)
7391
{
7492
foreach (string macro in file.Macros)
7593
macros.Add(macro);
7694
}
77-
macros.Add("_MSC_VER");
7895
macros.Add("WIN32");
7996
macros.Add("_WIN32");
80-
macros.Add("__cplusplus");
97+
8198
if (is64bitConfiguration)
8299
{
83100
macros.Add("_M_X64");
@@ -90,12 +107,12 @@ public override void analyze(List<SourceFile> filesToAnalyze, OutputWindowPane o
90107

91108
if (isDebugConfiguration)
92109
macros.Add("_DEBUG");
110+
}
93111

94-
foreach (string macro in macros)
95-
{
96-
String macroArgument = " -D" + macro;
97-
cppheckargs += macroArgument;
98-
}
112+
foreach (string macro in macros)
113+
{
114+
String macroArgument = " -D" + macro;
115+
cppheckargs += macroArgument;
99116
}
100117

101118
string analyzerPath = Properties.Settings.Default.CPPcheckPath;

CPPCheckPlugin/CPPCheckPluginPackage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ SourceFile createSourceFile(string filePath, Configuration targetConfig, dynamic
179179
{
180180
var configurationName = targetConfig.ConfigurationName;
181181
dynamic config = project.Configurations.Item(configurationName);
182-
SourceFile sourceForAnalysis = new SourceFile(filePath, project.ProjectDirectory.Replace(@"""", ""));
182+
String toolSetName = config.PlatformToolsetShortName;
183+
SourceFile sourceForAnalysis = new SourceFile(filePath, project.ProjectDirectory.Replace("\"", ""), toolSetName);
183184
dynamic toolsCollection = config.Tools;
184185
foreach (var tool in toolsCollection)
185186
{

CPPCheckPlugin/SourceFile.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,37 @@ namespace VSPackage.CPPCheckPlugin
77
{
88
class SourceFile
99
{
10-
public SourceFile(string fullPath, string projectBasePath)
10+
public enum VCCompilerVersion { vc2010, vc2012, vc2013, vcFuture };
11+
12+
public SourceFile(string fullPath, string projectBasePath, string vcCompilerName)
1113
{
1214
_fullPath = cleanPath(fullPath);
1315
_projectBasePath = cleanPath(projectBasePath);
16+
17+
if (vcCompilerName.Contains("2010"))
18+
_compilerVersion = VCCompilerVersion.vc2010;
19+
else if (vcCompilerName.Contains("2012"))
20+
_compilerVersion = VCCompilerVersion.vc2012;
21+
else if (vcCompilerName.Contains("2013"))
22+
_compilerVersion = VCCompilerVersion.vc2013;
23+
else
24+
_compilerVersion = VCCompilerVersion.vcFuture;
1425
}
1526

1627
// All include paths being added are resolved against projectBasePath
1728
public void addIncludePath(string path)
1829
{
19-
if (!String.IsNullOrEmpty(_projectBasePath) && !String.IsNullOrEmpty(path))
30+
if (!String.IsNullOrEmpty(_projectBasePath) && !String.IsNullOrEmpty(path) && !path.Equals(".") && !path.Equals("\\\".\\\""))
2031
{
21-
Debug.WriteLine("Processing path: " + path);
22-
_includePaths.Add(cleanPath(path.Contains(":") ? path : Path.Combine(_projectBasePath, path)));
32+
Debug.WriteLine("Processing path: " + path);
33+
if (path.Contains("\\:")) // absolute path
34+
_includePaths.Add(cleanPath(path));
35+
else
36+
{
37+
// Relative path - converting to absolute
38+
String pathForCombine = path.Replace("\"", String.Empty).TrimStart('\\', '/');
39+
_includePaths.Add(cleanPath(Path.GetFullPath(Path.Combine(_projectBasePath, pathForCombine)))); // Workaround for Path.Combine bugs
40+
}
2341
}
2442
}
2543

@@ -71,6 +89,11 @@ public List<string> Macros
7189
get { return _activeMacros; }
7290
}
7391

92+
public VCCompilerVersion vcCompilerVersion
93+
{
94+
get { return _compilerVersion; }
95+
}
96+
7497
private static string cleanPath(string path)
7598
{
7699
string result = path.Replace("\"", "").Replace("\\\\", "\\");
@@ -85,5 +108,6 @@ private static string cleanPath(string path)
85108
private string _projectBasePath = null;
86109
private List<string> _includePaths = new List<string>();
87110
private List<string> _activeMacros = new List<string>();
111+
private VCCompilerVersion _compilerVersion;
88112
}
89113
}

0 commit comments

Comments
 (0)