coccicheck: make transformation for strbuf_addf(sb, "...") more precise
We can replace strbuf_addf() calls that just add a simple string with calls to strbuf_addstr() to make the intent clearer. We need to be careful if that string contains printf format specifications like %%, though, as a simple replacement would change the output. Add checks to the semantic patch to make sure we only perform the transformation if the second argument is a string constant (possibly translated) that doesn't contain any percent signs. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Oct 3, 2016 at 00:58 UTC
353d84c537485500989b190c5af93ad224264e2c
1 file changed
+26
-3
contrib/coccinelle/strbuf.cocci
+26
-3
@@ -1,8 +1,31 @@
1
+@ strbuf_addf_with_format_only @
2
+expression E;
3
+constant fmt;
4
@@
2
-expression E1, E2;
5
+ strbuf_addf(E,
6
+(
7
+ fmt
8
+|
9
+ _(fmt)
10
+)
11
+ );
12
+
13
+@ script:python @
14
+fmt << strbuf_addf_with_format_only.fmt;
15
@@
4
-- strbuf_addf(E1, E2);
5
-+ strbuf_addstr(E1, E2);
16
+cocci.include_match("%" not in fmt)
17
+
18
+@ extends strbuf_addf_with_format_only @
19
+@@
20
+- strbuf_addf
21
++ strbuf_addstr
22
+ (E,
23
+(
24
+ fmt
25
+|
26
+ _(fmt)
27
+)
28
+ );
29
30
@@
31
expression E1, E2;