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

C++: Initial telemetry queries #17892

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

C++: Initial telemetry queries #17892

wants to merge 8 commits into from

Conversation

calumgrant
Copy link
Contributor

@calumgrant calumgrant commented Nov 1, 2024

This adds new internal telemetry queries, so should not need a changenote.

It loosely follows the examples in https://github.com/github/codeql/tree/main/java/ql/src/Telemetry and https://github.com/github/codeql/tree/main/csharp/ql/src/Telemetry.

As a new idea, Metrics.qll uses QL classes a bit more heavily, instead of binary predicates, but obviously they boil down to the same thing.

We don't (yet) emit a diagnostic warning about low quality databases.

Note that qltest can't test for missing includes, since these lead to a catastrophic error by default.

Fixes https://github.com/github/codeql-c-team/issues/2471

Pull Request checklist

All query authors

Internal query authors only

  • [ ] Autofixes generated based on these changes are valid, only needed if this PR makes significant changes to .ql, .qll, or .qhelp files. See the documentation (internal access required).
  • Changes are validated at scale (internal access required).
  • [ ] Adding a new query? Consider also adding the query to autofix.

@calumgrant calumgrant marked this pull request as ready for review November 4, 2024 09:28
@calumgrant calumgrant requested a review from a team as a code owner November 4, 2024 09:28
Copy link
Contributor

@jketema jketema left a comment

Choose a reason for hiding this comment

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

Is there are specific reason we're not re-using some of the functionality already present in cpp/ql/src/Diagnostics?

| identifier 'nsf2' is undefined | 1.0 |
| identifier 'so_is_this' is undefined | 1.0 |
| identifier 'uint32_t' is undefined | 1.0 |
| too few arguments in function call | 1.0 |
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are the values here floats? It seems like these are just discrete counts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's because all metrics are floats in general. But it might be worth specialising to int-metrics and float-metrics.

Copy link
Contributor

Choose a reason for hiding this comment

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

That seems a very bad design.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is 841c678 any better?

@calumgrant
Copy link
Contributor Author

Is there are specific reason we're not re-using some of the functionality already present in cpp/ql/src/Diagnostics?

The code in cpp/ql/src/Diagnostics isn't really that helpful, mainly because it's in the wrong format and has @kind diagnostic.

@jketema
Copy link
Contributor

jketema commented Nov 4, 2024

Is there are specific reason we're not re-using some of the functionality already present in cpp/ql/src/Diagnostics?

The code in cpp/ql/src/Diagnostics isn't really that helpful, mainly because it's in the wrong format and has @kind diagnostic.

Not even the libraries in that directory are of use?

Copy link
Contributor

@jketema jketema left a comment

Choose a reason for hiding this comment

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

Some questions about the used tags.

* Typically this is due to a missing include.
*/
class CannotOpenFile extends CompilerError {
CannotOpenFile() { this.hasTag("cannot_open_file") }
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this also cover cannot_open_file_reason?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will have a look at these tags 👍

* Currently unused.
*/
class UndefinedIdentifier extends CompilerError {
UndefinedIdentifier() { this.hasTag("undefined_identifier") }
Copy link
Contributor

Choose a reason for hiding this comment

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

There are a number of other ..._undefined_identifier tags, shouldn't those be covered too?

Copy link
Contributor Author

@calumgrant calumgrant Nov 4, 2024

Choose a reason for hiding this comment

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

I've had a search through the front end source code, and the other ec_undefined_ errors are all at warning or remark level (e.g. ec_undefined_preproc_id)

Copy link
Contributor

Choose a reason for hiding this comment

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

What about ec_range_based_for_undefined_identifier and ec_for_each_undefined_identifier?

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, I think I'll just delete UndefinedIdentifier as it's unused and not worth creating a test for.

* A syntax error.
*/
class SyntaxError extends CompilerError {
SyntaxError() { this.getTag().matches("exp_%") }
Copy link
Contributor

Choose a reason for hiding this comment

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

Does exp_ cover all possible syntax errors?

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, no. Searching for syntax_error(ec_ reveals various other syntax errors that I will add.

@calumgrant
Copy link
Contributor Author

Is there are specific reason we're not re-using some of the functionality already present in cpp/ql/src/Diagnostics?

The code in cpp/ql/src/Diagnostics isn't really that helpful, mainly because it's in the wrong format and has @kind diagnostic.

Not even the libraries in that directory are of use?

Well, telemetry is a bit simpler because it's a question of dumping counts and raw data, so we don't need to refine them in the same way.

@calumgrant calumgrant added the no-change-note-required This PR does not need a change note label Nov 4, 2024
Comment on lines 11 to 12
from CppMetrics::MissingIncludeCount e
select e.getIncludeText(), e.getValue()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit concerned that this might completely blow up on real-world projects to such an extent that the SARIF will be rejected when uploaded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I've limited the results to the top 500 - not sure if that's a sensible number or not.

Comment on lines 11 to 12
from CppMetrics::ErrorCount m
select m.toString(), m.getValue()
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit concerned that this might completely blow up on real-world projects to such an extent that the SARIF will be rejected when uploaded. Especially because the error messages can in principle be several lines long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++ no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants