diff --git a/.github/workflows/validate-rules-csv.yml b/.github/workflows/validate-rules-csv.yml new file mode 100644 index 0000000000..1c6418c98f --- /dev/null +++ b/.github/workflows/validate-rules-csv.yml @@ -0,0 +1,28 @@ +name: ⚙️ Validate Rules CSV + +on: + push: + branches: + - main + - "rc/**" + - next + pull_request: + branches: + - main + - "rc/**" + - next + + +jobs: + validate-rules-csv: + name: Validate Rules CSV + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Check Rules + shell: pwsh + run: scripts/util/Get-DuplicateRules.ps1 -Language 'all' -CIMode + + \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index d1f141cced..e2b393727f 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -185,6 +185,7 @@ "type": "pickString", "options": [ "Allocations", + "Banned", "BannedFunctions", "BannedLibraries", "BannedSyntax", diff --git a/c/misra/src/rules/RULE-4-10/PrecautionIncludeGuardsNotProvided.ql b/c/misra/src/rules/DIR-4-10/PrecautionIncludeGuardsNotProvided.ql similarity index 81% rename from c/misra/src/rules/RULE-4-10/PrecautionIncludeGuardsNotProvided.ql rename to c/misra/src/rules/DIR-4-10/PrecautionIncludeGuardsNotProvided.ql index deea2afa83..58ec5c80a9 100644 --- a/c/misra/src/rules/RULE-4-10/PrecautionIncludeGuardsNotProvided.ql +++ b/c/misra/src/rules/DIR-4-10/PrecautionIncludeGuardsNotProvided.ql @@ -1,12 +1,12 @@ /** * @id c/misra/precaution-include-guards-not-provided - * @name RULE-4-10: Precautions shall be taken in order to prevent the contents of a header file being included more than once + * @name DIR-4-10: Precautions shall be taken in order to prevent the contents of a header file being included more than once * @description Using anything other than a standard include guard form can make code confusing and * can lead to multiple or conflicting definitions. * @kind problem * @precision very-high * @problem.severity warning - * @tags external/misra/id/rule-4-10 + * @tags external/misra/id/dir-4-10 * correctness * maintainability * readability diff --git a/c/misra/src/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.ql b/c/misra/src/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.ql similarity index 91% rename from c/misra/src/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.ql rename to c/misra/src/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.ql index 84055145e3..a759a631b7 100644 --- a/c/misra/src/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.ql +++ b/c/misra/src/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.ql @@ -1,13 +1,13 @@ /** * @id c/misra/std-lib-dynamic-memory-allocation-used - * @name RULE-4-12: Dynamic memory allocation shall not be used + * @name DIR-4-12: Dynamic memory allocation shall not be used * @description Using dynamic memory allocation and deallocation can result to undefined behavior. * This query is for the Standard Library Implementation. Any implementation outside it * will require a separate query under the same directive. * @kind problem * @precision very-high * @problem.severity error - * @tags external/misra/id/rule-4-12 + * @tags external/misra/id/dir-4-12 * security * correctness * maintainability diff --git a/c/misra/src/rules/RULE-4-4/SectionsOfCodeShallNotBeCommentedOut.ql b/c/misra/src/rules/DIR-4-4/SectionsOfCodeShallNotBeCommentedOut.ql similarity index 87% rename from c/misra/src/rules/RULE-4-4/SectionsOfCodeShallNotBeCommentedOut.ql rename to c/misra/src/rules/DIR-4-4/SectionsOfCodeShallNotBeCommentedOut.ql index 52adbac29d..1831f211dd 100644 --- a/c/misra/src/rules/RULE-4-4/SectionsOfCodeShallNotBeCommentedOut.ql +++ b/c/misra/src/rules/DIR-4-4/SectionsOfCodeShallNotBeCommentedOut.ql @@ -1,11 +1,11 @@ /** * @id c/misra/sections-of-code-shall-not-be-commented-out - * @name RULE-4-4: Sections of code should not be commented out + * @name DIR-4-4: Sections of code should not be commented out * @description Commented out code may become out of date leading to developer confusion. * @kind problem * @precision high * @problem.severity warning - * @tags external/misra/id/rule-4-4 + * @tags external/misra/id/dir-4-4 * maintainability * readability * correctness diff --git a/c/misra/src/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql b/c/misra/src/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql similarity index 89% rename from c/misra/src/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql rename to c/misra/src/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql index 1521b9602a..56f2dd785d 100644 --- a/c/misra/src/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql +++ b/c/misra/src/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql @@ -1,13 +1,13 @@ /** * @id c/misra/object-with-no-pointer-dereference-should-be-opaque - * @name RULE-4-8: The implementation of an object shall be hidden if a pointer to its structure or union is never dereferenced within a translation unit + * @name DIR-4-8: The implementation of an object shall be hidden if a pointer to its structure or union is never dereferenced within a translation unit * @description If a pointer to a structure or union is never dereferenced within a translation * unit, then the implementation of the object should be hidden to prevent * unintentional changes. * @kind problem * @precision very-high * @problem.severity error - * @tags external/misra/id/rule-4-8 + * @tags external/misra/id/dir-4-8 * readability * maintainability * external/misra/obligation/advisory diff --git a/c/misra/test/rules/RULE-4-10/PrecautionIncludeGuardsNotProvided.testref b/c/misra/test/rules/DIR-4-10/PrecautionIncludeGuardsNotProvided.testref similarity index 100% rename from c/misra/test/rules/RULE-4-10/PrecautionIncludeGuardsNotProvided.testref rename to c/misra/test/rules/DIR-4-10/PrecautionIncludeGuardsNotProvided.testref diff --git a/c/misra/test/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.expected b/c/misra/test/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.expected similarity index 100% rename from c/misra/test/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.expected rename to c/misra/test/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.expected diff --git a/c/misra/test/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.qlref b/c/misra/test/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.qlref new file mode 100644 index 0000000000..b0cf5247b5 --- /dev/null +++ b/c/misra/test/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.qlref @@ -0,0 +1 @@ +rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-4-12/test.c b/c/misra/test/rules/DIR-4-12/test.c similarity index 100% rename from c/misra/test/rules/RULE-4-12/test.c rename to c/misra/test/rules/DIR-4-12/test.c diff --git a/c/misra/test/rules/RULE-4-4/SectionsOfCodeShallNotBeCommentedOut.testref b/c/misra/test/rules/DIR-4-4/SectionsOfCodeShallNotBeCommentedOut.testref similarity index 100% rename from c/misra/test/rules/RULE-4-4/SectionsOfCodeShallNotBeCommentedOut.testref rename to c/misra/test/rules/DIR-4-4/SectionsOfCodeShallNotBeCommentedOut.testref diff --git a/c/misra/test/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.expected b/c/misra/test/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.expected similarity index 100% rename from c/misra/test/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.expected rename to c/misra/test/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.expected diff --git a/c/misra/test/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.qlref b/c/misra/test/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.qlref new file mode 100644 index 0000000000..dfa03268cf --- /dev/null +++ b/c/misra/test/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.qlref @@ -0,0 +1 @@ +rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-4-8/test.c b/c/misra/test/rules/DIR-4-8/test.c similarity index 100% rename from c/misra/test/rules/RULE-4-8/test.c rename to c/misra/test/rules/DIR-4-8/test.c diff --git a/c/misra/test/rules/RULE-4-8/test.h b/c/misra/test/rules/DIR-4-8/test.h similarity index 100% rename from c/misra/test/rules/RULE-4-8/test.h rename to c/misra/test/rules/DIR-4-8/test.h diff --git a/c/misra/test/rules/RULE-4-8/test_2.c b/c/misra/test/rules/DIR-4-8/test_2.c similarity index 100% rename from c/misra/test/rules/RULE-4-8/test_2.c rename to c/misra/test/rules/DIR-4-8/test_2.c diff --git a/c/misra/test/rules/RULE-4-8/test_shared.h b/c/misra/test/rules/DIR-4-8/test_shared.h similarity index 100% rename from c/misra/test/rules/RULE-4-8/test_shared.h rename to c/misra/test/rules/DIR-4-8/test_shared.h diff --git a/c/misra/test/rules/RULE-4-10/NonUniqueIncludeGuards.testref b/c/misra/test/rules/RULE-4-10/NonUniqueIncludeGuards.testref deleted file mode 100644 index e38907a2fc..0000000000 --- a/c/misra/test/rules/RULE-4-10/NonUniqueIncludeGuards.testref +++ /dev/null @@ -1 +0,0 @@ -c/common/test/rules/nonuniqueincludeguardsused/NonUniqueIncludeGuardsUsed.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.qlref b/c/misra/test/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.qlref deleted file mode 100644 index a0602b1905..0000000000 --- a/c/misra/test/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.qlref +++ /dev/null @@ -1 +0,0 @@ -rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.ql \ No newline at end of file diff --git a/c/misra/test/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.qlref b/c/misra/test/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.qlref deleted file mode 100644 index 4a5c410c38..0000000000 --- a/c/misra/test/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.qlref +++ /dev/null @@ -1 +0,0 @@ -rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql \ No newline at end of file diff --git a/change_notes/2022-11-04-refactor-dir-rules.md b/change_notes/2022-11-04-refactor-dir-rules.md new file mode 100644 index 0000000000..303f718ddd --- /dev/null +++ b/change_notes/2022-11-04-refactor-dir-rules.md @@ -0,0 +1,9 @@ +- The following rules have been renamed: + - RULE-4-4 has been renamed to DIR-4-4 to reflect correct naming as per + MISRA C:2012 standard. + - RULE-4-8 has been renamed to DIR-4-8 to reflect correct naming as per + MISRA C:2012 standard. + - RULE-4-10 has been renamed to DIR-4-10 to reflect correct naming as per + MISRA C:2012 standard. + - RULE-4-12 has been renamed to DIR-4-12 to reflect correct naming as per + MISRA C:2012 standard. \ No newline at end of file diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Banned.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Banned.qll index 7a9af8467d..615b16c0ca 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Banned.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Banned.qll @@ -159,7 +159,7 @@ predicate isBannedQueryMetadata(Query query, string queryId, string ruleId) { queryId = // `@id` for the `stdLibDynamicMemoryAllocationUsed` query "c/misra/std-lib-dynamic-memory-allocation-used" and - ruleId = "RULE-4-12" + ruleId = "DIR-4-12" or query = // `Query` instance for the `octalConstantsUsed` query diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Pointers1.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Pointers1.qll index 506e3a5fba..11daf5543c 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Pointers1.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Pointers1.qll @@ -149,7 +149,7 @@ predicate isPointers1QueryMetadata(Query query, string queryId, string ruleId) { queryId = // `@id` for the `objectWithNoPointerDereferenceShouldBeOpaque` query "c/misra/object-with-no-pointer-dereference-should-be-opaque" and - ruleId = "RULE-4-8" + ruleId = "DIR-4-8" or query = // `Query` instance for the `pointerShouldPointToConstTypeWhenPossible` query diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Preprocessor2.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Preprocessor2.qll index 942f633f45..f8a1c8ba3b 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Preprocessor2.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Preprocessor2.qll @@ -40,7 +40,7 @@ predicate isPreprocessor2QueryMetadata(Query query, string queryId, string ruleI queryId = // `@id` for the `precautionIncludeGuardsNotProvided` query "c/misra/precaution-include-guards-not-provided" and - ruleId = "RULE-4-10" + ruleId = "DIR-4-10" } module Preprocessor2Package { diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Syntax.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Syntax.qll index 8109741e20..901b6393a4 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Syntax.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Syntax.qll @@ -43,7 +43,7 @@ predicate isSyntaxQueryMetadata(Query query, string queryId, string ruleId) { queryId = // `@id` for the `sectionsOfCodeShallNotBeCommentedOut` query "c/misra/sections-of-code-shall-not-be-commented-out" and - ruleId = "RULE-4-4" + ruleId = "DIR-4-4" or query = // `Query` instance for the `identifiersInTheSameNameSpaceUnambiguous` query diff --git a/rule_packages/c/Banned.json b/rule_packages/c/Banned.json index 46032e3a68..42decbb3e3 100644 --- a/rule_packages/c/Banned.json +++ b/rule_packages/c/Banned.json @@ -304,7 +304,7 @@ ], "title": "The Standard Library functions 'bsearch' and 'qsort' of 'stdlib.h' shall not be used" }, - "RULE-4-12": { + "DIR-4-12": { "properties": { "obligation": "required" }, diff --git a/rule_packages/c/Pointers1.json b/rule_packages/c/Pointers1.json index 991838e34f..6b2df1595c 100644 --- a/rule_packages/c/Pointers1.json +++ b/rule_packages/c/Pointers1.json @@ -294,7 +294,7 @@ ], "title": "The address of an object with automatic storage shall not be copied to another object that persists after the first object has ceased to exist" }, - "RULE-4-8": { + "DIR-4-8": { "properties": { "obligation": "advisory" }, diff --git a/rule_packages/c/Preprocessor2.json b/rule_packages/c/Preprocessor2.json index 66e759c5b9..9eeb7beba8 100644 --- a/rule_packages/c/Preprocessor2.json +++ b/rule_packages/c/Preprocessor2.json @@ -63,7 +63,7 @@ ], "title": "#undef should not be used" }, - "RULE-4-10": { + "DIR-4-10": { "properties": { "obligation": "required" }, diff --git a/rule_packages/c/Syntax.json b/rule_packages/c/Syntax.json index 476254130b..d294c44183 100644 --- a/rule_packages/c/Syntax.json +++ b/rule_packages/c/Syntax.json @@ -62,7 +62,7 @@ ], "title": "Octal and hexadecimal escape sequences shall be terminated" }, - "RULE-4-4": { + "DIR-4-4": { "properties": { "obligation": "advisory" }, diff --git a/rules.csv b/rules.csv index ec8b29eaf9..b10e97a9ba 100644 --- a/rules.csv +++ b/rules.csv @@ -599,23 +599,23 @@ c,CERT-C,STR34-C,Yes,Rule,,,Cast characters to unsigned char before converting t c,CERT-C,STR37-C,Yes,Rule,,,Arguments to character-handling functions must be representable as an unsigned char,,Strings2,Medium, c,CERT-C,STR38-C,Yes,Rule,,,Do not confuse narrow and wide character strings and functions,,Strings3,Medium, c,CERT-C,WIN30-C,OutOfScope,Rule,,,Properly pair allocation and deallocation functions,DCL54-CPP,,Easy, -c,MISRA-C-2012,RULE-1-1,No,Required,,,Any implementation-defined behaviour on which the output of the program depends shall be documented and understood,,,, -c,MISRA-C-2012,RULE-2-1,Yes,Required,,,All source files shall compile without any compilation errors,A1-4-3,Language,Medium, -c,MISRA-C-2012,RULE-3-1,No,Required,,,All code shall be traceable to documented requirements,,,, -c,MISRA-C-2012,RULE-4-1,No,Required,,,Run-time failures shall be minimized,,,, -c,MISRA-C-2012,RULE-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language,Import, +c,MISRA-C-2012,DIR-1-1,No,Required,,,Any implementation-defined behaviour on which the output of the program depends shall be documented and understood,,,, +c,MISRA-C-2012,DIR-2-1,Yes,Required,,,All source files shall compile without any compilation errors,A1-4-3,Language,Medium, +c,MISRA-C-2012,DIR-3-1,No,Required,,,All code shall be traceable to documented requirements,,,, +c,MISRA-C-2012,DIR-4-1,No,Required,,,Run-time failures shall be minimized,,,, +c,MISRA-C-2012,DIR-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language,Import, c,MISRA-C-2012,DIR-4-3,Yes,Required,,,Assembly language shall be encapsulated and isolated,,Language1,Medium, -c,MISRA-C-2012,RULE-4-4,Yes,Advisory,,,Sections of code should not be commented out,A2-7-2,Syntax,Import, +c,MISRA-C-2012,DIR-4-4,Yes,Advisory,,,Sections of code should not be commented out,A2-7-2,Syntax,Import, c,MISRA-C-2012,DIR-4-5,Yes,Advisory,,,Identifiers in the same name space with overlapping visibility should be typographically unambiguous,M2-10-1,Syntax,Easy, -c,MISRA-C-2012,RULE-4-6,Yes,Advisory,,,typedefs that indicate size and signedness should be used in place of the basic numerical types,,Types,Hard, -c,MISRA-C-2012,RULE-4-7,Yes,Required,,,"If a function returns error information, then that error information shall be tested",M0-3-2,Contracts,Import, -c,MISRA-C-2012,RULE-4-8,Yes,Advisory,,,"If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden",,Pointers1,Medium, -c,MISRA-C-2012,RULE-4-9,Yes,Advisory,,,A function should be used in preference to a function-like macro where they are interchangeable,,Preprocessor,Medium, -c,MISRA-C-2012,RULE-4-10,Yes,Required,,,Precautions shall be taken in order to prevent the contents of a header file being included more than once,M16-2-3,Preprocessor2,Medium, -c,MISRA-C-2012,RULE-4-11,Yes,Required,,,The validity of values passed to library functions shall be checked,,Contracts,Hard, -c,MISRA-C-2012,RULE-4-12,Yes,Required,,,Dynamic memory allocation shall not be used,,Banned,Medium, -c,MISRA-C-2012,RULE-4-13,Yes,Advisory,,,Functions which are designed to provide operations on a resource should be called in an appropriate sequence,,Contracts,Hard, -c,MISRA-C-2012,RULE-4-14,Yes,Required,,,The validity of values received from external sources shall be checked,,Contracts,Hard, +c,MISRA-C-2012,DIR-4-6,Yes,Advisory,,,typedefs that indicate size and signedness should be used in place of the basic numerical types,,Types,Hard, +c,MISRA-C-2012,DIR-4-7,Yes,Required,,,"If a function returns error information, then that error information shall be tested",M0-3-2,Contracts,Import, +c,MISRA-C-2012,DIR-4-8,Yes,Advisory,,,"If a pointer to a structure or union is never dereferenced within a translation unit, then the implementation of the object should be hidden",,Pointers1,Medium, +c,MISRA-C-2012,DIR-4-9,Yes,Advisory,,,A function should be used in preference to a function-like macro where they are interchangeable,,Preprocessor,Medium, +c,MISRA-C-2012,DIR-4-10,Yes,Required,,,Precautions shall be taken in order to prevent the contents of a header file being included more than once,M16-2-3,Preprocessor2,Medium, +c,MISRA-C-2012,DIR-4-11,Yes,Required,,,The validity of values passed to library functions shall be checked,,Contracts,Hard, +c,MISRA-C-2012,DIR-4-12,Yes,Required,,,Dynamic memory allocation shall not be used,,Banned,Medium, +c,MISRA-C-2012,DIR-4-13,Yes,Advisory,,,Functions which are designed to provide operations on a resource should be called in an appropriate sequence,,Contracts,Hard, +c,MISRA-C-2012,DIR-4-14,Yes,Required,,,The validity of values received from external sources shall be checked,,Contracts,Hard, c,MISRA-C-2012,RULE-1-1,Yes,Required,,,"The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation�s translation limits",,Language,Easy, c,MISRA-C-2012,RULE-1-2,Yes,Advisory,,,Language extensions should not be used,,Language,Easy, c,MISRA-C-2012,RULE-1-3,Yes,Required,,,There shall be no occurrence of undefined or critical unspecified behaviour,,Language,Hard, diff --git a/scripts/PSCodingStandards/Get-RulesFromCSV.ps1 b/scripts/PSCodingStandards/Get-RulesFromCSV.ps1 new file mode 100644 index 0000000000..54c29668e4 --- /dev/null +++ b/scripts/PSCodingStandards/Get-RulesFromCSV.ps1 @@ -0,0 +1,29 @@ +function Get-RulesFromCSV { + param( + [ValidateSet('c', 'cpp', 'all')] + [string] + $Language = 'all') + + + $csvFile = (Join-Path (Get-RepositoryRoot) "rules.csv") + + Write-Host "Loading rules for language=$Language from file $csvFile..." + + $csv = Import-Csv $csvFile + $filteredCSV = @() + # don't filter if not neeeded + if ($Language -eq 'all'){ + $filteredCSV = $csv + }else{ + foreach($rule in $csv){ + if($rule.Language -eq $Language){ + $filteredCSV += $rule + } + } + } + + Write-Host "Loaded $($filteredCSV.Length) rules." + + return $csv + +} \ No newline at end of file diff --git a/scripts/util/Get-DuplicateRules.ps1 b/scripts/util/Get-DuplicateRules.ps1 new file mode 100644 index 0000000000..d90f6fc716 --- /dev/null +++ b/scripts/util/Get-DuplicateRules.ps1 @@ -0,0 +1,47 @@ +#!/usr/bin/env pwsh +param( + [ValidateSet('c', 'cpp', 'all')] + [string] + $Language = 'all', + [switch] + $CIMode + +) + +Import-Module -Name "$PSScriptRoot/../PSCodingStandards/CodingStandards" + +# load the rules. +$rules = Get-RulesFromCSV -Language $Language + +# find out duplicates +$counter = @{} + +foreach($rule in $rules){ + $key = "$($rule.Language):$($rule.Standard):$($rule.ID)" + if($counter.Contains($key)){ + $counter[$key] += $rule + }else{ + $counter[$key] = @() + $counter[$key] += $rule + } +} + +$duplicates = @() +$numDuplicates = 0 + +foreach($k in $counter.Keys){ + if($counter[$k].Count -gt 1){ + $numDuplicates = $numDuplicates + 1 + foreach($v in $counter[$k]){ + $duplicates += $v + } + } +} + +$duplicates | Format-Table + +if(($CIMode) -and ($numDuplicates -gt 0)){ + throw "Found $numDuplicates duplicate Rule IDs" +}else{ + Write-Host "Found $numDuplicates duplicate Rule IDs" +} \ No newline at end of file