Visitar URL original
Ruby: Replace `getValueText` with `getConstantValue` by hvitved · Pull Request #7677 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20251230142805/https://github.com/github/codeql/pull/7677
Skip to content

Conversation

@hvitved
Copy link
Contributor

@hvitved hvitved commented Jan 20, 2022 •

This PR replaces string getValueText() with a typed ConstantValue getConstantValue() predicate. ConstantValue is a union type consisting of integers, floats, rationals, complex numbers, strings, Booleans, and nil.

Existing getValueText overloads have been replaced with appropriate getConstantValue overloads (e.g., IntegerLiteral now returns a ConstantValue::ConstantIntegerValue, as opposed to merely a string). This means that we can implement constant folding for binary operations appropriately, and I have also implemented constant folding for unary operations (+x and -x). As discussed on previous occasions, I have also removed the logic for truncating string concatenations x + y, and now such concatenations will simply have no value if the combined length exceeds 10,000.

@github-actions github-actions bot added the Ruby label Jan 20, 2022
@hvitved hvitved force-pushed the ruby/constant-value branch 6 times, most recently from 945ea10 to a1fa623 Compare January 20, 2022 20:37
@hvitved hvitved force-pushed the ruby/constant-value branch from a1fa623 to aa9cfeb Compare January 21, 2022 08:19
@hvitved hvitved marked this pull request as ready for review January 21, 2022 08:21
@hvitved hvitved requested a review from a team as a code owner January 21, 2022 08:21
@hvitved hvitved added the no-change-note-required This PR does not need a change note label Jan 21, 2022
@hvitved hvitved force-pushed the ruby/constant-value branch from bc0807e to 85e1cda Compare January 21, 2022 18:16
real = 0 and
imaginary = getComplexValue(_)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't appear to support complex numbers like 1+2i or 3-4i. Is it worth adding that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would have to be done in BinaryOperationCfgNode. We could add constant folding for complex and rational numbers there, but I think that should be done as follow-up work.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless I'm missing something, I don't think so? 1+2i is parsed as a single ComplexLiteral value, not an application of + to two separate literals.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Totally happy if we do this as follow-up work, but I think this is the right part of the code to be changing)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 Let's do it as follow-up.

Copy link
Contributor

@hmac hmac left a comment

Choose a reason for hiding this comment

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

This looks great - I just had a few questions on some edge cases.

| literals/literals.rb:13:1:13:5 | 0d900 | 0 |
| literals/literals.rb:16:1:16:6 | 0x1234 | 4660 |
| literals/literals.rb:17:1:17:10 | 0xdeadbeef | -559038737 |
| literals/literals.rb:18:1:18:11 | 0xF00D_face | -267519282 |
Copy link
Contributor

Choose a reason for hiding this comment

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

These values seem to disagree with Ruby:

[1] pry(main)> 0xF00D_face
=> 4027448014
[2] pry(main)> 0xdeadbeef
=> 3735928559

Copy link
Contributor

Choose a reason for hiding this comment

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

Just a guess, is CodeQL's int a signed 32-bit and these values are overflowing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, presumably because Ruby uses 64 bit integers, and QL uses 32 bits.

Copy link
Contributor

@hmac hmac left a comment

Choose a reason for hiding this comment

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

This is a real nice improvement!

@hvitved hvitved merged commit 0299b46 into github:main Jan 25, 2022
@hvitved hvitved deleted the ruby/constant-value branch January 25, 2022 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-change-note-required This PR does not need a change note Ruby

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants