Skip to content

Commit fea9922

Browse files
authored
Merge branch 'main' into rdmarsh2/allocation-expr-instanceof
2 parents 798a350 + 19c5636 commit fea9922

20 files changed

+275
-14
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
"Iterators",
223223
"Lambdas",
224224
"Language1",
225+
"Language2",
225226
"Literals",
226227
"Loops",
227228
"Macros",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/usage-of-assembly-language-should-be-documented
3+
* @name DIR-4-2: All usage of assembly language should be documented
4+
* @description Assembly language is not portable and should be documented.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/dir-4-2
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented
17+
18+
class UsageOfAssemblyLanguageShouldBeDocumentedQuery extends UsageOfAssemblerNotDocumentedSharedQuery {
19+
UsageOfAssemblyLanguageShouldBeDocumentedQuery() {
20+
this = Language2Package::usageOfAssemblyLanguageShouldBeDocumentedQuery()
21+
}
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/emergent-language-features-used
3+
* @name RULE-1-4: Emergent language features shall not be used
4+
* @description Emergent language features may have unpredictable behavior and should not be used.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-1-4
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Emergent
17+
18+
from C11::EmergentLanguageFeature ef
19+
where not isExcluded(ef, Language2Package::emergentLanguageFeaturesUsedQuery())
20+
select ef, "Usage of emergent language feature."
21+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/usageofassemblernotdocumented/UsageOfAssemblerNotDocumented.ql
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
| test.c:1:1:1:21 | #include <stdalign.h> | Usage of emergent language feature. |
2+
| test.c:2:1:2:22 | #include <stdatomic.h> | Usage of emergent language feature. |
3+
| test.c:3:1:3:24 | #include <stdnoreturn.h> | Usage of emergent language feature. |
4+
| test.c:4:1:4:20 | #include <threads.h> | Usage of emergent language feature. |
5+
| test.c:6:1:6:49 | #define MACRO(x) _Generic((x), int : 0, long : 1) | Usage of emergent language feature. |
6+
| test.c:7:1:7:32 | #define __STDC_WANT_LIB_EXT1__ 1 | Usage of emergent language feature. |
7+
| test.c:9:16:9:17 | f0 | Usage of emergent language feature. |
8+
| test.c:12:26:12:40 | atomic_new_type | Usage of emergent language feature. |
9+
| test.c:17:15:17:15 | i | Usage of emergent language feature. |
10+
| test.c:19:3:19:10 | alignas(...) | Usage of emergent language feature. |
11+
| test.c:20:3:20:9 | alignas(...) | Usage of emergent language feature. |
12+
| test.c:21:11:21:23 | alignof(int) | Usage of emergent language feature. |
13+
| test.c:22:12:22:23 | alignof(int) | Usage of emergent language feature. |
14+
| test.c:24:27:24:28 | i3 | Usage of emergent language feature. |
15+
| test.c:25:28:25:29 | i4 | Usage of emergent language feature. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-1-4/EmergentLanguageFeaturesUsed.ql

c/misra/test/rules/RULE-1-4/test.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdalign.h> //NON_COMPLIANT
2+
#include <stdatomic.h> //NON_COMPLIANT
3+
#include <stdnoreturn.h> //NON_COMPLIANT
4+
#include <threads.h> //NON_COMPLIANT
5+
6+
#define MACRO(x) _Generic((x), int : 0, long : 1) // NON_COMPLIANT
7+
#define __STDC_WANT_LIB_EXT1__ 1 // NON_COMPLIANT
8+
9+
_Noreturn void f0(); // NON_COMPLIANT
10+
11+
typedef int new_type; // COMPLIANT
12+
typedef _Atomic new_type atomic_new_type; // NON_COMPLIANT
13+
14+
void f(int p) {
15+
int i0 = _Generic(p, int : 0, long : 1); // NON_COMPLIANT[FALSE_NEGATIVE]
16+
17+
_Atomic int i; // NON_COMPLIANT
18+
19+
_Alignas(4) int i1; // NON_COMPLIANT
20+
alignas(4) int i2; // NON_COMPLIANT
21+
int a = _Alignof(int); // NON_COMPLIANT
22+
int a1 = alignof(int); // NON_COMPLIANT
23+
24+
static thread_local int i3; // NON_COMPLIANT
25+
static _Thread_local int i4; // NON_COMPLIANT
26+
}

cpp/autosar/src/rules/M7-4-1/UsageOfAssemblerNotDocumented.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented
2021

21-
from AsmStmt a
22-
where
23-
not isExcluded(a, BannedLibrariesPackage::usageOfAssemblerNotDocumentedQuery()) and
24-
not exists(Comment c | c.getCommentedElement() = a) and
25-
not a.isAffectedByMacro()
26-
select a, "Use of assembler is not documented."
22+
class UsageOfAssemblerNotDocumentedQuery extends UsageOfAssemblerNotDocumentedSharedQuery {
23+
UsageOfAssemblerNotDocumentedQuery() {
24+
this = BannedLibrariesPackage::usageOfAssemblerNotDocumentedQuery()
25+
}
26+
}

cpp/autosar/test/rules/M7-4-1/UsageOfAssemblerNotDocumented.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/usageofassemblernotdocumented/UsageOfAssemblerNotDocumented.ql
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import cpp
2+
3+
/**
4+
* Namespace for containing emergent language features in C11.
5+
*/
6+
module C11 {
7+
abstract class EmergentLanguageFeature extends Element { }
8+
9+
class AlignAsAttribute extends EmergentLanguageFeature, Attribute {
10+
AlignAsAttribute() { getName() = "_Alignas" }
11+
}
12+
13+
class AtomicVariableSpecifier extends EmergentLanguageFeature, Variable {
14+
AtomicVariableSpecifier() {
15+
getType().(DerivedType).getBaseType*().getASpecifier().getName() = "atomic"
16+
}
17+
}
18+
19+
class AtomicDeclaration extends EmergentLanguageFeature, Declaration {
20+
AtomicDeclaration() { getASpecifier().getName() = "atomic" }
21+
}
22+
23+
class ThreadLocalDeclaration extends EmergentLanguageFeature, Declaration {
24+
ThreadLocalDeclaration() { getASpecifier().getName() = "is_thread_local" }
25+
}
26+
27+
class EmergentHeader extends EmergentLanguageFeature, Include {
28+
EmergentHeader() {
29+
getIncludedFile().getBaseName() = ["stdalign.h", "stdatomic.h", "stdnoreturn.h", "threads.h"]
30+
}
31+
}
32+
33+
class LibExt1Macro extends EmergentLanguageFeature, Macro {
34+
LibExt1Macro() {
35+
getName() = "__STDC_WANT_LIB_EXT1__" and
36+
getBody() = "1"
37+
}
38+
}
39+
40+
class GenericMacro extends EmergentLanguageFeature, Macro {
41+
GenericMacro() { getBody().indexOf("_Generic") = 0 }
42+
}
43+
44+
class NoReturnSpecificer extends EmergentLanguageFeature, Function {
45+
NoReturnSpecificer() { getASpecifier().getName() = "noreturn" }
46+
}
47+
48+
class AlignOf extends EmergentLanguageFeature, AlignofTypeOperator { }
49+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Language2Query =
7+
TUsageOfAssemblyLanguageShouldBeDocumentedQuery() or
8+
TEmergentLanguageFeaturesUsedQuery()
9+
10+
predicate isLanguage2QueryMetadata(Query query, string queryId, string ruleId) {
11+
query =
12+
// `Query` instance for the `usageOfAssemblyLanguageShouldBeDocumented` query
13+
Language2Package::usageOfAssemblyLanguageShouldBeDocumentedQuery() and
14+
queryId =
15+
// `@id` for the `usageOfAssemblyLanguageShouldBeDocumented` query
16+
"c/misra/usage-of-assembly-language-should-be-documented" and
17+
ruleId = "DIR-4-2"
18+
or
19+
query =
20+
// `Query` instance for the `emergentLanguageFeaturesUsed` query
21+
Language2Package::emergentLanguageFeaturesUsedQuery() and
22+
queryId =
23+
// `@id` for the `emergentLanguageFeaturesUsed` query
24+
"c/misra/emergent-language-features-used" and
25+
ruleId = "RULE-1-4"
26+
}
27+
28+
module Language2Package {
29+
Query usageOfAssemblyLanguageShouldBeDocumentedQuery() {
30+
//autogenerate `Query` type
31+
result =
32+
// `Query` type for `usageOfAssemblyLanguageShouldBeDocumented` query
33+
TQueryC(TLanguage2PackageQuery(TUsageOfAssemblyLanguageShouldBeDocumentedQuery()))
34+
}
35+
36+
Query emergentLanguageFeaturesUsedQuery() {
37+
//autogenerate `Query` type
38+
result =
39+
// `Query` type for `emergentLanguageFeaturesUsed` query
40+
TQueryC(TLanguage2PackageQuery(TEmergentLanguageFeaturesUsedQuery()))
41+
}
42+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import IO2
2121
import IO3
2222
import IO4
2323
import Language1
24+
import Language2
2425
import Misc
2526
import Pointers1
2627
import Pointers2
@@ -58,6 +59,7 @@ newtype TCQuery =
5859
TIO3PackageQuery(IO3Query q) or
5960
TIO4PackageQuery(IO4Query q) or
6061
TLanguage1PackageQuery(Language1Query q) or
62+
TLanguage2PackageQuery(Language2Query q) or
6163
TMiscPackageQuery(MiscQuery q) or
6264
TPointers1PackageQuery(Pointers1Query q) or
6365
TPointers2PackageQuery(Pointers2Query q) or
@@ -95,6 +97,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) {
9597
isIO3QueryMetadata(query, queryId, ruleId) or
9698
isIO4QueryMetadata(query, queryId, ruleId) or
9799
isLanguage1QueryMetadata(query, queryId, ruleId) or
100+
isLanguage2QueryMetadata(query, queryId, ruleId) or
98101
isMiscQueryMetadata(query, queryId, ruleId) or
99102
isPointers1QueryMetadata(query, queryId, ruleId) or
100103
isPointers2QueryMetadata(query, queryId, ruleId) or
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Provides a library which includes a `problems` predicate for reporting
3+
* undocumented uses of assembly.
4+
*/
5+
6+
import cpp
7+
import codingstandards.cpp.Customizations
8+
import codingstandards.cpp.Exclusions
9+
10+
abstract class UsageOfAssemblerNotDocumentedSharedQuery extends Query { }
11+
12+
Query getQuery() { result instanceof UsageOfAssemblerNotDocumentedSharedQuery }
13+
14+
query predicate problems(AsmStmt a, string message) {
15+
not isExcluded(a, getQuery()) and
16+
not exists(Comment c | c.getCommentedElement() = a) and
17+
not a.isAffectedByMacro() and
18+
message = "Use of assembler is not documented."
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| test.cpp:8:42:8:58 | asm statement | Use of assembler is not documented. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// COMPLIANT
2+
void test_assembly_is_documented() {
3+
// This comment serves as documentation
4+
__asm__("ret\n");
5+
}
6+
7+
// NON_COMPLIANT
8+
void test_assembly_is_not_documented() { __asm__("ret\n"); }
9+
10+
// COMPLIANT
11+
#define RETURN __asm__("ret\n")
12+
void test_undocumented_assembly_from_macro() { RETURN; }

rule_packages/c/Language2.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"MISRA-C-2012": {
3+
"DIR-4-2": {
4+
"properties": {
5+
"obligation": "advisory"
6+
},
7+
"queries": [
8+
{
9+
"description": "Assembly language is not portable and should be documented.",
10+
"kind": "problem",
11+
"name": "All usage of assembly language should be documented",
12+
"precision": "very-high",
13+
"severity": "warning",
14+
"short_name": "UsageOfAssemblyLanguageShouldBeDocumented",
15+
"shared_implementation_short_name": "UsageOfAssemblerNotDocumented",
16+
"tags": [
17+
"maintainability",
18+
"readability"
19+
]
20+
}
21+
],
22+
"title": "All usage of assembly language should be documented"
23+
},
24+
"RULE-1-4": {
25+
"properties": {
26+
"obligation": "required"
27+
},
28+
"queries": [
29+
{
30+
"description": "Emergent language features may have unpredictable behavior and should not be used.",
31+
"kind": "problem",
32+
"name": "Emergent language features shall not be used",
33+
"precision": "very-high",
34+
"severity": "warning",
35+
"short_name": "EmergentLanguageFeaturesUsed",
36+
"tags": [
37+
"maintainability",
38+
"readability"
39+
]
40+
}
41+
],
42+
"title": "Emergent language features shall not be used"
43+
}
44+
}
45+
}

rule_packages/cpp/BannedLibraries.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
"precision": "very-high",
230230
"severity": "recommendation",
231231
"short_name": "UsageOfAssemblerNotDocumented",
232+
"shared_implementation_short_name": "UsageOfAssemblerNotDocumented",
232233
"tags": [
233234
"readability",
234235
"maintainability",

rules.csv

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cpp,AUTOSAR,A1-1-2,Yes,Required,Non-Automated,Implementation / Toolchain,A warni
1414
cpp,AUTOSAR,A1-1-3,Yes,Required,Non-Automated,Toolchain,An optimization option that disregards strict standard compliance shall not be turned on in the chosen compiler.,,Toolchain,Easy,Note: this is currently only possible for compilations that do not use response files.
1515
cpp,AUTOSAR,A1-2-1,No,Required,Non-Automated,Toolchain,"When using a compiler toolchain (including preprocessor, compiler itself, linker, C++ standard libraries) in safety-related software, the tool confidence level (TCL) shall be determined. In case of TCL2 or TCL3, the compiler shall undergo a 'Qualification of a software tool', as per ISO 26262-8.11.4.6 [6].",,,,Allocated target not covered by CodeQL
1616
cpp,AUTOSAR,A1-4-1,No,Required,Non-Automated,Implementation / Verification,Code metrics and their valid boundaries shall be defined and code shall comply with defined boundaries of code metrics.,,,,Allocated target not covered by CodeQL
17-
cpp,AUTOSAR,A1-4-3,No,Advisory,Automated,Implementation,All code should compile free of compiler warnings.,,,,"This should be checked via the compiler output, rather than CodeQL, which adds unecessary steps."
17+
cpp,AUTOSAR,A1-4-3,No,Advisory,Automated,Implementation,All code should compile free of compiler warnings.,,,,"This should be checked via the compiler output, rather than CodeQL, which adds unnecessary steps."
1818
cpp,AUTOSAR,A10-0-1,Yes,Required,Non-Automated,Design,Public inheritance shall be used to implement 'is-a' relationship.,,Inheritance,Audit,Report a list of Inheritance relationships.
1919
cpp,AUTOSAR,A10-0-2,Yes,Required,Non-Automated,Design,Membership or non-public inheritance shall be used to implement 'has-a' relationship.,,Inheritance,Audit,Report a list of membership relationships.
2020
cpp,AUTOSAR,A10-1-1,Yes,Required,Automated,Implementation,Class shall not be derived from more than one base class which is not an interface class.,,Inheritance,Easy,
@@ -600,10 +600,10 @@ c,CERT-C,STR37-C,Yes,Rule,,,Arguments to character-handling functions must be re
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,
602602
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,
603+
c,MISRA-C-2012,DIR-2-1,No,Required,,,All source files shall compile without any compilation errors,A1-4-3,,Medium,"This should be checked via the compiler output, rather than CodeQL, which adds unnecessary steps."
604604
c,MISRA-C-2012,DIR-3-1,No,Required,,,All code shall be traceable to documented requirements,,,,
605605
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,
606+
c,MISRA-C-2012,DIR-4-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language2,Import,
607607
c,MISRA-C-2012,DIR-4-3,Yes,Required,,,Assembly language shall be encapsulated and isolated,,Language1,Medium,
608608
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,
@@ -616,10 +616,10 @@ c,MISRA-C-2012,DIR-4-11,Yes,Required,,,The validity of values passed to library
616616
c,MISRA-C-2012,DIR-4-12,Yes,Required,,,Dynamic memory allocation shall not be used,,Banned,Medium,
617617
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,
618618
c,MISRA-C-2012,DIR-4-14,Yes,Required,,,The validity of values received from external sources shall be checked,,Contracts,Hard,
619-
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 implementations translation limits",,Language,Easy,
620-
c,MISRA-C-2012,RULE-1-2,Yes,Advisory,,,Language extensions should not be used,,Language,Easy,
621-
c,MISRA-C-2012,RULE-1-3,Yes,Required,,,There shall be no occurrence of undefined or critical unspecified behaviour,,Language,Hard,
622-
c,MISRA-C-2012,RULE-1-4,Yes,Required,,,Emergent language features shall not be used,,Language,Medium,
619+
c,MISRA-C-2012,RULE-1-1,No,Required,,,"The program shall contain no violations of the standard C syntax and constraints, and shall not exceed the implementation's translation limits",,,Easy,"This should be checked via the compiler output, rather than CodeQL, which adds unnecessary steps."
620+
c,MISRA-C-2012,RULE-1-2,Yes,Advisory,,,Language extensions should not be used,,Language3,Hard,
621+
c,MISRA-C-2012,RULE-1-3,Yes,Required,,,There shall be no occurrence of undefined or critical unspecified behaviour,,Language3,Hard,
622+
c,MISRA-C-2012,RULE-1-4,Yes,Required,,,Emergent language features shall not be used,,Language2,Medium,
623623
c,MISRA-C-2012,RULE-2-1,Yes,Required,,,A project shall not contain unreachable code,M0-1-1,DeadCode,Import,
624624
c,MISRA-C-2012,RULE-2-2,Yes,Required,,,There shall be no dead code,M0-1-9,DeadCode,Import,
625625
c,MISRA-C-2012,RULE-2-3,Yes,Advisory,,,A project should not contain unused type declarations,A0-1-6,DeadCode,Import,

0 commit comments

Comments
 (0)