Skip to content

C#: Follow line directives when getting element location#5005

Merged
tamasvajk merged 35 commits into
github:mainfrom
tamasvajk:feature/follow-line
Feb 4, 2021
Merged

C#: Follow line directives when getting element location#5005
tamasvajk merged 35 commits into
github:mainfrom
tamasvajk:feature/follow-line

Conversation

@tamasvajk

@tamasvajk tamasvajk commented Jan 22, 2021

Copy link
Copy Markdown
Contributor

This PR builds on #4976.

The main goal of this PR is to use locations of QL entities in non-generated files when possible. For example .cshtml files are compiled into .cshtml.g.cs files, and issues were reported in these generated files as these are the ones passed to csc. With this PR, locations are following #line directives. In case of aspnetcore, .cshtml.g.cs files point to the original .cshtml files.

This PR:

  • improves #line directive extraction by including the referenced file in the source archive, and referencing the file in the directive entity,
  • includes the same change for #pragma checksum,
  • Element::getLocation was improved to take into account #line directives.

Missing

@github-actions github-actions Bot added the C# label Jan 22, 2021
@tamasvajk tamasvajk force-pushed the feature/follow-line branch from 85b0051 to ff6ad12 Compare January 22, 2021 14:08
result = e.getALocation().(SourceLocation) and
(
not exists(e.getALocation().(SourceLocation).getMappedLocation()) or
e instanceof LineDirective

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is needed for line directives that are setting back the default line counting behavior:

y.cs:
#line 5 "x.cs"
var i = 1;     // reported at x.cs, line 5
#line default  // reported at x.cs, line 6, but y.cs, line 3 makes more sense

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, that would probably be a bit nicer. Would I do it like this?

  Location getMappedLocation() {
    locations_mapped(this, result) and
    not exists(LineDirective l | l.getALocation() = this)
  }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, that looks correct.

Comment on lines 134 to 144
# 17| 4: [BlockStmt] {...}
# 19| 0: [LocalVariableDeclStmt] ... ...;
# 19| 0: [LocalVariableDeclExpr] Int32 i
comments1.cs:
# 1| 0: [LocalVariableDeclStmt] ... ...;
# 1| 0: [LocalVariableDeclExpr] Int32 i
trivia.cs:
# 19| 0: [TypeMention] int
# 20| 1: [LocalVariableDeclStmt] ... ...;
# 20| 0: [LocalVariableDeclExpr] Int32 j
comments1.cs:
# 2| 1: [LocalVariableDeclStmt] ... ...;
# 2| 0: [LocalVariableDeclExpr] Int32 j
trivia.cs:
# 20| 0: [TypeMention] int

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what to do with this. The AST viewer is now also following the #line directives, which maybe makes sense. At the same time, it feels a bit strange that nodes in a tree might belong to different files.

@tamasvajk tamasvajk force-pushed the feature/follow-line branch from ff6ad12 to 8f1482d Compare January 22, 2021 14:43
@tamasvajk tamasvajk marked this pull request as ready for review January 25, 2021 09:06
@tamasvajk tamasvajk requested a review from a team as a code owner January 25, 2021 09:06
@tamasvajk tamasvajk force-pushed the feature/follow-line branch 2 times, most recently from e005a74 to 2f51845 Compare January 28, 2021 10:35

@hvitved hvitved left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great; only one question.

result = e.getALocation().(SourceLocation) and
(
not exists(e.getALocation().(SourceLocation).getMappedLocation()) or
e instanceof LineDirective

This comment was marked as resolved.

hvitved
hvitved previously approved these changes Feb 2, 2021
@tamasvajk

Copy link
Copy Markdown
Contributor Author

@hvitved The latest differences job finished. It seems okay. Now issues show up in non-cs files, like this missing ternary opportunity here:
https://github.com/mono/mono/blob/69dbe53ae83148f4b65e0fa0bcf100b381c1345a/mcs/ilasm/parser/ILParser.jay#L863

There are three new extraction errors, all relating to reading a file. I pushed an extra commit to fix the logged message.

@tamasvajk tamasvajk force-pushed the feature/follow-line branch from b7e6ffa to 88d1539 Compare February 4, 2021 07:42
@tamasvajk

Copy link
Copy Markdown
Contributor Author

I've rebased this PR and pushed a fix for the failing test.

@tamasvajk tamasvajk merged commit ca992f2 into github:main Feb 4, 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.

2 participants