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

Commit b1aa7ed

Browse files
committed
VS 2017, 2019 versions added, better _MSC_VER handling, minor refactoring
1 parent d983f69 commit b1aa7ed

File tree

3 files changed

+83
-81
lines changed

3 files changed

+83
-81
lines changed

CPPCheckPlugin/AnalyzerCppcheck.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,15 @@ private string getCPPCheckArgs(ConfiguredFiles configuredFiles, bool analysisOnS
186186
case SourceFile.VCCompilerVersion.vc2013:
187187
macros.Add("_MSC_VER=1800");
188188
break;
189-
case SourceFile.VCCompilerVersion.vcFuture:
189+
case SourceFile.VCCompilerVersion.vc2015:
190190
macros.Add("_MSC_VER=1900");
191191
break;
192+
case SourceFile.VCCompilerVersion.vc2017:
193+
macros.Add("_MSC_VER=1916");
194+
break;
195+
case SourceFile.VCCompilerVersion.vc2019:
196+
macros.Add("_MSC_VER=1920");
197+
break;
192198
}
193199

194200
foreach (var file in filesToAnalyze)

CPPCheckPlugin/CPPCheckPluginPackage.cs

Lines changed: 66 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void onCheckCurrentProjectRequested(object sender, EventArgs e)
242242
JoinableTaskFactory.Run(async () =>
243243
{
244244
await JoinableTaskFactory.SwitchToMainThreadAsync();
245-
await checkFirstActiveProjectAsync();
245+
_ = checkFirstActiveProjectAsync();
246246
});
247247
}
248248

@@ -251,16 +251,28 @@ private void onCheckAllProjectsRequested(object sender, EventArgs e)
251251
JoinableTaskFactory.Run(async () =>
252252
{
253253
await JoinableTaskFactory.SwitchToMainThreadAsync();
254-
await checkAllActiveProjectsAsync();
254+
255+
Object[] activeProjects = await getActiveProjectsAsync();
256+
if (activeProjects != null)
257+
_ = checkProjectsAsync(activeProjects);
255258
});
256259
}
257260

258261
private void onCheckSelectionsRequested(object sender, EventArgs e)
259262
{
260263
JoinableTaskFactory.Run(async () =>
261264
{
262-
await JoinableTaskFactory.SwitchToMainThreadAsync();
263-
await checkSelectionsAsync();
265+
List<ConfiguredFiles> configuredFilesList = await getActiveSelectionsAsync();
266+
267+
await _instance.JoinableTaskFactory.SwitchToMainThreadAsync();
268+
269+
MainToolWindow.Instance.ContentsType = ICodeAnalyzer.AnalysisType.ProjectAnalysis;
270+
MainToolWindow.Instance.showIfWindowNotCreated();
271+
272+
if (configuredFilesList.Count > 0)
273+
{
274+
runAnalysis(configuredFilesList, false);
275+
}
264276
});
265277
}
266278

@@ -279,69 +291,64 @@ private void documentSavedSync(Document document)
279291
{
280292
JoinableTaskFactory.Run(async () =>
281293
{
282-
await documentSavedAsync(document);
283-
});
284-
}
285-
286-
private async Task documentSavedAsync(Document document)
287-
{
288-
await JoinableTaskFactory.SwitchToMainThreadAsync();
289-
290-
if (document == null || document.Language != "C/C++")
291-
return;
294+
await JoinableTaskFactory.SwitchToMainThreadAsync();
292295

293-
if (Settings.Default.CheckSavedFilesHasValue && Settings.Default.CheckSavedFiles == false)
294-
return;
296+
if (document == null || document.Language != "C/C++")
297+
return;
295298

296-
if (document.ActiveWindow == null)
297-
{
298-
// We get here when new files are being created and added to the project and
299-
// then trying to obtain document.ProjectItem yields an exception. Will just skip this.
300-
return;
301-
}
302-
try
303-
{
304-
var kind = document.ProjectItem.ContainingProject.Kind;
305-
if (!isVisualCppProject(document.ProjectItem.ContainingProject.Kind))
306-
{
299+
if (Settings.Default.CheckSavedFilesHasValue && Settings.Default.CheckSavedFiles == false)
307300
return;
308-
}
309301

310-
Configuration currentConfig = null;
311-
try { currentConfig = document.ProjectItem.ConfigurationManager.ActiveConfiguration; }
312-
catch (Exception) { currentConfig = null; }
313-
if (currentConfig == null)
302+
if (document.ActiveWindow == null)
314303
{
315-
MessageBox.Show("Cannot perform check - no valid configuration selected", "Cppcheck error");
304+
// We get here when new files are being created and added to the project and
305+
// then trying to obtain document.ProjectItem yields an exception. Will just skip this.
316306
return;
317307
}
318-
319-
dynamic project = document.ProjectItem.ContainingProject.Object;
320-
SourceFile sourceForAnalysis = await createSourceFileAsync(document.FullName, currentConfig, project);
321-
if (sourceForAnalysis == null)
322-
return;
323-
324-
if (!Settings.Default.CheckSavedFilesHasValue)
308+
try
325309
{
326-
askCheckSavedFiles();
310+
var kind = document.ProjectItem.ContainingProject.Kind;
311+
if (!isVisualCppProject(document.ProjectItem.ContainingProject.Kind))
312+
{
313+
return;
314+
}
327315

328-
if (!Settings.Default.CheckSavedFiles)
316+
Configuration currentConfig = null;
317+
try { currentConfig = document.ProjectItem.ConfigurationManager.ActiveConfiguration; }
318+
catch (Exception) { currentConfig = null; }
319+
if (currentConfig == null)
320+
{
321+
MessageBox.Show("Cannot perform check - no valid configuration selected", "Cppcheck error");
329322
return;
330-
}
323+
}
331324

332-
MainToolWindow.Instance.showIfWindowNotCreated();
333-
MainToolWindow.Instance.ContentsType = ICodeAnalyzer.AnalysisType.DocumentSavedAnalysis;
334-
runSavedFileAnalysis(sourceForAnalysis, currentConfig);
335-
}
336-
catch (Exception ex)
337-
{
338-
if (_outputPane != null)
325+
dynamic project = document.ProjectItem.ContainingProject.Object;
326+
SourceFile sourceForAnalysis = await createSourceFileAsync(document.FullName, currentConfig, project);
327+
if (sourceForAnalysis == null)
328+
return;
329+
330+
if (!Settings.Default.CheckSavedFilesHasValue)
331+
{
332+
askCheckSavedFiles();
333+
334+
if (!Settings.Default.CheckSavedFiles)
335+
return;
336+
}
337+
338+
MainToolWindow.Instance.showIfWindowNotCreated();
339+
MainToolWindow.Instance.ContentsType = ICodeAnalyzer.AnalysisType.DocumentSavedAnalysis;
340+
runSavedFileAnalysis(sourceForAnalysis, currentConfig);
341+
}
342+
catch (Exception ex)
339343
{
340-
_outputPane.Clear();
341-
addTextToOutputWindow("Exception occurred in cppcheck add-in: " + ex.Message);
344+
if (_outputPane != null)
345+
{
346+
_outputPane.Clear();
347+
addTextToOutputWindow("Exception occurred in cppcheck add-in: " + ex.Message);
348+
}
349+
DebugTracer.Trace(ex);
342350
}
343-
DebugTracer.Trace(ex);
344-
}
351+
});
345352
}
346353

347354
public static void askCheckSavedFiles()
@@ -522,30 +529,6 @@ private async Task checkFirstActiveProjectAsync()
522529
_ = checkProjectsAsync(new Object[1] { activeProjects[0] });
523530
}
524531

525-
private async Task checkAllActiveProjectsAsync()
526-
{
527-
await JoinableTaskFactory.SwitchToMainThreadAsync();
528-
529-
Object[] activeProjects = await getActiveProjectsAsync();
530-
if (activeProjects != null)
531-
_ = checkProjectsAsync(activeProjects);
532-
}
533-
534-
private async Task checkSelectionsAsync()
535-
{
536-
List<ConfiguredFiles> configuredFilesList = await getActiveSelectionsAsync();
537-
538-
await _instance.JoinableTaskFactory.SwitchToMainThreadAsync();
539-
540-
MainToolWindow.Instance.ContentsType = ICodeAnalyzer.AnalysisType.ProjectAnalysis;
541-
MainToolWindow.Instance.showIfWindowNotCreated();
542-
543-
if (configuredFilesList.Count > 0)
544-
{
545-
runAnalysis(configuredFilesList, false);
546-
}
547-
}
548-
549532
private async Task<List<SourceFile>> getProjectFilesAsync(Project p, Configuration currentConfig)
550533
{
551534
await JoinableTaskFactory.SwitchToMainThreadAsync();
@@ -767,7 +750,11 @@ private async void checkProgressUpdated(object sender, ICodeAnalyzer.ProgressEve
767750
{
768751
await System.Threading.Tasks.Task.Delay(5000);
769752
await JoinableTaskFactory.SwitchToMainThreadAsync();
770-
statusBar.Progress(false, label, 100, 100);
753+
try
754+
{
755+
statusBar.Progress(false, label, 100, 100);
756+
}
757+
catch (Exception) { }
771758
});
772759
}
773760
}

CPPCheckPlugin/SourceFile.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace VSPackage.CPPCheckPlugin
99
{
1010
public class SourceFile
1111
{
12-
public enum VCCompilerVersion { vc2003, vc2005, vc2008, vc2010, vc2012, vc2013, vcFuture };
12+
public enum VCCompilerVersion { vc2003, vc2005, vc2008, vc2010, vc2012, vc2013, vc2015, vc2017, vc2019, vcFuture };
1313

1414
public SourceFile(string fullPath, string projectBasePath, string projectName, string vcCompilerName)
1515
{
@@ -50,6 +50,15 @@ public SourceFile(string fullPath, string projectBasePath, string projectName, s
5050
case 2013:
5151
_compilerVersion = VCCompilerVersion.vc2013;
5252
break;
53+
case 2015:
54+
_compilerVersion = VCCompilerVersion.vc2015;
55+
break;
56+
case 2017:
57+
_compilerVersion = VCCompilerVersion.vc2017;
58+
break;
59+
case 2019:
60+
_compilerVersion = VCCompilerVersion.vc2019;
61+
break;
5362
default:
5463
_compilerVersion = VCCompilerVersion.vcFuture;
5564
break;

0 commit comments

Comments
 (0)