mirror of
https://github.com/github/codeql.git
synced 2026-04-29 10:45:15 +02:00
C++/C#: Refactor some integer constant code
Make `bitsToBytesAndBits` omit the leftover bits if zero.
This commit is contained in:
committed by
Dave Bartolomeo
parent
51ff262cbc
commit
df21835759
@@ -192,3 +192,36 @@ predicate isGT(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a > b }
|
||||
*/
|
||||
bindingset[a, b]
|
||||
predicate isGE(IntValue a, IntValue b) { hasValue(a) and hasValue(b) and a >= b }
|
||||
|
||||
/**
|
||||
* Converts the bit count in `bits` to a byte count and a bit count in the form
|
||||
* "bytes:bits". If `bits` represents an integer number of bytes, the ":bits" section is omitted.
|
||||
* If `bits` does not have a known value, the result is "?".
|
||||
*/
|
||||
bindingset[bits]
|
||||
string bitsToBytesAndBits(IntValue bits) {
|
||||
exists(int bytes, int leftoverBits |
|
||||
hasValue(bits) and
|
||||
bytes = bits / 8 and
|
||||
leftoverBits = bits % 8 and
|
||||
if leftoverBits = 0 then
|
||||
result = bytes.toString()
|
||||
else
|
||||
result = bytes + ":" + leftoverBits
|
||||
) or
|
||||
not hasValue(bits) and result = "?"
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a printable string for a bit offset with possibly unknown value.
|
||||
*/
|
||||
bindingset[bitOffset]
|
||||
string getBitOffsetString(IntValue bitOffset) {
|
||||
if hasValue(bitOffset) then
|
||||
if bitOffset >= 0 then
|
||||
result = "+" + bitsToBytesAndBits(bitOffset)
|
||||
else
|
||||
result = "-" + bitsToBytesAndBits(neg(bitOffset))
|
||||
else
|
||||
result = "+?"
|
||||
}
|
||||
|
||||
@@ -30,5 +30,5 @@ Overlap getOverlap(IntValue defStart, IntValue defEnd, IntValue useStart, IntVal
|
||||
bindingset[start, end]
|
||||
string getIntervalString(IntValue start, IntValue end) {
|
||||
// We represent an interval has half-open, so print it as "[start..end)".
|
||||
result = "[" + intValueToString(start) + ".." + intValueToString(end) + ")"
|
||||
result = "[" + bitsToBytesAndBits(start) + ".." + bitsToBytesAndBits(end) + ")"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user