-
Notifications
You must be signed in to change notification settings - Fork 77
Declarations3 cpp misra 2023 #1099
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
knewbury01
wants to merge
3
commits into
github:main
Choose a base branch
from
knewbury01:knewbury01/cpp-misra2023-declarations3
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion
1
cpp/autosar/test/rules/M3-1-2/FunctionsDeclaredAtBlockScope.qlref
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
cpp/autosar/test/rules/M3-1-2/FunctionsDeclaredAtBlockScope.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 @@ | ||
| cpp/common/test/rules/functiondeclaredatblockscope/FunctionDeclaredAtBlockScope.ql |
44 changes: 44 additions & 0 deletions
44
cpp/common/src/codingstandards/cpp/exclusions/cpp/Declarations3.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 Declarations3Query = | ||
| TVariableDeclaredArrayTypeQuery() or | ||
| TBlockScopeFunctionAmbiguousQuery() | ||
|
|
||
| predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `variableDeclaredArrayType` query | ||
| Declarations3Package::variableDeclaredArrayTypeQuery() and | ||
| queryId = | ||
| // `@id` for the `variableDeclaredArrayType` query | ||
| "cpp/misra/variable-declared-array-type" and | ||
| ruleId = "RULE-11-3-1" and | ||
| category = "advisory" | ||
| or | ||
| query = | ||
| // `Query` instance for the `blockScopeFunctionAmbiguous` query | ||
| Declarations3Package::blockScopeFunctionAmbiguousQuery() and | ||
| queryId = | ||
| // `@id` for the `blockScopeFunctionAmbiguous` query | ||
| "cpp/misra/block-scope-function-ambiguous" and | ||
| ruleId = "RULE-6-0-1" and | ||
| category = "required" | ||
| } | ||
|
|
||
| module Declarations3Package { | ||
| Query variableDeclaredArrayTypeQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `variableDeclaredArrayType` query | ||
| TQueryCPP(TDeclarations3PackageQuery(TVariableDeclaredArrayTypeQuery())) | ||
| } | ||
|
|
||
| Query blockScopeFunctionAmbiguousQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `blockScopeFunctionAmbiguous` query | ||
| TQueryCPP(TDeclarations3PackageQuery(TBlockScopeFunctionAmbiguousQuery())) | ||
| } | ||
| } | ||
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
25 changes: 25 additions & 0 deletions
25
...c/codingstandards/cpp/rules/functiondeclaredatblockscope/FunctionDeclaredAtBlockScope.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,25 @@ | ||
| /** | ||
| * Provides a configurable module FunctionDeclaredAtBlockScope with a `problems` predicate | ||
| * for the following issue: | ||
| * A function declared at block scope can make code harder to read and may lead to | ||
| * developer confusion. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
|
|
||
| signature module FunctionDeclaredAtBlockScopeConfigSig { | ||
| Query getQuery(); | ||
| } | ||
|
|
||
| module FunctionDeclaredAtBlockScope<FunctionDeclaredAtBlockScopeConfigSig Config> { | ||
| query predicate problems(Function f, string message) { | ||
| exists(DeclStmt decl | | ||
| not isExcluded(decl, Config::getQuery()) and | ||
| not isExcluded(f, Config::getQuery()) and | ||
| decl.getADeclaration() = f and | ||
| message = "Function " + f.getName() + " is declared at block scope." | ||
| ) | ||
| } | ||
| } |
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
File renamed without changes.
8 changes: 8 additions & 0 deletions
8
cpp/common/test/rules/functiondeclaredatblockscope/FunctionDeclaredAtBlockScope.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.functiondeclaredatblockscope.FunctionDeclaredAtBlockScope | ||
|
|
||
| module TestFileConfig implements FunctionDeclaredAtBlockScopeConfigSig { | ||
| Query getQuery() { result instanceof TestQuery } | ||
| } | ||
|
|
||
| import FunctionDeclaredAtBlockScope<TestFileConfig> |
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
cpp/misra/src/rules/RULE-11-3-1/VariableDeclaredArrayType.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,35 @@ | ||
| /** | ||
| * @id cpp/misra/variable-declared-array-type | ||
| * @name RULE-11-3-1: Variables of array type should not be declared | ||
| * @description Using array type instead of container types can lead to difficulty manually managing | ||
| * size and accesses. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-11-3-1 | ||
| * correctness | ||
| * maintainability | ||
| * readability | ||
| * scope/single-translation-unit | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
|
|
||
| class FixedSizeCharArray extends Variable { | ||
| FixedSizeCharArray() { | ||
| this.isStatic() and | ||
| this.getUnspecifiedType().stripType() instanceof CharType and | ||
| this.getInitializer().getExpr() instanceof StringLiteral | ||
| } | ||
| } | ||
|
|
||
| from Variable v | ||
| where | ||
| not isExcluded(v, Declarations3Package::variableDeclaredArrayTypeQuery()) and | ||
| exists(ArrayType a | v.getType() = a | not v instanceof FixedSizeCharArray) and | ||
| // Exclude the compiler generated __func__ as it is the only way to access the function name information | ||
knewbury01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| not v.getName() = "__func__" | ||
| select v, "Variable " + v.getName() + " has an array type." | ||
25 changes: 25 additions & 0 deletions
25
cpp/misra/src/rules/RULE-6-0-1/BlockScopeFunctionAmbiguous.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/block-scope-function-ambiguous | ||
| * @name RULE-6-0-1: Block scope declarations shall not be visually ambiguous | ||
| * @description A function declared at block scope can make code harder to read and may lead to | ||
| * developer confusion. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity warning | ||
| * @tags external/misra/id/rule-6-0-1 | ||
| * maintainability | ||
| * readability | ||
| * scope/single-translation-unit | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.functiondeclaredatblockscope.FunctionDeclaredAtBlockScope | ||
|
|
||
| module BlockScopeFunctionAmbiguousConfig implements FunctionDeclaredAtBlockScopeConfigSig { | ||
| Query getQuery() { result = Declarations3Package::blockScopeFunctionAmbiguousQuery() } | ||
| } | ||
|
|
||
| import FunctionDeclaredAtBlockScope<BlockScopeFunctionAmbiguousConfig> |
9 changes: 9 additions & 0 deletions
9
cpp/misra/test/rules/RULE-11-3-1/VariableDeclaredArrayType.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,9 @@ | ||
| | test.cpp:5:5:5:5 | g | Variable g has an array type. | | ||
| | test.cpp:9:24:9:24 | a | Variable a has an array type. | | ||
| | test.cpp:10:7:10:8 | a1 | Variable a1 has an array type. | | ||
| | test.cpp:14:7:14:8 | a1 | Variable a1 has an array type. | | ||
| | test.cpp:19:7:19:7 | x | Variable x has an array type. | | ||
| | test.cpp:20:17:20:17 | a | Variable a has an array type. | | ||
| | test.cpp:22:8:22:9 | x1 | Variable x1 has an array type. | | ||
| | test.cpp:28:6:28:7 | g1 | Variable g1 has an array type. | | ||
| | test.cpp:34:21:34:21 | p | Variable p has an array type. | |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-11-3-1/VariableDeclaredArrayType.qlref
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 @@ | ||
| rules/RULE-11-3-1/VariableDeclaredArrayType.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,36 @@ | ||
|
|
||
| #include <array> | ||
| #include <string_view> | ||
|
|
||
| int g[100]; // NON_COMPLIANT | ||
|
|
||
| class C { | ||
| public: | ||
| static constexpr int a[1]{0}; // NON_COMPLIANT | ||
| int a1[1]; // NON_COMPLIANT | ||
| }; | ||
|
|
||
| struct S { | ||
| int a1[1]; // NON_COMPLIANT | ||
| }; | ||
|
|
||
| void test_c_arrays() { | ||
|
|
||
| int x[100]; // NON_COMPLIANT | ||
| constexpr int a[]{0, 1, 2}; // NON_COMPLIANT | ||
| const size_t s{1}; | ||
| char x1[s]; // NON_COMPLIANT | ||
| std::array<char, s> x2; // COMPLIANT | ||
|
|
||
| __func__; // COMPLIANT | ||
| } | ||
|
|
||
| char g1[] = "abc"; // NON_COMPLIANT | ||
| const char g2[] = "abc"; // COMPLIANT | ||
|
|
||
| using namespace std::literals; | ||
| const auto g3 = "abc"sv; // COMPLIANT | ||
|
|
||
| void param_test(int p[1], int (&p1)[1], int (*p2)[1], | ||
| int *p3) { // NON_COMPLIANT -- p, rest are compliant | ||
| } |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-6-0-1/BlockScopeFunctionAmbiguous.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 @@ | ||
| cpp/common/test/rules/functiondeclaredatblockscope/FunctionDeclaredAtBlockScope.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
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,53 @@ | ||
| { | ||
| "MISRA-C++-2023": { | ||
| "RULE-11-3-1": { | ||
| "properties": { | ||
| "enforcement": "decidable", | ||
| "obligation": "advisory" | ||
| }, | ||
| "queries": [ | ||
| { | ||
| "description": "Using array type instead of container types can lead to difficulty manually managing size and accesses.", | ||
| "kind": "problem", | ||
| "name": "Variables of array type should not be declared", | ||
| "precision": "very-high", | ||
| "severity": "error", | ||
knewbury01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "short_name": "VariableDeclaredArrayType", | ||
| "tags": [ | ||
| "correctness", | ||
| "maintainability", | ||
| "readability", | ||
| "scope/single-translation-unit" | ||
| ] | ||
| } | ||
| ], | ||
| "title": "Variables of array type should not be declared" | ||
| }, | ||
| "RULE-6-0-1": { | ||
| "properties": { | ||
| "enforcement": "decidable", | ||
| "obligation": "required" | ||
| }, | ||
| "queries": [ | ||
| { | ||
| "description": "A function declared at block scope can make code harder to read and may lead to developer confusion.", | ||
| "kind": "problem", | ||
| "name": "Block scope declarations shall not be visually ambiguous", | ||
| "precision": "very-high", | ||
| "severity": "warning", | ||
| "short_name": "BlockScopeFunctionAmbiguous", | ||
| "shared_implementation_short_name": "FunctionDeclaredAtBlockScope", | ||
| "tags": [ | ||
| "maintainability", | ||
| "readability", | ||
| "scope/single-translation-unit" | ||
| ], | ||
| "implementation_scope": { | ||
| "description": "The rule checks for functions in block scope only. The information CodeQL has access to in the database is not enough to cover the object with redundant parentheses case." | ||
| } | ||
| } | ||
| ], | ||
| "title": "Block scope declarations shall not be visually ambiguous" | ||
| } | ||
| } | ||
| } | ||
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.
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.