diff options
| author | Julia Lawall <Julia.Lawall@lip6.fr> | 2017-10-08 15:18:41 -0400 |
|---|---|---|
| committer | Masahiro Yamada <yamada.masahiro@socionext.com> | 2017-11-14 09:19:03 -0500 |
| commit | a44b86645a4a173a45e57d127ac037e88750ea6a (patch) | |
| tree | 1e8cf58defd03f11393ab8f28b58ac6db55ebbef /scripts | |
| parent | 1b18d05c7c204a59e0ac66cbfa813a7173c4426e (diff) | |
coccinelle: api: detect identical chip data arrays
This semantic patch detects duplicate arrays declared using BQ27XXX_DATA
within a single structure. It is currently specific to the file
drivers/power/supply/bq27xxx_battery.c. Nevertheless, having the script in
the kernel will allow others to check their code if the data structures
change in the future.
Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr>
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/coccinelle/api/check_bq27xxx_data.cocci | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/scripts/coccinelle/api/check_bq27xxx_data.cocci b/scripts/coccinelle/api/check_bq27xxx_data.cocci new file mode 100644 index 000000000000..9212b85169d2 --- /dev/null +++ b/scripts/coccinelle/api/check_bq27xxx_data.cocci | |||
| @@ -0,0 +1,161 @@ | |||
| 1 | /// Detect BQ27XXX_DATA structures with identical registers, dm registers or | ||
| 2 | /// properties. | ||
| 3 | //# Doesn't unfold macros used in register or property fields. | ||
| 4 | //# Requires OCaml scripting | ||
| 5 | /// | ||
| 6 | // Confidence: High | ||
| 7 | // Copyright: (C) 2017 Julia Lawall, Inria/LIP6, GPLv2. | ||
| 8 | // URL: http://coccinelle.lip6.fr/ | ||
| 9 | // Requires: 1.0.7 | ||
| 10 | // Keywords: BQ27XXX_DATA | ||
| 11 | |||
| 12 | virtual report | ||
| 13 | |||
| 14 | @initialize:ocaml@ | ||
| 15 | @@ | ||
| 16 | |||
| 17 | let print_report p msg = | ||
| 18 | let p = List.hd p in | ||
| 19 | Printf.printf "%s:%d:%d-%d: %s" p.file p.line p.col p.col_end msg | ||
| 20 | |||
| 21 | @str depends on report@ | ||
| 22 | type t; | ||
| 23 | identifier i,i1,i2; | ||
| 24 | expression e1,e2; | ||
| 25 | @@ | ||
| 26 | |||
| 27 | t i[] = { | ||
| 28 | ..., | ||
| 29 | [e1] = BQ27XXX_DATA(i1,...), | ||
| 30 | ..., | ||
| 31 | [e2] = BQ27XXX_DATA(i2,...), | ||
| 32 | ..., | ||
| 33 | }; | ||
| 34 | |||
| 35 | @script:ocaml tocheck@ | ||
| 36 | i1 << str.i1; | ||
| 37 | i2 << str.i2; | ||
| 38 | i1regs; i2regs; | ||
| 39 | i1dmregs; i2dmregs; | ||
| 40 | i1props; i2props; | ||
| 41 | @@ | ||
| 42 | |||
| 43 | if not(i1 = i2) | ||
| 44 | then | ||
| 45 | begin | ||
| 46 | i1regs := make_ident (i1 ^ "_regs"); | ||
| 47 | i2regs := make_ident (i2 ^ "_regs"); | ||
| 48 | i1dmregs := make_ident (i1 ^ "_dm_regs"); | ||
| 49 | i2dmregs := make_ident (i2 ^ "_dm_regs"); | ||
| 50 | i1props := make_ident (i1 ^ "_props"); | ||
| 51 | i2props := make_ident (i2 ^ "_props") | ||
| 52 | end | ||
| 53 | |||
| 54 | (* ---------------------------------------------------------------- *) | ||
| 55 | |||
| 56 | @getregs1@ | ||
| 57 | typedef u8; | ||
| 58 | identifier tocheck.i1regs; | ||
| 59 | initializer list i1regs_vals; | ||
| 60 | position p1; | ||
| 61 | @@ | ||
| 62 | |||
| 63 | u8 i1regs@p1[...] = { i1regs_vals, }; | ||
| 64 | |||
| 65 | @getregs2@ | ||
| 66 | identifier tocheck.i2regs; | ||
| 67 | initializer list i2regs_vals; | ||
| 68 | position p2; | ||
| 69 | @@ | ||
| 70 | |||
| 71 | u8 i2regs@p2[...] = { i2regs_vals, }; | ||
| 72 | |||
| 73 | @script:ocaml@ | ||
| 74 | (_,i1regs_vals) << getregs1.i1regs_vals; | ||
| 75 | (_,i2regs_vals) << getregs2.i2regs_vals; | ||
| 76 | i1regs << tocheck.i1regs; | ||
| 77 | i2regs << tocheck.i2regs; | ||
| 78 | p1 << getregs1.p1; | ||
| 79 | p2 << getregs2.p2; | ||
| 80 | @@ | ||
| 81 | |||
| 82 | if i1regs < i2regs && | ||
| 83 | List.sort compare i1regs_vals = List.sort compare i2regs_vals | ||
| 84 | then | ||
| 85 | let msg = | ||
| 86 | Printf.sprintf | ||
| 87 | "WARNING %s and %s (line %d) are identical\n" | ||
| 88 | i1regs i2regs (List.hd p2).line in | ||
| 89 | print_report p1 msg | ||
| 90 | |||
| 91 | (* ---------------------------------------------------------------- *) | ||
| 92 | |||
| 93 | @getdmregs1@ | ||
| 94 | identifier tocheck.i1dmregs; | ||
| 95 | initializer list i1dmregs_vals; | ||
| 96 | position p1; | ||
| 97 | @@ | ||
| 98 | |||
| 99 | struct bq27xxx_dm_reg i1dmregs@p1[] = { i1dmregs_vals, }; | ||
| 100 | |||
| 101 | @getdmregs2@ | ||
| 102 | identifier tocheck.i2dmregs; | ||
| 103 | initializer list i2dmregs_vals; | ||
| 104 | position p2; | ||
| 105 | @@ | ||
| 106 | |||
| 107 | struct bq27xxx_dm_reg i2dmregs@p2[] = { i2dmregs_vals, }; | ||
| 108 | |||
| 109 | @script:ocaml@ | ||
| 110 | (_,i1dmregs_vals) << getdmregs1.i1dmregs_vals; | ||
| 111 | (_,i2dmregs_vals) << getdmregs2.i2dmregs_vals; | ||
| 112 | i1dmregs << tocheck.i1dmregs; | ||
| 113 | i2dmregs << tocheck.i2dmregs; | ||
| 114 | p1 << getdmregs1.p1; | ||
| 115 | p2 << getdmregs2.p2; | ||
| 116 | @@ | ||
| 117 | |||
| 118 | if i1dmregs < i2dmregs && | ||
| 119 | List.sort compare i1dmregs_vals = List.sort compare i2dmregs_vals | ||
| 120 | then | ||
| 121 | let msg = | ||
| 122 | Printf.sprintf | ||
| 123 | "WARNING %s and %s (line %d) are identical\n" | ||
| 124 | i1dmregs i2dmregs (List.hd p2).line in | ||
| 125 | print_report p1 msg | ||
| 126 | |||
| 127 | (* ---------------------------------------------------------------- *) | ||
| 128 | |||
| 129 | @getprops1@ | ||
| 130 | identifier tocheck.i1props; | ||
| 131 | initializer list[n1] i1props_vals; | ||
| 132 | position p1; | ||
| 133 | @@ | ||
| 134 | |||
| 135 | enum power_supply_property i1props@p1[] = { i1props_vals, }; | ||
| 136 | |||
| 137 | @getprops2@ | ||
| 138 | identifier tocheck.i2props; | ||
| 139 | initializer list[n2] i2props_vals; | ||
| 140 | position p2; | ||
| 141 | @@ | ||
| 142 | |||
| 143 | enum power_supply_property i2props@p2[] = { i2props_vals, }; | ||
| 144 | |||
| 145 | @script:ocaml@ | ||
| 146 | (_,i1props_vals) << getprops1.i1props_vals; | ||
| 147 | (_,i2props_vals) << getprops2.i2props_vals; | ||
| 148 | i1props << tocheck.i1props; | ||
| 149 | i2props << tocheck.i2props; | ||
| 150 | p1 << getprops1.p1; | ||
| 151 | p2 << getprops2.p2; | ||
| 152 | @@ | ||
| 153 | |||
| 154 | if i1props < i2props && | ||
| 155 | List.sort compare i1props_vals = List.sort compare i2props_vals | ||
| 156 | then | ||
| 157 | let msg = | ||
| 158 | Printf.sprintf | ||
| 159 | "WARNING %s and %s (line %d) are identical\n" | ||
| 160 | i1props i2props (List.hd p2).line in | ||
| 161 | print_report p1 msg | ||
