Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | // Copyright (c) 2019 Nordic Semiconductor ASA
// SPDX-License-Identifer: Apache-2.0
// Replace integer constant timeouts with K_MSEC variants
//
// Some existing code assumes that timeout parameters are provided as
// integer milliseconds, when they were intended to be timeout values
// produced by specific constants and macros. Convert the integer
// literals to the desired equivalent.
//
// Options: --include-headers
virtual patch
virtual report
// ** Handle timeouts at the last position of kernel API arguments
// Base rule provides the complex identifier regular expression
@r_last_timeout@
identifier last_timeout =~ "(?x)^k_
( delayed_work_submit(|_to_queue)
| futex_wait
| mbox_data_block_get
| (mbox|msgq)_get
| mem_(pool|slab)_alloc
| mutex_lock
| pipe_(get|put)
| poll
| queue_get
| sem_take
| sleep
| stack_pop
| thread_create
| timer_start
| work_poll_submit(|_to_queue)
)$";
@@
last_timeout(...)
// Identify call sites where an identifier is used for the timeout
@r_last_timeout_id
extends r_last_timeout
@
identifier D;
position p;
@@
last_timeout@p(..., D)
// Select call sites where a constant literal (not identifier) is used
// for the timeout and replace the constant with the appropriate macro
@r_last_timeout_const_patch
extends r_last_timeout
depends on patch
@
constant C;
position p != r_last_timeout_id.p;
@@
last_timeout@p(...,
(
- 0
+ K_NO_WAIT
|
- -1
+ K_FOREVER
|
- C
+ K_MSEC(C)
)
)
@r_last_timeout_const_report
extends r_last_timeout
depends on report
@
constant C;
position p != r_last_timeout_id.p;
@@
last_timeout@p(..., C)
@script:python
depends on report
@
fn << r_last_timeout.last_timeout;
p << r_last_timeout_const_report.p;
C << r_last_timeout_const_report.C;
@@
msg = "WARNING: replace constant {} with timeout in {}".format(C, fn)
coccilib.report.print_report(p[0], msg);
// ** Convert integer delays in K_THREAD_DEFINE to the appropriate macro
// Identify declarers where an identifier is used for the delay
@r_thread_decl_id@
declarer name K_THREAD_DEFINE;
identifier C;
position p;
@@
K_THREAD_DEFINE@p(..., C);
// Select declarers with constant literal delay and replace with
// appropriate macro
@depends on patch@
declarer name K_THREAD_DEFINE;
constant C;
position p != r_thread_decl_id.p;
@@
K_THREAD_DEFINE@p(...,
(
- 0
+ K_NO_WAIT
|
- -1
+ K_FOREVER
|
- C
+ K_MSEC(C)
)
);
// Identify declarers where an identifier is used for the delay
@r_thread_decl_const
depends on report@
declarer name K_THREAD_DEFINE;
constant C;
position p != r_thread_decl_id.p;
@@
K_THREAD_DEFINE@p(..., C);
@script:python
depends on report
@
C << r_thread_decl_const.C;
p << r_thread_decl_const.p;
@@
msg = "WARNING: replace constant {} with timeout in K_THREAD_DEFINE".format(C)
coccilib.report.print_report(p[0], msg);
// ** Handle k_timer_start where the second (not last) argument is a
// ** constant literal.
// Select call sites where an identifier is used for the duration timeout
@r_timer_duration@
expression T;
identifier D;
expression I;
position p;
@@
k_timer_start@p(T, D, I)
// Select call sites where a constant literal (not identifier) is used
// for the timeout and replace the constant with the appropriate macro
@depends on patch@
expression T;
constant C;
expression I;
position p != r_timer_duration.p;
@@
k_timer_start@p(T,
(
- 0
+ K_NO_WAIT
|
- -1
+ K_FOREVER
|
- C
+ K_MSEC(C)
)
, I)
@r_timer_duration_report
depends on report
@
expression T;
constant C;
expression I;
position p != r_timer_duration.p;
@@
k_timer_start@p(T, C, I)
@script:python
depends on report
@
p << r_timer_duration_report.p;
C << r_timer_duration_report.C;
@@
msg = "WARNING: replace constant {} with duration timeout in k_timer_start".format(C)
coccilib.report.print_report(p[0], msg);
|