Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /// Use ARRAY_SIZE instead of dividing sizeof array with sizeof an element
0003 ///
0004 //# This makes an effort to find cases where ARRAY_SIZE can be used such as
0005 //# where there is a division of sizeof the array by the sizeof its first
0006 //# element or by any indexed element or the element type. It replaces the
0007 //# division of the two sizeofs by ARRAY_SIZE.
0008 //
0009 // Confidence: High
0010 // Copyright: (C) 2014 Himangi Saraogi.
0011 // Comments:
0012 // Options: --no-includes --include-headers
0013 
0014 virtual patch
0015 virtual context
0016 virtual org
0017 virtual report
0018 
0019 @i@
0020 @@
0021 
0022 #include <linux/kernel.h>
0023 
0024 //----------------------------------------------------------
0025 //  For context mode
0026 //----------------------------------------------------------
0027 
0028 @depends on i&&context@
0029 type T;
0030 T[] E;
0031 @@
0032 (
0033 * (sizeof(E)/sizeof(*E))
0034 |
0035 * (sizeof(E)/sizeof(E[...]))
0036 |
0037 * (sizeof(E)/sizeof(T))
0038 )
0039 
0040 //----------------------------------------------------------
0041 //  For patch mode
0042 //----------------------------------------------------------
0043 
0044 @depends on i&&patch@
0045 type T;
0046 T[] E;
0047 @@
0048 (
0049 - (sizeof(E)/sizeof(*E))
0050 + ARRAY_SIZE(E)
0051 |
0052 - (sizeof(E)/sizeof(E[...]))
0053 + ARRAY_SIZE(E)
0054 |
0055 - (sizeof(E)/sizeof(T))
0056 + ARRAY_SIZE(E)
0057 )
0058 
0059 //----------------------------------------------------------
0060 //  For org and report mode
0061 //----------------------------------------------------------
0062 
0063 @r depends on (org || report)@
0064 type T;
0065 T[] E;
0066 position p;
0067 @@
0068 (
0069  (sizeof(E)@p /sizeof(*E))
0070 |
0071  (sizeof(E)@p /sizeof(E[...]))
0072 |
0073  (sizeof(E)@p /sizeof(T))
0074 )
0075 
0076 @script:python depends on org@
0077 p << r.p;
0078 @@
0079 
0080 coccilib.org.print_todo(p[0], "WARNING should use ARRAY_SIZE")
0081 
0082 @script:python depends on report@
0083 p << r.p;
0084 @@
0085 
0086 msg="WARNING: Use ARRAY_SIZE"
0087 coccilib.report.print_report(p[0], msg)
0088