1// Copyright (c) 2020 Nordic Semiconductor ASA
2// SPDX-License-Identifier: Apache-2.0
3
4// Enforce preservation of const qualifier on config_info casts
5//
6// Drivers cast the device config_info pointer to a driver-specific
7// structure.  The object is const-qualified; make sure the cast
8// doesn't inadvertently remove that qualifier.
9//
10// Also add the qualifier to pointer definitions where it's missing.
11//
12// Note that this patch may produce incorrect results if config_info
13// appears as a tag in non-device aggregate types.
14//
15// Options: --include-headers
16
17virtual patch
18virtual report
19
20// bare: (struct T*)E
21@r_cci_bare_patch
22 depends on patch
23 disable optional_qualifier
24@
25identifier T;
26expression E;
27@@
28 (
29+const
30 struct T*)E->config_info
31
32// bare const: (struct T* const)E
33@r_cci_bare_lc_patch
34 depends on patch
35 disable optional_qualifier
36@
37identifier T;
38expression E;
39@@
40 (
41+const
42 struct T * const)E->config_info
43
44// asg: struct T *D = (const struct T*)
45@r_cci_asg_patch
46 depends on patch
47 disable optional_qualifier
48@
49identifier T;
50identifier D;
51expression E;
52@@
53+const
54 struct T * D = (const struct T*)E->config_info;
55
56// asg to const local: struct T * const D = (const struct T*)
57@r_cci_lc_asg_patch
58 depends on patch
59 disable optional_qualifier
60@
61identifier T;
62identifier D;
63expression E;
64@@
65+const
66 struct T * const D = (const struct T*)E->config_info;
67
68// asg via macro: struct T * D = DEV_CFG()
69@r_cci_asg_macro_patch
70 depends on patch
71 disable optional_qualifier
72@
73identifier T;
74identifier D;
75expression E;
76@@
77+const
78 struct T * D = DEV_CFG(E);
79
80// asg via macro to const local: struct T * const D = DEV_CFG()
81@r_cci_lc_asg_macro_patch
82 depends on patch
83 disable optional_qualifier
84@
85identifier T;
86identifier D;
87expression E;
88@@
89+const
90 struct T * const D = DEV_CFG(E);
91
92// asg via macro: struct T * D; ... ; D = (const struct T*)CI;
93@r_cci_delayed_asg_patch
94 depends on patch
95 disable optional_qualifier
96@
97identifier T;
98identifier D;
99expression E;
100@@
101+const
102 struct T * D;
103 ...
104 D = (const struct T*)E->config_info;
105
106// delayed asg via macro: struct T * D; ... ; D = DEV_CFG();
107@r_cci_delayed_asg_macro_patch
108 depends on patch
109 disable optional_qualifier
110@
111identifier T;
112identifier D;
113expression E;
114@@
115+const
116 struct T * D;
117 ...
118 D = DEV_CFG(E);
119
120@r_cci_report
121 depends on report
122 disable optional_qualifier
123@
124identifier T;
125expression E;
126position p;
127@@
128 (struct T*)E->config_info@p
129
130@script:python
131 depends on report
132@
133t << r_cci_report.T;
134p << r_cci_report.p;
135@@
136msg = "WARNING: cast of config_info to struct {} requires 'const'".format(t)
137coccilib.report.print_report(p[0], msg)
138