Running static code analysis

Starting with TwinCAT Build 4023.103, static code analysis can be launched via the Automation Interface. This allows for integration into CI/CD pipelines regardless of whether the analysis is enabled in the project.

The analysis is called using ITcPlcIECProject3.RunStaticAnalysis. The boolean parameter bCompile controls whether the project is recompiled before the analysis. Errors and warnings appear in the error list and in the output window in Visual Studio. ITcPlcIECProject2.CheckAllObjects is also available; it performs a complete compile-and-check routine and is independent of the project's static analysis configuration.

Requirements

C# sample

The PLC project Tree Item is cast to ITcPlcIECProject3. RunStaticAnalysis(true) compiles the code first. RunStaticAnalysis(false) skips the build and performs only the analysis.

ITcSmTreeItem plcProject = sysManager.LookupTreeItem(@"TIPC^PLC1^PLC1 Project");

// Cast to ITcPlcIECProject3 for RunStaticAnalysis
ITcPlcIECProject3 plcProject3 = (ITcPlcIECProject3)plcProject;

// true = compile first; false = analysis only, no build
plcProject3.RunStaticAnalysis(true);

// Check all objects (compile + check, available via ITcPlcIECProject2)
ITcPlcIECProject2 plcProject2 = (ITcPlcIECProject2)plcProject;
plcProject2.CheckAllObjects();

PowerShell sample

In PowerShell, RunStaticAnalysis is available directly on the Tree Item via late binding, without the need for an explicit cast.

$plcProject = $sysManager.LookupTreeItem("TIPC^PLC1^PLC1 Project")

# Call RunStaticAnalysis directly (late binding)
$plcProject.RunStaticAnalysis($true)
Running static code analysis 1:

The PowerShell sample only shows RunStaticAnalysis. CheckAllObjects from ITcPlcIECProject2 is available via late binding in the same way.