Skip to content

Commit 134137f

Browse files
committed
C++: Fix type errors in C code.
1 parent 1828970 commit 134137f

File tree

3 files changed

+84
-17
lines changed

3 files changed

+84
-17
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ newtype TInstructionTag =
9696
exists(Expr e | exists(e.getImplicitDestructorCall(index))) or
9797
exists(Stmt s | exists(s.getImplicitDestructorCall(index)))
9898
} or
99-
CoAwaitBranchTag()
99+
CoAwaitBranchTag() or
100+
BoolToIntConversionTag()
100101

101102
class InstructionTag extends TInstructionTag {
102103
final string toString() { result = getInstructionTagId(this) }
@@ -286,4 +287,6 @@ string getInstructionTagId(TInstructionTag tag) {
286287
)
287288
or
288289
tag = CoAwaitBranchTag() and result = "CoAwaitBranch"
290+
or
291+
tag = BoolToIntConversionTag() and result = "BoolToIntConversion"
289292
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,39 @@ predicate hasTranslatedSyntheticTemporaryObject(Expr expr) {
509509
not expr.hasLValueToRValueConversion()
510510
}
511511

512+
Opcode comparisonOpcode(ComparisonOperation expr) {
513+
expr instanceof EQExpr and result instanceof Opcode::CompareEQ
514+
or
515+
expr instanceof NEExpr and result instanceof Opcode::CompareNE
516+
or
517+
expr instanceof LTExpr and result instanceof Opcode::CompareLT
518+
or
519+
expr instanceof GTExpr and result instanceof Opcode::CompareGT
520+
or
521+
expr instanceof LEExpr and result instanceof Opcode::CompareLE
522+
or
523+
expr instanceof GEExpr and result instanceof Opcode::CompareGE
524+
}
525+
526+
private predicate parentExpectsBool(Expr child) {
527+
any(NotExpr notExpr).getOperand() = child
528+
or
529+
usedAsCondition(child)
530+
}
531+
532+
/**
533+
* Holds if `expr` should have a `TranslatedSyntheticBoolToIntConversion` on it.
534+
*/
535+
predicate hasTranslatedSyntheticBoolToIntConversion(Expr expr) {
536+
not parentExpectsBool(expr) and
537+
expr.getUnspecifiedType() instanceof IntType and
538+
(
539+
expr instanceof NotExpr
540+
or
541+
exists(comparisonOpcode(expr))
542+
)
543+
}
544+
512545
class StaticInitializedStaticLocalVariable extends StaticLocalVariable {
513546
StaticInitializedStaticLocalVariable() {
514547
this.hasInitializer() and
@@ -647,6 +680,9 @@ newtype TTranslatedElement =
647680
// A temporary object that we had to synthesize ourselves, so that we could do a field access or
648681
// method call on a prvalue.
649682
TTranslatedSyntheticTemporaryObject(Expr expr) { hasTranslatedSyntheticTemporaryObject(expr) } or
683+
TTranslatedSyntheticBoolToIntConversion(Expr expr) {
684+
hasTranslatedSyntheticBoolToIntConversion(expr)
685+
} or
650686
// For expressions that would not otherwise generate an instruction.
651687
TTranslatedResultCopy(Expr expr) {
652688
not ignoreExpr(expr) and

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

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,8 @@ abstract class TranslatedCoreExpr extends TranslatedExpr {
216216
not hasTranslatedLoad(expr) and
217217
not hasTranslatedSyntheticTemporaryObject(expr) and
218218
// If there's a result copy, then this expression's result is the copy.
219-
not exprNeedsCopyIfNotLoaded(expr)
219+
not exprNeedsCopyIfNotLoaded(expr) and
220+
not hasTranslatedSyntheticBoolToIntConversion(expr)
220221
}
221222
}
222223

@@ -358,11 +359,12 @@ class TranslatedConditionValue extends TranslatedCoreExpr, ConditionContext,
358359
}
359360

360361
/**
361-
* The IR translation of a node synthesized to adjust the value category of its operand.
362+
* The IR translation of a node synthesized to adjust the value category or type of its operand.
362363
* One of:
363364
* - `TranslatedLoad` - Convert from glvalue to prvalue by loading from the location.
364365
* - `TranslatedSyntheticTemporaryObject` - Convert from prvalue to glvalue by storing to a
365366
* temporary variable.
367+
* - `TranslatedSyntheticBoolToIntConversion` - Convert a prvalue Boolean to a prvalue integer.
366368
*/
367369
abstract class TranslatedValueCategoryAdjustment extends TranslatedExpr {
368370
final override Instruction getFirstInstruction(EdgeKind kind) {
@@ -513,6 +515,46 @@ class TranslatedSyntheticTemporaryObject extends TranslatedValueCategoryAdjustme
513515
}
514516
}
515517

518+
class TranslatedSyntheticBoolToIntConversion extends TranslatedValueCategoryAdjustment,
519+
TTranslatedSyntheticBoolToIntConversion
520+
{
521+
TranslatedSyntheticBoolToIntConversion() { this = TTranslatedSyntheticBoolToIntConversion(expr) }
522+
523+
override string toString() { result = "Bool-to-int conversion of " + expr.toString() }
524+
525+
override predicate hasInstruction(Opcode opcode, InstructionTag tag, CppType resultType) {
526+
opcode instanceof Opcode::Convert and
527+
tag = BoolToIntConversionTag() and
528+
resultType = getIntType()
529+
}
530+
531+
override predicate isResultGLValue() { none() }
532+
533+
override Instruction getInstructionSuccessorInternal(InstructionTag tag, EdgeKind kind) {
534+
tag = BoolToIntConversionTag() and
535+
kind instanceof GotoEdge and
536+
result = this.getParent().getChildSuccessor(this, kind)
537+
}
538+
539+
override Instruction getALastInstructionInternal() {
540+
result = this.getInstruction(BoolToIntConversionTag())
541+
}
542+
543+
override Instruction getChildSuccessorInternal(TranslatedElement child, EdgeKind kind) {
544+
child = this.getOperand() and
545+
result = this.getInstruction(BoolToIntConversionTag()) and
546+
kind instanceof GotoEdge
547+
}
548+
549+
override Instruction getResult() { result = this.getInstruction(BoolToIntConversionTag()) }
550+
551+
override Instruction getInstructionRegisterOperand(InstructionTag tag, OperandTag operandTag) {
552+
tag = BoolToIntConversionTag() and
553+
operandTag instanceof UnaryOperandTag and
554+
result = this.getOperand().getResult()
555+
}
556+
}
557+
516558
/**
517559
* IR translation of an expression that simply returns its result. We generate an otherwise useless
518560
* `CopyValue` instruction for these expressions so that there is at least one instruction
@@ -1794,20 +1836,6 @@ private Opcode binaryArithmeticOpcode(BinaryArithmeticOperation expr) {
17941836
expr instanceof PointerDiffExpr and result instanceof Opcode::PointerDiff
17951837
}
17961838

1797-
private Opcode comparisonOpcode(ComparisonOperation expr) {
1798-
expr instanceof EQExpr and result instanceof Opcode::CompareEQ
1799-
or
1800-
expr instanceof NEExpr and result instanceof Opcode::CompareNE
1801-
or
1802-
expr instanceof LTExpr and result instanceof Opcode::CompareLT
1803-
or
1804-
expr instanceof GTExpr and result instanceof Opcode::CompareGT
1805-
or
1806-
expr instanceof LEExpr and result instanceof Opcode::CompareLE
1807-
or
1808-
expr instanceof GEExpr and result instanceof Opcode::CompareGE
1809-
}
1810-
18111839
private Opcode spaceShipOpcode(SpaceshipExpr expr) {
18121840
exists(expr) and
18131841
result instanceof Opcode::Spaceship

0 commit comments

Comments
 (0)