Skip to content

Commit 16c0a8c

Browse files
committed
Rule 14.4: Convert to non-shared queries
This rule requires the use of the MISRA only essential types library. This is because in C comparison expressions do not natively have boolean type.
1 parent 745d04c commit 16c0a8c

16 files changed

+45
-27
lines changed

c/common/test/rules/nonbooleanifstmt/NonBooleanIfStmt.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

c/common/test/rules/nonbooleanifstmt/NonBooleanIfStmt.ql

Lines changed: 0 additions & 2 deletions
This file was deleted.

c/common/test/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

c/common/test/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.ql

Lines changed: 0 additions & 2 deletions
This file was deleted.

c/misra/src/rules/RULE-14-4/NonBooleanIfCondition.ql

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313

1414
import cpp
1515
import codingstandards.c.misra
16-
import codingstandards.cpp.rules.nonbooleanifstmt.NonBooleanIfStmt
16+
import codingstandards.c.misra.EssentialTypes
1717

18-
class NonBooleanIfConditionQuery extends NonBooleanIfStmtSharedQuery {
19-
NonBooleanIfConditionQuery() {
20-
this = Statements4Package::nonBooleanIfConditionQuery()
21-
}
22-
}
18+
from Expr condition, Type essentialType
19+
where
20+
not isExcluded(condition, Statements4Package::nonBooleanIfConditionQuery()) and
21+
exists(IfStmt ifStmt |
22+
not ifStmt.isFromUninstantiatedTemplate(_) and
23+
condition = ifStmt.getCondition() and
24+
essentialType = getEssentialType(ifStmt.getCondition()) and
25+
not getEssentialTypeCategory(essentialType) = EssentiallyBooleanType()
26+
)
27+
select condition, "If condition has non boolean essential type " + essentialType + "."

c/misra/src/rules/RULE-14-4/NonBooleanIterationCondition.ql

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,24 @@
1313

1414
import cpp
1515
import codingstandards.c.misra
16-
import codingstandards.cpp.rules.nonbooleaniterationstmt.NonBooleanIterationStmt
16+
import codingstandards.c.misra.EssentialTypes
1717

18-
class NonBooleanIterationConditionQuery extends NonBooleanIterationStmtSharedQuery {
19-
NonBooleanIterationConditionQuery() {
20-
this = Statements4Package::nonBooleanIterationConditionQuery()
21-
}
18+
/** A macro within the source location of this project. */
19+
class UserProvidedMacro extends Macro {
20+
UserProvidedMacro() { exists(this.getFile().getRelativePath()) }
2221
}
22+
23+
/** A macro defined within a library used by this project. */
24+
class LibraryMacro extends Macro {
25+
LibraryMacro() { not this instanceof UserProvidedMacro }
26+
}
27+
28+
from Expr condition, Loop l, Type essentialType
29+
where
30+
not isExcluded(condition, Statements4Package::nonBooleanIterationConditionQuery()) and
31+
// Exclude loops generated from library macros
32+
not l = any(LibraryMacro lm).getAnInvocation().getAGeneratedElement() and
33+
condition = l.getCondition() and
34+
essentialType = getEssentialType(condition) and
35+
not getEssentialTypeCategory(essentialType) = EssentiallyBooleanType()
36+
select condition, "Iteration condition has non boolean type " + essentialType + "."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:7:7:7:8 | l1 | If condition has non boolean essential type int. |
2+
| test.c:9:7:9:8 | call to f1 | If condition has non boolean essential type int. |
3+
| test.c:12:7:12:8 | l2 | If condition has non boolean essential type void *. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-14-4/NonBooleanIfCondition.ql

c/misra/test/rules/RULE-14-4/NonBooleanIfCondition.testref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test_iteration.c:5:20:5:20 | i | Iteration condition has non boolean type int. |
2+
| test_iteration.c:7:10:7:11 | l1 | Iteration condition has non boolean type int. |

0 commit comments

Comments
 (0)