Skip to content

Commit da20bb8

Browse files
committed
C++: Insert int-to-bool conversions at 'NotExpr's.
1 parent 14adf1a commit da20bb8

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ newtype TInstructionTag =
5151
ConditionValueResultLoadTag() or
5252
BoolConversionConstantTag() or
5353
BoolConversionCompareTag() or
54+
NotExprNotTag() or
55+
NotExprConstantTag() or
56+
NotExprCompareTag() or
5457
ResultCopyTag() or
5558
LoadTag() or // Implicit load due to lvalue-to-rvalue conversion
5659
CatchTag() or
@@ -193,6 +196,12 @@ string getInstructionTagId(TInstructionTag tag) {
193196
or
194197
tag = BoolConversionCompareTag() and result = "BoolConvComp"
195198
or
199+
tag = NotExprNotTag() and result = "NotExprWithBoolConversionNot"
200+
or
201+
tag = NotExprConstantTag() and result = "NotExprWithBoolConversionConstant"
202+
or
203+
tag = NotExprCompareTag() and result = "NotExprWithBoolConversionCompare"
204+
or
196205
tag = ResultCopyTag() and result = "ResultCopy"
197206
or
198207
tag = LoadTag() and result = "Load" // Implicit load due to lvalue-to-rvalue conversion

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

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,46 +1325,85 @@ class TranslatedNotExpr extends TranslatedNonConstantExpr {
13251325

13261326
override Type getExprType() { result instanceof BoolType }
13271327

1328+
private Type getOperandType() { result = this.getOperand().getExprType() }
1329+
13281330
final override Instruction getFirstInstruction(EdgeKind kind) {
13291331
result = this.getOperand().getFirstInstruction(kind)
13301332
}
13311333

13321334
override Instruction getALastInstructionInternal() {
1333-
result = this.getInstruction(OnlyInstructionTag())
1335+
result = this.getInstruction(NotExprNotTag())
13341336
}
13351337

13361338
final override TranslatedElement getChildInternal(int id) {
13371339
id = 0 and result = this.getOperand()
13381340
}
13391341

13401342
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
1341-
tag = OnlyInstructionTag() and
1343+
not this.getOperandType() instanceof BoolType and
1344+
(
1345+
tag = NotExprConstantTag() and
1346+
opcode instanceof Opcode::Constant and
1347+
resultType = getIntType()
1348+
or
1349+
tag = NotExprCompareTag() and
1350+
opcode instanceof Opcode::CompareNE and
1351+
resultType = getBoolType()
1352+
)
1353+
or
1354+
tag = NotExprNotTag() and
13421355
opcode instanceof Opcode::LogicalNot and
13431356
resultType = getBoolType()
13441357
}
13451358

13461359
final override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
1347-
tag = OnlyInstructionTag() and
1360+
tag = NotExprConstantTag() and
1361+
result = this.getInstruction(NotExprCompareTag()) and
1362+
kind instanceof GotoEdge
1363+
or
1364+
tag = NotExprCompareTag() and
1365+
result = this.getInstruction(NotExprNotTag()) and
1366+
kind instanceof GotoEdge
1367+
or
1368+
tag = NotExprNotTag() and
13481369
result = this.getParent().getChildSuccessor(this, kind)
13491370
}
13501371

13511372
final override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
13521373
child = this.getOperand() and
13531374
kind instanceof GotoEdge and
1354-
result = this.getInstruction(OnlyInstructionTag())
1375+
if this.getOperandType() instanceof BoolType
1376+
then result = this.getInstruction(NotExprNotTag())
1377+
else result = this.getInstruction(NotExprConstantTag())
13551378
}
13561379

13571380
final override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
1358-
tag = OnlyInstructionTag() and
1381+
tag = NotExprNotTag() and
13591382
operandTag instanceof UnaryOperandTag and
1360-
result = this.getOperand().getResult()
1383+
if this.getOperandType() instanceof BoolType
1384+
then result = this.getOperand().getResult()
1385+
else result = this.getInstruction(NotExprCompareTag())
1386+
or
1387+
tag = NotExprCompareTag() and
1388+
(
1389+
result = this.getOperand().getResult() and
1390+
operandTag instanceof LeftOperandTag
1391+
or
1392+
result = this.getInstruction(NotExprConstantTag()) and
1393+
operandTag instanceof RightOperandTag
1394+
)
13611395
}
13621396

13631397
private TranslatedExpr getOperand() {
13641398
result = getTranslatedExpr(expr.getOperand().getFullyConverted())
13651399
}
13661400

1367-
final override Instruction getResult() { result = this.getInstruction(OnlyInstructionTag()) }
1401+
final override Instruction getResult() { result = this.getInstruction(NotExprNotTag()) }
1402+
1403+
override string getInstructionConstantValue(InstructionTag tag) {
1404+
tag = NotExprConstantTag() and
1405+
result = "0"
1406+
}
13681407
}
13691408

13701409
/**

0 commit comments

Comments
 (0)