1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2@@
3/* Avoid running this transformation on the strempty function itself and
4 * on the "make_expression" macro in src/libsystemd/sd-bus/bus-convenience.c.
5 * As Coccinelle's Location object doesn't support macro "detection", use
6 * a pretty horrifying combo of specifying a file and a special "something_else"
7 * position element, which is, apparently, the default value of
8 * "current_element" before it's set (according to the source code), thus
9 * matching any "top level" position, including macros. Let's hope we never
10 * introduce a function called "something_else"...
11 */
12position p : script:python() {
13        not (p[0].current_element == "strempty" or
14                (p[0].file == "src/libsystemd/sd-bus/bus-convenience.c" and
15                        p[0].current_element == "something_else"))
16};
17expression s;
18@@
19(
20- s@p ?: ""
21+ strempty(s)
22|
23- s@p ? s : ""
24+ strempty(s)
25)
26
27@@
28position p : script:python() { p[0].current_element != "strempty" };
29expression s;
30@@
31- if (!s@p)
32-         s = "";
33+ s = strempty(s);
34
35@@
36position p : script:python() { p[0].current_element != "strnull" };
37expression s;
38@@
39(
40- s@p ?: "(null)"
41+ strnull(s)
42|
43- s@p ? s : "(null)"
44+ strnull(s)
45)
46
47@@
48position p : script:python() { p[0].current_element != "strnull" };
49expression s;
50@@
51- if (!s@p)
52-         s = "(null)";
53+ s = strnull(s);
54
55@@
56position p : script:python() { p[0].current_element != "strna" };
57expression s;
58@@
59(
60- s@p ?: "n/a"
61+ strna(s)
62|
63- s@p ? s : "n/a"
64+ strna(s)
65)
66
67@@
68position p : script:python() { p[0].current_element != "strna" };
69expression s;
70@@
71- if (!s@p)
72-         s = "n/a";
73+ s = strna(s);
74