-
Notifications
You must be signed in to change notification settings - Fork 77
Implement package SideEffects6 with rule 28.3.1, predicate should not have side effects #1051
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
Merged
MichaelRFairhurst
merged 14 commits into
main
from
michaelrfairhurst/side-effects-6-rule-28-3-1-predicate-side-effects
Mar 7, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f381feb
Initial draft for non-const function objects.
MichaelRFairhurst 8dba813
Next query first pass working
MichaelRFairhurst 1b83422
Add portability as an allowed tag
MichaelRFairhurst dd8135c
Add comments to new shared APIs.
MichaelRFairhurst 372fd8f
Fix stubs to add std::set back, and std::less
MichaelRFairhurst 8ec6c97
Fix OrderPredicateMustBeStrictlyWeak test compilation
MichaelRFairhurst 1663d24
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/sid…
MichaelRFairhurst bb3b9ef
Changes to std::map, hopefully fix test extractor errors
MichaelRFairhurst 81cbd29
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/sid…
MichaelRFairhurst 9536e50
copilot feedback
MichaelRFairhurst c794c0e
Merge branch 'main' into michaelrfairhurst/side-effects-6-rule-28-3-1…
mbaluda 279a8c6
Update cpp/misra/test/rules/RULE-28-3-1/PredicateWithPersistentSideEf…
mbaluda 5bed5c2
Update cpp/misra/src/rules/RULE-28-3-1/PredicateWithPersistentSideEff…
mbaluda bd18210
Merge remote-tracking branch 'origin/main' into michaelrfairhurst/sid…
MichaelRFairhurst 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import cpp | ||
|
|
||
| /** | ||
| * A predicate to simplify getting a namespace for a template parameter, since | ||
| * `TemplateParameterType`'s `getNamespace()` has no result, and `enclosingElement()` has no result, | ||
| * and there are multiple cases to navigate to work around this. | ||
| */ | ||
| Namespace getTemplateParameterNamespace(TypeTemplateParameter param) { | ||
| exists(Declaration decl | | ||
| param = decl.(TemplateClass).getATemplateArgument() or | ||
| param = decl.(TemplateVariable).getATemplateArgument() or | ||
| param = decl.(TemplateFunction).getATemplateArgument() | ||
| | | ||
| result = decl.getNamespace() | ||
| ) | ||
| } |
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
44 changes: 44 additions & 0 deletions
44
cpp/common/src/codingstandards/cpp/exclusions/cpp/SideEffects6.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,44 @@ | ||
| //** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
| import cpp | ||
| import RuleMetadata | ||
| import codingstandards.cpp.exclusions.RuleMetadata | ||
|
|
||
| newtype SideEffects6Query = | ||
| TPredicateWithPersistentSideEffectsQuery() or | ||
| TNonConstPredicateFunctionObjectQuery() | ||
|
|
||
| predicate isSideEffects6QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `predicateWithPersistentSideEffects` query | ||
| SideEffects6Package::predicateWithPersistentSideEffectsQuery() and | ||
| queryId = | ||
| // `@id` for the `predicateWithPersistentSideEffects` query | ||
| "cpp/misra/predicate-with-persistent-side-effects" and | ||
| ruleId = "RULE-28-3-1" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `nonConstPredicateFunctionObject` query | ||
| SideEffects6Package::nonConstPredicateFunctionObjectQuery() and | ||
| queryId = | ||
| // `@id` for the `nonConstPredicateFunctionObject` query | ||
| "cpp/misra/non-const-predicate-function-object" and | ||
| ruleId = "RULE-28-3-1" and | ||
| category = "required" | ||
| } | ||
|
|
||
| module SideEffects6Package { | ||
| Query predicateWithPersistentSideEffectsQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `predicateWithPersistentSideEffects` query | ||
| TQueryCPP(TSideEffects6PackageQuery(TPredicateWithPersistentSideEffectsQuery())) | ||
| } | ||
|
|
||
| Query nonConstPredicateFunctionObjectQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `nonConstPredicateFunctionObject` query | ||
| TQueryCPP(TSideEffects6PackageQuery(TNonConstPredicateFunctionObjectQuery())) | ||
| } | ||
| } |
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,118 @@ | ||
| /** | ||
| * A library for handling "predicate" types, which are function parameters in the standard library. | ||
| * | ||
| * For example, `std::sort` takes a predicate as its third argument, and `std::set` takes a | ||
| * predicate as its second template parameter. Predicates are always template parameters, and we | ||
| * can identify them by their name -- this is what MISRA expects us to do. | ||
| */ | ||
|
|
||
| import cpp | ||
| private import codingstandards.cpp.StdNamespace | ||
| private import codingstandards.cpp.ast.Templates | ||
| private import codingstandards.cpp.types.Templates | ||
| private import semmle.code.cpp.dataflow.new.DataFlow | ||
|
|
||
| /** | ||
| * A "predicate" type parameter as defined by MISRA, which is a standard library function named | ||
| * "Compare" or "Predicate" or "BinaryPredicate" in the standard library. | ||
| * | ||
| * To be more widely useful, we match more flexibly on the name, as `_Compare` is also common, etc. | ||
| * | ||
| * To get a particular `Type` that is _used_ as a predicate type, rather than the type parameter | ||
| * itself, see `getASubstitutedType()`. | ||
| */ | ||
| class PredicateType extends TypeTemplateParameter { | ||
| PredicateType() { | ||
| this.getName().matches(["%Compare%", "%Predicate%"]) and | ||
| getTemplateParameterNamespace(this) instanceof StdNS | ||
| } | ||
|
|
||
| /** | ||
| * Get a type that is used (anywhere, via some substitution) as this predicate type parameter. | ||
| * | ||
| * For example, `std::sort(..., ..., [](int a, int b) { return a < b; })` creates a `Closure` | ||
| * type, and substitutes that closure type for the predicate type parameter of `std::sort`. | ||
| */ | ||
| Type getASubstitutedType(Substitution sub) { result = sub.getSubstitutedTypeForParameter(this) } | ||
| } | ||
|
|
||
| /** | ||
| * A class type that has been substituted for a predicate type parameter, and has an `operator()` | ||
| * member function. | ||
| * | ||
| * For example, the closure type in `std::sort(..., ..., [](int a, int b) { return a < b; })` is a | ||
| * `PredicateFunctionObject`, and so is any `std::less<int>` that is used as a predicate type | ||
| * parameter, etc. | ||
| * | ||
| * This does not cover function pointer types, as these are not class types. | ||
| */ | ||
| class PredicateFunctionObject extends Class { | ||
| PredicateType pred; | ||
| Function operator; | ||
| Substitution sub; | ||
|
|
||
| PredicateFunctionObject() { | ||
| this = pred.getASubstitutedType(sub) and | ||
| operator.getDeclaringType() = this and | ||
| operator.getName() = "operator()" | ||
| } | ||
|
|
||
| /** | ||
| * Get the predicate type parameter that this function object is being substituted for. | ||
| */ | ||
| PredicateType getPredicateType() { result = pred } | ||
|
|
||
| /** | ||
| * Get the `operator()` function that this function object defines. This is the function that will | ||
| * be invoked and essentially defines the predicate behavior. | ||
| */ | ||
| Function getCallOperator() { result = operator } | ||
|
|
||
| /** | ||
| * Get the `Substitution` object that makes this type a `PredicateFunctionObject`. | ||
| * | ||
| * This is a particular instantiation of some template that contains a predicate type parameter, | ||
| * which is substituted by this type in that instantiation. The `Substitution` object may refer | ||
| * to a `TemplateClass`, `TemplateVariable`, or `TemplateFunction`. | ||
| */ | ||
| Substitution getSubstitution() { result = sub } | ||
| } | ||
|
|
||
| /** | ||
| * Gets a function access where the purpose of that access is to pass the accessed function as a | ||
| * predicate argument to a standard library template. | ||
| * | ||
| * For example, in `std::sort(..., ..., myCompare)`, where `myCompare` is a function, then | ||
| * `myCompare` will be converted into a function pointer and that function pointer will be used as | ||
| * a predicate in that `std::sort` call. | ||
| * | ||
| * This is more complex to identify than `PredicateFunctionObject` because the addressee of the | ||
| * function pointer is not necessarily statically known. | ||
| */ | ||
| class PredicateFunctionPointerUse extends FunctionAccess { | ||
| Expr functionPointerArgument; | ||
| FunctionCall templateFunctionCall; | ||
| FunctionTemplateInstantiation instantiation; | ||
| Substitution sub; | ||
| PredicateType pred; | ||
| Parameter parameter; | ||
| int index; | ||
|
|
||
| PredicateFunctionPointerUse() { | ||
| functionPointerArgument = templateFunctionCall.getArgument(index) and | ||
| templateFunctionCall.getTarget() = instantiation and | ||
| parameter = instantiation.getParameter(index) and | ||
| sub.asFunctionSubstitution() = instantiation and | ||
| parameter.getType() = sub.getSubstitutedTypeForParameter(pred) and | ||
| exists(DataFlow::Node func, DataFlow::Node arg | | ||
| func.asExpr() = this and | ||
| arg.asExpr() = functionPointerArgument and | ||
| DataFlow::localFlow(func, arg) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Get the predicate type parameter that this function pointer is being substituted for. | ||
| */ | ||
| PredicateType getPredicateType() { result = pred } | ||
| } | ||
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,80 @@ | ||
| import cpp | ||
|
|
||
| private newtype TSubstitution = | ||
| TClassSubstitution(ClassTemplateInstantiation cti) or | ||
| TFunctionSubstitution(FunctionTemplateInstantiation fti) or | ||
| TVariableSubstitution(VariableTemplateInstantiation vti) | ||
|
|
||
| /** | ||
| * A class to facilitate working with template substitutions, especially since templates may be a | ||
| * `TemplateClass`, `TemplateFunction`, or `TemplateVariable`, which complicates their usage. | ||
| * | ||
| * A `Substitution` in particular refers to an instantiation of that template of some kind, and | ||
| * allows analysis of which parameters were substituted with which types in that instantiation. | ||
| */ | ||
| class Substitution extends TSubstitution { | ||
| ClassTemplateInstantiation asClassSubstitution() { this = TClassSubstitution(result) } | ||
|
|
||
| FunctionTemplateInstantiation asFunctionSubstitution() { this = TFunctionSubstitution(result) } | ||
|
|
||
| VariableTemplateInstantiation asVariableSubstitution() { this = TVariableSubstitution(result) } | ||
|
|
||
| /** | ||
| * Get the nth template parameter type of the template that is being substituted. | ||
| * | ||
| * For example, in `std::vector<int>`, the 0th template parameter is `typename T`. | ||
| */ | ||
| TypeTemplateParameter getTemplateParameter(int index) { | ||
| result = this.asClassSubstitution().getTemplate().getTemplateArgument(index) or | ||
| result = this.asFunctionSubstitution().getTemplate().getTemplateArgument(index) or | ||
| result = this.asVariableSubstitution().getTemplate().getTemplateArgument(index) | ||
| } | ||
|
|
||
| /** | ||
| * Get the type that is substituting the nth template parameter in this substitution. | ||
| * | ||
| * For example, in `std::vector<int>`, the 0th substituted type is `int`. | ||
| */ | ||
| Type getSubstitutedType(int index) { | ||
| result = this.asClassSubstitution().getTemplateArgument(index) or | ||
| result = this.asFunctionSubstitution().getTemplateArgument(index) or | ||
| result = this.asVariableSubstitution().getTemplateArgument(index) | ||
| } | ||
|
|
||
| /** | ||
| * Get the type that is substituting the given template parameter in this substitution. | ||
| * | ||
| * For example, in `std::vector<int>`, this predicate matches the given type `int` with the type | ||
| * parameter `typename T`. | ||
| */ | ||
| Type getSubstitutedTypeForParameter(TypeTemplateParameter param) { | ||
| exists(int idx | | ||
| this.getTemplateParameter(idx) = param and | ||
| result = this.getSubstitutedType(idx) | ||
| ) | ||
| } | ||
|
|
||
| string toString() { | ||
| result = this.asClassSubstitution().toString() or | ||
| result = this.asFunctionSubstitution().toString() or | ||
| result = this.asVariableSubstitution().toString() | ||
| } | ||
|
|
||
| /** | ||
| * Get a `Locatable` that represents where this substitution was declared in the source code. | ||
| * | ||
| * The result may be a `TypeMention`, `Call`, etc. depending on the kind of template and how it is | ||
| * being used, but it handles the various template cases for you. | ||
| * | ||
| * Note that this instantiation may have been declared in multiple places. | ||
| */ | ||
| Locatable getASubstitutionSite() { | ||
| result.(TypeMention).getMentionedType() = this.asClassSubstitution() | ||
| or | ||
| result.(Call).getTarget() = this.asFunctionSubstitution() | ||
| or | ||
| result.(FunctionAccess).getTarget() = this.asFunctionSubstitution() | ||
| or | ||
| result.(VariableAccess).getTarget() = this.asVariableSubstitution() | ||
| } | ||
| } | ||
MichaelRFairhurst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.