Code Inspector – How to create a new check
Introduction
I always felt excited when I read about new ABAP related tools. One of the first such tool I fell in love was the Code Inspector. Around in 2001-2002 we were on 46C, and in one for the Code inspector presentation I saw a note about, that there is a down port for 46C with some limitation. I got green light from my manager to import it to our development system and I could play around with this tool … and fun begun.
I found very useful to perform static checks on the code. From that point I always used Code Inspector instead of the Extended syntax check.
About the Code inspector framework
The Code Inspector is a tool for the static analysis of ABAP programs and other SAP repository objects. Its available as of basis release 6.10 as part of SAP standard delivery and a down port is available for 46C with some limitations.
Code inspector is a framework, where all the categories and checks are based on ABAP classes. SAP continuously delivers new build-in checks for new tools (e.g.: ABAP Unit) and new objects (e.g.: BSP Applications).
Its also possible to add customer-built checks within this framework.
Main steps for creating a new check
- Create new category
- Create new check
- Create documentation
- Activate new category/check
In this example we will build a very simple check for searching string SDN. This example is based on SAP standard check CL_CI_TEST_FREE_SEARCH.
I believe standard checks are covers most of the customer needs, however new checks can be useful for checking customer specific programming conventions.
The goal of this example is to show how the code inspector checks are built.
Creating a new category
Copy CL_CI_CATEGORY_TEMPLATE to ZCL_CI_CATEGORY_SDN.
Select the package in the popup (e.g.: $TMP)
Change the CONSTRUCTOR method to the following:
Create the text elements and activate your class.
This method sets the attributes of this category in the code inspector check tree.
Creating a new check
Copy CL_CI_TEST_SCAN_TEMPLATE to ZCL_CI_SCAN_SDN.
Select the package in the popup (e.g.: $TMP)
Add the following attributes to the class:
C_MY_NAME Constant Private Type SEOCLSNAME ‘ZCL_CI_SCAN_SDN’
C_PROGRAM Constant Private Type SCI_ERRC Code Inspector: ID for Message Code ‘PROG’
C_SDN Constant Private Type CHAR03 Three-digit character field for Idocs ‘SDN’
Method constructor sets the attributes of this check in the code inspector check tree.
Method run to search for string SDN and pass the parameters to method INFORM
Method get_message_text is to generate explanation text in the check tree.
Documentation
Via transaction SE61 you can add documentation to the categories/checks.
Create the follwing documentations:
ZCL_CI_CATEGORY_SDN/000 New Category for SDN
ZCL_CI_SCAN_SDN/000 Search for SDN
Activate new categories and checks
Start transaction SCI. Navigate to Goto-> Test Administration in the menu. Set the flag for the new category and check in order to make them available in the framework.
Testing our new check
Create a simple test program containing string SDN.
Start transaction SCII, where you can test the code without saving the inspection.
In the Object selection block set Single, select Program and enter program name.
In the Check variant block select Temporary Definition and make sure that you flag New Category for SDN category and Seach for SDN check. Remember, these texts were defined in the constructors.
Press execute and when you got the message The inspection was carried out successfully go to the results.
Remarks
· There are some changes between releases in the mentioned menu path and also in the available techniques.
· This example was created on a SAP R/3 Enterprise 4.70×200 /WAS 6.20/ system.
· Although suppress message is possible via method INFORM parameter p_suppress, somehow I couldnt manage to make it work.
· If you import the package in 46C, you can expect some extra work during the next upgrade SPAU, however all of these object can be reset to SAP standard
Reference documents
OSS note 543359 Code Inspector for SAP R/3 Release 4.6C
Improving the Quality of Your ABAP Code Using the Code Inspector
Evaluating the Quality of Your ABAP Programs and Other Repository Objects with the Code Inspector
http://abap-inotai.googlecode.com/svn/trunk/
Peter
Yours is one of the few and best help that I could manage to locate on Code Inspector. But I have a few doubts regarding the code in the Run() method. I was unable to understand the purpose of the following statement :
IF ref_scan IS INITIAL.
CHECK get( ) = 'X'.
ENDIF.
What does it aims to do?
Similarly the satements,
CHECK statement_wa-from <= statement_wa-to.
CHECK statement_wa-type NA 'EMPSDR'.
are trying to achieve what?
It would be very helpful in gaining more clarity if you could explain this.
The code presented here is based on CL_CI_TEST_FREE_SEARCH->RUN.
The best is to put a break-point in method RUN to see what is happening there.
1)
IF ref_scan IS INITIAL.
CHECK get( ) = 'X'.
ENDIF.
If there is no scan is available, so REF_SCAN is initial / which is an istance of CL_CI_SCAN / then it will call the GET( ) method /from CL_CI_TEST_SCAN/ and call the constructor to create an instance for REF_SCAN. If this went fine, the return parameter P_RESULT is set to 'X'.
So, in nutshell it does two steps in one step, create object for REF_SCAN and check if it's went fine.
2)
CHECK statement_wa-from <= statement_wa-to.
This check might be removed, it's just checking if the statement start line is lower or equal to the statement end line. Normally it should be the case.
3)
CHECK statement_wa-type NA 'EMPSDR'.
I can't find this line in my code. However I think it's checking the statement type, which is based on data element STMNT_TYPE, which can be EXEC SQL, INCLUDE, macro, ..., rest.
In CL_CI_TEST_SCAN_NESTED->GET_STMNT_NAME you can find some info about type:
case P_STMNT_TYPE.
when 'D'. p_stmnt_name = 'DO/ENDDO'.
when 'W'. p_stmnt_name = 'WHILE/ENDWHILE'.
when 'L'. p_stmnt_name = 'LOOP/ENDLOOP'.
when 'V'. p_stmnt_name = 'PROVIDE/ENDPROVIDE'.
when 'S'. p_stmnt_name = 'SELECT/ENDSELECT'.
endcase.
Hope it helps.
Best regards,
Peter
I really thank you for your prompt reply and help. Your answer has realy made the thing clear to me which was essential for moving forward.
You have really got a rich experience in ABAP. Can I benefit from the same by asking you queries in the future (as I am still growing in this field ) and if yes, where shall i write to you?
Regards,
Shikha.
I really thank you for your prompt reply and help. Your answer has realy made the thing clear to me which was essential for moving forward.
You have really got a rich experience in ABAP. Can I benefit from the same by asking you queries in the future (as I am still growing in this field ) and if yes, where shall i write to you?
Regards,
Shikha.
In the CONSTRUCTOR of CL_CI_SCAN, the SCAN ABAP-SOURCE statement is used to get the statamenets.
If you check the F1-help, you can see that it's for "This statement is for internal use only. "
However there is some info about the type:
TYPE
Type of statement with the following possible values:
E (Native SQL statement between EXEC SQL and ENDEXEC)
I (INCLUDE prog )
J (INCLUDE prog, prog does not exist, can occur only in connection with the addition WITH INCLUDES )
T ( TYPE-POOLS pool)
V (TYPE-POOLS pool, pool nicht vorhanden)
R (Call a macro from table TRMAC)
D (Call a macro internally defined with DEFINE)
M (Macro definition between DEFINE and END-OF-DEFINITION)
C (COMPUTE statement, sometimes without COMPUTE as first token)
A (Method call in short form)
K (Other ABAP/4 key word)
N (Blank statement)
P (Comment between statements)
S (Comment within statements)
U (Unknown, non-blank statement)
Peter
I really thank you for your prompt reply and help. Your answer has realy made the thing clear to me which was essential for moving forward.
You have really got a rich experience in ABAP. Can I benefit from the same by asking you queries in the future (as I am still growing in this field ) and if yes, where shall i write to you?
Regards,
Shikha.
I'm happy that my answer helped you.
To be honest I wouldn't like to share my email because of SPAM reasons. However you can post your Code Inspector questions here, and for other topics I'd suggest to post them in the SDN ABAP forums. I'll check them regularly, when I have some time, which is unfortunately it's not the case recently:(
Best regards,
Peter
Currently, I am studying a code which has imbibed a lot of code and functionality from 'Code Inspector' and modified it further to make an indigeneous Code Inspector which suits the requirement's purpose. If I am stuck anywhere, I will definitely show up again with another query.
Thanks and Regards,
Shikha
I really thank you for your prompt reply and help. Your answer has realy made the thing clear to me which was essential for moving forward.
You have really got a rich experience in ABAP. Can I benefit from the same by asking you queries in the future (as I am still growing in this field ) and if yes, where shall i write to you?
Regards,
Shikha.
I really thank you for your prompt reply and help. Your answer has realy made the thing clear to me which was essential for moving forward.
You have really got a rich experience in ABAP. Can I benefit from the same by asking you queries in the future (as I am still growing in this field ) and if yes, where shall i write to you?
Regards,
Shikha.
I really thank you for your prompt reply and help. Your answer has realy made the thing clear to me which was essential for moving forward.
You have really got a rich experience in ABAP. Can I benefit from the same by asking you queries in the future (as I am still growing in this field ) and if yes, where shall i write to you?
Regards,
Shikha.
I like your introduction to creating new SCI checks. Do you know if there is any SAP or publicly available site where additional SCI checks can be made available?
In particular, I was thinking about developing a check which determines SAP4.6C compatibility in a SAP4.7 coding and I wonder whether such a check is already available.
Best regards,
Björn
Thanks for your feedback.
Sorry for the late answer, it seems something wrong with the notification as I didn't get any notification about your comment.
I'm not aware of any site for SCI checks. Any custom made can be shared via SAPlinkm but I didn't see any of them. SAP delivers new checks via new releases or basis support packages.
I don't think there is standard SCI check for 4.6C-4.70 compatibility. I think the best is to run the unicode check report in 46C, however I don't have the report name anymore as we don't have 46C system. I'll check my notes, hopefully I can find it back.
Best regards,
Peter
Peter
PS: It seems there is a new version of this program, which is called RSUNISCAN_FINAL.
thanks for the hint with the report.
A colleague of mine is now working on this compatibility check, which will actually run on NetWeaver to check for downwards compatibility. When he's finished, I'll see whether my company releases the code so we can send it to SDN as a contribution (even though there is the remote possiblity that no-one would be interested in such a check 😉 ).
Björn
P.S.: It's really sad that my name doesn't get displayed correctly :,(
Thanks for the info.
It seems unicode compatibility is not only an issue for upgrade from 46C, but for your name 🙂 Maybe if you try to enter with a different encoding it might work.
About the check, I'd be very curious. As it class based, the easiest would be to share it via SAPlink.
Best regards,
Peter
code inspector (SCI) with the Transport Organizor (SE01). Is it possible to automatically invoke SCI when someone tries to release a transport?
Thanks.
Soyab
As of release 6.40 SCI is integrated to the Transport organizer.
In lower releases you might use Badi CTS_REQUEST_CHECK method CHECK_BEFORE_RELEASE to trigger a code inspector check.
Best regards,
Peter
CTS_REQUEST_CHECK is not modifiable. I found another one that we use that is also a CHECK_BEFORE_RELEASE and I'm trying to do a CALL TRANSACTION on SCI.
In BAdI (ZCL_IM_VSD_TP_CREATION_CHK method
IF_EX_CTS_REQUEST_CHECK~CHECK_BEFORE_RELEASE) I call a function module.
The function module builds the BDC Table and performs a CALL TRANSACTION SCI (code inspector). This call transaction correctly processes screen
0100 to create a new inspection, screen 0200 is correctly populated and
the OKCODE is set to '=RUN' to execute the inspection. However, what
actually happens is that the information is SAVED and the execution is
NOT performed. If I go and execute SCI using the inspection name it
shows it as unexecuted and when I select 'RUN' it executes it and the
inspection results are created. My question is how do I get SCI to work
correctly in background via a call transaction. I have tried the OKCODE
= SAVE and then followed by =RUN it still gives me the same results. In
order for it to work I have to manually execute it in SCI using the
stored inspection.
I debugged the process and the background processing 'exits' after it
encounters the first 'commit work'. In foreground it continues after
the 'commit work'.
Is there a work around?
Any help would be extremely appreciated.
Thanks.
Soyab
>CTS_REQUEST_CHECK is not modifiable.
It's normal, it contains only the definition, you have to do the implementation.
I'd suggest to try transaction SCID, which calls with the default variant. Something like this:
set parameter id 'SCI_OBJ_TYPE' field 'PROG'.
set parameter id 'SCI_OBJ_NAME' field l_object_name.
call transaction 'SCID'.
Best regards,
Peter
Thanks for your advice.
I have another question.
I'm trying the 'Security checks' -> 'Check SY-SUBRC handling' and checking 'CALL FUNCTION'.
I'm trying to check a program that does:
CALL FUNCTION 'ARCHIVE_SAVE_OBJECT'
EXPORTING
archive_handle = handle.
On execution it does NOT flag it as an error/warning that the SY-SUBRC check is missing.
Is this a bug?
However, when I check 'changing DB Access' it correcly flags as an error the following condition is NO SY-SUBRC is checked:
UPDATE ZVBAKX etc etc
Thanks.
Soyab
I tried the same in our system (SAPKB62061) and got the same result. For me it looks like a bug. Checking SY-SUBRC makes no sense if there is no exception in the Function module, but in this case there is, so there should be an error.
I checked OSS Notes, but couldn't find any relevant one:(
Best regards,
Peter
Please check OSS Note 1296076 - Code Inspector: Improved check of SY-SUBRC handling.
Best regards,
Peter
thanks for this information / new check. I want to create a new check / div by zero. Can you help me ?
Thanks
Dorothée
I don't think Code Inspector is the tool for such checks. It's for static checks, and division by zero needs a dynamic check.
Peter
You're right...
Thanks for your feedback.
Dorothée
I have imported the code inspector into 46C.
Is it possible to include the naming conventions check in 46C versions.
Ravi
I have no access anymore for a 46C system with CI to check this, but I think naming convention check should be available as a standard check. You can check OSS note 543359 for the 46C limitations.
If the standard naming convention check doesn't meet with your requirements, you can just create a new check based on the standard one.
Best regards,
Peter
In Limitations it is not given. But i imported into 46C, in SCI Transaction i Can not see the Programing naming conventions check. Other checks are coming in SCI Tcode. We can including programming naming conventions check with extra programming.
Ravi
Maybe you can check in the SCI administration if CL_CI_CATEGORY_CONVENTIONS/CL_CI_TEST_ABAP_NAMING are available and the flag is set for them.
Peter
In SCI Administration the above two classes are not available and some of other classes are available.
1. Is it possible to add above mentioned classes to this(Custom developed).
2. If it is available in SCI administration is it possible to check the check boxes.
Please let me know. This is very crucial to us to develop in 46C.
Thanks in Advance.
Ravi
You can always active the available classes. See the chapter "Activate new categories and checks" in this blog.
If you need these checks in 46C, maybe it's worth to downport them manually from a higher release. For example you can get them from the Netweaver Sneak Preview for ABAP system, which can be downloaded from SDN and copy/adopt from there.
Best regards,
Peter
interesting article , do you know some FunctionModule how i can analy de sourcecode with a "check variante" likes the FM "EXTENDED_PROGRAM_CHECK" ?
I'm not sure if I understand your question correctly. Code Inspector works with classes and not Function modules to analyze the source code.
The mentioned FM "EXTENDED_PROGRAM_CHECK" is also encapsulated into class CL_CI_TEST_EXTENDED_CHECK and integrated to the Code inspector framework.
You have to call transaction SCI or SCII to analyze the code, you cannot really call separate FM to get the same result.
Best regards,
Peter
Hy,Dominik
I didn't see this error.
You can give a try to upload them via SAPLink from here and see if it works:
http://abap-inotai.googlecode.com/svn/trunk/
Best regards,
Peter
sorry, I simple forgot to define a test symbol for English for "My Category"(000).
So you can delete this post.
regards
Dominik
I cannot delete posts. Anyway it could be useful for others if they have the same problem.
I'm happy that it works.
Best regards
Peter
Thank You for such a nice article.
Right now, I need to create a check concerned with "Loop at" statement.
I am bit unclear about the statement table, token table etc, which is the most important part I need to understand to write a check.
Is there any document that can explain these things in detail?
Regards,
Raghotham S
I'm not aware of any documentation.
You can always create a test program, where you have the statement, example you want to inspect. Then you can start to build your check-class and debug it, what information is available etc.
It also depends what exactly you want to do on "LOOP AT", but you can have a look the following classes:
CL_CI_TEST_IMUD_NESTED Changing Database Accesses in Loops
CL_CI_TEST_SCAN_NESTED Nested Loops
CL_CI_TEST_SELECT_NESTED SELECTs in Loops
Best regards,
Peter
Thank You very much for the info.
It was of great help.
I shall debug and find out things 🙂
Regards,
Raghotham S
So my question is how can i change the status of the error message. With your tutorial in the result-table of the code inspector all the messages have the type "information" but i want "error" the red one. What parameters should I change that it works.
Just use for the inform( ) method call
p_kind = c_error.
Thank you very much.
c_error = error
c_note = information
c_warning = warning ???
Yes, exactly:
Message type (C_NOTE = information, C_WARNING = warning, C_ERROR = error message)
HI Peter,
i programmed my frist own check for the SCI. I show the result with the display_as_tree method. But now i have the problem that i cant jump by double click to postion in the scoure code.
Is there a special parameter to set, that this function is usable??? The result tree sad when i double click "The object " does not exist". I think the three cant find the object but why????
Thanks
Did you pass the p_position, p_line and p_column for the INFORM method? They are required for that.
Try something like this:
g_include = get_include( ).
g_line = get_line_abs( l_tokennr ).
g_column = get_column_abs( l_tokennr ).
CALL METHOD inform( p_sub_obj_type = c_type_include
p_sub_obj_name = g_include
p_position = g_position
p_line = g_line
p_column = g_column
p_errcnt = g_errcnt
p_kind = c_error
p_test = con_my_name
p_code = '0023' ).
Hello Peter,
thanks for your help. It worked awesome. I wrote in the last week 5 exams for the SCI. Now i had another problem. After all my own checks i used the method display as a tree to get the nice SCI overview. So my question is, is it possible to create a onw button on the result display of the display as a tree method. I haven't found any BADI.
Hi Gero,
I'm happy that you managed it in the end 🙂
Unfortunately I'm not aware of any possibility to add custom button to the tree. I'm afraid since the tree columns are added by the framework and there is possibility to only show documentation and attributes, probable there is no such possibility.
Peter
hi Peter,
it's a wonderful job and it help me so much.
i have a question, would you tell me, what is include program in SCI. i check the source code in CL_CI_SCAN. it seems the method constructor make some table for the to be checked code. the table is simple Token and Statement table. i want to get the local data and local type of class. what shall i do to get this. a deeper level of code analyse is important for me.
pls help me.
Thanks
Yinxiao
Hi Peter,
This blog helped me a lot in creating a new check in CI. I have created a new check in ECC 6.0 . When I run the code inspector for the program through SE38 I cannot find these new checks although I have enabled these in SCI administration. How can we make these checks available when we run the code inspector. Can you please suggest me.
Thanks in Advance.
Can you see them from SCII or SCI transaction?
I never had this problem, when they are activated and the CONSTRUCTORs are correctly done for check class and category, they should be there. Maybe it's also worth to check OSS notes, maybe there is also something in SAP standard.
Peter
Hi Peter,
Thanks for your quick reply.
I can see them in SCII when I check (the checkbox of the new category) and execute. But through SE38 execution I do not find this .
SE38 (and SE24, SE80, SE37, etc) using the DEFAULT global check variant and it doesn't contain your checks. You either modify DEFAULT (and adjust it after every upgrade), or you create your own variant and use that one from SCI/SCII transaction.
Amazing, Thanks for sharing this info.
Hi Peter,
Can we create new checks in ATC(ABAP test cockpit) too?
Hi Sri R,
I have to admit I never tried, since I work with an older SAP release, where ATC is not available.
However I believe if you use a code inspector variant, which includes your checks, it should also work.
Peter
Just checked in a 7.40/ERP Ehp7 system. If you go to menu 'Check -> ABAP Test Cockpit with ...' you can select the code inspector variant under Options tab. If you have a Z- variant prepared, which includes your checks, then it will be also executed.
Peter
Appreciate the Blog. As far as suppression via pseudo-comment is concerned. You must populate the table attribute SCIMESSAGES with your test information (code, type, etc.). One of the fields is PCOM for your pseudo-comment. I have a maintenance table that is configured and used to build this in my Constructor.
Next, when you call the INFORM method, the parameter P_POSITION needs to have the index/position of the statement you have identified from REF_SCAN->STATEMENTS.
In case I missed it above, using the index of the REF_SCAN->TOKENS, you find the statement by locating it in using FROM <= TOKEN_INDEX AND TO >= TOKEN_INDEX.
This index or position is what you need. 🙂
This will allow you to successfully use pseudo-comments with your new checks.
Thanks again for nice write up.
Hi Dustin,
Thanks for this additional info. 🙂
Peter
Hi Peter,
Thanks for the tutorial. I managed to implement some custom checks within SCI. One thing I wasn't able to find out is this: The messages of SLIN-Checks have forward-navigation enabled; i.e. when I click on a message such as " The exception ... is neither caught nor is it declared in the RAISING clause of ..." I jump directly into the method causing the error. Is it possible to enable this behaviour for custom checks, too?
Kind regards
Harald
Hi Harald,
It's a long time 🙂 , but as far as I remember in the INFORM( ) method you should pass the following parameters:
P_POSITION With Source: Number of Statement
P_LINE Number of source line where token appears (>= 1)
P_COLUMN Column position of token (>= 0)
If they are passed, the mentioned forward navigation should work.
Cheers,
Peter
Hi Peter,
Thank you for the quick response. I'm sorry to say that it doesn't work. I'll investigate further and post the solution if I can make it work.
Kind regards
Harald
Hi Harald,
Strange. Maybe it's worth to check OSS notes also.
For me the following approach works fine:
g_include = get_include( ).
g_line = get_line_abs( l_tokennr ).
g_column = get_column_abs( l_tokennr ).
g_errcnt = g_errcnt + 1.
* Inform --> Add error to output table
inform( p_sub_obj_type = c_type_include
p_sub_obj_name = g_include
p_position = g_position
p_line = g_line
p_column = g_column
p_errcnt = g_errcnt
p_kind = c_error
p_test = con_my_name
p_code = '0001' ).
Also in the message the Row and Column is mentioned:
I hope it helps.
Best regards,
Peter
Hi Peter,
It seems that forward navigation works only for subclasses of CL_CI_TEST_PROGRAM. I was using CL_CI_TEST_ROOT_TEMPLATE before and changing the inheritance relationship made it work. Thank you for your time and effort.
Best regards
Harald
Hi Harald,
Great, that you managed to find the reason.
Thanks for sharing it here!
Peter
Nice document. Thank You.
Great effort Peter..
Its very clear.!!!
Regards,
Siva R
Hi Peter,
Is there any way to get data type of the variable ij the code using above mentioned classes.
Please confirm.
Thanks and Regards,
Jitendra
Hi Jitenda,
To get the the variable names: I would check the CL_CI_TEST_ABAP_NAMING and CL_CI_TEST_ABAP_NAMING_NEW classes.
To get the type I think you can use the RTTI classes (Runtime Type Services (RTTS) - ABAP Development - SCN Wiki).
Best regards
Peter
Thanks Peter !!!
Actually I need the data type of variable which appears when we double click on the variable in ABAP code.
Once you double click on a variable, control goes to that particular variable declaration.
Can we get that type.
Please confirm....
Hi Jitenda,
I have to admit, it's a loooong time ago when I worked with custom code inspector checks.
However generally speaking I see no reason why RTTI would not work in your case. It should return the type of the variables.
Best regards,
Peter
Hi Peter,
nice work, I used it in my code and it helped a lot!
Just an update:
in recent ABAP version, custom check class could be created using inheritance from superclass CL_CI_TEST_ABAP_COMP_PROCS.
This gives very easy and quick custom check implementation.
In the class you could use attributes:
me->proc_defs (procedures in the code)
loop at procedures, and you will have proc_defs->stmts (abap statements).
loop at statements, and you will have stmts->tokens (parts of abap statements).
and attribute: infos (used to create messages)
Methods: just redefine method run, create new constructor method,
and in the method "run" call method inform() to add new message to the code inspector check.
Hi Denis,
Thanks for the info, I didn't know about CL_CI_TEST_ABAP_COMP_PROCS.
Best regards
Peter