scripts/coccinelle: add conversion of integers to timeout values

Some legacy code still passes integer literals in milliseconds as the
value to functions that take a timeout.  This usage interferes with
plans to replace the millisecond representation with a more generic
k_timeout_t value.  Add a Coccinelle script to convert call sites to
use the proper constants and macros.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2019-10-02 19:24:35 -05:00 committed by Kumar Gala
commit 0e5329331b

View file

@ -0,0 +1,81 @@
// 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.
// Handle k_timer_start delay parameters
@delay_id@
expression T, P;
position p;
identifier D;
@@
k_timer_start@p(T, D, P)
@@
expression T, P;
position p != delay_id.p;
@@
k_timer_start@p(T,
- 0
+ K_NO_WAIT
, P)
@@
expression T, P;
position p != delay_id.p;
@@
k_timer_start@p(T,
- -1
+ K_FOREVER
, P)
@@
expression T, P;
constant int D;
position p != delay_id.p;
@@
k_timer_start@p(T,
- D
+ K_MSEC(D)
, P)
// Handle timeouts at the end of the argument list
@end_id@
identifier f =~ "^k_(timer_start|queue_get|futex_wait|stack_pop|delayed_work_submit_to_queue|mutex_lock|sem_take|(msgq|mbox|pipe)_(block_)?(put|get)|mem_(slab|pool)_alloc|poll|thread_deadline_set)$";
position p;
identifier D;
@@
f@p(..., D)
@@
identifier f =~ "^k_(timer_start|queue_get|futex_wait|stack_pop|delayed_work_submit_to_queue|mutex_lock|sem_take|(msgq|mbox|pipe)_(block_)?(put|get)|mem_(slab|pool)_alloc|poll|thread_deadline_set)$";
position p != end_id.p;
@@
f@p(...,
- 0
+ K_NO_WAIT
)
@@
identifier f =~ "^k_(timer_start|queue_get|futex_wait|stack_pop|delayed_work_submit_to_queue|mutex_lock|sem_take|(msgq|mbox|pipe)_(block_)?(put|get)|mem_(slab|pool)_alloc|poll|thread_deadline_set)$";
position p != end_id.p;
@@
f@p(...,
- -1
+ K_FOREVER
)
@@
identifier f =~ "^k_(timer_start|queue_get|futex_wait|stack_pop|delayed_work_submit_to_queue|mutex_lock|sem_take|(msgq|mbox|pipe)_(block_)?(put|get)|mem_(slab|pool)_alloc|poll|thread_deadline_set)$";
position p != end_id.p;
constant int D;
@@
f@p(...,
- D
+ K_MSEC(D)
)