-
Notifications
You must be signed in to change notification settings - Fork 76
Michaelrfairhurst/declarations8 rule 6-2-3 do not duplicate source code #1112
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
d79cf17
fc8fb7e
ee92f89
ac06fe8
bfdc09a
5a72bef
8566bb2
f450e53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| - All queries using `Linkage.qll`: | ||
| - The logic for determining whether a namespace is within an anonymous namespace, directly or indirectly, has been refactored. | ||
| - No visible change in behavior or performance is expected. |
| 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 Declarations8Query = | ||
| TSourceCodeImplementedOnlyOnceQuery() or | ||
| TTemplateSpecializationWrongLocationQuery() or | ||
| TDuplicateTypeDefinitionsQuery() | ||
|
|
||
| predicate isDeclarations8QueryMetadata(Query query, string queryId, string ruleId, string category) { | ||
| query = | ||
| // `Query` instance for the `sourceCodeImplementedOnlyOnce` query | ||
| Declarations8Package::sourceCodeImplementedOnlyOnceQuery() and | ||
| queryId = | ||
| // `@id` for the `sourceCodeImplementedOnlyOnce` query | ||
| "cpp/misra/source-code-implemented-only-once" and | ||
| ruleId = "RULE-6-2-3" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `templateSpecializationWrongLocation` query | ||
| Declarations8Package::templateSpecializationWrongLocationQuery() and | ||
| queryId = | ||
| // `@id` for the `templateSpecializationWrongLocation` query | ||
| "cpp/misra/template-specialization-wrong-location" and | ||
| ruleId = "RULE-6-2-3" and | ||
| category = "required" | ||
| or | ||
| query = | ||
| // `Query` instance for the `duplicateTypeDefinitions` query | ||
| Declarations8Package::duplicateTypeDefinitionsQuery() and | ||
| queryId = | ||
| // `@id` for the `duplicateTypeDefinitions` query | ||
| "cpp/misra/duplicate-type-definitions" and | ||
| ruleId = "RULE-6-2-3" and | ||
| category = "required" | ||
| } | ||
|
|
||
| module Declarations8Package { | ||
| Query sourceCodeImplementedOnlyOnceQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `sourceCodeImplementedOnlyOnce` query | ||
| TQueryCPP(TDeclarations8PackageQuery(TSourceCodeImplementedOnlyOnceQuery())) | ||
| } | ||
|
|
||
| Query templateSpecializationWrongLocationQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `templateSpecializationWrongLocation` query | ||
| TQueryCPP(TDeclarations8PackageQuery(TTemplateSpecializationWrongLocationQuery())) | ||
| } | ||
|
|
||
| Query duplicateTypeDefinitionsQuery() { | ||
| //autogenerate `Query` type | ||
| result = | ||
| // `Query` type for `duplicateTypeDefinitions` query | ||
| TQueryCPP(TDeclarations8PackageQuery(TDuplicateTypeDefinitionsQuery())) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,41 @@ | ||||||||
| /** | ||||||||
| * @id cpp/misra/duplicate-type-definitions | ||||||||
| * @name RULE-6-2-3: RULE-6-2-3: Duplicate type definitions across files | ||||||||
| * @description Defining a type with the same fully qualified name in multiple files increases the | ||||||||
| * risk of ODR violations and undefined behavior. | ||||||||
| * @kind problem | ||||||||
| * @precision very-high | ||||||||
| * @problem.severity error | ||||||||
| * @tags external/misra/id/rule-6-2-3 | ||||||||
| * correctness | ||||||||
| * maintainability | ||||||||
| * scope/system | ||||||||
| * external/misra/enforcement/decidable | ||||||||
| * external/misra/obligation/required | ||||||||
| */ | ||||||||
|
|
||||||||
| import cpp | ||||||||
| import codingstandards.cpp.misra | ||||||||
| import codingstandards.cpp.Linkage | ||||||||
|
|
||||||||
| class UserTypeDefinition extends TypeDeclarationEntry { | ||||||||
| UserTypeDefinition() { | ||||||||
| (isDefinition() or getDeclaration() instanceof TypedefType) and | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I am not sure of this myself but wanted to check should this rule actually include typedefs at all? |
||||||||
| not getDeclaration().(Class).isAnonymous() and | ||||||||
| not getDeclaration().(Union).isAnonymous() and | ||||||||
| not getDeclaration().(Enum).isAnonymous() and | ||||||||
| not getDeclaration() instanceof ClassTemplateSpecialization and | ||||||||
| not getDeclaration().getNamespace() instanceof WithinAnonymousNamespace | ||||||||
| } | ||||||||
|
|
||||||||
| UserType getUserType() { result = getDeclaration() } | ||||||||
| } | ||||||||
|
|
||||||||
| from UserTypeDefinition t1, UserTypeDefinition t2 | ||||||||
| where | ||||||||
| not isExcluded(t1, Declarations8Package::duplicateTypeDefinitionsQuery()) and | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should have (maybe , double check, bc this then does cut out dups of things in /usr/include/ headers so not totally sure)
Suggested change
|
||||||||
| t1.getUserType().getQualifiedName() = t2.getUserType().getQualifiedName() and | ||||||||
| t1.getFile() != t2.getFile() and | ||||||||
| t1.getFile().getAbsolutePath() < t2.getFile().getAbsolutePath() // Report only once per pair | ||||||||
| select t1, "Type '" + t1.getUserType().getQualifiedName() + "' is defined in files $@ and $@.", t1, | ||||||||
| t1.getFile().getBaseName(), t2, t2.getFile().getBaseName() | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /** | ||
| * @id cpp/misra/source-code-implemented-only-once | ||
| * @name RULE-6-2-3: The source code used to implement an entity shall appear only once | ||
| * @description Implementing an entity in multiple source locations increases the risk of ODR | ||
| * violations and undefined behavior. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-6-2-3 | ||
| * correctness | ||
| * maintainability | ||
| * scope/system | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
|
|
||
| predicate isInline(DeclarationEntry d) { | ||
| // There is no way to detect if a `GlobalVariable` is declared inline. | ||
| d.getDeclaration().(Function).isInline() | ||
| } | ||
|
|
||
| from DeclarationEntry d1, DeclarationEntry d2, string namespace, string name | ||
| where | ||
| not isExcluded([d1, d2], Declarations8Package::sourceCodeImplementedOnlyOnceQuery()) and | ||
| d1 != d2 and | ||
| d1.isDefinition() and | ||
| d2.isDefinition() and | ||
| isInline(d1) and | ||
| isInline(d2) and | ||
| d1.getDeclaration().hasQualifiedName(namespace, name) and | ||
| d2.getDeclaration().hasQualifiedName(namespace, name) and | ||
| d1.getFile() != d2.getFile() and | ||
|
MichaelRFairhurst marked this conversation as resolved.
Outdated
|
||
| d1.getFile().getAbsolutePath() < d2.getFile().getAbsolutePath() | ||
| select d1, | ||
| "Inline variable '" + d1.getName() + | ||
| "' is defined in multiple files, violating the source code uniqueness requirement." | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * @id cpp/misra/template-specialization-wrong-location | ||
| * @name RULE-6-2-3: RULE-6-2-3: Template specializations in wrong location | ||
|
MichaelRFairhurst marked this conversation as resolved.
Outdated
|
||
| * @description Template specializations must be defined in the same file as the primary template or | ||
| * where a specialized type is defined to ensure visibility and avoid ODR violations. | ||
| * @kind problem | ||
| * @precision very-high | ||
| * @problem.severity error | ||
| * @tags external/misra/id/rule-6-2-3 | ||
| * correctness | ||
| * maintainability | ||
| * scope/system | ||
| * external/misra/enforcement/decidable | ||
| * external/misra/obligation/required | ||
| */ | ||
|
|
||
| import cpp | ||
| import codingstandards.cpp.misra | ||
|
|
||
| predicate specializedWithFileDeclaredType(ClassTemplateSpecialization spec) { | ||
| exists(Type argType | | ||
| spec.getTemplateArgument(_).(Type).getUnderlyingType() = argType and | ||
| spec.getFile() = argType.getFile() and | ||
| not argType instanceof TypeTemplateParameter | ||
| ) | ||
| } | ||
|
|
||
| from ClassTemplateSpecialization spec, Class primaryTemplate, File primaryFile | ||
| where | ||
| not isExcluded(spec, Declarations8Package::templateSpecializationWrongLocationQuery()) and | ||
| spec.getPrimaryTemplate() = primaryTemplate and | ||
| primaryFile = primaryTemplate.getFile() and | ||
| // The specialization is in a different file than the primary template | ||
| spec.getFile() != primaryFile and | ||
| // And it's not in the same file as any of its template arguments | ||
| not specializedWithFileDeclaredType(spec) | ||
| select spec, | ||
| "Template specialization '" + spec.getName() + | ||
| "' is declared in a different file than $@ and all specialized template arguments.", | ||
| primaryTemplate, primaryTemplate.getName() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| | test.cpp:15:8:15:22 | definition of StructRedefined | Type 'StructRedefined' is defined in files $@ and $@. | test.cpp:15:8:15:22 | definition of StructRedefined | test.cpp | test2.cpp:16:8:16:22 | definition of StructRedefined | test2.cpp | | ||
| | test.cpp:22:29:22:40 | definition of TplRedefined<T> | Type 'TplRedefined<T>' is defined in files $@ and $@. | test.cpp:22:29:22:40 | definition of TplRedefined<T> | test.cpp | test2.cpp:22:29:22:40 | definition of TplRedefined<T> | test2.cpp | | ||
| | test.cpp:26:6:26:18 | definition of DuplicateEnum | Type 'DuplicateEnum' is defined in files $@ and $@. | test.cpp:26:6:26:18 | definition of DuplicateEnum | test.cpp | test2.cpp:25:6:25:18 | definition of DuplicateEnum | test2.cpp | | ||
| | test.cpp:27:12:27:29 | definition of DuplicateEnumClass | Type 'DuplicateEnumClass' is defined in files $@ and $@. | test.cpp:27:12:27:29 | definition of DuplicateEnumClass | test.cpp | test2.cpp:26:12:26:29 | definition of DuplicateEnumClass | test2.cpp | | ||
| | test.cpp:29:17:29:32 | declaration of DuplicateTypedef | Type 'DuplicateTypedef' is defined in files $@ and $@. | test.cpp:29:17:29:32 | declaration of DuplicateTypedef | test.cpp | test2.cpp:28:17:28:32 | declaration of DuplicateTypedef | test2.cpp | | ||
| | test.cpp:30:7:30:20 | declaration of DuplicateUsing | Type 'DuplicateUsing' is defined in files $@ and $@. | test.cpp:30:7:30:20 | declaration of DuplicateUsing | test.cpp | test2.cpp:29:7:29:20 | declaration of DuplicateUsing | test2.cpp | | ||
| | test.cpp:31:7:31:20 | definition of DuplicateUnion | Type 'DuplicateUnion' is defined in files $@ and $@. | test.cpp:31:7:31:20 | definition of DuplicateUnion | test.cpp | test2.cpp:30:7:30:20 | definition of DuplicateUnion | test2.cpp | | ||
| | test.cpp:36:7:36:11 | definition of Outer | Type 'ns1::Outer' is defined in files $@ and $@. | test.cpp:36:7:36:11 | definition of Outer | test.cpp | test2.cpp:35:7:35:11 | definition of Outer | test2.cpp | | ||
| | test.cpp:37:9:37:13 | definition of Inner | Type 'ns1::Outer::Inner' is defined in files $@ and $@. | test.cpp:37:9:37:13 | definition of Inner | test.cpp | test2.cpp:36:9:36:13 | definition of Inner | test2.cpp | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-6-2-3/DuplicateTypeDefinitions.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| | test.cpp:6:13:6:26 | definition of func_redefined | Inline variable 'func_redefined' is defined in multiple files, violating the source code uniqueness requirement. | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-6-2-3/SourceCodeImplementedOnlyOnce.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| | noncompliant_specialization.h:3:19:3:28 | Tpl1<long> | Template specialization 'Tpl1<long>' is declared in a different file than $@ and all specialized template arguments. | template.h:10:29:10:32 | Tpl1<T> | Tpl1<T> | | ||
| | noncompliant_specialization.h:4:19:4:38 | Tpl1<C2> | Template specialization 'Tpl1<C2>' is declared in a different file than $@ and all specialized template arguments. | template.h:10:29:10:32 | Tpl1<T> | Tpl1<T> | | ||
| | noncompliant_specialization.h:5:19:5:35 | Tpl1<C2> | Template specialization 'Tpl1<C2>' is declared in a different file than $@ and all specialized template arguments. | template.h:10:29:10:32 | Tpl1<T> | Tpl1<T> | | ||
| | noncompliant_specialization.h:7:19:7:34 | Tpl2<long, long> | Template specialization 'Tpl2<long, long>' is declared in a different file than $@ and all specialized template arguments. | template.h:11:41:11:44 | Tpl2<A, B> | Tpl2<A, B> | | ||
| | noncompliant_specialization.h:8:19:8:54 | Tpl2<C2, C2> | Template specialization 'Tpl2<C2, C2>' is declared in a different file than $@ and all specialized template arguments. | template.h:11:41:11:44 | Tpl2<A, B> | Tpl2<A, B> | | ||
| | noncompliant_specialization.h:9:19:9:48 | Tpl2<C2, C2> | Template specialization 'Tpl2<C2, C2>' is declared in a different file than $@ and all specialized template arguments. | template.h:11:41:11:44 | Tpl2<A, B> | Tpl2<A, B> | | ||
| | noncompliant_specialization.h:11:29:11:41 | Tpl2<long, T> | Template specialization 'Tpl2<long, T>' is declared in a different file than $@ and all specialized template arguments. | template.h:11:41:11:44 | Tpl2<A, B> | Tpl2<A, B> | | ||
| | noncompliant_specialization.h:12:29:12:48 | Tpl2<C1, T> | Template specialization 'Tpl2<C1, T>' is declared in a different file than $@ and all specialized template arguments. | template.h:11:41:11:44 | Tpl2<A, B> | Tpl2<A, B> | | ||
| | noncompliant_specialization.h:14:19:14:25 | Tpl3<1> | Template specialization 'Tpl3<1>' is declared in a different file than $@ and all specialized template arguments. | template.h:12:22:12:25 | Tpl3<<unnamed>> | Tpl3<<unnamed>> | | ||
| | noncompliant_specialization.h:15:19:15:31 | Tpl4<long, 0> | Template specialization 'Tpl4<long, 0>' is declared in a different file than $@ and all specialized template arguments. | template.h:13:34:13:37 | Tpl4<T, <unnamed>> | Tpl4<T, <unnamed>> | | ||
| | noncompliant_specialization.h:16:19:16:41 | Tpl4<C1, 1> | Template specialization 'Tpl4<C1, 1>' is declared in a different file than $@ and all specialized template arguments. | template.h:13:34:13:37 | Tpl4<T, <unnamed>> | Tpl4<T, <unnamed>> | | ||
| | noncompliant_specialization.h:17:19:17:38 | Tpl4<C1, 1> | Template specialization 'Tpl4<C1, 1>' is declared in a different file than $@ and all specialized template arguments. | template.h:13:34:13:37 | Tpl4<T, <unnamed>> | Tpl4<T, <unnamed>> | | ||
| | noncompliant_specialization.h:18:24:18:43 | Tpl4<C2, N> | Template specialization 'Tpl4<C2, N>' is declared in a different file than $@ and all specialized template arguments. | template.h:13:34:13:37 | Tpl4<T, <unnamed>> | Tpl4<T, <unnamed>> | | ||
| | noncompliant_specialization.h:19:29:19:38 | Tpl4<T, 2> | Template specialization 'Tpl4<T, 2>' is declared in a different file than $@ and all specialized template arguments. | template.h:13:34:13:37 | Tpl4<T, <unnamed>> | Tpl4<T, <unnamed>> | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rules/RULE-6-2-3/TemplateSpecializationWrongLocation.ql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #ifndef CLASS_H | ||
| #define CLASS_H | ||
|
|
||
| namespace class_h { | ||
| class C1 {}; | ||
| class C2 {}; | ||
| } // namespace class_h | ||
|
|
||
| #endif // CLASS_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include "class.h" | ||
| #include "template.h" | ||
|
|
||
| namespace compliant_h { | ||
| class C1 {}; | ||
| } // namespace compliant_h | ||
|
|
||
| template <> class Tpl1<compliant_h::C1> {}; // COMPLIANT | ||
| template <> class Tpl2<compliant_h::C1, compliant_h::C1> {}; // COMPLIANT | ||
| template <> class Tpl2<template_h::C1, compliant_h::C1> {}; // COMPLIANT | ||
| template <> class Tpl2<class_h::C1, compliant_h::C1> {}; // COMPLIANT | ||
| template <> class Tpl2<compliant_h::C1, long> {}; // COMPLIANT | ||
|
|
||
| template <typename T> class Tpl2<T, compliant_h::C1> {}; // COMPLIANT | ||
|
|
||
| template<> class Tpl4<compliant_h::C1, 0> {}; // COMPLIANT | ||
| template<int N> class Tpl4<compliant_h::C1, N> {}; // COMPLIANT |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #include "template.h" | ||
|
|
||
| template <> class Tpl1<long> {}; // NON_COMPLIANT | ||
| template <> class Tpl1<template_h::C2> {}; // NON_COMPLIANT | ||
| template <> class Tpl1<class_h::C2> {}; // NON_COMPLIANT | ||
|
|
||
| template <> class Tpl2<long, long> {}; // NON_COMPLIANT | ||
| template <> class Tpl2<template_h::C2, template_h::C2> {}; // NON_COMPLIANT | ||
| template <> class Tpl2<class_h::C2, class_h::C2> {}; // NON_COMPLIANT | ||
|
|
||
| template <typename T> class Tpl2<long, T> {}; // NON_COMPLIANT | ||
| template <typename T> class Tpl2<class_h::C1, T> {}; // NON_COMPLIANT | ||
|
|
||
| template <> class Tpl3<1> {}; // NON_COMPLIANT | ||
| template <> class Tpl4<long, 0> {}; // NON_COMPLIANT | ||
| template <> class Tpl4<template_h::C1, 1> {}; // NON_COMPLIANT | ||
| template <> class Tpl4<class_h::C1, 1> {}; // NON_COMPLIANT | ||
| template <int N> class Tpl4<class_h::C2, N> {}; // NON_COMPLIANT | ||
| template <typename T> class Tpl4<T, 2> {}; // NON_COMPLIANT |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #ifndef TEMPLATE_H | ||
| #define TEMPLATE_H | ||
| #include "class.h" | ||
|
|
||
| namespace template_h { | ||
| class C1 {}; | ||
| class C2 {}; | ||
| } // namespace template_h | ||
|
|
||
| template <typename T> class Tpl1 {}; | ||
| template <typename A, typename B> class Tpl2 {}; | ||
| template <int> class Tpl3 {}; | ||
| template <typename T, int> class Tpl4 {}; | ||
|
|
||
| template <> class Tpl1<int> {}; // COMPLIANT | ||
| template <> class Tpl1<template_h::C1> {}; // COMPLIANT | ||
| template <> class Tpl1<class_h::C1> {}; // COMPLIANT | ||
|
|
||
| template <> class Tpl2<int, int> {}; // COMPLIANT | ||
| template <> class Tpl2<template_h::C1, template_h::C1> {}; // COMPLIANT | ||
| template <> class Tpl2<class_h::C1, class_h::C1> {}; // COMPLIANT | ||
|
|
||
| template <typename T> class Tpl2<int, T> {}; // COMPLIANT | ||
|
|
||
| template<> class Tpl3<0> {}; // COMPLIANT | ||
| template<> class Tpl4<int, 0> {}; // COMPLIANT | ||
| template<> class Tpl4<template_h::C1, 0> {}; // COMPLIANT | ||
| template<> class Tpl4<class_h::C1, 0> {}; // COMPLIANT | ||
|
|
||
| #endif // TEMPLATE_H |
Uh oh!
There was an error while loading. Please reload this page.