1// Copyright (c) 2020 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4// Uses a python database (a dict) to find where const struct device
5// variable are being used in zephyr functions and, if it's being in place
6// of a void*, it will print an ERROR for loosing the const qualifier.
7// If it's being used on an unknown functions from an external module such
8// as a HAL, it will print a WARNING in order to check if the const qualifier
9// is not lost.
10
11virtual report
12
13////////////////////
14// Initialization //
15////////////////////
16
17@initialize:python
18  depends on report
19@
20@@
21import pickle
22
23def check_and_report(F, f, D, nb_args, p):
24    if f in f_void and int(nb_args) in f_void[f]:
25        msg = "ERROR: in {} calling {} param with {}, \
26loosing const qualifier, please wrap".format(F, f, D)
27        coccilib.report.print_report(p[0], msg)
28    elif f not in f_void and f not in f_other and not f.isupper():
29        msg = "WARNING: in {} calling {} param with {}, \
30check if const qualifier is not lost".format(F, f, D)
31        coccilib.report.print_report(p[0], msg)
32
33// Loading function data base
34with open("function_names.pickle", "rb") as f:
35    data = pickle.load(f)
36f_void = data["f_void"]
37f_other = data["f_other"]
38
39
40///////////
41// Rules //
42///////////
43
44// Find usage of a device instance
45@r_find_dev_usage
46  depends on report
47@
48local idexpression struct device *D;
49expression list[nb_args] args;
50identifier f;
51position p;
52@@
53  f(args, D@p, ...)
54
55
56@script:python
57  depends on r_find_dev_usage
58@
59f << r_find_dev_usage.f;
60D << r_find_dev_usage.D;
61nb_args << r_find_dev_usage.nb_args;
62p << r_find_dev_usage.p;
63@@
64check_and_report(p[0].current_element, f, D, nb_args, p)
65