Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brodes/guard flow parsing k #17933

Closed
wants to merge 16 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions cpp/ql/lib/semmle/code/cpp/controlflow/IRGuards.qll
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ private predicate unary_compares_eq(
// ((test is `areEqual` => op == const + k2) and const == `k1`) =>
// test is `areEqual` => op == k1 + k2
inNonZeroCase = false and
exists(int k1, int k2, ConstantInstruction const |
exists(int k1, int k2, Instruction const |
compares_eq(derived, op, const.getAUse(), k2, areEqual, value) and
int_value(const) = k1 and
k = k1 + k2
Expand Down Expand Up @@ -1059,7 +1059,7 @@ private predicate compares_lt(Instruction test, Operand op, int k, boolean isLt,
compares_lt(derived.(LogicalNotInstruction).getUnary(), op, k, isLt, dual)
)
or
exists(int k1, int k2, ConstantInstruction const |
exists(int k1, int k2, Instruction const |
compares_lt(derived, op, const.getAUse(), k2, isLt, value) and
int_value(const) = k1 and
k = k1 + k2
Expand Down Expand Up @@ -1388,5 +1388,9 @@ private class IntegerOrPointerConstantInstruction extends ConstantInstruction {

/** The int value of integer constant expression. */
private int int_value(Instruction i) {
result = valueNumber(i).getAnInstruction().(IntegerOrPointerConstantInstruction).getValue().toInt()
result =
valueNumber(i).getAnInstruction().(IntegerOrPointerConstantInstruction).getValue().toInt()
or
// handle conversions
result = int_value(valueNumber(i).getAnInstruction().(ConvertInstruction).getUnary())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Watch out with materializing all Instructions when all we really care about is the value number. If we instead change the i parameter to be a ValueNumber instead this predicate will be a lot smaller.

Note that this will require changing all the callers so that they convert the argument to a value number. You can define an (inline) helper predicate to do that conversion for you. This will keep all callers as-is, and still avoid unnecessary materialization

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, but what about inlining int_value instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't inline it since it's recursive, unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah true true. The inline helper function I think is the winner then, which is basically the best of both worlds.

}
Loading