Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CodeQL query to detect redundant assignments
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
ryao committed Jan 22, 2024
1 parent ac944f0 commit 2b4f83c
Showing 6 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/codeql-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: "Custom CodeQL Analysis"

queries:
- uses: ./.github/codeql/custom-queries/cpp/redundantAssignment.ql
- uses: ./.github/codeql/openzfs-code-scanning.qls
4 changes: 4 additions & 0 deletions .github/codeql-python.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: "Custom CodeQL Analysis"

paths-ignore:
- tests
4 changes: 4 additions & 0 deletions .github/codeql/custom-queries/cpp/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: openzfs-cpp-queries
version: 0.0.0
libraryPathDependencies: codeql-cpp
suites: openzfs-cpp-suite
17 changes: 17 additions & 0 deletions .github/codeql/custom-queries/cpp/redundantAssignment.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import cpp

from Assignment firstAssign, Assignment secondAssign
where
// The first assignment is from 'a' to 'b'
firstAssign.getLValue().(VariableAccess).getTarget() = secondAssign.getRValue().(VariableAccess).getTarget() and
firstAssign.getRValue().(VariableAccess).getTarget() = secondAssign.getLValue().(VariableAccess).getTarget() and
// Ensure 'a' and 'b' are not modified in between these assignments
not exists(Assignment anyAssign |
anyAssign.getEnclosingFunction() = firstAssign.getEnclosingFunction() and
anyAssign.getLocation().isBetween(firstAssign.getLocation(), secondAssign.getLocation()) and
(
anyAssign.getLValue().(VariableAccess).getTarget() = firstAssign.getLValue().(VariableAccess).getTarget() or
anyAssign.getLValue().(VariableAccess).getTarget() = firstAssign.getRValue().(VariableAccess).getTarget()
)
)
select secondAssign, "This assignment is redundant."
3 changes: 3 additions & 0 deletions .github/codeql/openzfs-code-scanning.qls
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Reusing existing QL Pack
- import: codeql-suites/cpp-code-scanning.qls
from: codeql-cpp
1 change: 1 addition & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ jobs:
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
config-file: .github/codeql-${{ matrix.language }}.yml
languages: ${{ matrix.language }}

- name: Autobuild

0 comments on commit 2b4f83c

Please sign in to comment.