Skip to content

Commit dd39b97

Browse files
committed
C++: Insert int-to-bool conversions at conditions.
1 parent 2e3d349 commit dd39b97

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/InstructionTag.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ newtype TInstructionTag =
3838
AllocationSizeTag() or
3939
AllocationElementSizeTag() or
4040
AllocationExtentConvertTag() or
41+
ValueConditionCompareTag() or
42+
ValueConditionConstantTag() or
4143
ValueConditionConditionalBranchTag() or
4244
ConditionValueTrueTempAddressTag() or
4345
ConditionValueTrueConstantTag() or
@@ -167,6 +169,10 @@ string getInstructionTagId(TInstructionTag tag) {
167169
or
168170
tag = ValueConditionConditionalBranchTag() and result = "ValCondCondBranch"
169171
or
172+
tag = ValueConditionCompareTag() and result = "ValCondCondCompare"
173+
or
174+
tag = ValueConditionConstantTag() and result = "ValCondConstant"
175+
or
170176
tag = ConditionValueTrueTempAddressTag() and result = "CondValTrueTempAddr"
171177
or
172178
tag = ConditionValueTrueConstantTag() and result = "CondValTrueConst"

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedCondition.qll

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,49 @@ class TranslatedValueCondition extends TranslatedCondition, TTranslatedValueCond
187187

188188
final override predicate handlesDestructorsExplicitly() { none() } // TODO: this needs to be revisted when we get unnamed destructors
189189

190+
private Type getValueExprType() {
191+
result = this.getValueExpr().getExprType().getUnspecifiedType()
192+
}
193+
194+
predicate shouldGenerateCompareNE() { not this.getValueExprType() instanceof BoolType }
195+
190196
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
197+
this.shouldGenerateCompareNE() and
198+
(
199+
tag = ValueConditionCompareTag() and
200+
opcode instanceof Opcode::CompareNE and
201+
resultType = getBoolType()
202+
or
203+
tag = ValueConditionConstantTag() and
204+
opcode instanceof Opcode::Constant and
205+
resultType = getTypeForPRValue(this.getValueExprType())
206+
)
207+
or
191208
tag = ValueConditionConditionalBranchTag() and
192209
opcode instanceof Opcode::ConditionalBranch and
193210
resultType = getVoidType()
194211
}
195212

196213
override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
197214
child = this.getValueExpr() and
198-
result = this.getInstruction(ValueConditionConditionalBranchTag()) and
199-
kind instanceof GotoEdge
215+
kind instanceof GotoEdge and
216+
if this.shouldGenerateCompareNE()
217+
then result = this.getInstruction(ValueConditionConstantTag())
218+
else result = this.getInstruction(ValueConditionConditionalBranchTag())
200219
}
201220

202221
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
222+
this.shouldGenerateCompareNE() and
223+
(
224+
tag = ValueConditionConstantTag() and
225+
kind instanceof GotoEdge and
226+
result = this.getInstruction(ValueConditionCompareTag())
227+
or
228+
tag = ValueConditionCompareTag() and
229+
kind instanceof GotoEdge and
230+
result = this.getInstruction(ValueConditionConditionalBranchTag())
231+
)
232+
or
203233
tag = ValueConditionConditionalBranchTag() and
204234
(
205235
kind instanceof TrueEdge and
@@ -211,9 +241,26 @@ class TranslatedValueCondition extends TranslatedCondition, TTranslatedValueCond
211241
}
212242

213243
override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
244+
this.shouldGenerateCompareNE() and
245+
tag = ValueConditionCompareTag() and
246+
(
247+
operandTag instanceof LeftOperandTag and
248+
result = this.getValueExpr().getResult()
249+
or
250+
operandTag instanceof RightOperandTag and
251+
result = this.getInstruction(ValueConditionConstantTag())
252+
)
253+
or
214254
tag = ValueConditionConditionalBranchTag() and
215255
operandTag instanceof ConditionOperandTag and
216-
result = this.getValueExpr().getResult()
256+
if this.shouldGenerateCompareNE()
257+
then result = this.getInstruction(ValueConditionCompareTag())
258+
else result = this.getValueExpr().getResult()
259+
}
260+
261+
override string getInstructionConstantValue(InstructionTag tag) {
262+
tag = ValueConditionConstantTag() and
263+
result = "0"
217264
}
218265

219266
private TranslatedExpr getValueExpr() { result = getTranslatedExpr(expr) }

0 commit comments

Comments
 (0)