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. The tool is integrated in TwinCAT 3 PLC and can be seen as a supplement to the compiler.
Function overview
Static Analysis implements more than 100 coding rules, some of which can be parameterized and combined to create individual rule sets. The rule sets defined in "PLCopen Coding Guidelines" are taken into account in some rules. 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.
You can also define a naming convention for each possible data type, which is then checked for compliance. In addition, over 20 metrics are available to analyze and characterize the underlying source code. When calculated regularly, the metrics can indicate negative trends and deviations from quality targets. The key figures therefore represent an indicator for assessing software quality. For example, the tabular output contains metrics for the number of statements or the proportion of comments.
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 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).
Advantage
Static Analysis helps to write code that is easier to read and to identify potential sources of error during programming. On the one hand, this increases code quality and, on the other, saves a lot of time when developing applications and troubleshooting.
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 Forbidden symbols 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 key figures (e.g. the number of statements or the percentage 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 the licensed version of Static Analysis can be found in 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). |