-
Notifications
You must be signed in to change notification settings - Fork 76
Add package linkage 2 #1018
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
Merged
Add package linkage 2 #1018
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
77a17c2
Add RULE-6-5-2 linkage2
knewbury01 9f48c75
Add shared rule A3-1-1 for RULE-6-2-4 linkage2 and improve A3-1-1
knewbury01 14939e5
Apply suggestion from @Copilot
mbaluda 74fe2e7
Merge branch 'main' into knewbury01/Linkage2
mbaluda 9c3cd89
Update RuleMetadata.qll
mbaluda f327e11
Update cpp/common/src/codingstandards/cpp/rules/violationsofonedefini…
mbaluda 4d6052c
Add missing expected file update
knewbury01 e5da8c9
Add missing change notes file update
knewbury01 de0eb9e
Improve scope.qll library to handle global classes and align severity…
knewbury01 fe20f7e
Improve testcase for externallinkagearraywithoutexplicitsize and chan…
knewbury01 29e26a4
Add testcase expecteds that were missing, approved that these are exp…
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 |
|---|---|---|
|
|
@@ -259,6 +259,8 @@ | |
| "Language1", | ||
| "Language2", | ||
| "Language3", | ||
| "Linkage1", | ||
| "Linkage2", | ||
| "Literals", | ||
| "Loops", | ||
| "Macros", | ||
|
|
||
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-1` - `ViolationsOfOneDefinitionRule.ql`: | ||
| - The query previously would incorrectly allow cases where something was defined with `extern` and did not use the defined external linkage library to find external linkage. This change may result in the query finding more results. |
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
cpp/autosar/test/rules/A3-1-1/ViolationsOfOneDefinitionRule.expected
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
cpp/autosar/test/rules/A3-1-1/ViolationsOfOneDefinitionRule.qlref
This file was deleted.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
cpp/autosar/test/rules/A3-1-1/ViolationsOfOneDefinitionRule.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/violationsofonedefinitionrule/ViolationsOfOneDefinitionRule.ql |
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
cpp/common/src/codingstandards/cpp/exclusions/cpp/Linkage2.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 Linkage2Query = | ||
| TViolationsOfOneDefinitionRuleMisraQuery() or | ||
| TInternalLinkageSpecifiedAppropriatelyQuery() | ||
|
|
||
| predicate isLinkage2QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `violationsOfOneDefinitionRuleMisra` query | ||
| Linkage2Package::violationsOfOneDefinitionRuleMisraQuery() and | ||
| queryId = | ||
| // `@id` for the `violationsOfOneDefinitionRuleMisra` query | ||
| "cpp/misra/violations-of-one-definition-rule-misra" and | ||
| ruleId = "RULE-6-2-4" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `internalLinkageSpecifiedAppropriately` query | ||
| Linkage2Package::internalLinkageSpecifiedAppropriatelyQuery() and | ||
| queryId = | ||
| // `@id` for the `internalLinkageSpecifiedAppropriately` query | ||
| "cpp/misra/internal-linkage-specified-appropriately" and | ||
| ruleId = "RULE-6-5-2" and | ||
| category = "advisory" | ||
| } | ||
|
|
||
| module Linkage2Package { | ||
| Query violationsOfOneDefinitionRuleMisraQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `violationsOfOneDefinitionRuleMisra` query | ||
| TQueryCPP(TLinkage2PackageQuery(TViolationsOfOneDefinitionRuleMisraQuery())) | ||
| } | ||
|
|
||
| Query internalLinkageSpecifiedAppropriatelyQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `internalLinkageSpecifiedAppropriately` query | ||
| TQueryCPP(TLinkage2PackageQuery(TInternalLinkageSpecifiedAppropriatelyQuery())) | ||
| } | ||
| } |
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
74 changes: 74 additions & 0 deletions
74
...codingstandards/cpp/rules/violationsofonedefinitionrule/ViolationsOfOneDefinitionRule.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,74 @@ | ||
| /** | ||
| * Provides a library with a `problems` predicate for the following issue: | ||
| * Placing the definitions of functions or objects that are non-inline and have | ||
| * external linkage can lead to violations of the ODR and can lead to undefined | ||
| * behaviour. | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.Customizations | ||
| import codingstandards.cpp.Exclusions | ||
| import codingstandards.cpp.AcceptableHeader | ||
| import codingstandards.cpp.Linkage | ||
|
|
||
| predicate isInline(Function decl) { | ||
| exists(Specifier spec | | ||
| spec = decl.getASpecifier() and | ||
| ( | ||
| spec.hasName("inline") or | ||
| spec.hasName("constexpr") | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| abstract class ViolationsOfOneDefinitionRuleSharedQuery extends Query { } | ||
|
|
||
| Query getQuery() { result instanceof ViolationsOfOneDefinitionRuleSharedQuery } | ||
|
|
||
| query predicate problems(DeclarationEntry decl, string message, File declFile, string secondmessage) { | ||
| exists(string case | | ||
| not isExcluded(decl, getQuery()) and | ||
| declFile = decl.getFile() and | ||
| secondmessage = decl.getFile().getBaseName() and | ||
| message = | ||
| "Header file $@ contains " + case + " " + decl.getName() + | ||
| " that lead to One Defintion Rule violation." and | ||
|
mbaluda marked this conversation as resolved.
Outdated
|
||
| hasExternalLinkage(decl.getDeclaration()) and | ||
| ( | ||
| //a non-inline/non-extern function defined in a header | ||
| exists(FunctionDeclarationEntry fn | | ||
| fn.isDefinition() and | ||
| not ( | ||
| isInline(fn.getDeclaration()) | ||
| or | ||
| //any (defined) templates do not violate the ODR | ||
| fn.isFromUninstantiatedTemplate(_) | ||
| or | ||
| fn.isFromTemplateInstantiation(_) and | ||
| //except for specializations, those do violate ODR | ||
| not fn.isSpecialization() | ||
| or | ||
| //static/nonstatic member functions should still not be defined (so do not exclude here) | ||
| fn.getDeclaration().isStatic() and not fn.getFunction() instanceof MemberFunction | ||
| ) and | ||
| decl = fn and | ||
| case = "function" | ||
| ) | ||
| or | ||
| //an non-const object defined in a header | ||
| exists(Variable object | | ||
| not ( | ||
| object.isConstexpr() | ||
| or | ||
| object.isConst() | ||
| or | ||
| object.isStatic() | ||
| ) and | ||
| decl = object.getDefinition() and | ||
| case = "object" | ||
| ) | ||
| ) and | ||
| not decl.getDeclaration().getNamespace().isAnonymous() and | ||
| decl.getFile() instanceof AcceptableHeader | ||
| ) | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
cpp/common/test/rules/violationsofonedefinitionrule/ViolationsOfOneDefinitionRule.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,3 @@ | ||
| | test.hpp:3:6:3:7 | definition of f1 | Header file $@ contains function f1 that lead to One Defintion Rule violation. | test.hpp:0:0:0:0 | test.hpp | test.hpp | | ||
| | test.hpp:8:5:8:5 | definition of i | Header file $@ contains object i that lead to One Defintion Rule violation. | test.hpp:0:0:0:0 | test.hpp | test.hpp | | ||
| | test.hpp:9:12:9:13 | definition of i1 | Header file $@ contains object i1 that lead to One Defintion Rule violation. | test.hpp:0:0:0:0 | test.hpp | test.hpp | |
4 changes: 4 additions & 0 deletions
4
cpp/common/test/rules/violationsofonedefinitionrule/ViolationsOfOneDefinitionRule.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.violationsofonedefinitionrule.ViolationsOfOneDefinitionRule | ||
|
|
||
| class TestFileQuery extends ViolationsOfOneDefinitionRuleSharedQuery, TestQuery { } |
File renamed without changes.
26 changes: 26 additions & 0 deletions
26
cpp/common/test/rules/violationsofonedefinitionrule/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,26 @@ | ||
| void f(); // COMPLIANT | ||
|
|
||
| void f1() {} // NON_COMPLIANT | ||
| inline void f2() {} // COMPLIANT | ||
|
|
||
| template <typename T> void f3(T){}; // COMPLIANT - implicitly inline | ||
|
|
||
| int i; // NON_COMPLIANT | ||
| extern int i1 = 1; // NON_COMPLIANT | ||
|
|
||
| constexpr auto i2{1}; // COMPLIANT - not external linkage | ||
|
|
||
| struct S { | ||
| int i; // COMPLIANT - no linkage | ||
| inline static const int i1{1}; // COMPLIANT - inline | ||
| }; | ||
|
|
||
| class C { | ||
| static int m(); // COMPLIANT | ||
| int m1(); // COMPLIANT | ||
| }; | ||
|
|
||
| int C::m() {} // NON_COMPLIANT[FALSE_NEGATIVE] - external linkage library | ||
| // issue/namespace of class not found | ||
| int C::m1() {} // NON_COMPLIANT[FALSE_NEGATIVE] - external linkage library | ||
| // issue/namespace of class not found |
27 changes: 27 additions & 0 deletions
27
cpp/misra/src/rules/RULE-6-2-4/ViolationsOfOneDefinitionRuleMisra.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,27 @@ | ||
| /** | ||
| * @id cpp/misra/violations-of-one-definition-rule-misra | ||
| * @name RULE-6-2-4: A header file shall not contain definitions of functions or objects that are non-inline and have external linkage | ||
| * @description Placing the definitions of functions or objects that are non-inline and have | ||
| * external linkage can lead to violations of the ODR and can lead to undefined | ||
| * behaviour. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-6-2-4 | ||
| * correctness | ||
| * maintainability | ||
| * readability | ||
| * scope/single-translation-unit | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.rules.violationsofonedefinitionrule.ViolationsOfOneDefinitionRule | ||
|
|
||
| class ViolationsOfOneDefinitionRuleMisraQuery extends ViolationsOfOneDefinitionRuleSharedQuery { | ||
| ViolationsOfOneDefinitionRuleMisraQuery() { | ||
| this = Linkage2Package::violationsOfOneDefinitionRuleMisraQuery() | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
cpp/misra/src/rules/RULE-6-5-2/InternalLinkageSpecifiedAppropriately.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,46 @@ | ||
| /** | ||
| * @id cpp/misra/internal-linkage-specified-appropriately | ||
| * @name RULE-6-5-2: Internal linkage should be specified appropriately | ||
| * @description Using certain specifiers or declaring entities with internal linkage in certain | ||
| * namespaces can lead to confusion as to the linkage of the entity and can cause code | ||
| * to be more difficult to read. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-6-5-2 | ||
| * correctness | ||
| * maintainability | ||
| * readability | ||
| * scope/single-translation-unit | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/advisory | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
| import codingstandards.cpp.Linkage | ||
| import codingstandards.cpp.types.Pointers | ||
|
|
||
| from DeclarationEntry decl, string message | ||
| where | ||
| not isExcluded(decl, Linkage2Package::internalLinkageSpecifiedAppropriatelyQuery()) and | ||
| hasInternalLinkage(decl.getDeclaration()) and | ||
| //exclusions as per rule for const and constexpr Variables | ||
| not decl.getDeclaration().(Variable).getUnderlyingType().isConst() and | ||
| not decl.getDeclaration().(Variable).getType().(PointerOrArrayType).isDeeplyConstBelow() and | ||
|
mbaluda marked this conversation as resolved.
|
||
| not decl.getDeclaration().(Variable).isConstexpr() and | ||
| ( | ||
| decl.hasSpecifier("static") and | ||
| ( | ||
| decl.getDeclaration().getNamespace().isAnonymous() and | ||
| message = "Static specifier used in anonymous namespace." | ||
| or | ||
| not decl.getDeclaration().getNamespace().isAnonymous() and | ||
| message = "Static specifier used in non-anonymous namespace." | ||
| ) | ||
| or | ||
| decl.hasSpecifier("extern") and | ||
| decl.getDeclaration().getNamespace().isAnonymous() and | ||
| message = "Extern specifier used in anonymous namespace." | ||
| ) | ||
| select decl, message | ||
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-6-2-4/ViolationsOfOneDefinitionRuleMisra.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/violationsofonedefinitionrule/ViolationsOfOneDefinitionRule.ql |
4 changes: 4 additions & 0 deletions
4
cpp/misra/test/rules/RULE-6-5-2/InternalLinkageSpecifiedAppropriately.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 @@ | ||
| | test.cpp:1:13:1:13 | declaration of f | Static specifier used in non-anonymous namespace. | | ||
| | test.cpp:5:13:5:14 | declaration of f2 | Extern specifier used in anonymous namespace. | | ||
| | test.cpp:8:12:8:13 | declaration of i1 | Extern specifier used in anonymous namespace. | | ||
| | test.cpp:9:12:9:13 | definition of i2 | Static specifier used in anonymous namespace. | |
1 change: 1 addition & 0 deletions
1
cpp/misra/test/rules/RULE-6-5-2/InternalLinkageSpecifiedAppropriately.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-6-5-2/InternalLinkageSpecifiedAppropriately.ql |
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.