-
Notifications
You must be signed in to change notification settings - Fork 77
Declarations2 cpp misra 2023 #1097
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
knewbury01
merged 7 commits into
github:main
from
knewbury01:knewbury01/cpp-misra2023-declarations2
Apr 2, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9ee2c07
Add rule RULE-6-2-2 and shared with DCL40-C
knewbury01 4b23b50
Add RULE-6-7-1
knewbury01 31b1c54
Merge branch 'main' into knewbury01/cpp-misra2023-declarations2
knewbury01 4abe23f
Fix problem tag on DCL40-C
knewbury01 8eb98c0
Fix testcases incompatiblefunctiondeclaration etc to match cases needed
knewbury01 1e9c547
Address review comments
knewbury01 c239e24
Add pragma to equal types to improve perf
knewbury01 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
4 changes: 0 additions & 4 deletions
4
c/cert/test/rules/DCL40-C/IncompatibleFunctionDeclarations.expected
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
c/cert/test/rules/DCL40-C/IncompatibleFunctionDeclarations.qlref
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
c/cert/test/rules/DCL40-C/IncompatibleFunctionDeclarations.testref
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 @@ | ||
| c/common/test/rules/incompatiblefunctiondeclaration/IncompatibleFunctionDeclaration.ql |
2 changes: 0 additions & 2 deletions
2
c/cert/test/rules/DCL40-C/IncompatibleObjectDeclarations.expected
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
c/cert/test/rules/DCL40-C/IncompatibleObjectDeclarations.testref
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 @@ | ||
| c/common/test/rules/incompatibleobjectdeclaration/IncompatibleObjectDeclaration.ql |
4 changes: 4 additions & 0 deletions
4
c/common/test/rules/incompatiblefunctiondeclaration/IncompatibleFunctionDeclaration.expected
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,4 @@ | ||
| | test1.c:1:12:1:12 | declaration of f | The object is not compatible with a re-declaration $@. | test.c:1:6:1:6 | definition of f | f | | ||
| | test.c:1:6:1:6 | definition of f | The object is not compatible with a re-declaration $@. | test1.c:1:12:1:12 | declaration of f | f | | ||
| | test.c:5:6:5:7 | declaration of f1 | The object is not compatible with a re-declaration $@. | test1.c:2:13:2:14 | declaration of f1 | f1 | | ||
| | test.c:6:6:6:7 | definition of f2 | The object is not compatible with a re-declaration $@. | test1.c:3:6:3:7 | definition of f2 | f2 | |
8 changes: 8 additions & 0 deletions
8
c/common/test/rules/incompatiblefunctiondeclaration/IncompatibleFunctionDeclaration.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,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.incompatiblefunctiondeclaration.IncompatibleFunctionDeclaration | ||
|
|
||
| module TestFileConfig implements IncompatibleFunctionDeclarationConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import IncompatibleFunctionDeclaration<TestFileConfig> |
3 changes: 0 additions & 3 deletions
3
c/cert/test/rules/DCL40-C/test.c → ...es/incompatiblefunctiondeclaration/test.c
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
3 changes: 0 additions & 3 deletions
3
c/cert/test/rules/DCL40-C/test1.c → ...s/incompatiblefunctiondeclaration/test1.c
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
2 changes: 2 additions & 0 deletions
2
c/common/test/rules/incompatibleobjectdeclaration/IncompatibleObjectDeclaration.expected
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,2 @@ | ||
| | test1.c:2:12:2:12 | declaration of i | The object is not compatible with a re-declaration $@. | test.c:1:7:1:7 | definition of i | i | | ||
| | test.c:2:5:2:5 | definition of a | The object is not compatible with a re-declaration $@. | test1.c:1:13:1:13 | declaration of a | a | |
8 changes: 8 additions & 0 deletions
8
c/common/test/rules/incompatibleobjectdeclaration/IncompatibleObjectDeclaration.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,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.incompatibleobjectdeclaration.IncompatibleObjectDeclaration | ||
|
|
||
| module TestFileConfig implements IncompatibleObjectDeclarationConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import IncompatibleObjectDeclaration<TestFileConfig> |
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,2 @@ | ||
| short i; // NON_COMPLIANT | ||
| int a[] = {1, 2, 3, 4}; // NON_COMPLIANT | ||
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,2 @@ | ||
| extern int *a; // NON_COMPLIANT | ||
knewbury01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| extern int i; // NON_COMPLIANT | ||
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,2 @@ | ||
| - `DCL40-C` - `IncompatibleObjectDeclarations.ql`: | ||
| - The rule now is capable to detect equivalence based on compatible types instead of exact types (which was what it was previously checking on objects). This will eliminate false positives for some array types and types using equivalent typedefs. |
61 changes: 61 additions & 0 deletions
61
cpp/common/src/codingstandards/cpp/exclusions/cpp/Declarations2.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 Declarations2Query = | ||
| TIncompatibleObjectDeclarationsCppQuery() or | ||
| TIncompatibleFunctionDeclarationsCppQuery() or | ||
| TLocalVariableStaticStorageDurationQuery() | ||
|
|
||
| predicate isDeclarations2QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `incompatibleObjectDeclarationsCpp` query | ||
| Declarations2Package::incompatibleObjectDeclarationsCppQuery() and | ||
| queryId = | ||
| // `@id` for the `incompatibleObjectDeclarationsCpp` query | ||
| "cpp/misra/incompatible-object-declarations-cpp" and | ||
| ruleId = "RULE-6-2-2" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `incompatibleFunctionDeclarationsCpp` query | ||
| Declarations2Package::incompatibleFunctionDeclarationsCppQuery() and | ||
| queryId = | ||
| // `@id` for the `incompatibleFunctionDeclarationsCpp` query | ||
| "cpp/misra/incompatible-function-declarations-cpp" and | ||
| ruleId = "RULE-6-2-2" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `localVariableStaticStorageDuration` query | ||
| Declarations2Package::localVariableStaticStorageDurationQuery() and | ||
| queryId = | ||
| // `@id` for the `localVariableStaticStorageDuration` query | ||
| "cpp/misra/local-variable-static-storage-duration" and | ||
| ruleId = "RULE-6-7-1" and | ||
| category = "required" | ||
| } | ||
|
|
||
| module Declarations2Package { | ||
| Query incompatibleObjectDeclarationsCppQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `incompatibleObjectDeclarationsCpp` query | ||
| TQueryCPP(TDeclarations2PackageQuery(TIncompatibleObjectDeclarationsCppQuery())) | ||
| } | ||
|
|
||
| Query incompatibleFunctionDeclarationsCppQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `incompatibleFunctionDeclarationsCpp` query | ||
| TQueryCPP(TDeclarations2PackageQuery(TIncompatibleFunctionDeclarationsCppQuery())) | ||
| } | ||
|
|
||
| Query localVariableStaticStorageDurationQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `localVariableStaticStorageDuration` query | ||
| TQueryCPP(TDeclarations2PackageQuery(TLocalVariableStaticStorageDurationQuery())) | ||
| } | ||
| } |
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
62 changes: 62 additions & 0 deletions
62
...ngstandards/cpp/rules/incompatiblefunctiondeclaration/IncompatibleFunctionDeclaration.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,62 @@ | ||
| /** | ||
| * Provides a configurable module IncompatibleFunctionDeclaration with a `problems` predicate | ||
| * for the following issue: | ||
| * Declaring incompatible functions, in other words same named function of different | ||
| * return types or with different numbers of parameters or parameter types, then | ||
| * accessing those functions can lead to undefined behaviour. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.Identifiers | ||
| import codingstandards.cpp.types.Compatible | ||
|
|
||
| predicate interestedInFunctions( | ||
| FunctionDeclarationEntry f1, FunctionDeclarationEntry f2, ExternalIdentifiers d1, | ||
| ExternalIdentifiers d2 | ||
| ) { | ||
| not f1 = f2 and | ||
knewbury01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| d1 = f1.getDeclaration() and | ||
| d2 = f2.getDeclaration() and | ||
| f1.getName() = f2.getName() | ||
| } | ||
|
|
||
| predicate interestedInFunctions(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) { | ||
| interestedInFunctions(f1, f2, _, _) | ||
| } | ||
|
|
||
| module FuncDeclEquiv = | ||
| FunctionDeclarationTypeEquivalence<TypesCompatibleConfig, interestedInFunctions/2>; | ||
|
|
||
| signature module IncompatibleFunctionDeclarationConfigSig { | ||
| Query getQuery(); | ||
| } | ||
|
|
||
| module IncompatibleFunctionDeclaration<IncompatibleFunctionDeclarationConfigSig Config> { | ||
| query predicate problems( | ||
| FunctionDeclarationEntry f1, string message, FunctionDeclarationEntry f2, string secondMessage | ||
| ) { | ||
| exists(ExternalIdentifiers d1, ExternalIdentifiers d2 | | ||
| not isExcluded(f1, Config::getQuery()) and | ||
| not isExcluded(f2, Config::getQuery()) and | ||
| interestedInFunctions(f1, f2, d1, d2) and | ||
| ( | ||
| //return type check | ||
| not FuncDeclEquiv::equalReturnTypes(f1, f2) | ||
| or | ||
| //parameter type check | ||
| not FuncDeclEquiv::equalParameterTypes(f1, f2) | ||
| ) and | ||
| // Apply ordering on start line, trying to avoid the optimiser applying this join too early | ||
| // in the pipeline | ||
| exists(int f1Line, int f2Line | | ||
| f1.getLocation().hasLocationInfo(_, f1Line, _, _, _) and | ||
| f2.getLocation().hasLocationInfo(_, f2Line, _, _, _) and | ||
| f1Line >= f2Line | ||
| ) and | ||
| secondMessage = f2.getName() and | ||
| message = "The object is not compatible with a re-declaration $@." | ||
| ) | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
...codingstandards/cpp/rules/incompatibleobjectdeclaration/IncompatibleObjectDeclaration.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,49 @@ | ||
| /** | ||
| * Provides a configurable module IncompatibleObjectDeclaration with a `problems` predicate | ||
| * for the following issue: | ||
| * Declaring incompatible objects, in other words same named objects of different | ||
| * types, then accessing those objects can lead to undefined behavior. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.Identifiers | ||
| import codingstandards.cpp.types.Compatible | ||
|
|
||
| signature module IncompatibleObjectDeclarationConfigSig { | ||
| Query getQuery(); | ||
| } | ||
|
|
||
| class VariableDeclarationEntryExternal extends VariableDeclarationEntry { | ||
| VariableDeclarationEntryExternal() { this.getDeclaration() instanceof ExternalIdentifiers } | ||
| } | ||
|
|
||
| predicate relevantPair(VariableDeclarationEntryExternal varA, VariableDeclarationEntryExternal varB) { | ||
| not varA = varB and | ||
| varA.getVariable().getName() = varB.getVariable().getName() | ||
| } | ||
|
|
||
| predicate relevantTypes(Type a, Type b) { | ||
| exists(VariableDeclarationEntryExternal varA, VariableDeclarationEntryExternal varB | | ||
| relevantPair(varA, varB) and | ||
| a = varA.getType() and | ||
| b = varB.getType() | ||
| ) | ||
| } | ||
|
|
||
| module IncompatibleObjectDeclaration<IncompatibleObjectDeclarationConfigSig Config> { | ||
| query predicate problems( | ||
| VariableDeclarationEntryExternal decl1, string message, VariableDeclarationEntryExternal decl2, | ||
| string secondMessage | ||
| ) { | ||
| not isExcluded(decl1, Config::getQuery()) and | ||
| not isExcluded(decl2, Config::getQuery()) and | ||
| relevantPair(decl1, decl2) and | ||
| not TypeEquivalence<TypesCompatibleConfig, relevantTypes/2>::equalTypes(decl1.getType(), | ||
| decl2.getType()) and | ||
| decl1.getLocation().getStartLine() >= decl2.getLocation().getStartLine() and | ||
| secondMessage = decl2.getName() and | ||
| message = "The object is not compatible with a re-declaration $@." | ||
| } | ||
| } |
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
5 changes: 5 additions & 0 deletions
5
...ommon/test/rules/incompatiblefunctiondeclaration/IncompatibleFunctionDeclaration.expected
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,5 @@ | ||
| | test1.cpp:1:12:1:12 | declaration of f | The object is not compatible with a re-declaration $@. | test.cpp:1:6:1:6 | definition of f | f | | ||
| | test.cpp:1:6:1:6 | definition of f | The object is not compatible with a re-declaration $@. | test1.cpp:1:12:1:12 | declaration of f | f | | ||
| | test.cpp:5:6:5:7 | declaration of f1 | The object is not compatible with a re-declaration $@. | test1.cpp:2:13:2:14 | declaration of f1 | f1 | | ||
| | test.cpp:6:6:6:7 | definition of f2 | The object is not compatible with a re-declaration $@. | test1.cpp:3:6:3:7 | definition of f2 | f2 | | ||
| | test.cpp:9:17:9:18 | declaration of f4 | The object is not compatible with a re-declaration $@. | test1.cpp:6:17:6:18 | declaration of f4 | f4 | |
8 changes: 8 additions & 0 deletions
8
cpp/common/test/rules/incompatiblefunctiondeclaration/IncompatibleFunctionDeclaration.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,8 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.incompatiblefunctiondeclaration.IncompatibleFunctionDeclaration | ||
|
|
||
| module TestFileConfig implements IncompatibleFunctionDeclarationConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import IncompatibleFunctionDeclaration<TestFileConfig> |
10 changes: 10 additions & 0 deletions
10
cpp/common/test/rules/incompatiblefunctiondeclaration/test.cpp
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,10 @@ | ||
| long f(int a) { // NON_COMPLIANT | ||
| return a * 2; | ||
| } | ||
|
|
||
| void f1(long a); // NON_COMPLIANT | ||
| void f2() {} // NON_COMPLIANT | ||
| int f3(); // COMPLIANT | ||
|
|
||
| extern "C" long f4(int a); // NON_COMPLIANT | ||
| long f5(); // NON_COMPLIANT[FALSE_NEGATIVE] |
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.