Skip to content

Language 2 #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"Iterators",
"Lambdas",
"Language1",
"Language2",
"Literals",
"Loops",
"Macros",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @id c/misra/usage-of-assembly-language-should-be-documented
* @name DIR-4-2: All usage of assembly language should be documented
* @description Assembly language is not portable and should be documented.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/dir-4-2
* maintainability
* readability
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented

class UsageOfAssemblyLanguageShouldBeDocumentedQuery extends UsageOfAssemblerNotDocumentedSharedQuery {
UsageOfAssemblyLanguageShouldBeDocumentedQuery() {
this = Language2Package::usageOfAssemblyLanguageShouldBeDocumentedQuery()
}
}
21 changes: 21 additions & 0 deletions c/misra/src/rules/RULE-1-4/EmergentLanguageFeaturesUsed.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @id c/misra/emergent-language-features-used
* @name RULE-1-4: Emergent language features shall not be used
* @description Emergent language features may have unpredictable behavior and should not be used.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-1-4
* maintainability
* readability
* external/misra/obligation/required
*/

import cpp
import codingstandards.c.misra
import codingstandards.cpp.Emergent

from C11::EmergentLanguageFeature ef
where not isExcluded(ef, Language2Package::emergentLanguageFeaturesUsedQuery())
select ef, "Usage of emergent language feature."

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/usageofassemblernotdocumented/UsageOfAssemblerNotDocumented.ql
15 changes: 15 additions & 0 deletions c/misra/test/rules/RULE-1-4/EmergentLanguageFeaturesUsed.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
| test.c:1:1:1:21 | #include <stdalign.h> | Usage of emergent language feature. |
| test.c:2:1:2:22 | #include <stdatomic.h> | Usage of emergent language feature. |
| test.c:3:1:3:24 | #include <stdnoreturn.h> | Usage of emergent language feature. |
| test.c:4:1:4:20 | #include <threads.h> | Usage of emergent language feature. |
| test.c:6:1:6:49 | #define MACRO(x) _Generic((x), int : 0, long : 1) | Usage of emergent language feature. |
| test.c:7:1:7:32 | #define __STDC_WANT_LIB_EXT1__ 1 | Usage of emergent language feature. |
| test.c:9:16:9:17 | f0 | Usage of emergent language feature. |
| test.c:12:26:12:40 | atomic_new_type | Usage of emergent language feature. |
| test.c:17:15:17:15 | i | Usage of emergent language feature. |
| test.c:19:3:19:10 | alignas(...) | Usage of emergent language feature. |
| test.c:20:3:20:9 | alignas(...) | Usage of emergent language feature. |
| test.c:21:11:21:23 | alignof(int) | Usage of emergent language feature. |
| test.c:22:12:22:23 | alignof(int) | Usage of emergent language feature. |
| test.c:24:27:24:28 | i3 | Usage of emergent language feature. |
| test.c:25:28:25:29 | i4 | Usage of emergent language feature. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-1-4/EmergentLanguageFeaturesUsed.ql
26 changes: 26 additions & 0 deletions c/misra/test/rules/RULE-1-4/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdalign.h> //NON_COMPLIANT
#include <stdatomic.h> //NON_COMPLIANT
#include <stdnoreturn.h> //NON_COMPLIANT
#include <threads.h> //NON_COMPLIANT

#define MACRO(x) _Generic((x), int : 0, long : 1) // NON_COMPLIANT
#define __STDC_WANT_LIB_EXT1__ 1 // NON_COMPLIANT

_Noreturn void f0(); // NON_COMPLIANT

typedef int new_type; // COMPLIANT
typedef _Atomic new_type atomic_new_type; // NON_COMPLIANT

void f(int p) {
int i0 = _Generic(p, int : 0, long : 1); // NON_COMPLIANT[FALSE_NEGATIVE]

_Atomic int i; // NON_COMPLIANT

_Alignas(4) int i1; // NON_COMPLIANT
alignas(4) int i2; // NON_COMPLIANT
int a = _Alignof(int); // NON_COMPLIANT
int a1 = alignof(int); // NON_COMPLIANT

static thread_local int i3; // NON_COMPLIANT
static _Thread_local int i4; // NON_COMPLIANT
}
12 changes: 6 additions & 6 deletions cpp/autosar/src/rules/M7-4-1/UsageOfAssemblerNotDocumented.ql
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/usageofassemblernotdocumented/UsageOfAssemblerNotDocumented.ql
49 changes: 49 additions & 0 deletions cpp/common/src/codingstandards/cpp/Emergent.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import cpp

/**
* Namespace for containing emergent language features in C11.
*/
module C11 {
abstract class EmergentLanguageFeature extends Element { }

class AlignAsAttribute extends EmergentLanguageFeature, Attribute {
AlignAsAttribute() { getName() = "_Alignas" }
}

class AtomicVariableSpecifier extends EmergentLanguageFeature, Variable {
AtomicVariableSpecifier() {
getType().(DerivedType).getBaseType*().getASpecifier().getName() = "atomic"
}
}

class AtomicDeclaration extends EmergentLanguageFeature, Declaration {
AtomicDeclaration() { getASpecifier().getName() = "atomic" }
}

class ThreadLocalDeclaration extends EmergentLanguageFeature, Declaration {
ThreadLocalDeclaration() { getASpecifier().getName() = "is_thread_local" }
}

class EmergentHeader extends EmergentLanguageFeature, Include {
EmergentHeader() {
getIncludedFile().getBaseName() = ["stdalign.h", "stdatomic.h", "stdnoreturn.h", "threads.h"]
}
}

class LibExt1Macro extends EmergentLanguageFeature, Macro {
LibExt1Macro() {
getName() = "__STDC_WANT_LIB_EXT1__" and
getBody() = "1"
}
}

class GenericMacro extends EmergentLanguageFeature, Macro {
GenericMacro() { getBody().indexOf("_Generic") = 0 }
}

class NoReturnSpecificer extends EmergentLanguageFeature, Function {
NoReturnSpecificer() { getASpecifier().getName() = "noreturn" }
}

class AlignOf extends EmergentLanguageFeature, AlignofTypeOperator { }
}
42 changes: 42 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/c/Language2.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype Language2Query =
TUsageOfAssemblyLanguageShouldBeDocumentedQuery() or
TEmergentLanguageFeaturesUsedQuery()

predicate isLanguage2QueryMetadata(Query query, string queryId, string ruleId) {
query =
// `Query` instance for the `usageOfAssemblyLanguageShouldBeDocumented` query
Language2Package::usageOfAssemblyLanguageShouldBeDocumentedQuery() and
queryId =
// `@id` for the `usageOfAssemblyLanguageShouldBeDocumented` query
"c/misra/usage-of-assembly-language-should-be-documented" and
ruleId = "DIR-4-2"
or
query =
// `Query` instance for the `emergentLanguageFeaturesUsed` query
Language2Package::emergentLanguageFeaturesUsedQuery() and
queryId =
// `@id` for the `emergentLanguageFeaturesUsed` query
"c/misra/emergent-language-features-used" and
ruleId = "RULE-1-4"
}

module Language2Package {
Query usageOfAssemblyLanguageShouldBeDocumentedQuery() {
//autogenerate `Query` type
result =
// `Query` type for `usageOfAssemblyLanguageShouldBeDocumented` query
TQueryC(TLanguage2PackageQuery(TUsageOfAssemblyLanguageShouldBeDocumentedQuery()))
}

Query emergentLanguageFeaturesUsedQuery() {
//autogenerate `Query` type
result =
// `Query` type for `emergentLanguageFeaturesUsed` query
TQueryC(TLanguage2PackageQuery(TEmergentLanguageFeaturesUsedQuery()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import IO2
import IO3
import IO4
import Language1
import Language2
import Misc
import Pointers1
import Pointers2
Expand Down Expand Up @@ -58,6 +59,7 @@ newtype TCQuery =
TIO3PackageQuery(IO3Query q) or
TIO4PackageQuery(IO4Query q) or
TLanguage1PackageQuery(Language1Query q) or
TLanguage2PackageQuery(Language2Query q) or
TMiscPackageQuery(MiscQuery q) or
TPointers1PackageQuery(Pointers1Query q) or
TPointers2PackageQuery(Pointers2Query q) or
Expand Down Expand Up @@ -95,6 +97,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) {
isIO3QueryMetadata(query, queryId, ruleId) or
isIO4QueryMetadata(query, queryId, ruleId) or
isLanguage1QueryMetadata(query, queryId, ruleId) or
isLanguage2QueryMetadata(query, queryId, ruleId) or
isMiscQueryMetadata(query, queryId, ruleId) or
isPointers1QueryMetadata(query, queryId, ruleId) or
isPointers2QueryMetadata(query, queryId, ruleId) or
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Provides a library which includes a `problems` predicate for reporting
* undocumented uses of assembly.
*/

import cpp
import codingstandards.cpp.Customizations
import codingstandards.cpp.Exclusions

abstract class UsageOfAssemblerNotDocumentedSharedQuery extends Query { }

Query getQuery() { result instanceof UsageOfAssemblerNotDocumentedSharedQuery }

query predicate problems(AsmStmt a, string message) {
not isExcluded(a, getQuery()) and
not exists(Comment c | c.getCommentedElement() = a) and
not a.isAffectedByMacro() and
message = "Use of assembler is not documented."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
| test.cpp:8:42:8:58 | asm statement | Use of assembler is not documented. |
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented
12 changes: 12 additions & 0 deletions cpp/common/test/rules/usageofassemblernotdocumented/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// COMPLIANT
void test_assembly_is_documented() {
// This comment serves as documentation
__asm__("ret\n");
}

// NON_COMPLIANT
void test_assembly_is_not_documented() { __asm__("ret\n"); }

// COMPLIANT
#define RETURN __asm__("ret\n")
void test_undocumented_assembly_from_macro() { RETURN; }
45 changes: 45 additions & 0 deletions rule_packages/c/Language2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"MISRA-C-2012": {
"DIR-4-2": {
"properties": {
"obligation": "advisory"
},
"queries": [
{
"description": "Assembly language is not portable and should be documented.",
"kind": "problem",
"name": "All usage of assembly language should be documented",
"precision": "very-high",
"severity": "warning",
"short_name": "UsageOfAssemblyLanguageShouldBeDocumented",
"shared_implementation_short_name": "UsageOfAssemblerNotDocumented",
"tags": [
"maintainability",
"readability"
]
}
],
"title": "All usage of assembly language should be documented"
},
"RULE-1-4": {
"properties": {
"obligation": "required"
},
"queries": [
{
"description": "Emergent language features may have unpredictable behavior and should not be used.",
"kind": "problem",
"name": "Emergent language features shall not be used",
"precision": "very-high",
"severity": "warning",
"short_name": "EmergentLanguageFeaturesUsed",
"tags": [
"maintainability",
"readability"
]
}
],
"title": "Emergent language features shall not be used"
}
}
}
1 change: 1 addition & 0 deletions rule_packages/cpp/BannedLibraries.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
"precision": "very-high",
"severity": "recommendation",
"short_name": "UsageOfAssemblerNotDocumented",
"shared_implementation_short_name": "UsageOfAssemblerNotDocumented",
"tags": [
"readability",
"maintainability",
Expand Down
14 changes: 7 additions & 7 deletions rules.csv
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cpp,AUTOSAR,A1-1-2,Yes,Required,Non-Automated,Implementation / Toolchain,A warni
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.
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
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
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."
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."
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.
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.
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,
Expand Down Expand Up @@ -600,10 +600,10 @@ c,CERT-C,STR37-C,Yes,Rule,,,Arguments to character-handling functions must be re
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,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-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."
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-2,Yes,Advisory,,,All usage of assembly language should be documented,M7-4-1,Language2,Import,
c,MISRA-C-2012,DIR-4-3,Yes,Required,,,Assembly language shall be encapsulated and isolated,,Language1,Medium,
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,
Expand All @@ -616,10 +616,10 @@ c,MISRA-C-2012,DIR-4-11,Yes,Required,,,The validity of values passed to library
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 implementations 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,
c,MISRA-C-2012,RULE-1-4,Yes,Required,,,Emergent language features shall not be used,,Language,Medium,
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."
c,MISRA-C-2012,RULE-1-2,Yes,Advisory,,,Language extensions should not be used,,Language3,Hard,
c,MISRA-C-2012,RULE-1-3,Yes,Required,,,There shall be no occurrence of undefined or critical unspecified behaviour,,Language3,Hard,
c,MISRA-C-2012,RULE-1-4,Yes,Required,,,Emergent language features shall not be used,,Language2,Medium,
c,MISRA-C-2012,RULE-2-1,Yes,Required,,,A project shall not contain unreachable code,M0-1-1,DeadCode,Import,
c,MISRA-C-2012,RULE-2-2,Yes,Required,,,There shall be no dead code,M0-1-9,DeadCode,Import,
c,MISRA-C-2012,RULE-2-3,Yes,Advisory,,,A project should not contain unused type declarations,A0-1-6,DeadCode,Import,
Expand Down