What are pragmas and pseudo comments in ABAP?
When using the ABAP Test Cockpit (ATC) for statically and dynamically checking the quality of your ABAP code and related repository objects, some of the warnings and errors identified by the ATC aren’t relevant in your specific scenario. Leaving errors and warnings unaddressed might cause confusion at some future point-in-time when you or another developer needs to change the code. In order to reduce the confusion, ABAP pragmas and pseudo comments can be used to blend out irrelevant warnings and errors. Let us take a detailed look at what pseudo comments and pragmas are and how to use them.
Pseudo comments are program directives that influence check and test procedures. Pseudo comments have mostly become obsolete and have been replaced by pragmas or real additions.ABAP Keyword Documentation
Even though mostly obsolete, the pseudo comments are still relevant for some scenarios in the code inspector.
A pseudo comment looks like this:
METHOD get_sources. SELECT source, textlong INTO TABLE @rt_source FROM tb006 WHERE spras = @sy-langu. "#EC CI_SUBRC ENDMETHOD.
The pseudo comment
"#EC CI_SUBRC is used to hide a message telling us that
sy-subrc should be checked after a
SELECT statement. In the example above the desired behavior is that an empty table should be returned if the
SELECT statement is not successful, so there is no need to check the value of
Sometimes a line of code generates several warnings or errors which you may want to hide. Using pseudo comments, it is only possible to specify one pseudo comment for each program line. When multiple pseudo comments are required, the statement must be divided up to span multiple lines.
Note that the pseudo comment is placed after the ending dot of a statement.
Pragmas are program directives that can be used to hide warnings from various check tools.ABAP Keyword Documentation
Pragmas can be used to hide warnings from the ABAP compiler syntax check as well as from the extended program check.
The use of a pragma looks like this:
MESSAGE e001(ad) INTO DATA(lv_message) ##NEEDED.
##NEEDED tells the check tools that even though the variable
lv_message isn’t used for further processing, the variable is still needed. In this specific scenario, it is needed since we want to be able to perform a where-used search for the message
AD 001 from transaction SE91.
Not that a pragma is placed before the dot or comma ending the statement to which the pragma is applied. If multiple pragmas are needed for the same statement, they can be placed one after the other separated by space. It is also possible to place pragmas in front of a colon (:) of a chained statement. This applies the pragma to the whole chained statement:
DATA ##NEEDED: gt_messages TYPE bapiret2_t, gt_sel_data TYPE ty_sel_data_tt.
Personally I think that this is hard to read, and I’d much rather use the following format:
DATA: gt_messages TYPE bapiret2_t ##NEEDED, gt_sel_data TYPE ty_sel_data_tt ##NEEDED.
Some pragmas can be used with parameters. The example mentioned in the ABAP Keyword Documentation is
##SHADOW, which can be used with an optional parameter. It looks like this:
I’ve never come across this in practice, so I don’t have much experience to share. Feel free to give some input on this by writing a comment on the blog post.
The mapping between obsolete pseudo comments and pragmas
As already mentioned, most pseudo comments are obsolete and have been replaced by pragmas. SAP provides a program which can be run to find the mapping between obsolete pseudo comments and pragmas. The name of the program is
ABAP_SLIN_PRAGMAS. The main table used by the program is
SLIN_DESC, if you prefer looking up the pseudo comments and programs directly in a table viewer like SE16.
As an example, the pseudo comment
"#EC AUTFLD_MIS has been superseded by the pragma
TRPRAGMAT contain all pragmas.
I hope that this brief overview of pragmas and pseudo comments was useful. Please leave a comment if you think that I missed mentioning something crucial. Happy coding!
This blog post first appeared on the Developer Voyage blog at https://www.developervoyage.com/2019/09/03/what-are-pragmas-and-pseudo-comments-in-abap.html