simplify expressions that could be type-casts

This commit is contained in:
Erik Krogh Kristensen
2022-01-20 10:41:35 +01:00
parent 547f492be0
commit 4e8e3a7420
70 changed files with 123 additions and 143 deletions

View File

@@ -206,9 +206,7 @@ class Class extends UserType {
* it is callable by a particular caller. For C++11, there's also a question
* of whether to include members that are defaulted or deleted.
*/
deprecated predicate hasCopyConstructor() {
exists(CopyConstructor cc | cc = this.getAMemberFunction())
}
deprecated predicate hasCopyConstructor() { this.getAMemberFunction() instanceof CopyConstructor }
/**
* Holds if this class has a copy assignment operator that is either
@@ -224,7 +222,7 @@ class Class extends UserType {
* or deleted.
*/
deprecated predicate hasCopyAssignmentOperator() {
exists(CopyAssignmentOperator coa | coa = this.getAMemberFunction())
this.getAMemberFunction() instanceof CopyAssignmentOperator
}
/**
@@ -887,7 +885,7 @@ class NestedClass extends Class {
* pure virtual function.
*/
class AbstractClass extends Class {
AbstractClass() { exists(PureVirtualFunction f | this.getAMemberFunction() = f) }
AbstractClass() { this.getAMemberFunction() instanceof PureVirtualFunction }
override string getAPrimaryQlClass() { result = "AbstractClass" }
}

View File

@@ -286,13 +286,13 @@ class AttributeArgument extends Element, @attribute_arg {
override Location getLocation() { attribute_args(underlyingElement(this), _, _, _, result) }
override string toString() {
if exists(@attribute_arg_empty self | self = underlyingElement(this))
if underlyingElement(this) instanceof @attribute_arg_empty
then result = "empty argument"
else
exists(string prefix, string tail |
(if exists(this.getName()) then prefix = this.getName() + "=" else prefix = "") and
(
if exists(@attribute_arg_type self | self = underlyingElement(this))
if underlyingElement(this) instanceof @attribute_arg_type
then tail = this.getValueType().getName()
else tail = this.getValueText()
) and

View File

@@ -233,7 +233,7 @@ class XMLElement extends @xmlelement, XMLParent, XMLLocatable {
XMLAttribute getAttribute(string name) { result.getElement() = this and result.getName() = name }
/** Holds if this XML element has an attribute with the specified `name`. */
predicate hasAttribute(string name) { exists(XMLAttribute a | a = this.getAttribute(name)) }
predicate hasAttribute(string name) { exists(this.getAttribute(name)) }
/** Gets the value of the attribute with the specified `name`, if any. */
string getAttributeValue(string name) { result = this.getAttribute(name).getValue() }

View File

@@ -29,7 +29,7 @@ class GuardCondition extends Expr {
exists(IRGuardCondition ir | this = ir.getUnconvertedResultExpression())
or
// no binary operators in the IR
exists(GuardCondition gc | this.(BinaryLogicalOperation).getAnOperand() = gc)
this.(BinaryLogicalOperation).getAnOperand() instanceof GuardCondition
or
// the IR short-circuits if(!x)
// don't produce a guard condition for `y = !x` and other non-short-circuited cases
@@ -98,7 +98,7 @@ class GuardCondition extends Expr {
*/
private class GuardConditionFromBinaryLogicalOperator extends GuardCondition {
GuardConditionFromBinaryLogicalOperator() {
exists(GuardCondition gc | this.(BinaryLogicalOperation).getAnOperand() = gc)
this.(BinaryLogicalOperation).getAnOperand() instanceof GuardCondition
}
override predicate controls(BasicBlock controlled, boolean testIsTrue) {

View File

@@ -48,7 +48,7 @@ private class Argument extends Expr {
*/
class ArgumentNode extends Node {
ArgumentNode() {
exists(Argument arg | this.asExpr() = arg) or
this.asExpr() instanceof Argument or
this = getInstanceArgument(_)
}

View File

@@ -84,8 +84,8 @@ class VariableAccess extends Access, @varaccess {
exists(Assignment a | a.getLValue() = this) or
exists(CrementOperation c | c.getOperand() = this) or
exists(AddressOfExpr addof | addof.getOperand() = this) or
exists(ReferenceToExpr rte | this.getConversion() = rte) or
exists(ArrayToPointerConversion atpc | this.getConversion() = atpc)
this.getConversion() instanceof ReferenceToExpr or
this.getConversion() instanceof ArrayToPointerConversion
}
/**
@@ -104,8 +104,8 @@ class VariableAccess extends Access, @varaccess {
predicate isRValue() {
not exists(AssignExpr ae | ae.getLValue() = this) and
not exists(AddressOfExpr addof | addof.getOperand() = this) and
not exists(ReferenceToExpr rte | this.getConversion() = rte) and
not exists(ArrayToPointerConversion atpc | this.getConversion() = atpc)
not this.getConversion() instanceof ReferenceToExpr and
not this.getConversion() instanceof ArrayToPointerConversion
}
/**
@@ -218,9 +218,7 @@ class PointerFieldAccess extends FieldAccess {
class DotFieldAccess extends FieldAccess {
override string getAPrimaryQlClass() { result = "DotFieldAccess" }
DotFieldAccess() {
exists(Class c | c = this.getQualifier().getFullyConverted().getUnspecifiedType())
}
DotFieldAccess() { this.getQualifier().getFullyConverted().getUnspecifiedType() instanceof Class }
}
/**

View File

@@ -35,7 +35,7 @@ class Call extends Expr, NameQualifiableElement, TCall {
*
* For example, `ptr->f()` has a qualifier, whereas plain `f()` does not.
*/
predicate hasQualifier() { exists(Expr e | this.getChild(-1) = e) }
predicate hasQualifier() { exists(this.getChild(-1)) }
/**
* Gets the expression to the left of the function name or function pointer variable name.

View File

@@ -724,7 +724,7 @@ class SizeofOperator extends Expr, @runtime_sizeof {
* ```
*/
class SizeofExprOperator extends SizeofOperator {
SizeofExprOperator() { exists(Expr e | this.getChild(0) = e) }
SizeofExprOperator() { exists(this.getChild(0)) }
override string getAPrimaryQlClass() { result = "SizeofExprOperator" }
@@ -787,7 +787,7 @@ class AlignofOperator extends Expr, @runtime_alignof {
* ```
*/
class AlignofExprOperator extends AlignofOperator {
AlignofExprOperator() { exists(Expr e | this.getChild(0) = e) }
AlignofExprOperator() { exists(this.getChild(0)) }
/**
* Gets the contained expression.

View File

@@ -308,45 +308,45 @@ class MetricClass extends Class {
}
private string getAUsedHalsteadN1Operator() {
exists(CommaExpr e | e = this.getAnEnclosedExpression()) and result = "comma"
this.getAnEnclosedExpression() instanceof CommaExpr and result = "comma"
or
exists(ReferenceToExpr e | e = this.getAnEnclosedExpression()) and result = "refTo"
this.getAnEnclosedExpression() instanceof ReferenceToExpr and result = "refTo"
or
exists(PointerDereferenceExpr e | e = this.getAnEnclosedExpression()) and result = "dereference"
this.getAnEnclosedExpression() instanceof PointerDereferenceExpr and result = "dereference"
or
exists(CStyleCast e | e = this.getAnEnclosedExpression()) and result = "cCast"
this.getAnEnclosedExpression() instanceof CStyleCast and result = "cCast"
or
exists(StaticCast e | e = this.getAnEnclosedExpression()) and result = "staticCast"
this.getAnEnclosedExpression() instanceof StaticCast and result = "staticCast"
or
exists(ConstCast e | e = this.getAnEnclosedExpression()) and result = "constCast"
this.getAnEnclosedExpression() instanceof ConstCast and result = "constCast"
or
exists(ReinterpretCast e | e = this.getAnEnclosedExpression()) and result = "reinterpretCast"
this.getAnEnclosedExpression() instanceof ReinterpretCast and result = "reinterpretCast"
or
exists(DynamicCast e | e = this.getAnEnclosedExpression()) and result = "dynamicCast"
this.getAnEnclosedExpression() instanceof DynamicCast and result = "dynamicCast"
or
exists(SizeofExprOperator e | e = this.getAnEnclosedExpression()) and result = "sizeofExpr"
this.getAnEnclosedExpression() instanceof SizeofExprOperator and result = "sizeofExpr"
or
exists(SizeofTypeOperator e | e = this.getAnEnclosedExpression()) and result = "sizeofType"
this.getAnEnclosedExpression() instanceof SizeofTypeOperator and result = "sizeofType"
or
exists(IfStmt e | e = this.getAnEnclosedStmt()) and result = "ifVal"
this.getAnEnclosedStmt() instanceof IfStmt and result = "ifVal"
or
exists(SwitchStmt e | e = this.getAnEnclosedStmt()) and result = "switchVal"
this.getAnEnclosedStmt() instanceof SwitchStmt and result = "switchVal"
or
exists(ForStmt e | e = this.getAnEnclosedStmt()) and result = "forVal"
this.getAnEnclosedStmt() instanceof ForStmt and result = "forVal"
or
exists(DoStmt e | e = this.getAnEnclosedStmt()) and result = "doVal"
this.getAnEnclosedStmt() instanceof DoStmt and result = "doVal"
or
exists(WhileStmt e | e = this.getAnEnclosedStmt()) and result = "whileVal"
this.getAnEnclosedStmt() instanceof WhileStmt and result = "whileVal"
or
exists(GotoStmt e | e = this.getAnEnclosedStmt()) and result = "gotoVal"
this.getAnEnclosedStmt() instanceof GotoStmt and result = "gotoVal"
or
exists(ContinueStmt e | e = this.getAnEnclosedStmt()) and result = "continueVal"
this.getAnEnclosedStmt() instanceof ContinueStmt and result = "continueVal"
or
exists(BreakStmt e | e = this.getAnEnclosedStmt()) and result = "breakVal"
this.getAnEnclosedStmt() instanceof BreakStmt and result = "breakVal"
or
exists(ReturnStmt e | e = this.getAnEnclosedStmt()) and result = "returnVal"
this.getAnEnclosedStmt() instanceof ReturnStmt and result = "returnVal"
or
exists(SwitchCase e | e = this.getAnEnclosedStmt()) and result = "caseVal"
this.getAnEnclosedStmt() instanceof SwitchCase and result = "caseVal"
or
exists(IfStmt s | s = this.getAnEnclosedStmt() and s.hasElse()) and
result = "elseVal"

View File

@@ -397,7 +397,7 @@ class PaddedType extends Class {
// Support only single inheritance for now. If multiple inheritance is
// supported, be sure to fix up the calls to getABaseClass*() to correctly
// handle the presence of multiple base class subojects with the same type.
not exists(ClassDerivation cd | cd = this.getDerivation(1))
not exists(this.getDerivation(1))
}
/**

View File

@@ -72,7 +72,7 @@ predicate lvalue(Element e) {
or
exists(Cast c | lvalue(c) and e.(Expr).getConversion() = c)
or
exists(ReferenceToExpr toref | e.(Expr).getConversion() = toref)
e.(Expr).getConversion() instanceof ReferenceToExpr
or
// If f is a function-pointer, then the following two
// calls are equivalent: f() and (*f)()

View File

@@ -258,7 +258,7 @@ private predicate insideFunctionValueMoveTo(Element src, Element dest) {
format.getConversionChar(sourceArg - ffc.getTarget().getNumberOfParameters()) = ["s", "S"]
)
or
not exists(FormatLiteral fl | fl = c.(FormattingFunctionCall).getFormat())
not c.(FormattingFunctionCall).getFormat() instanceof FormatLiteral
or
not c instanceof FormattingFunctionCall
) and

View File

@@ -271,7 +271,7 @@ class IfStmt extends ConditionalStmt, @stmt_if {
* if (b) { x = 1; }
* ```
*/
predicate hasElse() { exists(Stmt s | this.getElse() = s) }
predicate hasElse() { exists(this.getElse()) }
override string toString() { result = "if (...) ... " }
@@ -357,7 +357,7 @@ class ConstexprIfStmt extends ConditionalStmt, @stmt_constexpr_if {
* if constexpr (b) { x = 1; }
* ```
*/
predicate hasElse() { exists(Stmt s | this.getElse() = s) }
predicate hasElse() { exists(this.getElse()) }
override string toString() { result = "if constexpr (...) ... " }

View File

@@ -30,8 +30,8 @@ where
// the next statement isn't breaking out of a switch
not s.(BreakStmt).getBreakable() instanceof SwitchStmt and
// the next statement isn't a loop that can be jumped into
not exists(LabelStmt ls | s.(Loop).getStmt().getAChild*() = ls) and
not exists(SwitchCase sc | s.(Loop).getStmt().getAChild*() = sc) and
not s.(Loop).getStmt().getAChild*() instanceof LabelStmt and
not s.(Loop).getStmt().getAChild*() instanceof SwitchCase and
// no preprocessor logic applies
not functionContainsPreprocCode(js.getEnclosingFunction())
select js, "This statement makes $@ unreachable.", s, s.toString()

View File

@@ -55,7 +55,7 @@ abstract class LeapYearFieldAccess extends YearFieldAccess {
op.getAnOperand() = this and
(
op instanceof AssignArithmeticOperation or
exists(BinaryArithmeticOperation bao | bao = op.getAnOperand()) or
op.getAnOperand() instanceof BinaryArithmeticOperation or
op instanceof CrementOperation
)
)
@@ -212,9 +212,7 @@ class ChecksForLeapYearFunctionCall extends FunctionCall {
class LeapYearCheckConfiguration extends DataFlow::Configuration {
LeapYearCheckConfiguration() { this = "LeapYearCheckConfiguration" }
override predicate isSource(DataFlow::Node source) {
exists(VariableAccess va | va = source.asExpr())
}
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof VariableAccess }
override predicate isSink(DataFlow::Node sink) {
exists(ChecksForLeapYearFunctionCall fc | sink.asExpr() = fc.getAnArgument())

View File

@@ -34,9 +34,7 @@ class SetSecurityDescriptorDaclFunctionCall extends FunctionCall {
class NullDaclConfig extends DataFlow::Configuration {
NullDaclConfig() { this = "NullDaclConfig" }
override predicate isSource(DataFlow::Node source) {
exists(NullValue nullExpr | source.asExpr() = nullExpr)
}
override predicate isSource(DataFlow::Node source) { source.asExpr() instanceof NullValue }
override predicate isSink(DataFlow::Node sink) {
exists(SetSecurityDescriptorDaclFunctionCall call, VariableAccess val | val = sink.asExpr() |

View File

@@ -24,10 +24,10 @@ class CallUsedToHandleErrors extends FunctionCall {
not exists(this.(ControlFlowNode).getASuccessor())
or
// call throwing an exception
exists(ThrowExpr tex | tex = this.(ControlFlowNode).getASuccessor())
this.(ControlFlowNode).getASuccessor() instanceof ThrowExpr
or
// call logging a message, possibly an error
exists(FormattingFunction ff | ff = this.(ControlFlowNode).getASuccessor())
this.(ControlFlowNode).getASuccessor() instanceof FormattingFunction
or
// enabling recursive search
exists(CallUsedToHandleErrors fr | getTarget() = fr.getEnclosingFunction())
@@ -37,9 +37,9 @@ class CallUsedToHandleErrors extends FunctionCall {
/** Holds if the conditions for a call outside the wrapper function are met. */
predicate conditionsOutsideWrapper(FunctionCall fcp) {
fcp.getNumberOfArguments() > 0 and
not exists(ConditionalStmt cdtmp | fcp.getEnclosingStmt().getParentStmt*() = cdtmp) and
not exists(Loop lptmp | fcp.getEnclosingStmt().getParentStmt*() = lptmp) and
not exists(ReturnStmt rttmp | fcp.getEnclosingStmt().getParentStmt*() = rttmp) and
not fcp.getEnclosingStmt().getParentStmt*() instanceof ConditionalStmt and
not fcp.getEnclosingStmt().getParentStmt*() instanceof Loop and
not fcp.getEnclosingStmt().getParentStmt*() instanceof ReturnStmt and
not exists(FunctionCall fctmp2 | fcp = fctmp2.getAnArgument().getAChild*()) and
not exists(Assignment astmp | fcp = astmp.getRValue().getAChild*()) and
not exists(Initializer intmp | fcp = intmp.getExpr().getAChild*()) and

View File

@@ -26,7 +26,7 @@ class CallMayNotReturn extends FunctionCall {
// call to another function that may not return
exists(CallMayNotReturn exit | getTarget() = exit.getEnclosingFunction())
or
exists(ThrowExpr tex | tex = this.(ControlFlowNode).getASuccessor())
this.(ControlFlowNode).getASuccessor() instanceof ThrowExpr
}
}
@@ -127,7 +127,7 @@ predicate similarArguments(FunctionCall fc, FunctionCall fc1) {
from FunctionCall fc, FunctionCall fc1
where
not exists(CallMayNotReturn fctmp | fctmp = fc.getASuccessor*()) and
not fc.getASuccessor*() instanceof CallMayNotReturn and
not exists(IfStmt ifs | ifs.getCondition().getAChild*() = fc) and
(
// detecting a repeated call situation within one function

View File

@@ -15,6 +15,6 @@ from EqualityOperation e, PointerToMemberType t, Class c
where
e.getAnOperand().getType() = t and
t.getClass() = c and
exists(VirtualFunction f | c.getAMemberFunction() = f)
c.getAMemberFunction() instanceof VirtualFunction
select e,
"AV Rule 97.1: Neither operand of an equality operator shall be a pointer to a virtual member function."