~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/coccinelle/misc/do_div.cocci

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /scripts/coccinelle/misc/do_div.cocci (Architecture ppc) and /scripts/coccinelle/misc/do_div.cocci (Architecture mips)


  1 // SPDX-License-Identifier: GPL-2.0-only            1 // SPDX-License-Identifier: GPL-2.0-only
  2 /// do_div() does a 64-by-32 division.              2 /// do_div() does a 64-by-32 division.
  3 /// When the divisor is long, unsigned long, u      3 /// When the divisor is long, unsigned long, u64, or s64,
  4 /// do_div() truncates it to 32 bits, this mea      4 /// do_div() truncates it to 32 bits, this means it can test
  5 /// non-zero and be truncated to 0 for divisio      5 /// non-zero and be truncated to 0 for division on 64bit platforms.
  6 ///                                                 6 ///
  7 //# This makes an effort to find those inappro      7 //# This makes an effort to find those inappropriate do_div() calls.
  8 //                                                  8 //
  9 // Confidence: Moderate                             9 // Confidence: Moderate
 10 // Copyright: (C) 2020 Wen Yang, Alibaba.          10 // Copyright: (C) 2020 Wen Yang, Alibaba.
 11 // Comments:                                       11 // Comments:
 12 // Options: --no-includes --include-headers        12 // Options: --no-includes --include-headers
 13                                                    13 
 14 virtual context                                    14 virtual context
 15 virtual org                                        15 virtual org
 16 virtual report                                     16 virtual report
 17                                                    17 
 18 @initialize:python@                                18 @initialize:python@
 19 @@                                                 19 @@
 20                                                    20 
 21 def get_digit_type_and_value(str):                 21 def get_digit_type_and_value(str):
 22     is_digit = False                               22     is_digit = False
 23     value = 0                                      23     value = 0
 24                                                    24 
 25     try:                                           25     try:
 26         if (str.isdigit()):                        26         if (str.isdigit()):
 27            is_digit = True                         27            is_digit = True
 28            value =  int(str, 0)                    28            value =  int(str, 0)
 29         elif (str.upper().endswith('ULL')):        29         elif (str.upper().endswith('ULL')):
 30            is_digit = True                         30            is_digit = True
 31            value = int(str[:-3], 0)                31            value = int(str[:-3], 0)
 32         elif (str.upper().endswith('LL')):         32         elif (str.upper().endswith('LL')):
 33            is_digit = True                         33            is_digit = True
 34            value = int(str[:-2], 0)                34            value = int(str[:-2], 0)
 35         elif (str.upper().endswith('UL')):         35         elif (str.upper().endswith('UL')):
 36            is_digit = True                         36            is_digit = True
 37            value = int(str[:-2], 0)                37            value = int(str[:-2], 0)
 38         elif (str.upper().endswith('L')):          38         elif (str.upper().endswith('L')):
 39            is_digit = True                         39            is_digit = True
 40            value = int(str[:-1], 0)                40            value = int(str[:-1], 0)
 41         elif (str.upper().endswith('U')):          41         elif (str.upper().endswith('U')):
 42            is_digit = True                         42            is_digit = True
 43            value = int(str[:-1], 0)                43            value = int(str[:-1], 0)
 44     except Exception as e:                         44     except Exception as e:
 45           print('Error:',e)                        45           print('Error:',e)
 46           is_digit = False                         46           is_digit = False
 47           value = 0                                47           value = 0
 48     finally:                                       48     finally:
 49         return is_digit, value                     49         return is_digit, value
 50                                                    50 
 51 def filter_out_safe_constants(str):                51 def filter_out_safe_constants(str):
 52     is_digit, value = get_digit_type_and_value     52     is_digit, value = get_digit_type_and_value(str)
 53     if (is_digit):                                 53     if (is_digit):
 54         if (value >= 0x100000000):                 54         if (value >= 0x100000000):
 55             return True                            55             return True
 56         else:                                      56         else:
 57             return False                           57             return False
 58     else:                                          58     else:
 59         return True                                59         return True
 60                                                    60 
 61 def construct_warnings(suggested_fun):             61 def construct_warnings(suggested_fun):
 62     msg="WARNING: do_div() does a 64-by-32 div     62     msg="WARNING: do_div() does a 64-by-32 division, please consider using %s instead."
 63     return  msg % suggested_fun                    63     return  msg % suggested_fun
 64                                                    64 
 65 @depends on context@                               65 @depends on context@
 66 expression f;                                      66 expression f;
 67 long l: script:python() { filter_out_safe_cons     67 long l: script:python() { filter_out_safe_constants(l) };
 68 unsigned long ul : script:python() { filter_ou     68 unsigned long ul : script:python() { filter_out_safe_constants(ul) };
 69 u64 ul64 : script:python() { filter_out_safe_c     69 u64 ul64 : script:python() { filter_out_safe_constants(ul64) };
 70 s64 sl64 : script:python() { filter_out_safe_c     70 s64 sl64 : script:python() { filter_out_safe_constants(sl64) };
 71                                                    71 
 72 @@                                                 72 @@
 73 (                                                  73 (
 74 * do_div(f, l);                                    74 * do_div(f, l);
 75 |                                                  75 |
 76 * do_div(f, ul);                                   76 * do_div(f, ul);
 77 |                                                  77 |
 78 * do_div(f, ul64);                                 78 * do_div(f, ul64);
 79 |                                                  79 |
 80 * do_div(f, sl64);                                 80 * do_div(f, sl64);
 81 )                                                  81 )
 82                                                    82 
 83 @r depends on (org || report)@                     83 @r depends on (org || report)@
 84 expression f;                                      84 expression f;
 85 position p;                                        85 position p;
 86 long l: script:python() { filter_out_safe_cons     86 long l: script:python() { filter_out_safe_constants(l) };
 87 unsigned long ul : script:python() { filter_ou     87 unsigned long ul : script:python() { filter_out_safe_constants(ul) };
 88 u64 ul64 : script:python() { filter_out_safe_c     88 u64 ul64 : script:python() { filter_out_safe_constants(ul64) };
 89 s64 sl64 : script:python() { filter_out_safe_c     89 s64 sl64 : script:python() { filter_out_safe_constants(sl64) };
 90 @@                                                 90 @@
 91 (                                                  91 (
 92 do_div@p(f, l);                                    92 do_div@p(f, l);
 93 |                                                  93 |
 94 do_div@p(f, ul);                                   94 do_div@p(f, ul);
 95 |                                                  95 |
 96 do_div@p(f, ul64);                                 96 do_div@p(f, ul64);
 97 |                                                  97 |
 98 do_div@p(f, sl64);                                 98 do_div@p(f, sl64);
 99 )                                                  99 )
100                                                   100 
101 @script:python depends on org@                    101 @script:python depends on org@
102 p << r.p;                                         102 p << r.p;
103 ul << r.ul;                                       103 ul << r.ul;
104 @@                                                104 @@
105                                                   105 
106 coccilib.org.print_todo(p[0], construct_warnin    106 coccilib.org.print_todo(p[0], construct_warnings("div64_ul"))
107                                                   107 
108 @script:python depends on org@                    108 @script:python depends on org@
109 p << r.p;                                         109 p << r.p;
110 l << r.l;                                         110 l << r.l;
111 @@                                                111 @@
112                                                   112 
113 coccilib.org.print_todo(p[0], construct_warnin    113 coccilib.org.print_todo(p[0], construct_warnings("div64_long"))
114                                                   114 
115 @script:python depends on org@                    115 @script:python depends on org@
116 p << r.p;                                         116 p << r.p;
117 ul64 << r.ul64;                                   117 ul64 << r.ul64;
118 @@                                                118 @@
119                                                   119 
120 coccilib.org.print_todo(p[0], construct_warnin    120 coccilib.org.print_todo(p[0], construct_warnings("div64_u64"))
121                                                   121 
122 @script:python depends on org@                    122 @script:python depends on org@
123 p << r.p;                                         123 p << r.p;
124 sl64 << r.sl64;                                   124 sl64 << r.sl64;
125 @@                                                125 @@
126                                                   126 
127 coccilib.org.print_todo(p[0], construct_warnin    127 coccilib.org.print_todo(p[0], construct_warnings("div64_s64"))
128                                                   128 
129 @script:python depends on report@                 129 @script:python depends on report@
130 p << r.p;                                         130 p << r.p;
131 ul << r.ul;                                       131 ul << r.ul;
132 @@                                                132 @@
133                                                   133 
134 coccilib.report.print_report(p[0], construct_w    134 coccilib.report.print_report(p[0], construct_warnings("div64_ul"))
135                                                   135 
136 @script:python depends on report@                 136 @script:python depends on report@
137 p << r.p;                                         137 p << r.p;
138 l << r.l;                                         138 l << r.l;
139 @@                                                139 @@
140                                                   140 
141 coccilib.report.print_report(p[0], construct_w    141 coccilib.report.print_report(p[0], construct_warnings("div64_long"))
142                                                   142 
143 @script:python depends on report@                 143 @script:python depends on report@
144 p << r.p;                                         144 p << r.p;
145 sl64 << r.sl64;                                   145 sl64 << r.sl64;
146 @@                                                146 @@
147                                                   147 
148 coccilib.report.print_report(p[0], construct_w    148 coccilib.report.print_report(p[0], construct_warnings("div64_s64"))
149                                                   149 
150 @script:python depends on report@                 150 @script:python depends on report@
151 p << r.p;                                         151 p << r.p;
152 ul64 << r.ul64;                                   152 ul64 << r.ul64;
153 @@                                                153 @@
154                                                   154 
155 coccilib.report.print_report(p[0], construct_w    155 coccilib.report.print_report(p[0], construct_warnings("div64_u64"))
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php