Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc8c0770

Browse files
committed
Separate out helper classes into libraries
1 parenta653b66 commitc8c0770

File tree

3 files changed

+91
-92
lines changed

3 files changed

+91
-92
lines changed

‎cpp/common/src/codingstandards/cpp/Loops.qll‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,34 @@ predicate isInvalidLoop(ForStmt forLoop, string reason, Locatable reasonLocation
374374
reason="its $@ is not a boolean"and
375375
reasonLabel="loop control variable"
376376
}
377+
378+
/**
379+
* A comparison expression that has the minimum qualification as being a valid termination
380+
* condition of a legacy for-loop. It is characterized by a value read from a variable being
381+
* compared to a value, which is supposed to be the loop bound.
382+
*/
383+
classLegacyForLoopConditionextendsRelationalOperation{
384+
/**
385+
* The legacy for-loop this relational operation is a condition of.
386+
*/
387+
ForStmtforLoop;
388+
VariableAccessloopCounter;
389+
ExprloopBound;
390+
391+
LegacyForLoopCondition(){
392+
loopCounter=this.getAnOperand()and
393+
loopBound=this.getAnOperand()and
394+
loopCounter.getTarget()=getAnIterationVariable(forLoop)and
395+
loopBound!=loopCounter
396+
}
397+
398+
/**
399+
* Gets the variable access to the loop counter variable, appearing in this loop condition.
400+
*/
401+
VariableAccessgetLoopCounter(){result=loopCounter}
402+
403+
/**
404+
* Gets the variable access to the loop bound variable, appearing in this loop condition.
405+
*/
406+
ExprgetLoopBound(){result=loopBound}
407+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import cpp
2+
3+
abstractclassLegacyForLoopUpdateExpressionextendsExpr{
4+
ForStmtforLoop;
5+
6+
LegacyForLoopUpdateExpression(){this=forLoop.getUpdate().getAChild*()}
7+
8+
abstractExprgetLoopStep();
9+
/* TODO: Complete below and use it for 3-2 */
10+
// abstract VariableAccess getUpdatedVariable();
11+
}
12+
13+
classCrementLegacyForLoopUpdateExpressionextendsLegacyForLoopUpdateExpression{
14+
CrementLegacyForLoopUpdateExpression(){thisinstanceofCrementOperation}
15+
16+
overrideExprgetLoopStep(){none()}
17+
}
18+
19+
classAssignAddOrSubExprextendsLegacyForLoopUpdateExpression{
20+
AssignAddOrSubExpr(){
21+
thisinstanceofAssignAddExpror
22+
thisinstanceofAssignSubExpr
23+
}
24+
25+
overrideExprgetLoopStep(){
26+
result=this.(AssignAddExpr).getRValue()or
27+
result=this.(AssignSubExpr).getRValue()
28+
}
29+
}
30+
31+
classAddOrSubThenAssignExprextendsLegacyForLoopUpdateExpression{
32+
ExprassignRhs;
33+
34+
AddOrSubThenAssignExpr(){
35+
this.(AssignExpr).getRValue()=assignRhsand
36+
(
37+
assignRhsinstanceofAddExpror
38+
assignRhsinstanceofSubExpr
39+
)
40+
}
41+
42+
overrideExprgetLoopStep(){
43+
(
44+
result=assignRhs.(AddExpr).getAnOperand()or
45+
result=assignRhs.(SubExpr).getAnOperand()
46+
)and
47+
exists(VariableAccessiterationVariableAccess|
48+
(
49+
iterationVariableAccess=assignRhs.(AddExpr).getAnOperand()
50+
or
51+
iterationVariableAccess=assignRhs.(SubExpr).getAnOperand()
52+
)and
53+
iterationVariableAccess.getTarget()=forLoop.getAnIterationVariable()and
54+
result!=iterationVariableAccess
55+
)
56+
}
57+
}

‎cpp/misra/src/rules/RULE-9-5-1/LegacyForStatementsShouldBeSimple.ql‎

Lines changed: 3 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,6 @@ import codingstandards.cpp.Call
2222
import codingstandards.cpp.Loops
2323
import codingstandards.cpp.misra.BuiltInTypeRules::MisraCpp23BuiltInTypes
2424

25-
/**
26-
* A comparison expression that has the minimum qualification as being a valid termination
27-
* condition of a legacy for-loop. It is characterized by a value read from a variable being
28-
* compared to a value, which is supposed to be the loop bound.
29-
*/
30-
classLegacyForLoopConditionextendsRelationalOperation{
31-
/**
32-
* The legacy for-loop this relational operation is a condition of.
33-
*/
34-
ForStmtforLoop;
35-
VariableAccessloopCounter;
36-
ExprloopBound;
37-
38-
LegacyForLoopCondition(){
39-
loopCounter=this.getAnOperand()and
40-
loopBound=this.getAnOperand()and
41-
loopCounter.getTarget()=getAnIterationVariable(forLoop)and
42-
loopBound!=loopCounter
43-
}
44-
45-
/**
46-
* Gets the variable access to the loop counter variable, embedded in this loop condition.
47-
*/
48-
VariableAccessgetLoopCounter(){result=loopCounter}
49-
50-
/**
51-
* Gets the variable access to the loop bound variable, embedded in this loop condition.
52-
*/
53-
ExprgetLoopBound(){result=loopBound}
54-
}
55-
5625
/**
5726
* Holds if the given expression may mutate the variable.
5827
*/
@@ -72,60 +41,6 @@ predicate variableModifiedInExpression(Expr expr, VariableAccess va) {
7241
valueToUpdate(va, _,expr)
7342
}
7443

75-
abstractclassLegacyForLoopUpdateExpressionextendsExpr{
76-
ForStmtforLoop;
77-
78-
LegacyForLoopUpdateExpression(){this=forLoop.getUpdate().getAChild*()}
79-
80-
abstractExprgetLoopStep();
81-
}
82-
83-
classCrementLegacyForLoopUpdateExpressionextendsLegacyForLoopUpdateExpression{
84-
CrementLegacyForLoopUpdateExpression(){thisinstanceofCrementOperation}
85-
86-
overrideExprgetLoopStep(){none()}
87-
}
88-
89-
classAssignAddOrSubExprextendsLegacyForLoopUpdateExpression{
90-
AssignAddOrSubExpr(){
91-
thisinstanceofAssignAddExpror
92-
thisinstanceofAssignSubExpr
93-
}
94-
95-
overrideExprgetLoopStep(){
96-
result=this.(AssignAddExpr).getRValue()or
97-
result=this.(AssignSubExpr).getRValue()
98-
}
99-
}
100-
101-
classAddOrSubThenAssignExprextendsLegacyForLoopUpdateExpression{
102-
ExprassignRhs;
103-
104-
AddOrSubThenAssignExpr(){
105-
this.(AssignExpr).getRValue()=assignRhsand
106-
(
107-
assignRhsinstanceofAddExpror
108-
assignRhsinstanceofSubExpr
109-
)
110-
}
111-
112-
overrideExprgetLoopStep(){
113-
(
114-
result=assignRhs.(AddExpr).getAnOperand()or
115-
result=assignRhs.(SubExpr).getAnOperand()
116-
)and
117-
exists(VariableAccessiterationVariableAccess|
118-
(
119-
iterationVariableAccess=assignRhs.(AddExpr).getAnOperand()
120-
or
121-
iterationVariableAccess=assignRhs.(SubExpr).getAnOperand()
122-
)and
123-
iterationVariableAccess.getTarget()=forLoop.getAnIterationVariable()and
124-
result!=iterationVariableAccess
125-
)
126-
}
127-
}
128-
12944
/**
13045
* Gets the loop step of a legacy for loop.
13146
*
@@ -146,9 +61,7 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
14661
*/
14762

14863
/* 1. Get the expression `E` when the update expression is `i += E` or `i -= E`. */
149-
result=forLoop.getUpdate().getAChild*().(AssignAddExpr).getRValue()
150-
or
151-
result=forLoop.getUpdate().getAChild*().(AssignSubExpr).getRValue()
64+
result=forLoop.getUpdate().getAChild*().(AssignAddOrSubExpr).getLoopStep()
15265
or
15366
/* 2. Get the expression `E` when the update expression is `i = i + E` or `i = i - E`. */
15467
(
@@ -175,13 +88,11 @@ Expr getLoopStepOfForStmt(ForStmt forLoop) {
17588
* 2. Another variable access of the same variable as the given variable access is assigned
17689
* to a non-const reference variable (thus constituting a `T` -> `&T` conversion.), i.e.
17790
* initialization and assignment.
178-
*/
179-
/*
91+
*
18092
* Note that pass-by-reference is dealt with in a different predicate named
18193
* `loopVariablePassedAsArgumentToNonConstReferenceParameter`, due to implementation
18294
* limitations.
18395
*/
184-
18596
predicateloopVariableAssignedToNonConstPointerOrReferenceType(
18697
ForStmtforLoop,VariableAccessloopVariableAccessInCondition
18798
){
@@ -199,7 +110,7 @@ predicate loopVariableAssignedToNonConstPointerOrReferenceType(
199110
assignmentRhs.(AddressOfExpr).getOperand()=
200111
loopVariableAccessInCondition.getTarget().getAnAccess()
201112
or
202-
/* 2.The address is taken: A loop variable access */
113+
/* 2.A reference is taken: A loop variable access */
203114
assignmentRhs=loopVariableAccessInCondition.getTarget().getAnAccess()
204115
)
205116
)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp