Skip to content

Commit

Permalink
Introduce the Coccinelle linter
Browse files Browse the repository at this point in the history
For mistakes that are easy to fix automatically, Coccinelle patches may
be written. If a patch finds a mistake, the linter will ask the human to
fix either the code or the patch.
  • Loading branch information
aitap committed Feb 2, 2025
1 parent b2fad33 commit de686d1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .ci/linters/c/cocci_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cocci_linter = if (!nzchar(Sys.which("spatch"))) function(...) {} else function(c_obj) {
bad <- FALSE
for (spfile in list.files(".ci/linters/cocci", full.names = TRUE)) {
# Coccinelle parser gets confused sometimes, so ignore stderr and the exit code
out = suppressWarnings(system2(
"spatch",
shQuote(c(
"--sp-file", spfile, c_obj$path, "--recursive-includes",
"-I", R.home("include"), "-I", "src"
)),
stdout = TRUE, stderr = FALSE
))
if (length(out) > 0) {
cat(sprintf("In file '%s', Coccinelle patch '%s' recommends the following changes:\n", c_obj$path, spfile))
writeLines(out)
bad <- TRUE
}
}
if (bad) stop("Please apply the changes above or fix the linter")
}
6 changes: 6 additions & 0 deletions .ci/linters/cocci/redundant_cast_to_same_pointer_type.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@@
type T;
T* E;
@@
- (T*)
E
4 changes: 4 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
- name: Install Coccinelle
run: |
apt-get update
apt-get -y install coccinelle
- name: Lint
run: |
linter_env = new.env()
Expand Down

0 comments on commit de686d1

Please sign in to comment.