-
Notifications
You must be signed in to change notification settings - Fork 73
Add linkage1 package #1016
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
5
commits into
github:main
Choose a base branch
from
knewbury01:knewbury01/Linkage1
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
Add linkage1 package #1016
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e32ee19
Implement RULE-6-0-2 by converting to (improved) shared rule with A3-1-4
knewbury01 70375b4
Fix id generated for RULE-6-0-2
knewbury01 b321608
Implement RULE-6-5-1 as a shared rule with A3-3-1
knewbury01 179e8b7
Fix formatting generated shared queries
knewbury01 53f6f6c
Apply review comments externallinkagearraywithoutexplicitsize
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
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,2 @@ | ||
| - `A3-1-4` - `ExternalLinkageArrayWithoutExplicitSizeAutosar.ql`: | ||
| - `ExternalLinkageArrayWithoutExplicitSize.ql` has been renamed to `ExternalLinkageArrayWithoutExplicitSizeAutosar.ql` to reflect shared query implementation. Additionally the query previously only detected explicit uses of `extern` to determine external linkage, and now would catch other cases that are possible where it is external linkage and an array is declared without an explicit size. | ||
37 changes: 0 additions & 37 deletions
37
cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.ql
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.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,26 @@ | ||
| /** | ||
| * @id cpp/autosar/external-linkage-array-without-explicit-size-autosar | ||
| * @name A3-1-4: When an array with external linkage is declared, its size shall be stated explicitly | ||
| * @description A developer can more safely access the elements of an array if the size of the array | ||
| * can be explicitly determined. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity warning | ||
| * @tags external/autosar/id/a3-1-4 | ||
| * correctness | ||
| * external/autosar/allocated-target/design | ||
| * external/autosar/allocated-target/implementation | ||
| * external/autosar/enforcement/automated | ||
| * external/autosar/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.autosar | ||
| import codingstandards.cpp.rules.externallinkagearraywithoutexplicitsize.ExternalLinkageArrayWithoutExplicitSize | ||
|
|
||
| class ExternalLinkageArrayWithoutExplicitSizeAutosarQuery extends ExternalLinkageArrayWithoutExplicitSizeSharedQuery | ||
| { | ||
| ExternalLinkageArrayWithoutExplicitSizeAutosarQuery() { | ||
| this = ScopePackage::externalLinkageArrayWithoutExplicitSizeAutosarQuery() | ||
| } | ||
| } |
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/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.expected
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.qlref
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.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/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.ql |
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.qlref
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.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/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.ql |
44 changes: 44 additions & 0 deletions
44
cpp/common/src/codingstandards/cpp/exclusions/cpp/Linkage1.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 Linkage1Query = | ||
| TExternalLinkageArrayWithoutExplicitSizeMisraQuery() or | ||
| TExternalLinkageNotDeclaredInHeaderFileMisraQuery() | ||
|
|
||
| predicate isLinkage1QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `externalLinkageArrayWithoutExplicitSizeMisra` query | ||
| Linkage1Package::externalLinkageArrayWithoutExplicitSizeMisraQuery() and | ||
| queryId = | ||
| // `@id` for the `externalLinkageArrayWithoutExplicitSizeMisra` query | ||
| "cpp/misra/external-linkage-array-without-explicit-size-misra" and | ||
| ruleId = "RULE-6-0-2" and | ||
| category = "advisory" | ||
| or | ||
| query = | ||
| // `Query` instance for the `externalLinkageNotDeclaredInHeaderFileMisra` query | ||
| Linkage1Package::externalLinkageNotDeclaredInHeaderFileMisraQuery() and | ||
| queryId = | ||
| // `@id` for the `externalLinkageNotDeclaredInHeaderFileMisra` query | ||
| "cpp/misra/external-linkage-not-declared-in-header-file-misra" and | ||
| ruleId = "RULE-6-5-1" and | ||
| category = "advisory" | ||
| } | ||
|
|
||
| module Linkage1Package { | ||
| Query externalLinkageArrayWithoutExplicitSizeMisraQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `externalLinkageArrayWithoutExplicitSizeMisra` query | ||
| TQueryCPP(TLinkage1PackageQuery(TExternalLinkageArrayWithoutExplicitSizeMisraQuery())) | ||
| } | ||
|
|
||
| Query externalLinkageNotDeclaredInHeaderFileMisraQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `externalLinkageNotDeclaredInHeaderFileMisra` query | ||
| TQueryCPP(TLinkage1PackageQuery(TExternalLinkageNotDeclaredInHeaderFileMisraQuery())) | ||
| } | ||
| } |
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
28 changes: 28 additions & 0 deletions
28
...rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.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,28 @@ | ||
| /** | ||
| * Provides a library with a `problems` predicate for the following issue: | ||
| * Declaring an array with external linkage without its size being explicitly specified can disallow consistency and range checks on the array size and usage. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.Linkage | ||
|
|
||
| abstract class ExternalLinkageArrayWithoutExplicitSizeSharedQuery extends Query { } | ||
|
|
||
| Query getQuery() { result instanceof ExternalLinkageArrayWithoutExplicitSizeSharedQuery } | ||
|
|
||
| query predicate problems(DeclarationEntry declEntry, string message) { | ||
| exists(Variable v, ArrayType arrayType | | ||
| not isExcluded(declEntry, getQuery()) and | ||
| message = | ||
| "The declared array '" + declEntry.getName() + | ||
| "' with external linkage doesn't specify the size explicitly." and | ||
| hasExternalLinkage(v) and | ||
| not arrayType.hasArraySize() and | ||
knewbury01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Holds if declEntry is an array variable declaration (not a definition) | ||
| v.getADeclarationEntry() = declEntry and | ||
| not declEntry.isDefinition() and | ||
| arrayType = declEntry.getType().stripTopLevelSpecifiers() | ||
| ) | ||
| } | ||
36 changes: 36 additions & 0 deletions
36
...p/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.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,36 @@ | ||
| /** | ||
| * Provides a library with a `problems` predicate for the following issue: | ||
| * Using objects or functions with external linkage in implementation files makes code | ||
| * harder to understand. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.Linkage | ||
| import codingstandards.cpp.EncapsulatingFunctions | ||
|
|
||
| abstract class ExternalLinkageNotDeclaredInHeaderFileSharedQuery extends Query { } | ||
|
|
||
| Query getQuery() { result instanceof ExternalLinkageNotDeclaredInHeaderFileSharedQuery } | ||
|
|
||
| query predicate problems(DeclarationEntry de, string message) { | ||
| not isExcluded(de, getQuery()) and | ||
| hasExternalLinkage(de.getDeclaration()) and | ||
| // Exclude subobjects such as struct members or member functions | ||
| de.getDeclaration().isTopLevel() and | ||
| // The declaration with external linkage does not have a declaration in a header file | ||
| exists(Compilation c | de.getFile() = c.getAFileCompiled()) and | ||
| not exists(DeclarationEntry otherDe | | ||
| de.getDeclaration() = otherDe.getDeclaration() and | ||
| not de = otherDe and | ||
| not otherDe.isDefinition() | ||
| | | ||
| otherDe.getFile() instanceof HeaderFile | ||
| ) and | ||
| // Main functions are an exception to the rule | ||
| not de.getDeclaration() instanceof MainFunction and | ||
| if de.getDeclaration() instanceof Function | ||
| then message = "Externally linked function '" + de.getName() + "' not declared in header file." | ||
| else message = "Externally linked object '" + de.getName() + "' not declared in header file." | ||
| } |
2 changes: 2 additions & 0 deletions
2
.../externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.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 @@ | ||
| | test.cpp:19:14:19:15 | declaration of e1 | The declared array 'e1' with external linkage doesn't specify the size explicitly. | | ||
| | test.hpp:2:13:2:26 | declaration of header_and_cpp | The declared array 'header_and_cpp' with external linkage doesn't specify the size explicitly. | |
4 changes: 4 additions & 0 deletions
4
.../rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.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,4 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.externallinkagearraywithoutexplicitsize.ExternalLinkageArrayWithoutExplicitSize | ||
|
|
||
| class TestFileQuery extends ExternalLinkageArrayWithoutExplicitSizeSharedQuery, TestQuery { } |
36 changes: 36 additions & 0 deletions
36
cpp/common/test/rules/externallinkagearraywithoutexplicitsize/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,36 @@ | ||
| #define TEST 1 | ||
|
|
||
| // globals - external linkage | ||
| int a[1]; // COMPLIANT | ||
| int x = 1; | ||
| // int a1[1 + x]; // NON_COMPLIANT - compiler checked | ||
| // int a2[x]; //NON_COMPLIANT - compiler checked | ||
| // int a3[1][x]; // NON_COMPLIANT - compiler checked | ||
| int a4[] = {1}; // COMPLIANT - size explicitly provided | ||
| // int a5[]; // NON_COMPLIANT - compiler checked | ||
| int a6[1 + 1]; // COMPLIANT - size explicitly provided | ||
| // int a7[x][1]; // NON_COMPLIANT - compiler checked | ||
| // int (*a8)[x]; // NON_COMPLIANT - compiler checked | ||
|
|
||
| void f(int n) { | ||
| int a1[] = {1}; // COMPLIANT - not external linkage | ||
| int a2[1]; // COMPLIANT - not external linkage | ||
|
|
||
| extern int e1[]; // NON_COMPLIANT | ||
| } | ||
|
|
||
| struct s { | ||
| // Structs must have at least one non-flexible array member. | ||
| int foo; | ||
|
|
||
| // static data members have external linkage - but not currently detected in | ||
knewbury01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // our external linkage lib - also FAMs are expected to be detected | ||
| // specifically in RULE-18-7 | ||
| static const int flexibleArrayMember[]; // NON_COMPLIANT[FALSE_NEGATIVE] | ||
| static int flexibleArrayMember2[]; // NON_COMPLIANT[FALSE_NEGATIVE] | ||
| }; | ||
|
|
||
| // test.cpp | ||
| #include "test.hpp" | ||
| // definition associated with a declaration from test.hpp | ||
| int header_and_cpp[1] = {1}; | ||
2 changes: 2 additions & 0 deletions
2
cpp/common/test/rules/externallinkagearraywithoutexplicitsize/test.hpp
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 @@ | ||
| // test.hpp | ||
| extern int header_and_cpp[]; // NON_COMPLIANT |
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
...st/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.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,4 @@ | ||
| // GENERATED FILE - DO NOT MODIFY | ||
| import codingstandards.cpp.rules.externallinkagenotdeclaredinheaderfile.ExternalLinkageNotDeclaredInHeaderFile | ||
|
|
||
| class TestFileQuery extends ExternalLinkageNotDeclaredInHeaderFileSharedQuery, TestQuery { } |
File renamed without changes.
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
cpp/misra/src/rules/RULE-6-0-2/ExternalLinkageArrayWithoutExplicitSizeMisra.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,26 @@ | ||
| /** | ||
| * @id cpp/misra/external-linkage-array-without-explicit-size-misra | ||
| * @name RULE-6-0-2: Arrays with external linkage declared without explicit size shall not be used | ||
| * @description Declaring an array with external linkage without its size being explicitly specified | ||
| * can disallow consistency and range checks on the array size and usage. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity warning | ||
| * @tags external/misra/id/rule-6-0-2 | ||
| * maintainability | ||
| * readability | ||
| * scope/single-translation-unit | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.externallinkagearraywithoutexplicitsize.ExternalLinkageArrayWithoutExplicitSize | ||
|
|
||
| class ExternalLinkageArrayWithoutExplicitSizeMisraQuery extends ExternalLinkageArrayWithoutExplicitSizeSharedQuery | ||
| { | ||
| ExternalLinkageArrayWithoutExplicitSizeMisraQuery() { | ||
| this = Linkage1Package::externalLinkageArrayWithoutExplicitSizeMisraQuery() | ||
| } | ||
| } |
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.