-
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
Open
MichaelRFairhurst
wants to merge
3
commits into
main
Choose a base branch
from
michaelrfairhurst/implement-package-exceptions3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
cpp/common/src/codingstandards/cpp/exclusions/cpp/Exceptions3.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
import cpp | ||
import RuleMetadata | ||
import codingstandards.cpp.exclusions.RuleMetadata | ||
|
||
newtype Exceptions3Query = | ||
TMissingCatchAllExceptionHandlerInMainQuery() or | ||
TClassExceptionCaughtByValueQuery() or | ||
TExceptionUnfriendlyFunctionMustBeNoexceptQuery() | ||
|
||
predicate isExceptions3QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
query = | ||
// `Query` instance for the `missingCatchAllExceptionHandlerInMain` query | ||
Exceptions3Package::missingCatchAllExceptionHandlerInMainQuery() and | ||
queryId = | ||
// `@id` for the `missingCatchAllExceptionHandlerInMain` query | ||
"cpp/misra/missing-catch-all-exception-handler-in-main" and | ||
ruleId = "RULE-18-3-1" and | ||
category = "advisory" | ||
or | ||
query = | ||
// `Query` instance for the `classExceptionCaughtByValue` query | ||
Exceptions3Package::classExceptionCaughtByValueQuery() and | ||
queryId = | ||
// `@id` for the `classExceptionCaughtByValue` query | ||
"cpp/misra/class-exception-caught-by-value" and | ||
ruleId = "RULE-18-3-2" and | ||
category = "required" | ||
or | ||
query = | ||
// `Query` instance for the `exceptionUnfriendlyFunctionMustBeNoexcept` query | ||
Exceptions3Package::exceptionUnfriendlyFunctionMustBeNoexceptQuery() and | ||
queryId = | ||
// `@id` for the `exceptionUnfriendlyFunctionMustBeNoexcept` query | ||
"cpp/misra/exception-unfriendly-function-must-be-noexcept" and | ||
ruleId = "RULE-18-4-1" and | ||
category = "required" | ||
} | ||
|
||
module Exceptions3Package { | ||
Query missingCatchAllExceptionHandlerInMainQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `missingCatchAllExceptionHandlerInMain` query | ||
TQueryCPP(TExceptions3PackageQuery(TMissingCatchAllExceptionHandlerInMainQuery())) | ||
} | ||
|
||
Query classExceptionCaughtByValueQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `classExceptionCaughtByValue` query | ||
TQueryCPP(TExceptions3PackageQuery(TClassExceptionCaughtByValueQuery())) | ||
} | ||
|
||
Query exceptionUnfriendlyFunctionMustBeNoexceptQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `exceptionUnfriendlyFunctionMustBeNoexcept` query | ||
TQueryCPP(TExceptions3PackageQuery(TExceptionUnfriendlyFunctionMustBeNoexceptQuery())) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
cpp/misra/src/rules/RULE-18-3-1/MissingCatchAllExceptionHandlerInMain.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @id cpp/misra/missing-catch-all-exception-handler-in-main | ||
* @name RULE-18-3-1: There should be at least one exception handler to catch all otherwise unhandled exceptions | ||
* @description The main function should have a catch-all exception handler (catch(...)) to catch | ||
* all otherwise unhandled exceptions. | ||
* @kind problem | ||
* @precision high | ||
* @problem.severity warning | ||
* @tags external/misra/id/rule-18-3-1 | ||
* scope/single-translation-unit | ||
* maintainability | ||
* external/misra/enforcement/decidable | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.cpp.misra | ||
import codingstandards.cpp.exceptions.ExceptionFlow | ||
import codingstandards.cpp.exceptions.ExceptionSpecifications | ||
|
||
/** | ||
* A function call in main that is not declared noexcept and is not within a catch-all | ||
* exception handler (catch(...)). | ||
*/ | ||
class UncaughtFunctionCallInMain extends FunctionCall { | ||
UncaughtFunctionCallInMain() { | ||
getEnclosingFunction().hasName("main") and | ||
not isNoExceptTrue(getTarget()) and | ||
not exists(TryStmt try | | ||
try = getATryStmt(this.getEnclosingStmt()) and | ||
try.getCatchClause(_) instanceof CatchAnyBlock | ||
) | ||
} | ||
|
||
/** | ||
* We only want to report one counter-example indicating a missing catch(...), so this holds only | ||
* for the first one we find. | ||
*/ | ||
predicate isFirst() { | ||
this = | ||
rank[1](UncaughtFunctionCallInMain fc | | ||
any() | ||
| | ||
fc order by fc.getLocation().getStartLine(), fc.getLocation().getStartColumn() | ||
) | ||
} | ||
} | ||
|
||
from Function f, UncaughtFunctionCallInMain fc | ||
where | ||
not isExcluded(f, Exceptions3Package::missingCatchAllExceptionHandlerInMainQuery()) and | ||
f.getName() = "main" and | ||
fc.isFirst() | ||
select f, | ||
"Main function has a $@ which is not within a try block with a catch-all ('catch(...)') handler.", | ||
fc, "function call that may throw" |
25 changes: 25 additions & 0 deletions
25
cpp/misra/src/rules/RULE-18-3-2/ClassExceptionCaughtByValue.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @id cpp/misra/class-exception-caught-by-value | ||
* @name RULE-18-3-2: An exception of class type shall be caught by const reference or reference | ||
* @description Catching exception classes by value can lead to slicing, which can result in | ||
* unexpected behavior. | ||
* @kind problem | ||
* @precision very-high | ||
* @problem.severity error | ||
* @tags external/misra/id/rule-18-3-2 | ||
* scope/single-translation-unit | ||
* correctness | ||
* maintainability | ||
* external/misra/enforcement/decidable | ||
* external/misra/obligation/required | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.cpp.misra | ||
|
||
from CatchBlock catch, Type type | ||
where | ||
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." | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, usetype.toString()
ortype.getQualifiedName()
to ensure a clear textual representation.Copilot uses AI. Check for mistakes.