Skip to content

Conversation

michaelnebel
Copy link
Contributor

@michaelnebel michaelnebel commented Sep 25, 2025

In this PR we change, how locations are extracted.
That is,

  • We only extract the locations of unbound declarations and uses the locations. This is to avoid extracting locations multiple times (if we should ever be able to use * IDs for extracting locations.
  • We explicitly avoid extracting empty locations (as this is calculated as best location in the QL code anyway).
  • We explicitly extract the "empty location" as the QL library assumes that this is always extracted.

With this change we do get a minor change to what locations are reported.

DCA shows "small" alerts discrepancies for

  • cs/dispose-not-called-on-throw
  • cs/dereferenced-value-may-be-null

I investigated the discrepancy for cs/dispose-not-called-on-throw for ASP.NET Core and it appears to boil down to the following issue: When extracting methods, we now only extract locations for unbound declarations and then use these locations for the method. However, it appears that we may extract multiple locations for unbound callables - both a location in source (for the override) and an assembly location (for the prototype). This could affect that the "best location" of a method goes from assembly to source (which could affect logic that uses whether a callable is in source or library).
For the cases I investigated for cs/dispose-not-called-on-throw it appears that some results are removed, where it is assumed that library methods can throw any exception - as this is just an approximation based on location - I think we can accept the discrepancy.

@github-actions github-actions bot added the C# label Sep 25, 2025
@michaelnebel michaelnebel force-pushed the csharp/reducelocationtuples branch 3 times, most recently from 295307c to 01b789d Compare September 29, 2025 09:37
@michaelnebel michaelnebel force-pushed the csharp/reducelocationtuples branch from 457e9b3 to 018ccb3 Compare September 30, 2025 09:37
@michaelnebel
Copy link
Contributor Author

@hvitved : Could you review, even though still i draft?

@hvitved
Copy link
Contributor

hvitved commented Oct 1, 2025

FYI: The most recent DCA run forgot the -X 'use-database-cache=false' -X 'change-ql-submodule-in-semmle-code=true' -X 'silent=false' arguments.

hvitved
hvitved previously approved these changes Oct 1, 2025
Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

Changes LGTM.

@michaelnebel
Copy link
Contributor Author

FYI: The most recent DCA run forgot the -X 'use-database-cache=false' -X 'change-ql-submodule-in-semmle-code=true' -X 'silent=false' arguments.

Ran it again, DCA reports the same alert changes. Performance looks good.
Will add a change note.

@michaelnebel michaelnebel marked this pull request as ready for review October 2, 2025 09:35
@michaelnebel michaelnebel requested a review from a team as a code owner October 2, 2025 09:35
@Copilot Copilot AI review requested due to automatic review settings October 2, 2025 09:35
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 optimizes location extraction for C# entities by only extracting locations for unbound declarations to avoid duplication and improve performance. The changes ensure that bound generic entities reuse the location information from their unbound counterparts.

  • Modified location extraction logic to use unbound declarations instead of extracting multiple times for bound generics
  • Added utility methods to filter out empty locations during trap file writing
  • Renamed GeneratedLocation to EmptyLocation for clarity and ensured it's always extracted

Reviewed Changes

Copilot reviewed 36 out of 36 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
csharp/ql/lib/semmle/code/csharp/*.qll Updated location retrieval methods to use unbound declarations
csharp/extractor/Semmle.Extraction.CSharp/Entities/*.cs Added conditional location extraction and helper methods for filtering empty locations
csharp/extractor/Semmle.Extraction.CSharp/Entities/Locations/EmptyLocation.cs Renamed from GeneratedLocation to EmptyLocation
csharp/extractor/Semmle.Extraction.CSharp/Extractor/*.cs Updated context methods and ensured empty location creation
csharp/ql/test/library-tests/locations/* Test files for verifying location extraction behavior

if (attributeSyntax is not null)
{
trapFile.attribute_location(this, Assembly.CreateOutputAssembly(Context));
WriteLocationToTrap(trapFile.attribute_location, this, Assembly.CreateOutputAssembly(Context));
Copy link
Preview

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

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

This call to WriteLocationToTrap appears incorrect. The second parameter should be the location, but Assembly.CreateOutputAssembly(Context) returns an Assembly entity, not a Location. This will likely cause a compilation error or runtime issue.

Suggested change
WriteLocationToTrap(trapFile.attribute_location, this, Assembly.CreateOutputAssembly(Context));
WriteLocationToTrap(trapFile.attribute_location, this, attributeSyntax.GetLocation());

Copilot uses AI. Check for mistakes.

public override void Populate(TextWriter trapFile)
{
var typeKey = VarargsType.Create(Context);
// !! Maybe originaldefinition is wrong
Copy link
Preview

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

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

The comment has a spelling error: 'originaldefinition' should be 'original definition' (two words).

Suggested change
// !! Maybe originaldefinition is wrong
// !! Maybe original definition is wrong

Copilot uses AI. Check for mistakes.

@hvitved
Copy link
Contributor

hvitved commented Oct 2, 2025

DCA confirms a reduction in TRAP size,

@michaelnebel michaelnebel merged commit b5aa972 into github:main Oct 2, 2025
25 checks passed
@michaelnebel michaelnebel deleted the csharp/reducelocationtuples branch October 2, 2025 10:32
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