Skip to content

C++: fix typedef resolution in ArrayType #19805

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 6 commits into from
Jun 18, 2025
Merged

Conversation

IdrissRio
Copy link
Contributor

@IdrissRio IdrissRio commented Jun 17, 2025

This PR fixes typedef resolution for ArrayType by overriding the resolveTypedefs predicate to correctly resolve typedefs.

The new tests use type.getATypeNameUse() to restrict output to type names explicitly present in the test source. Without this, the output would include duplicated lines for each ArrayType encountered, such as int[10], increasing test output size unnecessarily and making it "hard" to debug.

The PR includes a change note.

@IdrissRio IdrissRio marked this pull request as ready for review June 18, 2025 06:49
@Copilot Copilot AI review requested due to automatic review settings June 18, 2025 06:49
@IdrissRio IdrissRio requested a review from a team as a code owner June 18, 2025 06:49
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures that ArrayType typedefs are correctly resolved by overriding its resolveTypedefs predicate, updates corresponding tests, and adds a change note.

  • Override resolveTypedefs in ArrayType to return a correctly resolved base type and size.
  • Add a CodeQL test (ArrayTypedefs.ql) and update expected outputs to cover array typedef resolution.
  • Record the behavior change in a change-note.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
cpp/ql/lib/semmle/code/cpp/Type.qll Added resolveTypedefs override in ArrayType to fix typedef resolution.
cpp/ql/test/library-tests/typedefs/ArrayTypedefs.ql New query selecting ArrayType with resolved typedefs.
cpp/ql/test/library-tests/typedefs/ArrayTypedefs.expected Updated expected results for array typedef resolution.
cpp/ql/test/library-tests/typedefs/Typedefs1.expected Extended expected output to include array typedefs in simple cases.
cpp/ql/test/library-tests/typedefs/Typedefs3.expected Added entries for int_t and float_t resolutions in existing tests.
cpp/ql/lib/change-notes/2025-06-17-arraytype-typedefs.md Documented the fix for ArrayType typedef resolution.
Comments suppressed due to low confidence (1)

cpp/ql/lib/semmle/code/cpp/Type.qll:1593

  • The override for resolveTypedefs in ArrayType sets only the base type and array size but does not propagate qualifiers (e.g., const/volatile) or the VLA flag. Consider invoking super.resolveTypedefs() or explicitly copying qualifiers and isVla() to ensure all modifiers are retained.
  override Type resolveTypedefs() {

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.

Two small comments, otherwise LGTM

Comment on lines 1 to 7
typedef int int_t;
int_t g1[10];
int_t g2[2][4];

typedef float float_t;
float_t arr1[5];
float_t (*a_pointer)[10];
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer if we move this test to its own directory, as it's testing resolveTypedefs and not typedefs per se. Given that we don't have any tests for resolveTypedefs, I would just use that for the directory name, so we can add other kinds of resolveTypedefs tests in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! Thank you. I saw different naming conventions for test folders, and I opted for snake_case, as it seems to be the most commonly used.

@IdrissRio IdrissRio force-pushed the idrissrio/namespace-attributes branch from c220844 to 72559d5 Compare June 18, 2025 08:12
@IdrissRio IdrissRio merged commit eff1fba into main Jun 18, 2025
12 of 15 checks passed
@IdrissRio IdrissRio deleted the idrissrio/namespace-attributes branch June 18, 2025 08:21
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.

2 participants