Overview
With the integration of the static code analysis, a further programming tool is available in TwinCAT 3.1 that supports the PLC software development process.
Static code analysis should be regarded as a supplement to the compiler. It facilitates writing clearer code and can help uncover potential sources of error during programming. For example, it can report if a pointer variable has not been checked for nonzero before dereferencing. As a result, the user's attention is drawn to possibly inadvertent and erroneous implementations, so that these program points can be optimized at an early stage.
Static Analysis is integrated into TwinCAT 3 PLC as a programming tool. It checks the source code of a PLC project for deviations from certain coding rules, naming conventions or unauthorized symbols. The rule set defined in the PLCopen Coding Guidelines is used as the basis, complemented by additional checking options.
The Static Analysis can be triggered manually or performed automatically during the code generation. TwinCAT outputs the result of the analysis, i.e. messages regarding deviations from the specifications and rules, in the message window. In the PLC project properties you can define the parameters to be checked in detail. When configuring the rules, you can also define whether a rule violation is to be output as an error or a warning. You can use pragma instructions statements to exclude particular parts of the code from the check. For errors reported by Static Analysis based on precompile information, there is support in the ST Editor for immediate troubleshooting (QuickFix/Precompile).
In addition, you can display selected metrics to assess code quality in a separate view. Static Analysis determines these metrics from your program code. Key parameters are calculated that characterize the various program parts or express the properties of the software. They therefore provide an indication of the software quality. For example, the tabular output contains metrics for the number of statements or the proportion of comments.
Advantage
"Static code analysis" facilitates writing clearer code and can help uncover potential sources of error during programming.
Failure to observe a coding rule generally indicates an implementation weakness; correcting it enables early troubleshooting or error avoidance. The automatic control of the user-specific naming conventions also ensures that the control programs can be developed in a standardized manner with regard to type and variable names. This gives different PLC projects implemented on the basis of the same naming conventions a uniform look and feel, which greatly improves the readability of programs. In addition, the metrics provide an indication of the software quality.
Functionalities
An overview of the functionalities of "TwinCAT 3 PLC Static Analysis" is provided below:
- Static Analysis:
- Function: The Static Analysis checks the source code of a project for deviations from certain coding rules and naming conventions, as well as for forbidden symbols. The result is output in the message window.
- Configuration: The required coding rules, naming conventions and forbidden symbols can be configured in the Rules, Naming conventions and tabs of the PLC project properties.
- Standard metrics:
- Function: Certain metrics are applied to your source code, which express the software properties in the form of indicators (e.g. the number of statements or the proportion of comments). They provide an indication of the software quality. The results are output in the Standard Metrics view.
- Configuration: The required metrics can be configured in the Metrics tab of the PLC project properties.
Alternatively, there is an option to use a license-free version of Static Analysis that provides a very much reduced range of functions. A detailed comparison of the functions of the license-free and licensed versions of Static Analysis can be found in the chapter Installation.
Further information on installation, configuration and execution of the "Static Analysis" can be found on the following pages:
- Installation
- Configuration of the settings, rules, naming conventions, metrics and forbidden symbols
- Command 'Run static analysis'
- Command 'Run static analysis [Check all objects]'
- Command 'View Standard Metrics'
- Command 'View Standard Metrics [Check all objects]'
- Pragmas and attributes
- Examples
- Automation Interface support
![]() | Libraries TwinCAT only analyzes the application code of the current PLC project; the referenced libraries are ignored! If you have opened the library project, however, you can check the elements it contains with the help of the command Command 'Run static analysis [Check all objects]'. |
![]() | Punctual disablement of checks Pragmas and attributes can be used to disable checks for certain parts of the code. |
![]() | Static Analysis via the Automation Interface Static Analysis can be operated via the Automation Interface (see Automation Interface support). |