From 48e7aa8b7f78860cc2650673588cdfef991ba0cd Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Sat, 25 Apr 2026 15:55:51 +0200 Subject: [PATCH 1/7] Fix perofmance `inappropriate-bitwise-or-shift-operands` --- .../src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql b/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql index e1f1e21069..f0992d2f0f 100644 --- a/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql +++ b/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql @@ -22,6 +22,8 @@ predicate isConstantExpression(Expr e) { e.isConstant() } +bindingset[right, leftType] +pragma[inline_late] predicate isValidShiftConstantRange(Expr right, Type leftType) { exists(int value | value = right.getValue().toInt() and From 06080b0ae47215ec4d0046badb19e27e35de34a1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 25 Apr 2026 14:13:42 +0000 Subject: [PATCH 2/7] Add change note for RULE-7-0-4 performance improvement Agent-Logs-Url: https://github.com/github/codeql-coding-standards/sessions/f476011c-d007-4a94-af05-f48e06932289 Co-authored-by: mbaluda <5237080+mbaluda@users.noreply.github.com> --- ...5-fix-performance-inappropriate-bitwise-or-shift-operands.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md diff --git a/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md new file mode 100644 index 0000000000..65ecd99f34 --- /dev/null +++ b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md @@ -0,0 +1,2 @@ +- `RULE-7-0-4` - `InappropriateBitwiseOrShiftOperands.ql`: + - Improved evaluation performance of the `isValidShiftConstantRange` predicate by adding `bindingset[right, leftType]` and `pragma[inline_late]` annotations to guide the CodeQL optimizer. From 48a06df95596bd097eb65c6f2ca92e4a67524925 Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Sat, 25 Apr 2026 16:55:00 +0200 Subject: [PATCH 3/7] Optimize performance of bitwise or shift operands rule Enhanced performance of the 'isValidShiftConstantRange' predicate by adding annotations for optimization. --- ...fix-performance-inappropriate-bitwise-or-shift-operands.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md index 65ecd99f34..3b5858a8f7 100644 --- a/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md +++ b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md @@ -1,2 +1,4 @@ +- `RULE-6-4-2` - `InheritedOverridableMemberFunction.ql`: + - Improved evaluation performance. - `RULE-7-0-4` - `InappropriateBitwiseOrShiftOperands.ql`: - - Improved evaluation performance of the `isValidShiftConstantRange` predicate by adding `bindingset[right, leftType]` and `pragma[inline_late]` annotations to guide the CodeQL optimizer. + - Improved evaluation performance. From e4897718eb934384e5f6d50c519d30fd68032801 Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Sat, 25 Apr 2026 16:59:01 +0200 Subject: [PATCH 4/7] Fix RULE-6-4-2 performance --- ...ddenInheritedOverridableMemberFunction.qll | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll b/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll index ef99e01973..a9c5e506fc 100644 --- a/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll +++ b/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll @@ -12,15 +12,24 @@ abstract class HiddenInheritedOverridableMemberFunctionSharedQuery extends Query Query getQuery() { result instanceof HiddenInheritedOverridableMemberFunctionSharedQuery } +class OverridingDeclaration extends FunctionDeclarationEntry { + OverridingDeclaration() { this.getDeclaration().hasDefinition() implies not this.isDefinition() } +} + +class HiddenDeclaration extends OverridingDeclaration { + HiddenDeclaration() { + // Check if we are overriding a virtual inherited member function + this.getDeclaration().isVirtual() and + // Exclude private member functions, which cannot be inherited. + not this.getDeclaration().(MemberFunction).isPrivate() + } +} + query predicate problems( - FunctionDeclarationEntry overridingDecl, string message, FunctionDeclarationEntry hiddenDecl, + OverridingDeclaration overridingDecl, string message, HiddenDeclaration hiddenDecl, string hiddenDecl_string ) { not isExcluded(overridingDecl, getQuery()) and - // Check if we are overriding a virtual inherited member function - hiddenDecl.getDeclaration().isVirtual() and - // Exclude private member functions, which cannot be inherited. - not hiddenDecl.getDeclaration().(MemberFunction).isPrivate() and // The overriding declaration hides the hidden declaration if: ( // 1. the overriding declaration overrides a function in a base class that is an overload of the hidden declaration @@ -46,9 +55,6 @@ query predicate problems( overridingDecl.getDeclaration().getDeclaringType().getABaseClass() = hiddenDecl.getDeclaration().getDeclaringType() ) and - // Limit the results to the declarations and not the definitions, if any. - (overridingDecl.getDeclaration().hasDefinition() implies not overridingDecl.isDefinition()) and - (hiddenDecl.getDeclaration().hasDefinition() implies not hiddenDecl.isDefinition()) and message = "Declaration for member '" + overridingDecl.getName() + "' hides overridable inherited member function $@" and From e7b9b670fa1bd0e03580bad36eaf772cb5365802 Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Sat, 25 Apr 2026 18:58:39 +0200 Subject: [PATCH 5/7] RULE-7-0-4: exclude `insertion operator` --- ...ormance-inappropriate-bitwise-or-shift-operands.md | 1 + .../RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md index 3b5858a8f7..0b9a82c48a 100644 --- a/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md +++ b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md @@ -2,3 +2,4 @@ - Improved evaluation performance. - `RULE-7-0-4` - `InappropriateBitwiseOrShiftOperands.ql`: - Improved evaluation performance. + - Remove false positives related to the `insertion operator`. diff --git a/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql b/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql index f0992d2f0f..9e1321134b 100644 --- a/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql +++ b/cpp/misra/src/rules/RULE-7-0-4/InappropriateBitwiseOrShiftOperands.ql @@ -24,11 +24,11 @@ predicate isConstantExpression(Expr e) { bindingset[right, leftType] pragma[inline_late] -predicate isValidShiftConstantRange(Expr right, Type leftType) { +predicate isValidShiftConstantRange(Expr right, MisraCpp23BuiltInTypes::NumericType leftType) { exists(int value | value = right.getValue().toInt() and value >= 0 and - value < leftType.getSize() * 8 + value < leftType.getBuiltInSize() * 8 ) } @@ -99,7 +99,10 @@ where ) or // Shift operators - right operand must be unsigned or constant in valid range - exists(BinaryShiftOpOrAssignOp shift, Expr right, Type rightType, Type leftType | + exists( + BinaryShiftOpOrAssignOp shift, Expr right, Type rightType, + MisraCpp23BuiltInTypes::NumericType leftType + | right = shift.getRightOperand() and x = right and rightType = right.getExplicitlyConverted().getType() and @@ -110,7 +113,7 @@ where not isValidShiftConstantRange(right, leftType) and message = "Shift operator '" + shift.getOperator() + "' shifts by " + right.getValue().toInt() + - " which is not within the valid range 0.." + ((leftType.getSize() * 8) - 1) + "." + " which is not within the valid range 0.." + ((leftType.getBuiltInSize() * 8) - 1) + "." else ( not MisraCpp23BuiltInTypes::isUnsignedType(rightType) and message = From e2b7fe23f2e9ffbabb3f59ad49ea513ef994dd5f Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Sat, 25 Apr 2026 19:01:19 +0200 Subject: [PATCH 6/7] Make classes private --- .../HiddenInheritedOverridableMemberFunction.qll | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll b/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll index a9c5e506fc..3604c4756c 100644 --- a/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll +++ b/cpp/common/src/codingstandards/cpp/rules/hiddeninheritedoverridablememberfunction/HiddenInheritedOverridableMemberFunction.qll @@ -12,11 +12,11 @@ abstract class HiddenInheritedOverridableMemberFunctionSharedQuery extends Query Query getQuery() { result instanceof HiddenInheritedOverridableMemberFunctionSharedQuery } -class OverridingDeclaration extends FunctionDeclarationEntry { +private class OverridingDeclaration extends FunctionDeclarationEntry { OverridingDeclaration() { this.getDeclaration().hasDefinition() implies not this.isDefinition() } } -class HiddenDeclaration extends OverridingDeclaration { +private class HiddenDeclaration extends OverridingDeclaration { HiddenDeclaration() { // Check if we are overriding a virtual inherited member function this.getDeclaration().isVirtual() and From 6b3a97dd9e620cd6a789906fb98c073ff2714623 Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Sun, 26 Apr 2026 11:59:22 +0200 Subject: [PATCH 7/7] release notes --- ...5-fix-performance-inappropriate-bitwise-or-shift-operands.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md index 0b9a82c48a..3b7fee651f 100644 --- a/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md +++ b/change_notes/2026-04-25-fix-performance-inappropriate-bitwise-or-shift-operands.md @@ -2,4 +2,4 @@ - Improved evaluation performance. - `RULE-7-0-4` - `InappropriateBitwiseOrShiftOperands.ql`: - Improved evaluation performance. - - Remove false positives related to the `insertion operator`. + - Removed false positives related to the `insertion operator`.