- Notifications
You must be signed in to change notification settings - Fork67
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 ourterms of service andprivacy 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?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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())) | ||
} | ||
} |
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" |
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." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [nitpick] When concatenating the Suggested change
Copilot uses AI. Check for mistakes. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.