Pragmas and attributes
A pragma and various attributes are available to temporarily disable individual rules or naming conventions for the static analysis, i.e. to exclude certain code lines or program units from the evaluation.
Requirement: The rules or conventions are enabled or defined in the PLC-project properties. See also:
Attributes are inserted in the declaration part of a programming block in order to deactivate certain rules for a complete programming object.
Pragmas are used in the implementation part of a programming block in order to deactivate certain rules for individual code lines. The exception to this is rule SA0164, which can also be deactivated in the declaration part by a pragma.
Rules that are disabled in the project properties cannot be activated by a pragma or attribute. |
Rule SA0004 cannot be disabled by a pragma or an attribute. |
Pragmas in the implementation editor If you want to use a pragma in the implementation editor, this is currently possible in the ST and FBD/LD/IL editors. In FBD/LD/IL the desired pragma must be entered in a label. |
The following section provides an overview and a detailed description of the available pragmas and attributes.
Overview
- Pragma {analysis ...}
- for disabling coding rules in the implementation part
- can be used for individual code lines
- Attribute {attribute 'no-analysis'}
- for excluding programming objects (e.g. POU, GVL, DUT) from the static analysis (coding rules, naming conventions, forbidden symbols)
- can only be used for whole programming objects
- Attribute {attribute 'analysis' := '...'}
- for disabling coding rules in the declaration part
- can be used for individual declarations or for whole programming objects
- Attribute {attribute 'naming' := '...'}
- for disabling naming conventions in the declaration part
- can be used for individual declarations or for whole programming objects
- Attribute {attribute 'nameprefix' := '...'}
- for defining prefixes for instances of a structured data type
- can be used in the declaration part of a structured data type
- Attribute {attribute 'analysis:report-multiple-instance-calls'}
- for specifying that a function block instance should only be called once
- can be used in the declaration part of a function block
Detailed description
Pragma {analysis ...}
You can use the pragma {analysis -/+<rule number>} in the implementation part of a programming block in order to disregard individual coding rules for the following code lines. Coding rules are deactivated by specifying the rule numbers preceded by a minus sign ("-"). For activation they are preceded by a plus sign ("+"). You can specify any number of rules in the pragma with the help of comma separation.
Insertion location:
- Deactivation of rules: In the implementation part of the first code line from which the code analysis is disabled with {analysis - ...}.
- Activation of rules: After the last line of the deactivation with {analysis + ...}.
- For rule SA0164, the pragma can also be inserted in the declaration part before a comment.
Syntax:
- Deactivation of rules:
- one rule: {analysis -<rule number>}
- several rules: {analysis -<rule number>, -<further rule number>, -<further rule number>}
- Activation of rules:
- one rule: {analysis +<rule number>}
- several rules: {analysis +<rule number>, +<further rule number>, +<further rule number>}
Samples:
Rule 24 (only typed literals permitted) is to be disabled for one line (i.e. in these lines it is not necessary to write "nTest := DINT#99") and then enabled again:
{analysis -24}
nTest := 99;
{analysis +24}
nVar := INT#2;
Specification of several rules:
{analysis -10, -24, -18}
Attribute {attribute 'no-analysis'}
You can use the {attribute 'no-analysis'} attribute to exclude an entire programming object from the static analysis check. For this programming object no checks are carried out for the coding rules, naming conventions and forbidden symbols.
Insertion location:
above the declaration of a programming object
Syntax:
{attribute 'no-analysis'}
Samples:
{attribute 'qualified_only'}
{attribute 'no-analysis'}
VAR_GLOBAL
…
END_VAR
{attribute 'no-analysis'}
PROGRAM MAIN
VAR
…
END_VAR
Attribute {attribute 'analysis' := '...'}
You can use the attribute {attribute 'analysis' := '-<rule number>'} to switch off certain rules for individual declarations or for a complete programming object. The code rule is deactivated by specifying the rule number(s) with a minus sign in front. You can specify any number of rules in the attribute.
Insertion location:
above the declaration of a programming object or in the line above a variable declaration
Syntax:
- one rule: {attribute 'analysis' := '-<rule number>'}
- several rules: {attribute 'analysis' := '-<rule number>, -<further rule number>, -<further rule number>'}
Samples:
Rule 33 (unused variables) is to be disabled for all variables of the structure.
{attribute 'analysis' := '-33'}
TYPE ST_Sample :
STRUCT
bMember : BOOL;
nMember : INT;
END_STRUCT
END_TYPE
Checking of rules 28 (overlapping memory areas) and 33 (unused variables) is to be disabled for variable nVar1.
PROGRAM MAIN
VAR
{attribute 'analysis' := '-28, -33'}
nVar1 AT%QB21 : INT;
nVar2 AT%QD5 : DWORD;
nVar3 AT%QB41 : INT;
nVar4 AT%QD10 : DWORD;
END_VAR
Rule 6 (concurrent access) is to be disabled for a global variable, so that no error message is generated if write access to the variable occurs from more than one task.
VAR_GLOBAL
{attribute 'analysis' := '-6'}
nVar : INT;
bVar : BOOL;
END_VAR
Attribute {attribute 'naming' := '...'}
The attribute {attribute 'naming' := '...'} can be used in the declaration part in order to exclude individual declaration lines from the check for compliance with the current naming conventions.
Insertion location:
- Deactivation: in the declaration part above the relevant lines
- Activation: after the last line of the deactivation
Syntax:
{attribute 'naming' := '<off|on|omit>'}
- off, on: the check is disabled for all rows between the "off" and "on" statements
- omit: only the next row is excluded from the check
Sample:
It is assumed that the following naming conventions are defined:
- The identifiers of INT variables must have a prefix "n" (naming convention NC0014), e.g. "nVar1".
- Function block names must start with "FB_" (naming convention NC0103), e.g. "FB_Sample".
For the code shown below, the static analysis then only issues messages for the following variables: cVar, aVariable, bVariable.
PROGRAM MAIN
VAR
{attribute 'naming' := 'off'}
aVar : INT;
bVar : INT;
{attribute 'naming' := 'on'}
cVar : INT;
{attribute 'naming' := 'omit'}
dVar : INT;
fb1 : SampleFB;
fb2 : FB;
END_VAR
{attribute 'naming' := 'omit'}
FUNCTION_BLOCK SampleFB
…
{attribute 'naming' := 'off'}
FUNCTION_BLOCK FB
VAR
{attribute 'naming' := 'on'}
aVariable : INT;
bVariable : INT;
…
Attribute {attribute 'nameprefix' := '...'}
The attribute {attribute 'nameprefix' := '...'} defines a prefix for variables of a structured data type. A naming convention then applies to the effect that identifiers for instances of this type must have this prefix.
Insertion location:
above the declaration of a structured data type
Syntax:
{attribute ‘nameprefix’ := '<prefix>'}
Example:
The following naming conventions are defined in the category Naming conventions in the PLC project properties:
- Variables of the type of a structure (NC0032): st
- Structures (NC0151): ST_
Conversely, variables of the type "ST_Point" should not begin with the prefix "st", but with the prefix "pt".
In the following sample, the statistic analysis will output a message for "a1" and "st1" of the type "ST_Point" because the variable names do not begin with "pt". For variables of the type "ST_Test", conversely, the prefix "st" is expected.
TYPE ST_Test :
STRUCT
…
END_STRUCT
END_TYPE
{attribute 'nameprefix' := 'pt'}
TYPE ST_Point :
STRUCT
x : INT;
y : INT;
END_STRUCT
END_TYPE
PROGRAM MAIN
VAR
a1 : ST_Point; // => Invalid variable name 'a1'. Expect prefix 'pt'
st1 : ST_Point; // => Invalid variable name 'st1'. Expect prefix 'pt'
pt1 : ST_Point;
a2 : ST_Test; // => Invalid variable name 'a2'. Expect prefix 'st'
st2 : ST_Test;
pt2 : ST_Test; // => Invalid variable name 'st2'. Expect prefix 'st'
END_VAR
Attribute {attribute 'analysis:report-multiple-instance-calls'}
The attribute {attribute 'analysis:report-multiple-instance-calls'} identifies a function block for a check for rule 105: Only function blocks with this attribute are checked to ascertain whether the instances of the function block are called several times. The attribute has no effect if rule 105 is disabled in the Rules category in the PLC project properties.
Insertion location:
above the declaration of a function block
Syntax:
{attribute 'analysis:report-multiple-instance-calls'}
Sample:
In the following sample the static analysis will issue an error for fb2, since the instance is called more than once.
Function block FB_Test1 without attribute:
FUNCTION_BLOCK FB_Test1
…
Function block FB_Test2 with attribute:
{attribute 'analysis:report-multiple-instance-calls'}
FUNCTION_BLOCK FB_Test2
…
Program MAIN:
PROGRAM MAIN
VAR
fb1 : FB_Test1;
fb2 : FB_Test2;
END_VAR
fb1();
fb1();
fb2(); // => SA0105: Instance 'fb2' called more than once
fb2(); // => SA0105: Instance 'fb2' called more than once