-
Notifications
You must be signed in to change notification settings - Fork 67
Implement package exceptions3 for MISRA C++ #901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Implement package exceptions3 for MISRA C++ #901
Conversation
Adds support for: - RULE-18-3-1 - RULE-18-3-2 - RULE-18-4-1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a new “Exceptions3” package for MISRA C++ exception rules, adding three new rules (18-3-1, 18-3-2, 18-4-1) along with tests, query implementations, and metadata updates.
- Added unit tests (.cpp, .qlref, .expected) covering four variants of rule 18-3-1.
- Introduced CodeQL queries for RULE-18-3-1 (missing catch-all in main), RULE-18-3-2 (class exceptions caught by value), and RULE-18-4-1 (exception-unfriendly functions must be noexcept).
- Updated exclusion metadata (RuleMetadata.qll, Exceptions3.qll) and supporting QLL libraries (ExceptionFlow, Function, SpecialFunctionExceptions).
Reviewed Changes
Copilot reviewed 33 out of 33 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
cpp/misra/test/rules/RULE-18-3-1.4/test_except_inside_catch.cpp | Add NON-COMPLIANT test where cout in catch-all may throw |
cpp/misra/test/rules/RULE-18-3-1.4/MissingCatchAllExceptionHandlerInMain.qlref | Reference the new 18-3-1.4 test for the missing-catch rule |
cpp/misra/test/rules/RULE-18-3-1.4/MissingCatchAllExceptionHandlerInMain.expected | Add expected alert for test_except_inside_catch.cpp |
cpp/misra/test/rules/RULE-18-3-1.3/test_except_after_catch.cpp | Add NON-COMPLIANT test where f() after catch may throw |
cpp/misra/test/rules/RULE-18-3-1.3/MissingCatchAllExceptionHandlerInMain.qlref | Reference the new 18-3-1.3 test for the missing-catch rule |
cpp/misra/test/rules/RULE-18-3-1.3/MissingCatchAllExceptionHandlerInMain.expected | Add expected alert for test_except_after_catch.cpp |
cpp/misra/test/rules/RULE-18-3-1.2/test_except_before_catch.cpp | Add NON-COMPLIANT test where f() before catch may throw |
cpp/misra/test/rules/RULE-18-3-1.2/MissingCatchAllExceptionHandlerInMain.qlref | Reference the new 18-3-1.2 test for the missing-catch rule |
cpp/misra/test/rules/RULE-18-3-1.2/MissingCatchAllExceptionHandlerInMain.expected | Add expected alert for test_except_before_catch.cpp |
cpp/misra/test/rules/RULE-18-3-1.1/test_missing.cpp | Add NON-COMPLIANT test with no catch-all handler |
cpp/misra/test/rules/RULE-18-3-1.1/MissingCatchAllExceptionHandlerInMain.qlref | Reference the new 18-3-1.1 test for the missing-catch rule |
cpp/misra/test/rules/RULE-18-3-1.1/MissingCatchAllExceptionHandlerInMain.expected | Add expected alert for test_missing.cpp |
cpp/misra/src/rules/RULE-18-4-1/ExceptionUnfriendlyFunctionMustBeNoexcept.ql | Implement query for exception-unfriendly functions needing noexcept |
cpp/misra/src/rules/RULE-18-3-2/ClassExceptionCaughtByValue.ql | Implement query to enforce catching classes by (const) reference |
cpp/misra/src/rules/RULE-18-3-1/MissingCatchAllExceptionHandlerInMain.ql | Implement catch-all-in-main query with custom predicate |
cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll | Import new Exceptions3 package in exclusion metadata |
cpp/common/src/codingstandards/cpp/exclusions/cpp/Exceptions3.qll | Auto-generate Exceptions3Query type and metadata checks |
cpp/common/src/codingstandards/cpp/exceptions/ExceptionFlow.qll | Add helper getATryStmt to locate enclosing try blocks |
cpp/common/src/codingstandards/cpp/exceptions/Function.qll | Add UserDeclaredFunction and function-access-like classes |
cpp/common/src/codingstandards/cpp/exceptions/SpecialFunctionExceptions.qll | Extend special-function exception descriptions and contexts |
Comments suppressed due to low confidence (2)
cpp/common/src/codingstandards/cpp/exceptions/SpecialFunctionExceptions.qll:18
- [nitpick] Remove or consolidate the redundant import of
ExceptionFlow
, since it’s also imported with a fully-qualified path.
private import ExceptionFlow
cpp/common/src/codingstandards/cpp/exceptions/SpecialFunctionExceptions.qll:107
- [nitpick] Adjust indentation of the closing brace to align with the opening brace for consistency.
}
/** | ||
* @id cpp/misra/exception-unfriendly-function-must-be-noexcept | ||
* @name RULE-18-4-1: Exception-unfriendly functions shall be noexcept | ||
* @description Throwing exceptions in constructors, descructors, copy-constructors, move |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typo: change 'descructors' to 'destructors' in the rule description.
* @description Throwing exceptions in constructors, descructors, copy-constructors, move | |
* @description Throwing exceptions in constructors, destructors, copy-constructors, move |
Copilot uses AI. Check for mistakes.
@@ -121,6 +121,11 @@ TryStmt getNearestTry(Stmt s) { | |||
else result = getNearestTry(s.getParent()) | |||
} | |||
|
|||
/** Gets a try statement that contains the given statement. */ | |||
TryStmt getATryStmt(Stmt s) { | |||
result.getStmt() = s.getEnclosingStmt().getEnclosingBlock*() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The getATryStmt
predicate may match any enclosing block rather than specifically a TryStmt
. Consider modeling it after getNearestTry
to ensure it only returns actual TryStmt
nodes.
result.getStmt() = s.getEnclosingStmt().getEnclosingBlock*() | |
result = s.getEnclosingStmt().getEnclosingBlock*() and result instanceof TryStmt |
Copilot uses AI. Check for mistakes.
not isExcluded(catch, Exceptions3Package::classExceptionCaughtByValueQuery()) and | ||
type = catch.getParameter().getType() and | ||
type.stripTopLevelSpecifiers() instanceof Class | ||
select catch, "Catch block catches a class type '" + type + "' by value, which can lead to slicing." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] When concatenating the type
in the select message, use type.toString()
or type.getQualifiedName()
to ensure a clear textual representation.
select catch, "Catch block catches a class type '" + type + "' by value, which can lead to slicing." | |
select catch, "Catch block catches a class type '" + type.getQualifiedName() + "' by value, which can lead to slicing." |
Copilot uses AI. Check for mistakes.
Description
Misra C++ exceptions rules
Change request type
.ql
,.qll
,.qls
or unit tests)Rules with added or modified queries
Release change checklist
A change note (development_handbook.md#change-notes) is required for any pull request which modifies:
If you are only adding new rule queries, a change note is not required.
Author: Is a change note required?
🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.
Reviewer: Confirm that either a change note is not required or the change note is required and has been added.
Query development review checklist
For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:
Author
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
Reviewer
As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.