array_size.cocci (1729B)
1// SPDX-License-Identifier: GPL-2.0-only 2/// Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element 3/// 4//# This makes an effort to find cases where ARRAY_SIZE can be used such as 5//# where there is a division of sizeof the array by the sizeof its first 6//# element or by any indexed element or the element type. It replaces the 7//# division of the two sizeofs by ARRAY_SIZE. 8// 9// Confidence: High 10// Copyright: (C) 2014 Himangi Saraogi. 11// Comments: 12// Options: --no-includes --include-headers 13 14virtual patch 15virtual context 16virtual org 17virtual report 18 19@i@ 20@@ 21 22#include <linux/kernel.h> 23 24//---------------------------------------------------------- 25// For context mode 26//---------------------------------------------------------- 27 28@depends on i&&context@ 29type T; 30T[] E; 31@@ 32( 33* (sizeof(E)/sizeof(*E)) 34| 35* (sizeof(E)/sizeof(E[...])) 36| 37* (sizeof(E)/sizeof(T)) 38) 39 40//---------------------------------------------------------- 41// For patch mode 42//---------------------------------------------------------- 43 44@depends on i&&patch@ 45type T; 46T[] E; 47@@ 48( 49- (sizeof(E)/sizeof(*E)) 50+ ARRAY_SIZE(E) 51| 52- (sizeof(E)/sizeof(E[...])) 53+ ARRAY_SIZE(E) 54| 55- (sizeof(E)/sizeof(T)) 56+ ARRAY_SIZE(E) 57) 58 59//---------------------------------------------------------- 60// For org and report mode 61//---------------------------------------------------------- 62 63@r depends on (org || report)@ 64type T; 65T[] E; 66position p; 67@@ 68( 69 (sizeof(E)@p /sizeof(*E)) 70| 71 (sizeof(E)@p /sizeof(E[...])) 72| 73 (sizeof(E)@p /sizeof(T)) 74) 75 76@script:python depends on org@ 77p << r.p; 78@@ 79 80coccilib.org.print_todo(p[0], "WARNING should use ARRAY_SIZE") 81 82@script:python depends on report@ 83p << r.p; 84@@ 85 86msg="WARNING: Use ARRAY_SIZE" 87coccilib.report.print_report(p[0], msg) 88