Skip to content

Commit cfa947f

Browse files
aitapMichaelChiricoben-schwen
authored
Add Coccinelle lint checks for non-_RO accessors used with pointers to const (#7622)
* Use _RO accessors for const targets * use read-only accessors when appropriate (#7615) * one more _RO Co-authored-by: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com> * review suggestions Co-authored-by: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com> * botched suggested change * Use more read-only accessors * gfirstlast: use _RO accessors Keep the casts because they are currently necessary for the integer64 case. * add coccinelle rules to use read only accessors * CI should also run when cocci files are touched * cocci_linter.R: fix zero-length error reports * use_ro_accessors.cocci: expand linter rules * Apply suggestions from code review Co-authored-by: Michael Chirico <chiricom@google.com> * Revert "cocci_linter.R: fix zero-length error reports" This reverts commit 70d37da. * Fix the file path list element Setting $c_obj instead of $path caused the error messages to be zero-length due to the latter being returned as NULL. Co-Authored-By: Michael Chirico <chiricom@google.com> Co-Authored-By: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com> * Reorder checks according to SEXPTYPE order --------- Co-authored-by: Michael Chirico <chiricom@google.com> Co-authored-by: Benjamin Schwendinger <52290390+ben-schwen@users.noreply.github.com> Co-authored-by: Benjamin Schwendinger <benjaminschwe@gmail.com>
1 parent 293f3b0 commit cfa947f

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

.ci/linters/c/00preprocess.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.preprocess = function (f) list(
2-
c_obj = f, lines = readLines(f),
2+
path = f, lines = readLines(f),
33
preprocessed = system2(
44
"gcc", shQuote(c("-fpreprocessed", "-E", f)),
55
stdout = TRUE, stderr = FALSE
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* Ensure _RO accessors are used for assignment to 'const' variables */
2+
@@
3+
type T;
4+
const T *variable;
5+
expression E;
6+
@@
7+
- variable = RAW(E)
8+
+ variable = RAW_RO(E)
9+
10+
@@
11+
type T;
12+
const T *variable;
13+
expression E;
14+
@@
15+
- variable = LOGICAL(E)
16+
+ variable = LOGICAL_RO(E)
17+
18+
@@
19+
type T;
20+
const T *variable;
21+
expression E;
22+
@@
23+
- variable = INTEGER(E)
24+
+ variable = INTEGER_RO(E)
25+
26+
@@
27+
type T;
28+
const T *variable;
29+
expression E;
30+
@@
31+
- variable = REAL(E)
32+
+ variable = REAL_RO(E)
33+
34+
@@
35+
type T;
36+
const T *variable;
37+
expression E;
38+
@@
39+
- variable = COMPLEX(E)
40+
+ variable = COMPLEX_RO(E)
41+
42+
/* Just use _RO accessors directly instead of 'const' casting the writeable one */
43+
@@
44+
expression E;
45+
type T;
46+
@@
47+
-(const T*) RAW(E)
48+
+RAW_RO(E)
49+
50+
@@
51+
expression E;
52+
@@
53+
-(const int*) LOGICAL(E)
54+
+LOGICAL_RO(E)
55+
56+
@@
57+
expression E;
58+
@@
59+
-(const int*) INTEGER(E)
60+
+INTEGER_RO(E)
61+
62+
@@
63+
expression E;
64+
@@
65+
-(const double*) REAL(E)
66+
+REAL_RO(E)
67+
@@
68+
expression E;
69+
type T;
70+
@@
71+
-(const T*) COMPLEX(E)
72+
+COMPLEX_RO(E)
73+
74+
@@
75+
expression E;
76+
type T;
77+
@@
78+
-(const T*) STRING_PTR(E)
79+
+STRING_PTR_RO(E)

.github/workflows/code-quality.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
c:
3030
- '**/*.c'
3131
- '**/*.h'
32+
- '.ci/linters/cocci/**/*.cocci'
3233
po:
3334
- 'po/**/*.po'
3435
md:

0 commit comments

Comments
 (0)