diff options
| author | Himangi Saraogi <himangi774@gmail.com> | 2014-06-29 03:16:04 -0400 |
|---|---|---|
| committer | Michal Marek <mmarek@suse.cz> | 2014-08-06 06:10:17 -0400 |
| commit | 7ab04ea0e32cf8cabcbc33cefac36e5f9258734e (patch) | |
| tree | 82f41c8e314b54af5e994463fdf7d00008457c4e | |
| parent | 99fcec30e882ec214b689889b3599cf98f5c9843 (diff) | |
Coccinelle: Script to use ARRAY_SIZE instead of division of two sizeofs
This script detects cases where ARRAY_SIZE can be used such as
where there is a division of sizeof the array by the sizeof its first
element or by any indexed element or the element type. It replaces the
division of the two sizeofs by ARRAY_SIZE.
Signed-off-by: Himangi Saraogi <himangi774@gmail.com>
Acked-by: Julia Lawall <julia.lawall@lip6.fr>
Signed-off-by: Michal Marek <mmarek@suse.cz>
| -rw-r--r-- | scripts/coccinelle/misc/array_size.cocci | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/scripts/coccinelle/misc/array_size.cocci b/scripts/coccinelle/misc/array_size.cocci new file mode 100644 index 000000000000..81e279cd347b --- /dev/null +++ b/scripts/coccinelle/misc/array_size.cocci | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | /// Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element | ||
| 2 | /// | ||
| 3 | //# This makes an effort to find cases where ARRAY_SIZE can be used such as | ||
| 4 | //# where there is a division of sizeof the array by the sizeof its first | ||
| 5 | //# element or by any indexed element or the element type. It replaces the | ||
| 6 | //# division of the two sizeofs by ARRAY_SIZE. | ||
| 7 | // | ||
| 8 | // Confidence: High | ||
| 9 | // Copyright: (C) 2014 Himangi Saraogi. GPLv2. | ||
| 10 | // Comments: | ||
| 11 | // Options: --no-includes --include-headers | ||
| 12 | |||
| 13 | virtual patch | ||
| 14 | virtual context | ||
| 15 | virtual org | ||
| 16 | virtual report | ||
| 17 | |||
| 18 | @i@ | ||
| 19 | @@ | ||
| 20 | |||
| 21 | #include <linux/kernel.h> | ||
| 22 | |||
| 23 | //---------------------------------------------------------- | ||
| 24 | // For context mode | ||
| 25 | //---------------------------------------------------------- | ||
| 26 | |||
| 27 | @depends on i&&context@ | ||
| 28 | type T; | ||
| 29 | T[] E; | ||
| 30 | @@ | ||
| 31 | ( | ||
| 32 | * (sizeof(E)/sizeof(*E)) | ||
| 33 | | | ||
| 34 | * (sizeof(E)/sizeof(E[...])) | ||
| 35 | | | ||
| 36 | * (sizeof(E)/sizeof(T)) | ||
| 37 | ) | ||
| 38 | |||
| 39 | //---------------------------------------------------------- | ||
| 40 | // For patch mode | ||
| 41 | //---------------------------------------------------------- | ||
| 42 | |||
| 43 | @depends on i&&patch@ | ||
| 44 | type T; | ||
| 45 | T[] E; | ||
| 46 | @@ | ||
| 47 | ( | ||
| 48 | - (sizeof(E)/sizeof(*E)) | ||
| 49 | + ARRAY_SIZE(E) | ||
| 50 | | | ||
| 51 | - (sizeof(E)/sizeof(E[...])) | ||
| 52 | + ARRAY_SIZE(E) | ||
| 53 | | | ||
| 54 | - (sizeof(E)/sizeof(T)) | ||
| 55 | + ARRAY_SIZE(E) | ||
| 56 | ) | ||
| 57 | |||
| 58 | //---------------------------------------------------------- | ||
| 59 | // For org and report mode | ||
| 60 | //---------------------------------------------------------- | ||
| 61 | |||
| 62 | @r@ | ||
| 63 | type T; | ||
| 64 | T[] E; | ||
| 65 | position p; | ||
| 66 | @@ | ||
| 67 | ( | ||
| 68 | (sizeof(E)@p /sizeof(*E)) | ||
| 69 | | | ||
| 70 | (sizeof(E)@p /sizeof(E[...])) | ||
| 71 | | | ||
| 72 | (sizeof(E)@p /sizeof(T)) | ||
| 73 | ) | ||
| 74 | |||
| 75 | @script:python depends on i&&org@ | ||
| 76 | p << r.p; | ||
| 77 | @@ | ||
| 78 | |||
| 79 | coccilib.org.print_todo(p[0], "WARNING should use ARRAY_SIZE") | ||
| 80 | |||
| 81 | @script:python depends on i&&report@ | ||
| 82 | p << r.p; | ||
| 83 | @@ | ||
| 84 | |||
| 85 | msg="WARNING: Use ARRAY_SIZE" | ||
| 86 | coccilib.report.print_report(p[0], msg) | ||
| 87 | |||
