coccinelle / coccinelle

Source code of the Coccinelle project (mirror of the main Coccinelle repository located at Inria)
http://coccinelle.lip6.fr/
GNU General Public License v2.0
604 stars 101 forks source link

Support a source code search approach for finding duplicate code from two if branches #316

Open elfring opened 11 months ago

elfring commented 11 months ago

The analysis tool “Cppcheck 2.10.3-3.2” pointed a source code place out for further development considerations: (style) Condition 'tint==NULL' is always true [knownConditionTrueFalse]

:eyes: See also a corresponding change suggestion.

I tried another SmPL script variant out.

@replacement@
statement s;
@@
-if (tint == NULL)
-{
- ...
- cs->tint = GetColor(black);
- s
-}
+cs->tint = GetColor(tint ? tint : black);
+s

The following test result demonstrates a possible code transformation:

Markus_Elfring@Sonne:…/Projekte/fvwm3> spatch test_tint_determination2.cocci lokal/fvwm/colorset.c
…
@@ -962,14 +962,9 @@ void parse_colorset(int n, char *line)
                                have_pixels_changed = True;
                        }
                }
-               else if (tint == NULL)
-               {
-                       /* default */
-                       Pixel old_tint = cs->tint;
-                       PictureFreeColors(dpy, Pcmap, &cs->tint, 1, 0, True);
-                       cs->tint = GetColor(black);
-                       if (old_tint != cs->tint)
-                       {
+               else {
+                       cs->tint = GetColor(tint ? tint : black);
+                       if (old_tint != cs->tint) {
                                have_pixels_changed = True;
                        }
                }

:crystal_ball: How will the chances evolve to improve components of the software combination “Coccinelle 1.1.1-00478-g0afff7fa” for more desirable source code search (and change) results?

elfring commented 11 months ago

How do you think about to disable isomorphisms like the following for another analysis approach (so that extra case distinctions can be avoided from the application of SmPL disjunctions)? :thinking:

elfring commented 11 months ago

Why can known source code places also not be found with the following SmPL script variant?

@display disable is_null, isnt_null1, neg_if, ne_if, drop_else, commeq, commneq, ptr_to_array@
statement s;
@@
*if (tint != NULL)
*{
* ...
* PictureFreeColors(...);
* cs->tint = GetColor(tint);
* s
*}
*else if (tint == NULL)
*{
* ...
* PictureFreeColors(...);
* cs->tint = GetColor(black);
* s
*}

:crystal_ball: How will observed difficulties be resolved for the safe application of statement metavariables?

elfring commented 11 months ago