diff options
| author | Nicolas Palix <npalix@diku.dk> | 2010-08-24 11:39:10 -0400 |
|---|---|---|
| committer | Michal Marek <mmarek@suse.cz> | 2010-08-31 05:43:13 -0400 |
| commit | be8fe9d451936a7a412b02cb88a13ffe46879bcf (patch) | |
| tree | 71b33d2acc78d04f1d327e655e7cf7c33ca2cd7e /scripts/coccinelle/api/alloc | |
| parent | 77c272fb24507c43fa301e3da83eb7d67d36a544 (diff) | |
Coccinelle: Move alloc directory into api directory
alloc contains various semantic patches related
to the allocation APIs
Signed-off-by: Nicolas Palix <npalix@diku.dk>
Signed-off-by: Julia Lawall <julia@diku.dk>
Signed-off-by: Michal Marek <mmarek@suse.cz>
Diffstat (limited to 'scripts/coccinelle/api/alloc')
| -rw-r--r-- | scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci | 67 | ||||
| -rw-r--r-- | scripts/coccinelle/api/alloc/kzalloc-simple.cocci | 86 |
2 files changed, 153 insertions, 0 deletions
diff --git a/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci b/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci new file mode 100644 index 000000000000..7d4771d449c3 --- /dev/null +++ b/scripts/coccinelle/api/alloc/drop_kmalloc_cast.cocci | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | /// | ||
| 2 | /// Casting (void *) value returned by kmalloc is useless | ||
| 3 | /// as mentioned in Documentation/CodingStyle, Chap 14. | ||
| 4 | /// | ||
| 5 | // Confidence: High | ||
| 6 | // Copyright: 2009,2010 Nicolas Palix, DIKU. GPLv2. | ||
| 7 | // URL: http://coccinelle.lip6.fr/ | ||
| 8 | // Options: -no_includes -include_headers | ||
| 9 | // | ||
| 10 | // Keywords: kmalloc, kzalloc, kcalloc | ||
| 11 | // Version min: < 2.6.12 kmalloc | ||
| 12 | // Version min: < 2.6.12 kcalloc | ||
| 13 | // Version min: 2.6.14 kzalloc | ||
| 14 | // | ||
| 15 | |||
| 16 | virtual context | ||
| 17 | virtual patch | ||
| 18 | virtual org | ||
| 19 | virtual report | ||
| 20 | |||
| 21 | //---------------------------------------------------------- | ||
| 22 | // For context mode | ||
| 23 | //---------------------------------------------------------- | ||
| 24 | |||
| 25 | @depends on context@ | ||
| 26 | type T; | ||
| 27 | @@ | ||
| 28 | |||
| 29 | * (T *) | ||
| 30 | \(kmalloc\|kzalloc\|kcalloc\)(...) | ||
| 31 | |||
| 32 | //---------------------------------------------------------- | ||
| 33 | // For patch mode | ||
| 34 | //---------------------------------------------------------- | ||
| 35 | |||
| 36 | @depends on patch@ | ||
| 37 | type T; | ||
| 38 | @@ | ||
| 39 | |||
| 40 | - (T *) | ||
| 41 | \(kmalloc\|kzalloc\|kcalloc\)(...) | ||
| 42 | |||
| 43 | //---------------------------------------------------------- | ||
| 44 | // For org and report mode | ||
| 45 | //---------------------------------------------------------- | ||
| 46 | |||
| 47 | @r depends on org || report@ | ||
| 48 | type T; | ||
| 49 | position p; | ||
| 50 | @@ | ||
| 51 | |||
| 52 | (T@p *)\(kmalloc\|kzalloc\|kcalloc\)(...) | ||
| 53 | |||
| 54 | @script:python depends on org@ | ||
| 55 | p << r.p; | ||
| 56 | t << r.T; | ||
| 57 | @@ | ||
| 58 | |||
| 59 | coccilib.org.print_safe_todo(p[0], t) | ||
| 60 | |||
| 61 | @script:python depends on report@ | ||
| 62 | p << r.p; | ||
| 63 | t << r.T; | ||
| 64 | @@ | ||
| 65 | |||
| 66 | msg="WARNING: casting value returned by k[cmz]alloc to (%s *) is useless." % (t) | ||
| 67 | coccilib.report.print_report(p[0], msg) | ||
diff --git a/scripts/coccinelle/api/alloc/kzalloc-simple.cocci b/scripts/coccinelle/api/alloc/kzalloc-simple.cocci new file mode 100644 index 000000000000..046b9b16f8f9 --- /dev/null +++ b/scripts/coccinelle/api/alloc/kzalloc-simple.cocci | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | /// | ||
| 2 | /// Use kzalloc rather than kmalloc followed by memset with 0 | ||
| 3 | /// | ||
| 4 | /// This considers some simple cases that are common and easy to validate | ||
| 5 | /// Note in particular that there are no ...s in the rule, so all of the | ||
| 6 | /// matched code has to be contiguous | ||
| 7 | /// | ||
| 8 | // Confidence: High | ||
| 9 | // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU. GPLv2. | ||
| 10 | // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
| 11 | // URL: http://coccinelle.lip6.fr/rules/kzalloc.html | ||
| 12 | // Options: -no_includes -include_headers | ||
| 13 | // | ||
| 14 | // Keywords: kmalloc, kzalloc | ||
| 15 | // Version min: < 2.6.12 kmalloc | ||
| 16 | // Version min: 2.6.14 kzalloc | ||
| 17 | // | ||
| 18 | |||
| 19 | virtual context | ||
| 20 | virtual patch | ||
| 21 | virtual org | ||
| 22 | virtual report | ||
| 23 | |||
| 24 | //---------------------------------------------------------- | ||
| 25 | // For context mode | ||
| 26 | //---------------------------------------------------------- | ||
| 27 | |||
| 28 | @depends on context@ | ||
| 29 | type T, T2; | ||
| 30 | expression x; | ||
| 31 | expression E1,E2; | ||
| 32 | statement S; | ||
| 33 | @@ | ||
| 34 | |||
| 35 | * x = (T)kmalloc(E1,E2); | ||
| 36 | if ((x==NULL) || ...) S | ||
| 37 | * memset((T2)x,0,E1); | ||
| 38 | |||
| 39 | //---------------------------------------------------------- | ||
| 40 | // For patch mode | ||
| 41 | //---------------------------------------------------------- | ||
| 42 | |||
| 43 | @depends on patch@ | ||
| 44 | type T, T2; | ||
| 45 | expression x; | ||
| 46 | expression E1,E2; | ||
| 47 | statement S; | ||
| 48 | @@ | ||
| 49 | |||
| 50 | - x = (T)kmalloc(E1,E2); | ||
| 51 | + x = kzalloc(E1,E2); | ||
| 52 | if ((x==NULL) || ...) S | ||
| 53 | - memset((T2)x,0,E1); | ||
| 54 | |||
| 55 | //---------------------------------------------------------- | ||
| 56 | // For org mode | ||
| 57 | //---------------------------------------------------------- | ||
| 58 | |||
| 59 | @r depends on org || report@ | ||
| 60 | type T, T2; | ||
| 61 | expression x; | ||
| 62 | expression E1,E2; | ||
| 63 | statement S; | ||
| 64 | position p; | ||
| 65 | @@ | ||
| 66 | |||
| 67 | x = (T)kmalloc@p(E1,E2); | ||
| 68 | if ((x==NULL) || ...) S | ||
| 69 | memset((T2)x,0,E1); | ||
| 70 | |||
| 71 | @script:python depends on org@ | ||
| 72 | p << r.p; | ||
| 73 | x << r.x; | ||
| 74 | @@ | ||
| 75 | |||
| 76 | msg="%s" % (x) | ||
| 77 | msg_safe=msg.replace("[","@(").replace("]",")") | ||
| 78 | coccilib.org.print_todo(p[0], msg_safe) | ||
| 79 | |||
| 80 | @script:python depends on report@ | ||
| 81 | p << r.p; | ||
| 82 | x << r.x; | ||
| 83 | @@ | ||
| 84 | |||
| 85 | msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x) | ||
| 86 | coccilib.report.print_report(p[0], msg) | ||
