Skip to content

Commit 6554db0

Browse files
committed
Refactor to extractor PreprocessorIfOrElif.
1 parent 6dd5377 commit 6554db0

File tree

5 files changed

+23
-36
lines changed

5 files changed

+23
-36
lines changed

c/misra/src/rules/RULE-20-8/ControllingExpressionIfDirective.ql

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,35 @@
1414

1515
import cpp
1616
import codingstandards.c.misra
17+
import codingstandards.cpp.PreprocessorDirective
1718

1819
/* A controlling expression is evaluated if it is not excluded (guarded by another controlling expression that is not taken). This translates to it either being taken or not taken. */
1920
predicate isEvaluated(PreprocessorBranch b) { b.wasTaken() or b.wasNotTaken() }
2021

21-
class IfOrElifPreprocessorBranch extends PreprocessorBranch {
22-
IfOrElifPreprocessorBranch() {
23-
this instanceof PreprocessorIf or this instanceof PreprocessorElif
24-
}
25-
}
26-
2722
/**
2823
* Looks like it contains a single macro, which may be undefined
2924
*/
30-
class SimpleMacroPreprocessorBranch extends IfOrElifPreprocessorBranch {
25+
class SimpleMacroPreprocessorBranch extends PreprocessorIfOrElif {
3126
SimpleMacroPreprocessorBranch() { this.getHead().regexpMatch("[a-zA-Z_][a-zA-Z0-9_]+") }
3227
}
3328

34-
class SimpleNumericPreprocessorBranch extends IfOrElifPreprocessorBranch {
29+
class SimpleNumericPreprocessorBranch extends PreprocessorIfOrElif {
3530
SimpleNumericPreprocessorBranch() { this.getHead().regexpMatch("[0-9]+") }
3631
}
3732

3833
class ZeroOrOnePreprocessorBranch extends SimpleNumericPreprocessorBranch {
3934
ZeroOrOnePreprocessorBranch() { this.getHead().regexpMatch("[0|1]") }
4035
}
4136

42-
predicate containsOnlySafeOperators(IfOrElifPreprocessorBranch b) {
37+
predicate containsOnlySafeOperators(PreprocessorIfOrElif b) {
4338
containsOnlyDefinedOperator(b)
4439
or
4540
//logic: comparison operators eval last, so they make it safe?
4641
b.getHead().regexpMatch(".*[\\&\\&|\\|\\||>|<|==].*")
4742
}
4843

4944
//all defined operators is definitely safe
50-
predicate containsOnlyDefinedOperator(IfOrElifPreprocessorBranch b) {
45+
predicate containsOnlyDefinedOperator(PreprocessorIfOrElif b) {
5146
forall(string portion |
5247
portion =
5348
b.getHead()
@@ -65,7 +60,7 @@ class BinaryValuedMacro extends Macro {
6560
BinaryValuedMacro() { this.getBody().regexpMatch("\\(?(0|1)\\)?") }
6661
}
6762

68-
from IfOrElifPreprocessorBranch b, string msg
63+
from PreprocessorIfOrElif b, string msg
6964
where
7065
not isExcluded(b, Preprocessor3Package::controllingExpressionIfDirectiveQuery()) and
7166
isEvaluated(b) and

cpp/autosar/src/rules/M16-1-1/DefinedPreProcessorOperatorGeneratedFromExpansionFound.ql

+1-10
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,9 @@
1414

1515
import cpp
1616
import codingstandards.cpp.autosar
17+
import codingstandards.cpp.PreprocessorDirective
1718
import DefinedMacro
1819

19-
/**
20-
* An `if` or `elif` preprocessor branch.
21-
*/
22-
class PreprocessorIfOrElif extends PreprocessorBranch {
23-
PreprocessorIfOrElif() {
24-
this instanceof PreprocessorIf or
25-
this instanceof PreprocessorElif
26-
}
27-
}
28-
2920
from PreprocessorIfOrElif e, MacroUsesDefined m
3021
where
3122
not isExcluded(e, MacrosPackage::definedPreProcessorOperatorInOneOfTheTwoStandardFormsQuery()) and

cpp/autosar/src/rules/M16-1-1/DefinedPreProcessorOperatorInOneOfTheTwoStandardForms.ql

+2-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import cpp
1717
import codingstandards.cpp.autosar
18+
import codingstandards.cpp.PreprocessorDirective
1819

1920
//get what comes after each 'defined' used with or without parenth
2021
string matchesDefinedOperator(PreprocessorBranch e) {
@@ -34,12 +35,8 @@ string matchesDefinedOperator(PreprocessorBranch e) {
3435
)
3536
}
3637

37-
from PreprocessorBranch e, string arg
38+
from PreprocessorIfOrElif e, string arg
3839
where
39-
(
40-
e instanceof PreprocessorIf or
41-
e instanceof PreprocessorElif
42-
) and
4340
arg = matchesDefinedOperator(e) and
4441
not arg.regexpMatch("^\\w*$") and
4542
not isExcluded(e, MacrosPackage::definedPreProcessorOperatorInOneOfTheTwoStandardFormsQuery())

cpp/common/src/codingstandards/cpp/PreprocessorDirective.qll

+10
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,13 @@ PreprocessorDirective isLocatedInAMacroInvocation(MacroInvocation m) {
3030
result = p
3131
)
3232
}
33+
34+
/**
35+
* An `if` or `elif` preprocessor branch.
36+
*/
37+
class PreprocessorIfOrElif extends PreprocessorBranch {
38+
PreprocessorIfOrElif() {
39+
this instanceof PreprocessorIf or
40+
this instanceof PreprocessorElif
41+
}
42+
}

cpp/common/src/codingstandards/cpp/rules/undefinedmacroidentifiers/UndefinedMacroIdentifiers.qll

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cpp
22
import codingstandards.cpp.Exclusions
3+
import codingstandards.cpp.PreprocessorDirective
34

45
abstract class UndefinedMacroIdentifiersSharedQuery extends Query { }
56

@@ -76,17 +77,10 @@ string getAnIfDefdMacroIdentifier(PreprocessorBranch pb) {
7677
)
7778
}
7879

79-
class IfAndElifs extends PreprocessorBranch {
80-
IfAndElifs() {
81-
this instanceof PreprocessorIf or
82-
this instanceof PreprocessorElif
83-
}
84-
}
85-
86-
class BadIfAndElifs extends IfAndElifs {
80+
class UndefinedIdIfOrElif extends PreprocessorIfOrElif {
8781
string undefinedMacroIdentifier;
8882

89-
BadIfAndElifs() {
83+
UndefinedIdIfOrElif() {
9084
exists(string defRM |
9185
defRM =
9286
this.getHead()
@@ -113,7 +107,7 @@ class BadIfAndElifs extends IfAndElifs {
113107
string getAnUndefinedMacroIdentifier() { result = undefinedMacroIdentifier }
114108
}
115109

116-
query predicate problems(BadIfAndElifs b, string message) {
110+
query predicate problems(UndefinedIdIfOrElif b, string message) {
117111
not isExcluded(b, getQuery()) and
118112
message =
119113
"#if/#elif that uses a macro identifier " + b.getAnUndefinedMacroIdentifier() +

0 commit comments

Comments
 (0)