Skip to content

Commit 63e5622

Browse files
committed
Declarations7: add RULE-8-12
1 parent 48f7ef5 commit 63e5622

File tree

9 files changed

+99
-1
lines changed

9 files changed

+99
-1
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@
208208
"Declarations2",
209209
"Declarations3",
210210
"Declarations4",
211+
"Declarations7",
211212
"Exceptions1",
212213
"Exceptions2",
213214
"Expressions",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @id c/misra/value-implicit-enumeration-constant-not-unique
3+
* @name RULE-8-12: Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique
4+
* @description Using an implicitly specified enumeration constant that is not unique (with respect
5+
* to an explicitly specified constant) can lead to unexpected program behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-12
10+
* correctness
11+
* readability
12+
* external/misra/obligation/required
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
18+
/**
19+
* An `EnumConstant` that has an implicitly specified value:
20+
* `enum e { explicit = 1, implicit }`
21+
*/
22+
class ImplicitlySpecifiedEnumConstant extends EnumConstant {
23+
ImplicitlySpecifiedEnumConstant() {
24+
//implicitly specified have an initializer with ___location: `file://:0:0:0:0`
25+
not this.getInitializer().getLocation().getFile() = this.getFile()
26+
}
27+
}
28+
29+
from EnumConstant exp, ImplicitlySpecifiedEnumConstant imp
30+
where
31+
not isExcluded(exp, Declarations7Package::valueImplicitEnumerationConstantNotUniqueQuery()) and
32+
not isExcluded(imp, Declarations7Package::valueImplicitEnumerationConstantNotUniqueQuery()) and
33+
not exp = imp and
34+
imp.getValue() = exp.getValue() and
35+
imp.getDeclaringEnum() = exp.getDeclaringEnum() and
36+
//can technically be the same declared enum across multiple headers but those are not relevant to this rule
37+
imp.getFile() = exp.getFile()
38+
select imp, "Nonunique value of enum constant compared to $@", exp, exp.getName()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.c:3:18:3:19 | c4 | Nonunique value of enum constant compared to $@ | test.c:3:22:3:23 | c5 | c5 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-8-12/ValueImplicitEnumerationConstantNotUnique.ql

c/misra/test/rules/RULE-8-12/test.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
enum e {c = 3}; // COMPLIANT
2+
enum e1 {c1 = 3, c2}; // COMPLIANT
3+
enum e3 {c3 = 3, c4, c5 = 4}; // NON_COMPLIANT
4+
enum e4 {c6 = 3, c7, c8, c9 = 6}; // COMPLIANT
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Declarations7Query = TValueImplicitEnumerationConstantNotUniqueQuery()
7+
8+
predicate isDeclarations7QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `valueImplicitEnumerationConstantNotUnique` query
11+
Declarations7Package::valueImplicitEnumerationConstantNotUniqueQuery() and
12+
queryId =
13+
// `@id` for the `valueImplicitEnumerationConstantNotUnique` query
14+
"c/misra/value-implicit-enumeration-constant-not-unique" and
15+
ruleId = "RULE-8-12" and
16+
category = "required"
17+
}
18+
19+
module Declarations7Package {
20+
Query valueImplicitEnumerationConstantNotUniqueQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `valueImplicitEnumerationConstantNotUnique` query
24+
TQueryC(TDeclarations7PackageQuery(TValueImplicitEnumerationConstantNotUniqueQuery()))
25+
}
26+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Declarations1
1717
import Declarations2
1818
import Declarations3
1919
import Declarations4
20+
import Declarations7
2021
import Expressions
2122
import IO1
2223
import IO2
@@ -57,6 +58,7 @@ newtype TCQuery =
5758
TDeclarations2PackageQuery(Declarations2Query q) or
5859
TDeclarations3PackageQuery(Declarations3Query q) or
5960
TDeclarations4PackageQuery(Declarations4Query q) or
61+
TDeclarations7PackageQuery(Declarations7Query q) or
6062
TExpressionsPackageQuery(ExpressionsQuery q) or
6163
TIO1PackageQuery(IO1Query q) or
6264
TIO2PackageQuery(IO2Query q) or
@@ -97,6 +99,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
9799
isDeclarations2QueryMetadata(query, queryId, ruleId, category) or
98100
isDeclarations3QueryMetadata(query, queryId, ruleId, category) or
99101
isDeclarations4QueryMetadata(query, queryId, ruleId, category) or
102+
isDeclarations7QueryMetadata(query, queryId, ruleId, category) or
100103
isExpressionsQueryMetadata(query, queryId, ruleId, category) or
101104
isIO1QueryMetadata(query, queryId, ruleId, category) or
102105
isIO2QueryMetadata(query, queryId, ruleId, category) or

rule_packages/c/Declarations7.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-8-12": {
4+
"properties": {
5+
"obligation": "required"
6+
},
7+
"queries": [
8+
{
9+
"description": "Using an implicitly specified enumeration constant that is not unique (with respect to an explicitly specified constant) can lead to unexpected program behaviour.",
10+
"kind": "problem",
11+
"name": "Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",
12+
"precision": "very-high",
13+
"severity": "error",
14+
"short_name": "ValueImplicitEnumerationConstantNotUnique",
15+
"tags": [
16+
"correctness",
17+
"readability"
18+
]
19+
}
20+
],
21+
"title": "Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique"
22+
}
23+
}
24+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ c,MISRA-C-2012,RULE-8-8,Yes,Required,,,The static storage class specifier shall
657657
c,MISRA-C-2012,RULE-8-9,Yes,Advisory,,,An object should be defined at block scope if its identifier only appears in a single function,M3-4-1,Declarations,Medium,
658658
c,MISRA-C-2012,RULE-8-10,Yes,Required,,,An inline function shall be declared with the static storage class,,Declarations,Medium,
659659
c,MISRA-C-2012,RULE-8-11,Yes,Advisory,,,"When an array with external linkage is declared, its size should be explicitly specified",,Declarations,Medium,
660-
c,MISRA-C-2012,RULE-8-12,Yes,Required,,,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",,Declarations,Medium,
660+
c,MISRA-C-2012,RULE-8-12,Yes,Required,,,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",,Declarations7,Medium,
661661
c,MISRA-C-2012,RULE-8-13,Yes,Advisory,,,A pointer should point to a const-qualified type whenever possible,,Pointers1,Medium,
662662
c,MISRA-C-2012,RULE-8-14,Yes,Required,,,The restrict type qualifier shall not be used,,Banned,Easy,
663663
c,MISRA-C-2012,RULE-9-1,Yes,Mandatory,,,The value of an object with automatic storage duration shall not be read before it has been set,,InvalidMemory,Medium,

0 commit comments

Comments
 (0)