Skip to content

Implement MISRA-C++23 Preprocesser package rules 19-0-4, 19-1-1, and 19-2-1 #893

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Format additional files
  • Loading branch information
MichaelRFairhurst committed Apr 29, 2025
commit a31e0473429cac7dbc5258294a0168cfd5d438a6
20 changes: 9 additions & 11 deletions cpp/common/src/codingstandards/cpp/util/CondensedList.qll
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ signature module CondensedListSig {
*
* For instance, if connecting variables defined in a file, the index will be the line number of
* the variable in the file.
*
*
* The sparse index (which may have gaps) is used to determine the ordering of the items in the
* condensed list. Once the condensed list is created, the items in the list will automatically be
* assigned a dense index (which has no gaps).
*
*
* There must be no duplicate indices for the same division for correctness.
*/
int getSparseIndex(Division d, Item l);
Expand All @@ -40,7 +40,7 @@ signature module CondensedListSig {
/**
* A module to take orderable data (which may not be continuous) and condense it into one or more
* dense lists, with one such list per specified division.
*
*
* To instantiate this module, you need to provide a `CondensedListSig` module that
* specifies the spare index and division of the items to be connected.
*
Expand All @@ -67,7 +67,9 @@ signature module CondensedListSig {
module Condense<CondensedListSig Config> {
newtype TList =
THead(Config::Item l, Config::Division t) { denseRank(t, l) = 1 } or
TCons(ListEntry prev, Config::Item l) { prev.getDenseIndex() = denseRank(prev.getDivision(), l) - 1 }
TCons(ListEntry prev, Config::Item l) {
prev.getDenseIndex() = denseRank(prev.getDivision(), l) - 1
}

private module DenseRankConfig implements DenseRankInputSig2 {
class Ranked = Config::Item;
Expand All @@ -86,22 +88,18 @@ module Condense<CondensedListSig Config> {
exists(ListEntry prev | this = TCons(prev, _) and result = prev.getDivision())
}

string toString() {
result = getItem().toString() + " [index " + getDenseIndex() + "]"
}
string toString() { result = getItem().toString() + " [index " + getDenseIndex() + "]" }

Config::Item getItem() {
this = THead(result, _)
or
this = TCons(_, result)
}

int getDenseIndex() {
result = denseRank(getDivision(), getItem())
}
int getDenseIndex() { result = denseRank(getDivision(), getItem()) }

ListEntry getPrev() { this = TCons(result, _) }

ListEntry getNext() { result.getPrev() = this }
}
}
}
22 changes: 7 additions & 15 deletions cpp/common/src/codingstandards/cpp/util/Pair.qll
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
bindingset[this]
signature class ItemSig {
bindingset[this]
string toString();
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)
}
private newtype TAll = TSome(A a, B b) { ctor(a, b) }

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

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

string toString() {
result = getFirst().toString() + ", " + getSecond().toString()
}
string toString() { result = getFirst().toString() + ", " + getSecond().toString() }
}
}
}
}
31 changes: 14 additions & 17 deletions cpp/misra/src/rules/RULE-19-1-1/InvalidTokenInDefinedOperator.ql
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,26 @@
import cpp
import codingstandards.cpp.misra

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

bindingset[body]
predicate hasInvalidDefinedOperator(string body) {
body.regexpMatch(
// Contains text "defined" at a word break
".*\\bdefined" +
// 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
")).*")
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."
select ifDirective, "Invalid use of defined operator in if directive."
Loading