Skip to content

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

Merged
merged 13 commits into from
Jun 11, 2021

Conversation

jsinglet
Copy link
Contributor

@jsinglet jsinglet commented Jun 9, 2021

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.

@jsinglet jsinglet requested a review from a team as a code owner June 9, 2021 20:18
@github-actions github-actions bot added the C++ label Jun 9, 2021
Copy link
Contributor

@rdmarsh2 rdmarsh2 left a 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
Copy link
Contributor

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.

Suggested change
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* 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;
Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

@MathiasVP MathiasVP Jun 10, 2021

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).

Copy link
Contributor Author

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @MathiasVP!

jbj
jbj previously approved these changes Jun 11, 2021
Copy link
Contributor

@jbj jbj left a 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

Comment on lines +78 to +79
class FixedWidthIntegralType extends TFixedWidthIntegralType {
FixedWidthIntegralType() { this instanceof TFixedWidthIntegralType }
Copy link
Contributor

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).

Copy link
Contributor

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.

Copy link
Contributor

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).

Copy link
Contributor Author

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>
@jbj
Copy link
Contributor

jbj commented Jun 11, 2021

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.

@jbj jbj merged commit e23b88b into github:main Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants