From a5d9d0157eb5293ca670935c69f56d23321f696d Mon Sep 17 00:00:00 2001 From: Richard Yao Date: Sun, 21 Jan 2024 12:26:22 -0500 Subject: [PATCH] Add CodeQL query to detect redundant assignments Signed-off-by: Richard Yao --- .github/codeql-cpp.yml | 4 ++++ .github/codeql-python.yml | 4 ++++ .github/codeql/custom-queries/cpp/qlpack.yml | 8 ++++++++ .../custom-queries/cpp/redundantAssignment.ql | 18 ++++++++++++++++++ .github/workflows/codeql.yml | 1 + 5 files changed, 35 insertions(+) create mode 100644 .github/codeql-cpp.yml create mode 100644 .github/codeql-python.yml create mode 100644 .github/codeql/custom-queries/cpp/qlpack.yml create mode 100644 .github/codeql/custom-queries/cpp/redundantAssignment.ql diff --git a/.github/codeql-cpp.yml b/.github/codeql-cpp.yml new file mode 100644 index 000000000000..41aeac1f83d0 --- /dev/null +++ b/.github/codeql-cpp.yml @@ -0,0 +1,4 @@ +name: "Custom CodeQL Analysis" + +queries: + - uses: ./.github/codeql/custom-queries/cpp/redundantAssignment.ql diff --git a/.github/codeql-python.yml b/.github/codeql-python.yml new file mode 100644 index 000000000000..93cb4a435ed9 --- /dev/null +++ b/.github/codeql-python.yml @@ -0,0 +1,4 @@ +name: "Custom CodeQL Analysis" + +paths-ignore: + - tests diff --git a/.github/codeql/custom-queries/cpp/qlpack.yml b/.github/codeql/custom-queries/cpp/qlpack.yml new file mode 100644 index 000000000000..65fee66ad075 --- /dev/null +++ b/.github/codeql/custom-queries/cpp/qlpack.yml @@ -0,0 +1,8 @@ +name: cpp-custom-queries +version: 0.0.1 +library: false +groups: + - cpp + - examples +dependencies: + codeql/cpp-all: ${workspace} diff --git a/.github/codeql/custom-queries/cpp/redundantAssignment.ql b/.github/codeql/custom-queries/cpp/redundantAssignment.ql new file mode 100644 index 000000000000..37876aa15012 --- /dev/null +++ b/.github/codeql/custom-queries/cpp/redundantAssignment.ql @@ -0,0 +1,18 @@ +import cpp + +from AssignExpr firstAssign, AssignExpr secondAssign +where + // The first assignment is from 'a' to 'b' + firstAssign.getDest().(VarAccess).getTarget() = secondAssign.getSource().(VarAccess).getTarget() and + firstAssign.getSource().(VarAccess).getTarget() = secondAssign.getDest().(VarAccess).getTarget() and + // Ensure 'a' and 'b' are not modified in between these assignments + not exists(Expr anyExpr | + anyExpr.getEnclosingFunction() = firstAssign.getEnclosingFunction() and + anyExpr.isBetween(firstAssign, secondAssign) and + ( + anyExpr.(AssignExpr).getDest().(VarAccess).getTarget() = firstAssign.getDest().(VarAccess).getTarget() or + anyExpr.(AssignExpr).getDest().(VarAccess).getTarget() = firstAssign.getSource().(VarAccess).getTarget() + ) + ) +select secondAssign, "This assignment is redundant." + diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 037f8aca0eaa..7ccfc1492564 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -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