Skip to content

Commit ee795e6

Browse files
authored
Merge pull request #124 from jsinglet/jsinglet/rule-fix-dir-1-2
Fix Duplicate and Incorrect Rule IDs
2 parents 00a369f + 78b5f37 commit ee795e6

32 files changed

+147
-34
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: ⚙️ Validate Rules CSV
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- "rc/**"
8+
- next
9+
pull_request:
10+
branches:
11+
- main
12+
- "rc/**"
13+
- next
14+
15+
16+
jobs:
17+
validate-rules-csv:
18+
name: Validate Rules CSV
19+
runs-on: ubuntu-22.04
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v2
23+
24+
- name: Check Rules
25+
shell: pwsh
26+
run: scripts/util/Get-DuplicateRules.ps1 -Language 'all' -CIMode
27+
28+

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@
185185
"type": "pickString",
186186
"options": [
187187
"Allocations",
188+
"Banned",
188189
"BannedFunctions",
189190
"BannedLibraries",
190191
"BannedSyntax",

c/misra/src/rules/RULE-4-10/PrecautionIncludeGuardsNotProvided.ql renamed to c/misra/src/rules/DIR-4-10/PrecautionIncludeGuardsNotProvided.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/**
22
* @id c/misra/precaution-include-guards-not-provided
3-
* @name RULE-4-10: Precautions shall be taken in order to prevent the contents of a header file being included more than once
3+
* @name DIR-4-10: Precautions shall be taken in order to prevent the contents of a header file being included more than once
44
* @description Using anything other than a standard include guard form can make code confusing and
55
* can lead to multiple or conflicting definitions.
66
* @kind problem
77
* @precision very-high
88
* @problem.severity warning
9-
* @tags external/misra/id/rule-4-10
9+
* @tags external/misra/id/dir-4-10
1010
* correctness
1111
* maintainability
1212
* readability

c/misra/src/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.ql renamed to c/misra/src/rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* @id c/misra/std-lib-dynamic-memory-allocation-used
3-
* @name RULE-4-12: Dynamic memory allocation shall not be used
3+
* @name DIR-4-12: Dynamic memory allocation shall not be used
44
* @description Using dynamic memory allocation and deallocation can result to undefined behavior.
55
* This query is for the Standard Library Implementation. Any implementation outside it
66
* will require a separate query under the same directive.
77
* @kind problem
88
* @precision very-high
99
* @problem.severity error
10-
* @tags external/misra/id/rule-4-12
10+
* @tags external/misra/id/dir-4-12
1111
* security
1212
* correctness
1313
* maintainability

c/misra/src/rules/RULE-4-4/SectionsOfCodeShallNotBeCommentedOut.ql renamed to c/misra/src/rules/DIR-4-4/SectionsOfCodeShallNotBeCommentedOut.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* @id c/misra/sections-of-code-shall-not-be-commented-out
3-
* @name RULE-4-4: Sections of code should not be commented out
3+
* @name DIR-4-4: Sections of code should not be commented out
44
* @description Commented out code may become out of date leading to developer confusion.
55
* @kind problem
66
* @precision high
77
* @problem.severity warning
8-
* @tags external/misra/id/rule-4-4
8+
* @tags external/misra/id/dir-4-4
99
* maintainability
1010
* readability
1111
* correctness

c/misra/src/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql renamed to c/misra/src/rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* @id c/misra/object-with-no-pointer-dereference-should-be-opaque
3-
* @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
3+
* @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
44
* @description If a pointer to a structure or union is never dereferenced within a translation
55
* unit, then the implementation of the object should be hidden to prevent
66
* unintentional changes.
77
* @kind problem
88
* @precision very-high
99
* @problem.severity error
10-
* @tags external/misra/id/rule-4-8
10+
* @tags external/misra/id/dir-4-8
1111
* readability
1212
* maintainability
1313
* external/misra/obligation/advisory
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/DIR-4-12/StdLibDynamicMemoryAllocationUsed.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/DIR-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.ql

c/misra/test/rules/RULE-4-10/NonUniqueIncludeGuards.testref

Lines changed: 0 additions & 1 deletion
This file was deleted.

c/misra/test/rules/RULE-4-12/StdLibDynamicMemoryAllocationUsed.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.

c/misra/test/rules/RULE-4-8/ObjectWithNoPointerDereferenceShouldBeOpaque.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- The following rules have been renamed:
2+
- RULE-4-4 has been renamed to DIR-4-4 to reflect correct naming as per
3+
MISRA C:2012 standard.
4+
- RULE-4-8 has been renamed to DIR-4-8 to reflect correct naming as per
5+
MISRA C:2012 standard.
6+
- RULE-4-10 has been renamed to DIR-4-10 to reflect correct naming as per
7+
MISRA C:2012 standard.
8+
- RULE-4-12 has been renamed to DIR-4-12 to reflect correct naming as per
9+
MISRA C:2012 standard.

cpp/common/src/codingstandards/cpp/exclusions/c/Banned.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ predicate isBannedQueryMetadata(Query query, string queryId, string ruleId) {
159159
queryId =
160160
// `@id` for the `stdLibDynamicMemoryAllocationUsed` query
161161
"c/misra/std-lib-dynamic-memory-allocation-used" and
162-
ruleId = "RULE-4-12"
162+
ruleId = "DIR-4-12"
163163
or
164164
query =
165165
// `Query` instance for the `octalConstantsUsed` query

cpp/common/src/codingstandards/cpp/exclusions/c/Pointers1.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ predicate isPointers1QueryMetadata(Query query, string queryId, string ruleId) {
149149
queryId =
150150
// `@id` for the `objectWithNoPointerDereferenceShouldBeOpaque` query
151151
"c/misra/object-with-no-pointer-dereference-should-be-opaque" and
152-
ruleId = "RULE-4-8"
152+
ruleId = "DIR-4-8"
153153
or
154154
query =
155155
// `Query` instance for the `pointerShouldPointToConstTypeWhenPossible` query

cpp/common/src/codingstandards/cpp/exclusions/c/Preprocessor2.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ predicate isPreprocessor2QueryMetadata(Query query, string queryId, string ruleI
4040
queryId =
4141
// `@id` for the `precautionIncludeGuardsNotProvided` query
4242
"c/misra/precaution-include-guards-not-provided" and
43-
ruleId = "RULE-4-10"
43+
ruleId = "DIR-4-10"
4444
}
4545

4646
module Preprocessor2Package {

cpp/common/src/codingstandards/cpp/exclusions/c/Syntax.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ predicate isSyntaxQueryMetadata(Query query, string queryId, string ruleId) {
4343
queryId =
4444
// `@id` for the `sectionsOfCodeShallNotBeCommentedOut` query
4545
"c/misra/sections-of-code-shall-not-be-commented-out" and
46-
ruleId = "RULE-4-4"
46+
ruleId = "DIR-4-4"
4747
or
4848
query =
4949
// `Query` instance for the `identifiersInTheSameNameSpaceUnambiguous` query

rule_packages/c/Banned.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@
304304
],
305305
"title": "The Standard Library functions 'bsearch' and 'qsort' of 'stdlib.h' shall not be used"
306306
},
307-
"RULE-4-12": {
307+
"DIR-4-12": {
308308
"properties": {
309309
"obligation": "required"
310310
},

rule_packages/c/Pointers1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
],
295295
"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"
296296
},
297-
"RULE-4-8": {
297+
"DIR-4-8": {
298298
"properties": {
299299
"obligation": "advisory"
300300
},

rule_packages/c/Preprocessor2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
],
6464
"title": "#undef should not be used"
6565
},
66-
"RULE-4-10": {
66+
"DIR-4-10": {
6767
"properties": {
6868
"obligation": "required"
6969
},

rule_packages/c/Syntax.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
],
6363
"title": "Octal and hexadecimal escape sequences shall be terminated"
6464
},
65-
"RULE-4-4": {
65+
"DIR-4-4": {
6666
"properties": {
6767
"obligation": "advisory"
6868
},

rules.csv

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -599,23 +599,23 @@ c,CERT-C,STR34-C,Yes,Rule,,,Cast characters to unsigned char before converting t
599599
c,CERT-C,STR37-C,Yes,Rule,,,Arguments to character-handling functions must be representable as an unsigned char,,Strings2,Medium,
600600
c,CERT-C,STR38-C,Yes,Rule,,,Do not confuse narrow and wide character strings and functions,,Strings3,Medium,
601601
c,CERT-C,WIN30-C,OutOfScope,Rule,,,Properly pair allocation and deallocation functions,DCL54-CPP,,Easy,
602-
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,,,,
603-
c,MISRA-C-2012,RULE-2-1,Yes,Required,,,All source files shall compile without any compilation errors,A1-4-3,Language,Medium,
604-
c,MISRA-C-2012,RULE-3-1,No,Required,,,All code shall be traceable to documented requirements,,,,
605-
c,MISRA-C-2012,RULE-4-1,No,Required,,,Run-time failures shall be minimized,,,,
606-
c,MISRA-C-2012,RULE-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language,Import,
602+
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,,,,
603+
c,MISRA-C-2012,DIR-2-1,Yes,Required,,,All source files shall compile without any compilation errors,A1-4-3,Language,Medium,
604+
c,MISRA-C-2012,DIR-3-1,No,Required,,,All code shall be traceable to documented requirements,,,,
605+
c,MISRA-C-2012,DIR-4-1,No,Required,,,Run-time failures shall be minimized,,,,
606+
c,MISRA-C-2012,DIR-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language,Import,
607607
c,MISRA-C-2012,DIR-4-3,Yes,Required,,,Assembly language shall be encapsulated and isolated,,Language1,Medium,
608-
c,MISRA-C-2012,RULE-4-4,Yes,Advisory,,,Sections of code should not be commented out,A2-7-2,Syntax,Import,
608+
c,MISRA-C-2012,DIR-4-4,Yes,Advisory,,,Sections of code should not be commented out,A2-7-2,Syntax,Import,
609609
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,
610-
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,
611-
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,
612-
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,
613-
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,
614-
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,
615-
c,MISRA-C-2012,RULE-4-11,Yes,Required,,,The validity of values passed to library functions shall be checked,,Contracts,Hard,
616-
c,MISRA-C-2012,RULE-4-12,Yes,Required,,,Dynamic memory allocation shall not be used,,Banned,Medium,
617-
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,
618-
c,MISRA-C-2012,RULE-4-14,Yes,Required,,,The validity of values received from external sources shall be checked,,Contracts,Hard,
610+
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,
611+
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,
612+
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,
613+
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,
614+
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,
615+
c,MISRA-C-2012,DIR-4-11,Yes,Required,,,The validity of values passed to library functions shall be checked,,Contracts,Hard,
616+
c,MISRA-C-2012,DIR-4-12,Yes,Required,,,Dynamic memory allocation shall not be used,,Banned,Medium,
617+
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,
618+
c,MISRA-C-2012,DIR-4-14,Yes,Required,,,The validity of values received from external sources shall be checked,,Contracts,Hard,
619619
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,
620620
c,MISRA-C-2012,RULE-1-2,Yes,Advisory,,,Language extensions should not be used,,Language,Easy,
621621
c,MISRA-C-2012,RULE-1-3,Yes,Required,,,There shall be no occurrence of undefined or critical unspecified behaviour,,Language,Hard,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function Get-RulesFromCSV {
2+
param(
3+
[ValidateSet('c', 'cpp', 'all')]
4+
[string]
5+
$Language = 'all')
6+
7+
8+
$csvFile = (Join-Path (Get-RepositoryRoot) "rules.csv")
9+
10+
Write-Host "Loading rules for language=$Language from file $csvFile..."
11+
12+
$csv = Import-Csv $csvFile
13+
$filteredCSV = @()
14+
# don't filter if not neeeded
15+
if ($Language -eq 'all'){
16+
$filteredCSV = $csv
17+
}else{
18+
foreach($rule in $csv){
19+
if($rule.Language -eq $Language){
20+
$filteredCSV += $rule
21+
}
22+
}
23+
}
24+
25+
Write-Host "Loaded $($filteredCSV.Length) rules."
26+
27+
return $csv
28+
29+
}

scripts/util/Get-DuplicateRules.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env pwsh
2+
param(
3+
[ValidateSet('c', 'cpp', 'all')]
4+
[string]
5+
$Language = 'all',
6+
[switch]
7+
$CIMode
8+
9+
)
10+
11+
Import-Module -Name "$PSScriptRoot/../PSCodingStandards/CodingStandards"
12+
13+
# load the rules.
14+
$rules = Get-RulesFromCSV -Language $Language
15+
16+
# find out duplicates
17+
$counter = @{}
18+
19+
foreach($rule in $rules){
20+
$key = "$($rule.Language):$($rule.Standard):$($rule.ID)"
21+
if($counter.Contains($key)){
22+
$counter[$key] += $rule
23+
}else{
24+
$counter[$key] = @()
25+
$counter[$key] += $rule
26+
}
27+
}
28+
29+
$duplicates = @()
30+
$numDuplicates = 0
31+
32+
foreach($k in $counter.Keys){
33+
if($counter[$k].Count -gt 1){
34+
$numDuplicates = $numDuplicates + 1
35+
foreach($v in $counter[$k]){
36+
$duplicates += $v
37+
}
38+
}
39+
}
40+
41+
$duplicates | Format-Table
42+
43+
if(($CIMode) -and ($numDuplicates -gt 0)){
44+
throw "Found $numDuplicates duplicate Rule IDs"
45+
}else{
46+
Write-Host "Found $numDuplicates duplicate Rule IDs"
47+
}

0 commit comments

Comments
 (0)