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

Implement RULE-2-8, project should not contain unused object definitions #784

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

Conversation

MichaelRFairhurst
Copy link
Contributor

Description

Implement RULE-2-8, project should not contain unused object definitions

Also add a new AlertReporting shared query library for deduplicating results across macro definitions/invocations/etc.

Split __attribute__((unused)) variables (and similar) to a Strict pair of queries.

Change request type

  • Release or process automation (GitHub workflows, internal scripts)
  • Internal documentation
  • External documentation
  • Query files (.ql, .qll, .qls or unit tests)
  • External scripts (analysis report or other code shipped as part of a release)

Rules with added or modified queries

  • No rules added
  • Queries have been added for the following rules:
    • RULE-2-8
  • Queries have been modified for the following rules:
    • rule number here

Release change checklist

A change note (development_handbook.md#change-notes) is required for any pull request which modifies:

  • The structure or layout of the release artifacts.
  • The evaluation performance (memory, execution time) of an existing query.
  • The results of an existing query in any circumstance.

If you are only adding new rule queries, a change note is not required.

Author: Is a change note required?

  • Yes
  • No

🚨🚨🚨
Reviewer: Confirm that format of shared queries (not the .qll file, the
.ql file that imports it) is valid by running them within VS Code.

  • Confirmed

Reviewer: Confirm that either a change note is not required or the change note is required and has been added.

  • Confirmed

Query development review checklist

For PRs that add new queries or modify existing queries, the following checklist should be completed by both the author and reviewer:

Author

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

Reviewer

  • Have all the relevant rule package description files been checked in?
  • Have you verified that the metadata properties of each new query is set appropriately?
  • Do all the unit tests contain both "COMPLIANT" and "NON_COMPLIANT" cases?
  • Are the alert messages properly formatted and consistent with the style guide?
  • Have you run the queries on OpenPilot and verified that the performance and results are acceptable?
    As a rule of thumb, predicates specific to the query should take no more than 1 minute, and for simple queries be under 10 seconds. If this is not the case, this should be highlighted and agreed in the code review process.
  • Does the query have an appropriate level of in-query comments/documentation?
  • Have you considered/identified possible edge cases?
  • Does the query not reinvent features in the standard library?
  • Can the query be simplified further (not golfed!)

…ons.

Also add a new AlertReporting shared query library for deduplicating results
across macro definitions/invocations/etc.

Split __attribute__((unused)) variables (and similar) to a Strict pair of queries.
Copy link
Collaborator

@lcartey lcartey left a comment

Choose a reason for hiding this comment

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

Thanks! Providing some initial feedback. I still need to review the deduplicate macro code.

@MichaelRFairhurst
Copy link
Contributor Author

Apologies, I forgot I had unaddressed feedback here! Incorporated.

Copy link
Collaborator

@lcartey lcartey left a comment

Choose a reason for hiding this comment

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

Thanks! A few minor housekeeping bits, and some suggestions around the macro deduplication pieces.

@@ -51,6 +51,8 @@ Each coding standard consists of a list of "guidelines", however not all the gui

For some of the rules which are not amenable to static analysis, we may opt to provide a query which aids with "auditing" the rules. For example, AUTOSAR includes a rule (A10-0-1) "Public inheritance shall be used to implement 'is-a' relationship.". This is not directly amenable to static analysis, because it requires external context around the concept being modeled. However, we can provide an "audit" rule which reports all the public and private inheritance relationships in the program, so they can be manually verified.

For other rules, there may be means of indicating that a contravention is intentional, and where requiring a _devation report_ may be extra burdensome on developers and require double-entry. These results should be reported under a "strict" query. For instance, `RULE-2-8` "A project should not contain unused object definitions," where adding `__attribute__((unused))` may be preferable in order to suppress compiler warnings (which _deviation reports_ do not do) and are highly indicative of an intentional contravention by a developer.
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a versioning header that needs to be updated at the top of this document.

@@ -74,6 +74,7 @@ For each rule we therefore identify whether it is supportable or not. Furthermor

- **Automated** - the queries for the rule find contraventions directly.
- **Audit only** - the queries for the rule does not find contraventions directly, but instead report a list of _candidates_ that can be used as input into a manual audit. For example, `A10-0-1` (_Public inheritance shall be used to implement 'is-a' relationship_) is not directly amenable to static analysis, but CodeQL can be used to produce a list of all the locations that use public inheritance so they can be manually reviewed.
- **Strict only** - the queries for the rule find contraventions directly, but find results which are strongly indicated to be intentional, and where adding a _deviation report_ may be extra burden on developers. For example, in `RULE-2-8` (_A project should not contain unused object definitions_), declaring objects with `__attribute__((unused))` may be preferable to a _deviation report_, which will not suppress relevant compiler warnings, and therefore would otherwise require developer double-entry.
Copy link
Collaborator

Choose a reason for hiding this comment

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

There's a versioning header that needs to be updated at the top of this document.

}

string getMessageVariedResultInAllExpansions(Macro m) {
result = "Macro '" + m.getName() + "' defines unused object of varied names, for example, '$@'."
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggestion on wording:

Suggested change
result = "Macro '" + m.getName() + "' defines unused object of varied names, for example, '$@'."
result = "Macro '" + m.getName() + "' defines unused object with an invocation dependent name, for example, '$@'."

* report.getOptionalPlaceholderMessage()
* ```
*
* Note that this does *not* (currently) generate a result for elements not contained by a macro.
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm wondering if it would create a cleaner interface to do both in this library? That would reduce the number of queries required in this case to two queries, instead of four. It would also mean we could replace/integrate MacroUnwrapper, which is only currently used by one query.

* `ResultElement`, indicating that the real problem lies with that other `Macro` instead of with
* this particular invocation.
*/
class IsolatedMacroExpansionWithResultElement extends ResultMacroExpansion {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we should exclude LibraryMacros from this case, for consistency - as results grouped by a library macro will be excluded (because the location is outside the source archive).

See A5-2-2 for an example where we already do this.

* To do report such cases, either add support for that in this module, or write a separate query
* that reports `InvalidFoo` cases where not `.isInMacroExpansion()`.
*/
module DeduplicateMacroResults<
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe we could use this module in A5-2-2, which already has some manual handling for duplicate macro results, but could benefit from the improvements in this module. I don't think we need to do it in this PR, but should open an issue to track this as a follow up item.

class PotentiallyUnusedLocalVariable extends BasePotentiallyUnusedLocalVariable {
PotentiallyUnusedLocalVariable() {
// Ignore variables declared in macro expansions
not exists(DeclStmt ds | ds.getADeclaration() = this and ds.isInMacroExpansion())
Copy link
Collaborator

Choose a reason for hiding this comment

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

The existing queries simply ignore variables declared in macro expansions - we should add an issue to review backporting the macro handling capabilities from this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants