1// Copyright (c) 2020 Intel Corporation 2// SPDX-License-Identifier: Apache-2.0 3 4// In patch mode, patch all device instance to const (if not already). 5 6// In report mode: 7// Generate a q&d python database (a dict actually) of all the 8// declared zephyr functions. It will store each function name in 2 9// separate dicts: one storing all function having 1+ void* parameter 10// and one for all the other functions. It will store the positions 11// of the void* parameter in the first dict, and the actual number of 12// parameters in the second. 13// Then find_dev_usage.cocci can be used to verify if device instance 14// are not loosing their const qualifier. 15 16virtual patch 17virtual report 18 19 20//////////////////// 21// Initialization // 22//////////////////// 23 24@initialize:python 25 depends on report 26@ 27@@ 28import pickle 29f_void = {} 30f_other = {} 31 32// Insert a function into right dict depending on parameters 33def insert_function(f, params): 34 void_pos = [] 35 i = 0 36 for prm in params: 37 if prm.startswith("void *"): 38 void_pos.append(i) 39 i += 1 40 if len(void_pos) != 0: 41 f_void[f] = void_pos 42 else: 43 f_other[f] = i + 1 44 45 46/////////// 47// Rules // 48/////////// 49 50// Switch device instance to constant. 51@r_const_dev 52 depends on patch 53 disable optional_qualifier 54@ 55@@ 56-struct device * 57+const struct device * 58 59 60// Find function declarations 61@r_find_func_declare 62 depends on report 63@ 64identifier f; 65type ret_type; 66parameter list[nb_params] params; 67@@ 68 ret_type f(params); 69 70// Insert function declaration 71@script:python 72 depends on report 73@ 74f << r_find_func_declare.f; 75params << r_find_func_declare.params; 76@@ 77insert_function(f, params) 78 79 80// Find function implementations and inlines 81// (maybe it should focus on static only? 82// but then first rule should not match statics.) 83@r_find_func_impl_inlines 84 depends on report 85@ 86identifier f; 87type ret_type; 88parameter list[nb_params] params; 89@@ 90( 91 ret_type f(params) 92 { 93 ... 94 } 95| 96 static inline ret_type f(params) 97 { 98 ... 99 } 100) 101 102// Insert function implementations and inlines 103@script:python 104 depends on report 105@ 106f << r_find_func_impl_inlines.f; 107params << r_find_func_impl_inlines.params; 108@@ 109insert_function(f, params) 110 111 112////////////////// 113// Finalization // 114////////////////// 115 116@finalize:python 117 depends on report 118@ 119@@ 120with open("function_names.pickle", "wb") as f: 121 data = {} 122 data['f_void'] = f_void 123 data['f_other'] = f_other 124 pickle.dump(data, f, pickle.HIGHEST_PROTOCOL) 125