Open
Show file tree
Hide file tree
Changes from all commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Failed to load files.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype PreprocessorQuery =
TUndefOfMacroNotDefinedInFileQuery() or
TInvalidTokenInDefinedOperatorQuery() or
TDefinedOperatorExpandedInIfDirectiveQuery() or
TNoValidIfdefGuardInHeaderQuery()

predicate isPreprocessorQueryMetadata(Query query, string queryId, string ruleId, string category) {
query =
// `Query` instance for the `undefOfMacroNotDefinedInFile` query
PreprocessorPackage::undefOfMacroNotDefinedInFileQuery() and
queryId =
// `@id` for the `undefOfMacroNotDefinedInFile` query
"cpp/misra/undef-of-macro-not-defined-in-file" and
ruleId = "RULE-19-0-4" and
category = "advisory"
or
query =
// `Query` instance for the `invalidTokenInDefinedOperator` query
PreprocessorPackage::invalidTokenInDefinedOperatorQuery() and
queryId =
// `@id` for the `invalidTokenInDefinedOperator` query
"cpp/misra/invalid-token-in-defined-operator" and
ruleId = "RULE-19-1-1" and
category = "required"
or
query =
// `Query` instance for the `definedOperatorExpandedInIfDirective` query
PreprocessorPackage::definedOperatorExpandedInIfDirectiveQuery() and
queryId =
// `@id` for the `definedOperatorExpandedInIfDirective` query
"cpp/misra/defined-operator-expanded-in-if-directive" and
ruleId = "RULE-19-1-1" and
category = "required"
or
query =
// `Query` instance for the `noValidIfdefGuardInHeader` query
PreprocessorPackage::noValidIfdefGuardInHeaderQuery() and
queryId =
// `@id` for the `noValidIfdefGuardInHeader` query
"cpp/misra/no-valid-ifdef-guard-in-header" and
ruleId = "RULE-19-2-1" and
category = "required"
}

module PreprocessorPackage {
Query undefOfMacroNotDefinedInFileQuery() {
//autogenerate `Query` type
result =
// `Query` type for `undefOfMacroNotDefinedInFile` query
TQueryCPP(TPreprocessorPackageQuery(TUndefOfMacroNotDefinedInFileQuery()))
}

Query invalidTokenInDefinedOperatorQuery() {
//autogenerate `Query` type
result =
// `Query` type for `invalidTokenInDefinedOperator` query
TQueryCPP(TPreprocessorPackageQuery(TInvalidTokenInDefinedOperatorQuery()))
}

Query definedOperatorExpandedInIfDirectiveQuery() {
//autogenerate `Query` type
result =
// `Query` type for `definedOperatorExpandedInIfDirective` query
TQueryCPP(TPreprocessorPackageQuery(TDefinedOperatorExpandedInIfDirectiveQuery()))
}

Query noValidIfdefGuardInHeaderQuery() {
//autogenerate `Query` type
result =
// `Query` type for `noValidIfdefGuardInHeader` query
TQueryCPP(TPreprocessorPackageQuery(TNoValidIfdefGuardInHeaderQuery()))
}
}
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,6 +41,7 @@ import Operators
import OrderOfEvaluation
import OutOfBounds
import Pointers
import Preprocessor
import Representation
import Scope
import SideEffects1
Expand DownExpand Up@@ -96,6 +97,7 @@ newtype TCPPQuery =
TOrderOfEvaluationPackageQuery(OrderOfEvaluationQuery q) or
TOutOfBoundsPackageQuery(OutOfBoundsQuery q) or
TPointersPackageQuery(PointersQuery q) or
TPreprocessorPackageQuery(PreprocessorQuery q) or
TRepresentationPackageQuery(RepresentationQuery q) or
TScopePackageQuery(ScopeQuery q) or
TSideEffects1PackageQuery(SideEffects1Query q) or
Expand DownExpand Up@@ -151,6 +153,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isOrderOfEvaluationQueryMetadata(query, queryId, ruleId, category) or
isOutOfBoundsQueryMetadata(query, queryId, ruleId, category) or
isPointersQueryMetadata(query, queryId, ruleId, category) or
isPreprocessorQueryMetadata(query, queryId, ruleId, category) or
isRepresentationQueryMetadata(query, queryId, ruleId, category) or
isScopeQueryMetadata(query, queryId, ruleId, category) or
isSideEffects1QueryMetadata(query, queryId, ruleId, category) or
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
bindingset[this]
signature class ItemSig {
bindingset[this]
string toString();
}

module Pair<ItemSig A, ItemSig B> {
signature predicate pred(A a, B b);

module Where<pred/2 ctor> {
private newtype TAll = TSome(A a, B b) { ctor(a, b) }

class Pair extends TAll {
A getFirst() { this = TSome(result, _) }

B getSecond() { this = TSome(_, result) }

string toString() { result = getFirst().toString() + ", " + getSecond().toString() }
}
}
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
/**
* @id cpp/misra/undef-of-macro-not-defined-in-file
* @name RULE-19-0-4: #undef should only be used for macros defined previously in the same file
* @description Using #undef to undefine a macro that is not defined in the same file can lead to
* confusion.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-19-0-4
* scope/single-translation-unit
* readability
* maintainability
* external/misra/enforcement/decidable
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.util.CondensedList
import codingstandards.cpp.util.Pair

class DefOrUndef extends PreprocessorDirective {
string name;

DefOrUndef() {
name = this.(PreprocessorUndef).getName() or
name = this.(Macro).getName()
}

string getName() { result = name }
}

predicate relevantNameAndFile(string name, File file) {
exists(DefOrUndef m |
m.getName() = name and
m.getFile() = file
)
}

class StringFilePair = Pair<string, File>::Where<relevantNameAndFile/2>::Pair;

module DefUndefListConfig implements CondensedListSig {
class Division = StringFilePair;

class Item = DefOrUndef;

int getSparseIndex(StringFilePair division, DefOrUndef directive) {
directive.getName() = division.getFirst() and
directive.getFile() = division.getSecond() and
result = directive.getLocation().getStartLine()
}
}

class ListEntry = Condense<DefUndefListConfig>::ListEntry;

from PreprocessorUndef undef, ListEntry defUndefListEntry
where
not isExcluded(undef, PreprocessorPackage::undefOfMacroNotDefinedInFileQuery()) and
// There exists a def or undef for a given name and file, and it is an #undef
undef = defUndefListEntry.getItem() and
// Exclude cases where the previous def or undef with the same name in the same file is a #define
not exists(ListEntry prev |
prev = defUndefListEntry.getPrev() and
prev.getItem() instanceof Macro
)
select undef, "Undef of name '" + undef.getName() + "' not defined in the same file."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
/**
* @id cpp/misra/defined-operator-expanded-in-if-directive
* @name RULE-19-1-1: The defined preprocessor operator shall be used appropriately
* @description Macro expansions that produce the token 'defined' inside of an if directive result
* in undefined behavior.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-19-1-1
* scope/single-translation-unit
* correctness
* maintainability
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra

from PreprocessorIf ifDirective, MacroInvocation mi
where
not isExcluded(ifDirective, PreprocessorPackage::definedOperatorExpandedInIfDirectiveQuery()) and
ifDirective.getLocation().subsumes(mi.getLocation()) and
mi.getMacro().getBody().regexpMatch(".*defined.*")
select ifDirective,
"If directive contains macro expansion including the token 'defined' from macro $@, which results in undefined behavior.",
mi.getMacro(), mi.getMacroName()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
/**
* @id cpp/misra/invalid-token-in-defined-operator
* @name RULE-19-1-1: The defined preprocessor operator shall be used appropriately
* @description Using the defined operator without an immediately following optionally parenthesized
* identifier results in undefined behavior.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-19-1-1
* scope/single-translation-unit
* correctness
* maintainability
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra

string idRegex() { result = "[a-zA-Z_]([a-zA-Z_0-9]*)" }

bindingset[body]
predicate hasInvalidDefinedOperator(string body) {
body.regexpMatch(".*\\bdefined" +
// Contains text "defined" at a word break
// Negative zero width lookahead:
"(?!(" +
// (group) optional whitespace followed by a valid identifier
"(\\s*" + idRegex() + ")" +
// or
"|" +
// (group) optional whitespace followed by parenthesis and valid identifier
"(\\s*\\(\\s*" + idRegex() + "\\s*\\))" +
// End negative zero width lookahead, match remaining text
")).*")
}

from PreprocessorIf ifDirective
where
not isExcluded(ifDirective, PreprocessorPackage::invalidTokenInDefinedOperatorQuery()) and
hasInvalidDefinedOperator(ifDirective.getHead())
select ifDirective, "Invalid use of defined operator in if directive."
Loading
Loading