-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Implementation of standard C/C++ fixed width, minimum width, and maximum width types #6052
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Most of this looks good to me. I've requested a few changes to the doc comments, and you'll also need to autoformat your QL files and fix the failing test before we can merge this.
@@ -63,13 +63,258 @@ class Ptrdiff_t extends Type { | |||
override string getAPrimaryQlClass() { result = "Ptrdiff_t" } | |||
} | |||
|
|||
/** | |||
* A common base type for describing the C/C++ fixed-width numeric types |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When possible, qldoc should describe the elements in the target language rather than the QL class.
* A common base type for describing the C/C++ fixed-width numeric types | |
* A C/C++ fixed-width numeric type, such as `int8_t` |
} | ||
|
||
/** | ||
* A common base type for describing the C/C++ minimum-width numeric types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* A common base type for describing the C/C++ minimum-width numeric types. | |
* A C/C++ minimum-width numeric type, such as `int_least8_t` or `uint_fast16_t`. |
} | ||
|
||
/** | ||
* A common base type for describing the C/C++ maximum-width numeric types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* A common base type for describing the C/C++ maximum-width numeric types. | |
* A C/C++ maximum-width numeric type, either `intmax_t` or `uintmax_t`. |
} | ||
|
||
/** | ||
* A common base type for describing enum types that are based on fixed-width types. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* A common base type for describing enum types that are based on fixed-width types. | |
* An enum type based on a fixed-width integer type. For instance, `enum e: uint8_t = { a, b };` |
@@ -80,3 +80,6 @@ size_t st; | |||
ssize_t sst; | |||
ptrdiff_t pdt; | |||
wchar_t wct; | |||
|
|||
std::int8_t i8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'll need to declare std::int8_t
here so the test can be extracted properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rdmarsh2 -- ack, this was a mistake; that definition isn't supposed to be there.
/** | ||
* A common base type for describing the C/C++ fixed-width numeric types | ||
*/ | ||
abstract class FixedWidthIntegralType extends UserType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've made a big push to avoid public abstract classes in the base libraries, including this file. See https://github.com/github/codeql-c-team/issues/60 for some motivation and context. Abstract classes are great for the library-internal implementation but painful and dangerous to work with for consumers of the library. For code in the base libraries, the balance is toward reducing surprises for the library's consumers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What approach do you recommend here? This class won't behave correctly in its current form without abstract
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest going with a union approach similar to what we've done here: #3940
i.e., make a TFixedWidthIntegralType
that's a union of all the classes that currently implement the abstract class, and then make a FixedWidthIntegralType
class that extends TFixedWidthIntegralType
(and UserType
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jbj @geoffw0 @MathiasVP -- Please see the latest push to this PR which incorporates a bunch of suggestions from @jbj for resolving this issue. Please let me know so we can come to a consensus.
Also, I'm failing the changelog check but it doesn't look like it's been updated in a while. Shall I create a new directory and note these changes (if so, what directory/version to add?) or omit the changelog entry?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reading @MathiasVP -- I'll update and push again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've changed (🥁) the way we do change-notes (which is why that directory hasn't been updated in a while). See an example here on how we do it now (specifically, the file cpp/change-notes/2021-10-05-comparison-with-wider-type.md
created by that PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @MathiasVP!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo a typo fix
class FixedWidthIntegralType extends TFixedWidthIntegralType { | ||
FixedWidthIntegralType() { this instanceof TFixedWidthIntegralType } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worked!? I'm baffled. Did you know this would work, @MathiasVP? It seems like we can derive from an abstract class without extending it by using extends T
and this instanceof T
in combination (without abstract
it would be redundant).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is Pavel's "enshrinkening subtyping" pattern, right? It seems to fit this use-case very well, indeed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha. For the record, this pattern was discussed in https://github.slack.com/archives/CP0LHP150/p1606300178023900 (and I was apparently in the discussion).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ever-talented Mr. @lcartey showed me this pattern in a coding session where we were examining all of the options for representing this (and also the history of why we avoid it). We compared it against some other approaches, for example, one based around ADTs, but in the end this one seems to strike a nice balance between convenience and maintainability.
Co-authored-by: Jonas Jensen <jbj@github.com>
The tests are running as I'm writing this, but I believe earlier there was an autoformat error from "CPP Language Tests 2 (GitHub Actions)". Also, the QLDoc Checks are asking you to move (no need to copy) the QLDoc from the private abstract classes to the public ones. |
Implementation of standard C/C++ fixed width, minimum width, and maximum width types. Also some modification to the way intmax_t and uintmax_t are organized.