Skip to content

Implement MISRA23 Import rules #591

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 16 commits into from
Jul 23, 2024
Merged
401 changes: 401 additions & 0 deletions cpp/common/src/codingstandards/cpp/exclusions/cpp/ImportMisra23.qll

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Expressions
import Freed
import Functions
import IO
import ImportMisra23
import Includes
import Inheritance
import Initialization
Expand Down Expand Up @@ -74,6 +75,7 @@ newtype TCPPQuery =
TFreedPackageQuery(FreedQuery q) or
TFunctionsPackageQuery(FunctionsQuery q) or
TIOPackageQuery(IOQuery q) or
TImportMisra23PackageQuery(ImportMisra23Query q) or
TIncludesPackageQuery(IncludesQuery q) or
TInheritancePackageQuery(InheritanceQuery q) or
TInitializationPackageQuery(InitializationQuery q) or
Expand Down Expand Up @@ -127,6 +129,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isFreedQueryMetadata(query, queryId, ruleId, category) or
isFunctionsQueryMetadata(query, queryId, ruleId, category) or
isIOQueryMetadata(query, queryId, ruleId, category) or
isImportMisra23QueryMetadata(query, queryId, ruleId, category) or
isIncludesQueryMetadata(query, queryId, ruleId, category) or
isInheritanceQueryMetadata(query, queryId, ruleId, category) or
isInitializationQueryMetadata(query, queryId, ruleId, category) or
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @id cpp/misra/sections-of-code-should-not-be-commented-out
* @name DIR-5-7-2: Sections of code should not be “commented out”
* @description Commented out code may become out of date leading to developer confusion.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/dir-5-7-2
* maintainability
* readability
* correctness
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.sectionsofcodeshallnotbecommentedout.SectionsOfCodeShallNotBeCommentedOut

class SectionsOfCodeShouldNotBeCommentedOutQuery extends SectionsOfCodeShallNotBeCommentedOutSharedQuery
{
SectionsOfCodeShouldNotBeCommentedOutQuery() {
this = ImportMisra23Package::sectionsOfCodeShouldNotBeCommentedOutQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @id cpp/misra/declaration-of-an-object-indirections-level
* @name RULE-11-3-2: The declaration of an object should contain no more than two levels of pointer indirection
* @description Declarations with more than two levels of pointer nesting can result in code that is
* difficult to read and understand.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-11-3-2
* readability
* maintainability
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.donotusemorethantwolevelsofpointerindirection.DoNotUseMoreThanTwoLevelsOfPointerIndirection

class DeclarationOfAnObjectIndirectionsLevelQuery extends DoNotUseMoreThanTwoLevelsOfPointerIndirectionSharedQuery
{
DeclarationOfAnObjectIndirectionsLevelQuery() {
this = ImportMisra23Package::declarationOfAnObjectIndirectionsLevelQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id cpp/misra/handlers-refer-to-non-static-members-from-their-class
* @name RULE-18-3-3: Handlers for a function-try-block of a constructor or destructor shall not refer to non-static
* @description Handlers for a function-try-block of a constructor or destructor shall not refer to
* non-static members from their class or its bases.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-18-3-3
* correctness
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.destroyedvaluereferencedindestructorcatchblock.DestroyedValueReferencedInDestructorCatchBlock

class HandlersReferToNonStaticMembersFromTheirClassQuery extends DestroyedValueReferencedInDestructorCatchBlockSharedQuery
{
HandlersReferToNonStaticMembersFromTheirClassQuery() {
this = ImportMisra23Package::handlersReferToNonStaticMembersFromTheirClassQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id cpp/misra/include-directives-preceded-by-preprocessor-directives
* @name RULE-19-0-3: #include directives should only be preceded by preprocessor directives or comments
* @description Using anything other than other pre-processor directives or comments before an
* '#include' directive makes the code more difficult to read.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-19-0-3
* readability
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.preprocessorincludespreceded.PreprocessorIncludesPreceded

class IncludeDirectivesPrecededByPreprocessorDirectivesQuery extends PreprocessorIncludesPrecededSharedQuery
{
IncludeDirectivesPrecededByPreprocessorDirectivesQuery() {
this = ImportMisra23Package::includeDirectivesPrecededByPreprocessorDirectivesQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id cpp/misra/identifiers-used-in-the-controlling-expression-of
* @name RULE-19-1-3: All identifiers used in the controlling expression of #if or #elif preprocessing directives shall be
* @description All identifiers used in the controlling expression of #if or #elif preprocessing
* directives shall be defined prior to evaluation.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-19-1-3
* correctness
* readability
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.undefinedmacroidentifiers.UndefinedMacroIdentifiers

class IdentifiersUsedInTheControllingExpressionOfQuery extends UndefinedMacroIdentifiersSharedQuery {
IdentifiersUsedInTheControllingExpressionOfQuery() {
this = ImportMisra23Package::identifiersUsedInTheControllingExpressionOfQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id cpp/misra/chars-that-should-not-occur-in-header-file-name
* @name RULE-19-2-3: The ' or " or \ characters and the /* or // character sequences shall not occur in a header file
* @description The ' or " or \ characters and the /* or // character sequences shall not occur in a
* header file name.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-19-2-3
* scope/single-translation-unit
* correctness
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.preprocessorincludesforbiddenheadernames.PreprocessorIncludesForbiddenHeaderNames

class CharsThatShouldNotOccurInHeaderFileNameQuery extends PreprocessorIncludesForbiddenHeaderNamesSharedQuery
{
CharsThatShouldNotOccurInHeaderFileNameQuery() {
this = ImportMisra23Package::charsThatShouldNotOccurInHeaderFileNameQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @id cpp/misra/and-preprocessor-operators-should-not-be-used
* @name RULE-19-3-1: The # and ## preprocessor operators should not be used
* @description The order of evaluation for the '#' and '##' operators may differ between compilers,
* which can cause unexpected behaviour.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-19-3-1
* correctness
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.hashoperatorsused.HashOperatorsUsed

class AndPreprocessorOperatorsShouldNotBeUsedQuery extends HashOperatorsUsedSharedQuery {
AndPreprocessorOperatorsShouldNotBeUsedQuery() {
this = ImportMisra23Package::andPreprocessorOperatorsShouldNotBeUsedQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @id cpp/misra/tokens-that-look-like-directives-in-a-macro-argument
* @name RULE-19-3-5: Tokens that look like a preprocessing directive shall not occur within a macro argument
* @description Arguments to a function-like macro shall not contain tokens that look like
* pre-processing directives or else behaviour after macro expansion is unpredictable.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-19-3-5
* readability
* correctness
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.preprocessingdirectivewithinmacroargument.PreprocessingDirectiveWithinMacroArgument

class TokensThatLookLikeDirectivesInAMacroArgumentQuery extends PreprocessingDirectiveWithinMacroArgumentSharedQuery
{
TokensThatLookLikeDirectivesInAMacroArgumentQuery() {
this = ImportMisra23Package::tokensThatLookLikeDirectivesInAMacroArgumentQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @id cpp/misra/pointer-to-an-incomplete-class-type-deleted
* @name RULE-21-6-5: A pointer to an incomplete class type shall not be deleted
* @description Do not delete pointers to incomplete classes to prevent undefined behavior.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-21-6-5
* correctness
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.deleteofpointertoincompleteclass.DeleteOfPointerToIncompleteClass

class PointerToAnIncompleteClassTypeDeletedQuery extends DeleteOfPointerToIncompleteClassSharedQuery
{
PointerToAnIncompleteClassTypeDeletedQuery() {
this = ImportMisra23Package::pointerToAnIncompleteClassTypeDeletedQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @id cpp/misra/pointers-returned-by-locale-functions-must-be-used-as-const
* @name RULE-25-5-2: The pointers returned by environment functions should be treated as const
* @description The pointers returned by the C++ Standard Library functions localeconv, getenv,
* setlocale or strerror must only be used as if they have pointer to const-qualified
* type.
* @kind path-problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-25-5-2
* correctness
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/mandatory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.constlikereturnvalue.ConstLikeReturnValue

class PointersReturnedByLocaleFunctionsMustBeUsedAsConstQuery extends ConstLikeReturnValueSharedQuery
{
PointersReturnedByLocaleFunctionsMustBeUsedAsConstQuery() {
this = ImportMisra23Package::pointersReturnedByLocaleFunctionsMustBeUsedAsConstQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @id cpp/misra/call-to-setlocale-invalidates-old-pointers-misra
* @name RULE-25-5-3: The pointer returned by the Standard Library env functions is invalid
* @description The pointer returned by the Standard Library functions asctime, ctime, gmtime,
* localtime, localeconv, getenv, setlocale or strerror may be invalid following a
* subsequent call to the same function.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-25-5-3
* correctness
* scope/system
* external/misra/enforcement/undecidable
* external/misra/obligation/mandatory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.invalidatedenvstringpointers.InvalidatedEnvStringPointers

class CallToSetlocaleInvalidatesOldPointersMisraQuery extends InvalidatedEnvStringPointersSharedQuery
{
CallToSetlocaleInvalidatesOldPointersMisraQuery() {
this = ImportMisra23Package::callToSetlocaleInvalidatesOldPointersMisraQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @id cpp/misra/call-to-setlocale-invalidates-old-pointers-warn-misra
* @name RULE-25-5-3: The pointer returned by the Standard Library env functions is invalid warning
* @description The pointer returned by the Standard Library functions asctime, ctime, gmtime,
* localtime, localeconv, getenv, setlocale or strerror may be invalid following a
* subsequent call to the same function.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-25-5-3
* correctness
* scope/system
* external/misra/enforcement/undecidable
* external/misra/obligation/mandatory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.invalidatedenvstringpointerswarn.InvalidatedEnvStringPointersWarn

class CallToSetlocaleInvalidatesOldPointersWarnMisraQuery extends InvalidatedEnvStringPointersWarnSharedQuery
{
CallToSetlocaleInvalidatesOldPointersWarnMisraQuery() {
this = ImportMisra23Package::callToSetlocaleInvalidatesOldPointersWarnMisraQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @id cpp/misra/object-used-while-in-potentially-moved-from-state
* @name RULE-28-6-3: An object shall not be used while in a potentially moved-from state
* @description Moved-from object shall not be read-accessed.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-28-6-3
* correctness
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.movedfromobjectsunspecifiedstate.MovedFromObjectsUnspecifiedState

class ObjectUsedWhileInPotentiallyMovedFromStateQuery extends MovedFromObjectsUnspecifiedStateSharedQuery
{
ObjectUsedWhileInPotentiallyMovedFromStateQuery() {
this = ImportMisra23Package::objectUsedWhileInPotentiallyMovedFromStateQuery()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id cpp/misra/reads-and-writes-on-stream-not-separated-by-positioning
* @name RULE-30-0-2: Reads and writes on the same file stream shall be separated by a positioning operation
* @description Alternate input and output operations on a file stream shall not be used without an
* intervening flush or positioning call.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-30-0-2
* correctness
* scope/system
* external/misra/enforcement/undecidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.iofstreammissingpositioning.IOFstreamMissingPositioning

class ReadsAndWritesOnStreamNotSeparatedByPositioningQuery extends IOFstreamMissingPositioningSharedQuery
{
ReadsAndWritesOnStreamNotSeparatedByPositioningQuery() {
this = ImportMisra23Package::readsAndWritesOnStreamNotSeparatedByPositioningQuery()
}
}
Loading
Loading