Skip to content

Commit 834c46f

Browse files
committed
Prepare MISRA C:2025 development and rule examples
1 parent bc8a0eb commit 834c46f

88 files changed

Lines changed: 949 additions & 196 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- description: MISRA C 2023 - Third Edition, Second Revision
2+
- qlpack: codeql/misra-c-coding-standards
3+
- include:
4+
kind:
5+
- problem
6+
- path-problem
7+
tags contain:
8+
- external/misra/c/2012/third-edition-first-revision
9+
- external/misra/c/2012/amendment2
10+
- external/misra/c/2012/amendment3
11+
- external/misra/c/2012/amendment4
12+
- exclude:
13+
tags contain:
14+
- external/misra/audit
15+
- external/misra/strict
16+
- external/misra/default-disabled

‎c/misra/src/codeql-suites/misra-c-advisory.qls‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- description: MISRA C 2023 (Advisory)
1+
- description: MISRA C 2025 (Advisory)
22
- qlpack: codeql/misra-c-coding-standards
33
- include:
44
kind:
@@ -10,3 +10,4 @@
1010
tags contain:
1111
- external/misra/audit
1212
- external/misra/default-disabled
13+
- external/misra/legacy

‎c/misra/src/codeql-suites/misra-c-audit.qls‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- description: MISRA C 2023 (Audit)
1+
- description: MISRA C 2025 (Audit)
22
- qlpack: codeql/misra-c-coding-standards
33
- include:
44
kind:

‎c/misra/src/codeql-suites/misra-c-default.qls‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- description: MISRA C 2023 (Default)
1+
- description: MISRA C 2025 (Default)
22
- qlpack: codeql/misra-c-coding-standards
33
- include:
44
kind:
@@ -9,3 +9,4 @@
99
- external/misra/audit
1010
- external/misra/strict
1111
- external/misra/default-disabled
12+
- external/misra/legacy

‎c/misra/src/codeql-suites/misra-c-mandatory.qls‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- description: MISRA C 2023 (Advisory)
1+
- description: MISRA C 2025 (Mandatory)
22
- qlpack: codeql/misra-c-coding-standards
33
- include:
44
kind:
@@ -10,3 +10,4 @@
1010
tags contain:
1111
- external/misra/audit
1212
- external/misra/default-disabled
13+
- external/misra/legacy

‎c/misra/src/codeql-suites/misra-c-required.qls‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- description: MISRA C 2023 (Required)
1+
- description: MISRA C 2025 (Required)
22
- qlpack: codeql/misra-c-coding-standards
33
- include:
44
kind:
@@ -10,3 +10,4 @@
1010
tags contain:
1111
- external/misra/audit
1212
- external/misra/default-disabled
13+
- external/misra/legacy

‎c/misra/src/codeql-suites/misra-c-strict.qls‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- description: MISRA C 2023 (Strict)
1+
- description: MISRA C 2025 (Strict)
22
- qlpack: codeql/misra-c-coding-standards
33
- include:
44
kind:
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/** Provides the numeric-type detector shared by legacy and current directive wrappers. */
2+
3+
import cpp
4+
import codingstandards.cpp.BuiltInNumericTypes
5+
6+
/** A numeric type covered by the legacy directive. */
7+
private class NumericType extends BuiltInType {
8+
NumericType() {
9+
this.(CharType).isExplicitlySigned()
10+
or
11+
this.(CharType).isExplicitlyUnsigned()
12+
or
13+
this instanceof BuiltInIntegerType
14+
or
15+
this instanceof FloatType
16+
or
17+
this instanceof DoubleType
18+
or
19+
this instanceof LongDoubleType
20+
or
21+
this instanceof ComplexNumberType
22+
}
23+
}
24+
25+
/** A typedef name that encodes numeric width and signedness. */
26+
private class SizedTypeName extends string {
27+
string pattern;
28+
int width;
29+
30+
bindingset[this]
31+
pragma[inline]
32+
SizedTypeName() {
33+
pattern = "(u?int|c?float)(4|8|16|32|64|128)_t" and
34+
this.regexpMatch(pattern) and
35+
width = this.regexpCapture(pattern, 2).toInt()
36+
}
37+
38+
/** Gets the width encoded by this name. */
39+
bindingset[this]
40+
pragma[inline]
41+
int getWidth() { result = width }
42+
43+
/** Holds if this name denotes a complex type. */
44+
bindingset[this]
45+
pragma[inline]
46+
predicate isComplex() { this.charAt(0) = "c" }
47+
}
48+
49+
/** Holds if `type` has an invalid complex name, with `message` explaining why. */
50+
private predicate forbiddenComplexType(CTypedefType type, string message) {
51+
type.getName().(SizedTypeName).isComplex() and
52+
(
53+
if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType
54+
then
55+
type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() * 2 and
56+
message = "The typedef type " + type.getName() + " does not have its indicated real size."
57+
else message = "The typedef type " + type.getName() + " is not a complex type."
58+
)
59+
}
60+
61+
/** Holds if `type` has an invalid real name, with `message` explaining why. */
62+
private predicate forbiddenRealType(CTypedefType type, string message) {
63+
not type.getName().(SizedTypeName).isComplex() and
64+
(
65+
if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType
66+
then message = "The typedef name " + type.getName() + " does not indicate a complex type."
67+
else (
68+
type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() and
69+
message = "The typedef type " + type.getName() + " does not have its indicated size."
70+
)
71+
)
72+
}
73+
74+
/** Holds if `type` is a forbidden alias, with `message` explaining why. */
75+
private predicate forbiddenAlias(CTypedefType type, string message) {
76+
if type.getName() instanceof SizedTypeName
77+
then forbiddenRealType(type, message) or forbiddenComplexType(type, message)
78+
else (
79+
(
80+
type.getBaseType() instanceof NumericType
81+
or
82+
forbiddenAlias(type.getBaseType(), _)
83+
) and
84+
message = "The type " + type.getName() + " is not an alias to a fixed-width numeric type."
85+
)
86+
}
87+
88+
/** Holds if `element` violates the legacy directive, with `message` explaining why. */
89+
predicate problems(Element element, string message) {
90+
exists(Variable variable | variable = element |
91+
variable.getType() instanceof NumericType and
92+
not variable instanceof ExcludedVariable and
93+
message = "The type " + variable.getType() + " is not a fixed-width numeric type."
94+
)
95+
or
96+
forbiddenAlias(element, message)
97+
}
98+
99+
/** Holds if `element` declares or aliases a real or complex floating-point type. */
100+
predicate hasFloatingType(Element element) {
101+
exists(Type type |
102+
type = element.(Variable).getType() or type = element.(CTypedefType).getBaseType()
103+
|
104+
type.stripType() instanceof FloatingPointType
105+
or
106+
type.stripType() instanceof ComplexNumberType
107+
)
108+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/** Provides the reserved-macro detector shared by MISRA C guideline identities. */
2+
3+
import cpp
4+
import codingstandards.cpp.Naming
5+
6+
/** Gets a reserved name defined or undefined by `directive`. */
7+
string getReservedMacroName(PreprocessorDirective directive) {
8+
(
9+
directive.(Macro).hasName(result)
10+
or
11+
directive.(PreprocessorUndef).getName() = result
12+
) and
13+
(
14+
Naming::Cpp14::hasStandardLibraryMacroName(result)
15+
or
16+
Naming::Cpp14::hasStandardLibraryObjectName(result)
17+
or
18+
result.regexpMatch("_.*")
19+
or
20+
result = "defined"
21+
)
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/** Provides the exit detector shared by Advisory and Disapplied query wrappers. */
2+
3+
import cpp
4+
5+
/** A return statement explicitly written in source code. */
6+
private class UserReturn extends ReturnStmt {
7+
UserReturn() { not this.isCompilerGenerated() }
8+
}
9+
10+
/** Holds if `function` has an offending `statement`, with `message` explaining why. */
11+
predicate problems(Function function, string message, ReturnStmt statement) {
12+
function.hasDefinition() and
13+
count(function.getBlock()) = 1 and
14+
not function.isCompilerGenerated() and
15+
statement instanceof UserReturn and
16+
statement.getEnclosingFunction() = function and
17+
(
18+
count(UserReturn other | other.getEnclosingFunction() = function) > 1 and
19+
message = "Function has more than one $@."
20+
or
21+
count(UserReturn other | other.getEnclosingFunction() = function) = 1 and
22+
not function.getBlock().getLastStmt() instanceof UserReturn and
23+
message = "The $@ is not the last statement of the function."
24+
)
25+
}

0 commit comments

Comments
 (0)