diff --git a/change_notes/2026-01-15-a3-1-4-extern-to-full.md b/change_notes/2026-01-15-a3-1-4-extern-to-full.md new file mode 100644 index 0000000000..f947630692 --- /dev/null +++ b/change_notes/2026-01-15-a3-1-4-extern-to-full.md @@ -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. diff --git a/cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.ql b/cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.ql deleted file mode 100644 index 3a6f335167..0000000000 --- a/cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.ql +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @id cpp/autosar/external-linkage-array-without-explicit-size - * @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 - -class ArrayDeclarationEntry extends VariableDeclarationEntry { - ArrayDeclarationEntry() { this.getUnspecifiedType() instanceof ArrayType } - - predicate hasExplicitSize() { - exists(ArrayType at | at = this.getUnspecifiedType() | at.hasArraySize()) - } -} - -from ArrayDeclarationEntry ade -where - // We only consider extern specified arrays because we can't determine the size of an incomplete object - // which makes it difficult to access its element in a safe way. - ade.hasSpecifier("extern") and - not isExcluded(ade, ScopePackage::externalLinkageArrayWithoutExplicitSizeQuery()) and - not ade.hasExplicitSize() -select ade, - "The declared array '" + ade.getName() + - "' with external linkage doesn't specify the size explicitly." diff --git a/cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.ql b/cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.ql new file mode 100644 index 0000000000..d10560d93d --- /dev/null +++ b/cpp/autosar/src/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.ql @@ -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() + } +} diff --git a/cpp/autosar/src/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.ql b/cpp/autosar/src/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.ql index 06c6ad517c..97c115fe48 100644 --- a/cpp/autosar/src/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.ql +++ b/cpp/autosar/src/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.ql @@ -17,25 +17,11 @@ import cpp import codingstandards.cpp.autosar -import codingstandards.cpp.Linkage -import codingstandards.cpp.EncapsulatingFunctions +import codingstandards.cpp.rules.externallinkagenotdeclaredinheaderfile.ExternalLinkageNotDeclaredInHeaderFile -from DeclarationEntry de, string kind -where - not isExcluded(de, IncludesPackage::externalLinkageNotDeclaredInHeaderFileQuery()) 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 kind = "function" else kind = "object" -select de, "Externally linked " + kind + " '" + de.getName() + "' not declared in header file." +class ExternalLinkageNotDeclaredInHeaderFileQuery extends ExternalLinkageNotDeclaredInHeaderFileSharedQuery +{ + ExternalLinkageNotDeclaredInHeaderFileQuery() { + this = IncludesPackage::externalLinkageNotDeclaredInHeaderFileQuery() + } +} diff --git a/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.expected b/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.expected deleted file mode 100644 index fc237608ba..0000000000 --- a/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.expected +++ /dev/null @@ -1 +0,0 @@ -| test.cpp:1:12:1:17 | declaration of array1 | The declared array 'array1' with external linkage doesn't specify the size explicitly. | diff --git a/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.qlref b/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.qlref deleted file mode 100644 index 4b111a0e46..0000000000 --- a/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.qlref +++ /dev/null @@ -1 +0,0 @@ -rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSize.ql \ No newline at end of file diff --git a/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.testref b/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.testref new file mode 100644 index 0000000000..ff3f9dac42 --- /dev/null +++ b/cpp/autosar/test/rules/A3-1-4/ExternalLinkageArrayWithoutExplicitSizeAutosar.testref @@ -0,0 +1 @@ +cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.ql \ No newline at end of file diff --git a/cpp/autosar/test/rules/A3-1-4/test.cpp b/cpp/autosar/test/rules/A3-1-4/test.cpp deleted file mode 100644 index 8b5ac817d3..0000000000 --- a/cpp/autosar/test/rules/A3-1-4/test.cpp +++ /dev/null @@ -1,8 +0,0 @@ -extern int array1[]; // NON_COMPLIANT -extern int array2[2]; // COMPLIANT -constexpr int size1 = 2; -extern int - array3[size1]; // COMPLIANT; size can be explicitly determined through size1 -const int size2 = 2; -extern int - array4[size2]; // COMPLIANT; size can be explicitly determined through size2 \ No newline at end of file diff --git a/cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.qlref b/cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.qlref deleted file mode 100644 index 87cc111ea3..0000000000 --- a/cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.qlref +++ /dev/null @@ -1 +0,0 @@ -rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.ql \ No newline at end of file diff --git a/cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.testref b/cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.testref new file mode 100644 index 0000000000..3afbfbed70 --- /dev/null +++ b/cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.testref @@ -0,0 +1 @@ +cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.ql \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/cpp/Linkage1.qll b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Linkage1.qll new file mode 100644 index 0000000000..ff010831e9 --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Linkage1.qll @@ -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())) + } +} diff --git a/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll b/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll index 0c3cbcc28c..97e29240fb 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll @@ -34,6 +34,7 @@ import IntegerConversion import Invariants import Iterators import Lambdas +import Linkage1 import Literals import Loops import Macros @@ -96,6 +97,7 @@ newtype TCPPQuery = TInvariantsPackageQuery(InvariantsQuery q) or TIteratorsPackageQuery(IteratorsQuery q) or TLambdasPackageQuery(LambdasQuery q) or + TLinkage1PackageQuery(Linkage1Query q) or TLiteralsPackageQuery(LiteralsQuery q) or TLoopsPackageQuery(LoopsQuery q) or TMacrosPackageQuery(MacrosQuery q) or @@ -158,6 +160,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat isInvariantsQueryMetadata(query, queryId, ruleId, category) or isIteratorsQueryMetadata(query, queryId, ruleId, category) or isLambdasQueryMetadata(query, queryId, ruleId, category) or + isLinkage1QueryMetadata(query, queryId, ruleId, category) or isLiteralsQueryMetadata(query, queryId, ruleId, category) or isLoopsQueryMetadata(query, queryId, ruleId, category) or isMacrosQueryMetadata(query, queryId, ruleId, category) or diff --git a/cpp/common/src/codingstandards/cpp/exclusions/cpp/Scope.qll b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Scope.qll index c4a21040eb..9576e5ac91 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/cpp/Scope.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/cpp/Scope.qll @@ -5,7 +5,7 @@ import codingstandards.cpp.exclusions.RuleMetadata newtype ScopeQuery = TNonStandardEntitiesInStandardNamespacesQuery() or - TExternalLinkageArrayWithoutExplicitSizeQuery() or + TExternalLinkageArrayWithoutExplicitSizeAutosarQuery() or THiddenInheritedNonOverridableMemberFunctionQuery() or THiddenInheritedOverridableMemberFunctionQuery() or TDefinitionNotConsideredForUnqualifiedLookupQuery() or @@ -33,11 +33,11 @@ predicate isScopeQueryMetadata(Query query, string queryId, string ruleId, strin category = "required" or query = - // `Query` instance for the `externalLinkageArrayWithoutExplicitSize` query - ScopePackage::externalLinkageArrayWithoutExplicitSizeQuery() and + // `Query` instance for the `externalLinkageArrayWithoutExplicitSizeAutosar` query + ScopePackage::externalLinkageArrayWithoutExplicitSizeAutosarQuery() and queryId = - // `@id` for the `externalLinkageArrayWithoutExplicitSize` query - "cpp/autosar/external-linkage-array-without-explicit-size" and + // `@id` for the `externalLinkageArrayWithoutExplicitSizeAutosar` query + "cpp/autosar/external-linkage-array-without-explicit-size-autosar" and ruleId = "A3-1-4" and category = "required" or @@ -185,11 +185,11 @@ module ScopePackage { TQueryCPP(TScopePackageQuery(TNonStandardEntitiesInStandardNamespacesQuery())) } - Query externalLinkageArrayWithoutExplicitSizeQuery() { + Query externalLinkageArrayWithoutExplicitSizeAutosarQuery() { //autogenerate `Query` type result = - // `Query` type for `externalLinkageArrayWithoutExplicitSize` query - TQueryCPP(TScopePackageQuery(TExternalLinkageArrayWithoutExplicitSizeQuery())) + // `Query` type for `externalLinkageArrayWithoutExplicitSizeAutosar` query + TQueryCPP(TScopePackageQuery(TExternalLinkageArrayWithoutExplicitSizeAutosarQuery())) } Query hiddenInheritedNonOverridableMemberFunctionQuery() { diff --git a/cpp/common/src/codingstandards/cpp/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.qll b/cpp/common/src/codingstandards/cpp/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.qll new file mode 100644 index 0000000000..33f771c10d --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.qll @@ -0,0 +1,29 @@ +/** + * Provides a library with a `problems` predicate for the following issue: + * Introducing a function or object with external linkage outside of a header file can + * cause developer confusion about its translation unit access semantics. + */ + +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 + // Holds is if declEntry is an array variable declaration (not a definition) + v.getADeclarationEntry() = declEntry and + not declEntry.isDefinition() and + arrayType = v.getType().stripTopLevelSpecifiers() + ) +} diff --git a/cpp/common/src/codingstandards/cpp/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.qll b/cpp/common/src/codingstandards/cpp/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.qll new file mode 100644 index 0000000000..ea865a3c2f --- /dev/null +++ b/cpp/common/src/codingstandards/cpp/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.qll @@ -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." +} diff --git a/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.expected b/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.expected new file mode 100644 index 0000000000..1733ad190c --- /dev/null +++ b/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.expected @@ -0,0 +1 @@ +| test.cpp:19:14:19:15 | declaration of e1 | The declared array 'e1' with external linkage doesn't specify the size explicitly. | diff --git a/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.ql b/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.ql new file mode 100644 index 0000000000..98882ca380 --- /dev/null +++ b/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.ql @@ -0,0 +1,4 @@ +// GENERATED FILE - DO NOT MODIFY +import codingstandards.cpp.rules.externallinkagearraywithoutexplicitsize.ExternalLinkageArrayWithoutExplicitSize + +class TestFileQuery extends ExternalLinkageArrayWithoutExplicitSizeSharedQuery, TestQuery { } diff --git a/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/test.cpp b/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/test.cpp new file mode 100644 index 0000000000..a96177782d --- /dev/null +++ b/cpp/common/test/rules/externallinkagearraywithoutexplicitsize/test.cpp @@ -0,0 +1,29 @@ +#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 + // our external linkage lib - also FAMs are not in scope for this rule + static const int flexibleArrayMember[]; // COMPLIANT +}; \ No newline at end of file diff --git a/cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.expected b/cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.expected similarity index 100% rename from cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.expected rename to cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.expected diff --git a/cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.ql b/cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.ql new file mode 100644 index 0000000000..5e0662d739 --- /dev/null +++ b/cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.ql @@ -0,0 +1,4 @@ +// GENERATED FILE - DO NOT MODIFY +import codingstandards.cpp.rules.externallinkagenotdeclaredinheaderfile.ExternalLinkageNotDeclaredInHeaderFile + +class TestFileQuery extends ExternalLinkageNotDeclaredInHeaderFileSharedQuery, TestQuery { } diff --git a/cpp/autosar/test/rules/A3-3-1/test.cpp b/cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/test.cpp similarity index 100% rename from cpp/autosar/test/rules/A3-3-1/test.cpp rename to cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/test.cpp diff --git a/cpp/autosar/test/rules/A3-3-1/test.hpp b/cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/test.hpp similarity index 100% rename from cpp/autosar/test/rules/A3-3-1/test.hpp rename to cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/test.hpp diff --git a/cpp/misra/src/rules/RULE-6-0-2/ExternalLinkageArrayWithoutExplicitSizeMisra.ql b/cpp/misra/src/rules/RULE-6-0-2/ExternalLinkageArrayWithoutExplicitSizeMisra.ql new file mode 100644 index 0000000000..1889093463 --- /dev/null +++ b/cpp/misra/src/rules/RULE-6-0-2/ExternalLinkageArrayWithoutExplicitSizeMisra.ql @@ -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() + } +} diff --git a/cpp/misra/src/rules/RULE-6-5-1/ExternalLinkageNotDeclaredInHeaderFileMisra.ql b/cpp/misra/src/rules/RULE-6-5-1/ExternalLinkageNotDeclaredInHeaderFileMisra.ql new file mode 100644 index 0000000000..62d753f9d0 --- /dev/null +++ b/cpp/misra/src/rules/RULE-6-5-1/ExternalLinkageNotDeclaredInHeaderFileMisra.ql @@ -0,0 +1,27 @@ +/** + * @id cpp/misra/external-linkage-not-declared-in-header-file-misra + * @name RULE-6-5-1: Objects or functions with external linkage shall be declared in a header file + * @description Using objects or functions with external linkage in implementation files makes code + * harder to understand. + * @kind problem + * @precision very-high + * @problem.severity warning + * @tags external/misra/id/rule-6-5-1 + * correctness + * maintainability + * readability + * scope/single-translation-unit + * external/misra/enforcement/decidable + * external/misra/obligation/advisory + */ + +import cpp +import codingstandards.cpp.misra +import codingstandards.cpp.rules.externallinkagenotdeclaredinheaderfile.ExternalLinkageNotDeclaredInHeaderFile + +class ExternalLinkageNotDeclaredInHeaderFileMisraQuery extends ExternalLinkageNotDeclaredInHeaderFileSharedQuery +{ + ExternalLinkageNotDeclaredInHeaderFileMisraQuery() { + this = Linkage1Package::externalLinkageNotDeclaredInHeaderFileMisraQuery() + } +} diff --git a/cpp/misra/test/rules/RULE-6-0-2/ExternalLinkageArrayWithoutExplicitSizeMisra.testref b/cpp/misra/test/rules/RULE-6-0-2/ExternalLinkageArrayWithoutExplicitSizeMisra.testref new file mode 100644 index 0000000000..ff3f9dac42 --- /dev/null +++ b/cpp/misra/test/rules/RULE-6-0-2/ExternalLinkageArrayWithoutExplicitSizeMisra.testref @@ -0,0 +1 @@ +cpp/common/test/rules/externallinkagearraywithoutexplicitsize/ExternalLinkageArrayWithoutExplicitSize.ql \ No newline at end of file diff --git a/cpp/misra/test/rules/RULE-6-5-1/ExternalLinkageNotDeclaredInHeaderFileMisra.testref b/cpp/misra/test/rules/RULE-6-5-1/ExternalLinkageNotDeclaredInHeaderFileMisra.testref new file mode 100644 index 0000000000..3afbfbed70 --- /dev/null +++ b/cpp/misra/test/rules/RULE-6-5-1/ExternalLinkageNotDeclaredInHeaderFileMisra.testref @@ -0,0 +1 @@ +cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.ql \ No newline at end of file diff --git a/rule_packages/cpp/Includes.json b/rule_packages/cpp/Includes.json index 1d7f97ed04..ca23278fea 100644 --- a/rule_packages/cpp/Includes.json +++ b/rule_packages/cpp/Includes.json @@ -119,6 +119,7 @@ "precision": "very-high", "severity": "warning", "short_name": "ExternalLinkageNotDeclaredInHeaderFile", + "shared_implementation_short_name": "ExternalLinkageNotDeclaredInHeaderFile", "tags": [ "correctness", "maintainability", diff --git a/rule_packages/cpp/Linkage1.json b/rule_packages/cpp/Linkage1.json new file mode 100644 index 0000000000..7e5fb8e878 --- /dev/null +++ b/rule_packages/cpp/Linkage1.json @@ -0,0 +1,51 @@ +{ + "MISRA-C++-2023": { + "RULE-6-0-2": { + "properties": { + "enforcement": "decidable", + "obligation": "advisory" + }, + "queries": [ + { + "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", + "name": "Arrays with external linkage declared without explicit size shall not be used", + "precision": "very-high", + "severity": "warning", + "short_name": "ExternalLinkageArrayWithoutExplicitSizeMisra", + "shared_implementation_short_name": "ExternalLinkageArrayWithoutExplicitSize", + "tags": [ + "maintainability", + "readability", + "scope/single-translation-unit" + ] + } + ], + "title": "When an array with external linkage is declared, its size should be explicitly specified" + }, + "RULE-6-5-1": { + "properties": { + "enforcement": "decidable", + "obligation": "advisory" + }, + "queries": [ + { + "description": "Using objects or functions with external linkage in implementation files makes code harder to understand.", + "kind": "problem", + "name": "Objects or functions with external linkage shall be declared in a header file", + "precision": "very-high", + "severity": "warning", + "short_name": "ExternalLinkageNotDeclaredInHeaderFileMisra", + "shared_implementation_short_name": "ExternalLinkageNotDeclaredInHeaderFile", + "tags": [ + "correctness", + "maintainability", + "readability", + "scope/single-translation-unit" + ] + } + ], + "title": "A function or object with external linkage should be introduced in a header file" + } + } +} \ No newline at end of file diff --git a/rule_packages/cpp/Scope.json b/rule_packages/cpp/Scope.json index 6fc3aa8487..f32b6bf41a 100644 --- a/rule_packages/cpp/Scope.json +++ b/rule_packages/cpp/Scope.json @@ -40,7 +40,8 @@ "name": "When an array with external linkage is declared, its size shall be stated explicitly", "precision": "very-high", "severity": "warning", - "short_name": "ExternalLinkageArrayWithoutExplicitSize", + "short_name": "ExternalLinkageArrayWithoutExplicitSizeAutosar", + "shared_implementation_short_name": "ExternalLinkageArrayWithoutExplicitSize", "tags": [ "correctness" ] diff --git a/rules.csv b/rules.csv index a2eb7eddd8..1ffd63bdbb 100644 --- a/rules.csv +++ b/rules.csv @@ -850,7 +850,7 @@ cpp,MISRA-C++-2023,RULE-5-13-5,Yes,Required,Decidable,Single Translation Unit,Th cpp,MISRA-C++-2023,RULE-5-13-6,Yes,Required,Decidable,Single Translation Unit,An integer-literal of type long long shall not use a single L or l in any suffix,,Expressions2,Easy, cpp,MISRA-C++-2023,RULE-5-13-7,No,Required,Decidable,Single Translation Unit,String literals with different encoding prefixes shall not be concatenated,A2-13-2,,, cpp,MISRA-C++-2023,RULE-6-0-1,Yes,Required,Decidable,Single Translation Unit,Block scope declarations shall not be visually ambiguous,"M3-1-2,DCL53-CPP",Declarations2,Easy, -cpp,MISRA-C++-2023,RULE-6-0-2,Yes,Advisory,Decidable,Single Translation Unit,"When an array with external linkage is declared, its size should be explicitly specified",RULE-18-8,Linkage,Easy, +cpp,MISRA-C++-2023,RULE-6-0-2,Yes,Advisory,Decidable,Single Translation Unit,"When an array with external linkage is declared, its size should be explicitly specified",RULE-18-8,Linkage1,Import, cpp,MISRA-C++-2023,RULE-6-0-3,Yes,Advisory,Decidable,Single Translation Unit,"The only declarations in the global namespace should be main, namespace declarations and extern ""C"" declarations",M7-3-1,ImportMisra23,Import, cpp,MISRA-C++-2023,RULE-6-0-4,Yes,Required,Decidable,Single Translation Unit,The identifier main shall not be used for a function other than the global function main,M7-3-2,ImportMisra23,Import, cpp,MISRA-C++-2023,RULE-6-2-1,Yes,Required,Decidable,System,The one-definition rule shall not be violated,M3-2-2,ImportMisra23,Import, @@ -860,7 +860,7 @@ cpp,MISRA-C++-2023,RULE-6-2-4,Yes,Required,Decidable,Single Translation Unit,A h cpp,MISRA-C++-2023,RULE-6-4-1,Yes,Required,Decidable,Single Translation Unit,A variable declared in an inner scope shall not hide a variable declared in an outer scope,A2-10-1,ImportMisra23,Import, cpp,MISRA-C++-2023,RULE-6-4-2,Yes,Required,Decidable,Single Translation Unit,Derived classes shall not conceal functions that are inherited from their bases,A7-3-1,ImportMisra23,Import, cpp,MISRA-C++-2023,RULE-6-4-3,Yes,Required,Decidable,Single Translation Unit,A name that is present in a dependent base shall not be resolved by unqualified lookup,M14-6-1,ImportMisra23,Import, -cpp,MISRA-C++-2023,RULE-6-5-1,Yes,Advisory,Decidable,Single Translation Unit,A function or object with external linkage should be introduced in a header file,,Linkage,Medium, +cpp,MISRA-C++-2023,RULE-6-5-1,Yes,Advisory,Decidable,Single Translation Unit,A function or object with external linkage should be introduced in a header file,A3-3-1,Linkage1,Import, cpp,MISRA-C++-2023,RULE-6-5-2,Yes,Advisory,Decidable,Single Translation Unit,Internal linkage should be specified appropriately,,Linkage,Medium, cpp,MISRA-C++-2023,RULE-6-7-1,Yes,Required,Decidable,Single Translation Unit,Local variables shall not have static storage duration,,Declarations2,Easy, cpp,MISRA-C++-2023,RULE-6-7-2,Yes,Required,Decidable,Single Translation Unit,Global variables shall not be used,,Banned,Easy,