xref: /openbmc/linux/scripts/coccinelle/misc/array_size.cocci (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*7f904d7eSThomas Gleixner// SPDX-License-Identifier: GPL-2.0-only
27ab04ea0SHimangi Saraogi/// Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element
37ab04ea0SHimangi Saraogi///
47ab04ea0SHimangi Saraogi//# This makes an effort to find cases where ARRAY_SIZE can be used such as
57ab04ea0SHimangi Saraogi//# where there is a division of sizeof the array by the sizeof its first
67ab04ea0SHimangi Saraogi//# element or by any indexed element or the element type. It replaces the
77ab04ea0SHimangi Saraogi//# division of the two sizeofs by ARRAY_SIZE.
87ab04ea0SHimangi Saraogi//
97ab04ea0SHimangi Saraogi// Confidence: High
10*7f904d7eSThomas Gleixner// Copyright: (C) 2014 Himangi Saraogi.
117ab04ea0SHimangi Saraogi// Comments:
127ab04ea0SHimangi Saraogi// Options: --no-includes --include-headers
137ab04ea0SHimangi Saraogi
147ab04ea0SHimangi Saraogivirtual patch
157ab04ea0SHimangi Saraogivirtual context
167ab04ea0SHimangi Saraogivirtual org
177ab04ea0SHimangi Saraogivirtual report
187ab04ea0SHimangi Saraogi
197ab04ea0SHimangi Saraogi@i@
207ab04ea0SHimangi Saraogi@@
217ab04ea0SHimangi Saraogi
227ab04ea0SHimangi Saraogi#include <linux/kernel.h>
237ab04ea0SHimangi Saraogi
247ab04ea0SHimangi Saraogi//----------------------------------------------------------
257ab04ea0SHimangi Saraogi//  For context mode
267ab04ea0SHimangi Saraogi//----------------------------------------------------------
277ab04ea0SHimangi Saraogi
287ab04ea0SHimangi Saraogi@depends on i&&context@
297ab04ea0SHimangi Saraogitype T;
307ab04ea0SHimangi SaraogiT[] E;
317ab04ea0SHimangi Saraogi@@
327ab04ea0SHimangi Saraogi(
337ab04ea0SHimangi Saraogi* (sizeof(E)/sizeof(*E))
347ab04ea0SHimangi Saraogi|
357ab04ea0SHimangi Saraogi* (sizeof(E)/sizeof(E[...]))
367ab04ea0SHimangi Saraogi|
377ab04ea0SHimangi Saraogi* (sizeof(E)/sizeof(T))
387ab04ea0SHimangi Saraogi)
397ab04ea0SHimangi Saraogi
407ab04ea0SHimangi Saraogi//----------------------------------------------------------
417ab04ea0SHimangi Saraogi//  For patch mode
427ab04ea0SHimangi Saraogi//----------------------------------------------------------
437ab04ea0SHimangi Saraogi
447ab04ea0SHimangi Saraogi@depends on i&&patch@
457ab04ea0SHimangi Saraogitype T;
467ab04ea0SHimangi SaraogiT[] E;
477ab04ea0SHimangi Saraogi@@
487ab04ea0SHimangi Saraogi(
497ab04ea0SHimangi Saraogi- (sizeof(E)/sizeof(*E))
507ab04ea0SHimangi Saraogi+ ARRAY_SIZE(E)
517ab04ea0SHimangi Saraogi|
527ab04ea0SHimangi Saraogi- (sizeof(E)/sizeof(E[...]))
537ab04ea0SHimangi Saraogi+ ARRAY_SIZE(E)
547ab04ea0SHimangi Saraogi|
557ab04ea0SHimangi Saraogi- (sizeof(E)/sizeof(T))
567ab04ea0SHimangi Saraogi+ ARRAY_SIZE(E)
577ab04ea0SHimangi Saraogi)
587ab04ea0SHimangi Saraogi
597ab04ea0SHimangi Saraogi//----------------------------------------------------------
607ab04ea0SHimangi Saraogi//  For org and report mode
617ab04ea0SHimangi Saraogi//----------------------------------------------------------
627ab04ea0SHimangi Saraogi
63c7eaa887SJulia Lawall@r depends on (org || report)@
647ab04ea0SHimangi Saraogitype T;
657ab04ea0SHimangi SaraogiT[] E;
667ab04ea0SHimangi Saraogiposition p;
677ab04ea0SHimangi Saraogi@@
687ab04ea0SHimangi Saraogi(
697ab04ea0SHimangi Saraogi (sizeof(E)@p /sizeof(*E))
707ab04ea0SHimangi Saraogi|
717ab04ea0SHimangi Saraogi (sizeof(E)@p /sizeof(E[...]))
727ab04ea0SHimangi Saraogi|
737ab04ea0SHimangi Saraogi (sizeof(E)@p /sizeof(T))
747ab04ea0SHimangi Saraogi)
757ab04ea0SHimangi Saraogi
76cb00a4f3SJérémy Lefaure@script:python depends on org@
777ab04ea0SHimangi Saraogip << r.p;
787ab04ea0SHimangi Saraogi@@
797ab04ea0SHimangi Saraogi
807ab04ea0SHimangi Saraogicoccilib.org.print_todo(p[0], "WARNING should use ARRAY_SIZE")
817ab04ea0SHimangi Saraogi
82cb00a4f3SJérémy Lefaure@script:python depends on report@
837ab04ea0SHimangi Saraogip << r.p;
847ab04ea0SHimangi Saraogi@@
857ab04ea0SHimangi Saraogi
867ab04ea0SHimangi Saraogimsg="WARNING: Use ARRAY_SIZE"
877ab04ea0SHimangi Saraogicoccilib.report.print_report(p[0], msg)
887ab04ea0SHimangi Saraogi
89